intel_display.c 462 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402
  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 <drm/drm_atomic_uapi.h>
  49. #include <linux/dma_remapping.h>
  50. #include <linux/reservation.h>
  51. /* Primary plane formats for gen <= 3 */
  52. static const uint32_t i8xx_primary_formats[] = {
  53. DRM_FORMAT_C8,
  54. DRM_FORMAT_RGB565,
  55. DRM_FORMAT_XRGB1555,
  56. DRM_FORMAT_XRGB8888,
  57. };
  58. /* Primary plane formats for gen >= 4 */
  59. static const uint32_t i965_primary_formats[] = {
  60. DRM_FORMAT_C8,
  61. DRM_FORMAT_RGB565,
  62. DRM_FORMAT_XRGB8888,
  63. DRM_FORMAT_XBGR8888,
  64. DRM_FORMAT_XRGB2101010,
  65. DRM_FORMAT_XBGR2101010,
  66. };
  67. static const uint64_t i9xx_format_modifiers[] = {
  68. I915_FORMAT_MOD_X_TILED,
  69. DRM_FORMAT_MOD_LINEAR,
  70. DRM_FORMAT_MOD_INVALID
  71. };
  72. static const uint32_t skl_primary_formats[] = {
  73. DRM_FORMAT_C8,
  74. DRM_FORMAT_RGB565,
  75. DRM_FORMAT_XRGB8888,
  76. DRM_FORMAT_XBGR8888,
  77. DRM_FORMAT_ARGB8888,
  78. DRM_FORMAT_ABGR8888,
  79. DRM_FORMAT_XRGB2101010,
  80. DRM_FORMAT_XBGR2101010,
  81. DRM_FORMAT_YUYV,
  82. DRM_FORMAT_YVYU,
  83. DRM_FORMAT_UYVY,
  84. DRM_FORMAT_VYUY,
  85. };
  86. static const uint32_t skl_pri_planar_formats[] = {
  87. DRM_FORMAT_C8,
  88. DRM_FORMAT_RGB565,
  89. DRM_FORMAT_XRGB8888,
  90. DRM_FORMAT_XBGR8888,
  91. DRM_FORMAT_ARGB8888,
  92. DRM_FORMAT_ABGR8888,
  93. DRM_FORMAT_XRGB2101010,
  94. DRM_FORMAT_XBGR2101010,
  95. DRM_FORMAT_YUYV,
  96. DRM_FORMAT_YVYU,
  97. DRM_FORMAT_UYVY,
  98. DRM_FORMAT_VYUY,
  99. DRM_FORMAT_NV12,
  100. };
  101. static const uint64_t skl_format_modifiers_noccs[] = {
  102. I915_FORMAT_MOD_Yf_TILED,
  103. I915_FORMAT_MOD_Y_TILED,
  104. I915_FORMAT_MOD_X_TILED,
  105. DRM_FORMAT_MOD_LINEAR,
  106. DRM_FORMAT_MOD_INVALID
  107. };
  108. static const uint64_t skl_format_modifiers_ccs[] = {
  109. I915_FORMAT_MOD_Yf_TILED_CCS,
  110. I915_FORMAT_MOD_Y_TILED_CCS,
  111. I915_FORMAT_MOD_Yf_TILED,
  112. I915_FORMAT_MOD_Y_TILED,
  113. I915_FORMAT_MOD_X_TILED,
  114. DRM_FORMAT_MOD_LINEAR,
  115. DRM_FORMAT_MOD_INVALID
  116. };
  117. /* Cursor formats */
  118. static const uint32_t intel_cursor_formats[] = {
  119. DRM_FORMAT_ARGB8888,
  120. };
  121. static const uint64_t cursor_format_modifiers[] = {
  122. DRM_FORMAT_MOD_LINEAR,
  123. DRM_FORMAT_MOD_INVALID
  124. };
  125. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  126. struct intel_crtc_state *pipe_config);
  127. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  128. struct intel_crtc_state *pipe_config);
  129. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  130. struct drm_i915_gem_object *obj,
  131. struct drm_mode_fb_cmd2 *mode_cmd);
  132. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  133. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  134. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  135. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  136. struct intel_link_m_n *m_n,
  137. struct intel_link_m_n *m2_n2);
  138. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  139. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  140. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  141. static void vlv_prepare_pll(struct intel_crtc *crtc,
  142. const struct intel_crtc_state *pipe_config);
  143. static void chv_prepare_pll(struct intel_crtc *crtc,
  144. const struct intel_crtc_state *pipe_config);
  145. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  146. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  147. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  148. struct intel_crtc_state *crtc_state);
  149. static void skylake_pfit_enable(struct intel_crtc *crtc);
  150. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  151. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  152. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  153. struct drm_modeset_acquire_ctx *ctx);
  154. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  155. struct intel_limit {
  156. struct {
  157. int min, max;
  158. } dot, vco, n, m, m1, m2, p, p1;
  159. struct {
  160. int dot_limit;
  161. int p2_slow, p2_fast;
  162. } p2;
  163. };
  164. /* returns HPLL frequency in kHz */
  165. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  166. {
  167. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  168. /* Obtain SKU information */
  169. mutex_lock(&dev_priv->sb_lock);
  170. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  171. CCK_FUSE_HPLL_FREQ_MASK;
  172. mutex_unlock(&dev_priv->sb_lock);
  173. return vco_freq[hpll_freq] * 1000;
  174. }
  175. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  176. const char *name, u32 reg, int ref_freq)
  177. {
  178. u32 val;
  179. int divider;
  180. mutex_lock(&dev_priv->sb_lock);
  181. val = vlv_cck_read(dev_priv, reg);
  182. mutex_unlock(&dev_priv->sb_lock);
  183. divider = val & CCK_FREQUENCY_VALUES;
  184. WARN((val & CCK_FREQUENCY_STATUS) !=
  185. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  186. "%s change in progress\n", name);
  187. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  188. }
  189. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  190. const char *name, u32 reg)
  191. {
  192. if (dev_priv->hpll_freq == 0)
  193. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  194. return vlv_get_cck_clock(dev_priv, name, reg,
  195. dev_priv->hpll_freq);
  196. }
  197. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  198. {
  199. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  200. return;
  201. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  202. CCK_CZ_CLOCK_CONTROL);
  203. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  204. }
  205. static inline u32 /* units of 100MHz */
  206. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  207. const struct intel_crtc_state *pipe_config)
  208. {
  209. if (HAS_DDI(dev_priv))
  210. return pipe_config->port_clock; /* SPLL */
  211. else
  212. return dev_priv->fdi_pll_freq;
  213. }
  214. static const struct intel_limit intel_limits_i8xx_dac = {
  215. .dot = { .min = 25000, .max = 350000 },
  216. .vco = { .min = 908000, .max = 1512000 },
  217. .n = { .min = 2, .max = 16 },
  218. .m = { .min = 96, .max = 140 },
  219. .m1 = { .min = 18, .max = 26 },
  220. .m2 = { .min = 6, .max = 16 },
  221. .p = { .min = 4, .max = 128 },
  222. .p1 = { .min = 2, .max = 33 },
  223. .p2 = { .dot_limit = 165000,
  224. .p2_slow = 4, .p2_fast = 2 },
  225. };
  226. static const struct intel_limit intel_limits_i8xx_dvo = {
  227. .dot = { .min = 25000, .max = 350000 },
  228. .vco = { .min = 908000, .max = 1512000 },
  229. .n = { .min = 2, .max = 16 },
  230. .m = { .min = 96, .max = 140 },
  231. .m1 = { .min = 18, .max = 26 },
  232. .m2 = { .min = 6, .max = 16 },
  233. .p = { .min = 4, .max = 128 },
  234. .p1 = { .min = 2, .max = 33 },
  235. .p2 = { .dot_limit = 165000,
  236. .p2_slow = 4, .p2_fast = 4 },
  237. };
  238. static const struct intel_limit intel_limits_i8xx_lvds = {
  239. .dot = { .min = 25000, .max = 350000 },
  240. .vco = { .min = 908000, .max = 1512000 },
  241. .n = { .min = 2, .max = 16 },
  242. .m = { .min = 96, .max = 140 },
  243. .m1 = { .min = 18, .max = 26 },
  244. .m2 = { .min = 6, .max = 16 },
  245. .p = { .min = 4, .max = 128 },
  246. .p1 = { .min = 1, .max = 6 },
  247. .p2 = { .dot_limit = 165000,
  248. .p2_slow = 14, .p2_fast = 7 },
  249. };
  250. static const struct intel_limit intel_limits_i9xx_sdvo = {
  251. .dot = { .min = 20000, .max = 400000 },
  252. .vco = { .min = 1400000, .max = 2800000 },
  253. .n = { .min = 1, .max = 6 },
  254. .m = { .min = 70, .max = 120 },
  255. .m1 = { .min = 8, .max = 18 },
  256. .m2 = { .min = 3, .max = 7 },
  257. .p = { .min = 5, .max = 80 },
  258. .p1 = { .min = 1, .max = 8 },
  259. .p2 = { .dot_limit = 200000,
  260. .p2_slow = 10, .p2_fast = 5 },
  261. };
  262. static const struct intel_limit intel_limits_i9xx_lvds = {
  263. .dot = { .min = 20000, .max = 400000 },
  264. .vco = { .min = 1400000, .max = 2800000 },
  265. .n = { .min = 1, .max = 6 },
  266. .m = { .min = 70, .max = 120 },
  267. .m1 = { .min = 8, .max = 18 },
  268. .m2 = { .min = 3, .max = 7 },
  269. .p = { .min = 7, .max = 98 },
  270. .p1 = { .min = 1, .max = 8 },
  271. .p2 = { .dot_limit = 112000,
  272. .p2_slow = 14, .p2_fast = 7 },
  273. };
  274. static const struct intel_limit intel_limits_g4x_sdvo = {
  275. .dot = { .min = 25000, .max = 270000 },
  276. .vco = { .min = 1750000, .max = 3500000},
  277. .n = { .min = 1, .max = 4 },
  278. .m = { .min = 104, .max = 138 },
  279. .m1 = { .min = 17, .max = 23 },
  280. .m2 = { .min = 5, .max = 11 },
  281. .p = { .min = 10, .max = 30 },
  282. .p1 = { .min = 1, .max = 3},
  283. .p2 = { .dot_limit = 270000,
  284. .p2_slow = 10,
  285. .p2_fast = 10
  286. },
  287. };
  288. static const struct intel_limit intel_limits_g4x_hdmi = {
  289. .dot = { .min = 22000, .max = 400000 },
  290. .vco = { .min = 1750000, .max = 3500000},
  291. .n = { .min = 1, .max = 4 },
  292. .m = { .min = 104, .max = 138 },
  293. .m1 = { .min = 16, .max = 23 },
  294. .m2 = { .min = 5, .max = 11 },
  295. .p = { .min = 5, .max = 80 },
  296. .p1 = { .min = 1, .max = 8},
  297. .p2 = { .dot_limit = 165000,
  298. .p2_slow = 10, .p2_fast = 5 },
  299. };
  300. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  301. .dot = { .min = 20000, .max = 115000 },
  302. .vco = { .min = 1750000, .max = 3500000 },
  303. .n = { .min = 1, .max = 3 },
  304. .m = { .min = 104, .max = 138 },
  305. .m1 = { .min = 17, .max = 23 },
  306. .m2 = { .min = 5, .max = 11 },
  307. .p = { .min = 28, .max = 112 },
  308. .p1 = { .min = 2, .max = 8 },
  309. .p2 = { .dot_limit = 0,
  310. .p2_slow = 14, .p2_fast = 14
  311. },
  312. };
  313. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  314. .dot = { .min = 80000, .max = 224000 },
  315. .vco = { .min = 1750000, .max = 3500000 },
  316. .n = { .min = 1, .max = 3 },
  317. .m = { .min = 104, .max = 138 },
  318. .m1 = { .min = 17, .max = 23 },
  319. .m2 = { .min = 5, .max = 11 },
  320. .p = { .min = 14, .max = 42 },
  321. .p1 = { .min = 2, .max = 6 },
  322. .p2 = { .dot_limit = 0,
  323. .p2_slow = 7, .p2_fast = 7
  324. },
  325. };
  326. static const struct intel_limit intel_limits_pineview_sdvo = {
  327. .dot = { .min = 20000, .max = 400000},
  328. .vco = { .min = 1700000, .max = 3500000 },
  329. /* Pineview's Ncounter is a ring counter */
  330. .n = { .min = 3, .max = 6 },
  331. .m = { .min = 2, .max = 256 },
  332. /* Pineview only has one combined m divider, which we treat as m2. */
  333. .m1 = { .min = 0, .max = 0 },
  334. .m2 = { .min = 0, .max = 254 },
  335. .p = { .min = 5, .max = 80 },
  336. .p1 = { .min = 1, .max = 8 },
  337. .p2 = { .dot_limit = 200000,
  338. .p2_slow = 10, .p2_fast = 5 },
  339. };
  340. static const struct intel_limit intel_limits_pineview_lvds = {
  341. .dot = { .min = 20000, .max = 400000 },
  342. .vco = { .min = 1700000, .max = 3500000 },
  343. .n = { .min = 3, .max = 6 },
  344. .m = { .min = 2, .max = 256 },
  345. .m1 = { .min = 0, .max = 0 },
  346. .m2 = { .min = 0, .max = 254 },
  347. .p = { .min = 7, .max = 112 },
  348. .p1 = { .min = 1, .max = 8 },
  349. .p2 = { .dot_limit = 112000,
  350. .p2_slow = 14, .p2_fast = 14 },
  351. };
  352. /* Ironlake / Sandybridge
  353. *
  354. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  355. * the range value for them is (actual_value - 2).
  356. */
  357. static const struct intel_limit intel_limits_ironlake_dac = {
  358. .dot = { .min = 25000, .max = 350000 },
  359. .vco = { .min = 1760000, .max = 3510000 },
  360. .n = { .min = 1, .max = 5 },
  361. .m = { .min = 79, .max = 127 },
  362. .m1 = { .min = 12, .max = 22 },
  363. .m2 = { .min = 5, .max = 9 },
  364. .p = { .min = 5, .max = 80 },
  365. .p1 = { .min = 1, .max = 8 },
  366. .p2 = { .dot_limit = 225000,
  367. .p2_slow = 10, .p2_fast = 5 },
  368. };
  369. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  370. .dot = { .min = 25000, .max = 350000 },
  371. .vco = { .min = 1760000, .max = 3510000 },
  372. .n = { .min = 1, .max = 3 },
  373. .m = { .min = 79, .max = 118 },
  374. .m1 = { .min = 12, .max = 22 },
  375. .m2 = { .min = 5, .max = 9 },
  376. .p = { .min = 28, .max = 112 },
  377. .p1 = { .min = 2, .max = 8 },
  378. .p2 = { .dot_limit = 225000,
  379. .p2_slow = 14, .p2_fast = 14 },
  380. };
  381. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  382. .dot = { .min = 25000, .max = 350000 },
  383. .vco = { .min = 1760000, .max = 3510000 },
  384. .n = { .min = 1, .max = 3 },
  385. .m = { .min = 79, .max = 127 },
  386. .m1 = { .min = 12, .max = 22 },
  387. .m2 = { .min = 5, .max = 9 },
  388. .p = { .min = 14, .max = 56 },
  389. .p1 = { .min = 2, .max = 8 },
  390. .p2 = { .dot_limit = 225000,
  391. .p2_slow = 7, .p2_fast = 7 },
  392. };
  393. /* LVDS 100mhz refclk limits. */
  394. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  395. .dot = { .min = 25000, .max = 350000 },
  396. .vco = { .min = 1760000, .max = 3510000 },
  397. .n = { .min = 1, .max = 2 },
  398. .m = { .min = 79, .max = 126 },
  399. .m1 = { .min = 12, .max = 22 },
  400. .m2 = { .min = 5, .max = 9 },
  401. .p = { .min = 28, .max = 112 },
  402. .p1 = { .min = 2, .max = 8 },
  403. .p2 = { .dot_limit = 225000,
  404. .p2_slow = 14, .p2_fast = 14 },
  405. };
  406. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  407. .dot = { .min = 25000, .max = 350000 },
  408. .vco = { .min = 1760000, .max = 3510000 },
  409. .n = { .min = 1, .max = 3 },
  410. .m = { .min = 79, .max = 126 },
  411. .m1 = { .min = 12, .max = 22 },
  412. .m2 = { .min = 5, .max = 9 },
  413. .p = { .min = 14, .max = 42 },
  414. .p1 = { .min = 2, .max = 6 },
  415. .p2 = { .dot_limit = 225000,
  416. .p2_slow = 7, .p2_fast = 7 },
  417. };
  418. static const struct intel_limit intel_limits_vlv = {
  419. /*
  420. * These are the data rate limits (measured in fast clocks)
  421. * since those are the strictest limits we have. The fast
  422. * clock and actual rate limits are more relaxed, so checking
  423. * them would make no difference.
  424. */
  425. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  426. .vco = { .min = 4000000, .max = 6000000 },
  427. .n = { .min = 1, .max = 7 },
  428. .m1 = { .min = 2, .max = 3 },
  429. .m2 = { .min = 11, .max = 156 },
  430. .p1 = { .min = 2, .max = 3 },
  431. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  432. };
  433. static const struct intel_limit intel_limits_chv = {
  434. /*
  435. * These are the data rate limits (measured in fast clocks)
  436. * since those are the strictest limits we have. The fast
  437. * clock and actual rate limits are more relaxed, so checking
  438. * them would make no difference.
  439. */
  440. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  441. .vco = { .min = 4800000, .max = 6480000 },
  442. .n = { .min = 1, .max = 1 },
  443. .m1 = { .min = 2, .max = 2 },
  444. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  445. .p1 = { .min = 2, .max = 4 },
  446. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  447. };
  448. static const struct intel_limit intel_limits_bxt = {
  449. /* FIXME: find real dot limits */
  450. .dot = { .min = 0, .max = INT_MAX },
  451. .vco = { .min = 4800000, .max = 6700000 },
  452. .n = { .min = 1, .max = 1 },
  453. .m1 = { .min = 2, .max = 2 },
  454. /* FIXME: find real m2 limits */
  455. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  456. .p1 = { .min = 2, .max = 4 },
  457. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  458. };
  459. static void
  460. skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
  461. {
  462. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  463. return;
  464. if (enable)
  465. I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
  466. else
  467. I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
  468. }
  469. static void
  470. skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
  471. {
  472. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  473. return;
  474. if (enable)
  475. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  476. DUPS1_GATING_DIS | DUPS2_GATING_DIS);
  477. else
  478. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  479. I915_READ(CLKGATE_DIS_PSL(pipe)) &
  480. ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
  481. }
  482. static bool
  483. needs_modeset(const struct drm_crtc_state *state)
  484. {
  485. return drm_atomic_crtc_needs_modeset(state);
  486. }
  487. /*
  488. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  489. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  490. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  491. * The helpers' return value is the rate of the clock that is fed to the
  492. * display engine's pipe which can be the above fast dot clock rate or a
  493. * divided-down version of it.
  494. */
  495. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  496. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  497. {
  498. clock->m = clock->m2 + 2;
  499. clock->p = clock->p1 * clock->p2;
  500. if (WARN_ON(clock->n == 0 || clock->p == 0))
  501. return 0;
  502. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  503. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  504. return clock->dot;
  505. }
  506. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  507. {
  508. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  509. }
  510. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  511. {
  512. clock->m = i9xx_dpll_compute_m(clock);
  513. clock->p = clock->p1 * clock->p2;
  514. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  515. return 0;
  516. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  517. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  518. return clock->dot;
  519. }
  520. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  521. {
  522. clock->m = clock->m1 * clock->m2;
  523. clock->p = clock->p1 * clock->p2;
  524. if (WARN_ON(clock->n == 0 || clock->p == 0))
  525. return 0;
  526. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  527. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  528. return clock->dot / 5;
  529. }
  530. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  531. {
  532. clock->m = clock->m1 * clock->m2;
  533. clock->p = clock->p1 * clock->p2;
  534. if (WARN_ON(clock->n == 0 || clock->p == 0))
  535. return 0;
  536. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  537. clock->n << 22);
  538. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  539. return clock->dot / 5;
  540. }
  541. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  542. /*
  543. * Returns whether the given set of divisors are valid for a given refclk with
  544. * the given connectors.
  545. */
  546. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  547. const struct intel_limit *limit,
  548. const struct dpll *clock)
  549. {
  550. if (clock->n < limit->n.min || limit->n.max < clock->n)
  551. INTELPllInvalid("n out of range\n");
  552. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  553. INTELPllInvalid("p1 out of range\n");
  554. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  555. INTELPllInvalid("m2 out of range\n");
  556. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  557. INTELPllInvalid("m1 out of range\n");
  558. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  559. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  560. if (clock->m1 <= clock->m2)
  561. INTELPllInvalid("m1 <= m2\n");
  562. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  563. !IS_GEN9_LP(dev_priv)) {
  564. if (clock->p < limit->p.min || limit->p.max < clock->p)
  565. INTELPllInvalid("p out of range\n");
  566. if (clock->m < limit->m.min || limit->m.max < clock->m)
  567. INTELPllInvalid("m out of range\n");
  568. }
  569. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  570. INTELPllInvalid("vco out of range\n");
  571. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  572. * connector, etc., rather than just a single range.
  573. */
  574. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  575. INTELPllInvalid("dot out of range\n");
  576. return true;
  577. }
  578. static int
  579. i9xx_select_p2_div(const struct intel_limit *limit,
  580. const struct intel_crtc_state *crtc_state,
  581. int target)
  582. {
  583. struct drm_device *dev = crtc_state->base.crtc->dev;
  584. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  585. /*
  586. * For LVDS just rely on its current settings for dual-channel.
  587. * We haven't figured out how to reliably set up different
  588. * single/dual channel state, if we even can.
  589. */
  590. if (intel_is_dual_link_lvds(dev))
  591. return limit->p2.p2_fast;
  592. else
  593. return limit->p2.p2_slow;
  594. } else {
  595. if (target < limit->p2.dot_limit)
  596. return limit->p2.p2_slow;
  597. else
  598. return limit->p2.p2_fast;
  599. }
  600. }
  601. /*
  602. * Returns a set of divisors for the desired target clock with the given
  603. * refclk, or FALSE. The returned values represent the clock equation:
  604. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  605. *
  606. * Target and reference clocks are specified in kHz.
  607. *
  608. * If match_clock is provided, then best_clock P divider must match the P
  609. * divider from @match_clock used for LVDS downclocking.
  610. */
  611. static bool
  612. i9xx_find_best_dpll(const struct intel_limit *limit,
  613. struct intel_crtc_state *crtc_state,
  614. int target, int refclk, struct dpll *match_clock,
  615. struct dpll *best_clock)
  616. {
  617. struct drm_device *dev = crtc_state->base.crtc->dev;
  618. struct dpll clock;
  619. int err = target;
  620. memset(best_clock, 0, sizeof(*best_clock));
  621. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  622. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  623. clock.m1++) {
  624. for (clock.m2 = limit->m2.min;
  625. clock.m2 <= limit->m2.max; clock.m2++) {
  626. if (clock.m2 >= clock.m1)
  627. break;
  628. for (clock.n = limit->n.min;
  629. clock.n <= limit->n.max; clock.n++) {
  630. for (clock.p1 = limit->p1.min;
  631. clock.p1 <= limit->p1.max; clock.p1++) {
  632. int this_err;
  633. i9xx_calc_dpll_params(refclk, &clock);
  634. if (!intel_PLL_is_valid(to_i915(dev),
  635. limit,
  636. &clock))
  637. continue;
  638. if (match_clock &&
  639. clock.p != match_clock->p)
  640. continue;
  641. this_err = abs(clock.dot - target);
  642. if (this_err < err) {
  643. *best_clock = clock;
  644. err = this_err;
  645. }
  646. }
  647. }
  648. }
  649. }
  650. return (err != target);
  651. }
  652. /*
  653. * Returns a set of divisors for the desired target clock with the given
  654. * refclk, or FALSE. The returned values represent the clock equation:
  655. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  656. *
  657. * Target and reference clocks are specified in kHz.
  658. *
  659. * If match_clock is provided, then best_clock P divider must match the P
  660. * divider from @match_clock used for LVDS downclocking.
  661. */
  662. static bool
  663. pnv_find_best_dpll(const struct intel_limit *limit,
  664. struct intel_crtc_state *crtc_state,
  665. int target, int refclk, struct dpll *match_clock,
  666. struct dpll *best_clock)
  667. {
  668. struct drm_device *dev = crtc_state->base.crtc->dev;
  669. struct dpll clock;
  670. int err = target;
  671. memset(best_clock, 0, sizeof(*best_clock));
  672. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  673. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  674. clock.m1++) {
  675. for (clock.m2 = limit->m2.min;
  676. clock.m2 <= limit->m2.max; clock.m2++) {
  677. for (clock.n = limit->n.min;
  678. clock.n <= limit->n.max; clock.n++) {
  679. for (clock.p1 = limit->p1.min;
  680. clock.p1 <= limit->p1.max; clock.p1++) {
  681. int this_err;
  682. pnv_calc_dpll_params(refclk, &clock);
  683. if (!intel_PLL_is_valid(to_i915(dev),
  684. limit,
  685. &clock))
  686. continue;
  687. if (match_clock &&
  688. clock.p != match_clock->p)
  689. continue;
  690. this_err = abs(clock.dot - target);
  691. if (this_err < err) {
  692. *best_clock = clock;
  693. err = this_err;
  694. }
  695. }
  696. }
  697. }
  698. }
  699. return (err != target);
  700. }
  701. /*
  702. * Returns a set of divisors for the desired target clock with the given
  703. * refclk, or FALSE. The returned values represent the clock equation:
  704. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  705. *
  706. * Target and reference clocks are specified in kHz.
  707. *
  708. * If match_clock is provided, then best_clock P divider must match the P
  709. * divider from @match_clock used for LVDS downclocking.
  710. */
  711. static bool
  712. g4x_find_best_dpll(const struct intel_limit *limit,
  713. struct intel_crtc_state *crtc_state,
  714. int target, int refclk, struct dpll *match_clock,
  715. struct dpll *best_clock)
  716. {
  717. struct drm_device *dev = crtc_state->base.crtc->dev;
  718. struct dpll clock;
  719. int max_n;
  720. bool found = false;
  721. /* approximately equals target * 0.00585 */
  722. int err_most = (target >> 8) + (target >> 9);
  723. memset(best_clock, 0, sizeof(*best_clock));
  724. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  725. max_n = limit->n.max;
  726. /* based on hardware requirement, prefer smaller n to precision */
  727. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  728. /* based on hardware requirement, prefere larger m1,m2 */
  729. for (clock.m1 = limit->m1.max;
  730. clock.m1 >= limit->m1.min; clock.m1--) {
  731. for (clock.m2 = limit->m2.max;
  732. clock.m2 >= limit->m2.min; clock.m2--) {
  733. for (clock.p1 = limit->p1.max;
  734. clock.p1 >= limit->p1.min; clock.p1--) {
  735. int this_err;
  736. i9xx_calc_dpll_params(refclk, &clock);
  737. if (!intel_PLL_is_valid(to_i915(dev),
  738. limit,
  739. &clock))
  740. continue;
  741. this_err = abs(clock.dot - target);
  742. if (this_err < err_most) {
  743. *best_clock = clock;
  744. err_most = this_err;
  745. max_n = clock.n;
  746. found = true;
  747. }
  748. }
  749. }
  750. }
  751. }
  752. return found;
  753. }
  754. /*
  755. * Check if the calculated PLL configuration is more optimal compared to the
  756. * best configuration and error found so far. Return the calculated error.
  757. */
  758. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  759. const struct dpll *calculated_clock,
  760. const struct dpll *best_clock,
  761. unsigned int best_error_ppm,
  762. unsigned int *error_ppm)
  763. {
  764. /*
  765. * For CHV ignore the error and consider only the P value.
  766. * Prefer a bigger P value based on HW requirements.
  767. */
  768. if (IS_CHERRYVIEW(to_i915(dev))) {
  769. *error_ppm = 0;
  770. return calculated_clock->p > best_clock->p;
  771. }
  772. if (WARN_ON_ONCE(!target_freq))
  773. return false;
  774. *error_ppm = div_u64(1000000ULL *
  775. abs(target_freq - calculated_clock->dot),
  776. target_freq);
  777. /*
  778. * Prefer a better P value over a better (smaller) error if the error
  779. * is small. Ensure this preference for future configurations too by
  780. * setting the error to 0.
  781. */
  782. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  783. *error_ppm = 0;
  784. return true;
  785. }
  786. return *error_ppm + 10 < best_error_ppm;
  787. }
  788. /*
  789. * Returns a set of divisors for the desired target clock with the given
  790. * refclk, or FALSE. The returned values represent the clock equation:
  791. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  792. */
  793. static bool
  794. vlv_find_best_dpll(const struct intel_limit *limit,
  795. struct intel_crtc_state *crtc_state,
  796. int target, int refclk, struct dpll *match_clock,
  797. struct dpll *best_clock)
  798. {
  799. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  800. struct drm_device *dev = crtc->base.dev;
  801. struct dpll clock;
  802. unsigned int bestppm = 1000000;
  803. /* min update 19.2 MHz */
  804. int max_n = min(limit->n.max, refclk / 19200);
  805. bool found = false;
  806. target *= 5; /* fast clock */
  807. memset(best_clock, 0, sizeof(*best_clock));
  808. /* based on hardware requirement, prefer smaller n to precision */
  809. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  810. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  811. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  812. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  813. clock.p = clock.p1 * clock.p2;
  814. /* based on hardware requirement, prefer bigger m1,m2 values */
  815. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  816. unsigned int ppm;
  817. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  818. refclk * clock.m1);
  819. vlv_calc_dpll_params(refclk, &clock);
  820. if (!intel_PLL_is_valid(to_i915(dev),
  821. limit,
  822. &clock))
  823. continue;
  824. if (!vlv_PLL_is_optimal(dev, target,
  825. &clock,
  826. best_clock,
  827. bestppm, &ppm))
  828. continue;
  829. *best_clock = clock;
  830. bestppm = ppm;
  831. found = true;
  832. }
  833. }
  834. }
  835. }
  836. return found;
  837. }
  838. /*
  839. * Returns a set of divisors for the desired target clock with the given
  840. * refclk, or FALSE. The returned values represent the clock equation:
  841. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  842. */
  843. static bool
  844. chv_find_best_dpll(const struct intel_limit *limit,
  845. struct intel_crtc_state *crtc_state,
  846. int target, int refclk, struct dpll *match_clock,
  847. struct dpll *best_clock)
  848. {
  849. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  850. struct drm_device *dev = crtc->base.dev;
  851. unsigned int best_error_ppm;
  852. struct dpll clock;
  853. uint64_t m2;
  854. int found = false;
  855. memset(best_clock, 0, sizeof(*best_clock));
  856. best_error_ppm = 1000000;
  857. /*
  858. * Based on hardware doc, the n always set to 1, and m1 always
  859. * set to 2. If requires to support 200Mhz refclk, we need to
  860. * revisit this because n may not 1 anymore.
  861. */
  862. clock.n = 1, clock.m1 = 2;
  863. target *= 5; /* fast clock */
  864. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  865. for (clock.p2 = limit->p2.p2_fast;
  866. clock.p2 >= limit->p2.p2_slow;
  867. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  868. unsigned int error_ppm;
  869. clock.p = clock.p1 * clock.p2;
  870. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  871. clock.n) << 22, refclk * clock.m1);
  872. if (m2 > INT_MAX/clock.m1)
  873. continue;
  874. clock.m2 = m2;
  875. chv_calc_dpll_params(refclk, &clock);
  876. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  877. continue;
  878. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  879. best_error_ppm, &error_ppm))
  880. continue;
  881. *best_clock = clock;
  882. best_error_ppm = error_ppm;
  883. found = true;
  884. }
  885. }
  886. return found;
  887. }
  888. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  889. struct dpll *best_clock)
  890. {
  891. int refclk = 100000;
  892. const struct intel_limit *limit = &intel_limits_bxt;
  893. return chv_find_best_dpll(limit, crtc_state,
  894. target_clock, refclk, NULL, best_clock);
  895. }
  896. bool intel_crtc_active(struct intel_crtc *crtc)
  897. {
  898. /* Be paranoid as we can arrive here with only partial
  899. * state retrieved from the hardware during setup.
  900. *
  901. * We can ditch the adjusted_mode.crtc_clock check as soon
  902. * as Haswell has gained clock readout/fastboot support.
  903. *
  904. * We can ditch the crtc->primary->state->fb check as soon as we can
  905. * properly reconstruct framebuffers.
  906. *
  907. * FIXME: The intel_crtc->active here should be switched to
  908. * crtc->state->active once we have proper CRTC states wired up
  909. * for atomic.
  910. */
  911. return crtc->active && crtc->base.primary->state->fb &&
  912. crtc->config->base.adjusted_mode.crtc_clock;
  913. }
  914. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  915. enum pipe pipe)
  916. {
  917. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  918. return crtc->config->cpu_transcoder;
  919. }
  920. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  921. enum pipe pipe)
  922. {
  923. i915_reg_t reg = PIPEDSL(pipe);
  924. u32 line1, line2;
  925. u32 line_mask;
  926. if (IS_GEN2(dev_priv))
  927. line_mask = DSL_LINEMASK_GEN2;
  928. else
  929. line_mask = DSL_LINEMASK_GEN3;
  930. line1 = I915_READ(reg) & line_mask;
  931. msleep(5);
  932. line2 = I915_READ(reg) & line_mask;
  933. return line1 != line2;
  934. }
  935. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  936. {
  937. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  938. enum pipe pipe = crtc->pipe;
  939. /* Wait for the display line to settle/start moving */
  940. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  941. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  942. pipe_name(pipe), onoff(state));
  943. }
  944. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  945. {
  946. wait_for_pipe_scanline_moving(crtc, false);
  947. }
  948. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  949. {
  950. wait_for_pipe_scanline_moving(crtc, true);
  951. }
  952. static void
  953. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  954. {
  955. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  956. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  957. if (INTEL_GEN(dev_priv) >= 4) {
  958. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  959. i915_reg_t reg = PIPECONF(cpu_transcoder);
  960. /* Wait for the Pipe State to go off */
  961. if (intel_wait_for_register(dev_priv,
  962. reg, I965_PIPECONF_ACTIVE, 0,
  963. 100))
  964. WARN(1, "pipe_off wait timed out\n");
  965. } else {
  966. intel_wait_for_pipe_scanline_stopped(crtc);
  967. }
  968. }
  969. /* Only for pre-ILK configs */
  970. void assert_pll(struct drm_i915_private *dev_priv,
  971. enum pipe pipe, bool state)
  972. {
  973. u32 val;
  974. bool cur_state;
  975. val = I915_READ(DPLL(pipe));
  976. cur_state = !!(val & DPLL_VCO_ENABLE);
  977. I915_STATE_WARN(cur_state != state,
  978. "PLL state assertion failure (expected %s, current %s)\n",
  979. onoff(state), onoff(cur_state));
  980. }
  981. /* XXX: the dsi pll is shared between MIPI DSI ports */
  982. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  983. {
  984. u32 val;
  985. bool cur_state;
  986. mutex_lock(&dev_priv->sb_lock);
  987. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  988. mutex_unlock(&dev_priv->sb_lock);
  989. cur_state = val & DSI_PLL_VCO_EN;
  990. I915_STATE_WARN(cur_state != state,
  991. "DSI PLL state assertion failure (expected %s, current %s)\n",
  992. onoff(state), onoff(cur_state));
  993. }
  994. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  995. enum pipe pipe, bool state)
  996. {
  997. bool cur_state;
  998. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  999. pipe);
  1000. if (HAS_DDI(dev_priv)) {
  1001. /* DDI does not have a specific FDI_TX register */
  1002. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1003. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1004. } else {
  1005. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1006. cur_state = !!(val & FDI_TX_ENABLE);
  1007. }
  1008. I915_STATE_WARN(cur_state != state,
  1009. "FDI TX state assertion failure (expected %s, current %s)\n",
  1010. onoff(state), onoff(cur_state));
  1011. }
  1012. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1013. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1014. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1015. enum pipe pipe, bool state)
  1016. {
  1017. u32 val;
  1018. bool cur_state;
  1019. val = I915_READ(FDI_RX_CTL(pipe));
  1020. cur_state = !!(val & FDI_RX_ENABLE);
  1021. I915_STATE_WARN(cur_state != state,
  1022. "FDI RX state assertion failure (expected %s, current %s)\n",
  1023. onoff(state), onoff(cur_state));
  1024. }
  1025. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1026. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1027. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1028. enum pipe pipe)
  1029. {
  1030. u32 val;
  1031. /* ILK FDI PLL is always enabled */
  1032. if (IS_GEN5(dev_priv))
  1033. return;
  1034. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1035. if (HAS_DDI(dev_priv))
  1036. return;
  1037. val = I915_READ(FDI_TX_CTL(pipe));
  1038. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1039. }
  1040. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. u32 val;
  1044. bool cur_state;
  1045. val = I915_READ(FDI_RX_CTL(pipe));
  1046. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1047. I915_STATE_WARN(cur_state != state,
  1048. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1049. onoff(state), onoff(cur_state));
  1050. }
  1051. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1052. {
  1053. i915_reg_t pp_reg;
  1054. u32 val;
  1055. enum pipe panel_pipe = INVALID_PIPE;
  1056. bool locked = true;
  1057. if (WARN_ON(HAS_DDI(dev_priv)))
  1058. return;
  1059. if (HAS_PCH_SPLIT(dev_priv)) {
  1060. u32 port_sel;
  1061. pp_reg = PP_CONTROL(0);
  1062. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1063. switch (port_sel) {
  1064. case PANEL_PORT_SELECT_LVDS:
  1065. intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
  1066. break;
  1067. case PANEL_PORT_SELECT_DPA:
  1068. intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
  1069. break;
  1070. case PANEL_PORT_SELECT_DPC:
  1071. intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
  1072. break;
  1073. case PANEL_PORT_SELECT_DPD:
  1074. intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
  1075. break;
  1076. default:
  1077. MISSING_CASE(port_sel);
  1078. break;
  1079. }
  1080. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1081. /* presumably write lock depends on pipe, not port select */
  1082. pp_reg = PP_CONTROL(pipe);
  1083. panel_pipe = pipe;
  1084. } else {
  1085. u32 port_sel;
  1086. pp_reg = PP_CONTROL(0);
  1087. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1088. WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
  1089. intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
  1090. }
  1091. val = I915_READ(pp_reg);
  1092. if (!(val & PANEL_POWER_ON) ||
  1093. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1094. locked = false;
  1095. I915_STATE_WARN(panel_pipe == pipe && locked,
  1096. "panel assertion failure, pipe %c regs locked\n",
  1097. pipe_name(pipe));
  1098. }
  1099. void assert_pipe(struct drm_i915_private *dev_priv,
  1100. enum pipe pipe, bool state)
  1101. {
  1102. bool cur_state;
  1103. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1104. pipe);
  1105. enum intel_display_power_domain power_domain;
  1106. /* we keep both pipes enabled on 830 */
  1107. if (IS_I830(dev_priv))
  1108. state = true;
  1109. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1110. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1111. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1112. cur_state = !!(val & PIPECONF_ENABLE);
  1113. intel_display_power_put(dev_priv, power_domain);
  1114. } else {
  1115. cur_state = false;
  1116. }
  1117. I915_STATE_WARN(cur_state != state,
  1118. "pipe %c assertion failure (expected %s, current %s)\n",
  1119. pipe_name(pipe), onoff(state), onoff(cur_state));
  1120. }
  1121. static void assert_plane(struct intel_plane *plane, bool state)
  1122. {
  1123. enum pipe pipe;
  1124. bool cur_state;
  1125. cur_state = plane->get_hw_state(plane, &pipe);
  1126. I915_STATE_WARN(cur_state != state,
  1127. "%s assertion failure (expected %s, current %s)\n",
  1128. plane->base.name, onoff(state), onoff(cur_state));
  1129. }
  1130. #define assert_plane_enabled(p) assert_plane(p, true)
  1131. #define assert_plane_disabled(p) assert_plane(p, false)
  1132. static void assert_planes_disabled(struct intel_crtc *crtc)
  1133. {
  1134. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1135. struct intel_plane *plane;
  1136. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1137. assert_plane_disabled(plane);
  1138. }
  1139. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1140. {
  1141. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1142. drm_crtc_vblank_put(crtc);
  1143. }
  1144. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1145. enum pipe pipe)
  1146. {
  1147. u32 val;
  1148. bool enabled;
  1149. val = I915_READ(PCH_TRANSCONF(pipe));
  1150. enabled = !!(val & TRANS_ENABLE);
  1151. I915_STATE_WARN(enabled,
  1152. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1153. pipe_name(pipe));
  1154. }
  1155. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1156. enum pipe pipe, enum port port,
  1157. i915_reg_t dp_reg)
  1158. {
  1159. enum pipe port_pipe;
  1160. bool state;
  1161. state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
  1162. I915_STATE_WARN(state && port_pipe == pipe,
  1163. "PCH DP %c enabled on transcoder %c, should be disabled\n",
  1164. port_name(port), pipe_name(pipe));
  1165. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
  1166. "IBX PCH DP %c still using transcoder B\n",
  1167. port_name(port));
  1168. }
  1169. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1170. enum pipe pipe, enum port port,
  1171. i915_reg_t hdmi_reg)
  1172. {
  1173. enum pipe port_pipe;
  1174. bool state;
  1175. state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
  1176. I915_STATE_WARN(state && port_pipe == pipe,
  1177. "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
  1178. port_name(port), pipe_name(pipe));
  1179. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
  1180. "IBX PCH HDMI %c still using transcoder B\n",
  1181. port_name(port));
  1182. }
  1183. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe)
  1185. {
  1186. enum pipe port_pipe;
  1187. assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
  1188. assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
  1189. assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
  1190. I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
  1191. port_pipe == pipe,
  1192. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1193. pipe_name(pipe));
  1194. I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
  1195. port_pipe == pipe,
  1196. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1197. pipe_name(pipe));
  1198. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
  1199. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
  1200. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
  1201. }
  1202. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1203. const struct intel_crtc_state *pipe_config)
  1204. {
  1205. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1206. enum pipe pipe = crtc->pipe;
  1207. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1208. POSTING_READ(DPLL(pipe));
  1209. udelay(150);
  1210. if (intel_wait_for_register(dev_priv,
  1211. DPLL(pipe),
  1212. DPLL_LOCK_VLV,
  1213. DPLL_LOCK_VLV,
  1214. 1))
  1215. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1216. }
  1217. static void vlv_enable_pll(struct intel_crtc *crtc,
  1218. const struct intel_crtc_state *pipe_config)
  1219. {
  1220. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1221. enum pipe pipe = crtc->pipe;
  1222. assert_pipe_disabled(dev_priv, pipe);
  1223. /* PLL is protected by panel, make sure we can write it */
  1224. assert_panel_unlocked(dev_priv, pipe);
  1225. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1226. _vlv_enable_pll(crtc, pipe_config);
  1227. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1228. POSTING_READ(DPLL_MD(pipe));
  1229. }
  1230. static void _chv_enable_pll(struct intel_crtc *crtc,
  1231. const struct intel_crtc_state *pipe_config)
  1232. {
  1233. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1234. enum pipe pipe = crtc->pipe;
  1235. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1236. u32 tmp;
  1237. mutex_lock(&dev_priv->sb_lock);
  1238. /* Enable back the 10bit clock to display controller */
  1239. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1240. tmp |= DPIO_DCLKP_EN;
  1241. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1242. mutex_unlock(&dev_priv->sb_lock);
  1243. /*
  1244. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1245. */
  1246. udelay(1);
  1247. /* Enable PLL */
  1248. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1249. /* Check PLL is locked */
  1250. if (intel_wait_for_register(dev_priv,
  1251. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1252. 1))
  1253. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1254. }
  1255. static void chv_enable_pll(struct intel_crtc *crtc,
  1256. const struct intel_crtc_state *pipe_config)
  1257. {
  1258. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1259. enum pipe pipe = crtc->pipe;
  1260. assert_pipe_disabled(dev_priv, pipe);
  1261. /* PLL is protected by panel, make sure we can write it */
  1262. assert_panel_unlocked(dev_priv, pipe);
  1263. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1264. _chv_enable_pll(crtc, pipe_config);
  1265. if (pipe != PIPE_A) {
  1266. /*
  1267. * WaPixelRepeatModeFixForC0:chv
  1268. *
  1269. * DPLLCMD is AWOL. Use chicken bits to propagate
  1270. * the value from DPLLBMD to either pipe B or C.
  1271. */
  1272. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1273. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1274. I915_WRITE(CBR4_VLV, 0);
  1275. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1276. /*
  1277. * DPLLB VGA mode also seems to cause problems.
  1278. * We should always have it disabled.
  1279. */
  1280. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1281. } else {
  1282. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1283. POSTING_READ(DPLL_MD(pipe));
  1284. }
  1285. }
  1286. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1287. {
  1288. struct intel_crtc *crtc;
  1289. int count = 0;
  1290. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1291. count += crtc->base.state->active &&
  1292. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1293. }
  1294. return count;
  1295. }
  1296. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1297. const struct intel_crtc_state *crtc_state)
  1298. {
  1299. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1300. i915_reg_t reg = DPLL(crtc->pipe);
  1301. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1302. int i;
  1303. assert_pipe_disabled(dev_priv, crtc->pipe);
  1304. /* PLL is protected by panel, make sure we can write it */
  1305. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1306. assert_panel_unlocked(dev_priv, crtc->pipe);
  1307. /* Enable DVO 2x clock on both PLLs if necessary */
  1308. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1309. /*
  1310. * It appears to be important that we don't enable this
  1311. * for the current pipe before otherwise configuring the
  1312. * PLL. No idea how this should be handled if multiple
  1313. * DVO outputs are enabled simultaneosly.
  1314. */
  1315. dpll |= DPLL_DVO_2X_MODE;
  1316. I915_WRITE(DPLL(!crtc->pipe),
  1317. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1318. }
  1319. /*
  1320. * Apparently we need to have VGA mode enabled prior to changing
  1321. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1322. * dividers, even though the register value does change.
  1323. */
  1324. I915_WRITE(reg, 0);
  1325. I915_WRITE(reg, dpll);
  1326. /* Wait for the clocks to stabilize. */
  1327. POSTING_READ(reg);
  1328. udelay(150);
  1329. if (INTEL_GEN(dev_priv) >= 4) {
  1330. I915_WRITE(DPLL_MD(crtc->pipe),
  1331. crtc_state->dpll_hw_state.dpll_md);
  1332. } else {
  1333. /* The pixel multiplier can only be updated once the
  1334. * DPLL is enabled and the clocks are stable.
  1335. *
  1336. * So write it again.
  1337. */
  1338. I915_WRITE(reg, dpll);
  1339. }
  1340. /* We do this three times for luck */
  1341. for (i = 0; i < 3; i++) {
  1342. I915_WRITE(reg, dpll);
  1343. POSTING_READ(reg);
  1344. udelay(150); /* wait for warmup */
  1345. }
  1346. }
  1347. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1348. {
  1349. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1350. enum pipe pipe = crtc->pipe;
  1351. /* Disable DVO 2x clock on both PLLs if necessary */
  1352. if (IS_I830(dev_priv) &&
  1353. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1354. !intel_num_dvo_pipes(dev_priv)) {
  1355. I915_WRITE(DPLL(PIPE_B),
  1356. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1357. I915_WRITE(DPLL(PIPE_A),
  1358. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1359. }
  1360. /* Don't disable pipe or pipe PLLs if needed */
  1361. if (IS_I830(dev_priv))
  1362. return;
  1363. /* Make sure the pipe isn't still relying on us */
  1364. assert_pipe_disabled(dev_priv, pipe);
  1365. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1366. POSTING_READ(DPLL(pipe));
  1367. }
  1368. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1369. {
  1370. u32 val;
  1371. /* Make sure the pipe isn't still relying on us */
  1372. assert_pipe_disabled(dev_priv, pipe);
  1373. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1374. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1375. if (pipe != PIPE_A)
  1376. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1377. I915_WRITE(DPLL(pipe), val);
  1378. POSTING_READ(DPLL(pipe));
  1379. }
  1380. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1381. {
  1382. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1383. u32 val;
  1384. /* Make sure the pipe isn't still relying on us */
  1385. assert_pipe_disabled(dev_priv, pipe);
  1386. val = DPLL_SSC_REF_CLK_CHV |
  1387. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1388. if (pipe != PIPE_A)
  1389. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1390. I915_WRITE(DPLL(pipe), val);
  1391. POSTING_READ(DPLL(pipe));
  1392. mutex_lock(&dev_priv->sb_lock);
  1393. /* Disable 10bit clock to display controller */
  1394. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1395. val &= ~DPIO_DCLKP_EN;
  1396. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1397. mutex_unlock(&dev_priv->sb_lock);
  1398. }
  1399. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1400. struct intel_digital_port *dport,
  1401. unsigned int expected_mask)
  1402. {
  1403. u32 port_mask;
  1404. i915_reg_t dpll_reg;
  1405. switch (dport->base.port) {
  1406. case PORT_B:
  1407. port_mask = DPLL_PORTB_READY_MASK;
  1408. dpll_reg = DPLL(0);
  1409. break;
  1410. case PORT_C:
  1411. port_mask = DPLL_PORTC_READY_MASK;
  1412. dpll_reg = DPLL(0);
  1413. expected_mask <<= 4;
  1414. break;
  1415. case PORT_D:
  1416. port_mask = DPLL_PORTD_READY_MASK;
  1417. dpll_reg = DPIO_PHY_STATUS;
  1418. break;
  1419. default:
  1420. BUG();
  1421. }
  1422. if (intel_wait_for_register(dev_priv,
  1423. dpll_reg, port_mask, expected_mask,
  1424. 1000))
  1425. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1426. port_name(dport->base.port),
  1427. I915_READ(dpll_reg) & port_mask, expected_mask);
  1428. }
  1429. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1430. enum pipe pipe)
  1431. {
  1432. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1433. pipe);
  1434. i915_reg_t reg;
  1435. uint32_t val, pipeconf_val;
  1436. /* Make sure PCH DPLL is enabled */
  1437. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1438. /* FDI must be feeding us bits for PCH ports */
  1439. assert_fdi_tx_enabled(dev_priv, pipe);
  1440. assert_fdi_rx_enabled(dev_priv, pipe);
  1441. if (HAS_PCH_CPT(dev_priv)) {
  1442. /* Workaround: Set the timing override bit before enabling the
  1443. * pch transcoder. */
  1444. reg = TRANS_CHICKEN2(pipe);
  1445. val = I915_READ(reg);
  1446. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1447. I915_WRITE(reg, val);
  1448. }
  1449. reg = PCH_TRANSCONF(pipe);
  1450. val = I915_READ(reg);
  1451. pipeconf_val = I915_READ(PIPECONF(pipe));
  1452. if (HAS_PCH_IBX(dev_priv)) {
  1453. /*
  1454. * Make the BPC in transcoder be consistent with
  1455. * that in pipeconf reg. For HDMI we must use 8bpc
  1456. * here for both 8bpc and 12bpc.
  1457. */
  1458. val &= ~PIPECONF_BPC_MASK;
  1459. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1460. val |= PIPECONF_8BPC;
  1461. else
  1462. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1463. }
  1464. val &= ~TRANS_INTERLACE_MASK;
  1465. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1466. if (HAS_PCH_IBX(dev_priv) &&
  1467. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1468. val |= TRANS_LEGACY_INTERLACED_ILK;
  1469. else
  1470. val |= TRANS_INTERLACED;
  1471. else
  1472. val |= TRANS_PROGRESSIVE;
  1473. I915_WRITE(reg, val | TRANS_ENABLE);
  1474. if (intel_wait_for_register(dev_priv,
  1475. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1476. 100))
  1477. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1478. }
  1479. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1480. enum transcoder cpu_transcoder)
  1481. {
  1482. u32 val, pipeconf_val;
  1483. /* FDI must be feeding us bits for PCH ports */
  1484. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1485. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1486. /* Workaround: set timing override bit. */
  1487. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1488. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1489. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1490. val = TRANS_ENABLE;
  1491. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1492. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1493. PIPECONF_INTERLACED_ILK)
  1494. val |= TRANS_INTERLACED;
  1495. else
  1496. val |= TRANS_PROGRESSIVE;
  1497. I915_WRITE(LPT_TRANSCONF, val);
  1498. if (intel_wait_for_register(dev_priv,
  1499. LPT_TRANSCONF,
  1500. TRANS_STATE_ENABLE,
  1501. TRANS_STATE_ENABLE,
  1502. 100))
  1503. DRM_ERROR("Failed to enable PCH transcoder\n");
  1504. }
  1505. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1506. enum pipe pipe)
  1507. {
  1508. i915_reg_t reg;
  1509. uint32_t val;
  1510. /* FDI relies on the transcoder */
  1511. assert_fdi_tx_disabled(dev_priv, pipe);
  1512. assert_fdi_rx_disabled(dev_priv, pipe);
  1513. /* Ports must be off as well */
  1514. assert_pch_ports_disabled(dev_priv, pipe);
  1515. reg = PCH_TRANSCONF(pipe);
  1516. val = I915_READ(reg);
  1517. val &= ~TRANS_ENABLE;
  1518. I915_WRITE(reg, val);
  1519. /* wait for PCH transcoder off, transcoder state */
  1520. if (intel_wait_for_register(dev_priv,
  1521. reg, TRANS_STATE_ENABLE, 0,
  1522. 50))
  1523. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1524. if (HAS_PCH_CPT(dev_priv)) {
  1525. /* Workaround: Clear the timing override chicken bit again. */
  1526. reg = TRANS_CHICKEN2(pipe);
  1527. val = I915_READ(reg);
  1528. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1529. I915_WRITE(reg, val);
  1530. }
  1531. }
  1532. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1533. {
  1534. u32 val;
  1535. val = I915_READ(LPT_TRANSCONF);
  1536. val &= ~TRANS_ENABLE;
  1537. I915_WRITE(LPT_TRANSCONF, val);
  1538. /* wait for PCH transcoder off, transcoder state */
  1539. if (intel_wait_for_register(dev_priv,
  1540. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1541. 50))
  1542. DRM_ERROR("Failed to disable PCH transcoder\n");
  1543. /* Workaround: clear timing override bit. */
  1544. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1545. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1546. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1547. }
  1548. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1549. {
  1550. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1551. if (HAS_PCH_LPT(dev_priv))
  1552. return PIPE_A;
  1553. else
  1554. return crtc->pipe;
  1555. }
  1556. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1557. {
  1558. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1559. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1560. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1561. enum pipe pipe = crtc->pipe;
  1562. i915_reg_t reg;
  1563. u32 val;
  1564. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1565. assert_planes_disabled(crtc);
  1566. /*
  1567. * A pipe without a PLL won't actually be able to drive bits from
  1568. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1569. * need the check.
  1570. */
  1571. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1572. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1573. assert_dsi_pll_enabled(dev_priv);
  1574. else
  1575. assert_pll_enabled(dev_priv, pipe);
  1576. } else {
  1577. if (new_crtc_state->has_pch_encoder) {
  1578. /* if driving the PCH, we need FDI enabled */
  1579. assert_fdi_rx_pll_enabled(dev_priv,
  1580. intel_crtc_pch_transcoder(crtc));
  1581. assert_fdi_tx_pll_enabled(dev_priv,
  1582. (enum pipe) cpu_transcoder);
  1583. }
  1584. /* FIXME: assert CPU port conditions for SNB+ */
  1585. }
  1586. reg = PIPECONF(cpu_transcoder);
  1587. val = I915_READ(reg);
  1588. if (val & PIPECONF_ENABLE) {
  1589. /* we keep both pipes enabled on 830 */
  1590. WARN_ON(!IS_I830(dev_priv));
  1591. return;
  1592. }
  1593. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1594. POSTING_READ(reg);
  1595. /*
  1596. * Until the pipe starts PIPEDSL reads will return a stale value,
  1597. * which causes an apparent vblank timestamp jump when PIPEDSL
  1598. * resets to its proper value. That also messes up the frame count
  1599. * when it's derived from the timestamps. So let's wait for the
  1600. * pipe to start properly before we call drm_crtc_vblank_on()
  1601. */
  1602. if (dev_priv->drm.max_vblank_count == 0)
  1603. intel_wait_for_pipe_scanline_moving(crtc);
  1604. }
  1605. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1606. {
  1607. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1608. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1609. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1610. enum pipe pipe = crtc->pipe;
  1611. i915_reg_t reg;
  1612. u32 val;
  1613. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1614. /*
  1615. * Make sure planes won't keep trying to pump pixels to us,
  1616. * or we might hang the display.
  1617. */
  1618. assert_planes_disabled(crtc);
  1619. reg = PIPECONF(cpu_transcoder);
  1620. val = I915_READ(reg);
  1621. if ((val & PIPECONF_ENABLE) == 0)
  1622. return;
  1623. /*
  1624. * Double wide has implications for planes
  1625. * so best keep it disabled when not needed.
  1626. */
  1627. if (old_crtc_state->double_wide)
  1628. val &= ~PIPECONF_DOUBLE_WIDE;
  1629. /* Don't disable pipe or pipe PLLs if needed */
  1630. if (!IS_I830(dev_priv))
  1631. val &= ~PIPECONF_ENABLE;
  1632. I915_WRITE(reg, val);
  1633. if ((val & PIPECONF_ENABLE) == 0)
  1634. intel_wait_for_pipe_off(old_crtc_state);
  1635. }
  1636. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1637. {
  1638. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1639. }
  1640. static unsigned int
  1641. intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
  1642. {
  1643. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1644. unsigned int cpp = fb->format->cpp[color_plane];
  1645. switch (fb->modifier) {
  1646. case DRM_FORMAT_MOD_LINEAR:
  1647. return cpp;
  1648. case I915_FORMAT_MOD_X_TILED:
  1649. if (IS_GEN2(dev_priv))
  1650. return 128;
  1651. else
  1652. return 512;
  1653. case I915_FORMAT_MOD_Y_TILED_CCS:
  1654. if (color_plane == 1)
  1655. return 128;
  1656. /* fall through */
  1657. case I915_FORMAT_MOD_Y_TILED:
  1658. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1659. return 128;
  1660. else
  1661. return 512;
  1662. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1663. if (color_plane == 1)
  1664. return 128;
  1665. /* fall through */
  1666. case I915_FORMAT_MOD_Yf_TILED:
  1667. switch (cpp) {
  1668. case 1:
  1669. return 64;
  1670. case 2:
  1671. case 4:
  1672. return 128;
  1673. case 8:
  1674. case 16:
  1675. return 256;
  1676. default:
  1677. MISSING_CASE(cpp);
  1678. return cpp;
  1679. }
  1680. break;
  1681. default:
  1682. MISSING_CASE(fb->modifier);
  1683. return cpp;
  1684. }
  1685. }
  1686. static unsigned int
  1687. intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
  1688. {
  1689. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1690. return 1;
  1691. else
  1692. return intel_tile_size(to_i915(fb->dev)) /
  1693. intel_tile_width_bytes(fb, color_plane);
  1694. }
  1695. /* Return the tile dimensions in pixel units */
  1696. static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
  1697. unsigned int *tile_width,
  1698. unsigned int *tile_height)
  1699. {
  1700. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
  1701. unsigned int cpp = fb->format->cpp[color_plane];
  1702. *tile_width = tile_width_bytes / cpp;
  1703. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1704. }
  1705. unsigned int
  1706. intel_fb_align_height(const struct drm_framebuffer *fb,
  1707. int color_plane, unsigned int height)
  1708. {
  1709. unsigned int tile_height = intel_tile_height(fb, color_plane);
  1710. return ALIGN(height, tile_height);
  1711. }
  1712. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1713. {
  1714. unsigned int size = 0;
  1715. int i;
  1716. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1717. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1718. return size;
  1719. }
  1720. static void
  1721. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1722. const struct drm_framebuffer *fb,
  1723. unsigned int rotation)
  1724. {
  1725. view->type = I915_GGTT_VIEW_NORMAL;
  1726. if (drm_rotation_90_or_270(rotation)) {
  1727. view->type = I915_GGTT_VIEW_ROTATED;
  1728. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1729. }
  1730. }
  1731. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1732. {
  1733. if (IS_I830(dev_priv))
  1734. return 16 * 1024;
  1735. else if (IS_I85X(dev_priv))
  1736. return 256;
  1737. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1738. return 32;
  1739. else
  1740. return 4 * 1024;
  1741. }
  1742. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1743. {
  1744. if (INTEL_GEN(dev_priv) >= 9)
  1745. return 256 * 1024;
  1746. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1747. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1748. return 128 * 1024;
  1749. else if (INTEL_GEN(dev_priv) >= 4)
  1750. return 4 * 1024;
  1751. else
  1752. return 0;
  1753. }
  1754. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1755. int color_plane)
  1756. {
  1757. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1758. /* AUX_DIST needs only 4K alignment */
  1759. if (color_plane == 1)
  1760. return 4096;
  1761. switch (fb->modifier) {
  1762. case DRM_FORMAT_MOD_LINEAR:
  1763. return intel_linear_alignment(dev_priv);
  1764. case I915_FORMAT_MOD_X_TILED:
  1765. if (INTEL_GEN(dev_priv) >= 9)
  1766. return 256 * 1024;
  1767. return 0;
  1768. case I915_FORMAT_MOD_Y_TILED_CCS:
  1769. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1770. case I915_FORMAT_MOD_Y_TILED:
  1771. case I915_FORMAT_MOD_Yf_TILED:
  1772. return 1 * 1024 * 1024;
  1773. default:
  1774. MISSING_CASE(fb->modifier);
  1775. return 0;
  1776. }
  1777. }
  1778. static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
  1779. {
  1780. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1781. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1782. return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
  1783. }
  1784. struct i915_vma *
  1785. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1786. const struct i915_ggtt_view *view,
  1787. bool uses_fence,
  1788. unsigned long *out_flags)
  1789. {
  1790. struct drm_device *dev = fb->dev;
  1791. struct drm_i915_private *dev_priv = to_i915(dev);
  1792. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  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. /* Note that the w/a also requires 64 PTE of padding following the
  1799. * bo. We currently fill all unused PTE with the shadow page and so
  1800. * we should always have valid PTE following the scanout preventing
  1801. * the VT-d warning.
  1802. */
  1803. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1804. alignment = 256 * 1024;
  1805. /*
  1806. * Global gtt pte registers are special registers which actually forward
  1807. * writes to a chunk of system memory. Which means that there is no risk
  1808. * that the register values disappear as soon as we call
  1809. * intel_runtime_pm_put(), so it is correct to wrap only the
  1810. * pin/unpin/fence and not more.
  1811. */
  1812. intel_runtime_pm_get(dev_priv);
  1813. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1814. pinctl = 0;
  1815. /* Valleyview is definitely limited to scanning out the first
  1816. * 512MiB. Lets presume this behaviour was inherited from the
  1817. * g4x display engine and that all earlier gen are similarly
  1818. * limited. Testing suggests that it is a little more
  1819. * complicated than this. For example, Cherryview appears quite
  1820. * happy to scanout from anywhere within its global aperture.
  1821. */
  1822. if (HAS_GMCH_DISPLAY(dev_priv))
  1823. pinctl |= PIN_MAPPABLE;
  1824. vma = i915_gem_object_pin_to_display_plane(obj,
  1825. alignment, view, pinctl);
  1826. if (IS_ERR(vma))
  1827. goto err;
  1828. if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
  1829. int ret;
  1830. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1831. * fence, whereas 965+ only requires a fence if using
  1832. * framebuffer compression. For simplicity, we always, when
  1833. * possible, install a fence as the cost is not that onerous.
  1834. *
  1835. * If we fail to fence the tiled scanout, then either the
  1836. * modeset will reject the change (which is highly unlikely as
  1837. * the affected systems, all but one, do not have unmappable
  1838. * space) or we will not be able to enable full powersaving
  1839. * techniques (also likely not to apply due to various limits
  1840. * FBC and the like impose on the size of the buffer, which
  1841. * presumably we violated anyway with this unmappable buffer).
  1842. * Anyway, it is presumably better to stumble onwards with
  1843. * something and try to run the system in a "less than optimal"
  1844. * mode that matches the user configuration.
  1845. */
  1846. ret = i915_vma_pin_fence(vma);
  1847. if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
  1848. i915_gem_object_unpin_from_display_plane(vma);
  1849. vma = ERR_PTR(ret);
  1850. goto err;
  1851. }
  1852. if (ret == 0 && vma->fence)
  1853. *out_flags |= PLANE_HAS_FENCE;
  1854. }
  1855. i915_vma_get(vma);
  1856. err:
  1857. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1858. intel_runtime_pm_put(dev_priv);
  1859. return vma;
  1860. }
  1861. void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
  1862. {
  1863. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1864. if (flags & PLANE_HAS_FENCE)
  1865. i915_vma_unpin_fence(vma);
  1866. i915_gem_object_unpin_from_display_plane(vma);
  1867. i915_vma_put(vma);
  1868. }
  1869. static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
  1870. unsigned int rotation)
  1871. {
  1872. if (drm_rotation_90_or_270(rotation))
  1873. return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
  1874. else
  1875. return fb->pitches[color_plane];
  1876. }
  1877. /*
  1878. * Convert the x/y offsets into a linear offset.
  1879. * Only valid with 0/180 degree rotation, which is fine since linear
  1880. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1881. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1882. */
  1883. u32 intel_fb_xy_to_linear(int x, int y,
  1884. const struct intel_plane_state *state,
  1885. int color_plane)
  1886. {
  1887. const struct drm_framebuffer *fb = state->base.fb;
  1888. unsigned int cpp = fb->format->cpp[color_plane];
  1889. unsigned int pitch = state->color_plane[color_plane].stride;
  1890. return y * pitch + x * cpp;
  1891. }
  1892. /*
  1893. * Add the x/y offsets derived from fb->offsets[] to the user
  1894. * specified plane src x/y offsets. The resulting x/y offsets
  1895. * specify the start of scanout from the beginning of the gtt mapping.
  1896. */
  1897. void intel_add_fb_offsets(int *x, int *y,
  1898. const struct intel_plane_state *state,
  1899. int color_plane)
  1900. {
  1901. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1902. unsigned int rotation = state->base.rotation;
  1903. if (drm_rotation_90_or_270(rotation)) {
  1904. *x += intel_fb->rotated[color_plane].x;
  1905. *y += intel_fb->rotated[color_plane].y;
  1906. } else {
  1907. *x += intel_fb->normal[color_plane].x;
  1908. *y += intel_fb->normal[color_plane].y;
  1909. }
  1910. }
  1911. static u32 intel_adjust_tile_offset(int *x, int *y,
  1912. unsigned int tile_width,
  1913. unsigned int tile_height,
  1914. unsigned int tile_size,
  1915. unsigned int pitch_tiles,
  1916. u32 old_offset,
  1917. u32 new_offset)
  1918. {
  1919. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1920. unsigned int tiles;
  1921. WARN_ON(old_offset & (tile_size - 1));
  1922. WARN_ON(new_offset & (tile_size - 1));
  1923. WARN_ON(new_offset > old_offset);
  1924. tiles = (old_offset - new_offset) / tile_size;
  1925. *y += tiles / pitch_tiles * tile_height;
  1926. *x += tiles % pitch_tiles * tile_width;
  1927. /* minimize x in case it got needlessly big */
  1928. *y += *x / pitch_pixels * tile_height;
  1929. *x %= pitch_pixels;
  1930. return new_offset;
  1931. }
  1932. static u32 intel_adjust_aligned_offset(int *x, int *y,
  1933. const struct drm_framebuffer *fb,
  1934. int color_plane,
  1935. unsigned int rotation,
  1936. unsigned int pitch,
  1937. u32 old_offset, u32 new_offset)
  1938. {
  1939. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1940. unsigned int cpp = fb->format->cpp[color_plane];
  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, color_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_plane_adjust_aligned_offset(int *x, int *y,
  1968. const struct intel_plane_state *state,
  1969. int color_plane,
  1970. u32 old_offset, u32 new_offset)
  1971. {
  1972. return intel_adjust_aligned_offset(x, y, state->base.fb, color_plane,
  1973. state->base.rotation,
  1974. state->color_plane[color_plane].stride,
  1975. old_offset, new_offset);
  1976. }
  1977. /*
  1978. * Computes the aligned offset to the base tile and adjusts
  1979. * x, y. bytes per pixel is assumed to be a power-of-two.
  1980. *
  1981. * In the 90/270 rotated case, x and y are assumed
  1982. * to be already rotated to match the rotated GTT view, and
  1983. * pitch is the tile_height aligned framebuffer height.
  1984. *
  1985. * This function is used when computing the derived information
  1986. * under intel_framebuffer, so using any of that information
  1987. * here is not allowed. Anything under drm_framebuffer can be
  1988. * used. This is why the user has to pass in the pitch since it
  1989. * is specified in the rotated orientation.
  1990. */
  1991. static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
  1992. int *x, int *y,
  1993. const struct drm_framebuffer *fb,
  1994. int color_plane,
  1995. unsigned int pitch,
  1996. unsigned int rotation,
  1997. u32 alignment)
  1998. {
  1999. uint64_t fb_modifier = fb->modifier;
  2000. unsigned int cpp = fb->format->cpp[color_plane];
  2001. u32 offset, offset_aligned;
  2002. if (alignment)
  2003. alignment--;
  2004. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2005. unsigned int tile_size, tile_width, tile_height;
  2006. unsigned int tile_rows, tiles, pitch_tiles;
  2007. tile_size = intel_tile_size(dev_priv);
  2008. intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
  2009. if (drm_rotation_90_or_270(rotation)) {
  2010. pitch_tiles = pitch / tile_height;
  2011. swap(tile_width, tile_height);
  2012. } else {
  2013. pitch_tiles = pitch / (tile_width * cpp);
  2014. }
  2015. tile_rows = *y / tile_height;
  2016. *y %= tile_height;
  2017. tiles = *x / tile_width;
  2018. *x %= tile_width;
  2019. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2020. offset_aligned = offset & ~alignment;
  2021. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2022. tile_size, pitch_tiles,
  2023. offset, offset_aligned);
  2024. } else {
  2025. offset = *y * pitch + *x * cpp;
  2026. offset_aligned = offset & ~alignment;
  2027. *y = (offset & alignment) / pitch;
  2028. *x = ((offset & alignment) - *y * pitch) / cpp;
  2029. }
  2030. return offset_aligned;
  2031. }
  2032. static u32 intel_plane_compute_aligned_offset(int *x, int *y,
  2033. const struct intel_plane_state *state,
  2034. int color_plane)
  2035. {
  2036. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2037. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2038. const struct drm_framebuffer *fb = state->base.fb;
  2039. unsigned int rotation = state->base.rotation;
  2040. int pitch = state->color_plane[color_plane].stride;
  2041. u32 alignment;
  2042. if (intel_plane->id == PLANE_CURSOR)
  2043. alignment = intel_cursor_alignment(dev_priv);
  2044. else
  2045. alignment = intel_surf_alignment(fb, color_plane);
  2046. return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
  2047. pitch, rotation, alignment);
  2048. }
  2049. /* Convert the fb->offset[] into x/y offsets */
  2050. static int intel_fb_offset_to_xy(int *x, int *y,
  2051. const struct drm_framebuffer *fb,
  2052. int color_plane)
  2053. {
  2054. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2055. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2056. fb->offsets[color_plane] % intel_tile_size(dev_priv))
  2057. return -EINVAL;
  2058. *x = 0;
  2059. *y = 0;
  2060. intel_adjust_aligned_offset(x, y,
  2061. fb, color_plane, DRM_MODE_ROTATE_0,
  2062. fb->pitches[color_plane],
  2063. fb->offsets[color_plane], 0);
  2064. return 0;
  2065. }
  2066. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2067. {
  2068. switch (fb_modifier) {
  2069. case I915_FORMAT_MOD_X_TILED:
  2070. return I915_TILING_X;
  2071. case I915_FORMAT_MOD_Y_TILED:
  2072. case I915_FORMAT_MOD_Y_TILED_CCS:
  2073. return I915_TILING_Y;
  2074. default:
  2075. return I915_TILING_NONE;
  2076. }
  2077. }
  2078. /*
  2079. * From the Sky Lake PRM:
  2080. * "The Color Control Surface (CCS) contains the compression status of
  2081. * the cache-line pairs. The compression state of the cache-line pair
  2082. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2083. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2084. * cache-line-pairs. CCS is always Y tiled."
  2085. *
  2086. * Since cache line pairs refers to horizontally adjacent cache lines,
  2087. * each cache line in the CCS corresponds to an area of 32x16 cache
  2088. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2089. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2090. * main surface.
  2091. */
  2092. static const struct drm_format_info ccs_formats[] = {
  2093. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2094. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2095. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2096. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2097. };
  2098. static const struct drm_format_info *
  2099. lookup_format_info(const struct drm_format_info formats[],
  2100. int num_formats, u32 format)
  2101. {
  2102. int i;
  2103. for (i = 0; i < num_formats; i++) {
  2104. if (formats[i].format == format)
  2105. return &formats[i];
  2106. }
  2107. return NULL;
  2108. }
  2109. static const struct drm_format_info *
  2110. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2111. {
  2112. switch (cmd->modifier[0]) {
  2113. case I915_FORMAT_MOD_Y_TILED_CCS:
  2114. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2115. return lookup_format_info(ccs_formats,
  2116. ARRAY_SIZE(ccs_formats),
  2117. cmd->pixel_format);
  2118. default:
  2119. return NULL;
  2120. }
  2121. }
  2122. bool is_ccs_modifier(u64 modifier)
  2123. {
  2124. return modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2125. modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
  2126. }
  2127. static int
  2128. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2129. struct drm_framebuffer *fb)
  2130. {
  2131. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2132. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2133. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2134. u32 gtt_offset_rotated = 0;
  2135. unsigned int max_size = 0;
  2136. int i, num_planes = fb->format->num_planes;
  2137. unsigned int tile_size = intel_tile_size(dev_priv);
  2138. for (i = 0; i < num_planes; i++) {
  2139. unsigned int width, height;
  2140. unsigned int cpp, size;
  2141. u32 offset;
  2142. int x, y;
  2143. int ret;
  2144. cpp = fb->format->cpp[i];
  2145. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2146. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2147. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2148. if (ret) {
  2149. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2150. i, fb->offsets[i]);
  2151. return ret;
  2152. }
  2153. if (is_ccs_modifier(fb->modifier) && i == 1) {
  2154. int hsub = fb->format->hsub;
  2155. int vsub = fb->format->vsub;
  2156. int tile_width, tile_height;
  2157. int main_x, main_y;
  2158. int ccs_x, ccs_y;
  2159. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2160. tile_width *= hsub;
  2161. tile_height *= vsub;
  2162. ccs_x = (x * hsub) % tile_width;
  2163. ccs_y = (y * vsub) % tile_height;
  2164. main_x = intel_fb->normal[0].x % tile_width;
  2165. main_y = intel_fb->normal[0].y % tile_height;
  2166. /*
  2167. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2168. * x/y offsets must match between CCS and the main surface.
  2169. */
  2170. if (main_x != ccs_x || main_y != ccs_y) {
  2171. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2172. main_x, main_y,
  2173. ccs_x, ccs_y,
  2174. intel_fb->normal[0].x,
  2175. intel_fb->normal[0].y,
  2176. x, y);
  2177. return -EINVAL;
  2178. }
  2179. }
  2180. /*
  2181. * The fence (if used) is aligned to the start of the object
  2182. * so having the framebuffer wrap around across the edge of the
  2183. * fenced region doesn't really work. We have no API to configure
  2184. * the fence start offset within the object (nor could we probably
  2185. * on gen2/3). So it's just easier if we just require that the
  2186. * fb layout agrees with the fence layout. We already check that the
  2187. * fb stride matches the fence stride elsewhere.
  2188. */
  2189. if (i == 0 && i915_gem_object_is_tiled(obj) &&
  2190. (x + width) * cpp > fb->pitches[i]) {
  2191. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2192. i, fb->offsets[i]);
  2193. return -EINVAL;
  2194. }
  2195. /*
  2196. * First pixel of the framebuffer from
  2197. * the start of the normal gtt mapping.
  2198. */
  2199. intel_fb->normal[i].x = x;
  2200. intel_fb->normal[i].y = y;
  2201. offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
  2202. fb->pitches[i],
  2203. DRM_MODE_ROTATE_0,
  2204. tile_size);
  2205. offset /= tile_size;
  2206. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2207. unsigned int tile_width, tile_height;
  2208. unsigned int pitch_tiles;
  2209. struct drm_rect r;
  2210. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2211. rot_info->plane[i].offset = offset;
  2212. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2213. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2214. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2215. intel_fb->rotated[i].pitch =
  2216. rot_info->plane[i].height * tile_height;
  2217. /* how many tiles does this plane need */
  2218. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2219. /*
  2220. * If the plane isn't horizontally tile aligned,
  2221. * we need one more tile.
  2222. */
  2223. if (x != 0)
  2224. size++;
  2225. /* rotate the x/y offsets to match the GTT view */
  2226. r.x1 = x;
  2227. r.y1 = y;
  2228. r.x2 = x + width;
  2229. r.y2 = y + height;
  2230. drm_rect_rotate(&r,
  2231. rot_info->plane[i].width * tile_width,
  2232. rot_info->plane[i].height * tile_height,
  2233. DRM_MODE_ROTATE_270);
  2234. x = r.x1;
  2235. y = r.y1;
  2236. /* rotate the tile dimensions to match the GTT view */
  2237. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2238. swap(tile_width, tile_height);
  2239. /*
  2240. * We only keep the x/y offsets, so push all of the
  2241. * gtt offset into the x/y offsets.
  2242. */
  2243. intel_adjust_tile_offset(&x, &y,
  2244. tile_width, tile_height,
  2245. tile_size, pitch_tiles,
  2246. gtt_offset_rotated * tile_size, 0);
  2247. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2248. /*
  2249. * First pixel of the framebuffer from
  2250. * the start of the rotated gtt mapping.
  2251. */
  2252. intel_fb->rotated[i].x = x;
  2253. intel_fb->rotated[i].y = y;
  2254. } else {
  2255. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2256. x * cpp, tile_size);
  2257. }
  2258. /* how many tiles in total needed in the bo */
  2259. max_size = max(max_size, offset + size);
  2260. }
  2261. if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
  2262. DRM_DEBUG_KMS("fb too big for bo (need %llu bytes, have %zu bytes)\n",
  2263. mul_u32_u32(max_size, tile_size), obj->base.size);
  2264. return -EINVAL;
  2265. }
  2266. return 0;
  2267. }
  2268. static int i9xx_format_to_fourcc(int format)
  2269. {
  2270. switch (format) {
  2271. case DISPPLANE_8BPP:
  2272. return DRM_FORMAT_C8;
  2273. case DISPPLANE_BGRX555:
  2274. return DRM_FORMAT_XRGB1555;
  2275. case DISPPLANE_BGRX565:
  2276. return DRM_FORMAT_RGB565;
  2277. default:
  2278. case DISPPLANE_BGRX888:
  2279. return DRM_FORMAT_XRGB8888;
  2280. case DISPPLANE_RGBX888:
  2281. return DRM_FORMAT_XBGR8888;
  2282. case DISPPLANE_BGRX101010:
  2283. return DRM_FORMAT_XRGB2101010;
  2284. case DISPPLANE_RGBX101010:
  2285. return DRM_FORMAT_XBGR2101010;
  2286. }
  2287. }
  2288. int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2289. {
  2290. switch (format) {
  2291. case PLANE_CTL_FORMAT_RGB_565:
  2292. return DRM_FORMAT_RGB565;
  2293. case PLANE_CTL_FORMAT_NV12:
  2294. return DRM_FORMAT_NV12;
  2295. default:
  2296. case PLANE_CTL_FORMAT_XRGB_8888:
  2297. if (rgb_order) {
  2298. if (alpha)
  2299. return DRM_FORMAT_ABGR8888;
  2300. else
  2301. return DRM_FORMAT_XBGR8888;
  2302. } else {
  2303. if (alpha)
  2304. return DRM_FORMAT_ARGB8888;
  2305. else
  2306. return DRM_FORMAT_XRGB8888;
  2307. }
  2308. case PLANE_CTL_FORMAT_XRGB_2101010:
  2309. if (rgb_order)
  2310. return DRM_FORMAT_XBGR2101010;
  2311. else
  2312. return DRM_FORMAT_XRGB2101010;
  2313. }
  2314. }
  2315. static bool
  2316. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2317. struct intel_initial_plane_config *plane_config)
  2318. {
  2319. struct drm_device *dev = crtc->base.dev;
  2320. struct drm_i915_private *dev_priv = to_i915(dev);
  2321. struct drm_i915_gem_object *obj = NULL;
  2322. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2323. struct drm_framebuffer *fb = &plane_config->fb->base;
  2324. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2325. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2326. PAGE_SIZE);
  2327. size_aligned -= base_aligned;
  2328. if (plane_config->size == 0)
  2329. return false;
  2330. /* If the FB is too big, just don't use it since fbdev is not very
  2331. * important and we should probably use that space with FBC or other
  2332. * features. */
  2333. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2334. return false;
  2335. switch (fb->modifier) {
  2336. case DRM_FORMAT_MOD_LINEAR:
  2337. case I915_FORMAT_MOD_X_TILED:
  2338. case I915_FORMAT_MOD_Y_TILED:
  2339. break;
  2340. default:
  2341. DRM_DEBUG_DRIVER("Unsupported modifier for initial FB: 0x%llx\n",
  2342. fb->modifier);
  2343. return false;
  2344. }
  2345. mutex_lock(&dev->struct_mutex);
  2346. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2347. base_aligned,
  2348. base_aligned,
  2349. size_aligned);
  2350. mutex_unlock(&dev->struct_mutex);
  2351. if (!obj)
  2352. return false;
  2353. switch (plane_config->tiling) {
  2354. case I915_TILING_NONE:
  2355. break;
  2356. case I915_TILING_X:
  2357. case I915_TILING_Y:
  2358. obj->tiling_and_stride = fb->pitches[0] | plane_config->tiling;
  2359. break;
  2360. default:
  2361. MISSING_CASE(plane_config->tiling);
  2362. return false;
  2363. }
  2364. mode_cmd.pixel_format = fb->format->format;
  2365. mode_cmd.width = fb->width;
  2366. mode_cmd.height = fb->height;
  2367. mode_cmd.pitches[0] = fb->pitches[0];
  2368. mode_cmd.modifier[0] = fb->modifier;
  2369. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2370. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2371. DRM_DEBUG_KMS("intel fb init failed\n");
  2372. goto out_unref_obj;
  2373. }
  2374. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2375. return true;
  2376. out_unref_obj:
  2377. i915_gem_object_put(obj);
  2378. return false;
  2379. }
  2380. static void
  2381. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2382. struct intel_plane_state *plane_state,
  2383. bool visible)
  2384. {
  2385. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2386. plane_state->base.visible = visible;
  2387. if (visible)
  2388. crtc_state->base.plane_mask |= drm_plane_mask(&plane->base);
  2389. else
  2390. crtc_state->base.plane_mask &= ~drm_plane_mask(&plane->base);
  2391. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2392. crtc_state->base.crtc->name,
  2393. crtc_state->active_planes);
  2394. }
  2395. static void fixup_active_planes(struct intel_crtc_state *crtc_state)
  2396. {
  2397. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  2398. struct drm_plane *plane;
  2399. /*
  2400. * Active_planes aliases if multiple "primary" or cursor planes
  2401. * have been used on the same (or wrong) pipe. plane_mask uses
  2402. * unique ids, hence we can use that to reconstruct active_planes.
  2403. */
  2404. crtc_state->active_planes = 0;
  2405. drm_for_each_plane_mask(plane, &dev_priv->drm,
  2406. crtc_state->base.plane_mask)
  2407. crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
  2408. }
  2409. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2410. struct intel_plane *plane)
  2411. {
  2412. struct intel_crtc_state *crtc_state =
  2413. to_intel_crtc_state(crtc->base.state);
  2414. struct intel_plane_state *plane_state =
  2415. to_intel_plane_state(plane->base.state);
  2416. intel_set_plane_visible(crtc_state, plane_state, false);
  2417. fixup_active_planes(crtc_state);
  2418. if (plane->id == PLANE_PRIMARY)
  2419. intel_pre_disable_primary_noatomic(&crtc->base);
  2420. trace_intel_disable_plane(&plane->base, crtc);
  2421. plane->disable_plane(plane, crtc);
  2422. }
  2423. static void
  2424. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2425. struct intel_initial_plane_config *plane_config)
  2426. {
  2427. struct drm_device *dev = intel_crtc->base.dev;
  2428. struct drm_i915_private *dev_priv = to_i915(dev);
  2429. struct drm_crtc *c;
  2430. struct drm_i915_gem_object *obj;
  2431. struct drm_plane *primary = intel_crtc->base.primary;
  2432. struct drm_plane_state *plane_state = primary->state;
  2433. struct intel_plane *intel_plane = to_intel_plane(primary);
  2434. struct intel_plane_state *intel_state =
  2435. to_intel_plane_state(plane_state);
  2436. struct drm_framebuffer *fb;
  2437. if (!plane_config->fb)
  2438. return;
  2439. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2440. fb = &plane_config->fb->base;
  2441. goto valid_fb;
  2442. }
  2443. kfree(plane_config->fb);
  2444. /*
  2445. * Failed to alloc the obj, check to see if we should share
  2446. * an fb with another CRTC instead
  2447. */
  2448. for_each_crtc(dev, c) {
  2449. struct intel_plane_state *state;
  2450. if (c == &intel_crtc->base)
  2451. continue;
  2452. if (!to_intel_crtc(c)->active)
  2453. continue;
  2454. state = to_intel_plane_state(c->primary->state);
  2455. if (!state->vma)
  2456. continue;
  2457. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2458. fb = state->base.fb;
  2459. drm_framebuffer_get(fb);
  2460. goto valid_fb;
  2461. }
  2462. }
  2463. /*
  2464. * We've failed to reconstruct the BIOS FB. Current display state
  2465. * indicates that the primary plane is visible, but has a NULL FB,
  2466. * which will lead to problems later if we don't fix it up. The
  2467. * simplest solution is to just disable the primary plane now and
  2468. * pretend the BIOS never had it enabled.
  2469. */
  2470. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2471. return;
  2472. valid_fb:
  2473. intel_state->base.rotation = plane_config->rotation;
  2474. intel_fill_fb_ggtt_view(&intel_state->view, fb,
  2475. intel_state->base.rotation);
  2476. intel_state->color_plane[0].stride =
  2477. intel_fb_pitch(fb, 0, intel_state->base.rotation);
  2478. mutex_lock(&dev->struct_mutex);
  2479. intel_state->vma =
  2480. intel_pin_and_fence_fb_obj(fb,
  2481. &intel_state->view,
  2482. intel_plane_uses_fence(intel_state),
  2483. &intel_state->flags);
  2484. mutex_unlock(&dev->struct_mutex);
  2485. if (IS_ERR(intel_state->vma)) {
  2486. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2487. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2488. intel_state->vma = NULL;
  2489. drm_framebuffer_put(fb);
  2490. return;
  2491. }
  2492. obj = intel_fb_obj(fb);
  2493. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  2494. plane_state->src_x = 0;
  2495. plane_state->src_y = 0;
  2496. plane_state->src_w = fb->width << 16;
  2497. plane_state->src_h = fb->height << 16;
  2498. plane_state->crtc_x = 0;
  2499. plane_state->crtc_y = 0;
  2500. plane_state->crtc_w = fb->width;
  2501. plane_state->crtc_h = fb->height;
  2502. intel_state->base.src = drm_plane_state_src(plane_state);
  2503. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2504. if (i915_gem_object_is_tiled(obj))
  2505. dev_priv->preserve_bios_swizzle = true;
  2506. plane_state->fb = fb;
  2507. plane_state->crtc = &intel_crtc->base;
  2508. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2509. &obj->frontbuffer_bits);
  2510. }
  2511. static int skl_max_plane_width(const struct drm_framebuffer *fb,
  2512. int color_plane,
  2513. unsigned int rotation)
  2514. {
  2515. int cpp = fb->format->cpp[color_plane];
  2516. switch (fb->modifier) {
  2517. case DRM_FORMAT_MOD_LINEAR:
  2518. case I915_FORMAT_MOD_X_TILED:
  2519. switch (cpp) {
  2520. case 8:
  2521. return 4096;
  2522. case 4:
  2523. case 2:
  2524. case 1:
  2525. return 8192;
  2526. default:
  2527. MISSING_CASE(cpp);
  2528. break;
  2529. }
  2530. break;
  2531. case I915_FORMAT_MOD_Y_TILED_CCS:
  2532. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2533. /* FIXME AUX plane? */
  2534. case I915_FORMAT_MOD_Y_TILED:
  2535. case I915_FORMAT_MOD_Yf_TILED:
  2536. switch (cpp) {
  2537. case 8:
  2538. return 2048;
  2539. case 4:
  2540. return 4096;
  2541. case 2:
  2542. case 1:
  2543. return 8192;
  2544. default:
  2545. MISSING_CASE(cpp);
  2546. break;
  2547. }
  2548. break;
  2549. default:
  2550. MISSING_CASE(fb->modifier);
  2551. }
  2552. return 2048;
  2553. }
  2554. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2555. int main_x, int main_y, u32 main_offset)
  2556. {
  2557. const struct drm_framebuffer *fb = plane_state->base.fb;
  2558. int hsub = fb->format->hsub;
  2559. int vsub = fb->format->vsub;
  2560. int aux_x = plane_state->color_plane[1].x;
  2561. int aux_y = plane_state->color_plane[1].y;
  2562. u32 aux_offset = plane_state->color_plane[1].offset;
  2563. u32 alignment = intel_surf_alignment(fb, 1);
  2564. while (aux_offset >= main_offset && aux_y <= main_y) {
  2565. int x, y;
  2566. if (aux_x == main_x && aux_y == main_y)
  2567. break;
  2568. if (aux_offset == 0)
  2569. break;
  2570. x = aux_x / hsub;
  2571. y = aux_y / vsub;
  2572. aux_offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 1,
  2573. aux_offset, aux_offset - alignment);
  2574. aux_x = x * hsub + aux_x % hsub;
  2575. aux_y = y * vsub + aux_y % vsub;
  2576. }
  2577. if (aux_x != main_x || aux_y != main_y)
  2578. return false;
  2579. plane_state->color_plane[1].offset = aux_offset;
  2580. plane_state->color_plane[1].x = aux_x;
  2581. plane_state->color_plane[1].y = aux_y;
  2582. return true;
  2583. }
  2584. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2585. {
  2586. const struct drm_framebuffer *fb = plane_state->base.fb;
  2587. unsigned int rotation = plane_state->base.rotation;
  2588. int x = plane_state->base.src.x1 >> 16;
  2589. int y = plane_state->base.src.y1 >> 16;
  2590. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2591. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2592. int max_width = skl_max_plane_width(fb, 0, rotation);
  2593. int max_height = 4096;
  2594. u32 alignment, offset, aux_offset = plane_state->color_plane[1].offset;
  2595. if (w > max_width || h > max_height) {
  2596. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2597. w, h, max_width, max_height);
  2598. return -EINVAL;
  2599. }
  2600. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2601. offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
  2602. alignment = intel_surf_alignment(fb, 0);
  2603. /*
  2604. * AUX surface offset is specified as the distance from the
  2605. * main surface offset, and it must be non-negative. Make
  2606. * sure that is what we will get.
  2607. */
  2608. if (offset > aux_offset)
  2609. offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
  2610. offset, aux_offset & ~(alignment - 1));
  2611. /*
  2612. * When using an X-tiled surface, the plane blows up
  2613. * if the x offset + width exceed the stride.
  2614. *
  2615. * TODO: linear and Y-tiled seem fine, Yf untested,
  2616. */
  2617. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2618. int cpp = fb->format->cpp[0];
  2619. while ((x + w) * cpp > plane_state->color_plane[0].stride) {
  2620. if (offset == 0) {
  2621. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2622. return -EINVAL;
  2623. }
  2624. offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
  2625. offset, offset - alignment);
  2626. }
  2627. }
  2628. /*
  2629. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2630. * they match with the main surface x/y offsets.
  2631. */
  2632. if (is_ccs_modifier(fb->modifier)) {
  2633. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2634. if (offset == 0)
  2635. break;
  2636. offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
  2637. offset, offset - alignment);
  2638. }
  2639. if (x != plane_state->color_plane[1].x || y != plane_state->color_plane[1].y) {
  2640. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2641. return -EINVAL;
  2642. }
  2643. }
  2644. plane_state->color_plane[0].offset = offset;
  2645. plane_state->color_plane[0].x = x;
  2646. plane_state->color_plane[0].y = y;
  2647. return 0;
  2648. }
  2649. static int
  2650. skl_check_nv12_surface(struct intel_plane_state *plane_state)
  2651. {
  2652. /* Display WA #1106 */
  2653. if (plane_state->base.rotation !=
  2654. (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
  2655. plane_state->base.rotation != DRM_MODE_ROTATE_270)
  2656. return 0;
  2657. /*
  2658. * src coordinates are rotated here.
  2659. * We check height but report it as width
  2660. */
  2661. if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
  2662. DRM_DEBUG_KMS("src width must be multiple "
  2663. "of 4 for rotated NV12\n");
  2664. return -EINVAL;
  2665. }
  2666. return 0;
  2667. }
  2668. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2669. {
  2670. const struct drm_framebuffer *fb = plane_state->base.fb;
  2671. unsigned int rotation = plane_state->base.rotation;
  2672. int max_width = skl_max_plane_width(fb, 1, rotation);
  2673. int max_height = 4096;
  2674. int x = plane_state->base.src.x1 >> 17;
  2675. int y = plane_state->base.src.y1 >> 17;
  2676. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2677. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2678. u32 offset;
  2679. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2680. offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
  2681. /* FIXME not quite sure how/if these apply to the chroma plane */
  2682. if (w > max_width || h > max_height) {
  2683. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2684. w, h, max_width, max_height);
  2685. return -EINVAL;
  2686. }
  2687. plane_state->color_plane[1].offset = offset;
  2688. plane_state->color_plane[1].x = x;
  2689. plane_state->color_plane[1].y = y;
  2690. return 0;
  2691. }
  2692. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2693. {
  2694. const struct drm_framebuffer *fb = plane_state->base.fb;
  2695. int src_x = plane_state->base.src.x1 >> 16;
  2696. int src_y = plane_state->base.src.y1 >> 16;
  2697. int hsub = fb->format->hsub;
  2698. int vsub = fb->format->vsub;
  2699. int x = src_x / hsub;
  2700. int y = src_y / vsub;
  2701. u32 offset;
  2702. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2703. offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
  2704. plane_state->color_plane[1].offset = offset;
  2705. plane_state->color_plane[1].x = x * hsub + src_x % hsub;
  2706. plane_state->color_plane[1].y = y * vsub + src_y % vsub;
  2707. return 0;
  2708. }
  2709. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2710. {
  2711. const struct drm_framebuffer *fb = plane_state->base.fb;
  2712. unsigned int rotation = plane_state->base.rotation;
  2713. int ret;
  2714. intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
  2715. plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
  2716. plane_state->color_plane[1].stride = intel_fb_pitch(fb, 1, rotation);
  2717. ret = intel_plane_check_stride(plane_state);
  2718. if (ret)
  2719. return ret;
  2720. if (!plane_state->base.visible)
  2721. return 0;
  2722. /* Rotate src coordinates to match rotated GTT view */
  2723. if (drm_rotation_90_or_270(rotation))
  2724. drm_rect_rotate(&plane_state->base.src,
  2725. fb->width << 16, fb->height << 16,
  2726. DRM_MODE_ROTATE_270);
  2727. /*
  2728. * Handle the AUX surface first since
  2729. * the main surface setup depends on it.
  2730. */
  2731. if (fb->format->format == DRM_FORMAT_NV12) {
  2732. ret = skl_check_nv12_surface(plane_state);
  2733. if (ret)
  2734. return ret;
  2735. ret = skl_check_nv12_aux_surface(plane_state);
  2736. if (ret)
  2737. return ret;
  2738. } else if (is_ccs_modifier(fb->modifier)) {
  2739. ret = skl_check_ccs_aux_surface(plane_state);
  2740. if (ret)
  2741. return ret;
  2742. } else {
  2743. plane_state->color_plane[1].offset = ~0xfff;
  2744. plane_state->color_plane[1].x = 0;
  2745. plane_state->color_plane[1].y = 0;
  2746. }
  2747. ret = skl_check_main_surface(plane_state);
  2748. if (ret)
  2749. return ret;
  2750. return 0;
  2751. }
  2752. unsigned int
  2753. i9xx_plane_max_stride(struct intel_plane *plane,
  2754. u32 pixel_format, u64 modifier,
  2755. unsigned int rotation)
  2756. {
  2757. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2758. if (!HAS_GMCH_DISPLAY(dev_priv)) {
  2759. return 32*1024;
  2760. } else if (INTEL_GEN(dev_priv) >= 4) {
  2761. if (modifier == I915_FORMAT_MOD_X_TILED)
  2762. return 16*1024;
  2763. else
  2764. return 32*1024;
  2765. } else if (INTEL_GEN(dev_priv) >= 3) {
  2766. if (modifier == I915_FORMAT_MOD_X_TILED)
  2767. return 8*1024;
  2768. else
  2769. return 16*1024;
  2770. } else {
  2771. if (plane->i9xx_plane == PLANE_C)
  2772. return 4*1024;
  2773. else
  2774. return 8*1024;
  2775. }
  2776. }
  2777. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2778. const struct intel_plane_state *plane_state)
  2779. {
  2780. struct drm_i915_private *dev_priv =
  2781. to_i915(plane_state->base.plane->dev);
  2782. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2783. const struct drm_framebuffer *fb = plane_state->base.fb;
  2784. unsigned int rotation = plane_state->base.rotation;
  2785. u32 dspcntr;
  2786. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2787. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2788. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2789. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2790. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2791. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2792. if (INTEL_GEN(dev_priv) < 5)
  2793. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2794. switch (fb->format->format) {
  2795. case DRM_FORMAT_C8:
  2796. dspcntr |= DISPPLANE_8BPP;
  2797. break;
  2798. case DRM_FORMAT_XRGB1555:
  2799. dspcntr |= DISPPLANE_BGRX555;
  2800. break;
  2801. case DRM_FORMAT_RGB565:
  2802. dspcntr |= DISPPLANE_BGRX565;
  2803. break;
  2804. case DRM_FORMAT_XRGB8888:
  2805. dspcntr |= DISPPLANE_BGRX888;
  2806. break;
  2807. case DRM_FORMAT_XBGR8888:
  2808. dspcntr |= DISPPLANE_RGBX888;
  2809. break;
  2810. case DRM_FORMAT_XRGB2101010:
  2811. dspcntr |= DISPPLANE_BGRX101010;
  2812. break;
  2813. case DRM_FORMAT_XBGR2101010:
  2814. dspcntr |= DISPPLANE_RGBX101010;
  2815. break;
  2816. default:
  2817. MISSING_CASE(fb->format->format);
  2818. return 0;
  2819. }
  2820. if (INTEL_GEN(dev_priv) >= 4 &&
  2821. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2822. dspcntr |= DISPPLANE_TILED;
  2823. if (rotation & DRM_MODE_ROTATE_180)
  2824. dspcntr |= DISPPLANE_ROTATE_180;
  2825. if (rotation & DRM_MODE_REFLECT_X)
  2826. dspcntr |= DISPPLANE_MIRROR;
  2827. return dspcntr;
  2828. }
  2829. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2830. {
  2831. struct drm_i915_private *dev_priv =
  2832. to_i915(plane_state->base.plane->dev);
  2833. const struct drm_framebuffer *fb = plane_state->base.fb;
  2834. unsigned int rotation = plane_state->base.rotation;
  2835. int src_x = plane_state->base.src.x1 >> 16;
  2836. int src_y = plane_state->base.src.y1 >> 16;
  2837. u32 offset;
  2838. int ret;
  2839. intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
  2840. plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
  2841. ret = intel_plane_check_stride(plane_state);
  2842. if (ret)
  2843. return ret;
  2844. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2845. if (INTEL_GEN(dev_priv) >= 4)
  2846. offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
  2847. plane_state, 0);
  2848. else
  2849. offset = 0;
  2850. /* HSW/BDW do this automagically in hardware */
  2851. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2852. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2853. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2854. if (rotation & DRM_MODE_ROTATE_180) {
  2855. src_x += src_w - 1;
  2856. src_y += src_h - 1;
  2857. } else if (rotation & DRM_MODE_REFLECT_X) {
  2858. src_x += src_w - 1;
  2859. }
  2860. }
  2861. plane_state->color_plane[0].offset = offset;
  2862. plane_state->color_plane[0].x = src_x;
  2863. plane_state->color_plane[0].y = src_y;
  2864. return 0;
  2865. }
  2866. static int
  2867. i9xx_plane_check(struct intel_crtc_state *crtc_state,
  2868. struct intel_plane_state *plane_state)
  2869. {
  2870. int ret;
  2871. ret = chv_plane_check_rotation(plane_state);
  2872. if (ret)
  2873. return ret;
  2874. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  2875. &crtc_state->base,
  2876. DRM_PLANE_HELPER_NO_SCALING,
  2877. DRM_PLANE_HELPER_NO_SCALING,
  2878. false, true);
  2879. if (ret)
  2880. return ret;
  2881. if (!plane_state->base.visible)
  2882. return 0;
  2883. ret = intel_plane_check_src_coordinates(plane_state);
  2884. if (ret)
  2885. return ret;
  2886. ret = i9xx_check_plane_surface(plane_state);
  2887. if (ret)
  2888. return ret;
  2889. plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
  2890. return 0;
  2891. }
  2892. static void i9xx_update_plane(struct intel_plane *plane,
  2893. const struct intel_crtc_state *crtc_state,
  2894. const struct intel_plane_state *plane_state)
  2895. {
  2896. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2897. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2898. u32 linear_offset;
  2899. u32 dspcntr = plane_state->ctl;
  2900. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2901. int x = plane_state->color_plane[0].x;
  2902. int y = plane_state->color_plane[0].y;
  2903. unsigned long irqflags;
  2904. u32 dspaddr_offset;
  2905. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2906. if (INTEL_GEN(dev_priv) >= 4)
  2907. dspaddr_offset = plane_state->color_plane[0].offset;
  2908. else
  2909. dspaddr_offset = linear_offset;
  2910. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2911. if (INTEL_GEN(dev_priv) < 4) {
  2912. /* pipesrc and dspsize control the size that is scaled from,
  2913. * which should always be the user's requested size.
  2914. */
  2915. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2916. ((crtc_state->pipe_src_h - 1) << 16) |
  2917. (crtc_state->pipe_src_w - 1));
  2918. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2919. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2920. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2921. ((crtc_state->pipe_src_h - 1) << 16) |
  2922. (crtc_state->pipe_src_w - 1));
  2923. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2924. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2925. }
  2926. I915_WRITE_FW(reg, dspcntr);
  2927. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), plane_state->color_plane[0].stride);
  2928. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2929. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2930. intel_plane_ggtt_offset(plane_state) +
  2931. dspaddr_offset);
  2932. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2933. } else if (INTEL_GEN(dev_priv) >= 4) {
  2934. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2935. intel_plane_ggtt_offset(plane_state) +
  2936. dspaddr_offset);
  2937. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2938. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2939. } else {
  2940. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2941. intel_plane_ggtt_offset(plane_state) +
  2942. dspaddr_offset);
  2943. }
  2944. POSTING_READ_FW(reg);
  2945. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2946. }
  2947. static void i9xx_disable_plane(struct intel_plane *plane,
  2948. struct intel_crtc *crtc)
  2949. {
  2950. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2951. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2952. unsigned long irqflags;
  2953. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2954. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2955. if (INTEL_GEN(dev_priv) >= 4)
  2956. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2957. else
  2958. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2959. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2960. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2961. }
  2962. static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
  2963. enum pipe *pipe)
  2964. {
  2965. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2966. enum intel_display_power_domain power_domain;
  2967. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2968. bool ret;
  2969. u32 val;
  2970. /*
  2971. * Not 100% correct for planes that can move between pipes,
  2972. * but that's only the case for gen2-4 which don't have any
  2973. * display power wells.
  2974. */
  2975. power_domain = POWER_DOMAIN_PIPE(plane->pipe);
  2976. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2977. return false;
  2978. val = I915_READ(DSPCNTR(i9xx_plane));
  2979. ret = val & DISPLAY_PLANE_ENABLE;
  2980. if (INTEL_GEN(dev_priv) >= 5)
  2981. *pipe = plane->pipe;
  2982. else
  2983. *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  2984. DISPPLANE_SEL_PIPE_SHIFT;
  2985. intel_display_power_put(dev_priv, power_domain);
  2986. return ret;
  2987. }
  2988. static u32
  2989. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
  2990. {
  2991. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2992. return 64;
  2993. else
  2994. return intel_tile_width_bytes(fb, color_plane);
  2995. }
  2996. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2997. {
  2998. struct drm_device *dev = intel_crtc->base.dev;
  2999. struct drm_i915_private *dev_priv = to_i915(dev);
  3000. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  3001. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  3002. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  3003. }
  3004. /*
  3005. * This function detaches (aka. unbinds) unused scalers in hardware
  3006. */
  3007. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  3008. {
  3009. struct intel_crtc_scaler_state *scaler_state;
  3010. int i;
  3011. scaler_state = &intel_crtc->config->scaler_state;
  3012. /* loop through and disable scalers that aren't in use */
  3013. for (i = 0; i < intel_crtc->num_scalers; i++) {
  3014. if (!scaler_state->scalers[i].in_use)
  3015. skl_detach_scaler(intel_crtc, i);
  3016. }
  3017. }
  3018. u32 skl_plane_stride(const struct intel_plane_state *plane_state,
  3019. int color_plane)
  3020. {
  3021. const struct drm_framebuffer *fb = plane_state->base.fb;
  3022. unsigned int rotation = plane_state->base.rotation;
  3023. u32 stride = plane_state->color_plane[color_plane].stride;
  3024. if (color_plane >= fb->format->num_planes)
  3025. return 0;
  3026. /*
  3027. * The stride is either expressed as a multiple of 64 bytes chunks for
  3028. * linear buffers or in number of tiles for tiled buffers.
  3029. */
  3030. if (drm_rotation_90_or_270(rotation))
  3031. stride /= intel_tile_height(fb, color_plane);
  3032. else
  3033. stride /= intel_fb_stride_alignment(fb, color_plane);
  3034. return stride;
  3035. }
  3036. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  3037. {
  3038. switch (pixel_format) {
  3039. case DRM_FORMAT_C8:
  3040. return PLANE_CTL_FORMAT_INDEXED;
  3041. case DRM_FORMAT_RGB565:
  3042. return PLANE_CTL_FORMAT_RGB_565;
  3043. case DRM_FORMAT_XBGR8888:
  3044. case DRM_FORMAT_ABGR8888:
  3045. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  3046. case DRM_FORMAT_XRGB8888:
  3047. case DRM_FORMAT_ARGB8888:
  3048. return PLANE_CTL_FORMAT_XRGB_8888;
  3049. case DRM_FORMAT_XRGB2101010:
  3050. return PLANE_CTL_FORMAT_XRGB_2101010;
  3051. case DRM_FORMAT_XBGR2101010:
  3052. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  3053. case DRM_FORMAT_YUYV:
  3054. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  3055. case DRM_FORMAT_YVYU:
  3056. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  3057. case DRM_FORMAT_UYVY:
  3058. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  3059. case DRM_FORMAT_VYUY:
  3060. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  3061. case DRM_FORMAT_NV12:
  3062. return PLANE_CTL_FORMAT_NV12;
  3063. default:
  3064. MISSING_CASE(pixel_format);
  3065. }
  3066. return 0;
  3067. }
  3068. /*
  3069. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  3070. * to be already pre-multiplied. We need to add a knob (or a different
  3071. * DRM_FORMAT) for user-space to configure that.
  3072. */
  3073. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  3074. {
  3075. switch (pixel_format) {
  3076. case DRM_FORMAT_ABGR8888:
  3077. case DRM_FORMAT_ARGB8888:
  3078. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  3079. default:
  3080. return PLANE_CTL_ALPHA_DISABLE;
  3081. }
  3082. }
  3083. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  3084. {
  3085. switch (pixel_format) {
  3086. case DRM_FORMAT_ABGR8888:
  3087. case DRM_FORMAT_ARGB8888:
  3088. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  3089. default:
  3090. return PLANE_COLOR_ALPHA_DISABLE;
  3091. }
  3092. }
  3093. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  3094. {
  3095. switch (fb_modifier) {
  3096. case DRM_FORMAT_MOD_LINEAR:
  3097. break;
  3098. case I915_FORMAT_MOD_X_TILED:
  3099. return PLANE_CTL_TILED_X;
  3100. case I915_FORMAT_MOD_Y_TILED:
  3101. return PLANE_CTL_TILED_Y;
  3102. case I915_FORMAT_MOD_Y_TILED_CCS:
  3103. return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
  3104. case I915_FORMAT_MOD_Yf_TILED:
  3105. return PLANE_CTL_TILED_YF;
  3106. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3107. return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
  3108. default:
  3109. MISSING_CASE(fb_modifier);
  3110. }
  3111. return 0;
  3112. }
  3113. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  3114. {
  3115. switch (rotate) {
  3116. case DRM_MODE_ROTATE_0:
  3117. break;
  3118. /*
  3119. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3120. * while i915 HW rotation is clockwise, thats why this swapping.
  3121. */
  3122. case DRM_MODE_ROTATE_90:
  3123. return PLANE_CTL_ROTATE_270;
  3124. case DRM_MODE_ROTATE_180:
  3125. return PLANE_CTL_ROTATE_180;
  3126. case DRM_MODE_ROTATE_270:
  3127. return PLANE_CTL_ROTATE_90;
  3128. default:
  3129. MISSING_CASE(rotate);
  3130. }
  3131. return 0;
  3132. }
  3133. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3134. {
  3135. switch (reflect) {
  3136. case 0:
  3137. break;
  3138. case DRM_MODE_REFLECT_X:
  3139. return PLANE_CTL_FLIP_HORIZONTAL;
  3140. case DRM_MODE_REFLECT_Y:
  3141. default:
  3142. MISSING_CASE(reflect);
  3143. }
  3144. return 0;
  3145. }
  3146. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3147. const struct intel_plane_state *plane_state)
  3148. {
  3149. struct drm_i915_private *dev_priv =
  3150. to_i915(plane_state->base.plane->dev);
  3151. const struct drm_framebuffer *fb = plane_state->base.fb;
  3152. unsigned int rotation = plane_state->base.rotation;
  3153. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3154. u32 plane_ctl;
  3155. plane_ctl = PLANE_CTL_ENABLE;
  3156. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3157. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3158. plane_ctl |=
  3159. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3160. PLANE_CTL_PIPE_CSC_ENABLE |
  3161. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3162. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3163. plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
  3164. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3165. plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
  3166. }
  3167. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3168. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3169. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3170. if (INTEL_GEN(dev_priv) >= 10)
  3171. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3172. DRM_MODE_REFLECT_MASK);
  3173. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3174. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3175. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3176. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3177. return plane_ctl;
  3178. }
  3179. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3180. const struct intel_plane_state *plane_state)
  3181. {
  3182. struct drm_i915_private *dev_priv =
  3183. to_i915(plane_state->base.plane->dev);
  3184. const struct drm_framebuffer *fb = plane_state->base.fb;
  3185. u32 plane_color_ctl = 0;
  3186. if (INTEL_GEN(dev_priv) < 11) {
  3187. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3188. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3189. }
  3190. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3191. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3192. if (fb->format->is_yuv) {
  3193. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3194. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3195. else
  3196. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
  3197. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3198. plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
  3199. }
  3200. return plane_color_ctl;
  3201. }
  3202. static int
  3203. __intel_display_resume(struct drm_device *dev,
  3204. struct drm_atomic_state *state,
  3205. struct drm_modeset_acquire_ctx *ctx)
  3206. {
  3207. struct drm_crtc_state *crtc_state;
  3208. struct drm_crtc *crtc;
  3209. int i, ret;
  3210. intel_modeset_setup_hw_state(dev, ctx);
  3211. i915_redisable_vga(to_i915(dev));
  3212. if (!state)
  3213. return 0;
  3214. /*
  3215. * We've duplicated the state, pointers to the old state are invalid.
  3216. *
  3217. * Don't attempt to use the old state until we commit the duplicated state.
  3218. */
  3219. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3220. /*
  3221. * Force recalculation even if we restore
  3222. * current state. With fast modeset this may not result
  3223. * in a modeset when the state is compatible.
  3224. */
  3225. crtc_state->mode_changed = true;
  3226. }
  3227. /* ignore any reset values/BIOS leftovers in the WM registers */
  3228. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3229. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3230. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3231. WARN_ON(ret == -EDEADLK);
  3232. return ret;
  3233. }
  3234. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3235. {
  3236. return intel_has_gpu_reset(dev_priv) &&
  3237. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3238. }
  3239. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3240. {
  3241. struct drm_device *dev = &dev_priv->drm;
  3242. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3243. struct drm_atomic_state *state;
  3244. int ret;
  3245. /* reset doesn't touch the display */
  3246. if (!i915_modparams.force_reset_modeset_test &&
  3247. !gpu_reset_clobbers_display(dev_priv))
  3248. return;
  3249. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3250. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3251. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3252. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3253. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3254. i915_gem_set_wedged(dev_priv);
  3255. }
  3256. /*
  3257. * Need mode_config.mutex so that we don't
  3258. * trample ongoing ->detect() and whatnot.
  3259. */
  3260. mutex_lock(&dev->mode_config.mutex);
  3261. drm_modeset_acquire_init(ctx, 0);
  3262. while (1) {
  3263. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3264. if (ret != -EDEADLK)
  3265. break;
  3266. drm_modeset_backoff(ctx);
  3267. }
  3268. /*
  3269. * Disabling the crtcs gracefully seems nicer. Also the
  3270. * g33 docs say we should at least disable all the planes.
  3271. */
  3272. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3273. if (IS_ERR(state)) {
  3274. ret = PTR_ERR(state);
  3275. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3276. return;
  3277. }
  3278. ret = drm_atomic_helper_disable_all(dev, ctx);
  3279. if (ret) {
  3280. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3281. drm_atomic_state_put(state);
  3282. return;
  3283. }
  3284. dev_priv->modeset_restore_state = state;
  3285. state->acquire_ctx = ctx;
  3286. }
  3287. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3288. {
  3289. struct drm_device *dev = &dev_priv->drm;
  3290. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3291. struct drm_atomic_state *state;
  3292. int ret;
  3293. /* reset doesn't touch the display */
  3294. if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  3295. return;
  3296. state = fetch_and_zero(&dev_priv->modeset_restore_state);
  3297. if (!state)
  3298. goto unlock;
  3299. /* reset doesn't touch the display */
  3300. if (!gpu_reset_clobbers_display(dev_priv)) {
  3301. /* for testing only restore the display */
  3302. ret = __intel_display_resume(dev, state, ctx);
  3303. if (ret)
  3304. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3305. } else {
  3306. /*
  3307. * The display has been reset as well,
  3308. * so need a full re-initialization.
  3309. */
  3310. intel_runtime_pm_disable_interrupts(dev_priv);
  3311. intel_runtime_pm_enable_interrupts(dev_priv);
  3312. intel_pps_unlock_regs_wa(dev_priv);
  3313. intel_modeset_init_hw(dev);
  3314. intel_init_clock_gating(dev_priv);
  3315. spin_lock_irq(&dev_priv->irq_lock);
  3316. if (dev_priv->display.hpd_irq_setup)
  3317. dev_priv->display.hpd_irq_setup(dev_priv);
  3318. spin_unlock_irq(&dev_priv->irq_lock);
  3319. ret = __intel_display_resume(dev, state, ctx);
  3320. if (ret)
  3321. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3322. intel_hpd_init(dev_priv);
  3323. }
  3324. drm_atomic_state_put(state);
  3325. unlock:
  3326. drm_modeset_drop_locks(ctx);
  3327. drm_modeset_acquire_fini(ctx);
  3328. mutex_unlock(&dev->mode_config.mutex);
  3329. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3330. }
  3331. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3332. const struct intel_crtc_state *new_crtc_state)
  3333. {
  3334. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3335. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3336. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3337. crtc->base.mode = new_crtc_state->base.mode;
  3338. /*
  3339. * Update pipe size and adjust fitter if needed: the reason for this is
  3340. * that in compute_mode_changes we check the native mode (not the pfit
  3341. * mode) to see if we can flip rather than do a full mode set. In the
  3342. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3343. * pfit state, we'll end up with a big fb scanned out into the wrong
  3344. * sized surface.
  3345. */
  3346. I915_WRITE(PIPESRC(crtc->pipe),
  3347. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3348. (new_crtc_state->pipe_src_h - 1));
  3349. /* on skylake this is done by detaching scalers */
  3350. if (INTEL_GEN(dev_priv) >= 9) {
  3351. skl_detach_scalers(crtc);
  3352. if (new_crtc_state->pch_pfit.enabled)
  3353. skylake_pfit_enable(crtc);
  3354. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3355. if (new_crtc_state->pch_pfit.enabled)
  3356. ironlake_pfit_enable(crtc);
  3357. else if (old_crtc_state->pch_pfit.enabled)
  3358. ironlake_pfit_disable(crtc, true);
  3359. }
  3360. }
  3361. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3362. {
  3363. struct drm_device *dev = crtc->base.dev;
  3364. struct drm_i915_private *dev_priv = to_i915(dev);
  3365. int pipe = crtc->pipe;
  3366. i915_reg_t reg;
  3367. u32 temp;
  3368. /* enable normal train */
  3369. reg = FDI_TX_CTL(pipe);
  3370. temp = I915_READ(reg);
  3371. if (IS_IVYBRIDGE(dev_priv)) {
  3372. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3373. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3374. } else {
  3375. temp &= ~FDI_LINK_TRAIN_NONE;
  3376. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3377. }
  3378. I915_WRITE(reg, temp);
  3379. reg = FDI_RX_CTL(pipe);
  3380. temp = I915_READ(reg);
  3381. if (HAS_PCH_CPT(dev_priv)) {
  3382. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3383. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3384. } else {
  3385. temp &= ~FDI_LINK_TRAIN_NONE;
  3386. temp |= FDI_LINK_TRAIN_NONE;
  3387. }
  3388. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3389. /* wait one idle pattern time */
  3390. POSTING_READ(reg);
  3391. udelay(1000);
  3392. /* IVB wants error correction enabled */
  3393. if (IS_IVYBRIDGE(dev_priv))
  3394. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3395. FDI_FE_ERRC_ENABLE);
  3396. }
  3397. /* The FDI link training functions for ILK/Ibexpeak. */
  3398. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3399. const struct intel_crtc_state *crtc_state)
  3400. {
  3401. struct drm_device *dev = crtc->base.dev;
  3402. struct drm_i915_private *dev_priv = to_i915(dev);
  3403. int pipe = crtc->pipe;
  3404. i915_reg_t reg;
  3405. u32 temp, tries;
  3406. /* FDI needs bits from pipe first */
  3407. assert_pipe_enabled(dev_priv, pipe);
  3408. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3409. for train result */
  3410. reg = FDI_RX_IMR(pipe);
  3411. temp = I915_READ(reg);
  3412. temp &= ~FDI_RX_SYMBOL_LOCK;
  3413. temp &= ~FDI_RX_BIT_LOCK;
  3414. I915_WRITE(reg, temp);
  3415. I915_READ(reg);
  3416. udelay(150);
  3417. /* enable CPU FDI TX and PCH FDI RX */
  3418. reg = FDI_TX_CTL(pipe);
  3419. temp = I915_READ(reg);
  3420. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3421. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3422. temp &= ~FDI_LINK_TRAIN_NONE;
  3423. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3424. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3425. reg = FDI_RX_CTL(pipe);
  3426. temp = I915_READ(reg);
  3427. temp &= ~FDI_LINK_TRAIN_NONE;
  3428. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3429. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3430. POSTING_READ(reg);
  3431. udelay(150);
  3432. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3433. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3434. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3435. FDI_RX_PHASE_SYNC_POINTER_EN);
  3436. reg = FDI_RX_IIR(pipe);
  3437. for (tries = 0; tries < 5; tries++) {
  3438. temp = I915_READ(reg);
  3439. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3440. if ((temp & FDI_RX_BIT_LOCK)) {
  3441. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3442. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3443. break;
  3444. }
  3445. }
  3446. if (tries == 5)
  3447. DRM_ERROR("FDI train 1 fail!\n");
  3448. /* Train 2 */
  3449. reg = FDI_TX_CTL(pipe);
  3450. temp = I915_READ(reg);
  3451. temp &= ~FDI_LINK_TRAIN_NONE;
  3452. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3453. I915_WRITE(reg, temp);
  3454. reg = FDI_RX_CTL(pipe);
  3455. temp = I915_READ(reg);
  3456. temp &= ~FDI_LINK_TRAIN_NONE;
  3457. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3458. I915_WRITE(reg, temp);
  3459. POSTING_READ(reg);
  3460. udelay(150);
  3461. reg = FDI_RX_IIR(pipe);
  3462. for (tries = 0; tries < 5; tries++) {
  3463. temp = I915_READ(reg);
  3464. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3465. if (temp & FDI_RX_SYMBOL_LOCK) {
  3466. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3467. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3468. break;
  3469. }
  3470. }
  3471. if (tries == 5)
  3472. DRM_ERROR("FDI train 2 fail!\n");
  3473. DRM_DEBUG_KMS("FDI train done\n");
  3474. }
  3475. static const int snb_b_fdi_train_param[] = {
  3476. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3477. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3478. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3479. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3480. };
  3481. /* The FDI link training functions for SNB/Cougarpoint. */
  3482. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3483. const struct intel_crtc_state *crtc_state)
  3484. {
  3485. struct drm_device *dev = crtc->base.dev;
  3486. struct drm_i915_private *dev_priv = to_i915(dev);
  3487. int pipe = crtc->pipe;
  3488. i915_reg_t reg;
  3489. u32 temp, i, retry;
  3490. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3491. for train result */
  3492. reg = FDI_RX_IMR(pipe);
  3493. temp = I915_READ(reg);
  3494. temp &= ~FDI_RX_SYMBOL_LOCK;
  3495. temp &= ~FDI_RX_BIT_LOCK;
  3496. I915_WRITE(reg, temp);
  3497. POSTING_READ(reg);
  3498. udelay(150);
  3499. /* enable CPU FDI TX and PCH FDI RX */
  3500. reg = FDI_TX_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3503. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3504. temp &= ~FDI_LINK_TRAIN_NONE;
  3505. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3506. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3507. /* SNB-B */
  3508. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3509. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3510. I915_WRITE(FDI_RX_MISC(pipe),
  3511. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3512. reg = FDI_RX_CTL(pipe);
  3513. temp = I915_READ(reg);
  3514. if (HAS_PCH_CPT(dev_priv)) {
  3515. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3516. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3517. } else {
  3518. temp &= ~FDI_LINK_TRAIN_NONE;
  3519. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3520. }
  3521. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3522. POSTING_READ(reg);
  3523. udelay(150);
  3524. for (i = 0; i < 4; i++) {
  3525. reg = FDI_TX_CTL(pipe);
  3526. temp = I915_READ(reg);
  3527. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3528. temp |= snb_b_fdi_train_param[i];
  3529. I915_WRITE(reg, temp);
  3530. POSTING_READ(reg);
  3531. udelay(500);
  3532. for (retry = 0; retry < 5; retry++) {
  3533. reg = FDI_RX_IIR(pipe);
  3534. temp = I915_READ(reg);
  3535. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3536. if (temp & FDI_RX_BIT_LOCK) {
  3537. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3538. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3539. break;
  3540. }
  3541. udelay(50);
  3542. }
  3543. if (retry < 5)
  3544. break;
  3545. }
  3546. if (i == 4)
  3547. DRM_ERROR("FDI train 1 fail!\n");
  3548. /* Train 2 */
  3549. reg = FDI_TX_CTL(pipe);
  3550. temp = I915_READ(reg);
  3551. temp &= ~FDI_LINK_TRAIN_NONE;
  3552. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3553. if (IS_GEN6(dev_priv)) {
  3554. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3555. /* SNB-B */
  3556. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3557. }
  3558. I915_WRITE(reg, temp);
  3559. reg = FDI_RX_CTL(pipe);
  3560. temp = I915_READ(reg);
  3561. if (HAS_PCH_CPT(dev_priv)) {
  3562. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3563. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3564. } else {
  3565. temp &= ~FDI_LINK_TRAIN_NONE;
  3566. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3567. }
  3568. I915_WRITE(reg, temp);
  3569. POSTING_READ(reg);
  3570. udelay(150);
  3571. for (i = 0; i < 4; i++) {
  3572. reg = FDI_TX_CTL(pipe);
  3573. temp = I915_READ(reg);
  3574. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3575. temp |= snb_b_fdi_train_param[i];
  3576. I915_WRITE(reg, temp);
  3577. POSTING_READ(reg);
  3578. udelay(500);
  3579. for (retry = 0; retry < 5; retry++) {
  3580. reg = FDI_RX_IIR(pipe);
  3581. temp = I915_READ(reg);
  3582. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3583. if (temp & FDI_RX_SYMBOL_LOCK) {
  3584. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3585. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3586. break;
  3587. }
  3588. udelay(50);
  3589. }
  3590. if (retry < 5)
  3591. break;
  3592. }
  3593. if (i == 4)
  3594. DRM_ERROR("FDI train 2 fail!\n");
  3595. DRM_DEBUG_KMS("FDI train done.\n");
  3596. }
  3597. /* Manual link training for Ivy Bridge A0 parts */
  3598. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3599. const struct intel_crtc_state *crtc_state)
  3600. {
  3601. struct drm_device *dev = crtc->base.dev;
  3602. struct drm_i915_private *dev_priv = to_i915(dev);
  3603. int pipe = crtc->pipe;
  3604. i915_reg_t reg;
  3605. u32 temp, i, j;
  3606. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3607. for train result */
  3608. reg = FDI_RX_IMR(pipe);
  3609. temp = I915_READ(reg);
  3610. temp &= ~FDI_RX_SYMBOL_LOCK;
  3611. temp &= ~FDI_RX_BIT_LOCK;
  3612. I915_WRITE(reg, temp);
  3613. POSTING_READ(reg);
  3614. udelay(150);
  3615. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3616. I915_READ(FDI_RX_IIR(pipe)));
  3617. /* Try each vswing and preemphasis setting twice before moving on */
  3618. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3619. /* disable first in case we need to retry */
  3620. reg = FDI_TX_CTL(pipe);
  3621. temp = I915_READ(reg);
  3622. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3623. temp &= ~FDI_TX_ENABLE;
  3624. I915_WRITE(reg, temp);
  3625. reg = FDI_RX_CTL(pipe);
  3626. temp = I915_READ(reg);
  3627. temp &= ~FDI_LINK_TRAIN_AUTO;
  3628. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3629. temp &= ~FDI_RX_ENABLE;
  3630. I915_WRITE(reg, temp);
  3631. /* enable CPU FDI TX and PCH FDI RX */
  3632. reg = FDI_TX_CTL(pipe);
  3633. temp = I915_READ(reg);
  3634. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3635. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3636. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3637. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3638. temp |= snb_b_fdi_train_param[j/2];
  3639. temp |= FDI_COMPOSITE_SYNC;
  3640. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3641. I915_WRITE(FDI_RX_MISC(pipe),
  3642. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3643. reg = FDI_RX_CTL(pipe);
  3644. temp = I915_READ(reg);
  3645. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3646. temp |= FDI_COMPOSITE_SYNC;
  3647. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3648. POSTING_READ(reg);
  3649. udelay(1); /* should be 0.5us */
  3650. for (i = 0; i < 4; i++) {
  3651. reg = FDI_RX_IIR(pipe);
  3652. temp = I915_READ(reg);
  3653. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3654. if (temp & FDI_RX_BIT_LOCK ||
  3655. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3656. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3657. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3658. i);
  3659. break;
  3660. }
  3661. udelay(1); /* should be 0.5us */
  3662. }
  3663. if (i == 4) {
  3664. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3665. continue;
  3666. }
  3667. /* Train 2 */
  3668. reg = FDI_TX_CTL(pipe);
  3669. temp = I915_READ(reg);
  3670. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3671. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3672. I915_WRITE(reg, temp);
  3673. reg = FDI_RX_CTL(pipe);
  3674. temp = I915_READ(reg);
  3675. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3676. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3677. I915_WRITE(reg, temp);
  3678. POSTING_READ(reg);
  3679. udelay(2); /* should be 1.5us */
  3680. for (i = 0; i < 4; i++) {
  3681. reg = FDI_RX_IIR(pipe);
  3682. temp = I915_READ(reg);
  3683. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3684. if (temp & FDI_RX_SYMBOL_LOCK ||
  3685. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3686. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3687. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3688. i);
  3689. goto train_done;
  3690. }
  3691. udelay(2); /* should be 1.5us */
  3692. }
  3693. if (i == 4)
  3694. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3695. }
  3696. train_done:
  3697. DRM_DEBUG_KMS("FDI train done.\n");
  3698. }
  3699. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3700. {
  3701. struct drm_device *dev = intel_crtc->base.dev;
  3702. struct drm_i915_private *dev_priv = to_i915(dev);
  3703. int pipe = intel_crtc->pipe;
  3704. i915_reg_t reg;
  3705. u32 temp;
  3706. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3707. reg = FDI_RX_CTL(pipe);
  3708. temp = I915_READ(reg);
  3709. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3710. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3711. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3712. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3713. POSTING_READ(reg);
  3714. udelay(200);
  3715. /* Switch from Rawclk to PCDclk */
  3716. temp = I915_READ(reg);
  3717. I915_WRITE(reg, temp | FDI_PCDCLK);
  3718. POSTING_READ(reg);
  3719. udelay(200);
  3720. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3721. reg = FDI_TX_CTL(pipe);
  3722. temp = I915_READ(reg);
  3723. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3724. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3725. POSTING_READ(reg);
  3726. udelay(100);
  3727. }
  3728. }
  3729. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3730. {
  3731. struct drm_device *dev = intel_crtc->base.dev;
  3732. struct drm_i915_private *dev_priv = to_i915(dev);
  3733. int pipe = intel_crtc->pipe;
  3734. i915_reg_t reg;
  3735. u32 temp;
  3736. /* Switch from PCDclk to Rawclk */
  3737. reg = FDI_RX_CTL(pipe);
  3738. temp = I915_READ(reg);
  3739. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3740. /* Disable CPU FDI TX PLL */
  3741. reg = FDI_TX_CTL(pipe);
  3742. temp = I915_READ(reg);
  3743. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3744. POSTING_READ(reg);
  3745. udelay(100);
  3746. reg = FDI_RX_CTL(pipe);
  3747. temp = I915_READ(reg);
  3748. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3749. /* Wait for the clocks to turn off. */
  3750. POSTING_READ(reg);
  3751. udelay(100);
  3752. }
  3753. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3754. {
  3755. struct drm_device *dev = crtc->dev;
  3756. struct drm_i915_private *dev_priv = to_i915(dev);
  3757. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3758. int pipe = intel_crtc->pipe;
  3759. i915_reg_t reg;
  3760. u32 temp;
  3761. /* disable CPU FDI tx and PCH FDI rx */
  3762. reg = FDI_TX_CTL(pipe);
  3763. temp = I915_READ(reg);
  3764. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3765. POSTING_READ(reg);
  3766. reg = FDI_RX_CTL(pipe);
  3767. temp = I915_READ(reg);
  3768. temp &= ~(0x7 << 16);
  3769. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3770. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3771. POSTING_READ(reg);
  3772. udelay(100);
  3773. /* Ironlake workaround, disable clock pointer after downing FDI */
  3774. if (HAS_PCH_IBX(dev_priv))
  3775. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3776. /* still set train pattern 1 */
  3777. reg = FDI_TX_CTL(pipe);
  3778. temp = I915_READ(reg);
  3779. temp &= ~FDI_LINK_TRAIN_NONE;
  3780. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3781. I915_WRITE(reg, temp);
  3782. reg = FDI_RX_CTL(pipe);
  3783. temp = I915_READ(reg);
  3784. if (HAS_PCH_CPT(dev_priv)) {
  3785. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3786. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3787. } else {
  3788. temp &= ~FDI_LINK_TRAIN_NONE;
  3789. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3790. }
  3791. /* BPC in FDI rx is consistent with that in PIPECONF */
  3792. temp &= ~(0x07 << 16);
  3793. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3794. I915_WRITE(reg, temp);
  3795. POSTING_READ(reg);
  3796. udelay(100);
  3797. }
  3798. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3799. {
  3800. struct drm_crtc *crtc;
  3801. bool cleanup_done;
  3802. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3803. struct drm_crtc_commit *commit;
  3804. spin_lock(&crtc->commit_lock);
  3805. commit = list_first_entry_or_null(&crtc->commit_list,
  3806. struct drm_crtc_commit, commit_entry);
  3807. cleanup_done = commit ?
  3808. try_wait_for_completion(&commit->cleanup_done) : true;
  3809. spin_unlock(&crtc->commit_lock);
  3810. if (cleanup_done)
  3811. continue;
  3812. drm_crtc_wait_one_vblank(crtc);
  3813. return true;
  3814. }
  3815. return false;
  3816. }
  3817. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3818. {
  3819. u32 temp;
  3820. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3821. mutex_lock(&dev_priv->sb_lock);
  3822. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3823. temp |= SBI_SSCCTL_DISABLE;
  3824. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3825. mutex_unlock(&dev_priv->sb_lock);
  3826. }
  3827. /* Program iCLKIP clock to the desired frequency */
  3828. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3829. {
  3830. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3831. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3832. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3833. u32 temp;
  3834. lpt_disable_iclkip(dev_priv);
  3835. /* The iCLK virtual clock root frequency is in MHz,
  3836. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3837. * divisors, it is necessary to divide one by another, so we
  3838. * convert the virtual clock precision to KHz here for higher
  3839. * precision.
  3840. */
  3841. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3842. u32 iclk_virtual_root_freq = 172800 * 1000;
  3843. u32 iclk_pi_range = 64;
  3844. u32 desired_divisor;
  3845. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3846. clock << auxdiv);
  3847. divsel = (desired_divisor / iclk_pi_range) - 2;
  3848. phaseinc = desired_divisor % iclk_pi_range;
  3849. /*
  3850. * Near 20MHz is a corner case which is
  3851. * out of range for the 7-bit divisor
  3852. */
  3853. if (divsel <= 0x7f)
  3854. break;
  3855. }
  3856. /* This should not happen with any sane values */
  3857. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3858. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3859. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3860. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3861. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3862. clock,
  3863. auxdiv,
  3864. divsel,
  3865. phasedir,
  3866. phaseinc);
  3867. mutex_lock(&dev_priv->sb_lock);
  3868. /* Program SSCDIVINTPHASE6 */
  3869. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3870. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3871. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3872. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3873. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3874. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3875. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3876. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3877. /* Program SSCAUXDIV */
  3878. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3879. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3880. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3881. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3882. /* Enable modulator and associated divider */
  3883. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3884. temp &= ~SBI_SSCCTL_DISABLE;
  3885. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3886. mutex_unlock(&dev_priv->sb_lock);
  3887. /* Wait for initialization time */
  3888. udelay(24);
  3889. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3890. }
  3891. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3892. {
  3893. u32 divsel, phaseinc, auxdiv;
  3894. u32 iclk_virtual_root_freq = 172800 * 1000;
  3895. u32 iclk_pi_range = 64;
  3896. u32 desired_divisor;
  3897. u32 temp;
  3898. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3899. return 0;
  3900. mutex_lock(&dev_priv->sb_lock);
  3901. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3902. if (temp & SBI_SSCCTL_DISABLE) {
  3903. mutex_unlock(&dev_priv->sb_lock);
  3904. return 0;
  3905. }
  3906. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3907. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3908. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3909. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3910. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3911. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3912. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3913. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3914. mutex_unlock(&dev_priv->sb_lock);
  3915. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3916. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3917. desired_divisor << auxdiv);
  3918. }
  3919. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3920. enum pipe pch_transcoder)
  3921. {
  3922. struct drm_device *dev = crtc->base.dev;
  3923. struct drm_i915_private *dev_priv = to_i915(dev);
  3924. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3925. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3926. I915_READ(HTOTAL(cpu_transcoder)));
  3927. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3928. I915_READ(HBLANK(cpu_transcoder)));
  3929. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3930. I915_READ(HSYNC(cpu_transcoder)));
  3931. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3932. I915_READ(VTOTAL(cpu_transcoder)));
  3933. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3934. I915_READ(VBLANK(cpu_transcoder)));
  3935. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3936. I915_READ(VSYNC(cpu_transcoder)));
  3937. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3938. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3939. }
  3940. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3941. {
  3942. struct drm_i915_private *dev_priv = to_i915(dev);
  3943. uint32_t temp;
  3944. temp = I915_READ(SOUTH_CHICKEN1);
  3945. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3946. return;
  3947. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3948. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3949. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3950. if (enable)
  3951. temp |= FDI_BC_BIFURCATION_SELECT;
  3952. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3953. I915_WRITE(SOUTH_CHICKEN1, temp);
  3954. POSTING_READ(SOUTH_CHICKEN1);
  3955. }
  3956. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3957. {
  3958. struct drm_device *dev = intel_crtc->base.dev;
  3959. switch (intel_crtc->pipe) {
  3960. case PIPE_A:
  3961. break;
  3962. case PIPE_B:
  3963. if (intel_crtc->config->fdi_lanes > 2)
  3964. cpt_set_fdi_bc_bifurcation(dev, false);
  3965. else
  3966. cpt_set_fdi_bc_bifurcation(dev, true);
  3967. break;
  3968. case PIPE_C:
  3969. cpt_set_fdi_bc_bifurcation(dev, true);
  3970. break;
  3971. default:
  3972. BUG();
  3973. }
  3974. }
  3975. /*
  3976. * Finds the encoder associated with the given CRTC. This can only be
  3977. * used when we know that the CRTC isn't feeding multiple encoders!
  3978. */
  3979. static struct intel_encoder *
  3980. intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
  3981. const struct intel_crtc_state *crtc_state)
  3982. {
  3983. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3984. const struct drm_connector_state *connector_state;
  3985. const struct drm_connector *connector;
  3986. struct intel_encoder *encoder = NULL;
  3987. int num_encoders = 0;
  3988. int i;
  3989. for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
  3990. if (connector_state->crtc != &crtc->base)
  3991. continue;
  3992. encoder = to_intel_encoder(connector_state->best_encoder);
  3993. num_encoders++;
  3994. }
  3995. WARN(num_encoders != 1, "%d encoders for pipe %c\n",
  3996. num_encoders, pipe_name(crtc->pipe));
  3997. return encoder;
  3998. }
  3999. /*
  4000. * Enable PCH resources required for PCH ports:
  4001. * - PCH PLLs
  4002. * - FDI training & RX/TX
  4003. * - update transcoder timings
  4004. * - DP transcoding bits
  4005. * - transcoder
  4006. */
  4007. static void ironlake_pch_enable(const struct intel_atomic_state *state,
  4008. const struct intel_crtc_state *crtc_state)
  4009. {
  4010. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4011. struct drm_device *dev = crtc->base.dev;
  4012. struct drm_i915_private *dev_priv = to_i915(dev);
  4013. int pipe = crtc->pipe;
  4014. u32 temp;
  4015. assert_pch_transcoder_disabled(dev_priv, pipe);
  4016. if (IS_IVYBRIDGE(dev_priv))
  4017. ivybridge_update_fdi_bc_bifurcation(crtc);
  4018. /* Write the TU size bits before fdi link training, so that error
  4019. * detection works. */
  4020. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  4021. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  4022. /* For PCH output, training FDI link */
  4023. dev_priv->display.fdi_link_train(crtc, crtc_state);
  4024. /* We need to program the right clock selection before writing the pixel
  4025. * mutliplier into the DPLL. */
  4026. if (HAS_PCH_CPT(dev_priv)) {
  4027. u32 sel;
  4028. temp = I915_READ(PCH_DPLL_SEL);
  4029. temp |= TRANS_DPLL_ENABLE(pipe);
  4030. sel = TRANS_DPLLB_SEL(pipe);
  4031. if (crtc_state->shared_dpll ==
  4032. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  4033. temp |= sel;
  4034. else
  4035. temp &= ~sel;
  4036. I915_WRITE(PCH_DPLL_SEL, temp);
  4037. }
  4038. /* XXX: pch pll's can be enabled any time before we enable the PCH
  4039. * transcoder, and we actually should do this to not upset any PCH
  4040. * transcoder that already use the clock when we share it.
  4041. *
  4042. * Note that enable_shared_dpll tries to do the right thing, but
  4043. * get_shared_dpll unconditionally resets the pll - we need that to have
  4044. * the right LVDS enable sequence. */
  4045. intel_enable_shared_dpll(crtc);
  4046. /* set transcoder timing, panel must allow it */
  4047. assert_panel_unlocked(dev_priv, pipe);
  4048. ironlake_pch_transcoder_set_timings(crtc, pipe);
  4049. intel_fdi_normal_train(crtc);
  4050. /* For PCH DP, enable TRANS_DP_CTL */
  4051. if (HAS_PCH_CPT(dev_priv) &&
  4052. intel_crtc_has_dp_encoder(crtc_state)) {
  4053. const struct drm_display_mode *adjusted_mode =
  4054. &crtc_state->base.adjusted_mode;
  4055. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  4056. i915_reg_t reg = TRANS_DP_CTL(pipe);
  4057. enum port port;
  4058. temp = I915_READ(reg);
  4059. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  4060. TRANS_DP_SYNC_MASK |
  4061. TRANS_DP_BPC_MASK);
  4062. temp |= TRANS_DP_OUTPUT_ENABLE;
  4063. temp |= bpc << 9; /* same format but at 11:9 */
  4064. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  4065. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  4066. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  4067. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  4068. port = intel_get_crtc_new_encoder(state, crtc_state)->port;
  4069. WARN_ON(port < PORT_B || port > PORT_D);
  4070. temp |= TRANS_DP_PORT_SEL(port);
  4071. I915_WRITE(reg, temp);
  4072. }
  4073. ironlake_enable_pch_transcoder(dev_priv, pipe);
  4074. }
  4075. static void lpt_pch_enable(const struct intel_atomic_state *state,
  4076. const struct intel_crtc_state *crtc_state)
  4077. {
  4078. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4079. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4080. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  4081. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4082. lpt_program_iclkip(crtc);
  4083. /* Set transcoder timing. */
  4084. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4085. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4086. }
  4087. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4088. {
  4089. struct drm_i915_private *dev_priv = to_i915(dev);
  4090. i915_reg_t dslreg = PIPEDSL(pipe);
  4091. u32 temp;
  4092. temp = I915_READ(dslreg);
  4093. udelay(500);
  4094. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4095. if (wait_for(I915_READ(dslreg) != temp, 5))
  4096. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4097. }
  4098. }
  4099. /*
  4100. * The hardware phase 0.0 refers to the center of the pixel.
  4101. * We want to start from the top/left edge which is phase
  4102. * -0.5. That matches how the hardware calculates the scaling
  4103. * factors (from top-left of the first pixel to bottom-right
  4104. * of the last pixel, as opposed to the pixel centers).
  4105. *
  4106. * For 4:2:0 subsampled chroma planes we obviously have to
  4107. * adjust that so that the chroma sample position lands in
  4108. * the right spot.
  4109. *
  4110. * Note that for packed YCbCr 4:2:2 formats there is no way to
  4111. * control chroma siting. The hardware simply replicates the
  4112. * chroma samples for both of the luma samples, and thus we don't
  4113. * actually get the expected MPEG2 chroma siting convention :(
  4114. * The same behaviour is observed on pre-SKL platforms as well.
  4115. *
  4116. * Theory behind the formula (note that we ignore sub-pixel
  4117. * source coordinates):
  4118. * s = source sample position
  4119. * d = destination sample position
  4120. *
  4121. * Downscaling 4:1:
  4122. * -0.5
  4123. * | 0.0
  4124. * | | 1.5 (initial phase)
  4125. * | | |
  4126. * v v v
  4127. * | s | s | s | s |
  4128. * | d |
  4129. *
  4130. * Upscaling 1:4:
  4131. * -0.5
  4132. * | -0.375 (initial phase)
  4133. * | | 0.0
  4134. * | | |
  4135. * v v v
  4136. * | s |
  4137. * | d | d | d | d |
  4138. */
  4139. u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
  4140. {
  4141. int phase = -0x8000;
  4142. u16 trip = 0;
  4143. if (chroma_cosited)
  4144. phase += (sub - 1) * 0x8000 / sub;
  4145. phase += scale / (2 * sub);
  4146. /*
  4147. * Hardware initial phase limited to [-0.5:1.5].
  4148. * Since the max hardware scale factor is 3.0, we
  4149. * should never actually excdeed 1.0 here.
  4150. */
  4151. WARN_ON(phase < -0x8000 || phase > 0x18000);
  4152. if (phase < 0)
  4153. phase = 0x10000 + phase;
  4154. else
  4155. trip = PS_PHASE_TRIP;
  4156. return ((phase >> 2) & PS_PHASE_MASK) | trip;
  4157. }
  4158. static int
  4159. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4160. unsigned int scaler_user, int *scaler_id,
  4161. int src_w, int src_h, int dst_w, int dst_h,
  4162. bool plane_scaler_check,
  4163. uint32_t pixel_format)
  4164. {
  4165. struct intel_crtc_scaler_state *scaler_state =
  4166. &crtc_state->scaler_state;
  4167. struct intel_crtc *intel_crtc =
  4168. to_intel_crtc(crtc_state->base.crtc);
  4169. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4170. const struct drm_display_mode *adjusted_mode =
  4171. &crtc_state->base.adjusted_mode;
  4172. int need_scaling;
  4173. /*
  4174. * Src coordinates are already rotated by 270 degrees for
  4175. * the 90/270 degree plane rotation cases (to match the
  4176. * GTT mapping), hence no need to account for rotation here.
  4177. */
  4178. need_scaling = src_w != dst_w || src_h != dst_h;
  4179. if (plane_scaler_check)
  4180. if (pixel_format == DRM_FORMAT_NV12)
  4181. need_scaling = true;
  4182. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4183. need_scaling = true;
  4184. /*
  4185. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4186. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4187. * Once NV12 is enabled, handle it here while allocating scaler
  4188. * for NV12.
  4189. */
  4190. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4191. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4192. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4193. return -EINVAL;
  4194. }
  4195. /*
  4196. * if plane is being disabled or scaler is no more required or force detach
  4197. * - free scaler binded to this plane/crtc
  4198. * - in order to do this, update crtc->scaler_usage
  4199. *
  4200. * Here scaler state in crtc_state is set free so that
  4201. * scaler can be assigned to other user. Actual register
  4202. * update to free the scaler is done in plane/panel-fit programming.
  4203. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4204. */
  4205. if (force_detach || !need_scaling) {
  4206. if (*scaler_id >= 0) {
  4207. scaler_state->scaler_users &= ~(1 << scaler_user);
  4208. scaler_state->scalers[*scaler_id].in_use = 0;
  4209. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4210. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4211. intel_crtc->pipe, scaler_user, *scaler_id,
  4212. scaler_state->scaler_users);
  4213. *scaler_id = -1;
  4214. }
  4215. return 0;
  4216. }
  4217. if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
  4218. (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
  4219. DRM_DEBUG_KMS("NV12: src dimensions not met\n");
  4220. return -EINVAL;
  4221. }
  4222. /* range checks */
  4223. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4224. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4225. (IS_GEN11(dev_priv) &&
  4226. (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
  4227. dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
  4228. (!IS_GEN11(dev_priv) &&
  4229. (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4230. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
  4231. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4232. "size is out of scaler range\n",
  4233. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4234. return -EINVAL;
  4235. }
  4236. /* mark this plane as a scaler user in crtc_state */
  4237. scaler_state->scaler_users |= (1 << scaler_user);
  4238. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4239. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4240. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4241. scaler_state->scaler_users);
  4242. return 0;
  4243. }
  4244. /**
  4245. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4246. *
  4247. * @state: crtc's scaler state
  4248. *
  4249. * Return
  4250. * 0 - scaler_usage updated successfully
  4251. * error - requested scaling cannot be supported or other error condition
  4252. */
  4253. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4254. {
  4255. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4256. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4257. &state->scaler_state.scaler_id,
  4258. state->pipe_src_w, state->pipe_src_h,
  4259. adjusted_mode->crtc_hdisplay,
  4260. adjusted_mode->crtc_vdisplay, false, 0);
  4261. }
  4262. /**
  4263. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4264. * @crtc_state: crtc's scaler state
  4265. * @plane_state: atomic plane state to update
  4266. *
  4267. * Return
  4268. * 0 - scaler_usage updated successfully
  4269. * error - requested scaling cannot be supported or other error condition
  4270. */
  4271. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4272. struct intel_plane_state *plane_state)
  4273. {
  4274. struct intel_plane *intel_plane =
  4275. to_intel_plane(plane_state->base.plane);
  4276. struct drm_framebuffer *fb = plane_state->base.fb;
  4277. int ret;
  4278. bool force_detach = !fb || !plane_state->base.visible;
  4279. ret = skl_update_scaler(crtc_state, force_detach,
  4280. drm_plane_index(&intel_plane->base),
  4281. &plane_state->scaler_id,
  4282. drm_rect_width(&plane_state->base.src) >> 16,
  4283. drm_rect_height(&plane_state->base.src) >> 16,
  4284. drm_rect_width(&plane_state->base.dst),
  4285. drm_rect_height(&plane_state->base.dst),
  4286. fb ? true : false, fb ? fb->format->format : 0);
  4287. if (ret || plane_state->scaler_id < 0)
  4288. return ret;
  4289. /* check colorkey */
  4290. if (plane_state->ckey.flags) {
  4291. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4292. intel_plane->base.base.id,
  4293. intel_plane->base.name);
  4294. return -EINVAL;
  4295. }
  4296. /* Check src format */
  4297. switch (fb->format->format) {
  4298. case DRM_FORMAT_RGB565:
  4299. case DRM_FORMAT_XBGR8888:
  4300. case DRM_FORMAT_XRGB8888:
  4301. case DRM_FORMAT_ABGR8888:
  4302. case DRM_FORMAT_ARGB8888:
  4303. case DRM_FORMAT_XRGB2101010:
  4304. case DRM_FORMAT_XBGR2101010:
  4305. case DRM_FORMAT_YUYV:
  4306. case DRM_FORMAT_YVYU:
  4307. case DRM_FORMAT_UYVY:
  4308. case DRM_FORMAT_VYUY:
  4309. case DRM_FORMAT_NV12:
  4310. break;
  4311. default:
  4312. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4313. intel_plane->base.base.id, intel_plane->base.name,
  4314. fb->base.id, fb->format->format);
  4315. return -EINVAL;
  4316. }
  4317. return 0;
  4318. }
  4319. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4320. {
  4321. int i;
  4322. for (i = 0; i < crtc->num_scalers; i++)
  4323. skl_detach_scaler(crtc, i);
  4324. }
  4325. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4326. {
  4327. struct drm_device *dev = crtc->base.dev;
  4328. struct drm_i915_private *dev_priv = to_i915(dev);
  4329. int pipe = crtc->pipe;
  4330. struct intel_crtc_scaler_state *scaler_state =
  4331. &crtc->config->scaler_state;
  4332. if (crtc->config->pch_pfit.enabled) {
  4333. u16 uv_rgb_hphase, uv_rgb_vphase;
  4334. int pfit_w, pfit_h, hscale, vscale;
  4335. int id;
  4336. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4337. return;
  4338. pfit_w = (crtc->config->pch_pfit.size >> 16) & 0xFFFF;
  4339. pfit_h = crtc->config->pch_pfit.size & 0xFFFF;
  4340. hscale = (crtc->config->pipe_src_w << 16) / pfit_w;
  4341. vscale = (crtc->config->pipe_src_h << 16) / pfit_h;
  4342. uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
  4343. uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
  4344. id = scaler_state->scaler_id;
  4345. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4346. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4347. I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
  4348. PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
  4349. I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
  4350. PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
  4351. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4352. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4353. }
  4354. }
  4355. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4356. {
  4357. struct drm_device *dev = crtc->base.dev;
  4358. struct drm_i915_private *dev_priv = to_i915(dev);
  4359. int pipe = crtc->pipe;
  4360. if (crtc->config->pch_pfit.enabled) {
  4361. /* Force use of hard-coded filter coefficients
  4362. * as some pre-programmed values are broken,
  4363. * e.g. x201.
  4364. */
  4365. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4366. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4367. PF_PIPE_SEL_IVB(pipe));
  4368. else
  4369. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4370. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4371. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4372. }
  4373. }
  4374. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4375. {
  4376. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4377. struct drm_device *dev = crtc->base.dev;
  4378. struct drm_i915_private *dev_priv = to_i915(dev);
  4379. if (!crtc_state->ips_enabled)
  4380. return;
  4381. /*
  4382. * We can only enable IPS after we enable a plane and wait for a vblank
  4383. * This function is called from post_plane_update, which is run after
  4384. * a vblank wait.
  4385. */
  4386. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4387. if (IS_BROADWELL(dev_priv)) {
  4388. mutex_lock(&dev_priv->pcu_lock);
  4389. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4390. IPS_ENABLE | IPS_PCODE_CONTROL));
  4391. mutex_unlock(&dev_priv->pcu_lock);
  4392. /* Quoting Art Runyan: "its not safe to expect any particular
  4393. * value in IPS_CTL bit 31 after enabling IPS through the
  4394. * mailbox." Moreover, the mailbox may return a bogus state,
  4395. * so we need to just enable it and continue on.
  4396. */
  4397. } else {
  4398. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4399. /* The bit only becomes 1 in the next vblank, so this wait here
  4400. * is essentially intel_wait_for_vblank. If we don't have this
  4401. * and don't wait for vblanks until the end of crtc_enable, then
  4402. * the HW state readout code will complain that the expected
  4403. * IPS_CTL value is not the one we read. */
  4404. if (intel_wait_for_register(dev_priv,
  4405. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4406. 50))
  4407. DRM_ERROR("Timed out waiting for IPS enable\n");
  4408. }
  4409. }
  4410. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4411. {
  4412. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4413. struct drm_device *dev = crtc->base.dev;
  4414. struct drm_i915_private *dev_priv = to_i915(dev);
  4415. if (!crtc_state->ips_enabled)
  4416. return;
  4417. if (IS_BROADWELL(dev_priv)) {
  4418. mutex_lock(&dev_priv->pcu_lock);
  4419. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4420. mutex_unlock(&dev_priv->pcu_lock);
  4421. /*
  4422. * Wait for PCODE to finish disabling IPS. The BSpec specified
  4423. * 42ms timeout value leads to occasional timeouts so use 100ms
  4424. * instead.
  4425. */
  4426. if (intel_wait_for_register(dev_priv,
  4427. IPS_CTL, IPS_ENABLE, 0,
  4428. 100))
  4429. DRM_ERROR("Timed out waiting for IPS disable\n");
  4430. } else {
  4431. I915_WRITE(IPS_CTL, 0);
  4432. POSTING_READ(IPS_CTL);
  4433. }
  4434. /* We need to wait for a vblank before we can disable the plane. */
  4435. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4436. }
  4437. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4438. {
  4439. if (intel_crtc->overlay) {
  4440. struct drm_device *dev = intel_crtc->base.dev;
  4441. mutex_lock(&dev->struct_mutex);
  4442. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4443. mutex_unlock(&dev->struct_mutex);
  4444. }
  4445. /* Let userspace switch the overlay on again. In most cases userspace
  4446. * has to recompute where to put it anyway.
  4447. */
  4448. }
  4449. /**
  4450. * intel_post_enable_primary - Perform operations after enabling primary plane
  4451. * @crtc: the CRTC whose primary plane was just enabled
  4452. * @new_crtc_state: the enabling state
  4453. *
  4454. * Performs potentially sleeping operations that must be done after the primary
  4455. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4456. * called due to an explicit primary plane update, or due to an implicit
  4457. * re-enable that is caused when a sprite plane is updated to no longer
  4458. * completely hide the primary plane.
  4459. */
  4460. static void
  4461. intel_post_enable_primary(struct drm_crtc *crtc,
  4462. const struct intel_crtc_state *new_crtc_state)
  4463. {
  4464. struct drm_device *dev = crtc->dev;
  4465. struct drm_i915_private *dev_priv = to_i915(dev);
  4466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4467. int pipe = intel_crtc->pipe;
  4468. /*
  4469. * Gen2 reports pipe underruns whenever all planes are disabled.
  4470. * So don't enable underrun reporting before at least some planes
  4471. * are enabled.
  4472. * FIXME: Need to fix the logic to work when we turn off all planes
  4473. * but leave the pipe running.
  4474. */
  4475. if (IS_GEN2(dev_priv))
  4476. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4477. /* Underruns don't always raise interrupts, so check manually. */
  4478. intel_check_cpu_fifo_underruns(dev_priv);
  4479. intel_check_pch_fifo_underruns(dev_priv);
  4480. }
  4481. /* FIXME get rid of this and use pre_plane_update */
  4482. static void
  4483. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4484. {
  4485. struct drm_device *dev = crtc->dev;
  4486. struct drm_i915_private *dev_priv = to_i915(dev);
  4487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4488. int pipe = intel_crtc->pipe;
  4489. /*
  4490. * Gen2 reports pipe underruns whenever all planes are disabled.
  4491. * So disable underrun reporting before all the planes get disabled.
  4492. */
  4493. if (IS_GEN2(dev_priv))
  4494. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4495. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4496. /*
  4497. * Vblank time updates from the shadow to live plane control register
  4498. * are blocked if the memory self-refresh mode is active at that
  4499. * moment. So to make sure the plane gets truly disabled, disable
  4500. * first the self-refresh mode. The self-refresh enable bit in turn
  4501. * will be checked/applied by the HW only at the next frame start
  4502. * event which is after the vblank start event, so we need to have a
  4503. * wait-for-vblank between disabling the plane and the pipe.
  4504. */
  4505. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4506. intel_set_memory_cxsr(dev_priv, false))
  4507. intel_wait_for_vblank(dev_priv, pipe);
  4508. }
  4509. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4510. const struct intel_crtc_state *new_crtc_state)
  4511. {
  4512. if (!old_crtc_state->ips_enabled)
  4513. return false;
  4514. if (needs_modeset(&new_crtc_state->base))
  4515. return true;
  4516. return !new_crtc_state->ips_enabled;
  4517. }
  4518. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4519. const struct intel_crtc_state *new_crtc_state)
  4520. {
  4521. if (!new_crtc_state->ips_enabled)
  4522. return false;
  4523. if (needs_modeset(&new_crtc_state->base))
  4524. return true;
  4525. /*
  4526. * We can't read out IPS on broadwell, assume the worst and
  4527. * forcibly enable IPS on the first fastset.
  4528. */
  4529. if (new_crtc_state->update_pipe &&
  4530. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4531. return true;
  4532. return !old_crtc_state->ips_enabled;
  4533. }
  4534. static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
  4535. const struct intel_crtc_state *crtc_state)
  4536. {
  4537. if (!crtc_state->nv12_planes)
  4538. return false;
  4539. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  4540. return false;
  4541. if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
  4542. IS_CANNONLAKE(dev_priv))
  4543. return true;
  4544. return false;
  4545. }
  4546. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4547. {
  4548. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4549. struct drm_device *dev = crtc->base.dev;
  4550. struct drm_i915_private *dev_priv = to_i915(dev);
  4551. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4552. struct intel_crtc_state *pipe_config =
  4553. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4554. crtc);
  4555. struct drm_plane *primary = crtc->base.primary;
  4556. struct drm_plane_state *old_primary_state =
  4557. drm_atomic_get_old_plane_state(old_state, primary);
  4558. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4559. if (pipe_config->update_wm_post && pipe_config->base.active)
  4560. intel_update_watermarks(crtc);
  4561. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4562. hsw_enable_ips(pipe_config);
  4563. if (old_primary_state) {
  4564. struct drm_plane_state *new_primary_state =
  4565. drm_atomic_get_new_plane_state(old_state, primary);
  4566. intel_fbc_post_update(crtc);
  4567. if (new_primary_state->visible &&
  4568. (needs_modeset(&pipe_config->base) ||
  4569. !old_primary_state->visible))
  4570. intel_post_enable_primary(&crtc->base, pipe_config);
  4571. }
  4572. /* Display WA 827 */
  4573. if (needs_nv12_wa(dev_priv, old_crtc_state) &&
  4574. !needs_nv12_wa(dev_priv, pipe_config)) {
  4575. skl_wa_clkgate(dev_priv, crtc->pipe, false);
  4576. skl_wa_528(dev_priv, crtc->pipe, false);
  4577. }
  4578. }
  4579. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4580. struct intel_crtc_state *pipe_config)
  4581. {
  4582. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4583. struct drm_device *dev = crtc->base.dev;
  4584. struct drm_i915_private *dev_priv = to_i915(dev);
  4585. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4586. struct drm_plane *primary = crtc->base.primary;
  4587. struct drm_plane_state *old_primary_state =
  4588. drm_atomic_get_old_plane_state(old_state, primary);
  4589. bool modeset = needs_modeset(&pipe_config->base);
  4590. struct intel_atomic_state *old_intel_state =
  4591. to_intel_atomic_state(old_state);
  4592. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4593. hsw_disable_ips(old_crtc_state);
  4594. if (old_primary_state) {
  4595. struct intel_plane_state *new_primary_state =
  4596. intel_atomic_get_new_plane_state(old_intel_state,
  4597. to_intel_plane(primary));
  4598. intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
  4599. /*
  4600. * Gen2 reports pipe underruns whenever all planes are disabled.
  4601. * So disable underrun reporting before all the planes get disabled.
  4602. */
  4603. if (IS_GEN2(dev_priv) && old_primary_state->visible &&
  4604. (modeset || !new_primary_state->base.visible))
  4605. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4606. }
  4607. /* Display WA 827 */
  4608. if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
  4609. needs_nv12_wa(dev_priv, pipe_config)) {
  4610. skl_wa_clkgate(dev_priv, crtc->pipe, true);
  4611. skl_wa_528(dev_priv, crtc->pipe, true);
  4612. }
  4613. /*
  4614. * Vblank time updates from the shadow to live plane control register
  4615. * are blocked if the memory self-refresh mode is active at that
  4616. * moment. So to make sure the plane gets truly disabled, disable
  4617. * first the self-refresh mode. The self-refresh enable bit in turn
  4618. * will be checked/applied by the HW only at the next frame start
  4619. * event which is after the vblank start event, so we need to have a
  4620. * wait-for-vblank between disabling the plane and the pipe.
  4621. */
  4622. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4623. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4624. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4625. /*
  4626. * IVB workaround: must disable low power watermarks for at least
  4627. * one frame before enabling scaling. LP watermarks can be re-enabled
  4628. * when scaling is disabled.
  4629. *
  4630. * WaCxSRDisabledForSpriteScaling:ivb
  4631. */
  4632. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4633. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4634. /*
  4635. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4636. * watermark programming here.
  4637. */
  4638. if (needs_modeset(&pipe_config->base))
  4639. return;
  4640. /*
  4641. * For platforms that support atomic watermarks, program the
  4642. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4643. * will be the intermediate values that are safe for both pre- and
  4644. * post- vblank; when vblank happens, the 'active' values will be set
  4645. * to the final 'target' values and we'll do this again to get the
  4646. * optimal watermarks. For gen9+ platforms, the values we program here
  4647. * will be the final target values which will get automatically latched
  4648. * at vblank time; no further programming will be necessary.
  4649. *
  4650. * If a platform hasn't been transitioned to atomic watermarks yet,
  4651. * we'll continue to update watermarks the old way, if flags tell
  4652. * us to.
  4653. */
  4654. if (dev_priv->display.initial_watermarks != NULL)
  4655. dev_priv->display.initial_watermarks(old_intel_state,
  4656. pipe_config);
  4657. else if (pipe_config->update_wm_pre)
  4658. intel_update_watermarks(crtc);
  4659. }
  4660. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4661. {
  4662. struct drm_device *dev = crtc->dev;
  4663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4664. struct drm_plane *p;
  4665. int pipe = intel_crtc->pipe;
  4666. intel_crtc_dpms_overlay_disable(intel_crtc);
  4667. drm_for_each_plane_mask(p, dev, plane_mask)
  4668. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4669. /*
  4670. * FIXME: Once we grow proper nuclear flip support out of this we need
  4671. * to compute the mask of flip planes precisely. For the time being
  4672. * consider this a flip to a NULL plane.
  4673. */
  4674. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4675. }
  4676. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4677. struct intel_crtc_state *crtc_state,
  4678. struct drm_atomic_state *old_state)
  4679. {
  4680. struct drm_connector_state *conn_state;
  4681. struct drm_connector *conn;
  4682. int i;
  4683. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4684. struct intel_encoder *encoder =
  4685. to_intel_encoder(conn_state->best_encoder);
  4686. if (conn_state->crtc != crtc)
  4687. continue;
  4688. if (encoder->pre_pll_enable)
  4689. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4690. }
  4691. }
  4692. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4693. struct intel_crtc_state *crtc_state,
  4694. struct drm_atomic_state *old_state)
  4695. {
  4696. struct drm_connector_state *conn_state;
  4697. struct drm_connector *conn;
  4698. int i;
  4699. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4700. struct intel_encoder *encoder =
  4701. to_intel_encoder(conn_state->best_encoder);
  4702. if (conn_state->crtc != crtc)
  4703. continue;
  4704. if (encoder->pre_enable)
  4705. encoder->pre_enable(encoder, crtc_state, conn_state);
  4706. }
  4707. }
  4708. static void intel_encoders_enable(struct drm_crtc *crtc,
  4709. struct intel_crtc_state *crtc_state,
  4710. struct drm_atomic_state *old_state)
  4711. {
  4712. struct drm_connector_state *conn_state;
  4713. struct drm_connector *conn;
  4714. int i;
  4715. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4716. struct intel_encoder *encoder =
  4717. to_intel_encoder(conn_state->best_encoder);
  4718. if (conn_state->crtc != crtc)
  4719. continue;
  4720. encoder->enable(encoder, crtc_state, conn_state);
  4721. intel_opregion_notify_encoder(encoder, true);
  4722. }
  4723. }
  4724. static void intel_encoders_disable(struct drm_crtc *crtc,
  4725. struct intel_crtc_state *old_crtc_state,
  4726. struct drm_atomic_state *old_state)
  4727. {
  4728. struct drm_connector_state *old_conn_state;
  4729. struct drm_connector *conn;
  4730. int i;
  4731. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4732. struct intel_encoder *encoder =
  4733. to_intel_encoder(old_conn_state->best_encoder);
  4734. if (old_conn_state->crtc != crtc)
  4735. continue;
  4736. intel_opregion_notify_encoder(encoder, false);
  4737. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4738. }
  4739. }
  4740. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4741. struct intel_crtc_state *old_crtc_state,
  4742. struct drm_atomic_state *old_state)
  4743. {
  4744. struct drm_connector_state *old_conn_state;
  4745. struct drm_connector *conn;
  4746. int i;
  4747. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4748. struct intel_encoder *encoder =
  4749. to_intel_encoder(old_conn_state->best_encoder);
  4750. if (old_conn_state->crtc != crtc)
  4751. continue;
  4752. if (encoder->post_disable)
  4753. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4754. }
  4755. }
  4756. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4757. struct intel_crtc_state *old_crtc_state,
  4758. struct drm_atomic_state *old_state)
  4759. {
  4760. struct drm_connector_state *old_conn_state;
  4761. struct drm_connector *conn;
  4762. int i;
  4763. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4764. struct intel_encoder *encoder =
  4765. to_intel_encoder(old_conn_state->best_encoder);
  4766. if (old_conn_state->crtc != crtc)
  4767. continue;
  4768. if (encoder->post_pll_disable)
  4769. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4770. }
  4771. }
  4772. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4773. struct drm_atomic_state *old_state)
  4774. {
  4775. struct drm_crtc *crtc = pipe_config->base.crtc;
  4776. struct drm_device *dev = crtc->dev;
  4777. struct drm_i915_private *dev_priv = to_i915(dev);
  4778. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4779. int pipe = intel_crtc->pipe;
  4780. struct intel_atomic_state *old_intel_state =
  4781. to_intel_atomic_state(old_state);
  4782. if (WARN_ON(intel_crtc->active))
  4783. return;
  4784. /*
  4785. * Sometimes spurious CPU pipe underruns happen during FDI
  4786. * training, at least with VGA+HDMI cloning. Suppress them.
  4787. *
  4788. * On ILK we get an occasional spurious CPU pipe underruns
  4789. * between eDP port A enable and vdd enable. Also PCH port
  4790. * enable seems to result in the occasional CPU pipe underrun.
  4791. *
  4792. * Spurious PCH underruns also occur during PCH enabling.
  4793. */
  4794. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4795. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4796. if (intel_crtc->config->has_pch_encoder)
  4797. intel_prepare_shared_dpll(intel_crtc);
  4798. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4799. intel_dp_set_m_n(intel_crtc, M1_N1);
  4800. intel_set_pipe_timings(intel_crtc);
  4801. intel_set_pipe_src_size(intel_crtc);
  4802. if (intel_crtc->config->has_pch_encoder) {
  4803. intel_cpu_transcoder_set_m_n(intel_crtc,
  4804. &intel_crtc->config->fdi_m_n, NULL);
  4805. }
  4806. ironlake_set_pipeconf(crtc);
  4807. intel_crtc->active = true;
  4808. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4809. if (intel_crtc->config->has_pch_encoder) {
  4810. /* Note: FDI PLL enabling _must_ be done before we enable the
  4811. * cpu pipes, hence this is separate from all the other fdi/pch
  4812. * enabling. */
  4813. ironlake_fdi_pll_enable(intel_crtc);
  4814. } else {
  4815. assert_fdi_tx_disabled(dev_priv, pipe);
  4816. assert_fdi_rx_disabled(dev_priv, pipe);
  4817. }
  4818. ironlake_pfit_enable(intel_crtc);
  4819. /*
  4820. * On ILK+ LUT must be loaded before the pipe is running but with
  4821. * clocks enabled
  4822. */
  4823. intel_color_load_luts(&pipe_config->base);
  4824. if (dev_priv->display.initial_watermarks != NULL)
  4825. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4826. intel_enable_pipe(pipe_config);
  4827. if (intel_crtc->config->has_pch_encoder)
  4828. ironlake_pch_enable(old_intel_state, pipe_config);
  4829. assert_vblank_disabled(crtc);
  4830. drm_crtc_vblank_on(crtc);
  4831. intel_encoders_enable(crtc, pipe_config, old_state);
  4832. if (HAS_PCH_CPT(dev_priv))
  4833. cpt_verify_modeset(dev, intel_crtc->pipe);
  4834. /*
  4835. * Must wait for vblank to avoid spurious PCH FIFO underruns.
  4836. * And a second vblank wait is needed at least on ILK with
  4837. * some interlaced HDMI modes. Let's do the double wait always
  4838. * in case there are more corner cases we don't know about.
  4839. */
  4840. if (intel_crtc->config->has_pch_encoder) {
  4841. intel_wait_for_vblank(dev_priv, pipe);
  4842. intel_wait_for_vblank(dev_priv, pipe);
  4843. }
  4844. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4845. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4846. }
  4847. /* IPS only exists on ULT machines and is tied to pipe A. */
  4848. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4849. {
  4850. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4851. }
  4852. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4853. enum pipe pipe, bool apply)
  4854. {
  4855. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4856. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4857. if (apply)
  4858. val |= mask;
  4859. else
  4860. val &= ~mask;
  4861. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4862. }
  4863. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4864. {
  4865. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4866. enum pipe pipe = crtc->pipe;
  4867. uint32_t val;
  4868. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4869. /* Program B credit equally to all pipes */
  4870. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4871. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4872. }
  4873. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4874. struct drm_atomic_state *old_state)
  4875. {
  4876. struct drm_crtc *crtc = pipe_config->base.crtc;
  4877. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4878. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4879. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4880. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4881. struct intel_atomic_state *old_intel_state =
  4882. to_intel_atomic_state(old_state);
  4883. bool psl_clkgate_wa;
  4884. u32 pipe_chicken;
  4885. if (WARN_ON(intel_crtc->active))
  4886. return;
  4887. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4888. if (intel_crtc->config->shared_dpll)
  4889. intel_enable_shared_dpll(intel_crtc);
  4890. if (INTEL_GEN(dev_priv) >= 11)
  4891. icl_map_plls_to_ports(crtc, pipe_config, old_state);
  4892. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4893. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4894. intel_dp_set_m_n(intel_crtc, M1_N1);
  4895. if (!transcoder_is_dsi(cpu_transcoder))
  4896. intel_set_pipe_timings(intel_crtc);
  4897. intel_set_pipe_src_size(intel_crtc);
  4898. if (cpu_transcoder != TRANSCODER_EDP &&
  4899. !transcoder_is_dsi(cpu_transcoder)) {
  4900. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4901. intel_crtc->config->pixel_multiplier - 1);
  4902. }
  4903. if (intel_crtc->config->has_pch_encoder) {
  4904. intel_cpu_transcoder_set_m_n(intel_crtc,
  4905. &intel_crtc->config->fdi_m_n, NULL);
  4906. }
  4907. if (!transcoder_is_dsi(cpu_transcoder))
  4908. haswell_set_pipeconf(crtc);
  4909. haswell_set_pipemisc(crtc);
  4910. intel_color_set_csc(&pipe_config->base);
  4911. intel_crtc->active = true;
  4912. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4913. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4914. intel_crtc->config->pch_pfit.enabled;
  4915. if (psl_clkgate_wa)
  4916. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4917. if (INTEL_GEN(dev_priv) >= 9)
  4918. skylake_pfit_enable(intel_crtc);
  4919. else
  4920. ironlake_pfit_enable(intel_crtc);
  4921. /*
  4922. * On ILK+ LUT must be loaded before the pipe is running but with
  4923. * clocks enabled
  4924. */
  4925. intel_color_load_luts(&pipe_config->base);
  4926. /*
  4927. * Display WA #1153: enable hardware to bypass the alpha math
  4928. * and rounding for per-pixel values 00 and 0xff
  4929. */
  4930. if (INTEL_GEN(dev_priv) >= 11) {
  4931. pipe_chicken = I915_READ(PIPE_CHICKEN(pipe));
  4932. if (!(pipe_chicken & PER_PIXEL_ALPHA_BYPASS_EN))
  4933. I915_WRITE_FW(PIPE_CHICKEN(pipe),
  4934. pipe_chicken | PER_PIXEL_ALPHA_BYPASS_EN);
  4935. }
  4936. intel_ddi_set_pipe_settings(pipe_config);
  4937. if (!transcoder_is_dsi(cpu_transcoder))
  4938. intel_ddi_enable_transcoder_func(pipe_config);
  4939. if (dev_priv->display.initial_watermarks != NULL)
  4940. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4941. if (INTEL_GEN(dev_priv) >= 11)
  4942. icl_pipe_mbus_enable(intel_crtc);
  4943. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4944. if (!transcoder_is_dsi(cpu_transcoder))
  4945. intel_enable_pipe(pipe_config);
  4946. if (intel_crtc->config->has_pch_encoder)
  4947. lpt_pch_enable(old_intel_state, pipe_config);
  4948. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4949. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4950. assert_vblank_disabled(crtc);
  4951. drm_crtc_vblank_on(crtc);
  4952. intel_encoders_enable(crtc, pipe_config, old_state);
  4953. if (psl_clkgate_wa) {
  4954. intel_wait_for_vblank(dev_priv, pipe);
  4955. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4956. }
  4957. /* If we change the relative order between pipe/planes enabling, we need
  4958. * to change the workaround. */
  4959. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4960. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4961. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4962. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4963. }
  4964. }
  4965. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4966. {
  4967. struct drm_device *dev = crtc->base.dev;
  4968. struct drm_i915_private *dev_priv = to_i915(dev);
  4969. int pipe = crtc->pipe;
  4970. /* To avoid upsetting the power well on haswell only disable the pfit if
  4971. * it's in use. The hw state code will make sure we get this right. */
  4972. if (force || crtc->config->pch_pfit.enabled) {
  4973. I915_WRITE(PF_CTL(pipe), 0);
  4974. I915_WRITE(PF_WIN_POS(pipe), 0);
  4975. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4976. }
  4977. }
  4978. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4979. struct drm_atomic_state *old_state)
  4980. {
  4981. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4982. struct drm_device *dev = crtc->dev;
  4983. struct drm_i915_private *dev_priv = to_i915(dev);
  4984. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4985. int pipe = intel_crtc->pipe;
  4986. /*
  4987. * Sometimes spurious CPU pipe underruns happen when the
  4988. * pipe is already disabled, but FDI RX/TX is still enabled.
  4989. * Happens at least with VGA+HDMI cloning. Suppress them.
  4990. */
  4991. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4992. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4993. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4994. drm_crtc_vblank_off(crtc);
  4995. assert_vblank_disabled(crtc);
  4996. intel_disable_pipe(old_crtc_state);
  4997. ironlake_pfit_disable(intel_crtc, false);
  4998. if (intel_crtc->config->has_pch_encoder)
  4999. ironlake_fdi_disable(crtc);
  5000. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5001. if (intel_crtc->config->has_pch_encoder) {
  5002. ironlake_disable_pch_transcoder(dev_priv, pipe);
  5003. if (HAS_PCH_CPT(dev_priv)) {
  5004. i915_reg_t reg;
  5005. u32 temp;
  5006. /* disable TRANS_DP_CTL */
  5007. reg = TRANS_DP_CTL(pipe);
  5008. temp = I915_READ(reg);
  5009. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  5010. TRANS_DP_PORT_SEL_MASK);
  5011. temp |= TRANS_DP_PORT_SEL_NONE;
  5012. I915_WRITE(reg, temp);
  5013. /* disable DPLL_SEL */
  5014. temp = I915_READ(PCH_DPLL_SEL);
  5015. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  5016. I915_WRITE(PCH_DPLL_SEL, temp);
  5017. }
  5018. ironlake_fdi_pll_disable(intel_crtc);
  5019. }
  5020. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5021. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  5022. }
  5023. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5024. struct drm_atomic_state *old_state)
  5025. {
  5026. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5027. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5029. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  5030. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5031. drm_crtc_vblank_off(crtc);
  5032. assert_vblank_disabled(crtc);
  5033. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  5034. if (!transcoder_is_dsi(cpu_transcoder))
  5035. intel_disable_pipe(old_crtc_state);
  5036. if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
  5037. intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
  5038. if (!transcoder_is_dsi(cpu_transcoder))
  5039. intel_ddi_disable_transcoder_func(old_crtc_state);
  5040. if (INTEL_GEN(dev_priv) >= 9)
  5041. skylake_scaler_disable(intel_crtc);
  5042. else
  5043. ironlake_pfit_disable(intel_crtc, false);
  5044. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5045. if (INTEL_GEN(dev_priv) >= 11)
  5046. icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
  5047. }
  5048. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  5049. {
  5050. struct drm_device *dev = crtc->base.dev;
  5051. struct drm_i915_private *dev_priv = to_i915(dev);
  5052. struct intel_crtc_state *pipe_config = crtc->config;
  5053. if (!pipe_config->gmch_pfit.control)
  5054. return;
  5055. /*
  5056. * The panel fitter should only be adjusted whilst the pipe is disabled,
  5057. * according to register description and PRM.
  5058. */
  5059. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  5060. assert_pipe_disabled(dev_priv, crtc->pipe);
  5061. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  5062. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  5063. /* Border color in case we don't scale up to the full screen. Black by
  5064. * default, change to something else for debugging. */
  5065. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  5066. }
  5067. bool intel_port_is_combophy(struct drm_i915_private *dev_priv, enum port port)
  5068. {
  5069. if (port == PORT_NONE)
  5070. return false;
  5071. if (IS_ICELAKE(dev_priv))
  5072. return port <= PORT_B;
  5073. return false;
  5074. }
  5075. bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
  5076. {
  5077. if (IS_ICELAKE(dev_priv))
  5078. return port >= PORT_C && port <= PORT_F;
  5079. return false;
  5080. }
  5081. enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
  5082. {
  5083. if (!intel_port_is_tc(dev_priv, port))
  5084. return PORT_TC_NONE;
  5085. return port - PORT_C;
  5086. }
  5087. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  5088. {
  5089. switch (port) {
  5090. case PORT_A:
  5091. return POWER_DOMAIN_PORT_DDI_A_LANES;
  5092. case PORT_B:
  5093. return POWER_DOMAIN_PORT_DDI_B_LANES;
  5094. case PORT_C:
  5095. return POWER_DOMAIN_PORT_DDI_C_LANES;
  5096. case PORT_D:
  5097. return POWER_DOMAIN_PORT_DDI_D_LANES;
  5098. case PORT_E:
  5099. return POWER_DOMAIN_PORT_DDI_E_LANES;
  5100. case PORT_F:
  5101. return POWER_DOMAIN_PORT_DDI_F_LANES;
  5102. default:
  5103. MISSING_CASE(port);
  5104. return POWER_DOMAIN_PORT_OTHER;
  5105. }
  5106. }
  5107. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  5108. struct intel_crtc_state *crtc_state)
  5109. {
  5110. struct drm_device *dev = crtc->dev;
  5111. struct drm_i915_private *dev_priv = to_i915(dev);
  5112. struct drm_encoder *encoder;
  5113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5114. enum pipe pipe = intel_crtc->pipe;
  5115. u64 mask;
  5116. enum transcoder transcoder = crtc_state->cpu_transcoder;
  5117. if (!crtc_state->base.active)
  5118. return 0;
  5119. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  5120. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  5121. if (crtc_state->pch_pfit.enabled ||
  5122. crtc_state->pch_pfit.force_thru)
  5123. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  5124. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  5125. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5126. mask |= BIT_ULL(intel_encoder->power_domain);
  5127. }
  5128. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  5129. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  5130. if (crtc_state->shared_dpll)
  5131. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  5132. return mask;
  5133. }
  5134. static u64
  5135. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  5136. struct intel_crtc_state *crtc_state)
  5137. {
  5138. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5140. enum intel_display_power_domain domain;
  5141. u64 domains, new_domains, old_domains;
  5142. old_domains = intel_crtc->enabled_power_domains;
  5143. intel_crtc->enabled_power_domains = new_domains =
  5144. get_crtc_power_domains(crtc, crtc_state);
  5145. domains = new_domains & ~old_domains;
  5146. for_each_power_domain(domain, domains)
  5147. intel_display_power_get(dev_priv, domain);
  5148. return old_domains & ~new_domains;
  5149. }
  5150. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  5151. u64 domains)
  5152. {
  5153. enum intel_display_power_domain domain;
  5154. for_each_power_domain(domain, domains)
  5155. intel_display_power_put(dev_priv, domain);
  5156. }
  5157. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5158. struct drm_atomic_state *old_state)
  5159. {
  5160. struct intel_atomic_state *old_intel_state =
  5161. to_intel_atomic_state(old_state);
  5162. struct drm_crtc *crtc = pipe_config->base.crtc;
  5163. struct drm_device *dev = crtc->dev;
  5164. struct drm_i915_private *dev_priv = to_i915(dev);
  5165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5166. int pipe = intel_crtc->pipe;
  5167. if (WARN_ON(intel_crtc->active))
  5168. return;
  5169. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5170. intel_dp_set_m_n(intel_crtc, M1_N1);
  5171. intel_set_pipe_timings(intel_crtc);
  5172. intel_set_pipe_src_size(intel_crtc);
  5173. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5174. struct drm_i915_private *dev_priv = to_i915(dev);
  5175. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5176. I915_WRITE(CHV_CANVAS(pipe), 0);
  5177. }
  5178. i9xx_set_pipeconf(intel_crtc);
  5179. intel_color_set_csc(&pipe_config->base);
  5180. intel_crtc->active = true;
  5181. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5182. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5183. if (IS_CHERRYVIEW(dev_priv)) {
  5184. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5185. chv_enable_pll(intel_crtc, intel_crtc->config);
  5186. } else {
  5187. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5188. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5189. }
  5190. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5191. i9xx_pfit_enable(intel_crtc);
  5192. intel_color_load_luts(&pipe_config->base);
  5193. dev_priv->display.initial_watermarks(old_intel_state,
  5194. pipe_config);
  5195. intel_enable_pipe(pipe_config);
  5196. assert_vblank_disabled(crtc);
  5197. drm_crtc_vblank_on(crtc);
  5198. intel_encoders_enable(crtc, pipe_config, old_state);
  5199. }
  5200. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5201. {
  5202. struct drm_device *dev = crtc->base.dev;
  5203. struct drm_i915_private *dev_priv = to_i915(dev);
  5204. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5205. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5206. }
  5207. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5208. struct drm_atomic_state *old_state)
  5209. {
  5210. struct intel_atomic_state *old_intel_state =
  5211. to_intel_atomic_state(old_state);
  5212. struct drm_crtc *crtc = pipe_config->base.crtc;
  5213. struct drm_device *dev = crtc->dev;
  5214. struct drm_i915_private *dev_priv = to_i915(dev);
  5215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5216. enum pipe pipe = intel_crtc->pipe;
  5217. if (WARN_ON(intel_crtc->active))
  5218. return;
  5219. i9xx_set_pll_dividers(intel_crtc);
  5220. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5221. intel_dp_set_m_n(intel_crtc, M1_N1);
  5222. intel_set_pipe_timings(intel_crtc);
  5223. intel_set_pipe_src_size(intel_crtc);
  5224. i9xx_set_pipeconf(intel_crtc);
  5225. intel_crtc->active = true;
  5226. if (!IS_GEN2(dev_priv))
  5227. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5228. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5229. i9xx_enable_pll(intel_crtc, pipe_config);
  5230. i9xx_pfit_enable(intel_crtc);
  5231. intel_color_load_luts(&pipe_config->base);
  5232. if (dev_priv->display.initial_watermarks != NULL)
  5233. dev_priv->display.initial_watermarks(old_intel_state,
  5234. intel_crtc->config);
  5235. else
  5236. intel_update_watermarks(intel_crtc);
  5237. intel_enable_pipe(pipe_config);
  5238. assert_vblank_disabled(crtc);
  5239. drm_crtc_vblank_on(crtc);
  5240. intel_encoders_enable(crtc, pipe_config, old_state);
  5241. }
  5242. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5243. {
  5244. struct drm_device *dev = crtc->base.dev;
  5245. struct drm_i915_private *dev_priv = to_i915(dev);
  5246. if (!crtc->config->gmch_pfit.control)
  5247. return;
  5248. assert_pipe_disabled(dev_priv, crtc->pipe);
  5249. DRM_DEBUG_KMS("disabling pfit, current: 0x%08x\n",
  5250. I915_READ(PFIT_CONTROL));
  5251. I915_WRITE(PFIT_CONTROL, 0);
  5252. }
  5253. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5254. struct drm_atomic_state *old_state)
  5255. {
  5256. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5257. struct drm_device *dev = crtc->dev;
  5258. struct drm_i915_private *dev_priv = to_i915(dev);
  5259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5260. int pipe = intel_crtc->pipe;
  5261. /*
  5262. * On gen2 planes are double buffered but the pipe isn't, so we must
  5263. * wait for planes to fully turn off before disabling the pipe.
  5264. */
  5265. if (IS_GEN2(dev_priv))
  5266. intel_wait_for_vblank(dev_priv, pipe);
  5267. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5268. drm_crtc_vblank_off(crtc);
  5269. assert_vblank_disabled(crtc);
  5270. intel_disable_pipe(old_crtc_state);
  5271. i9xx_pfit_disable(intel_crtc);
  5272. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5273. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5274. if (IS_CHERRYVIEW(dev_priv))
  5275. chv_disable_pll(dev_priv, pipe);
  5276. else if (IS_VALLEYVIEW(dev_priv))
  5277. vlv_disable_pll(dev_priv, pipe);
  5278. else
  5279. i9xx_disable_pll(intel_crtc);
  5280. }
  5281. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5282. if (!IS_GEN2(dev_priv))
  5283. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5284. if (!dev_priv->display.initial_watermarks)
  5285. intel_update_watermarks(intel_crtc);
  5286. /* clock the pipe down to 640x480@60 to potentially save power */
  5287. if (IS_I830(dev_priv))
  5288. i830_enable_pipe(dev_priv, pipe);
  5289. }
  5290. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5291. struct drm_modeset_acquire_ctx *ctx)
  5292. {
  5293. struct intel_encoder *encoder;
  5294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5295. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5296. enum intel_display_power_domain domain;
  5297. struct intel_plane *plane;
  5298. u64 domains;
  5299. struct drm_atomic_state *state;
  5300. struct intel_crtc_state *crtc_state;
  5301. int ret;
  5302. if (!intel_crtc->active)
  5303. return;
  5304. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5305. const struct intel_plane_state *plane_state =
  5306. to_intel_plane_state(plane->base.state);
  5307. if (plane_state->base.visible)
  5308. intel_plane_disable_noatomic(intel_crtc, plane);
  5309. }
  5310. state = drm_atomic_state_alloc(crtc->dev);
  5311. if (!state) {
  5312. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5313. crtc->base.id, crtc->name);
  5314. return;
  5315. }
  5316. state->acquire_ctx = ctx;
  5317. /* Everything's already locked, -EDEADLK can't happen. */
  5318. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5319. ret = drm_atomic_add_affected_connectors(state, crtc);
  5320. WARN_ON(IS_ERR(crtc_state) || ret);
  5321. dev_priv->display.crtc_disable(crtc_state, state);
  5322. drm_atomic_state_put(state);
  5323. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5324. crtc->base.id, crtc->name);
  5325. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5326. crtc->state->active = false;
  5327. intel_crtc->active = false;
  5328. crtc->enabled = false;
  5329. crtc->state->connector_mask = 0;
  5330. crtc->state->encoder_mask = 0;
  5331. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5332. encoder->base.crtc = NULL;
  5333. intel_fbc_disable(intel_crtc);
  5334. intel_update_watermarks(intel_crtc);
  5335. intel_disable_shared_dpll(intel_crtc);
  5336. domains = intel_crtc->enabled_power_domains;
  5337. for_each_power_domain(domain, domains)
  5338. intel_display_power_put(dev_priv, domain);
  5339. intel_crtc->enabled_power_domains = 0;
  5340. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5341. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5342. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5343. }
  5344. /*
  5345. * turn all crtc's off, but do not adjust state
  5346. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5347. */
  5348. int intel_display_suspend(struct drm_device *dev)
  5349. {
  5350. struct drm_i915_private *dev_priv = to_i915(dev);
  5351. struct drm_atomic_state *state;
  5352. int ret;
  5353. state = drm_atomic_helper_suspend(dev);
  5354. ret = PTR_ERR_OR_ZERO(state);
  5355. if (ret)
  5356. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5357. else
  5358. dev_priv->modeset_restore_state = state;
  5359. return ret;
  5360. }
  5361. void intel_encoder_destroy(struct drm_encoder *encoder)
  5362. {
  5363. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5364. drm_encoder_cleanup(encoder);
  5365. kfree(intel_encoder);
  5366. }
  5367. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5368. * internal consistency). */
  5369. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5370. struct drm_connector_state *conn_state)
  5371. {
  5372. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5373. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5374. connector->base.base.id,
  5375. connector->base.name);
  5376. if (connector->get_hw_state(connector)) {
  5377. struct intel_encoder *encoder = connector->encoder;
  5378. I915_STATE_WARN(!crtc_state,
  5379. "connector enabled without attached crtc\n");
  5380. if (!crtc_state)
  5381. return;
  5382. I915_STATE_WARN(!crtc_state->active,
  5383. "connector is active, but attached crtc isn't\n");
  5384. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5385. return;
  5386. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5387. "atomic encoder doesn't match attached encoder\n");
  5388. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5389. "attached encoder crtc differs from connector crtc\n");
  5390. } else {
  5391. I915_STATE_WARN(crtc_state && crtc_state->active,
  5392. "attached crtc is active, but connector isn't\n");
  5393. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5394. "best encoder set without crtc!\n");
  5395. }
  5396. }
  5397. int intel_connector_init(struct intel_connector *connector)
  5398. {
  5399. struct intel_digital_connector_state *conn_state;
  5400. /*
  5401. * Allocate enough memory to hold intel_digital_connector_state,
  5402. * This might be a few bytes too many, but for connectors that don't
  5403. * need it we'll free the state and allocate a smaller one on the first
  5404. * succesful commit anyway.
  5405. */
  5406. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5407. if (!conn_state)
  5408. return -ENOMEM;
  5409. __drm_atomic_helper_connector_reset(&connector->base,
  5410. &conn_state->base);
  5411. return 0;
  5412. }
  5413. struct intel_connector *intel_connector_alloc(void)
  5414. {
  5415. struct intel_connector *connector;
  5416. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5417. if (!connector)
  5418. return NULL;
  5419. if (intel_connector_init(connector) < 0) {
  5420. kfree(connector);
  5421. return NULL;
  5422. }
  5423. return connector;
  5424. }
  5425. /*
  5426. * Free the bits allocated by intel_connector_alloc.
  5427. * This should only be used after intel_connector_alloc has returned
  5428. * successfully, and before drm_connector_init returns successfully.
  5429. * Otherwise the destroy callbacks for the connector and the state should
  5430. * take care of proper cleanup/free
  5431. */
  5432. void intel_connector_free(struct intel_connector *connector)
  5433. {
  5434. kfree(to_intel_digital_connector_state(connector->base.state));
  5435. kfree(connector);
  5436. }
  5437. /* Simple connector->get_hw_state implementation for encoders that support only
  5438. * one connector and no cloning and hence the encoder state determines the state
  5439. * of the connector. */
  5440. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5441. {
  5442. enum pipe pipe = 0;
  5443. struct intel_encoder *encoder = connector->encoder;
  5444. return encoder->get_hw_state(encoder, &pipe);
  5445. }
  5446. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5447. {
  5448. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5449. return crtc_state->fdi_lanes;
  5450. return 0;
  5451. }
  5452. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5453. struct intel_crtc_state *pipe_config)
  5454. {
  5455. struct drm_i915_private *dev_priv = to_i915(dev);
  5456. struct drm_atomic_state *state = pipe_config->base.state;
  5457. struct intel_crtc *other_crtc;
  5458. struct intel_crtc_state *other_crtc_state;
  5459. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5460. pipe_name(pipe), pipe_config->fdi_lanes);
  5461. if (pipe_config->fdi_lanes > 4) {
  5462. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5463. pipe_name(pipe), pipe_config->fdi_lanes);
  5464. return -EINVAL;
  5465. }
  5466. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5467. if (pipe_config->fdi_lanes > 2) {
  5468. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5469. pipe_config->fdi_lanes);
  5470. return -EINVAL;
  5471. } else {
  5472. return 0;
  5473. }
  5474. }
  5475. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5476. return 0;
  5477. /* Ivybridge 3 pipe is really complicated */
  5478. switch (pipe) {
  5479. case PIPE_A:
  5480. return 0;
  5481. case PIPE_B:
  5482. if (pipe_config->fdi_lanes <= 2)
  5483. return 0;
  5484. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5485. other_crtc_state =
  5486. intel_atomic_get_crtc_state(state, other_crtc);
  5487. if (IS_ERR(other_crtc_state))
  5488. return PTR_ERR(other_crtc_state);
  5489. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5490. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5491. pipe_name(pipe), pipe_config->fdi_lanes);
  5492. return -EINVAL;
  5493. }
  5494. return 0;
  5495. case PIPE_C:
  5496. if (pipe_config->fdi_lanes > 2) {
  5497. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5498. pipe_name(pipe), pipe_config->fdi_lanes);
  5499. return -EINVAL;
  5500. }
  5501. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5502. other_crtc_state =
  5503. intel_atomic_get_crtc_state(state, other_crtc);
  5504. if (IS_ERR(other_crtc_state))
  5505. return PTR_ERR(other_crtc_state);
  5506. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5507. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5508. return -EINVAL;
  5509. }
  5510. return 0;
  5511. default:
  5512. BUG();
  5513. }
  5514. }
  5515. #define RETRY 1
  5516. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5517. struct intel_crtc_state *pipe_config)
  5518. {
  5519. struct drm_device *dev = intel_crtc->base.dev;
  5520. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5521. int lane, link_bw, fdi_dotclock, ret;
  5522. bool needs_recompute = false;
  5523. retry:
  5524. /* FDI is a binary signal running at ~2.7GHz, encoding
  5525. * each output octet as 10 bits. The actual frequency
  5526. * is stored as a divider into a 100MHz clock, and the
  5527. * mode pixel clock is stored in units of 1KHz.
  5528. * Hence the bw of each lane in terms of the mode signal
  5529. * is:
  5530. */
  5531. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5532. fdi_dotclock = adjusted_mode->crtc_clock;
  5533. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5534. pipe_config->pipe_bpp);
  5535. pipe_config->fdi_lanes = lane;
  5536. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5537. link_bw, &pipe_config->fdi_m_n, false);
  5538. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5539. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5540. pipe_config->pipe_bpp -= 2*3;
  5541. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5542. pipe_config->pipe_bpp);
  5543. needs_recompute = true;
  5544. pipe_config->bw_constrained = true;
  5545. goto retry;
  5546. }
  5547. if (needs_recompute)
  5548. return RETRY;
  5549. return ret;
  5550. }
  5551. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5552. {
  5553. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5554. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5555. /* IPS only exists on ULT machines and is tied to pipe A. */
  5556. if (!hsw_crtc_supports_ips(crtc))
  5557. return false;
  5558. if (!i915_modparams.enable_ips)
  5559. return false;
  5560. if (crtc_state->pipe_bpp > 24)
  5561. return false;
  5562. /*
  5563. * We compare against max which means we must take
  5564. * the increased cdclk requirement into account when
  5565. * calculating the new cdclk.
  5566. *
  5567. * Should measure whether using a lower cdclk w/o IPS
  5568. */
  5569. if (IS_BROADWELL(dev_priv) &&
  5570. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5571. return false;
  5572. return true;
  5573. }
  5574. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5575. {
  5576. struct drm_i915_private *dev_priv =
  5577. to_i915(crtc_state->base.crtc->dev);
  5578. struct intel_atomic_state *intel_state =
  5579. to_intel_atomic_state(crtc_state->base.state);
  5580. if (!hsw_crtc_state_ips_capable(crtc_state))
  5581. return false;
  5582. if (crtc_state->ips_force_disable)
  5583. return false;
  5584. /* IPS should be fine as long as at least one plane is enabled. */
  5585. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5586. return false;
  5587. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5588. if (IS_BROADWELL(dev_priv) &&
  5589. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5590. return false;
  5591. return true;
  5592. }
  5593. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5594. {
  5595. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5596. /* GDG double wide on either pipe, otherwise pipe A only */
  5597. return INTEL_GEN(dev_priv) < 4 &&
  5598. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5599. }
  5600. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5601. {
  5602. uint32_t pixel_rate;
  5603. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5604. /*
  5605. * We only use IF-ID interlacing. If we ever use
  5606. * PF-ID we'll need to adjust the pixel_rate here.
  5607. */
  5608. if (pipe_config->pch_pfit.enabled) {
  5609. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5610. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5611. pipe_w = pipe_config->pipe_src_w;
  5612. pipe_h = pipe_config->pipe_src_h;
  5613. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5614. pfit_h = pfit_size & 0xFFFF;
  5615. if (pipe_w < pfit_w)
  5616. pipe_w = pfit_w;
  5617. if (pipe_h < pfit_h)
  5618. pipe_h = pfit_h;
  5619. if (WARN_ON(!pfit_w || !pfit_h))
  5620. return pixel_rate;
  5621. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5622. pfit_w * pfit_h);
  5623. }
  5624. return pixel_rate;
  5625. }
  5626. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5627. {
  5628. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5629. if (HAS_GMCH_DISPLAY(dev_priv))
  5630. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5631. crtc_state->pixel_rate =
  5632. crtc_state->base.adjusted_mode.crtc_clock;
  5633. else
  5634. crtc_state->pixel_rate =
  5635. ilk_pipe_pixel_rate(crtc_state);
  5636. }
  5637. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5638. struct intel_crtc_state *pipe_config)
  5639. {
  5640. struct drm_device *dev = crtc->base.dev;
  5641. struct drm_i915_private *dev_priv = to_i915(dev);
  5642. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5643. int clock_limit = dev_priv->max_dotclk_freq;
  5644. if (INTEL_GEN(dev_priv) < 4) {
  5645. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5646. /*
  5647. * Enable double wide mode when the dot clock
  5648. * is > 90% of the (display) core speed.
  5649. */
  5650. if (intel_crtc_supports_double_wide(crtc) &&
  5651. adjusted_mode->crtc_clock > clock_limit) {
  5652. clock_limit = dev_priv->max_dotclk_freq;
  5653. pipe_config->double_wide = true;
  5654. }
  5655. }
  5656. if (adjusted_mode->crtc_clock > clock_limit) {
  5657. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5658. adjusted_mode->crtc_clock, clock_limit,
  5659. yesno(pipe_config->double_wide));
  5660. return -EINVAL;
  5661. }
  5662. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5663. /*
  5664. * There is only one pipe CSC unit per pipe, and we need that
  5665. * for output conversion from RGB->YCBCR. So if CTM is already
  5666. * applied we can't support YCBCR420 output.
  5667. */
  5668. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5669. return -EINVAL;
  5670. }
  5671. /*
  5672. * Pipe horizontal size must be even in:
  5673. * - DVO ganged mode
  5674. * - LVDS dual channel mode
  5675. * - Double wide pipe
  5676. */
  5677. if (pipe_config->pipe_src_w & 1) {
  5678. if (pipe_config->double_wide) {
  5679. DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
  5680. return -EINVAL;
  5681. }
  5682. if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5683. intel_is_dual_link_lvds(dev)) {
  5684. DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
  5685. return -EINVAL;
  5686. }
  5687. }
  5688. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5689. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5690. */
  5691. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5692. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5693. return -EINVAL;
  5694. intel_crtc_compute_pixel_rate(pipe_config);
  5695. if (pipe_config->has_pch_encoder)
  5696. return ironlake_fdi_compute_config(crtc, pipe_config);
  5697. return 0;
  5698. }
  5699. static void
  5700. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5701. {
  5702. while (*num > DATA_LINK_M_N_MASK ||
  5703. *den > DATA_LINK_M_N_MASK) {
  5704. *num >>= 1;
  5705. *den >>= 1;
  5706. }
  5707. }
  5708. static void compute_m_n(unsigned int m, unsigned int n,
  5709. uint32_t *ret_m, uint32_t *ret_n,
  5710. bool constant_n)
  5711. {
  5712. /*
  5713. * Several DP dongles in particular seem to be fussy about
  5714. * too large link M/N values. Give N value as 0x8000 that
  5715. * should be acceptable by specific devices. 0x8000 is the
  5716. * specified fixed N value for asynchronous clock mode,
  5717. * which the devices expect also in synchronous clock mode.
  5718. */
  5719. if (constant_n)
  5720. *ret_n = 0x8000;
  5721. else
  5722. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5723. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5724. intel_reduce_m_n_ratio(ret_m, ret_n);
  5725. }
  5726. void
  5727. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5728. int pixel_clock, int link_clock,
  5729. struct intel_link_m_n *m_n,
  5730. bool constant_n)
  5731. {
  5732. m_n->tu = 64;
  5733. compute_m_n(bits_per_pixel * pixel_clock,
  5734. link_clock * nlanes * 8,
  5735. &m_n->gmch_m, &m_n->gmch_n,
  5736. constant_n);
  5737. compute_m_n(pixel_clock, link_clock,
  5738. &m_n->link_m, &m_n->link_n,
  5739. constant_n);
  5740. }
  5741. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5742. {
  5743. if (i915_modparams.panel_use_ssc >= 0)
  5744. return i915_modparams.panel_use_ssc != 0;
  5745. return dev_priv->vbt.lvds_use_ssc
  5746. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5747. }
  5748. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5749. {
  5750. return (1 << dpll->n) << 16 | dpll->m2;
  5751. }
  5752. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5753. {
  5754. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5755. }
  5756. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5757. struct intel_crtc_state *crtc_state,
  5758. struct dpll *reduced_clock)
  5759. {
  5760. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5761. u32 fp, fp2 = 0;
  5762. if (IS_PINEVIEW(dev_priv)) {
  5763. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5764. if (reduced_clock)
  5765. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5766. } else {
  5767. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5768. if (reduced_clock)
  5769. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5770. }
  5771. crtc_state->dpll_hw_state.fp0 = fp;
  5772. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5773. reduced_clock) {
  5774. crtc_state->dpll_hw_state.fp1 = fp2;
  5775. } else {
  5776. crtc_state->dpll_hw_state.fp1 = fp;
  5777. }
  5778. }
  5779. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5780. pipe)
  5781. {
  5782. u32 reg_val;
  5783. /*
  5784. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5785. * and set it to a reasonable value instead.
  5786. */
  5787. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5788. reg_val &= 0xffffff00;
  5789. reg_val |= 0x00000030;
  5790. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5791. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5792. reg_val &= 0x00ffffff;
  5793. reg_val |= 0x8c000000;
  5794. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5795. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5796. reg_val &= 0xffffff00;
  5797. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5798. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5799. reg_val &= 0x00ffffff;
  5800. reg_val |= 0xb0000000;
  5801. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5802. }
  5803. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5804. struct intel_link_m_n *m_n)
  5805. {
  5806. struct drm_device *dev = crtc->base.dev;
  5807. struct drm_i915_private *dev_priv = to_i915(dev);
  5808. int pipe = crtc->pipe;
  5809. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5810. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5811. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5812. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5813. }
  5814. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5815. struct intel_link_m_n *m_n,
  5816. struct intel_link_m_n *m2_n2)
  5817. {
  5818. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5819. int pipe = crtc->pipe;
  5820. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5821. if (INTEL_GEN(dev_priv) >= 5) {
  5822. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5823. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5824. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5825. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5826. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5827. * for gen < 8) and if DRRS is supported (to make sure the
  5828. * registers are not unnecessarily accessed).
  5829. */
  5830. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5831. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5832. I915_WRITE(PIPE_DATA_M2(transcoder),
  5833. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5834. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5835. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5836. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5837. }
  5838. } else {
  5839. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5840. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5841. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5842. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5843. }
  5844. }
  5845. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5846. {
  5847. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5848. if (m_n == M1_N1) {
  5849. dp_m_n = &crtc->config->dp_m_n;
  5850. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5851. } else if (m_n == M2_N2) {
  5852. /*
  5853. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5854. * needs to be programmed into M1_N1.
  5855. */
  5856. dp_m_n = &crtc->config->dp_m2_n2;
  5857. } else {
  5858. DRM_ERROR("Unsupported divider value\n");
  5859. return;
  5860. }
  5861. if (crtc->config->has_pch_encoder)
  5862. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5863. else
  5864. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5865. }
  5866. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5867. struct intel_crtc_state *pipe_config)
  5868. {
  5869. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5870. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5871. if (crtc->pipe != PIPE_A)
  5872. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5873. /* DPLL not used with DSI, but still need the rest set up */
  5874. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5875. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5876. DPLL_EXT_BUFFER_ENABLE_VLV;
  5877. pipe_config->dpll_hw_state.dpll_md =
  5878. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5879. }
  5880. static void chv_compute_dpll(struct intel_crtc *crtc,
  5881. struct intel_crtc_state *pipe_config)
  5882. {
  5883. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5884. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5885. if (crtc->pipe != PIPE_A)
  5886. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5887. /* DPLL not used with DSI, but still need the rest set up */
  5888. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5889. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5890. pipe_config->dpll_hw_state.dpll_md =
  5891. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5892. }
  5893. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5894. const struct intel_crtc_state *pipe_config)
  5895. {
  5896. struct drm_device *dev = crtc->base.dev;
  5897. struct drm_i915_private *dev_priv = to_i915(dev);
  5898. enum pipe pipe = crtc->pipe;
  5899. u32 mdiv;
  5900. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5901. u32 coreclk, reg_val;
  5902. /* Enable Refclk */
  5903. I915_WRITE(DPLL(pipe),
  5904. pipe_config->dpll_hw_state.dpll &
  5905. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5906. /* No need to actually set up the DPLL with DSI */
  5907. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5908. return;
  5909. mutex_lock(&dev_priv->sb_lock);
  5910. bestn = pipe_config->dpll.n;
  5911. bestm1 = pipe_config->dpll.m1;
  5912. bestm2 = pipe_config->dpll.m2;
  5913. bestp1 = pipe_config->dpll.p1;
  5914. bestp2 = pipe_config->dpll.p2;
  5915. /* See eDP HDMI DPIO driver vbios notes doc */
  5916. /* PLL B needs special handling */
  5917. if (pipe == PIPE_B)
  5918. vlv_pllb_recal_opamp(dev_priv, pipe);
  5919. /* Set up Tx target for periodic Rcomp update */
  5920. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5921. /* Disable target IRef on PLL */
  5922. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5923. reg_val &= 0x00ffffff;
  5924. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5925. /* Disable fast lock */
  5926. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5927. /* Set idtafcrecal before PLL is enabled */
  5928. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5929. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5930. mdiv |= ((bestn << DPIO_N_SHIFT));
  5931. mdiv |= (1 << DPIO_K_SHIFT);
  5932. /*
  5933. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5934. * but we don't support that).
  5935. * Note: don't use the DAC post divider as it seems unstable.
  5936. */
  5937. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5938. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5939. mdiv |= DPIO_ENABLE_CALIBRATION;
  5940. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5941. /* Set HBR and RBR LPF coefficients */
  5942. if (pipe_config->port_clock == 162000 ||
  5943. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5944. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5945. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5946. 0x009f0003);
  5947. else
  5948. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5949. 0x00d0000f);
  5950. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5951. /* Use SSC source */
  5952. if (pipe == PIPE_A)
  5953. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5954. 0x0df40000);
  5955. else
  5956. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5957. 0x0df70000);
  5958. } else { /* HDMI or VGA */
  5959. /* Use bend source */
  5960. if (pipe == PIPE_A)
  5961. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5962. 0x0df70000);
  5963. else
  5964. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5965. 0x0df40000);
  5966. }
  5967. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5968. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5969. if (intel_crtc_has_dp_encoder(crtc->config))
  5970. coreclk |= 0x01000000;
  5971. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5972. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5973. mutex_unlock(&dev_priv->sb_lock);
  5974. }
  5975. static void chv_prepare_pll(struct intel_crtc *crtc,
  5976. const struct intel_crtc_state *pipe_config)
  5977. {
  5978. struct drm_device *dev = crtc->base.dev;
  5979. struct drm_i915_private *dev_priv = to_i915(dev);
  5980. enum pipe pipe = crtc->pipe;
  5981. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5982. u32 loopfilter, tribuf_calcntr;
  5983. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5984. u32 dpio_val;
  5985. int vco;
  5986. /* Enable Refclk and SSC */
  5987. I915_WRITE(DPLL(pipe),
  5988. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5989. /* No need to actually set up the DPLL with DSI */
  5990. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5991. return;
  5992. bestn = pipe_config->dpll.n;
  5993. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5994. bestm1 = pipe_config->dpll.m1;
  5995. bestm2 = pipe_config->dpll.m2 >> 22;
  5996. bestp1 = pipe_config->dpll.p1;
  5997. bestp2 = pipe_config->dpll.p2;
  5998. vco = pipe_config->dpll.vco;
  5999. dpio_val = 0;
  6000. loopfilter = 0;
  6001. mutex_lock(&dev_priv->sb_lock);
  6002. /* p1 and p2 divider */
  6003. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6004. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6005. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6006. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6007. 1 << DPIO_CHV_K_DIV_SHIFT);
  6008. /* Feedback post-divider - m2 */
  6009. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6010. /* Feedback refclk divider - n and m1 */
  6011. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6012. DPIO_CHV_M1_DIV_BY_2 |
  6013. 1 << DPIO_CHV_N_DIV_SHIFT);
  6014. /* M2 fraction division */
  6015. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6016. /* M2 fraction division enable */
  6017. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6018. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6019. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6020. if (bestm2_frac)
  6021. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6022. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6023. /* Program digital lock detect threshold */
  6024. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6025. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6026. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6027. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6028. if (!bestm2_frac)
  6029. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6030. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6031. /* Loop filter */
  6032. if (vco == 5400000) {
  6033. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6034. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6035. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6036. tribuf_calcntr = 0x9;
  6037. } else if (vco <= 6200000) {
  6038. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6039. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6040. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6041. tribuf_calcntr = 0x9;
  6042. } else if (vco <= 6480000) {
  6043. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6044. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6045. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6046. tribuf_calcntr = 0x8;
  6047. } else {
  6048. /* Not supported. Apply the same limits as in the max case */
  6049. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6050. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6051. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6052. tribuf_calcntr = 0;
  6053. }
  6054. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6055. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6056. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6057. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6058. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6059. /* AFC Recal */
  6060. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6061. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6062. DPIO_AFC_RECAL);
  6063. mutex_unlock(&dev_priv->sb_lock);
  6064. }
  6065. /**
  6066. * vlv_force_pll_on - forcibly enable just the PLL
  6067. * @dev_priv: i915 private structure
  6068. * @pipe: pipe PLL to enable
  6069. * @dpll: PLL configuration
  6070. *
  6071. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6072. * in cases where we need the PLL enabled even when @pipe is not going to
  6073. * be enabled.
  6074. */
  6075. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6076. const struct dpll *dpll)
  6077. {
  6078. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6079. struct intel_crtc_state *pipe_config;
  6080. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6081. if (!pipe_config)
  6082. return -ENOMEM;
  6083. pipe_config->base.crtc = &crtc->base;
  6084. pipe_config->pixel_multiplier = 1;
  6085. pipe_config->dpll = *dpll;
  6086. if (IS_CHERRYVIEW(dev_priv)) {
  6087. chv_compute_dpll(crtc, pipe_config);
  6088. chv_prepare_pll(crtc, pipe_config);
  6089. chv_enable_pll(crtc, pipe_config);
  6090. } else {
  6091. vlv_compute_dpll(crtc, pipe_config);
  6092. vlv_prepare_pll(crtc, pipe_config);
  6093. vlv_enable_pll(crtc, pipe_config);
  6094. }
  6095. kfree(pipe_config);
  6096. return 0;
  6097. }
  6098. /**
  6099. * vlv_force_pll_off - forcibly disable just the PLL
  6100. * @dev_priv: i915 private structure
  6101. * @pipe: pipe PLL to disable
  6102. *
  6103. * Disable the PLL for @pipe. To be used in cases where we need
  6104. * the PLL enabled even when @pipe is not going to be enabled.
  6105. */
  6106. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6107. {
  6108. if (IS_CHERRYVIEW(dev_priv))
  6109. chv_disable_pll(dev_priv, pipe);
  6110. else
  6111. vlv_disable_pll(dev_priv, pipe);
  6112. }
  6113. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6114. struct intel_crtc_state *crtc_state,
  6115. struct dpll *reduced_clock)
  6116. {
  6117. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6118. u32 dpll;
  6119. struct dpll *clock = &crtc_state->dpll;
  6120. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6121. dpll = DPLL_VGA_MODE_DIS;
  6122. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6123. dpll |= DPLLB_MODE_LVDS;
  6124. else
  6125. dpll |= DPLLB_MODE_DAC_SERIAL;
  6126. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6127. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6128. dpll |= (crtc_state->pixel_multiplier - 1)
  6129. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6130. }
  6131. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6132. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6133. dpll |= DPLL_SDVO_HIGH_SPEED;
  6134. if (intel_crtc_has_dp_encoder(crtc_state))
  6135. dpll |= DPLL_SDVO_HIGH_SPEED;
  6136. /* compute bitmask from p1 value */
  6137. if (IS_PINEVIEW(dev_priv))
  6138. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6139. else {
  6140. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6141. if (IS_G4X(dev_priv) && reduced_clock)
  6142. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6143. }
  6144. switch (clock->p2) {
  6145. case 5:
  6146. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6147. break;
  6148. case 7:
  6149. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6150. break;
  6151. case 10:
  6152. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6153. break;
  6154. case 14:
  6155. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6156. break;
  6157. }
  6158. if (INTEL_GEN(dev_priv) >= 4)
  6159. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6160. if (crtc_state->sdvo_tv_clock)
  6161. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6162. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6163. intel_panel_use_ssc(dev_priv))
  6164. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6165. else
  6166. dpll |= PLL_REF_INPUT_DREFCLK;
  6167. dpll |= DPLL_VCO_ENABLE;
  6168. crtc_state->dpll_hw_state.dpll = dpll;
  6169. if (INTEL_GEN(dev_priv) >= 4) {
  6170. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6171. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6172. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6173. }
  6174. }
  6175. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6176. struct intel_crtc_state *crtc_state,
  6177. struct dpll *reduced_clock)
  6178. {
  6179. struct drm_device *dev = crtc->base.dev;
  6180. struct drm_i915_private *dev_priv = to_i915(dev);
  6181. u32 dpll;
  6182. struct dpll *clock = &crtc_state->dpll;
  6183. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6184. dpll = DPLL_VGA_MODE_DIS;
  6185. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6186. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6187. } else {
  6188. if (clock->p1 == 2)
  6189. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6190. else
  6191. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6192. if (clock->p2 == 4)
  6193. dpll |= PLL_P2_DIVIDE_BY_4;
  6194. }
  6195. if (!IS_I830(dev_priv) &&
  6196. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6197. dpll |= DPLL_DVO_2X_MODE;
  6198. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6199. intel_panel_use_ssc(dev_priv))
  6200. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6201. else
  6202. dpll |= PLL_REF_INPUT_DREFCLK;
  6203. dpll |= DPLL_VCO_ENABLE;
  6204. crtc_state->dpll_hw_state.dpll = dpll;
  6205. }
  6206. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6207. {
  6208. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6209. enum pipe pipe = intel_crtc->pipe;
  6210. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6211. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6212. uint32_t crtc_vtotal, crtc_vblank_end;
  6213. int vsyncshift = 0;
  6214. /* We need to be careful not to changed the adjusted mode, for otherwise
  6215. * the hw state checker will get angry at the mismatch. */
  6216. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6217. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6218. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6219. /* the chip adds 2 halflines automatically */
  6220. crtc_vtotal -= 1;
  6221. crtc_vblank_end -= 1;
  6222. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6223. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6224. else
  6225. vsyncshift = adjusted_mode->crtc_hsync_start -
  6226. adjusted_mode->crtc_htotal / 2;
  6227. if (vsyncshift < 0)
  6228. vsyncshift += adjusted_mode->crtc_htotal;
  6229. }
  6230. if (INTEL_GEN(dev_priv) > 3)
  6231. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6232. I915_WRITE(HTOTAL(cpu_transcoder),
  6233. (adjusted_mode->crtc_hdisplay - 1) |
  6234. ((adjusted_mode->crtc_htotal - 1) << 16));
  6235. I915_WRITE(HBLANK(cpu_transcoder),
  6236. (adjusted_mode->crtc_hblank_start - 1) |
  6237. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6238. I915_WRITE(HSYNC(cpu_transcoder),
  6239. (adjusted_mode->crtc_hsync_start - 1) |
  6240. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6241. I915_WRITE(VTOTAL(cpu_transcoder),
  6242. (adjusted_mode->crtc_vdisplay - 1) |
  6243. ((crtc_vtotal - 1) << 16));
  6244. I915_WRITE(VBLANK(cpu_transcoder),
  6245. (adjusted_mode->crtc_vblank_start - 1) |
  6246. ((crtc_vblank_end - 1) << 16));
  6247. I915_WRITE(VSYNC(cpu_transcoder),
  6248. (adjusted_mode->crtc_vsync_start - 1) |
  6249. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6250. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6251. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6252. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6253. * bits. */
  6254. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6255. (pipe == PIPE_B || pipe == PIPE_C))
  6256. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6257. }
  6258. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6259. {
  6260. struct drm_device *dev = intel_crtc->base.dev;
  6261. struct drm_i915_private *dev_priv = to_i915(dev);
  6262. enum pipe pipe = intel_crtc->pipe;
  6263. /* pipesrc controls the size that is scaled from, which should
  6264. * always be the user's requested size.
  6265. */
  6266. I915_WRITE(PIPESRC(pipe),
  6267. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6268. (intel_crtc->config->pipe_src_h - 1));
  6269. }
  6270. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6271. struct intel_crtc_state *pipe_config)
  6272. {
  6273. struct drm_device *dev = crtc->base.dev;
  6274. struct drm_i915_private *dev_priv = to_i915(dev);
  6275. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6276. uint32_t tmp;
  6277. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6278. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6279. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6280. tmp = I915_READ(HBLANK(cpu_transcoder));
  6281. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6282. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6283. tmp = I915_READ(HSYNC(cpu_transcoder));
  6284. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6285. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6286. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6287. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6288. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6289. tmp = I915_READ(VBLANK(cpu_transcoder));
  6290. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6291. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6292. tmp = I915_READ(VSYNC(cpu_transcoder));
  6293. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6294. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6295. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6296. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6297. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6298. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6299. }
  6300. }
  6301. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6302. struct intel_crtc_state *pipe_config)
  6303. {
  6304. struct drm_device *dev = crtc->base.dev;
  6305. struct drm_i915_private *dev_priv = to_i915(dev);
  6306. u32 tmp;
  6307. tmp = I915_READ(PIPESRC(crtc->pipe));
  6308. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6309. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6310. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6311. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6312. }
  6313. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6314. struct intel_crtc_state *pipe_config)
  6315. {
  6316. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6317. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6318. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6319. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6320. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6321. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6322. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6323. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6324. mode->flags = pipe_config->base.adjusted_mode.flags;
  6325. mode->type = DRM_MODE_TYPE_DRIVER;
  6326. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6327. mode->hsync = drm_mode_hsync(mode);
  6328. mode->vrefresh = drm_mode_vrefresh(mode);
  6329. drm_mode_set_name(mode);
  6330. }
  6331. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6332. {
  6333. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6334. uint32_t pipeconf;
  6335. pipeconf = 0;
  6336. /* we keep both pipes enabled on 830 */
  6337. if (IS_I830(dev_priv))
  6338. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6339. if (intel_crtc->config->double_wide)
  6340. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6341. /* only g4x and later have fancy bpc/dither controls */
  6342. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6343. IS_CHERRYVIEW(dev_priv)) {
  6344. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6345. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6346. pipeconf |= PIPECONF_DITHER_EN |
  6347. PIPECONF_DITHER_TYPE_SP;
  6348. switch (intel_crtc->config->pipe_bpp) {
  6349. case 18:
  6350. pipeconf |= PIPECONF_6BPC;
  6351. break;
  6352. case 24:
  6353. pipeconf |= PIPECONF_8BPC;
  6354. break;
  6355. case 30:
  6356. pipeconf |= PIPECONF_10BPC;
  6357. break;
  6358. default:
  6359. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6360. BUG();
  6361. }
  6362. }
  6363. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6364. if (INTEL_GEN(dev_priv) < 4 ||
  6365. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6366. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6367. else
  6368. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6369. } else
  6370. pipeconf |= PIPECONF_PROGRESSIVE;
  6371. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6372. intel_crtc->config->limited_color_range)
  6373. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6374. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6375. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6376. }
  6377. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6378. struct intel_crtc_state *crtc_state)
  6379. {
  6380. struct drm_device *dev = crtc->base.dev;
  6381. struct drm_i915_private *dev_priv = to_i915(dev);
  6382. const struct intel_limit *limit;
  6383. int refclk = 48000;
  6384. memset(&crtc_state->dpll_hw_state, 0,
  6385. sizeof(crtc_state->dpll_hw_state));
  6386. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6387. if (intel_panel_use_ssc(dev_priv)) {
  6388. refclk = dev_priv->vbt.lvds_ssc_freq;
  6389. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6390. }
  6391. limit = &intel_limits_i8xx_lvds;
  6392. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6393. limit = &intel_limits_i8xx_dvo;
  6394. } else {
  6395. limit = &intel_limits_i8xx_dac;
  6396. }
  6397. if (!crtc_state->clock_set &&
  6398. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6399. refclk, NULL, &crtc_state->dpll)) {
  6400. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6401. return -EINVAL;
  6402. }
  6403. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6404. return 0;
  6405. }
  6406. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6407. struct intel_crtc_state *crtc_state)
  6408. {
  6409. struct drm_device *dev = crtc->base.dev;
  6410. struct drm_i915_private *dev_priv = to_i915(dev);
  6411. const struct intel_limit *limit;
  6412. int refclk = 96000;
  6413. memset(&crtc_state->dpll_hw_state, 0,
  6414. sizeof(crtc_state->dpll_hw_state));
  6415. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6416. if (intel_panel_use_ssc(dev_priv)) {
  6417. refclk = dev_priv->vbt.lvds_ssc_freq;
  6418. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6419. }
  6420. if (intel_is_dual_link_lvds(dev))
  6421. limit = &intel_limits_g4x_dual_channel_lvds;
  6422. else
  6423. limit = &intel_limits_g4x_single_channel_lvds;
  6424. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6425. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6426. limit = &intel_limits_g4x_hdmi;
  6427. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6428. limit = &intel_limits_g4x_sdvo;
  6429. } else {
  6430. /* The option is for other outputs */
  6431. limit = &intel_limits_i9xx_sdvo;
  6432. }
  6433. if (!crtc_state->clock_set &&
  6434. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6435. refclk, NULL, &crtc_state->dpll)) {
  6436. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6437. return -EINVAL;
  6438. }
  6439. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6440. return 0;
  6441. }
  6442. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6443. struct intel_crtc_state *crtc_state)
  6444. {
  6445. struct drm_device *dev = crtc->base.dev;
  6446. struct drm_i915_private *dev_priv = to_i915(dev);
  6447. const struct intel_limit *limit;
  6448. int refclk = 96000;
  6449. memset(&crtc_state->dpll_hw_state, 0,
  6450. sizeof(crtc_state->dpll_hw_state));
  6451. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6452. if (intel_panel_use_ssc(dev_priv)) {
  6453. refclk = dev_priv->vbt.lvds_ssc_freq;
  6454. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6455. }
  6456. limit = &intel_limits_pineview_lvds;
  6457. } else {
  6458. limit = &intel_limits_pineview_sdvo;
  6459. }
  6460. if (!crtc_state->clock_set &&
  6461. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6462. refclk, NULL, &crtc_state->dpll)) {
  6463. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6464. return -EINVAL;
  6465. }
  6466. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6467. return 0;
  6468. }
  6469. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6470. struct intel_crtc_state *crtc_state)
  6471. {
  6472. struct drm_device *dev = crtc->base.dev;
  6473. struct drm_i915_private *dev_priv = to_i915(dev);
  6474. const struct intel_limit *limit;
  6475. int refclk = 96000;
  6476. memset(&crtc_state->dpll_hw_state, 0,
  6477. sizeof(crtc_state->dpll_hw_state));
  6478. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6479. if (intel_panel_use_ssc(dev_priv)) {
  6480. refclk = dev_priv->vbt.lvds_ssc_freq;
  6481. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6482. }
  6483. limit = &intel_limits_i9xx_lvds;
  6484. } else {
  6485. limit = &intel_limits_i9xx_sdvo;
  6486. }
  6487. if (!crtc_state->clock_set &&
  6488. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6489. refclk, NULL, &crtc_state->dpll)) {
  6490. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6491. return -EINVAL;
  6492. }
  6493. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6494. return 0;
  6495. }
  6496. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6497. struct intel_crtc_state *crtc_state)
  6498. {
  6499. int refclk = 100000;
  6500. const struct intel_limit *limit = &intel_limits_chv;
  6501. memset(&crtc_state->dpll_hw_state, 0,
  6502. sizeof(crtc_state->dpll_hw_state));
  6503. if (!crtc_state->clock_set &&
  6504. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6505. refclk, NULL, &crtc_state->dpll)) {
  6506. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6507. return -EINVAL;
  6508. }
  6509. chv_compute_dpll(crtc, crtc_state);
  6510. return 0;
  6511. }
  6512. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6513. struct intel_crtc_state *crtc_state)
  6514. {
  6515. int refclk = 100000;
  6516. const struct intel_limit *limit = &intel_limits_vlv;
  6517. memset(&crtc_state->dpll_hw_state, 0,
  6518. sizeof(crtc_state->dpll_hw_state));
  6519. if (!crtc_state->clock_set &&
  6520. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6521. refclk, NULL, &crtc_state->dpll)) {
  6522. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6523. return -EINVAL;
  6524. }
  6525. vlv_compute_dpll(crtc, crtc_state);
  6526. return 0;
  6527. }
  6528. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6529. struct intel_crtc_state *pipe_config)
  6530. {
  6531. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6532. uint32_t tmp;
  6533. if (INTEL_GEN(dev_priv) <= 3 &&
  6534. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6535. return;
  6536. tmp = I915_READ(PFIT_CONTROL);
  6537. if (!(tmp & PFIT_ENABLE))
  6538. return;
  6539. /* Check whether the pfit is attached to our pipe. */
  6540. if (INTEL_GEN(dev_priv) < 4) {
  6541. if (crtc->pipe != PIPE_B)
  6542. return;
  6543. } else {
  6544. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6545. return;
  6546. }
  6547. pipe_config->gmch_pfit.control = tmp;
  6548. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6549. }
  6550. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6551. struct intel_crtc_state *pipe_config)
  6552. {
  6553. struct drm_device *dev = crtc->base.dev;
  6554. struct drm_i915_private *dev_priv = to_i915(dev);
  6555. int pipe = pipe_config->cpu_transcoder;
  6556. struct dpll clock;
  6557. u32 mdiv;
  6558. int refclk = 100000;
  6559. /* In case of DSI, DPLL will not be used */
  6560. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6561. return;
  6562. mutex_lock(&dev_priv->sb_lock);
  6563. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6564. mutex_unlock(&dev_priv->sb_lock);
  6565. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6566. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6567. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6568. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6569. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6570. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6571. }
  6572. static void
  6573. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6574. struct intel_initial_plane_config *plane_config)
  6575. {
  6576. struct drm_device *dev = crtc->base.dev;
  6577. struct drm_i915_private *dev_priv = to_i915(dev);
  6578. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6579. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6580. enum pipe pipe;
  6581. u32 val, base, offset;
  6582. int fourcc, pixel_format;
  6583. unsigned int aligned_height;
  6584. struct drm_framebuffer *fb;
  6585. struct intel_framebuffer *intel_fb;
  6586. if (!plane->get_hw_state(plane, &pipe))
  6587. return;
  6588. WARN_ON(pipe != crtc->pipe);
  6589. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6590. if (!intel_fb) {
  6591. DRM_DEBUG_KMS("failed to alloc fb\n");
  6592. return;
  6593. }
  6594. fb = &intel_fb->base;
  6595. fb->dev = dev;
  6596. val = I915_READ(DSPCNTR(i9xx_plane));
  6597. if (INTEL_GEN(dev_priv) >= 4) {
  6598. if (val & DISPPLANE_TILED) {
  6599. plane_config->tiling = I915_TILING_X;
  6600. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6601. }
  6602. if (val & DISPPLANE_ROTATE_180)
  6603. plane_config->rotation = DRM_MODE_ROTATE_180;
  6604. }
  6605. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
  6606. val & DISPPLANE_MIRROR)
  6607. plane_config->rotation |= DRM_MODE_REFLECT_X;
  6608. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6609. fourcc = i9xx_format_to_fourcc(pixel_format);
  6610. fb->format = drm_format_info(fourcc);
  6611. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6612. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6613. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6614. } else if (INTEL_GEN(dev_priv) >= 4) {
  6615. if (plane_config->tiling)
  6616. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6617. else
  6618. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6619. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6620. } else {
  6621. base = I915_READ(DSPADDR(i9xx_plane));
  6622. }
  6623. plane_config->base = base;
  6624. val = I915_READ(PIPESRC(pipe));
  6625. fb->width = ((val >> 16) & 0xfff) + 1;
  6626. fb->height = ((val >> 0) & 0xfff) + 1;
  6627. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6628. fb->pitches[0] = val & 0xffffffc0;
  6629. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6630. plane_config->size = fb->pitches[0] * aligned_height;
  6631. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6632. crtc->base.name, plane->base.name, fb->width, fb->height,
  6633. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6634. plane_config->size);
  6635. plane_config->fb = intel_fb;
  6636. }
  6637. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6638. struct intel_crtc_state *pipe_config)
  6639. {
  6640. struct drm_device *dev = crtc->base.dev;
  6641. struct drm_i915_private *dev_priv = to_i915(dev);
  6642. int pipe = pipe_config->cpu_transcoder;
  6643. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6644. struct dpll clock;
  6645. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6646. int refclk = 100000;
  6647. /* In case of DSI, DPLL will not be used */
  6648. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6649. return;
  6650. mutex_lock(&dev_priv->sb_lock);
  6651. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6652. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6653. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6654. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6655. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6656. mutex_unlock(&dev_priv->sb_lock);
  6657. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6658. clock.m2 = (pll_dw0 & 0xff) << 22;
  6659. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6660. clock.m2 |= pll_dw2 & 0x3fffff;
  6661. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6662. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6663. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6664. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6665. }
  6666. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6667. struct intel_crtc_state *pipe_config)
  6668. {
  6669. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6670. enum intel_display_power_domain power_domain;
  6671. uint32_t tmp;
  6672. bool ret;
  6673. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6674. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6675. return false;
  6676. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6677. pipe_config->shared_dpll = NULL;
  6678. ret = false;
  6679. tmp = I915_READ(PIPECONF(crtc->pipe));
  6680. if (!(tmp & PIPECONF_ENABLE))
  6681. goto out;
  6682. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6683. IS_CHERRYVIEW(dev_priv)) {
  6684. switch (tmp & PIPECONF_BPC_MASK) {
  6685. case PIPECONF_6BPC:
  6686. pipe_config->pipe_bpp = 18;
  6687. break;
  6688. case PIPECONF_8BPC:
  6689. pipe_config->pipe_bpp = 24;
  6690. break;
  6691. case PIPECONF_10BPC:
  6692. pipe_config->pipe_bpp = 30;
  6693. break;
  6694. default:
  6695. break;
  6696. }
  6697. }
  6698. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6699. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6700. pipe_config->limited_color_range = true;
  6701. if (INTEL_GEN(dev_priv) < 4)
  6702. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6703. intel_get_pipe_timings(crtc, pipe_config);
  6704. intel_get_pipe_src_size(crtc, pipe_config);
  6705. i9xx_get_pfit_config(crtc, pipe_config);
  6706. if (INTEL_GEN(dev_priv) >= 4) {
  6707. /* No way to read it out on pipes B and C */
  6708. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6709. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6710. else
  6711. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6712. pipe_config->pixel_multiplier =
  6713. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6714. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6715. pipe_config->dpll_hw_state.dpll_md = tmp;
  6716. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6717. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6718. tmp = I915_READ(DPLL(crtc->pipe));
  6719. pipe_config->pixel_multiplier =
  6720. ((tmp & SDVO_MULTIPLIER_MASK)
  6721. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6722. } else {
  6723. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6724. * port and will be fixed up in the encoder->get_config
  6725. * function. */
  6726. pipe_config->pixel_multiplier = 1;
  6727. }
  6728. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6729. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6730. /*
  6731. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6732. * on 830. Filter it out here so that we don't
  6733. * report errors due to that.
  6734. */
  6735. if (IS_I830(dev_priv))
  6736. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6737. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6738. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6739. } else {
  6740. /* Mask out read-only status bits. */
  6741. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6742. DPLL_PORTC_READY_MASK |
  6743. DPLL_PORTB_READY_MASK);
  6744. }
  6745. if (IS_CHERRYVIEW(dev_priv))
  6746. chv_crtc_clock_get(crtc, pipe_config);
  6747. else if (IS_VALLEYVIEW(dev_priv))
  6748. vlv_crtc_clock_get(crtc, pipe_config);
  6749. else
  6750. i9xx_crtc_clock_get(crtc, pipe_config);
  6751. /*
  6752. * Normally the dotclock is filled in by the encoder .get_config()
  6753. * but in case the pipe is enabled w/o any ports we need a sane
  6754. * default.
  6755. */
  6756. pipe_config->base.adjusted_mode.crtc_clock =
  6757. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6758. ret = true;
  6759. out:
  6760. intel_display_power_put(dev_priv, power_domain);
  6761. return ret;
  6762. }
  6763. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6764. {
  6765. struct intel_encoder *encoder;
  6766. int i;
  6767. u32 val, final;
  6768. bool has_lvds = false;
  6769. bool has_cpu_edp = false;
  6770. bool has_panel = false;
  6771. bool has_ck505 = false;
  6772. bool can_ssc = false;
  6773. bool using_ssc_source = false;
  6774. /* We need to take the global config into account */
  6775. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6776. switch (encoder->type) {
  6777. case INTEL_OUTPUT_LVDS:
  6778. has_panel = true;
  6779. has_lvds = true;
  6780. break;
  6781. case INTEL_OUTPUT_EDP:
  6782. has_panel = true;
  6783. if (encoder->port == PORT_A)
  6784. has_cpu_edp = true;
  6785. break;
  6786. default:
  6787. break;
  6788. }
  6789. }
  6790. if (HAS_PCH_IBX(dev_priv)) {
  6791. has_ck505 = dev_priv->vbt.display_clock_mode;
  6792. can_ssc = has_ck505;
  6793. } else {
  6794. has_ck505 = false;
  6795. can_ssc = true;
  6796. }
  6797. /* Check if any DPLLs are using the SSC source */
  6798. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6799. u32 temp = I915_READ(PCH_DPLL(i));
  6800. if (!(temp & DPLL_VCO_ENABLE))
  6801. continue;
  6802. if ((temp & PLL_REF_INPUT_MASK) ==
  6803. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6804. using_ssc_source = true;
  6805. break;
  6806. }
  6807. }
  6808. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6809. has_panel, has_lvds, has_ck505, using_ssc_source);
  6810. /* Ironlake: try to setup display ref clock before DPLL
  6811. * enabling. This is only under driver's control after
  6812. * PCH B stepping, previous chipset stepping should be
  6813. * ignoring this setting.
  6814. */
  6815. val = I915_READ(PCH_DREF_CONTROL);
  6816. /* As we must carefully and slowly disable/enable each source in turn,
  6817. * compute the final state we want first and check if we need to
  6818. * make any changes at all.
  6819. */
  6820. final = val;
  6821. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6822. if (has_ck505)
  6823. final |= DREF_NONSPREAD_CK505_ENABLE;
  6824. else
  6825. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6826. final &= ~DREF_SSC_SOURCE_MASK;
  6827. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6828. final &= ~DREF_SSC1_ENABLE;
  6829. if (has_panel) {
  6830. final |= DREF_SSC_SOURCE_ENABLE;
  6831. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6832. final |= DREF_SSC1_ENABLE;
  6833. if (has_cpu_edp) {
  6834. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6835. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6836. else
  6837. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6838. } else
  6839. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6840. } else if (using_ssc_source) {
  6841. final |= DREF_SSC_SOURCE_ENABLE;
  6842. final |= DREF_SSC1_ENABLE;
  6843. }
  6844. if (final == val)
  6845. return;
  6846. /* Always enable nonspread source */
  6847. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6848. if (has_ck505)
  6849. val |= DREF_NONSPREAD_CK505_ENABLE;
  6850. else
  6851. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6852. if (has_panel) {
  6853. val &= ~DREF_SSC_SOURCE_MASK;
  6854. val |= DREF_SSC_SOURCE_ENABLE;
  6855. /* SSC must be turned on before enabling the CPU output */
  6856. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6857. DRM_DEBUG_KMS("Using SSC on panel\n");
  6858. val |= DREF_SSC1_ENABLE;
  6859. } else
  6860. val &= ~DREF_SSC1_ENABLE;
  6861. /* Get SSC going before enabling the outputs */
  6862. I915_WRITE(PCH_DREF_CONTROL, val);
  6863. POSTING_READ(PCH_DREF_CONTROL);
  6864. udelay(200);
  6865. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6866. /* Enable CPU source on CPU attached eDP */
  6867. if (has_cpu_edp) {
  6868. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6869. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6870. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6871. } else
  6872. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6873. } else
  6874. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6875. I915_WRITE(PCH_DREF_CONTROL, val);
  6876. POSTING_READ(PCH_DREF_CONTROL);
  6877. udelay(200);
  6878. } else {
  6879. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6880. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6881. /* Turn off CPU output */
  6882. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6883. I915_WRITE(PCH_DREF_CONTROL, val);
  6884. POSTING_READ(PCH_DREF_CONTROL);
  6885. udelay(200);
  6886. if (!using_ssc_source) {
  6887. DRM_DEBUG_KMS("Disabling SSC source\n");
  6888. /* Turn off the SSC source */
  6889. val &= ~DREF_SSC_SOURCE_MASK;
  6890. val |= DREF_SSC_SOURCE_DISABLE;
  6891. /* Turn off SSC1 */
  6892. val &= ~DREF_SSC1_ENABLE;
  6893. I915_WRITE(PCH_DREF_CONTROL, val);
  6894. POSTING_READ(PCH_DREF_CONTROL);
  6895. udelay(200);
  6896. }
  6897. }
  6898. BUG_ON(val != final);
  6899. }
  6900. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6901. {
  6902. uint32_t tmp;
  6903. tmp = I915_READ(SOUTH_CHICKEN2);
  6904. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6905. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6906. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6907. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6908. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6909. tmp = I915_READ(SOUTH_CHICKEN2);
  6910. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6911. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6912. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6913. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6914. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6915. }
  6916. /* WaMPhyProgramming:hsw */
  6917. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6918. {
  6919. uint32_t tmp;
  6920. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6921. tmp &= ~(0xFF << 24);
  6922. tmp |= (0x12 << 24);
  6923. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6924. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6925. tmp |= (1 << 11);
  6926. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6927. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6928. tmp |= (1 << 11);
  6929. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6930. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6931. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6932. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6933. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6934. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6935. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6936. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6937. tmp &= ~(7 << 13);
  6938. tmp |= (5 << 13);
  6939. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6940. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6941. tmp &= ~(7 << 13);
  6942. tmp |= (5 << 13);
  6943. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6944. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6945. tmp &= ~0xFF;
  6946. tmp |= 0x1C;
  6947. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6948. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6949. tmp &= ~0xFF;
  6950. tmp |= 0x1C;
  6951. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6952. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6953. tmp &= ~(0xFF << 16);
  6954. tmp |= (0x1C << 16);
  6955. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6956. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6957. tmp &= ~(0xFF << 16);
  6958. tmp |= (0x1C << 16);
  6959. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6960. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6961. tmp |= (1 << 27);
  6962. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6963. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6964. tmp |= (1 << 27);
  6965. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6966. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6967. tmp &= ~(0xF << 28);
  6968. tmp |= (4 << 28);
  6969. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6970. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6971. tmp &= ~(0xF << 28);
  6972. tmp |= (4 << 28);
  6973. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6974. }
  6975. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6976. * Programming" based on the parameters passed:
  6977. * - Sequence to enable CLKOUT_DP
  6978. * - Sequence to enable CLKOUT_DP without spread
  6979. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6980. */
  6981. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6982. bool with_spread, bool with_fdi)
  6983. {
  6984. uint32_t reg, tmp;
  6985. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6986. with_spread = true;
  6987. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6988. with_fdi, "LP PCH doesn't have FDI\n"))
  6989. with_fdi = false;
  6990. mutex_lock(&dev_priv->sb_lock);
  6991. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6992. tmp &= ~SBI_SSCCTL_DISABLE;
  6993. tmp |= SBI_SSCCTL_PATHALT;
  6994. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6995. udelay(24);
  6996. if (with_spread) {
  6997. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6998. tmp &= ~SBI_SSCCTL_PATHALT;
  6999. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7000. if (with_fdi) {
  7001. lpt_reset_fdi_mphy(dev_priv);
  7002. lpt_program_fdi_mphy(dev_priv);
  7003. }
  7004. }
  7005. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7006. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7007. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7008. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7009. mutex_unlock(&dev_priv->sb_lock);
  7010. }
  7011. /* Sequence to disable CLKOUT_DP */
  7012. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  7013. {
  7014. uint32_t reg, tmp;
  7015. mutex_lock(&dev_priv->sb_lock);
  7016. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7017. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7018. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7019. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7020. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7021. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7022. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7023. tmp |= SBI_SSCCTL_PATHALT;
  7024. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7025. udelay(32);
  7026. }
  7027. tmp |= SBI_SSCCTL_DISABLE;
  7028. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7029. }
  7030. mutex_unlock(&dev_priv->sb_lock);
  7031. }
  7032. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7033. static const uint16_t sscdivintphase[] = {
  7034. [BEND_IDX( 50)] = 0x3B23,
  7035. [BEND_IDX( 45)] = 0x3B23,
  7036. [BEND_IDX( 40)] = 0x3C23,
  7037. [BEND_IDX( 35)] = 0x3C23,
  7038. [BEND_IDX( 30)] = 0x3D23,
  7039. [BEND_IDX( 25)] = 0x3D23,
  7040. [BEND_IDX( 20)] = 0x3E23,
  7041. [BEND_IDX( 15)] = 0x3E23,
  7042. [BEND_IDX( 10)] = 0x3F23,
  7043. [BEND_IDX( 5)] = 0x3F23,
  7044. [BEND_IDX( 0)] = 0x0025,
  7045. [BEND_IDX( -5)] = 0x0025,
  7046. [BEND_IDX(-10)] = 0x0125,
  7047. [BEND_IDX(-15)] = 0x0125,
  7048. [BEND_IDX(-20)] = 0x0225,
  7049. [BEND_IDX(-25)] = 0x0225,
  7050. [BEND_IDX(-30)] = 0x0325,
  7051. [BEND_IDX(-35)] = 0x0325,
  7052. [BEND_IDX(-40)] = 0x0425,
  7053. [BEND_IDX(-45)] = 0x0425,
  7054. [BEND_IDX(-50)] = 0x0525,
  7055. };
  7056. /*
  7057. * Bend CLKOUT_DP
  7058. * steps -50 to 50 inclusive, in steps of 5
  7059. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7060. * change in clock period = -(steps / 10) * 5.787 ps
  7061. */
  7062. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7063. {
  7064. uint32_t tmp;
  7065. int idx = BEND_IDX(steps);
  7066. if (WARN_ON(steps % 5 != 0))
  7067. return;
  7068. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7069. return;
  7070. mutex_lock(&dev_priv->sb_lock);
  7071. if (steps % 10 != 0)
  7072. tmp = 0xAAAAAAAB;
  7073. else
  7074. tmp = 0x00000000;
  7075. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7076. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7077. tmp &= 0xffff0000;
  7078. tmp |= sscdivintphase[idx];
  7079. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7080. mutex_unlock(&dev_priv->sb_lock);
  7081. }
  7082. #undef BEND_IDX
  7083. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  7084. {
  7085. struct intel_encoder *encoder;
  7086. bool has_vga = false;
  7087. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7088. switch (encoder->type) {
  7089. case INTEL_OUTPUT_ANALOG:
  7090. has_vga = true;
  7091. break;
  7092. default:
  7093. break;
  7094. }
  7095. }
  7096. if (has_vga) {
  7097. lpt_bend_clkout_dp(dev_priv, 0);
  7098. lpt_enable_clkout_dp(dev_priv, true, true);
  7099. } else {
  7100. lpt_disable_clkout_dp(dev_priv);
  7101. }
  7102. }
  7103. /*
  7104. * Initialize reference clocks when the driver loads
  7105. */
  7106. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  7107. {
  7108. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7109. ironlake_init_pch_refclk(dev_priv);
  7110. else if (HAS_PCH_LPT(dev_priv))
  7111. lpt_init_pch_refclk(dev_priv);
  7112. }
  7113. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7114. {
  7115. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7117. int pipe = intel_crtc->pipe;
  7118. uint32_t val;
  7119. val = 0;
  7120. switch (intel_crtc->config->pipe_bpp) {
  7121. case 18:
  7122. val |= PIPECONF_6BPC;
  7123. break;
  7124. case 24:
  7125. val |= PIPECONF_8BPC;
  7126. break;
  7127. case 30:
  7128. val |= PIPECONF_10BPC;
  7129. break;
  7130. case 36:
  7131. val |= PIPECONF_12BPC;
  7132. break;
  7133. default:
  7134. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7135. BUG();
  7136. }
  7137. if (intel_crtc->config->dither)
  7138. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7139. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7140. val |= PIPECONF_INTERLACED_ILK;
  7141. else
  7142. val |= PIPECONF_PROGRESSIVE;
  7143. if (intel_crtc->config->limited_color_range)
  7144. val |= PIPECONF_COLOR_RANGE_SELECT;
  7145. I915_WRITE(PIPECONF(pipe), val);
  7146. POSTING_READ(PIPECONF(pipe));
  7147. }
  7148. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7149. {
  7150. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7152. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7153. u32 val = 0;
  7154. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7155. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7156. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7157. val |= PIPECONF_INTERLACED_ILK;
  7158. else
  7159. val |= PIPECONF_PROGRESSIVE;
  7160. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7161. POSTING_READ(PIPECONF(cpu_transcoder));
  7162. }
  7163. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7164. {
  7165. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7166. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7167. struct intel_crtc_state *config = intel_crtc->config;
  7168. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7169. u32 val = 0;
  7170. switch (intel_crtc->config->pipe_bpp) {
  7171. case 18:
  7172. val |= PIPEMISC_DITHER_6_BPC;
  7173. break;
  7174. case 24:
  7175. val |= PIPEMISC_DITHER_8_BPC;
  7176. break;
  7177. case 30:
  7178. val |= PIPEMISC_DITHER_10_BPC;
  7179. break;
  7180. case 36:
  7181. val |= PIPEMISC_DITHER_12_BPC;
  7182. break;
  7183. default:
  7184. /* Case prevented by pipe_config_set_bpp. */
  7185. BUG();
  7186. }
  7187. if (intel_crtc->config->dither)
  7188. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7189. if (config->ycbcr420) {
  7190. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  7191. PIPEMISC_YUV420_ENABLE |
  7192. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7193. }
  7194. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7195. }
  7196. }
  7197. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7198. {
  7199. /*
  7200. * Account for spread spectrum to avoid
  7201. * oversubscribing the link. Max center spread
  7202. * is 2.5%; use 5% for safety's sake.
  7203. */
  7204. u32 bps = target_clock * bpp * 21 / 20;
  7205. return DIV_ROUND_UP(bps, link_bw * 8);
  7206. }
  7207. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7208. {
  7209. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7210. }
  7211. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7212. struct intel_crtc_state *crtc_state,
  7213. struct dpll *reduced_clock)
  7214. {
  7215. struct drm_crtc *crtc = &intel_crtc->base;
  7216. struct drm_device *dev = crtc->dev;
  7217. struct drm_i915_private *dev_priv = to_i915(dev);
  7218. u32 dpll, fp, fp2;
  7219. int factor;
  7220. /* Enable autotuning of the PLL clock (if permissible) */
  7221. factor = 21;
  7222. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7223. if ((intel_panel_use_ssc(dev_priv) &&
  7224. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7225. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7226. factor = 25;
  7227. } else if (crtc_state->sdvo_tv_clock)
  7228. factor = 20;
  7229. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7230. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7231. fp |= FP_CB_TUNE;
  7232. if (reduced_clock) {
  7233. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7234. if (reduced_clock->m < factor * reduced_clock->n)
  7235. fp2 |= FP_CB_TUNE;
  7236. } else {
  7237. fp2 = fp;
  7238. }
  7239. dpll = 0;
  7240. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7241. dpll |= DPLLB_MODE_LVDS;
  7242. else
  7243. dpll |= DPLLB_MODE_DAC_SERIAL;
  7244. dpll |= (crtc_state->pixel_multiplier - 1)
  7245. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7246. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7247. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7248. dpll |= DPLL_SDVO_HIGH_SPEED;
  7249. if (intel_crtc_has_dp_encoder(crtc_state))
  7250. dpll |= DPLL_SDVO_HIGH_SPEED;
  7251. /*
  7252. * The high speed IO clock is only really required for
  7253. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7254. * possible to share the DPLL between CRT and HDMI. Enabling
  7255. * the clock needlessly does no real harm, except use up a
  7256. * bit of power potentially.
  7257. *
  7258. * We'll limit this to IVB with 3 pipes, since it has only two
  7259. * DPLLs and so DPLL sharing is the only way to get three pipes
  7260. * driving PCH ports at the same time. On SNB we could do this,
  7261. * and potentially avoid enabling the second DPLL, but it's not
  7262. * clear if it''s a win or loss power wise. No point in doing
  7263. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7264. */
  7265. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7266. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7267. dpll |= DPLL_SDVO_HIGH_SPEED;
  7268. /* compute bitmask from p1 value */
  7269. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7270. /* also FPA1 */
  7271. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7272. switch (crtc_state->dpll.p2) {
  7273. case 5:
  7274. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7275. break;
  7276. case 7:
  7277. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7278. break;
  7279. case 10:
  7280. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7281. break;
  7282. case 14:
  7283. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7284. break;
  7285. }
  7286. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7287. intel_panel_use_ssc(dev_priv))
  7288. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7289. else
  7290. dpll |= PLL_REF_INPUT_DREFCLK;
  7291. dpll |= DPLL_VCO_ENABLE;
  7292. crtc_state->dpll_hw_state.dpll = dpll;
  7293. crtc_state->dpll_hw_state.fp0 = fp;
  7294. crtc_state->dpll_hw_state.fp1 = fp2;
  7295. }
  7296. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7297. struct intel_crtc_state *crtc_state)
  7298. {
  7299. struct drm_device *dev = crtc->base.dev;
  7300. struct drm_i915_private *dev_priv = to_i915(dev);
  7301. const struct intel_limit *limit;
  7302. int refclk = 120000;
  7303. memset(&crtc_state->dpll_hw_state, 0,
  7304. sizeof(crtc_state->dpll_hw_state));
  7305. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7306. if (!crtc_state->has_pch_encoder)
  7307. return 0;
  7308. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7309. if (intel_panel_use_ssc(dev_priv)) {
  7310. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7311. dev_priv->vbt.lvds_ssc_freq);
  7312. refclk = dev_priv->vbt.lvds_ssc_freq;
  7313. }
  7314. if (intel_is_dual_link_lvds(dev)) {
  7315. if (refclk == 100000)
  7316. limit = &intel_limits_ironlake_dual_lvds_100m;
  7317. else
  7318. limit = &intel_limits_ironlake_dual_lvds;
  7319. } else {
  7320. if (refclk == 100000)
  7321. limit = &intel_limits_ironlake_single_lvds_100m;
  7322. else
  7323. limit = &intel_limits_ironlake_single_lvds;
  7324. }
  7325. } else {
  7326. limit = &intel_limits_ironlake_dac;
  7327. }
  7328. if (!crtc_state->clock_set &&
  7329. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7330. refclk, NULL, &crtc_state->dpll)) {
  7331. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7332. return -EINVAL;
  7333. }
  7334. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7335. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7336. DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
  7337. pipe_name(crtc->pipe));
  7338. return -EINVAL;
  7339. }
  7340. return 0;
  7341. }
  7342. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7343. struct intel_link_m_n *m_n)
  7344. {
  7345. struct drm_device *dev = crtc->base.dev;
  7346. struct drm_i915_private *dev_priv = to_i915(dev);
  7347. enum pipe pipe = crtc->pipe;
  7348. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7349. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7350. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7351. & ~TU_SIZE_MASK;
  7352. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7353. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7354. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7355. }
  7356. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7357. enum transcoder transcoder,
  7358. struct intel_link_m_n *m_n,
  7359. struct intel_link_m_n *m2_n2)
  7360. {
  7361. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7362. enum pipe pipe = crtc->pipe;
  7363. if (INTEL_GEN(dev_priv) >= 5) {
  7364. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7365. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7366. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7367. & ~TU_SIZE_MASK;
  7368. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7369. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7370. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7371. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7372. * gen < 8) and if DRRS is supported (to make sure the
  7373. * registers are not unnecessarily read).
  7374. */
  7375. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7376. crtc->config->has_drrs) {
  7377. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7378. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7379. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7380. & ~TU_SIZE_MASK;
  7381. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7382. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7383. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7384. }
  7385. } else {
  7386. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7387. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7388. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7389. & ~TU_SIZE_MASK;
  7390. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7391. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7392. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7393. }
  7394. }
  7395. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7396. struct intel_crtc_state *pipe_config)
  7397. {
  7398. if (pipe_config->has_pch_encoder)
  7399. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7400. else
  7401. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7402. &pipe_config->dp_m_n,
  7403. &pipe_config->dp_m2_n2);
  7404. }
  7405. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7406. struct intel_crtc_state *pipe_config)
  7407. {
  7408. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7409. &pipe_config->fdi_m_n, NULL);
  7410. }
  7411. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7412. struct intel_crtc_state *pipe_config)
  7413. {
  7414. struct drm_device *dev = crtc->base.dev;
  7415. struct drm_i915_private *dev_priv = to_i915(dev);
  7416. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7417. uint32_t ps_ctrl = 0;
  7418. int id = -1;
  7419. int i;
  7420. /* find scaler attached to this pipe */
  7421. for (i = 0; i < crtc->num_scalers; i++) {
  7422. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7423. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7424. id = i;
  7425. pipe_config->pch_pfit.enabled = true;
  7426. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7427. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7428. break;
  7429. }
  7430. }
  7431. scaler_state->scaler_id = id;
  7432. if (id >= 0) {
  7433. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7434. } else {
  7435. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7436. }
  7437. }
  7438. static void
  7439. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7440. struct intel_initial_plane_config *plane_config)
  7441. {
  7442. struct drm_device *dev = crtc->base.dev;
  7443. struct drm_i915_private *dev_priv = to_i915(dev);
  7444. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7445. enum plane_id plane_id = plane->id;
  7446. enum pipe pipe;
  7447. u32 val, base, offset, stride_mult, tiling, alpha;
  7448. int fourcc, pixel_format;
  7449. unsigned int aligned_height;
  7450. struct drm_framebuffer *fb;
  7451. struct intel_framebuffer *intel_fb;
  7452. if (!plane->get_hw_state(plane, &pipe))
  7453. return;
  7454. WARN_ON(pipe != crtc->pipe);
  7455. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7456. if (!intel_fb) {
  7457. DRM_DEBUG_KMS("failed to alloc fb\n");
  7458. return;
  7459. }
  7460. fb = &intel_fb->base;
  7461. fb->dev = dev;
  7462. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7463. if (INTEL_GEN(dev_priv) >= 11)
  7464. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7465. else
  7466. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7467. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7468. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7469. alpha &= PLANE_COLOR_ALPHA_MASK;
  7470. } else {
  7471. alpha = val & PLANE_CTL_ALPHA_MASK;
  7472. }
  7473. fourcc = skl_format_to_fourcc(pixel_format,
  7474. val & PLANE_CTL_ORDER_RGBX, alpha);
  7475. fb->format = drm_format_info(fourcc);
  7476. tiling = val & PLANE_CTL_TILED_MASK;
  7477. switch (tiling) {
  7478. case PLANE_CTL_TILED_LINEAR:
  7479. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7480. break;
  7481. case PLANE_CTL_TILED_X:
  7482. plane_config->tiling = I915_TILING_X;
  7483. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7484. break;
  7485. case PLANE_CTL_TILED_Y:
  7486. plane_config->tiling = I915_TILING_Y;
  7487. if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
  7488. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7489. else
  7490. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7491. break;
  7492. case PLANE_CTL_TILED_YF:
  7493. if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
  7494. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7495. else
  7496. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7497. break;
  7498. default:
  7499. MISSING_CASE(tiling);
  7500. goto error;
  7501. }
  7502. /*
  7503. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  7504. * while i915 HW rotation is clockwise, thats why this swapping.
  7505. */
  7506. switch (val & PLANE_CTL_ROTATE_MASK) {
  7507. case PLANE_CTL_ROTATE_0:
  7508. plane_config->rotation = DRM_MODE_ROTATE_0;
  7509. break;
  7510. case PLANE_CTL_ROTATE_90:
  7511. plane_config->rotation = DRM_MODE_ROTATE_270;
  7512. break;
  7513. case PLANE_CTL_ROTATE_180:
  7514. plane_config->rotation = DRM_MODE_ROTATE_180;
  7515. break;
  7516. case PLANE_CTL_ROTATE_270:
  7517. plane_config->rotation = DRM_MODE_ROTATE_90;
  7518. break;
  7519. }
  7520. if (INTEL_GEN(dev_priv) >= 10 &&
  7521. val & PLANE_CTL_FLIP_HORIZONTAL)
  7522. plane_config->rotation |= DRM_MODE_REFLECT_X;
  7523. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7524. plane_config->base = base;
  7525. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7526. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7527. fb->height = ((val >> 16) & 0xfff) + 1;
  7528. fb->width = ((val >> 0) & 0x1fff) + 1;
  7529. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7530. stride_mult = intel_fb_stride_alignment(fb, 0);
  7531. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7532. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7533. plane_config->size = fb->pitches[0] * aligned_height;
  7534. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7535. crtc->base.name, plane->base.name, fb->width, fb->height,
  7536. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7537. plane_config->size);
  7538. plane_config->fb = intel_fb;
  7539. return;
  7540. error:
  7541. kfree(intel_fb);
  7542. }
  7543. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7544. struct intel_crtc_state *pipe_config)
  7545. {
  7546. struct drm_device *dev = crtc->base.dev;
  7547. struct drm_i915_private *dev_priv = to_i915(dev);
  7548. uint32_t tmp;
  7549. tmp = I915_READ(PF_CTL(crtc->pipe));
  7550. if (tmp & PF_ENABLE) {
  7551. pipe_config->pch_pfit.enabled = true;
  7552. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7553. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7554. /* We currently do not free assignements of panel fitters on
  7555. * ivb/hsw (since we don't use the higher upscaling modes which
  7556. * differentiates them) so just WARN about this case for now. */
  7557. if (IS_GEN7(dev_priv)) {
  7558. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7559. PF_PIPE_SEL_IVB(crtc->pipe));
  7560. }
  7561. }
  7562. }
  7563. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7564. struct intel_crtc_state *pipe_config)
  7565. {
  7566. struct drm_device *dev = crtc->base.dev;
  7567. struct drm_i915_private *dev_priv = to_i915(dev);
  7568. enum intel_display_power_domain power_domain;
  7569. uint32_t tmp;
  7570. bool ret;
  7571. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7572. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7573. return false;
  7574. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7575. pipe_config->shared_dpll = NULL;
  7576. ret = false;
  7577. tmp = I915_READ(PIPECONF(crtc->pipe));
  7578. if (!(tmp & PIPECONF_ENABLE))
  7579. goto out;
  7580. switch (tmp & PIPECONF_BPC_MASK) {
  7581. case PIPECONF_6BPC:
  7582. pipe_config->pipe_bpp = 18;
  7583. break;
  7584. case PIPECONF_8BPC:
  7585. pipe_config->pipe_bpp = 24;
  7586. break;
  7587. case PIPECONF_10BPC:
  7588. pipe_config->pipe_bpp = 30;
  7589. break;
  7590. case PIPECONF_12BPC:
  7591. pipe_config->pipe_bpp = 36;
  7592. break;
  7593. default:
  7594. break;
  7595. }
  7596. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7597. pipe_config->limited_color_range = true;
  7598. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7599. struct intel_shared_dpll *pll;
  7600. enum intel_dpll_id pll_id;
  7601. pipe_config->has_pch_encoder = true;
  7602. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7603. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7604. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7605. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7606. if (HAS_PCH_IBX(dev_priv)) {
  7607. /*
  7608. * The pipe->pch transcoder and pch transcoder->pll
  7609. * mapping is fixed.
  7610. */
  7611. pll_id = (enum intel_dpll_id) crtc->pipe;
  7612. } else {
  7613. tmp = I915_READ(PCH_DPLL_SEL);
  7614. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7615. pll_id = DPLL_ID_PCH_PLL_B;
  7616. else
  7617. pll_id= DPLL_ID_PCH_PLL_A;
  7618. }
  7619. pipe_config->shared_dpll =
  7620. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7621. pll = pipe_config->shared_dpll;
  7622. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7623. &pipe_config->dpll_hw_state));
  7624. tmp = pipe_config->dpll_hw_state.dpll;
  7625. pipe_config->pixel_multiplier =
  7626. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7627. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7628. ironlake_pch_clock_get(crtc, pipe_config);
  7629. } else {
  7630. pipe_config->pixel_multiplier = 1;
  7631. }
  7632. intel_get_pipe_timings(crtc, pipe_config);
  7633. intel_get_pipe_src_size(crtc, pipe_config);
  7634. ironlake_get_pfit_config(crtc, pipe_config);
  7635. ret = true;
  7636. out:
  7637. intel_display_power_put(dev_priv, power_domain);
  7638. return ret;
  7639. }
  7640. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7641. {
  7642. struct drm_device *dev = &dev_priv->drm;
  7643. struct intel_crtc *crtc;
  7644. for_each_intel_crtc(dev, crtc)
  7645. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7646. pipe_name(crtc->pipe));
  7647. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL2),
  7648. "Display power well on\n");
  7649. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7650. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7651. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7652. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7653. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7654. "CPU PWM1 enabled\n");
  7655. if (IS_HASWELL(dev_priv))
  7656. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7657. "CPU PWM2 enabled\n");
  7658. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7659. "PCH PWM1 enabled\n");
  7660. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7661. "Utility pin enabled\n");
  7662. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7663. /*
  7664. * In theory we can still leave IRQs enabled, as long as only the HPD
  7665. * interrupts remain enabled. We used to check for that, but since it's
  7666. * gen-specific and since we only disable LCPLL after we fully disable
  7667. * the interrupts, the check below should be enough.
  7668. */
  7669. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7670. }
  7671. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7672. {
  7673. if (IS_HASWELL(dev_priv))
  7674. return I915_READ(D_COMP_HSW);
  7675. else
  7676. return I915_READ(D_COMP_BDW);
  7677. }
  7678. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7679. {
  7680. if (IS_HASWELL(dev_priv)) {
  7681. mutex_lock(&dev_priv->pcu_lock);
  7682. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7683. val))
  7684. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7685. mutex_unlock(&dev_priv->pcu_lock);
  7686. } else {
  7687. I915_WRITE(D_COMP_BDW, val);
  7688. POSTING_READ(D_COMP_BDW);
  7689. }
  7690. }
  7691. /*
  7692. * This function implements pieces of two sequences from BSpec:
  7693. * - Sequence for display software to disable LCPLL
  7694. * - Sequence for display software to allow package C8+
  7695. * The steps implemented here are just the steps that actually touch the LCPLL
  7696. * register. Callers should take care of disabling all the display engine
  7697. * functions, doing the mode unset, fixing interrupts, etc.
  7698. */
  7699. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7700. bool switch_to_fclk, bool allow_power_down)
  7701. {
  7702. uint32_t val;
  7703. assert_can_disable_lcpll(dev_priv);
  7704. val = I915_READ(LCPLL_CTL);
  7705. if (switch_to_fclk) {
  7706. val |= LCPLL_CD_SOURCE_FCLK;
  7707. I915_WRITE(LCPLL_CTL, val);
  7708. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7709. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7710. DRM_ERROR("Switching to FCLK failed\n");
  7711. val = I915_READ(LCPLL_CTL);
  7712. }
  7713. val |= LCPLL_PLL_DISABLE;
  7714. I915_WRITE(LCPLL_CTL, val);
  7715. POSTING_READ(LCPLL_CTL);
  7716. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7717. DRM_ERROR("LCPLL still locked\n");
  7718. val = hsw_read_dcomp(dev_priv);
  7719. val |= D_COMP_COMP_DISABLE;
  7720. hsw_write_dcomp(dev_priv, val);
  7721. ndelay(100);
  7722. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7723. 1))
  7724. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7725. if (allow_power_down) {
  7726. val = I915_READ(LCPLL_CTL);
  7727. val |= LCPLL_POWER_DOWN_ALLOW;
  7728. I915_WRITE(LCPLL_CTL, val);
  7729. POSTING_READ(LCPLL_CTL);
  7730. }
  7731. }
  7732. /*
  7733. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7734. * source.
  7735. */
  7736. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7737. {
  7738. uint32_t val;
  7739. val = I915_READ(LCPLL_CTL);
  7740. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7741. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7742. return;
  7743. /*
  7744. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7745. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7746. */
  7747. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7748. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7749. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7750. I915_WRITE(LCPLL_CTL, val);
  7751. POSTING_READ(LCPLL_CTL);
  7752. }
  7753. val = hsw_read_dcomp(dev_priv);
  7754. val |= D_COMP_COMP_FORCE;
  7755. val &= ~D_COMP_COMP_DISABLE;
  7756. hsw_write_dcomp(dev_priv, val);
  7757. val = I915_READ(LCPLL_CTL);
  7758. val &= ~LCPLL_PLL_DISABLE;
  7759. I915_WRITE(LCPLL_CTL, val);
  7760. if (intel_wait_for_register(dev_priv,
  7761. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7762. 5))
  7763. DRM_ERROR("LCPLL not locked yet\n");
  7764. if (val & LCPLL_CD_SOURCE_FCLK) {
  7765. val = I915_READ(LCPLL_CTL);
  7766. val &= ~LCPLL_CD_SOURCE_FCLK;
  7767. I915_WRITE(LCPLL_CTL, val);
  7768. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7769. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7770. DRM_ERROR("Switching back to LCPLL failed\n");
  7771. }
  7772. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7773. intel_update_cdclk(dev_priv);
  7774. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7775. }
  7776. /*
  7777. * Package states C8 and deeper are really deep PC states that can only be
  7778. * reached when all the devices on the system allow it, so even if the graphics
  7779. * device allows PC8+, it doesn't mean the system will actually get to these
  7780. * states. Our driver only allows PC8+ when going into runtime PM.
  7781. *
  7782. * The requirements for PC8+ are that all the outputs are disabled, the power
  7783. * well is disabled and most interrupts are disabled, and these are also
  7784. * requirements for runtime PM. When these conditions are met, we manually do
  7785. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7786. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7787. * hang the machine.
  7788. *
  7789. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7790. * the state of some registers, so when we come back from PC8+ we need to
  7791. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7792. * need to take care of the registers kept by RC6. Notice that this happens even
  7793. * if we don't put the device in PCI D3 state (which is what currently happens
  7794. * because of the runtime PM support).
  7795. *
  7796. * For more, read "Display Sequences for Package C8" on the hardware
  7797. * documentation.
  7798. */
  7799. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7800. {
  7801. uint32_t val;
  7802. DRM_DEBUG_KMS("Enabling package C8+\n");
  7803. if (HAS_PCH_LPT_LP(dev_priv)) {
  7804. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7805. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7806. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7807. }
  7808. lpt_disable_clkout_dp(dev_priv);
  7809. hsw_disable_lcpll(dev_priv, true, true);
  7810. }
  7811. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7812. {
  7813. uint32_t val;
  7814. DRM_DEBUG_KMS("Disabling package C8+\n");
  7815. hsw_restore_lcpll(dev_priv);
  7816. lpt_init_pch_refclk(dev_priv);
  7817. if (HAS_PCH_LPT_LP(dev_priv)) {
  7818. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7819. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7820. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7821. }
  7822. }
  7823. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7824. struct intel_crtc_state *crtc_state)
  7825. {
  7826. struct intel_atomic_state *state =
  7827. to_intel_atomic_state(crtc_state->base.state);
  7828. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7829. struct intel_encoder *encoder =
  7830. intel_get_crtc_new_encoder(state, crtc_state);
  7831. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7832. DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
  7833. pipe_name(crtc->pipe));
  7834. return -EINVAL;
  7835. }
  7836. }
  7837. return 0;
  7838. }
  7839. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7840. enum port port,
  7841. struct intel_crtc_state *pipe_config)
  7842. {
  7843. enum intel_dpll_id id;
  7844. u32 temp;
  7845. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7846. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7847. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7848. return;
  7849. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7850. }
  7851. static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7852. enum port port,
  7853. struct intel_crtc_state *pipe_config)
  7854. {
  7855. enum intel_dpll_id id;
  7856. u32 temp;
  7857. /* TODO: TBT pll not implemented. */
  7858. switch (port) {
  7859. case PORT_A:
  7860. case PORT_B:
  7861. temp = I915_READ(DPCLKA_CFGCR0_ICL) &
  7862. DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7863. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7864. if (WARN_ON(id != DPLL_ID_ICL_DPLL0 && id != DPLL_ID_ICL_DPLL1))
  7865. return;
  7866. break;
  7867. case PORT_C:
  7868. id = DPLL_ID_ICL_MGPLL1;
  7869. break;
  7870. case PORT_D:
  7871. id = DPLL_ID_ICL_MGPLL2;
  7872. break;
  7873. case PORT_E:
  7874. id = DPLL_ID_ICL_MGPLL3;
  7875. break;
  7876. case PORT_F:
  7877. id = DPLL_ID_ICL_MGPLL4;
  7878. break;
  7879. default:
  7880. MISSING_CASE(port);
  7881. return;
  7882. }
  7883. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7884. }
  7885. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7886. enum port port,
  7887. struct intel_crtc_state *pipe_config)
  7888. {
  7889. enum intel_dpll_id id;
  7890. switch (port) {
  7891. case PORT_A:
  7892. id = DPLL_ID_SKL_DPLL0;
  7893. break;
  7894. case PORT_B:
  7895. id = DPLL_ID_SKL_DPLL1;
  7896. break;
  7897. case PORT_C:
  7898. id = DPLL_ID_SKL_DPLL2;
  7899. break;
  7900. default:
  7901. DRM_ERROR("Incorrect port type\n");
  7902. return;
  7903. }
  7904. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7905. }
  7906. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7907. enum port port,
  7908. struct intel_crtc_state *pipe_config)
  7909. {
  7910. enum intel_dpll_id id;
  7911. u32 temp;
  7912. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7913. id = temp >> (port * 3 + 1);
  7914. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7915. return;
  7916. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7917. }
  7918. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7919. enum port port,
  7920. struct intel_crtc_state *pipe_config)
  7921. {
  7922. enum intel_dpll_id id;
  7923. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7924. switch (ddi_pll_sel) {
  7925. case PORT_CLK_SEL_WRPLL1:
  7926. id = DPLL_ID_WRPLL1;
  7927. break;
  7928. case PORT_CLK_SEL_WRPLL2:
  7929. id = DPLL_ID_WRPLL2;
  7930. break;
  7931. case PORT_CLK_SEL_SPLL:
  7932. id = DPLL_ID_SPLL;
  7933. break;
  7934. case PORT_CLK_SEL_LCPLL_810:
  7935. id = DPLL_ID_LCPLL_810;
  7936. break;
  7937. case PORT_CLK_SEL_LCPLL_1350:
  7938. id = DPLL_ID_LCPLL_1350;
  7939. break;
  7940. case PORT_CLK_SEL_LCPLL_2700:
  7941. id = DPLL_ID_LCPLL_2700;
  7942. break;
  7943. default:
  7944. MISSING_CASE(ddi_pll_sel);
  7945. /* fall through */
  7946. case PORT_CLK_SEL_NONE:
  7947. return;
  7948. }
  7949. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7950. }
  7951. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7952. struct intel_crtc_state *pipe_config,
  7953. u64 *power_domain_mask)
  7954. {
  7955. struct drm_device *dev = crtc->base.dev;
  7956. struct drm_i915_private *dev_priv = to_i915(dev);
  7957. enum intel_display_power_domain power_domain;
  7958. u32 tmp;
  7959. /*
  7960. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7961. * transcoder handled below.
  7962. */
  7963. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7964. /*
  7965. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7966. * consistency and less surprising code; it's in always on power).
  7967. */
  7968. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7969. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7970. enum pipe trans_edp_pipe;
  7971. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7972. default:
  7973. WARN(1, "unknown pipe linked to edp transcoder\n");
  7974. /* fall through */
  7975. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7976. case TRANS_DDI_EDP_INPUT_A_ON:
  7977. trans_edp_pipe = PIPE_A;
  7978. break;
  7979. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7980. trans_edp_pipe = PIPE_B;
  7981. break;
  7982. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7983. trans_edp_pipe = PIPE_C;
  7984. break;
  7985. }
  7986. if (trans_edp_pipe == crtc->pipe)
  7987. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7988. }
  7989. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7990. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7991. return false;
  7992. *power_domain_mask |= BIT_ULL(power_domain);
  7993. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7994. return tmp & PIPECONF_ENABLE;
  7995. }
  7996. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7997. struct intel_crtc_state *pipe_config,
  7998. u64 *power_domain_mask)
  7999. {
  8000. struct drm_device *dev = crtc->base.dev;
  8001. struct drm_i915_private *dev_priv = to_i915(dev);
  8002. enum intel_display_power_domain power_domain;
  8003. enum port port;
  8004. enum transcoder cpu_transcoder;
  8005. u32 tmp;
  8006. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8007. if (port == PORT_A)
  8008. cpu_transcoder = TRANSCODER_DSI_A;
  8009. else
  8010. cpu_transcoder = TRANSCODER_DSI_C;
  8011. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8012. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8013. continue;
  8014. *power_domain_mask |= BIT_ULL(power_domain);
  8015. /*
  8016. * The PLL needs to be enabled with a valid divider
  8017. * configuration, otherwise accessing DSI registers will hang
  8018. * the machine. See BSpec North Display Engine
  8019. * registers/MIPI[BXT]. We can break out here early, since we
  8020. * need the same DSI PLL to be enabled for both DSI ports.
  8021. */
  8022. if (!bxt_dsi_pll_is_enabled(dev_priv))
  8023. break;
  8024. /* XXX: this works for video mode only */
  8025. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8026. if (!(tmp & DPI_ENABLE))
  8027. continue;
  8028. tmp = I915_READ(MIPI_CTRL(port));
  8029. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8030. continue;
  8031. pipe_config->cpu_transcoder = cpu_transcoder;
  8032. break;
  8033. }
  8034. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8035. }
  8036. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8037. struct intel_crtc_state *pipe_config)
  8038. {
  8039. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8040. struct intel_shared_dpll *pll;
  8041. enum port port;
  8042. uint32_t tmp;
  8043. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8044. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8045. if (IS_ICELAKE(dev_priv))
  8046. icelake_get_ddi_pll(dev_priv, port, pipe_config);
  8047. else if (IS_CANNONLAKE(dev_priv))
  8048. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  8049. else if (IS_GEN9_BC(dev_priv))
  8050. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8051. else if (IS_GEN9_LP(dev_priv))
  8052. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8053. else
  8054. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8055. pll = pipe_config->shared_dpll;
  8056. if (pll) {
  8057. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  8058. &pipe_config->dpll_hw_state));
  8059. }
  8060. /*
  8061. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8062. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8063. * the PCH transcoder is on.
  8064. */
  8065. if (INTEL_GEN(dev_priv) < 9 &&
  8066. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8067. pipe_config->has_pch_encoder = true;
  8068. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8069. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8070. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8071. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8072. }
  8073. }
  8074. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8075. struct intel_crtc_state *pipe_config)
  8076. {
  8077. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8078. enum intel_display_power_domain power_domain;
  8079. u64 power_domain_mask;
  8080. bool active;
  8081. intel_crtc_init_scalers(crtc, pipe_config);
  8082. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8083. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8084. return false;
  8085. power_domain_mask = BIT_ULL(power_domain);
  8086. pipe_config->shared_dpll = NULL;
  8087. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8088. if (IS_GEN9_LP(dev_priv) &&
  8089. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8090. WARN_ON(active);
  8091. active = true;
  8092. }
  8093. if (!active)
  8094. goto out;
  8095. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8096. haswell_get_ddi_port_state(crtc, pipe_config);
  8097. intel_get_pipe_timings(crtc, pipe_config);
  8098. }
  8099. intel_get_pipe_src_size(crtc, pipe_config);
  8100. pipe_config->gamma_mode =
  8101. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8102. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  8103. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  8104. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  8105. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  8106. bool blend_mode_420 = tmp &
  8107. PIPEMISC_YUV420_MODE_FULL_BLEND;
  8108. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  8109. if (pipe_config->ycbcr420 != clrspace_yuv ||
  8110. pipe_config->ycbcr420 != blend_mode_420)
  8111. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  8112. } else if (clrspace_yuv) {
  8113. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  8114. }
  8115. }
  8116. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8117. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8118. power_domain_mask |= BIT_ULL(power_domain);
  8119. if (INTEL_GEN(dev_priv) >= 9)
  8120. skylake_get_pfit_config(crtc, pipe_config);
  8121. else
  8122. ironlake_get_pfit_config(crtc, pipe_config);
  8123. }
  8124. if (hsw_crtc_supports_ips(crtc)) {
  8125. if (IS_HASWELL(dev_priv))
  8126. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  8127. else {
  8128. /*
  8129. * We cannot readout IPS state on broadwell, set to
  8130. * true so we can set it to a defined state on first
  8131. * commit.
  8132. */
  8133. pipe_config->ips_enabled = true;
  8134. }
  8135. }
  8136. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8137. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8138. pipe_config->pixel_multiplier =
  8139. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8140. } else {
  8141. pipe_config->pixel_multiplier = 1;
  8142. }
  8143. out:
  8144. for_each_power_domain(power_domain, power_domain_mask)
  8145. intel_display_power_put(dev_priv, power_domain);
  8146. return active;
  8147. }
  8148. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  8149. {
  8150. struct drm_i915_private *dev_priv =
  8151. to_i915(plane_state->base.plane->dev);
  8152. const struct drm_framebuffer *fb = plane_state->base.fb;
  8153. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8154. u32 base;
  8155. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  8156. base = obj->phys_handle->busaddr;
  8157. else
  8158. base = intel_plane_ggtt_offset(plane_state);
  8159. base += plane_state->color_plane[0].offset;
  8160. /* ILK+ do this automagically */
  8161. if (HAS_GMCH_DISPLAY(dev_priv) &&
  8162. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8163. base += (plane_state->base.crtc_h *
  8164. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  8165. return base;
  8166. }
  8167. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  8168. {
  8169. int x = plane_state->base.crtc_x;
  8170. int y = plane_state->base.crtc_y;
  8171. u32 pos = 0;
  8172. if (x < 0) {
  8173. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8174. x = -x;
  8175. }
  8176. pos |= x << CURSOR_X_SHIFT;
  8177. if (y < 0) {
  8178. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8179. y = -y;
  8180. }
  8181. pos |= y << CURSOR_Y_SHIFT;
  8182. return pos;
  8183. }
  8184. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  8185. {
  8186. const struct drm_mode_config *config =
  8187. &plane_state->base.plane->dev->mode_config;
  8188. int width = plane_state->base.crtc_w;
  8189. int height = plane_state->base.crtc_h;
  8190. return width > 0 && width <= config->cursor_width &&
  8191. height > 0 && height <= config->cursor_height;
  8192. }
  8193. static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
  8194. {
  8195. const struct drm_framebuffer *fb = plane_state->base.fb;
  8196. unsigned int rotation = plane_state->base.rotation;
  8197. int src_x, src_y;
  8198. u32 offset;
  8199. int ret;
  8200. intel_fill_fb_ggtt_view(&plane_state->view, fb, rotation);
  8201. plane_state->color_plane[0].stride = intel_fb_pitch(fb, 0, rotation);
  8202. ret = intel_plane_check_stride(plane_state);
  8203. if (ret)
  8204. return ret;
  8205. src_x = plane_state->base.src_x >> 16;
  8206. src_y = plane_state->base.src_y >> 16;
  8207. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  8208. offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
  8209. plane_state, 0);
  8210. if (src_x != 0 || src_y != 0) {
  8211. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  8212. return -EINVAL;
  8213. }
  8214. plane_state->color_plane[0].offset = offset;
  8215. return 0;
  8216. }
  8217. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  8218. struct intel_plane_state *plane_state)
  8219. {
  8220. const struct drm_framebuffer *fb = plane_state->base.fb;
  8221. int ret;
  8222. if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  8223. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  8224. return -EINVAL;
  8225. }
  8226. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  8227. &crtc_state->base,
  8228. DRM_PLANE_HELPER_NO_SCALING,
  8229. DRM_PLANE_HELPER_NO_SCALING,
  8230. true, true);
  8231. if (ret)
  8232. return ret;
  8233. if (!plane_state->base.visible)
  8234. return 0;
  8235. ret = intel_plane_check_src_coordinates(plane_state);
  8236. if (ret)
  8237. return ret;
  8238. ret = intel_cursor_check_surface(plane_state);
  8239. if (ret)
  8240. return ret;
  8241. return 0;
  8242. }
  8243. static unsigned int
  8244. i845_cursor_max_stride(struct intel_plane *plane,
  8245. u32 pixel_format, u64 modifier,
  8246. unsigned int rotation)
  8247. {
  8248. return 2048;
  8249. }
  8250. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8251. const struct intel_plane_state *plane_state)
  8252. {
  8253. return CURSOR_ENABLE |
  8254. CURSOR_GAMMA_ENABLE |
  8255. CURSOR_FORMAT_ARGB |
  8256. CURSOR_STRIDE(plane_state->color_plane[0].stride);
  8257. }
  8258. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  8259. {
  8260. int width = plane_state->base.crtc_w;
  8261. /*
  8262. * 845g/865g are only limited by the width of their cursors,
  8263. * the height is arbitrary up to the precision of the register.
  8264. */
  8265. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  8266. }
  8267. static int i845_check_cursor(struct intel_crtc_state *crtc_state,
  8268. struct intel_plane_state *plane_state)
  8269. {
  8270. const struct drm_framebuffer *fb = plane_state->base.fb;
  8271. int ret;
  8272. ret = intel_check_cursor(crtc_state, plane_state);
  8273. if (ret)
  8274. return ret;
  8275. /* if we want to turn off the cursor ignore width and height */
  8276. if (!fb)
  8277. return 0;
  8278. /* Check for which cursor types we support */
  8279. if (!i845_cursor_size_ok(plane_state)) {
  8280. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8281. plane_state->base.crtc_w,
  8282. plane_state->base.crtc_h);
  8283. return -EINVAL;
  8284. }
  8285. WARN_ON(plane_state->base.visible &&
  8286. plane_state->color_plane[0].stride != fb->pitches[0]);
  8287. switch (fb->pitches[0]) {
  8288. case 256:
  8289. case 512:
  8290. case 1024:
  8291. case 2048:
  8292. break;
  8293. default:
  8294. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  8295. fb->pitches[0]);
  8296. return -EINVAL;
  8297. }
  8298. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  8299. return 0;
  8300. }
  8301. static void i845_update_cursor(struct intel_plane *plane,
  8302. const struct intel_crtc_state *crtc_state,
  8303. const struct intel_plane_state *plane_state)
  8304. {
  8305. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8306. u32 cntl = 0, base = 0, pos = 0, size = 0;
  8307. unsigned long irqflags;
  8308. if (plane_state && plane_state->base.visible) {
  8309. unsigned int width = plane_state->base.crtc_w;
  8310. unsigned int height = plane_state->base.crtc_h;
  8311. cntl = plane_state->ctl;
  8312. size = (height << 12) | width;
  8313. base = intel_cursor_base(plane_state);
  8314. pos = intel_cursor_position(plane_state);
  8315. }
  8316. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8317. /* On these chipsets we can only modify the base/size/stride
  8318. * whilst the cursor is disabled.
  8319. */
  8320. if (plane->cursor.base != base ||
  8321. plane->cursor.size != size ||
  8322. plane->cursor.cntl != cntl) {
  8323. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  8324. I915_WRITE_FW(CURBASE(PIPE_A), base);
  8325. I915_WRITE_FW(CURSIZE, size);
  8326. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8327. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  8328. plane->cursor.base = base;
  8329. plane->cursor.size = size;
  8330. plane->cursor.cntl = cntl;
  8331. } else {
  8332. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8333. }
  8334. POSTING_READ_FW(CURCNTR(PIPE_A));
  8335. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8336. }
  8337. static void i845_disable_cursor(struct intel_plane *plane,
  8338. struct intel_crtc *crtc)
  8339. {
  8340. i845_update_cursor(plane, NULL, NULL);
  8341. }
  8342. static bool i845_cursor_get_hw_state(struct intel_plane *plane,
  8343. enum pipe *pipe)
  8344. {
  8345. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8346. enum intel_display_power_domain power_domain;
  8347. bool ret;
  8348. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  8349. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8350. return false;
  8351. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  8352. *pipe = PIPE_A;
  8353. intel_display_power_put(dev_priv, power_domain);
  8354. return ret;
  8355. }
  8356. static unsigned int
  8357. i9xx_cursor_max_stride(struct intel_plane *plane,
  8358. u32 pixel_format, u64 modifier,
  8359. unsigned int rotation)
  8360. {
  8361. return plane->base.dev->mode_config.cursor_width * 4;
  8362. }
  8363. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8364. const struct intel_plane_state *plane_state)
  8365. {
  8366. struct drm_i915_private *dev_priv =
  8367. to_i915(plane_state->base.plane->dev);
  8368. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8369. u32 cntl = 0;
  8370. if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  8371. cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
  8372. if (INTEL_GEN(dev_priv) <= 10) {
  8373. cntl |= MCURSOR_GAMMA_ENABLE;
  8374. if (HAS_DDI(dev_priv))
  8375. cntl |= MCURSOR_PIPE_CSC_ENABLE;
  8376. }
  8377. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8378. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8379. switch (plane_state->base.crtc_w) {
  8380. case 64:
  8381. cntl |= MCURSOR_MODE_64_ARGB_AX;
  8382. break;
  8383. case 128:
  8384. cntl |= MCURSOR_MODE_128_ARGB_AX;
  8385. break;
  8386. case 256:
  8387. cntl |= MCURSOR_MODE_256_ARGB_AX;
  8388. break;
  8389. default:
  8390. MISSING_CASE(plane_state->base.crtc_w);
  8391. return 0;
  8392. }
  8393. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8394. cntl |= MCURSOR_ROTATE_180;
  8395. return cntl;
  8396. }
  8397. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8398. {
  8399. struct drm_i915_private *dev_priv =
  8400. to_i915(plane_state->base.plane->dev);
  8401. int width = plane_state->base.crtc_w;
  8402. int height = plane_state->base.crtc_h;
  8403. if (!intel_cursor_size_ok(plane_state))
  8404. return false;
  8405. /* Cursor width is limited to a few power-of-two sizes */
  8406. switch (width) {
  8407. case 256:
  8408. case 128:
  8409. case 64:
  8410. break;
  8411. default:
  8412. return false;
  8413. }
  8414. /*
  8415. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8416. * height from 8 lines up to the cursor width, when the
  8417. * cursor is not rotated. Everything else requires square
  8418. * cursors.
  8419. */
  8420. if (HAS_CUR_FBC(dev_priv) &&
  8421. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8422. if (height < 8 || height > width)
  8423. return false;
  8424. } else {
  8425. if (height != width)
  8426. return false;
  8427. }
  8428. return true;
  8429. }
  8430. static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
  8431. struct intel_plane_state *plane_state)
  8432. {
  8433. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  8434. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8435. const struct drm_framebuffer *fb = plane_state->base.fb;
  8436. enum pipe pipe = plane->pipe;
  8437. int ret;
  8438. ret = intel_check_cursor(crtc_state, plane_state);
  8439. if (ret)
  8440. return ret;
  8441. /* if we want to turn off the cursor ignore width and height */
  8442. if (!fb)
  8443. return 0;
  8444. /* Check for which cursor types we support */
  8445. if (!i9xx_cursor_size_ok(plane_state)) {
  8446. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8447. plane_state->base.crtc_w,
  8448. plane_state->base.crtc_h);
  8449. return -EINVAL;
  8450. }
  8451. WARN_ON(plane_state->base.visible &&
  8452. plane_state->color_plane[0].stride != fb->pitches[0]);
  8453. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8454. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8455. fb->pitches[0], plane_state->base.crtc_w);
  8456. return -EINVAL;
  8457. }
  8458. /*
  8459. * There's something wrong with the cursor on CHV pipe C.
  8460. * If it straddles the left edge of the screen then
  8461. * moving it away from the edge or disabling it often
  8462. * results in a pipe underrun, and often that can lead to
  8463. * dead pipe (constant underrun reported, and it scans
  8464. * out just a solid color). To recover from that, the
  8465. * display power well must be turned off and on again.
  8466. * Refuse the put the cursor into that compromised position.
  8467. */
  8468. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8469. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8470. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8471. return -EINVAL;
  8472. }
  8473. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8474. return 0;
  8475. }
  8476. static void i9xx_update_cursor(struct intel_plane *plane,
  8477. const struct intel_crtc_state *crtc_state,
  8478. const struct intel_plane_state *plane_state)
  8479. {
  8480. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8481. enum pipe pipe = plane->pipe;
  8482. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8483. unsigned long irqflags;
  8484. if (plane_state && plane_state->base.visible) {
  8485. cntl = plane_state->ctl;
  8486. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8487. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8488. base = intel_cursor_base(plane_state);
  8489. pos = intel_cursor_position(plane_state);
  8490. }
  8491. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8492. /*
  8493. * On some platforms writing CURCNTR first will also
  8494. * cause CURPOS to be armed by the CURBASE write.
  8495. * Without the CURCNTR write the CURPOS write would
  8496. * arm itself. Thus we always start the full update
  8497. * with a CURCNTR write.
  8498. *
  8499. * On other platforms CURPOS always requires the
  8500. * CURBASE write to arm the update. Additonally
  8501. * a write to any of the cursor register will cancel
  8502. * an already armed cursor update. Thus leaving out
  8503. * the CURBASE write after CURPOS could lead to a
  8504. * cursor that doesn't appear to move, or even change
  8505. * shape. Thus we always write CURBASE.
  8506. *
  8507. * CURCNTR and CUR_FBC_CTL are always
  8508. * armed by the CURBASE write only.
  8509. */
  8510. if (plane->cursor.base != base ||
  8511. plane->cursor.size != fbc_ctl ||
  8512. plane->cursor.cntl != cntl) {
  8513. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8514. if (HAS_CUR_FBC(dev_priv))
  8515. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8516. I915_WRITE_FW(CURPOS(pipe), pos);
  8517. I915_WRITE_FW(CURBASE(pipe), base);
  8518. plane->cursor.base = base;
  8519. plane->cursor.size = fbc_ctl;
  8520. plane->cursor.cntl = cntl;
  8521. } else {
  8522. I915_WRITE_FW(CURPOS(pipe), pos);
  8523. I915_WRITE_FW(CURBASE(pipe), base);
  8524. }
  8525. POSTING_READ_FW(CURBASE(pipe));
  8526. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8527. }
  8528. static void i9xx_disable_cursor(struct intel_plane *plane,
  8529. struct intel_crtc *crtc)
  8530. {
  8531. i9xx_update_cursor(plane, NULL, NULL);
  8532. }
  8533. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
  8534. enum pipe *pipe)
  8535. {
  8536. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8537. enum intel_display_power_domain power_domain;
  8538. bool ret;
  8539. u32 val;
  8540. /*
  8541. * Not 100% correct for planes that can move between pipes,
  8542. * but that's only the case for gen2-3 which don't have any
  8543. * display power wells.
  8544. */
  8545. power_domain = POWER_DOMAIN_PIPE(plane->pipe);
  8546. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8547. return false;
  8548. val = I915_READ(CURCNTR(plane->pipe));
  8549. ret = val & MCURSOR_MODE;
  8550. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8551. *pipe = plane->pipe;
  8552. else
  8553. *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
  8554. MCURSOR_PIPE_SELECT_SHIFT;
  8555. intel_display_power_put(dev_priv, power_domain);
  8556. return ret;
  8557. }
  8558. /* VESA 640x480x72Hz mode to set on the pipe */
  8559. static const struct drm_display_mode load_detect_mode = {
  8560. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8561. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8562. };
  8563. struct drm_framebuffer *
  8564. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8565. struct drm_mode_fb_cmd2 *mode_cmd)
  8566. {
  8567. struct intel_framebuffer *intel_fb;
  8568. int ret;
  8569. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8570. if (!intel_fb)
  8571. return ERR_PTR(-ENOMEM);
  8572. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8573. if (ret)
  8574. goto err;
  8575. return &intel_fb->base;
  8576. err:
  8577. kfree(intel_fb);
  8578. return ERR_PTR(ret);
  8579. }
  8580. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8581. struct drm_crtc *crtc)
  8582. {
  8583. struct drm_plane *plane;
  8584. struct drm_plane_state *plane_state;
  8585. int ret, i;
  8586. ret = drm_atomic_add_affected_planes(state, crtc);
  8587. if (ret)
  8588. return ret;
  8589. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8590. if (plane_state->crtc != crtc)
  8591. continue;
  8592. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8593. if (ret)
  8594. return ret;
  8595. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8596. }
  8597. return 0;
  8598. }
  8599. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8600. const struct drm_display_mode *mode,
  8601. struct intel_load_detect_pipe *old,
  8602. struct drm_modeset_acquire_ctx *ctx)
  8603. {
  8604. struct intel_crtc *intel_crtc;
  8605. struct intel_encoder *intel_encoder =
  8606. intel_attached_encoder(connector);
  8607. struct drm_crtc *possible_crtc;
  8608. struct drm_encoder *encoder = &intel_encoder->base;
  8609. struct drm_crtc *crtc = NULL;
  8610. struct drm_device *dev = encoder->dev;
  8611. struct drm_i915_private *dev_priv = to_i915(dev);
  8612. struct drm_mode_config *config = &dev->mode_config;
  8613. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8614. struct drm_connector_state *connector_state;
  8615. struct intel_crtc_state *crtc_state;
  8616. int ret, i = -1;
  8617. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8618. connector->base.id, connector->name,
  8619. encoder->base.id, encoder->name);
  8620. old->restore_state = NULL;
  8621. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8622. /*
  8623. * Algorithm gets a little messy:
  8624. *
  8625. * - if the connector already has an assigned crtc, use it (but make
  8626. * sure it's on first)
  8627. *
  8628. * - try to find the first unused crtc that can drive this connector,
  8629. * and use that if we find one
  8630. */
  8631. /* See if we already have a CRTC for this connector */
  8632. if (connector->state->crtc) {
  8633. crtc = connector->state->crtc;
  8634. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8635. if (ret)
  8636. goto fail;
  8637. /* Make sure the crtc and connector are running */
  8638. goto found;
  8639. }
  8640. /* Find an unused one (if possible) */
  8641. for_each_crtc(dev, possible_crtc) {
  8642. i++;
  8643. if (!(encoder->possible_crtcs & (1 << i)))
  8644. continue;
  8645. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8646. if (ret)
  8647. goto fail;
  8648. if (possible_crtc->state->enable) {
  8649. drm_modeset_unlock(&possible_crtc->mutex);
  8650. continue;
  8651. }
  8652. crtc = possible_crtc;
  8653. break;
  8654. }
  8655. /*
  8656. * If we didn't find an unused CRTC, don't use any.
  8657. */
  8658. if (!crtc) {
  8659. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8660. ret = -ENODEV;
  8661. goto fail;
  8662. }
  8663. found:
  8664. intel_crtc = to_intel_crtc(crtc);
  8665. state = drm_atomic_state_alloc(dev);
  8666. restore_state = drm_atomic_state_alloc(dev);
  8667. if (!state || !restore_state) {
  8668. ret = -ENOMEM;
  8669. goto fail;
  8670. }
  8671. state->acquire_ctx = ctx;
  8672. restore_state->acquire_ctx = ctx;
  8673. connector_state = drm_atomic_get_connector_state(state, connector);
  8674. if (IS_ERR(connector_state)) {
  8675. ret = PTR_ERR(connector_state);
  8676. goto fail;
  8677. }
  8678. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8679. if (ret)
  8680. goto fail;
  8681. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8682. if (IS_ERR(crtc_state)) {
  8683. ret = PTR_ERR(crtc_state);
  8684. goto fail;
  8685. }
  8686. crtc_state->base.active = crtc_state->base.enable = true;
  8687. if (!mode)
  8688. mode = &load_detect_mode;
  8689. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8690. if (ret)
  8691. goto fail;
  8692. ret = intel_modeset_disable_planes(state, crtc);
  8693. if (ret)
  8694. goto fail;
  8695. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8696. if (!ret)
  8697. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8698. if (!ret)
  8699. ret = drm_atomic_add_affected_planes(restore_state, crtc);
  8700. if (ret) {
  8701. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8702. goto fail;
  8703. }
  8704. ret = drm_atomic_commit(state);
  8705. if (ret) {
  8706. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8707. goto fail;
  8708. }
  8709. old->restore_state = restore_state;
  8710. drm_atomic_state_put(state);
  8711. /* let the connector get through one full cycle before testing */
  8712. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8713. return true;
  8714. fail:
  8715. if (state) {
  8716. drm_atomic_state_put(state);
  8717. state = NULL;
  8718. }
  8719. if (restore_state) {
  8720. drm_atomic_state_put(restore_state);
  8721. restore_state = NULL;
  8722. }
  8723. if (ret == -EDEADLK)
  8724. return ret;
  8725. return false;
  8726. }
  8727. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8728. struct intel_load_detect_pipe *old,
  8729. struct drm_modeset_acquire_ctx *ctx)
  8730. {
  8731. struct intel_encoder *intel_encoder =
  8732. intel_attached_encoder(connector);
  8733. struct drm_encoder *encoder = &intel_encoder->base;
  8734. struct drm_atomic_state *state = old->restore_state;
  8735. int ret;
  8736. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8737. connector->base.id, connector->name,
  8738. encoder->base.id, encoder->name);
  8739. if (!state)
  8740. return;
  8741. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8742. if (ret)
  8743. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8744. drm_atomic_state_put(state);
  8745. }
  8746. static int i9xx_pll_refclk(struct drm_device *dev,
  8747. const struct intel_crtc_state *pipe_config)
  8748. {
  8749. struct drm_i915_private *dev_priv = to_i915(dev);
  8750. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8751. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8752. return dev_priv->vbt.lvds_ssc_freq;
  8753. else if (HAS_PCH_SPLIT(dev_priv))
  8754. return 120000;
  8755. else if (!IS_GEN2(dev_priv))
  8756. return 96000;
  8757. else
  8758. return 48000;
  8759. }
  8760. /* Returns the clock of the currently programmed mode of the given pipe. */
  8761. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8762. struct intel_crtc_state *pipe_config)
  8763. {
  8764. struct drm_device *dev = crtc->base.dev;
  8765. struct drm_i915_private *dev_priv = to_i915(dev);
  8766. int pipe = pipe_config->cpu_transcoder;
  8767. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8768. u32 fp;
  8769. struct dpll clock;
  8770. int port_clock;
  8771. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8772. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8773. fp = pipe_config->dpll_hw_state.fp0;
  8774. else
  8775. fp = pipe_config->dpll_hw_state.fp1;
  8776. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8777. if (IS_PINEVIEW(dev_priv)) {
  8778. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8779. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8780. } else {
  8781. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8782. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8783. }
  8784. if (!IS_GEN2(dev_priv)) {
  8785. if (IS_PINEVIEW(dev_priv))
  8786. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8787. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8788. else
  8789. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8790. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8791. switch (dpll & DPLL_MODE_MASK) {
  8792. case DPLLB_MODE_DAC_SERIAL:
  8793. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8794. 5 : 10;
  8795. break;
  8796. case DPLLB_MODE_LVDS:
  8797. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8798. 7 : 14;
  8799. break;
  8800. default:
  8801. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8802. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8803. return;
  8804. }
  8805. if (IS_PINEVIEW(dev_priv))
  8806. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8807. else
  8808. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8809. } else {
  8810. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8811. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8812. if (is_lvds) {
  8813. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8814. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8815. if (lvds & LVDS_CLKB_POWER_UP)
  8816. clock.p2 = 7;
  8817. else
  8818. clock.p2 = 14;
  8819. } else {
  8820. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8821. clock.p1 = 2;
  8822. else {
  8823. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8824. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8825. }
  8826. if (dpll & PLL_P2_DIVIDE_BY_4)
  8827. clock.p2 = 4;
  8828. else
  8829. clock.p2 = 2;
  8830. }
  8831. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8832. }
  8833. /*
  8834. * This value includes pixel_multiplier. We will use
  8835. * port_clock to compute adjusted_mode.crtc_clock in the
  8836. * encoder's get_config() function.
  8837. */
  8838. pipe_config->port_clock = port_clock;
  8839. }
  8840. int intel_dotclock_calculate(int link_freq,
  8841. const struct intel_link_m_n *m_n)
  8842. {
  8843. /*
  8844. * The calculation for the data clock is:
  8845. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8846. * But we want to avoid losing precison if possible, so:
  8847. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8848. *
  8849. * and the link clock is simpler:
  8850. * link_clock = (m * link_clock) / n
  8851. */
  8852. if (!m_n->link_n)
  8853. return 0;
  8854. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8855. }
  8856. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8857. struct intel_crtc_state *pipe_config)
  8858. {
  8859. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8860. /* read out port_clock from the DPLL */
  8861. i9xx_crtc_clock_get(crtc, pipe_config);
  8862. /*
  8863. * In case there is an active pipe without active ports,
  8864. * we may need some idea for the dotclock anyway.
  8865. * Calculate one based on the FDI configuration.
  8866. */
  8867. pipe_config->base.adjusted_mode.crtc_clock =
  8868. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8869. &pipe_config->fdi_m_n);
  8870. }
  8871. /* Returns the currently programmed mode of the given encoder. */
  8872. struct drm_display_mode *
  8873. intel_encoder_current_mode(struct intel_encoder *encoder)
  8874. {
  8875. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8876. struct intel_crtc_state *crtc_state;
  8877. struct drm_display_mode *mode;
  8878. struct intel_crtc *crtc;
  8879. enum pipe pipe;
  8880. if (!encoder->get_hw_state(encoder, &pipe))
  8881. return NULL;
  8882. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8883. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8884. if (!mode)
  8885. return NULL;
  8886. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8887. if (!crtc_state) {
  8888. kfree(mode);
  8889. return NULL;
  8890. }
  8891. crtc_state->base.crtc = &crtc->base;
  8892. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8893. kfree(crtc_state);
  8894. kfree(mode);
  8895. return NULL;
  8896. }
  8897. encoder->get_config(encoder, crtc_state);
  8898. intel_mode_from_pipe_config(mode, crtc_state);
  8899. kfree(crtc_state);
  8900. return mode;
  8901. }
  8902. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8903. {
  8904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8905. drm_crtc_cleanup(crtc);
  8906. kfree(intel_crtc);
  8907. }
  8908. /**
  8909. * intel_wm_need_update - Check whether watermarks need updating
  8910. * @plane: drm plane
  8911. * @state: new plane state
  8912. *
  8913. * Check current plane state versus the new one to determine whether
  8914. * watermarks need to be recalculated.
  8915. *
  8916. * Returns true or false.
  8917. */
  8918. static bool intel_wm_need_update(struct drm_plane *plane,
  8919. struct drm_plane_state *state)
  8920. {
  8921. struct intel_plane_state *new = to_intel_plane_state(state);
  8922. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8923. /* Update watermarks on tiling or size changes. */
  8924. if (new->base.visible != cur->base.visible)
  8925. return true;
  8926. if (!cur->base.fb || !new->base.fb)
  8927. return false;
  8928. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8929. cur->base.rotation != new->base.rotation ||
  8930. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8931. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8932. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8933. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8934. return true;
  8935. return false;
  8936. }
  8937. static bool needs_scaling(const struct intel_plane_state *state)
  8938. {
  8939. int src_w = drm_rect_width(&state->base.src) >> 16;
  8940. int src_h = drm_rect_height(&state->base.src) >> 16;
  8941. int dst_w = drm_rect_width(&state->base.dst);
  8942. int dst_h = drm_rect_height(&state->base.dst);
  8943. return (src_w != dst_w || src_h != dst_h);
  8944. }
  8945. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8946. struct drm_crtc_state *crtc_state,
  8947. const struct intel_plane_state *old_plane_state,
  8948. struct drm_plane_state *plane_state)
  8949. {
  8950. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8951. struct drm_crtc *crtc = crtc_state->crtc;
  8952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8953. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8954. struct drm_device *dev = crtc->dev;
  8955. struct drm_i915_private *dev_priv = to_i915(dev);
  8956. bool mode_changed = needs_modeset(crtc_state);
  8957. bool was_crtc_enabled = old_crtc_state->base.active;
  8958. bool is_crtc_enabled = crtc_state->active;
  8959. bool turn_off, turn_on, visible, was_visible;
  8960. struct drm_framebuffer *fb = plane_state->fb;
  8961. int ret;
  8962. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8963. ret = skl_update_scaler_plane(
  8964. to_intel_crtc_state(crtc_state),
  8965. to_intel_plane_state(plane_state));
  8966. if (ret)
  8967. return ret;
  8968. }
  8969. was_visible = old_plane_state->base.visible;
  8970. visible = plane_state->visible;
  8971. if (!was_crtc_enabled && WARN_ON(was_visible))
  8972. was_visible = false;
  8973. /*
  8974. * Visibility is calculated as if the crtc was on, but
  8975. * after scaler setup everything depends on it being off
  8976. * when the crtc isn't active.
  8977. *
  8978. * FIXME this is wrong for watermarks. Watermarks should also
  8979. * be computed as if the pipe would be active. Perhaps move
  8980. * per-plane wm computation to the .check_plane() hook, and
  8981. * only combine the results from all planes in the current place?
  8982. */
  8983. if (!is_crtc_enabled) {
  8984. plane_state->visible = visible = false;
  8985. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8986. }
  8987. if (!was_visible && !visible)
  8988. return 0;
  8989. if (fb != old_plane_state->base.fb)
  8990. pipe_config->fb_changed = true;
  8991. turn_off = was_visible && (!visible || mode_changed);
  8992. turn_on = visible && (!was_visible || mode_changed);
  8993. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8994. intel_crtc->base.base.id, intel_crtc->base.name,
  8995. plane->base.base.id, plane->base.name,
  8996. fb ? fb->base.id : -1);
  8997. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8998. plane->base.base.id, plane->base.name,
  8999. was_visible, visible,
  9000. turn_off, turn_on, mode_changed);
  9001. if (turn_on) {
  9002. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9003. pipe_config->update_wm_pre = true;
  9004. /* must disable cxsr around plane enable/disable */
  9005. if (plane->id != PLANE_CURSOR)
  9006. pipe_config->disable_cxsr = true;
  9007. } else if (turn_off) {
  9008. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9009. pipe_config->update_wm_post = true;
  9010. /* must disable cxsr around plane enable/disable */
  9011. if (plane->id != PLANE_CURSOR)
  9012. pipe_config->disable_cxsr = true;
  9013. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  9014. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  9015. /* FIXME bollocks */
  9016. pipe_config->update_wm_pre = true;
  9017. pipe_config->update_wm_post = true;
  9018. }
  9019. }
  9020. if (visible || was_visible)
  9021. pipe_config->fb_bits |= plane->frontbuffer_bit;
  9022. /*
  9023. * WaCxSRDisabledForSpriteScaling:ivb
  9024. *
  9025. * cstate->update_wm was already set above, so this flag will
  9026. * take effect when we commit and program watermarks.
  9027. */
  9028. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  9029. needs_scaling(to_intel_plane_state(plane_state)) &&
  9030. !needs_scaling(old_plane_state))
  9031. pipe_config->disable_lp_wm = true;
  9032. return 0;
  9033. }
  9034. static bool encoders_cloneable(const struct intel_encoder *a,
  9035. const struct intel_encoder *b)
  9036. {
  9037. /* masks could be asymmetric, so check both ways */
  9038. return a == b || (a->cloneable & (1 << b->type) &&
  9039. b->cloneable & (1 << a->type));
  9040. }
  9041. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9042. struct intel_crtc *crtc,
  9043. struct intel_encoder *encoder)
  9044. {
  9045. struct intel_encoder *source_encoder;
  9046. struct drm_connector *connector;
  9047. struct drm_connector_state *connector_state;
  9048. int i;
  9049. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9050. if (connector_state->crtc != &crtc->base)
  9051. continue;
  9052. source_encoder =
  9053. to_intel_encoder(connector_state->best_encoder);
  9054. if (!encoders_cloneable(encoder, source_encoder))
  9055. return false;
  9056. }
  9057. return true;
  9058. }
  9059. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9060. struct drm_crtc_state *crtc_state)
  9061. {
  9062. struct drm_device *dev = crtc->dev;
  9063. struct drm_i915_private *dev_priv = to_i915(dev);
  9064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9065. struct intel_crtc_state *pipe_config =
  9066. to_intel_crtc_state(crtc_state);
  9067. struct drm_atomic_state *state = crtc_state->state;
  9068. int ret;
  9069. bool mode_changed = needs_modeset(crtc_state);
  9070. if (mode_changed && !crtc_state->active)
  9071. pipe_config->update_wm_post = true;
  9072. if (mode_changed && crtc_state->enable &&
  9073. dev_priv->display.crtc_compute_clock &&
  9074. !WARN_ON(pipe_config->shared_dpll)) {
  9075. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9076. pipe_config);
  9077. if (ret)
  9078. return ret;
  9079. }
  9080. if (crtc_state->color_mgmt_changed) {
  9081. ret = intel_color_check(crtc, crtc_state);
  9082. if (ret)
  9083. return ret;
  9084. /*
  9085. * Changing color management on Intel hardware is
  9086. * handled as part of planes update.
  9087. */
  9088. crtc_state->planes_changed = true;
  9089. }
  9090. ret = 0;
  9091. if (dev_priv->display.compute_pipe_wm) {
  9092. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9093. if (ret) {
  9094. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9095. return ret;
  9096. }
  9097. }
  9098. if (dev_priv->display.compute_intermediate_wm &&
  9099. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9100. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9101. return 0;
  9102. /*
  9103. * Calculate 'intermediate' watermarks that satisfy both the
  9104. * old state and the new state. We can program these
  9105. * immediately.
  9106. */
  9107. ret = dev_priv->display.compute_intermediate_wm(dev,
  9108. intel_crtc,
  9109. pipe_config);
  9110. if (ret) {
  9111. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9112. return ret;
  9113. }
  9114. } else if (dev_priv->display.compute_intermediate_wm) {
  9115. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9116. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9117. }
  9118. if (INTEL_GEN(dev_priv) >= 9) {
  9119. if (mode_changed)
  9120. ret = skl_update_scaler_crtc(pipe_config);
  9121. if (!ret)
  9122. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  9123. pipe_config);
  9124. if (!ret)
  9125. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9126. pipe_config);
  9127. }
  9128. if (HAS_IPS(dev_priv))
  9129. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  9130. return ret;
  9131. }
  9132. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9133. .atomic_begin = intel_begin_crtc_commit,
  9134. .atomic_flush = intel_finish_crtc_commit,
  9135. .atomic_check = intel_crtc_atomic_check,
  9136. };
  9137. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9138. {
  9139. struct intel_connector *connector;
  9140. struct drm_connector_list_iter conn_iter;
  9141. drm_connector_list_iter_begin(dev, &conn_iter);
  9142. for_each_intel_connector_iter(connector, &conn_iter) {
  9143. if (connector->base.state->crtc)
  9144. drm_connector_put(&connector->base);
  9145. if (connector->base.encoder) {
  9146. connector->base.state->best_encoder =
  9147. connector->base.encoder;
  9148. connector->base.state->crtc =
  9149. connector->base.encoder->crtc;
  9150. drm_connector_get(&connector->base);
  9151. } else {
  9152. connector->base.state->best_encoder = NULL;
  9153. connector->base.state->crtc = NULL;
  9154. }
  9155. }
  9156. drm_connector_list_iter_end(&conn_iter);
  9157. }
  9158. static void
  9159. connected_sink_compute_bpp(struct intel_connector *connector,
  9160. struct intel_crtc_state *pipe_config)
  9161. {
  9162. const struct drm_display_info *info = &connector->base.display_info;
  9163. int bpp = pipe_config->pipe_bpp;
  9164. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9165. connector->base.base.id,
  9166. connector->base.name);
  9167. /* Don't use an invalid EDID bpc value */
  9168. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9169. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9170. bpp, info->bpc * 3);
  9171. pipe_config->pipe_bpp = info->bpc * 3;
  9172. }
  9173. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9174. if (info->bpc == 0 && bpp > 24) {
  9175. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9176. bpp);
  9177. pipe_config->pipe_bpp = 24;
  9178. }
  9179. }
  9180. static int
  9181. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9182. struct intel_crtc_state *pipe_config)
  9183. {
  9184. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9185. struct drm_atomic_state *state;
  9186. struct drm_connector *connector;
  9187. struct drm_connector_state *connector_state;
  9188. int bpp, i;
  9189. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9190. IS_CHERRYVIEW(dev_priv)))
  9191. bpp = 10*3;
  9192. else if (INTEL_GEN(dev_priv) >= 5)
  9193. bpp = 12*3;
  9194. else
  9195. bpp = 8*3;
  9196. pipe_config->pipe_bpp = bpp;
  9197. state = pipe_config->base.state;
  9198. /* Clamp display bpp to EDID value */
  9199. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9200. if (connector_state->crtc != &crtc->base)
  9201. continue;
  9202. connected_sink_compute_bpp(to_intel_connector(connector),
  9203. pipe_config);
  9204. }
  9205. return bpp;
  9206. }
  9207. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9208. {
  9209. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9210. "type: 0x%x flags: 0x%x\n",
  9211. mode->crtc_clock,
  9212. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9213. mode->crtc_hsync_end, mode->crtc_htotal,
  9214. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9215. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9216. }
  9217. static inline void
  9218. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9219. unsigned int lane_count, struct intel_link_m_n *m_n)
  9220. {
  9221. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9222. id, lane_count,
  9223. m_n->gmch_m, m_n->gmch_n,
  9224. m_n->link_m, m_n->link_n, m_n->tu);
  9225. }
  9226. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  9227. static const char * const output_type_str[] = {
  9228. OUTPUT_TYPE(UNUSED),
  9229. OUTPUT_TYPE(ANALOG),
  9230. OUTPUT_TYPE(DVO),
  9231. OUTPUT_TYPE(SDVO),
  9232. OUTPUT_TYPE(LVDS),
  9233. OUTPUT_TYPE(TVOUT),
  9234. OUTPUT_TYPE(HDMI),
  9235. OUTPUT_TYPE(DP),
  9236. OUTPUT_TYPE(EDP),
  9237. OUTPUT_TYPE(DSI),
  9238. OUTPUT_TYPE(DDI),
  9239. OUTPUT_TYPE(DP_MST),
  9240. };
  9241. #undef OUTPUT_TYPE
  9242. static void snprintf_output_types(char *buf, size_t len,
  9243. unsigned int output_types)
  9244. {
  9245. char *str = buf;
  9246. int i;
  9247. str[0] = '\0';
  9248. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  9249. int r;
  9250. if ((output_types & BIT(i)) == 0)
  9251. continue;
  9252. r = snprintf(str, len, "%s%s",
  9253. str != buf ? "," : "", output_type_str[i]);
  9254. if (r >= len)
  9255. break;
  9256. str += r;
  9257. len -= r;
  9258. output_types &= ~BIT(i);
  9259. }
  9260. WARN_ON_ONCE(output_types != 0);
  9261. }
  9262. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9263. struct intel_crtc_state *pipe_config,
  9264. const char *context)
  9265. {
  9266. struct drm_device *dev = crtc->base.dev;
  9267. struct drm_i915_private *dev_priv = to_i915(dev);
  9268. struct drm_plane *plane;
  9269. struct intel_plane *intel_plane;
  9270. struct intel_plane_state *state;
  9271. struct drm_framebuffer *fb;
  9272. char buf[64];
  9273. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9274. crtc->base.base.id, crtc->base.name, context);
  9275. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  9276. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  9277. buf, pipe_config->output_types);
  9278. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9279. transcoder_name(pipe_config->cpu_transcoder),
  9280. pipe_config->pipe_bpp, pipe_config->dither);
  9281. if (pipe_config->has_pch_encoder)
  9282. intel_dump_m_n_config(pipe_config, "fdi",
  9283. pipe_config->fdi_lanes,
  9284. &pipe_config->fdi_m_n);
  9285. if (pipe_config->ycbcr420)
  9286. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  9287. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9288. intel_dump_m_n_config(pipe_config, "dp m_n",
  9289. pipe_config->lane_count, &pipe_config->dp_m_n);
  9290. if (pipe_config->has_drrs)
  9291. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9292. pipe_config->lane_count,
  9293. &pipe_config->dp_m2_n2);
  9294. }
  9295. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9296. pipe_config->has_audio, pipe_config->has_infoframe);
  9297. DRM_DEBUG_KMS("requested mode:\n");
  9298. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9299. DRM_DEBUG_KMS("adjusted mode:\n");
  9300. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9301. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9302. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9303. pipe_config->port_clock,
  9304. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9305. pipe_config->pixel_rate);
  9306. if (INTEL_GEN(dev_priv) >= 9)
  9307. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9308. crtc->num_scalers,
  9309. pipe_config->scaler_state.scaler_users,
  9310. pipe_config->scaler_state.scaler_id);
  9311. if (HAS_GMCH_DISPLAY(dev_priv))
  9312. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9313. pipe_config->gmch_pfit.control,
  9314. pipe_config->gmch_pfit.pgm_ratios,
  9315. pipe_config->gmch_pfit.lvds_border_bits);
  9316. else
  9317. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9318. pipe_config->pch_pfit.pos,
  9319. pipe_config->pch_pfit.size,
  9320. enableddisabled(pipe_config->pch_pfit.enabled));
  9321. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9322. pipe_config->ips_enabled, pipe_config->double_wide);
  9323. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9324. DRM_DEBUG_KMS("planes on this crtc\n");
  9325. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9326. struct drm_format_name_buf format_name;
  9327. intel_plane = to_intel_plane(plane);
  9328. if (intel_plane->pipe != crtc->pipe)
  9329. continue;
  9330. state = to_intel_plane_state(plane->state);
  9331. fb = state->base.fb;
  9332. if (!fb) {
  9333. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9334. plane->base.id, plane->name, state->scaler_id);
  9335. continue;
  9336. }
  9337. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9338. plane->base.id, plane->name,
  9339. fb->base.id, fb->width, fb->height,
  9340. drm_get_format_name(fb->format->format, &format_name));
  9341. if (INTEL_GEN(dev_priv) >= 9)
  9342. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9343. state->scaler_id,
  9344. state->base.src.x1 >> 16,
  9345. state->base.src.y1 >> 16,
  9346. drm_rect_width(&state->base.src) >> 16,
  9347. drm_rect_height(&state->base.src) >> 16,
  9348. state->base.dst.x1, state->base.dst.y1,
  9349. drm_rect_width(&state->base.dst),
  9350. drm_rect_height(&state->base.dst));
  9351. }
  9352. }
  9353. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9354. {
  9355. struct drm_device *dev = state->dev;
  9356. struct drm_connector *connector;
  9357. struct drm_connector_list_iter conn_iter;
  9358. unsigned int used_ports = 0;
  9359. unsigned int used_mst_ports = 0;
  9360. bool ret = true;
  9361. /*
  9362. * Walk the connector list instead of the encoder
  9363. * list to detect the problem on ddi platforms
  9364. * where there's just one encoder per digital port.
  9365. */
  9366. drm_connector_list_iter_begin(dev, &conn_iter);
  9367. drm_for_each_connector_iter(connector, &conn_iter) {
  9368. struct drm_connector_state *connector_state;
  9369. struct intel_encoder *encoder;
  9370. connector_state = drm_atomic_get_new_connector_state(state, connector);
  9371. if (!connector_state)
  9372. connector_state = connector->state;
  9373. if (!connector_state->best_encoder)
  9374. continue;
  9375. encoder = to_intel_encoder(connector_state->best_encoder);
  9376. WARN_ON(!connector_state->crtc);
  9377. switch (encoder->type) {
  9378. unsigned int port_mask;
  9379. case INTEL_OUTPUT_DDI:
  9380. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9381. break;
  9382. /* else: fall through */
  9383. case INTEL_OUTPUT_DP:
  9384. case INTEL_OUTPUT_HDMI:
  9385. case INTEL_OUTPUT_EDP:
  9386. port_mask = 1 << encoder->port;
  9387. /* the same port mustn't appear more than once */
  9388. if (used_ports & port_mask)
  9389. ret = false;
  9390. used_ports |= port_mask;
  9391. break;
  9392. case INTEL_OUTPUT_DP_MST:
  9393. used_mst_ports |=
  9394. 1 << encoder->port;
  9395. break;
  9396. default:
  9397. break;
  9398. }
  9399. }
  9400. drm_connector_list_iter_end(&conn_iter);
  9401. /* can't mix MST and SST/HDMI on the same port */
  9402. if (used_ports & used_mst_ports)
  9403. return false;
  9404. return ret;
  9405. }
  9406. static void
  9407. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9408. {
  9409. struct drm_i915_private *dev_priv =
  9410. to_i915(crtc_state->base.crtc->dev);
  9411. struct intel_crtc_scaler_state scaler_state;
  9412. struct intel_dpll_hw_state dpll_hw_state;
  9413. struct intel_shared_dpll *shared_dpll;
  9414. struct intel_crtc_wm_state wm_state;
  9415. bool force_thru, ips_force_disable;
  9416. /* FIXME: before the switch to atomic started, a new pipe_config was
  9417. * kzalloc'd. Code that depends on any field being zero should be
  9418. * fixed, so that the crtc_state can be safely duplicated. For now,
  9419. * only fields that are know to not cause problems are preserved. */
  9420. scaler_state = crtc_state->scaler_state;
  9421. shared_dpll = crtc_state->shared_dpll;
  9422. dpll_hw_state = crtc_state->dpll_hw_state;
  9423. force_thru = crtc_state->pch_pfit.force_thru;
  9424. ips_force_disable = crtc_state->ips_force_disable;
  9425. if (IS_G4X(dev_priv) ||
  9426. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9427. wm_state = crtc_state->wm;
  9428. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9429. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9430. memset(&crtc_state->base + 1, 0,
  9431. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9432. crtc_state->scaler_state = scaler_state;
  9433. crtc_state->shared_dpll = shared_dpll;
  9434. crtc_state->dpll_hw_state = dpll_hw_state;
  9435. crtc_state->pch_pfit.force_thru = force_thru;
  9436. crtc_state->ips_force_disable = ips_force_disable;
  9437. if (IS_G4X(dev_priv) ||
  9438. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9439. crtc_state->wm = wm_state;
  9440. }
  9441. static int
  9442. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9443. struct intel_crtc_state *pipe_config)
  9444. {
  9445. struct drm_atomic_state *state = pipe_config->base.state;
  9446. struct intel_encoder *encoder;
  9447. struct drm_connector *connector;
  9448. struct drm_connector_state *connector_state;
  9449. int base_bpp, ret = -EINVAL;
  9450. int i;
  9451. bool retry = true;
  9452. clear_intel_crtc_state(pipe_config);
  9453. pipe_config->cpu_transcoder =
  9454. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9455. /*
  9456. * Sanitize sync polarity flags based on requested ones. If neither
  9457. * positive or negative polarity is requested, treat this as meaning
  9458. * negative polarity.
  9459. */
  9460. if (!(pipe_config->base.adjusted_mode.flags &
  9461. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9462. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9463. if (!(pipe_config->base.adjusted_mode.flags &
  9464. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9465. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9466. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9467. pipe_config);
  9468. if (base_bpp < 0)
  9469. goto fail;
  9470. /*
  9471. * Determine the real pipe dimensions. Note that stereo modes can
  9472. * increase the actual pipe size due to the frame doubling and
  9473. * insertion of additional space for blanks between the frame. This
  9474. * is stored in the crtc timings. We use the requested mode to do this
  9475. * computation to clearly distinguish it from the adjusted mode, which
  9476. * can be changed by the connectors in the below retry loop.
  9477. */
  9478. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9479. &pipe_config->pipe_src_w,
  9480. &pipe_config->pipe_src_h);
  9481. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9482. if (connector_state->crtc != crtc)
  9483. continue;
  9484. encoder = to_intel_encoder(connector_state->best_encoder);
  9485. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9486. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9487. goto fail;
  9488. }
  9489. /*
  9490. * Determine output_types before calling the .compute_config()
  9491. * hooks so that the hooks can use this information safely.
  9492. */
  9493. if (encoder->compute_output_type)
  9494. pipe_config->output_types |=
  9495. BIT(encoder->compute_output_type(encoder, pipe_config,
  9496. connector_state));
  9497. else
  9498. pipe_config->output_types |= BIT(encoder->type);
  9499. }
  9500. encoder_retry:
  9501. /* Ensure the port clock defaults are reset when retrying. */
  9502. pipe_config->port_clock = 0;
  9503. pipe_config->pixel_multiplier = 1;
  9504. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9505. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9506. CRTC_STEREO_DOUBLE);
  9507. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9508. * adjust it according to limitations or connector properties, and also
  9509. * a chance to reject the mode entirely.
  9510. */
  9511. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9512. if (connector_state->crtc != crtc)
  9513. continue;
  9514. encoder = to_intel_encoder(connector_state->best_encoder);
  9515. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9516. DRM_DEBUG_KMS("Encoder config failure\n");
  9517. goto fail;
  9518. }
  9519. }
  9520. /* Set default port clock if not overwritten by the encoder. Needs to be
  9521. * done afterwards in case the encoder adjusts the mode. */
  9522. if (!pipe_config->port_clock)
  9523. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9524. * pipe_config->pixel_multiplier;
  9525. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9526. if (ret < 0) {
  9527. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9528. goto fail;
  9529. }
  9530. if (ret == RETRY) {
  9531. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9532. ret = -EINVAL;
  9533. goto fail;
  9534. }
  9535. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9536. retry = false;
  9537. goto encoder_retry;
  9538. }
  9539. /* Dithering seems to not pass-through bits correctly when it should, so
  9540. * only enable it on 6bpc panels and when its not a compliance
  9541. * test requesting 6bpc video pattern.
  9542. */
  9543. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9544. !pipe_config->dither_force_disable;
  9545. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9546. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9547. fail:
  9548. return ret;
  9549. }
  9550. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9551. {
  9552. int diff;
  9553. if (clock1 == clock2)
  9554. return true;
  9555. if (!clock1 || !clock2)
  9556. return false;
  9557. diff = abs(clock1 - clock2);
  9558. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9559. return true;
  9560. return false;
  9561. }
  9562. static bool
  9563. intel_compare_m_n(unsigned int m, unsigned int n,
  9564. unsigned int m2, unsigned int n2,
  9565. bool exact)
  9566. {
  9567. if (m == m2 && n == n2)
  9568. return true;
  9569. if (exact || !m || !n || !m2 || !n2)
  9570. return false;
  9571. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9572. if (n > n2) {
  9573. while (n > n2) {
  9574. m2 <<= 1;
  9575. n2 <<= 1;
  9576. }
  9577. } else if (n < n2) {
  9578. while (n < n2) {
  9579. m <<= 1;
  9580. n <<= 1;
  9581. }
  9582. }
  9583. if (n != n2)
  9584. return false;
  9585. return intel_fuzzy_clock_check(m, m2);
  9586. }
  9587. static bool
  9588. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9589. struct intel_link_m_n *m2_n2,
  9590. bool adjust)
  9591. {
  9592. if (m_n->tu == m2_n2->tu &&
  9593. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9594. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9595. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9596. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9597. if (adjust)
  9598. *m2_n2 = *m_n;
  9599. return true;
  9600. }
  9601. return false;
  9602. }
  9603. static void __printf(3, 4)
  9604. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9605. {
  9606. struct va_format vaf;
  9607. va_list args;
  9608. va_start(args, format);
  9609. vaf.fmt = format;
  9610. vaf.va = &args;
  9611. if (adjust)
  9612. drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
  9613. else
  9614. drm_err("mismatch in %s %pV", name, &vaf);
  9615. va_end(args);
  9616. }
  9617. static bool
  9618. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9619. struct intel_crtc_state *current_config,
  9620. struct intel_crtc_state *pipe_config,
  9621. bool adjust)
  9622. {
  9623. bool ret = true;
  9624. bool fixup_inherited = adjust &&
  9625. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9626. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9627. #define PIPE_CONF_CHECK_X(name) do { \
  9628. if (current_config->name != pipe_config->name) { \
  9629. pipe_config_err(adjust, __stringify(name), \
  9630. "(expected 0x%08x, found 0x%08x)\n", \
  9631. current_config->name, \
  9632. pipe_config->name); \
  9633. ret = false; \
  9634. } \
  9635. } while (0)
  9636. #define PIPE_CONF_CHECK_I(name) do { \
  9637. if (current_config->name != pipe_config->name) { \
  9638. pipe_config_err(adjust, __stringify(name), \
  9639. "(expected %i, found %i)\n", \
  9640. current_config->name, \
  9641. pipe_config->name); \
  9642. ret = false; \
  9643. } \
  9644. } while (0)
  9645. #define PIPE_CONF_CHECK_BOOL(name) do { \
  9646. if (current_config->name != pipe_config->name) { \
  9647. pipe_config_err(adjust, __stringify(name), \
  9648. "(expected %s, found %s)\n", \
  9649. yesno(current_config->name), \
  9650. yesno(pipe_config->name)); \
  9651. ret = false; \
  9652. } \
  9653. } while (0)
  9654. /*
  9655. * Checks state where we only read out the enabling, but not the entire
  9656. * state itself (like full infoframes or ELD for audio). These states
  9657. * require a full modeset on bootup to fix up.
  9658. */
  9659. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
  9660. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9661. PIPE_CONF_CHECK_BOOL(name); \
  9662. } else { \
  9663. pipe_config_err(adjust, __stringify(name), \
  9664. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9665. yesno(current_config->name), \
  9666. yesno(pipe_config->name)); \
  9667. ret = false; \
  9668. } \
  9669. } while (0)
  9670. #define PIPE_CONF_CHECK_P(name) do { \
  9671. if (current_config->name != pipe_config->name) { \
  9672. pipe_config_err(adjust, __stringify(name), \
  9673. "(expected %p, found %p)\n", \
  9674. current_config->name, \
  9675. pipe_config->name); \
  9676. ret = false; \
  9677. } \
  9678. } while (0)
  9679. #define PIPE_CONF_CHECK_M_N(name) do { \
  9680. if (!intel_compare_link_m_n(&current_config->name, \
  9681. &pipe_config->name,\
  9682. adjust)) { \
  9683. pipe_config_err(adjust, __stringify(name), \
  9684. "(expected tu %i gmch %i/%i link %i/%i, " \
  9685. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9686. current_config->name.tu, \
  9687. current_config->name.gmch_m, \
  9688. current_config->name.gmch_n, \
  9689. current_config->name.link_m, \
  9690. current_config->name.link_n, \
  9691. pipe_config->name.tu, \
  9692. pipe_config->name.gmch_m, \
  9693. pipe_config->name.gmch_n, \
  9694. pipe_config->name.link_m, \
  9695. pipe_config->name.link_n); \
  9696. ret = false; \
  9697. } \
  9698. } while (0)
  9699. /* This is required for BDW+ where there is only one set of registers for
  9700. * switching between high and low RR.
  9701. * This macro can be used whenever a comparison has to be made between one
  9702. * hw state and multiple sw state variables.
  9703. */
  9704. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
  9705. if (!intel_compare_link_m_n(&current_config->name, \
  9706. &pipe_config->name, adjust) && \
  9707. !intel_compare_link_m_n(&current_config->alt_name, \
  9708. &pipe_config->name, adjust)) { \
  9709. pipe_config_err(adjust, __stringify(name), \
  9710. "(expected tu %i gmch %i/%i link %i/%i, " \
  9711. "or tu %i gmch %i/%i link %i/%i, " \
  9712. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9713. current_config->name.tu, \
  9714. current_config->name.gmch_m, \
  9715. current_config->name.gmch_n, \
  9716. current_config->name.link_m, \
  9717. current_config->name.link_n, \
  9718. current_config->alt_name.tu, \
  9719. current_config->alt_name.gmch_m, \
  9720. current_config->alt_name.gmch_n, \
  9721. current_config->alt_name.link_m, \
  9722. current_config->alt_name.link_n, \
  9723. pipe_config->name.tu, \
  9724. pipe_config->name.gmch_m, \
  9725. pipe_config->name.gmch_n, \
  9726. pipe_config->name.link_m, \
  9727. pipe_config->name.link_n); \
  9728. ret = false; \
  9729. } \
  9730. } while (0)
  9731. #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
  9732. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9733. pipe_config_err(adjust, __stringify(name), \
  9734. "(%x) (expected %i, found %i)\n", \
  9735. (mask), \
  9736. current_config->name & (mask), \
  9737. pipe_config->name & (mask)); \
  9738. ret = false; \
  9739. } \
  9740. } while (0)
  9741. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
  9742. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9743. pipe_config_err(adjust, __stringify(name), \
  9744. "(expected %i, found %i)\n", \
  9745. current_config->name, \
  9746. pipe_config->name); \
  9747. ret = false; \
  9748. } \
  9749. } while (0)
  9750. #define PIPE_CONF_QUIRK(quirk) \
  9751. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9752. PIPE_CONF_CHECK_I(cpu_transcoder);
  9753. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9754. PIPE_CONF_CHECK_I(fdi_lanes);
  9755. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9756. PIPE_CONF_CHECK_I(lane_count);
  9757. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9758. if (INTEL_GEN(dev_priv) < 8) {
  9759. PIPE_CONF_CHECK_M_N(dp_m_n);
  9760. if (current_config->has_drrs)
  9761. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9762. } else
  9763. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9764. PIPE_CONF_CHECK_X(output_types);
  9765. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9766. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9767. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9768. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9769. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9770. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9771. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9772. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9773. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9774. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9775. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9776. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9777. PIPE_CONF_CHECK_I(pixel_multiplier);
  9778. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9779. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9780. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9781. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9782. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9783. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9784. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9785. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9786. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9787. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9788. DRM_MODE_FLAG_INTERLACE);
  9789. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9790. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9791. DRM_MODE_FLAG_PHSYNC);
  9792. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9793. DRM_MODE_FLAG_NHSYNC);
  9794. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9795. DRM_MODE_FLAG_PVSYNC);
  9796. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9797. DRM_MODE_FLAG_NVSYNC);
  9798. }
  9799. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9800. /* pfit ratios are autocomputed by the hw on gen4+ */
  9801. if (INTEL_GEN(dev_priv) < 4)
  9802. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9803. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9804. if (!adjust) {
  9805. PIPE_CONF_CHECK_I(pipe_src_w);
  9806. PIPE_CONF_CHECK_I(pipe_src_h);
  9807. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9808. if (current_config->pch_pfit.enabled) {
  9809. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9810. PIPE_CONF_CHECK_X(pch_pfit.size);
  9811. }
  9812. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9813. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9814. }
  9815. PIPE_CONF_CHECK_BOOL(double_wide);
  9816. PIPE_CONF_CHECK_P(shared_dpll);
  9817. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9818. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9819. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9820. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9821. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9822. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9823. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9824. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9825. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9826. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9827. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9828. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9829. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9830. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9831. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9832. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9833. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9834. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9835. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9836. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9837. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9838. PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
  9839. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
  9840. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
  9841. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
  9842. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
  9843. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
  9844. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
  9845. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
  9846. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
  9847. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
  9848. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9849. PIPE_CONF_CHECK_X(dsi_pll.div);
  9850. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9851. PIPE_CONF_CHECK_I(pipe_bpp);
  9852. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9853. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9854. PIPE_CONF_CHECK_I(min_voltage_level);
  9855. #undef PIPE_CONF_CHECK_X
  9856. #undef PIPE_CONF_CHECK_I
  9857. #undef PIPE_CONF_CHECK_BOOL
  9858. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9859. #undef PIPE_CONF_CHECK_P
  9860. #undef PIPE_CONF_CHECK_FLAGS
  9861. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9862. #undef PIPE_CONF_QUIRK
  9863. return ret;
  9864. }
  9865. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9866. const struct intel_crtc_state *pipe_config)
  9867. {
  9868. if (pipe_config->has_pch_encoder) {
  9869. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9870. &pipe_config->fdi_m_n);
  9871. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9872. /*
  9873. * FDI already provided one idea for the dotclock.
  9874. * Yell if the encoder disagrees.
  9875. */
  9876. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9877. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9878. fdi_dotclock, dotclock);
  9879. }
  9880. }
  9881. static void verify_wm_state(struct drm_crtc *crtc,
  9882. struct drm_crtc_state *new_state)
  9883. {
  9884. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9885. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9886. struct skl_pipe_wm hw_wm, *sw_wm;
  9887. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9888. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9889. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9890. const enum pipe pipe = intel_crtc->pipe;
  9891. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9892. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9893. return;
  9894. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9895. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9896. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9897. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9898. if (INTEL_GEN(dev_priv) >= 11)
  9899. if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
  9900. DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
  9901. sw_ddb->enabled_slices,
  9902. hw_ddb.enabled_slices);
  9903. /* planes */
  9904. for_each_universal_plane(dev_priv, pipe, plane) {
  9905. hw_plane_wm = &hw_wm.planes[plane];
  9906. sw_plane_wm = &sw_wm->planes[plane];
  9907. /* Watermarks */
  9908. for (level = 0; level <= max_level; level++) {
  9909. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9910. &sw_plane_wm->wm[level]))
  9911. continue;
  9912. 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",
  9913. pipe_name(pipe), plane + 1, level,
  9914. sw_plane_wm->wm[level].plane_en,
  9915. sw_plane_wm->wm[level].plane_res_b,
  9916. sw_plane_wm->wm[level].plane_res_l,
  9917. hw_plane_wm->wm[level].plane_en,
  9918. hw_plane_wm->wm[level].plane_res_b,
  9919. hw_plane_wm->wm[level].plane_res_l);
  9920. }
  9921. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9922. &sw_plane_wm->trans_wm)) {
  9923. 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",
  9924. pipe_name(pipe), plane + 1,
  9925. sw_plane_wm->trans_wm.plane_en,
  9926. sw_plane_wm->trans_wm.plane_res_b,
  9927. sw_plane_wm->trans_wm.plane_res_l,
  9928. hw_plane_wm->trans_wm.plane_en,
  9929. hw_plane_wm->trans_wm.plane_res_b,
  9930. hw_plane_wm->trans_wm.plane_res_l);
  9931. }
  9932. /* DDB */
  9933. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9934. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9935. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9936. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9937. pipe_name(pipe), plane + 1,
  9938. sw_ddb_entry->start, sw_ddb_entry->end,
  9939. hw_ddb_entry->start, hw_ddb_entry->end);
  9940. }
  9941. }
  9942. /*
  9943. * cursor
  9944. * If the cursor plane isn't active, we may not have updated it's ddb
  9945. * allocation. In that case since the ddb allocation will be updated
  9946. * once the plane becomes visible, we can skip this check
  9947. */
  9948. if (1) {
  9949. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9950. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9951. /* Watermarks */
  9952. for (level = 0; level <= max_level; level++) {
  9953. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9954. &sw_plane_wm->wm[level]))
  9955. continue;
  9956. 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",
  9957. pipe_name(pipe), level,
  9958. sw_plane_wm->wm[level].plane_en,
  9959. sw_plane_wm->wm[level].plane_res_b,
  9960. sw_plane_wm->wm[level].plane_res_l,
  9961. hw_plane_wm->wm[level].plane_en,
  9962. hw_plane_wm->wm[level].plane_res_b,
  9963. hw_plane_wm->wm[level].plane_res_l);
  9964. }
  9965. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9966. &sw_plane_wm->trans_wm)) {
  9967. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9968. pipe_name(pipe),
  9969. sw_plane_wm->trans_wm.plane_en,
  9970. sw_plane_wm->trans_wm.plane_res_b,
  9971. sw_plane_wm->trans_wm.plane_res_l,
  9972. hw_plane_wm->trans_wm.plane_en,
  9973. hw_plane_wm->trans_wm.plane_res_b,
  9974. hw_plane_wm->trans_wm.plane_res_l);
  9975. }
  9976. /* DDB */
  9977. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9978. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9979. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9980. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9981. pipe_name(pipe),
  9982. sw_ddb_entry->start, sw_ddb_entry->end,
  9983. hw_ddb_entry->start, hw_ddb_entry->end);
  9984. }
  9985. }
  9986. }
  9987. static void
  9988. verify_connector_state(struct drm_device *dev,
  9989. struct drm_atomic_state *state,
  9990. struct drm_crtc *crtc)
  9991. {
  9992. struct drm_connector *connector;
  9993. struct drm_connector_state *new_conn_state;
  9994. int i;
  9995. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9996. struct drm_encoder *encoder = connector->encoder;
  9997. struct drm_crtc_state *crtc_state = NULL;
  9998. if (new_conn_state->crtc != crtc)
  9999. continue;
  10000. if (crtc)
  10001. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  10002. intel_connector_verify_state(crtc_state, new_conn_state);
  10003. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  10004. "connector's atomic encoder doesn't match legacy encoder\n");
  10005. }
  10006. }
  10007. static void
  10008. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  10009. {
  10010. struct intel_encoder *encoder;
  10011. struct drm_connector *connector;
  10012. struct drm_connector_state *old_conn_state, *new_conn_state;
  10013. int i;
  10014. for_each_intel_encoder(dev, encoder) {
  10015. bool enabled = false, found = false;
  10016. enum pipe pipe;
  10017. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10018. encoder->base.base.id,
  10019. encoder->base.name);
  10020. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  10021. new_conn_state, i) {
  10022. if (old_conn_state->best_encoder == &encoder->base)
  10023. found = true;
  10024. if (new_conn_state->best_encoder != &encoder->base)
  10025. continue;
  10026. found = enabled = true;
  10027. I915_STATE_WARN(new_conn_state->crtc !=
  10028. encoder->base.crtc,
  10029. "connector's crtc doesn't match encoder crtc\n");
  10030. }
  10031. if (!found)
  10032. continue;
  10033. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10034. "encoder's enabled state mismatch "
  10035. "(expected %i, found %i)\n",
  10036. !!encoder->base.crtc, enabled);
  10037. if (!encoder->base.crtc) {
  10038. bool active;
  10039. active = encoder->get_hw_state(encoder, &pipe);
  10040. I915_STATE_WARN(active,
  10041. "encoder detached but still enabled on pipe %c.\n",
  10042. pipe_name(pipe));
  10043. }
  10044. }
  10045. }
  10046. static void
  10047. verify_crtc_state(struct drm_crtc *crtc,
  10048. struct drm_crtc_state *old_crtc_state,
  10049. struct drm_crtc_state *new_crtc_state)
  10050. {
  10051. struct drm_device *dev = crtc->dev;
  10052. struct drm_i915_private *dev_priv = to_i915(dev);
  10053. struct intel_encoder *encoder;
  10054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10055. struct intel_crtc_state *pipe_config, *sw_config;
  10056. struct drm_atomic_state *old_state;
  10057. bool active;
  10058. old_state = old_crtc_state->state;
  10059. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10060. pipe_config = to_intel_crtc_state(old_crtc_state);
  10061. memset(pipe_config, 0, sizeof(*pipe_config));
  10062. pipe_config->base.crtc = crtc;
  10063. pipe_config->base.state = old_state;
  10064. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10065. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10066. /* we keep both pipes enabled on 830 */
  10067. if (IS_I830(dev_priv))
  10068. active = new_crtc_state->active;
  10069. I915_STATE_WARN(new_crtc_state->active != active,
  10070. "crtc active state doesn't match with hw state "
  10071. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10072. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10073. "transitional active state does not match atomic hw state "
  10074. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10075. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10076. enum pipe pipe;
  10077. active = encoder->get_hw_state(encoder, &pipe);
  10078. I915_STATE_WARN(active != new_crtc_state->active,
  10079. "[ENCODER:%i] active %i with crtc active %i\n",
  10080. encoder->base.base.id, active, new_crtc_state->active);
  10081. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10082. "Encoder connected to wrong pipe %c\n",
  10083. pipe_name(pipe));
  10084. if (active)
  10085. encoder->get_config(encoder, pipe_config);
  10086. }
  10087. intel_crtc_compute_pixel_rate(pipe_config);
  10088. if (!new_crtc_state->active)
  10089. return;
  10090. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10091. sw_config = to_intel_crtc_state(new_crtc_state);
  10092. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10093. pipe_config, false)) {
  10094. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10095. intel_dump_pipe_config(intel_crtc, pipe_config,
  10096. "[hw state]");
  10097. intel_dump_pipe_config(intel_crtc, sw_config,
  10098. "[sw state]");
  10099. }
  10100. }
  10101. static void
  10102. intel_verify_planes(struct intel_atomic_state *state)
  10103. {
  10104. struct intel_plane *plane;
  10105. const struct intel_plane_state *plane_state;
  10106. int i;
  10107. for_each_new_intel_plane_in_state(state, plane,
  10108. plane_state, i)
  10109. assert_plane(plane, plane_state->base.visible);
  10110. }
  10111. static void
  10112. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10113. struct intel_shared_dpll *pll,
  10114. struct drm_crtc *crtc,
  10115. struct drm_crtc_state *new_state)
  10116. {
  10117. struct intel_dpll_hw_state dpll_hw_state;
  10118. unsigned int crtc_mask;
  10119. bool active;
  10120. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10121. DRM_DEBUG_KMS("%s\n", pll->info->name);
  10122. active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10123. if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
  10124. I915_STATE_WARN(!pll->on && pll->active_mask,
  10125. "pll in active use but not on in sw tracking\n");
  10126. I915_STATE_WARN(pll->on && !pll->active_mask,
  10127. "pll is on but not used by any active crtc\n");
  10128. I915_STATE_WARN(pll->on != active,
  10129. "pll on state mismatch (expected %i, found %i)\n",
  10130. pll->on, active);
  10131. }
  10132. if (!crtc) {
  10133. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10134. "more active pll users than references: %x vs %x\n",
  10135. pll->active_mask, pll->state.crtc_mask);
  10136. return;
  10137. }
  10138. crtc_mask = drm_crtc_mask(crtc);
  10139. if (new_state->active)
  10140. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10141. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10142. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10143. else
  10144. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10145. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10146. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10147. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10148. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10149. crtc_mask, pll->state.crtc_mask);
  10150. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10151. &dpll_hw_state,
  10152. sizeof(dpll_hw_state)),
  10153. "pll hw state mismatch\n");
  10154. }
  10155. static void
  10156. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10157. struct drm_crtc_state *old_crtc_state,
  10158. struct drm_crtc_state *new_crtc_state)
  10159. {
  10160. struct drm_i915_private *dev_priv = to_i915(dev);
  10161. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10162. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10163. if (new_state->shared_dpll)
  10164. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10165. if (old_state->shared_dpll &&
  10166. old_state->shared_dpll != new_state->shared_dpll) {
  10167. unsigned int crtc_mask = drm_crtc_mask(crtc);
  10168. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10169. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10170. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10171. pipe_name(drm_crtc_index(crtc)));
  10172. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10173. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10174. pipe_name(drm_crtc_index(crtc)));
  10175. }
  10176. }
  10177. static void
  10178. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10179. struct drm_atomic_state *state,
  10180. struct drm_crtc_state *old_state,
  10181. struct drm_crtc_state *new_state)
  10182. {
  10183. if (!needs_modeset(new_state) &&
  10184. !to_intel_crtc_state(new_state)->update_pipe)
  10185. return;
  10186. verify_wm_state(crtc, new_state);
  10187. verify_connector_state(crtc->dev, state, crtc);
  10188. verify_crtc_state(crtc, old_state, new_state);
  10189. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10190. }
  10191. static void
  10192. verify_disabled_dpll_state(struct drm_device *dev)
  10193. {
  10194. struct drm_i915_private *dev_priv = to_i915(dev);
  10195. int i;
  10196. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10197. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10198. }
  10199. static void
  10200. intel_modeset_verify_disabled(struct drm_device *dev,
  10201. struct drm_atomic_state *state)
  10202. {
  10203. verify_encoder_state(dev, state);
  10204. verify_connector_state(dev, state, NULL);
  10205. verify_disabled_dpll_state(dev);
  10206. }
  10207. static void update_scanline_offset(struct intel_crtc *crtc)
  10208. {
  10209. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10210. /*
  10211. * The scanline counter increments at the leading edge of hsync.
  10212. *
  10213. * On most platforms it starts counting from vtotal-1 on the
  10214. * first active line. That means the scanline counter value is
  10215. * always one less than what we would expect. Ie. just after
  10216. * start of vblank, which also occurs at start of hsync (on the
  10217. * last active line), the scanline counter will read vblank_start-1.
  10218. *
  10219. * On gen2 the scanline counter starts counting from 1 instead
  10220. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10221. * to keep the value positive), instead of adding one.
  10222. *
  10223. * On HSW+ the behaviour of the scanline counter depends on the output
  10224. * type. For DP ports it behaves like most other platforms, but on HDMI
  10225. * there's an extra 1 line difference. So we need to add two instead of
  10226. * one to the value.
  10227. *
  10228. * On VLV/CHV DSI the scanline counter would appear to increment
  10229. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  10230. * that means we can't tell whether we're in vblank or not while
  10231. * we're on that particular line. We must still set scanline_offset
  10232. * to 1 so that the vblank timestamps come out correct when we query
  10233. * the scanline counter from within the vblank interrupt handler.
  10234. * However if queried just before the start of vblank we'll get an
  10235. * answer that's slightly in the future.
  10236. */
  10237. if (IS_GEN2(dev_priv)) {
  10238. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10239. int vtotal;
  10240. vtotal = adjusted_mode->crtc_vtotal;
  10241. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10242. vtotal /= 2;
  10243. crtc->scanline_offset = vtotal - 1;
  10244. } else if (HAS_DDI(dev_priv) &&
  10245. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10246. crtc->scanline_offset = 2;
  10247. } else
  10248. crtc->scanline_offset = 1;
  10249. }
  10250. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10251. {
  10252. struct drm_device *dev = state->dev;
  10253. struct drm_i915_private *dev_priv = to_i915(dev);
  10254. struct drm_crtc *crtc;
  10255. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10256. int i;
  10257. if (!dev_priv->display.crtc_compute_clock)
  10258. return;
  10259. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10261. struct intel_shared_dpll *old_dpll =
  10262. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10263. if (!needs_modeset(new_crtc_state))
  10264. continue;
  10265. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10266. if (!old_dpll)
  10267. continue;
  10268. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10269. }
  10270. }
  10271. /*
  10272. * This implements the workaround described in the "notes" section of the mode
  10273. * set sequence documentation. When going from no pipes or single pipe to
  10274. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10275. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10276. */
  10277. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10278. {
  10279. struct drm_crtc_state *crtc_state;
  10280. struct intel_crtc *intel_crtc;
  10281. struct drm_crtc *crtc;
  10282. struct intel_crtc_state *first_crtc_state = NULL;
  10283. struct intel_crtc_state *other_crtc_state = NULL;
  10284. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10285. int i;
  10286. /* look at all crtc's that are going to be enabled in during modeset */
  10287. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10288. intel_crtc = to_intel_crtc(crtc);
  10289. if (!crtc_state->active || !needs_modeset(crtc_state))
  10290. continue;
  10291. if (first_crtc_state) {
  10292. other_crtc_state = to_intel_crtc_state(crtc_state);
  10293. break;
  10294. } else {
  10295. first_crtc_state = to_intel_crtc_state(crtc_state);
  10296. first_pipe = intel_crtc->pipe;
  10297. }
  10298. }
  10299. /* No workaround needed? */
  10300. if (!first_crtc_state)
  10301. return 0;
  10302. /* w/a possibly needed, check how many crtc's are already enabled. */
  10303. for_each_intel_crtc(state->dev, intel_crtc) {
  10304. struct intel_crtc_state *pipe_config;
  10305. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10306. if (IS_ERR(pipe_config))
  10307. return PTR_ERR(pipe_config);
  10308. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10309. if (!pipe_config->base.active ||
  10310. needs_modeset(&pipe_config->base))
  10311. continue;
  10312. /* 2 or more enabled crtcs means no need for w/a */
  10313. if (enabled_pipe != INVALID_PIPE)
  10314. return 0;
  10315. enabled_pipe = intel_crtc->pipe;
  10316. }
  10317. if (enabled_pipe != INVALID_PIPE)
  10318. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10319. else if (other_crtc_state)
  10320. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10321. return 0;
  10322. }
  10323. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10324. {
  10325. struct drm_crtc *crtc;
  10326. /* Add all pipes to the state */
  10327. for_each_crtc(state->dev, crtc) {
  10328. struct drm_crtc_state *crtc_state;
  10329. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10330. if (IS_ERR(crtc_state))
  10331. return PTR_ERR(crtc_state);
  10332. }
  10333. return 0;
  10334. }
  10335. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10336. {
  10337. struct drm_crtc *crtc;
  10338. /*
  10339. * Add all pipes to the state, and force
  10340. * a modeset on all the active ones.
  10341. */
  10342. for_each_crtc(state->dev, crtc) {
  10343. struct drm_crtc_state *crtc_state;
  10344. int ret;
  10345. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10346. if (IS_ERR(crtc_state))
  10347. return PTR_ERR(crtc_state);
  10348. if (!crtc_state->active || needs_modeset(crtc_state))
  10349. continue;
  10350. crtc_state->mode_changed = true;
  10351. ret = drm_atomic_add_affected_connectors(state, crtc);
  10352. if (ret)
  10353. return ret;
  10354. ret = drm_atomic_add_affected_planes(state, crtc);
  10355. if (ret)
  10356. return ret;
  10357. }
  10358. return 0;
  10359. }
  10360. static int intel_modeset_checks(struct drm_atomic_state *state)
  10361. {
  10362. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10363. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10364. struct drm_crtc *crtc;
  10365. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10366. int ret = 0, i;
  10367. if (!check_digital_port_conflicts(state)) {
  10368. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10369. return -EINVAL;
  10370. }
  10371. intel_state->modeset = true;
  10372. intel_state->active_crtcs = dev_priv->active_crtcs;
  10373. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10374. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10375. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10376. if (new_crtc_state->active)
  10377. intel_state->active_crtcs |= 1 << i;
  10378. else
  10379. intel_state->active_crtcs &= ~(1 << i);
  10380. if (old_crtc_state->active != new_crtc_state->active)
  10381. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10382. }
  10383. /*
  10384. * See if the config requires any additional preparation, e.g.
  10385. * to adjust global state with pipes off. We need to do this
  10386. * here so we can get the modeset_pipe updated config for the new
  10387. * mode set on this crtc. For other crtcs we need to use the
  10388. * adjusted_mode bits in the crtc directly.
  10389. */
  10390. if (dev_priv->display.modeset_calc_cdclk) {
  10391. ret = dev_priv->display.modeset_calc_cdclk(state);
  10392. if (ret < 0)
  10393. return ret;
  10394. /*
  10395. * Writes to dev_priv->cdclk.logical must protected by
  10396. * holding all the crtc locks, even if we don't end up
  10397. * touching the hardware
  10398. */
  10399. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10400. &intel_state->cdclk.logical)) {
  10401. ret = intel_lock_all_pipes(state);
  10402. if (ret < 0)
  10403. return ret;
  10404. }
  10405. /* All pipes must be switched off while we change the cdclk. */
  10406. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10407. &intel_state->cdclk.actual)) {
  10408. ret = intel_modeset_all_pipes(state);
  10409. if (ret < 0)
  10410. return ret;
  10411. }
  10412. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10413. intel_state->cdclk.logical.cdclk,
  10414. intel_state->cdclk.actual.cdclk);
  10415. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10416. intel_state->cdclk.logical.voltage_level,
  10417. intel_state->cdclk.actual.voltage_level);
  10418. } else {
  10419. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10420. }
  10421. intel_modeset_clear_plls(state);
  10422. if (IS_HASWELL(dev_priv))
  10423. return haswell_mode_set_planes_workaround(state);
  10424. return 0;
  10425. }
  10426. /*
  10427. * Handle calculation of various watermark data at the end of the atomic check
  10428. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10429. * handlers to ensure that all derived state has been updated.
  10430. */
  10431. static int calc_watermark_data(struct drm_atomic_state *state)
  10432. {
  10433. struct drm_device *dev = state->dev;
  10434. struct drm_i915_private *dev_priv = to_i915(dev);
  10435. /* Is there platform-specific watermark information to calculate? */
  10436. if (dev_priv->display.compute_global_watermarks)
  10437. return dev_priv->display.compute_global_watermarks(state);
  10438. return 0;
  10439. }
  10440. /**
  10441. * intel_atomic_check - validate state object
  10442. * @dev: drm device
  10443. * @state: state to validate
  10444. */
  10445. static int intel_atomic_check(struct drm_device *dev,
  10446. struct drm_atomic_state *state)
  10447. {
  10448. struct drm_i915_private *dev_priv = to_i915(dev);
  10449. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10450. struct drm_crtc *crtc;
  10451. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10452. int ret, i;
  10453. bool any_ms = false;
  10454. /* Catch I915_MODE_FLAG_INHERITED */
  10455. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  10456. crtc_state, i) {
  10457. if (crtc_state->mode.private_flags !=
  10458. old_crtc_state->mode.private_flags)
  10459. crtc_state->mode_changed = true;
  10460. }
  10461. ret = drm_atomic_helper_check_modeset(dev, state);
  10462. if (ret)
  10463. return ret;
  10464. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10465. struct intel_crtc_state *pipe_config =
  10466. to_intel_crtc_state(crtc_state);
  10467. if (!needs_modeset(crtc_state))
  10468. continue;
  10469. if (!crtc_state->enable) {
  10470. any_ms = true;
  10471. continue;
  10472. }
  10473. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10474. if (ret) {
  10475. intel_dump_pipe_config(to_intel_crtc(crtc),
  10476. pipe_config, "[failed]");
  10477. return ret;
  10478. }
  10479. if (i915_modparams.fastboot &&
  10480. intel_pipe_config_compare(dev_priv,
  10481. to_intel_crtc_state(old_crtc_state),
  10482. pipe_config, true)) {
  10483. crtc_state->mode_changed = false;
  10484. pipe_config->update_pipe = true;
  10485. }
  10486. if (needs_modeset(crtc_state))
  10487. any_ms = true;
  10488. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10489. needs_modeset(crtc_state) ?
  10490. "[modeset]" : "[fastset]");
  10491. }
  10492. if (any_ms) {
  10493. ret = intel_modeset_checks(state);
  10494. if (ret)
  10495. return ret;
  10496. } else {
  10497. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10498. }
  10499. ret = drm_atomic_helper_check_planes(dev, state);
  10500. if (ret)
  10501. return ret;
  10502. intel_fbc_choose_crtc(dev_priv, intel_state);
  10503. return calc_watermark_data(state);
  10504. }
  10505. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10506. struct drm_atomic_state *state)
  10507. {
  10508. return drm_atomic_helper_prepare_planes(dev, state);
  10509. }
  10510. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10511. {
  10512. struct drm_device *dev = crtc->base.dev;
  10513. if (!dev->max_vblank_count)
  10514. return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
  10515. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10516. }
  10517. static void intel_update_crtc(struct drm_crtc *crtc,
  10518. struct drm_atomic_state *state,
  10519. struct drm_crtc_state *old_crtc_state,
  10520. struct drm_crtc_state *new_crtc_state)
  10521. {
  10522. struct drm_device *dev = crtc->dev;
  10523. struct drm_i915_private *dev_priv = to_i915(dev);
  10524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10525. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10526. bool modeset = needs_modeset(new_crtc_state);
  10527. struct intel_plane_state *new_plane_state =
  10528. intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
  10529. to_intel_plane(crtc->primary));
  10530. if (modeset) {
  10531. update_scanline_offset(intel_crtc);
  10532. dev_priv->display.crtc_enable(pipe_config, state);
  10533. /* vblanks work again, re-enable pipe CRC. */
  10534. intel_crtc_enable_pipe_crc(intel_crtc);
  10535. } else {
  10536. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10537. pipe_config);
  10538. }
  10539. if (new_plane_state)
  10540. intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
  10541. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10542. }
  10543. static void intel_update_crtcs(struct drm_atomic_state *state)
  10544. {
  10545. struct drm_crtc *crtc;
  10546. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10547. int i;
  10548. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10549. if (!new_crtc_state->active)
  10550. continue;
  10551. intel_update_crtc(crtc, state, old_crtc_state,
  10552. new_crtc_state);
  10553. }
  10554. }
  10555. static void skl_update_crtcs(struct drm_atomic_state *state)
  10556. {
  10557. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10558. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10559. struct drm_crtc *crtc;
  10560. struct intel_crtc *intel_crtc;
  10561. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10562. struct intel_crtc_state *cstate;
  10563. unsigned int updated = 0;
  10564. bool progress;
  10565. enum pipe pipe;
  10566. int i;
  10567. u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
  10568. u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
  10569. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10570. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10571. /* ignore allocations for crtc's that have been turned off. */
  10572. if (new_crtc_state->active)
  10573. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10574. /* If 2nd DBuf slice required, enable it here */
  10575. if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
  10576. icl_dbuf_slices_update(dev_priv, required_slices);
  10577. /*
  10578. * Whenever the number of active pipes changes, we need to make sure we
  10579. * update the pipes in the right order so that their ddb allocations
  10580. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10581. * cause pipe underruns and other bad stuff.
  10582. */
  10583. do {
  10584. progress = false;
  10585. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10586. bool vbl_wait = false;
  10587. unsigned int cmask = drm_crtc_mask(crtc);
  10588. intel_crtc = to_intel_crtc(crtc);
  10589. cstate = to_intel_crtc_state(new_crtc_state);
  10590. pipe = intel_crtc->pipe;
  10591. if (updated & cmask || !cstate->base.active)
  10592. continue;
  10593. if (skl_ddb_allocation_overlaps(dev_priv,
  10594. entries,
  10595. &cstate->wm.skl.ddb,
  10596. i))
  10597. continue;
  10598. updated |= cmask;
  10599. entries[i] = &cstate->wm.skl.ddb;
  10600. /*
  10601. * If this is an already active pipe, it's DDB changed,
  10602. * and this isn't the last pipe that needs updating
  10603. * then we need to wait for a vblank to pass for the
  10604. * new ddb allocation to take effect.
  10605. */
  10606. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10607. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10608. !new_crtc_state->active_changed &&
  10609. intel_state->wm_results.dirty_pipes != updated)
  10610. vbl_wait = true;
  10611. intel_update_crtc(crtc, state, old_crtc_state,
  10612. new_crtc_state);
  10613. if (vbl_wait)
  10614. intel_wait_for_vblank(dev_priv, pipe);
  10615. progress = true;
  10616. }
  10617. } while (progress);
  10618. /* If 2nd DBuf slice is no more required disable it */
  10619. if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
  10620. icl_dbuf_slices_update(dev_priv, required_slices);
  10621. }
  10622. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10623. {
  10624. struct intel_atomic_state *state, *next;
  10625. struct llist_node *freed;
  10626. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10627. llist_for_each_entry_safe(state, next, freed, freed)
  10628. drm_atomic_state_put(&state->base);
  10629. }
  10630. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10631. {
  10632. struct drm_i915_private *dev_priv =
  10633. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10634. intel_atomic_helper_free_state(dev_priv);
  10635. }
  10636. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10637. {
  10638. struct wait_queue_entry wait_fence, wait_reset;
  10639. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10640. init_wait_entry(&wait_fence, 0);
  10641. init_wait_entry(&wait_reset, 0);
  10642. for (;;) {
  10643. prepare_to_wait(&intel_state->commit_ready.wait,
  10644. &wait_fence, TASK_UNINTERRUPTIBLE);
  10645. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10646. &wait_reset, TASK_UNINTERRUPTIBLE);
  10647. if (i915_sw_fence_done(&intel_state->commit_ready)
  10648. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10649. break;
  10650. schedule();
  10651. }
  10652. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10653. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10654. }
  10655. static void intel_atomic_cleanup_work(struct work_struct *work)
  10656. {
  10657. struct drm_atomic_state *state =
  10658. container_of(work, struct drm_atomic_state, commit_work);
  10659. struct drm_i915_private *i915 = to_i915(state->dev);
  10660. drm_atomic_helper_cleanup_planes(&i915->drm, state);
  10661. drm_atomic_helper_commit_cleanup_done(state);
  10662. drm_atomic_state_put(state);
  10663. intel_atomic_helper_free_state(i915);
  10664. }
  10665. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10666. {
  10667. struct drm_device *dev = state->dev;
  10668. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10669. struct drm_i915_private *dev_priv = to_i915(dev);
  10670. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10671. struct drm_crtc *crtc;
  10672. struct intel_crtc_state *intel_cstate;
  10673. u64 put_domains[I915_MAX_PIPES] = {};
  10674. int i;
  10675. intel_atomic_commit_fence_wait(intel_state);
  10676. drm_atomic_helper_wait_for_dependencies(state);
  10677. if (intel_state->modeset)
  10678. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10679. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10680. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10681. if (needs_modeset(new_crtc_state) ||
  10682. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10683. put_domains[to_intel_crtc(crtc)->pipe] =
  10684. modeset_get_crtc_power_domains(crtc,
  10685. to_intel_crtc_state(new_crtc_state));
  10686. }
  10687. if (!needs_modeset(new_crtc_state))
  10688. continue;
  10689. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10690. to_intel_crtc_state(new_crtc_state));
  10691. if (old_crtc_state->active) {
  10692. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10693. /*
  10694. * We need to disable pipe CRC before disabling the pipe,
  10695. * or we race against vblank off.
  10696. */
  10697. intel_crtc_disable_pipe_crc(intel_crtc);
  10698. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10699. intel_crtc->active = false;
  10700. intel_fbc_disable(intel_crtc);
  10701. intel_disable_shared_dpll(intel_crtc);
  10702. /*
  10703. * Underruns don't always raise
  10704. * interrupts, so check manually.
  10705. */
  10706. intel_check_cpu_fifo_underruns(dev_priv);
  10707. intel_check_pch_fifo_underruns(dev_priv);
  10708. /* FIXME unify this for all platforms */
  10709. if (!new_crtc_state->active &&
  10710. !HAS_GMCH_DISPLAY(dev_priv) &&
  10711. dev_priv->display.initial_watermarks)
  10712. dev_priv->display.initial_watermarks(intel_state,
  10713. to_intel_crtc_state(new_crtc_state));
  10714. }
  10715. }
  10716. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10717. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10718. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10719. if (intel_state->modeset) {
  10720. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10721. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10722. /*
  10723. * SKL workaround: bspec recommends we disable the SAGV when we
  10724. * have more then one pipe enabled
  10725. */
  10726. if (!intel_can_enable_sagv(state))
  10727. intel_disable_sagv(dev_priv);
  10728. intel_modeset_verify_disabled(dev, state);
  10729. }
  10730. /* Complete the events for pipes that have now been disabled */
  10731. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10732. bool modeset = needs_modeset(new_crtc_state);
  10733. /* Complete events for now disable pipes here. */
  10734. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10735. spin_lock_irq(&dev->event_lock);
  10736. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10737. spin_unlock_irq(&dev->event_lock);
  10738. new_crtc_state->event = NULL;
  10739. }
  10740. }
  10741. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10742. dev_priv->display.update_crtcs(state);
  10743. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10744. * already, but still need the state for the delayed optimization. To
  10745. * fix this:
  10746. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10747. * - schedule that vblank worker _before_ calling hw_done
  10748. * - at the start of commit_tail, cancel it _synchrously
  10749. * - switch over to the vblank wait helper in the core after that since
  10750. * we don't need out special handling any more.
  10751. */
  10752. drm_atomic_helper_wait_for_flip_done(dev, state);
  10753. /*
  10754. * Now that the vblank has passed, we can go ahead and program the
  10755. * optimal watermarks on platforms that need two-step watermark
  10756. * programming.
  10757. *
  10758. * TODO: Move this (and other cleanup) to an async worker eventually.
  10759. */
  10760. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10761. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10762. if (dev_priv->display.optimize_watermarks)
  10763. dev_priv->display.optimize_watermarks(intel_state,
  10764. intel_cstate);
  10765. }
  10766. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10767. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10768. if (put_domains[i])
  10769. modeset_put_power_domains(dev_priv, put_domains[i]);
  10770. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10771. }
  10772. if (intel_state->modeset)
  10773. intel_verify_planes(intel_state);
  10774. if (intel_state->modeset && intel_can_enable_sagv(state))
  10775. intel_enable_sagv(dev_priv);
  10776. drm_atomic_helper_commit_hw_done(state);
  10777. if (intel_state->modeset) {
  10778. /* As one of the primary mmio accessors, KMS has a high
  10779. * likelihood of triggering bugs in unclaimed access. After we
  10780. * finish modesetting, see if an error has been flagged, and if
  10781. * so enable debugging for the next modeset - and hope we catch
  10782. * the culprit.
  10783. */
  10784. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10785. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10786. }
  10787. /*
  10788. * Defer the cleanup of the old state to a separate worker to not
  10789. * impede the current task (userspace for blocking modesets) that
  10790. * are executed inline. For out-of-line asynchronous modesets/flips,
  10791. * deferring to a new worker seems overkill, but we would place a
  10792. * schedule point (cond_resched()) here anyway to keep latencies
  10793. * down.
  10794. */
  10795. INIT_WORK(&state->commit_work, intel_atomic_cleanup_work);
  10796. queue_work(system_highpri_wq, &state->commit_work);
  10797. }
  10798. static void intel_atomic_commit_work(struct work_struct *work)
  10799. {
  10800. struct drm_atomic_state *state =
  10801. container_of(work, struct drm_atomic_state, commit_work);
  10802. intel_atomic_commit_tail(state);
  10803. }
  10804. static int __i915_sw_fence_call
  10805. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10806. enum i915_sw_fence_notify notify)
  10807. {
  10808. struct intel_atomic_state *state =
  10809. container_of(fence, struct intel_atomic_state, commit_ready);
  10810. switch (notify) {
  10811. case FENCE_COMPLETE:
  10812. /* we do blocking waits in the worker, nothing to do here */
  10813. break;
  10814. case FENCE_FREE:
  10815. {
  10816. struct intel_atomic_helper *helper =
  10817. &to_i915(state->base.dev)->atomic_helper;
  10818. if (llist_add(&state->freed, &helper->free_list))
  10819. schedule_work(&helper->free_work);
  10820. break;
  10821. }
  10822. }
  10823. return NOTIFY_DONE;
  10824. }
  10825. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10826. {
  10827. struct drm_plane_state *old_plane_state, *new_plane_state;
  10828. struct drm_plane *plane;
  10829. int i;
  10830. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10831. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10832. intel_fb_obj(new_plane_state->fb),
  10833. to_intel_plane(plane)->frontbuffer_bit);
  10834. }
  10835. /**
  10836. * intel_atomic_commit - commit validated state object
  10837. * @dev: DRM device
  10838. * @state: the top-level driver state object
  10839. * @nonblock: nonblocking commit
  10840. *
  10841. * This function commits a top-level state object that has been validated
  10842. * with drm_atomic_helper_check().
  10843. *
  10844. * RETURNS
  10845. * Zero for success or -errno.
  10846. */
  10847. static int intel_atomic_commit(struct drm_device *dev,
  10848. struct drm_atomic_state *state,
  10849. bool nonblock)
  10850. {
  10851. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10852. struct drm_i915_private *dev_priv = to_i915(dev);
  10853. int ret = 0;
  10854. drm_atomic_state_get(state);
  10855. i915_sw_fence_init(&intel_state->commit_ready,
  10856. intel_atomic_commit_ready);
  10857. /*
  10858. * The intel_legacy_cursor_update() fast path takes care
  10859. * of avoiding the vblank waits for simple cursor
  10860. * movement and flips. For cursor on/off and size changes,
  10861. * we want to perform the vblank waits so that watermark
  10862. * updates happen during the correct frames. Gen9+ have
  10863. * double buffered watermarks and so shouldn't need this.
  10864. *
  10865. * Unset state->legacy_cursor_update before the call to
  10866. * drm_atomic_helper_setup_commit() because otherwise
  10867. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10868. * we get FIFO underruns because we didn't wait
  10869. * for vblank.
  10870. *
  10871. * FIXME doing watermarks and fb cleanup from a vblank worker
  10872. * (assuming we had any) would solve these problems.
  10873. */
  10874. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10875. struct intel_crtc_state *new_crtc_state;
  10876. struct intel_crtc *crtc;
  10877. int i;
  10878. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10879. if (new_crtc_state->wm.need_postvbl_update ||
  10880. new_crtc_state->update_wm_post)
  10881. state->legacy_cursor_update = false;
  10882. }
  10883. ret = intel_atomic_prepare_commit(dev, state);
  10884. if (ret) {
  10885. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10886. i915_sw_fence_commit(&intel_state->commit_ready);
  10887. return ret;
  10888. }
  10889. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10890. if (!ret)
  10891. ret = drm_atomic_helper_swap_state(state, true);
  10892. if (ret) {
  10893. i915_sw_fence_commit(&intel_state->commit_ready);
  10894. drm_atomic_helper_cleanup_planes(dev, state);
  10895. return ret;
  10896. }
  10897. dev_priv->wm.distrust_bios_wm = false;
  10898. intel_shared_dpll_swap_state(state);
  10899. intel_atomic_track_fbs(state);
  10900. if (intel_state->modeset) {
  10901. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10902. sizeof(intel_state->min_cdclk));
  10903. memcpy(dev_priv->min_voltage_level,
  10904. intel_state->min_voltage_level,
  10905. sizeof(intel_state->min_voltage_level));
  10906. dev_priv->active_crtcs = intel_state->active_crtcs;
  10907. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10908. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10909. }
  10910. drm_atomic_state_get(state);
  10911. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10912. i915_sw_fence_commit(&intel_state->commit_ready);
  10913. if (nonblock && intel_state->modeset) {
  10914. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10915. } else if (nonblock) {
  10916. queue_work(system_unbound_wq, &state->commit_work);
  10917. } else {
  10918. if (intel_state->modeset)
  10919. flush_workqueue(dev_priv->modeset_wq);
  10920. intel_atomic_commit_tail(state);
  10921. }
  10922. return 0;
  10923. }
  10924. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10925. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10926. .set_config = drm_atomic_helper_set_config,
  10927. .destroy = intel_crtc_destroy,
  10928. .page_flip = drm_atomic_helper_page_flip,
  10929. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10930. .atomic_destroy_state = intel_crtc_destroy_state,
  10931. .set_crc_source = intel_crtc_set_crc_source,
  10932. .verify_crc_source = intel_crtc_verify_crc_source,
  10933. .get_crc_sources = intel_crtc_get_crc_sources,
  10934. };
  10935. struct wait_rps_boost {
  10936. struct wait_queue_entry wait;
  10937. struct drm_crtc *crtc;
  10938. struct i915_request *request;
  10939. };
  10940. static int do_rps_boost(struct wait_queue_entry *_wait,
  10941. unsigned mode, int sync, void *key)
  10942. {
  10943. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10944. struct i915_request *rq = wait->request;
  10945. /*
  10946. * If we missed the vblank, but the request is already running it
  10947. * is reasonable to assume that it will complete before the next
  10948. * vblank without our intervention, so leave RPS alone.
  10949. */
  10950. if (!i915_request_started(rq))
  10951. gen6_rps_boost(rq, NULL);
  10952. i915_request_put(rq);
  10953. drm_crtc_vblank_put(wait->crtc);
  10954. list_del(&wait->wait.entry);
  10955. kfree(wait);
  10956. return 1;
  10957. }
  10958. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10959. struct dma_fence *fence)
  10960. {
  10961. struct wait_rps_boost *wait;
  10962. if (!dma_fence_is_i915(fence))
  10963. return;
  10964. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10965. return;
  10966. if (drm_crtc_vblank_get(crtc))
  10967. return;
  10968. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10969. if (!wait) {
  10970. drm_crtc_vblank_put(crtc);
  10971. return;
  10972. }
  10973. wait->request = to_request(dma_fence_get(fence));
  10974. wait->crtc = crtc;
  10975. wait->wait.func = do_rps_boost;
  10976. wait->wait.flags = 0;
  10977. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10978. }
  10979. static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
  10980. {
  10981. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  10982. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10983. struct drm_framebuffer *fb = plane_state->base.fb;
  10984. struct i915_vma *vma;
  10985. if (plane->id == PLANE_CURSOR &&
  10986. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10987. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10988. const int align = intel_cursor_alignment(dev_priv);
  10989. int err;
  10990. err = i915_gem_object_attach_phys(obj, align);
  10991. if (err)
  10992. return err;
  10993. }
  10994. vma = intel_pin_and_fence_fb_obj(fb,
  10995. &plane_state->view,
  10996. intel_plane_uses_fence(plane_state),
  10997. &plane_state->flags);
  10998. if (IS_ERR(vma))
  10999. return PTR_ERR(vma);
  11000. plane_state->vma = vma;
  11001. return 0;
  11002. }
  11003. static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
  11004. {
  11005. struct i915_vma *vma;
  11006. vma = fetch_and_zero(&old_plane_state->vma);
  11007. if (vma)
  11008. intel_unpin_fb_vma(vma, old_plane_state->flags);
  11009. }
  11010. static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
  11011. {
  11012. struct i915_sched_attr attr = {
  11013. .priority = I915_PRIORITY_DISPLAY,
  11014. };
  11015. i915_gem_object_wait_priority(obj, 0, &attr);
  11016. }
  11017. /**
  11018. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11019. * @plane: drm plane to prepare for
  11020. * @new_state: the plane state being prepared
  11021. *
  11022. * Prepares a framebuffer for usage on a display plane. Generally this
  11023. * involves pinning the underlying object and updating the frontbuffer tracking
  11024. * bits. Some older platforms need special physical address handling for
  11025. * cursor planes.
  11026. *
  11027. * Must be called with struct_mutex held.
  11028. *
  11029. * Returns 0 on success, negative error code on failure.
  11030. */
  11031. int
  11032. intel_prepare_plane_fb(struct drm_plane *plane,
  11033. struct drm_plane_state *new_state)
  11034. {
  11035. struct intel_atomic_state *intel_state =
  11036. to_intel_atomic_state(new_state->state);
  11037. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11038. struct drm_framebuffer *fb = new_state->fb;
  11039. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11040. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11041. int ret;
  11042. if (old_obj) {
  11043. struct drm_crtc_state *crtc_state =
  11044. drm_atomic_get_new_crtc_state(new_state->state,
  11045. plane->state->crtc);
  11046. /* Big Hammer, we also need to ensure that any pending
  11047. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11048. * current scanout is retired before unpinning the old
  11049. * framebuffer. Note that we rely on userspace rendering
  11050. * into the buffer attached to the pipe they are waiting
  11051. * on. If not, userspace generates a GPU hang with IPEHR
  11052. * point to the MI_WAIT_FOR_EVENT.
  11053. *
  11054. * This should only fail upon a hung GPU, in which case we
  11055. * can safely continue.
  11056. */
  11057. if (needs_modeset(crtc_state)) {
  11058. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11059. old_obj->resv, NULL,
  11060. false, 0,
  11061. GFP_KERNEL);
  11062. if (ret < 0)
  11063. return ret;
  11064. }
  11065. }
  11066. if (new_state->fence) { /* explicit fencing */
  11067. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11068. new_state->fence,
  11069. I915_FENCE_TIMEOUT,
  11070. GFP_KERNEL);
  11071. if (ret < 0)
  11072. return ret;
  11073. }
  11074. if (!obj)
  11075. return 0;
  11076. ret = i915_gem_object_pin_pages(obj);
  11077. if (ret)
  11078. return ret;
  11079. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11080. if (ret) {
  11081. i915_gem_object_unpin_pages(obj);
  11082. return ret;
  11083. }
  11084. ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
  11085. fb_obj_bump_render_priority(obj);
  11086. mutex_unlock(&dev_priv->drm.struct_mutex);
  11087. i915_gem_object_unpin_pages(obj);
  11088. if (ret)
  11089. return ret;
  11090. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11091. if (!new_state->fence) { /* implicit fencing */
  11092. struct dma_fence *fence;
  11093. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11094. obj->resv, NULL,
  11095. false, I915_FENCE_TIMEOUT,
  11096. GFP_KERNEL);
  11097. if (ret < 0)
  11098. return ret;
  11099. fence = reservation_object_get_excl_rcu(obj->resv);
  11100. if (fence) {
  11101. add_rps_boost_after_vblank(new_state->crtc, fence);
  11102. dma_fence_put(fence);
  11103. }
  11104. } else {
  11105. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  11106. }
  11107. /*
  11108. * We declare pageflips to be interactive and so merit a small bias
  11109. * towards upclocking to deliver the frame on time. By only changing
  11110. * the RPS thresholds to sample more regularly and aim for higher
  11111. * clocks we can hopefully deliver low power workloads (like kodi)
  11112. * that are not quite steady state without resorting to forcing
  11113. * maximum clocks following a vblank miss (see do_rps_boost()).
  11114. */
  11115. if (!intel_state->rps_interactive) {
  11116. intel_rps_mark_interactive(dev_priv, true);
  11117. intel_state->rps_interactive = true;
  11118. }
  11119. return 0;
  11120. }
  11121. /**
  11122. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11123. * @plane: drm plane to clean up for
  11124. * @old_state: the state from the previous modeset
  11125. *
  11126. * Cleans up a framebuffer that has just been removed from a plane.
  11127. *
  11128. * Must be called with struct_mutex held.
  11129. */
  11130. void
  11131. intel_cleanup_plane_fb(struct drm_plane *plane,
  11132. struct drm_plane_state *old_state)
  11133. {
  11134. struct intel_atomic_state *intel_state =
  11135. to_intel_atomic_state(old_state->state);
  11136. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11137. if (intel_state->rps_interactive) {
  11138. intel_rps_mark_interactive(dev_priv, false);
  11139. intel_state->rps_interactive = false;
  11140. }
  11141. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11142. mutex_lock(&dev_priv->drm.struct_mutex);
  11143. intel_plane_unpin_fb(to_intel_plane_state(old_state));
  11144. mutex_unlock(&dev_priv->drm.struct_mutex);
  11145. }
  11146. int
  11147. skl_max_scale(const struct intel_crtc_state *crtc_state,
  11148. u32 pixel_format)
  11149. {
  11150. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  11151. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11152. int max_scale, mult;
  11153. int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
  11154. if (!crtc_state->base.enable)
  11155. return DRM_PLANE_HELPER_NO_SCALING;
  11156. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11157. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11158. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  11159. max_dotclk *= 2;
  11160. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11161. return DRM_PLANE_HELPER_NO_SCALING;
  11162. /*
  11163. * skl max scale is lower of:
  11164. * close to 3 but not 3, -1 is for that purpose
  11165. * or
  11166. * cdclk/crtc_clock
  11167. */
  11168. mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
  11169. tmpclk1 = (1 << 16) * mult - 1;
  11170. tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
  11171. max_scale = min(tmpclk1, tmpclk2);
  11172. return max_scale;
  11173. }
  11174. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11175. struct drm_crtc_state *old_crtc_state)
  11176. {
  11177. struct drm_device *dev = crtc->dev;
  11178. struct drm_i915_private *dev_priv = to_i915(dev);
  11179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11180. struct intel_crtc_state *old_intel_cstate =
  11181. to_intel_crtc_state(old_crtc_state);
  11182. struct intel_atomic_state *old_intel_state =
  11183. to_intel_atomic_state(old_crtc_state->state);
  11184. struct intel_crtc_state *intel_cstate =
  11185. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  11186. bool modeset = needs_modeset(&intel_cstate->base);
  11187. if (!modeset &&
  11188. (intel_cstate->base.color_mgmt_changed ||
  11189. intel_cstate->update_pipe)) {
  11190. intel_color_set_csc(&intel_cstate->base);
  11191. intel_color_load_luts(&intel_cstate->base);
  11192. }
  11193. /* Perform vblank evasion around commit operation */
  11194. intel_pipe_update_start(intel_cstate);
  11195. if (modeset)
  11196. goto out;
  11197. if (intel_cstate->update_pipe)
  11198. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  11199. else if (INTEL_GEN(dev_priv) >= 9)
  11200. skl_detach_scalers(intel_crtc);
  11201. out:
  11202. if (dev_priv->display.atomic_update_watermarks)
  11203. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11204. intel_cstate);
  11205. }
  11206. void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
  11207. struct intel_crtc_state *crtc_state)
  11208. {
  11209. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11210. if (!IS_GEN2(dev_priv))
  11211. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
  11212. if (crtc_state->has_pch_encoder) {
  11213. enum pipe pch_transcoder =
  11214. intel_crtc_pch_transcoder(crtc);
  11215. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  11216. }
  11217. }
  11218. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11219. struct drm_crtc_state *old_crtc_state)
  11220. {
  11221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11222. struct intel_atomic_state *old_intel_state =
  11223. to_intel_atomic_state(old_crtc_state->state);
  11224. struct intel_crtc_state *new_crtc_state =
  11225. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  11226. intel_pipe_update_end(new_crtc_state);
  11227. if (new_crtc_state->update_pipe &&
  11228. !needs_modeset(&new_crtc_state->base) &&
  11229. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
  11230. intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
  11231. }
  11232. /**
  11233. * intel_plane_destroy - destroy a plane
  11234. * @plane: plane to destroy
  11235. *
  11236. * Common destruction function for all types of planes (primary, cursor,
  11237. * sprite).
  11238. */
  11239. void intel_plane_destroy(struct drm_plane *plane)
  11240. {
  11241. drm_plane_cleanup(plane);
  11242. kfree(to_intel_plane(plane));
  11243. }
  11244. static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
  11245. u32 format, u64 modifier)
  11246. {
  11247. switch (modifier) {
  11248. case DRM_FORMAT_MOD_LINEAR:
  11249. case I915_FORMAT_MOD_X_TILED:
  11250. break;
  11251. default:
  11252. return false;
  11253. }
  11254. switch (format) {
  11255. case DRM_FORMAT_C8:
  11256. case DRM_FORMAT_RGB565:
  11257. case DRM_FORMAT_XRGB1555:
  11258. case DRM_FORMAT_XRGB8888:
  11259. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11260. modifier == I915_FORMAT_MOD_X_TILED;
  11261. default:
  11262. return false;
  11263. }
  11264. }
  11265. static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
  11266. u32 format, u64 modifier)
  11267. {
  11268. switch (modifier) {
  11269. case DRM_FORMAT_MOD_LINEAR:
  11270. case I915_FORMAT_MOD_X_TILED:
  11271. break;
  11272. default:
  11273. return false;
  11274. }
  11275. switch (format) {
  11276. case DRM_FORMAT_C8:
  11277. case DRM_FORMAT_RGB565:
  11278. case DRM_FORMAT_XRGB8888:
  11279. case DRM_FORMAT_XBGR8888:
  11280. case DRM_FORMAT_XRGB2101010:
  11281. case DRM_FORMAT_XBGR2101010:
  11282. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11283. modifier == I915_FORMAT_MOD_X_TILED;
  11284. default:
  11285. return false;
  11286. }
  11287. }
  11288. static bool skl_plane_format_mod_supported(struct drm_plane *_plane,
  11289. u32 format, u64 modifier)
  11290. {
  11291. struct intel_plane *plane = to_intel_plane(_plane);
  11292. switch (modifier) {
  11293. case DRM_FORMAT_MOD_LINEAR:
  11294. case I915_FORMAT_MOD_X_TILED:
  11295. case I915_FORMAT_MOD_Y_TILED:
  11296. case I915_FORMAT_MOD_Yf_TILED:
  11297. break;
  11298. case I915_FORMAT_MOD_Y_TILED_CCS:
  11299. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11300. if (!plane->has_ccs)
  11301. return false;
  11302. break;
  11303. default:
  11304. return false;
  11305. }
  11306. switch (format) {
  11307. case DRM_FORMAT_XRGB8888:
  11308. case DRM_FORMAT_XBGR8888:
  11309. case DRM_FORMAT_ARGB8888:
  11310. case DRM_FORMAT_ABGR8888:
  11311. if (is_ccs_modifier(modifier))
  11312. return true;
  11313. /* fall through */
  11314. case DRM_FORMAT_RGB565:
  11315. case DRM_FORMAT_XRGB2101010:
  11316. case DRM_FORMAT_XBGR2101010:
  11317. case DRM_FORMAT_YUYV:
  11318. case DRM_FORMAT_YVYU:
  11319. case DRM_FORMAT_UYVY:
  11320. case DRM_FORMAT_VYUY:
  11321. case DRM_FORMAT_NV12:
  11322. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  11323. return true;
  11324. /* fall through */
  11325. case DRM_FORMAT_C8:
  11326. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  11327. modifier == I915_FORMAT_MOD_X_TILED ||
  11328. modifier == I915_FORMAT_MOD_Y_TILED)
  11329. return true;
  11330. /* fall through */
  11331. default:
  11332. return false;
  11333. }
  11334. }
  11335. static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
  11336. u32 format, u64 modifier)
  11337. {
  11338. return modifier == DRM_FORMAT_MOD_LINEAR &&
  11339. format == DRM_FORMAT_ARGB8888;
  11340. }
  11341. static struct drm_plane_funcs skl_plane_funcs = {
  11342. .update_plane = drm_atomic_helper_update_plane,
  11343. .disable_plane = drm_atomic_helper_disable_plane,
  11344. .destroy = intel_plane_destroy,
  11345. .atomic_get_property = intel_plane_atomic_get_property,
  11346. .atomic_set_property = intel_plane_atomic_set_property,
  11347. .atomic_duplicate_state = intel_plane_duplicate_state,
  11348. .atomic_destroy_state = intel_plane_destroy_state,
  11349. .format_mod_supported = skl_plane_format_mod_supported,
  11350. };
  11351. static struct drm_plane_funcs i965_plane_funcs = {
  11352. .update_plane = drm_atomic_helper_update_plane,
  11353. .disable_plane = drm_atomic_helper_disable_plane,
  11354. .destroy = intel_plane_destroy,
  11355. .atomic_get_property = intel_plane_atomic_get_property,
  11356. .atomic_set_property = intel_plane_atomic_set_property,
  11357. .atomic_duplicate_state = intel_plane_duplicate_state,
  11358. .atomic_destroy_state = intel_plane_destroy_state,
  11359. .format_mod_supported = i965_plane_format_mod_supported,
  11360. };
  11361. static struct drm_plane_funcs i8xx_plane_funcs = {
  11362. .update_plane = drm_atomic_helper_update_plane,
  11363. .disable_plane = drm_atomic_helper_disable_plane,
  11364. .destroy = intel_plane_destroy,
  11365. .atomic_get_property = intel_plane_atomic_get_property,
  11366. .atomic_set_property = intel_plane_atomic_set_property,
  11367. .atomic_duplicate_state = intel_plane_duplicate_state,
  11368. .atomic_destroy_state = intel_plane_destroy_state,
  11369. .format_mod_supported = i8xx_plane_format_mod_supported,
  11370. };
  11371. static int
  11372. intel_legacy_cursor_update(struct drm_plane *plane,
  11373. struct drm_crtc *crtc,
  11374. struct drm_framebuffer *fb,
  11375. int crtc_x, int crtc_y,
  11376. unsigned int crtc_w, unsigned int crtc_h,
  11377. uint32_t src_x, uint32_t src_y,
  11378. uint32_t src_w, uint32_t src_h,
  11379. struct drm_modeset_acquire_ctx *ctx)
  11380. {
  11381. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11382. int ret;
  11383. struct drm_plane_state *old_plane_state, *new_plane_state;
  11384. struct intel_plane *intel_plane = to_intel_plane(plane);
  11385. struct drm_framebuffer *old_fb;
  11386. struct drm_crtc_state *crtc_state = crtc->state;
  11387. /*
  11388. * When crtc is inactive or there is a modeset pending,
  11389. * wait for it to complete in the slowpath
  11390. */
  11391. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11392. to_intel_crtc_state(crtc_state)->update_pipe)
  11393. goto slow;
  11394. old_plane_state = plane->state;
  11395. /*
  11396. * Don't do an async update if there is an outstanding commit modifying
  11397. * the plane. This prevents our async update's changes from getting
  11398. * overridden by a previous synchronous update's state.
  11399. */
  11400. if (old_plane_state->commit &&
  11401. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  11402. goto slow;
  11403. /*
  11404. * If any parameters change that may affect watermarks,
  11405. * take the slowpath. Only changing fb or position should be
  11406. * in the fastpath.
  11407. */
  11408. if (old_plane_state->crtc != crtc ||
  11409. old_plane_state->src_w != src_w ||
  11410. old_plane_state->src_h != src_h ||
  11411. old_plane_state->crtc_w != crtc_w ||
  11412. old_plane_state->crtc_h != crtc_h ||
  11413. !old_plane_state->fb != !fb)
  11414. goto slow;
  11415. new_plane_state = intel_plane_duplicate_state(plane);
  11416. if (!new_plane_state)
  11417. return -ENOMEM;
  11418. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11419. new_plane_state->src_x = src_x;
  11420. new_plane_state->src_y = src_y;
  11421. new_plane_state->src_w = src_w;
  11422. new_plane_state->src_h = src_h;
  11423. new_plane_state->crtc_x = crtc_x;
  11424. new_plane_state->crtc_y = crtc_y;
  11425. new_plane_state->crtc_w = crtc_w;
  11426. new_plane_state->crtc_h = crtc_h;
  11427. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11428. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  11429. to_intel_plane_state(plane->state),
  11430. to_intel_plane_state(new_plane_state));
  11431. if (ret)
  11432. goto out_free;
  11433. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11434. if (ret)
  11435. goto out_free;
  11436. ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
  11437. if (ret)
  11438. goto out_unlock;
  11439. intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
  11440. old_fb = old_plane_state->fb;
  11441. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11442. intel_plane->frontbuffer_bit);
  11443. /* Swap plane state */
  11444. plane->state = new_plane_state;
  11445. if (plane->state->visible) {
  11446. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11447. intel_plane->update_plane(intel_plane,
  11448. to_intel_crtc_state(crtc->state),
  11449. to_intel_plane_state(plane->state));
  11450. } else {
  11451. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11452. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11453. }
  11454. intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
  11455. out_unlock:
  11456. mutex_unlock(&dev_priv->drm.struct_mutex);
  11457. out_free:
  11458. if (ret)
  11459. intel_plane_destroy_state(plane, new_plane_state);
  11460. else
  11461. intel_plane_destroy_state(plane, old_plane_state);
  11462. return ret;
  11463. slow:
  11464. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11465. crtc_x, crtc_y, crtc_w, crtc_h,
  11466. src_x, src_y, src_w, src_h, ctx);
  11467. }
  11468. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11469. .update_plane = intel_legacy_cursor_update,
  11470. .disable_plane = drm_atomic_helper_disable_plane,
  11471. .destroy = intel_plane_destroy,
  11472. .atomic_get_property = intel_plane_atomic_get_property,
  11473. .atomic_set_property = intel_plane_atomic_set_property,
  11474. .atomic_duplicate_state = intel_plane_duplicate_state,
  11475. .atomic_destroy_state = intel_plane_destroy_state,
  11476. .format_mod_supported = intel_cursor_format_mod_supported,
  11477. };
  11478. static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
  11479. enum i9xx_plane_id i9xx_plane)
  11480. {
  11481. if (!HAS_FBC(dev_priv))
  11482. return false;
  11483. if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  11484. return i9xx_plane == PLANE_A; /* tied to pipe A */
  11485. else if (IS_IVYBRIDGE(dev_priv))
  11486. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
  11487. i9xx_plane == PLANE_C;
  11488. else if (INTEL_GEN(dev_priv) >= 4)
  11489. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
  11490. else
  11491. return i9xx_plane == PLANE_A;
  11492. }
  11493. static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
  11494. enum pipe pipe, enum plane_id plane_id)
  11495. {
  11496. if (!HAS_FBC(dev_priv))
  11497. return false;
  11498. return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
  11499. }
  11500. bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
  11501. enum pipe pipe, enum plane_id plane_id)
  11502. {
  11503. /*
  11504. * FIXME: ICL requires two hardware planes for scanning out NV12
  11505. * framebuffers. Do not advertize support until this is implemented.
  11506. */
  11507. if (INTEL_GEN(dev_priv) >= 11)
  11508. return false;
  11509. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11510. return false;
  11511. if (INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv) && pipe == PIPE_C)
  11512. return false;
  11513. if (plane_id != PLANE_PRIMARY && plane_id != PLANE_SPRITE0)
  11514. return false;
  11515. return true;
  11516. }
  11517. static struct intel_plane *
  11518. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11519. {
  11520. struct intel_plane *primary = NULL;
  11521. struct intel_plane_state *state = NULL;
  11522. const struct drm_plane_funcs *plane_funcs;
  11523. const uint32_t *intel_primary_formats;
  11524. unsigned int supported_rotations;
  11525. unsigned int num_formats;
  11526. const uint64_t *modifiers;
  11527. int ret;
  11528. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11529. if (!primary) {
  11530. ret = -ENOMEM;
  11531. goto fail;
  11532. }
  11533. state = intel_create_plane_state(&primary->base);
  11534. if (!state) {
  11535. ret = -ENOMEM;
  11536. goto fail;
  11537. }
  11538. primary->base.state = &state->base;
  11539. if (INTEL_GEN(dev_priv) >= 9)
  11540. state->scaler_id = -1;
  11541. primary->pipe = pipe;
  11542. /*
  11543. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11544. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11545. */
  11546. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11547. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11548. else
  11549. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11550. primary->id = PLANE_PRIMARY;
  11551. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11552. if (INTEL_GEN(dev_priv) >= 9)
  11553. primary->has_fbc = skl_plane_has_fbc(dev_priv,
  11554. primary->pipe,
  11555. primary->id);
  11556. else
  11557. primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
  11558. primary->i9xx_plane);
  11559. if (primary->has_fbc) {
  11560. struct intel_fbc *fbc = &dev_priv->fbc;
  11561. fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
  11562. }
  11563. if (INTEL_GEN(dev_priv) >= 9) {
  11564. primary->has_ccs = skl_plane_has_ccs(dev_priv, pipe,
  11565. PLANE_PRIMARY);
  11566. if (skl_plane_has_planar(dev_priv, pipe, PLANE_PRIMARY)) {
  11567. intel_primary_formats = skl_pri_planar_formats;
  11568. num_formats = ARRAY_SIZE(skl_pri_planar_formats);
  11569. } else {
  11570. intel_primary_formats = skl_primary_formats;
  11571. num_formats = ARRAY_SIZE(skl_primary_formats);
  11572. }
  11573. if (primary->has_ccs)
  11574. modifiers = skl_format_modifiers_ccs;
  11575. else
  11576. modifiers = skl_format_modifiers_noccs;
  11577. primary->max_stride = skl_plane_max_stride;
  11578. primary->update_plane = skl_update_plane;
  11579. primary->disable_plane = skl_disable_plane;
  11580. primary->get_hw_state = skl_plane_get_hw_state;
  11581. primary->check_plane = skl_plane_check;
  11582. plane_funcs = &skl_plane_funcs;
  11583. } else if (INTEL_GEN(dev_priv) >= 4) {
  11584. intel_primary_formats = i965_primary_formats;
  11585. num_formats = ARRAY_SIZE(i965_primary_formats);
  11586. modifiers = i9xx_format_modifiers;
  11587. primary->max_stride = i9xx_plane_max_stride;
  11588. primary->update_plane = i9xx_update_plane;
  11589. primary->disable_plane = i9xx_disable_plane;
  11590. primary->get_hw_state = i9xx_plane_get_hw_state;
  11591. primary->check_plane = i9xx_plane_check;
  11592. plane_funcs = &i965_plane_funcs;
  11593. } else {
  11594. intel_primary_formats = i8xx_primary_formats;
  11595. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11596. modifiers = i9xx_format_modifiers;
  11597. primary->max_stride = i9xx_plane_max_stride;
  11598. primary->update_plane = i9xx_update_plane;
  11599. primary->disable_plane = i9xx_disable_plane;
  11600. primary->get_hw_state = i9xx_plane_get_hw_state;
  11601. primary->check_plane = i9xx_plane_check;
  11602. plane_funcs = &i8xx_plane_funcs;
  11603. }
  11604. if (INTEL_GEN(dev_priv) >= 9)
  11605. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11606. 0, plane_funcs,
  11607. intel_primary_formats, num_formats,
  11608. modifiers,
  11609. DRM_PLANE_TYPE_PRIMARY,
  11610. "plane 1%c", pipe_name(pipe));
  11611. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11612. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11613. 0, plane_funcs,
  11614. intel_primary_formats, num_formats,
  11615. modifiers,
  11616. DRM_PLANE_TYPE_PRIMARY,
  11617. "primary %c", pipe_name(pipe));
  11618. else
  11619. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11620. 0, plane_funcs,
  11621. intel_primary_formats, num_formats,
  11622. modifiers,
  11623. DRM_PLANE_TYPE_PRIMARY,
  11624. "plane %c",
  11625. plane_name(primary->i9xx_plane));
  11626. if (ret)
  11627. goto fail;
  11628. if (INTEL_GEN(dev_priv) >= 10) {
  11629. supported_rotations =
  11630. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11631. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11632. DRM_MODE_REFLECT_X;
  11633. } else if (INTEL_GEN(dev_priv) >= 9) {
  11634. supported_rotations =
  11635. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11636. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11637. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11638. supported_rotations =
  11639. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11640. DRM_MODE_REFLECT_X;
  11641. } else if (INTEL_GEN(dev_priv) >= 4) {
  11642. supported_rotations =
  11643. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11644. } else {
  11645. supported_rotations = DRM_MODE_ROTATE_0;
  11646. }
  11647. if (INTEL_GEN(dev_priv) >= 4)
  11648. drm_plane_create_rotation_property(&primary->base,
  11649. DRM_MODE_ROTATE_0,
  11650. supported_rotations);
  11651. if (INTEL_GEN(dev_priv) >= 9)
  11652. drm_plane_create_color_properties(&primary->base,
  11653. BIT(DRM_COLOR_YCBCR_BT601) |
  11654. BIT(DRM_COLOR_YCBCR_BT709),
  11655. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  11656. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  11657. DRM_COLOR_YCBCR_BT709,
  11658. DRM_COLOR_YCBCR_LIMITED_RANGE);
  11659. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11660. return primary;
  11661. fail:
  11662. kfree(state);
  11663. kfree(primary);
  11664. return ERR_PTR(ret);
  11665. }
  11666. static struct intel_plane *
  11667. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11668. enum pipe pipe)
  11669. {
  11670. struct intel_plane *cursor = NULL;
  11671. struct intel_plane_state *state = NULL;
  11672. int ret;
  11673. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11674. if (!cursor) {
  11675. ret = -ENOMEM;
  11676. goto fail;
  11677. }
  11678. state = intel_create_plane_state(&cursor->base);
  11679. if (!state) {
  11680. ret = -ENOMEM;
  11681. goto fail;
  11682. }
  11683. cursor->base.state = &state->base;
  11684. cursor->pipe = pipe;
  11685. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11686. cursor->id = PLANE_CURSOR;
  11687. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11688. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11689. cursor->max_stride = i845_cursor_max_stride;
  11690. cursor->update_plane = i845_update_cursor;
  11691. cursor->disable_plane = i845_disable_cursor;
  11692. cursor->get_hw_state = i845_cursor_get_hw_state;
  11693. cursor->check_plane = i845_check_cursor;
  11694. } else {
  11695. cursor->max_stride = i9xx_cursor_max_stride;
  11696. cursor->update_plane = i9xx_update_cursor;
  11697. cursor->disable_plane = i9xx_disable_cursor;
  11698. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11699. cursor->check_plane = i9xx_check_cursor;
  11700. }
  11701. cursor->cursor.base = ~0;
  11702. cursor->cursor.cntl = ~0;
  11703. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11704. cursor->cursor.size = ~0;
  11705. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11706. 0, &intel_cursor_plane_funcs,
  11707. intel_cursor_formats,
  11708. ARRAY_SIZE(intel_cursor_formats),
  11709. cursor_format_modifiers,
  11710. DRM_PLANE_TYPE_CURSOR,
  11711. "cursor %c", pipe_name(pipe));
  11712. if (ret)
  11713. goto fail;
  11714. if (INTEL_GEN(dev_priv) >= 4)
  11715. drm_plane_create_rotation_property(&cursor->base,
  11716. DRM_MODE_ROTATE_0,
  11717. DRM_MODE_ROTATE_0 |
  11718. DRM_MODE_ROTATE_180);
  11719. if (INTEL_GEN(dev_priv) >= 9)
  11720. state->scaler_id = -1;
  11721. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11722. return cursor;
  11723. fail:
  11724. kfree(state);
  11725. kfree(cursor);
  11726. return ERR_PTR(ret);
  11727. }
  11728. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11729. struct intel_crtc_state *crtc_state)
  11730. {
  11731. struct intel_crtc_scaler_state *scaler_state =
  11732. &crtc_state->scaler_state;
  11733. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11734. int i;
  11735. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11736. if (!crtc->num_scalers)
  11737. return;
  11738. for (i = 0; i < crtc->num_scalers; i++) {
  11739. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11740. scaler->in_use = 0;
  11741. scaler->mode = PS_SCALER_MODE_DYN;
  11742. }
  11743. scaler_state->scaler_id = -1;
  11744. }
  11745. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11746. {
  11747. struct intel_crtc *intel_crtc;
  11748. struct intel_crtc_state *crtc_state = NULL;
  11749. struct intel_plane *primary = NULL;
  11750. struct intel_plane *cursor = NULL;
  11751. int sprite, ret;
  11752. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11753. if (!intel_crtc)
  11754. return -ENOMEM;
  11755. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11756. if (!crtc_state) {
  11757. ret = -ENOMEM;
  11758. goto fail;
  11759. }
  11760. intel_crtc->config = crtc_state;
  11761. intel_crtc->base.state = &crtc_state->base;
  11762. crtc_state->base.crtc = &intel_crtc->base;
  11763. primary = intel_primary_plane_create(dev_priv, pipe);
  11764. if (IS_ERR(primary)) {
  11765. ret = PTR_ERR(primary);
  11766. goto fail;
  11767. }
  11768. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11769. for_each_sprite(dev_priv, pipe, sprite) {
  11770. struct intel_plane *plane;
  11771. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11772. if (IS_ERR(plane)) {
  11773. ret = PTR_ERR(plane);
  11774. goto fail;
  11775. }
  11776. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11777. }
  11778. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11779. if (IS_ERR(cursor)) {
  11780. ret = PTR_ERR(cursor);
  11781. goto fail;
  11782. }
  11783. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11784. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11785. &primary->base, &cursor->base,
  11786. &intel_crtc_funcs,
  11787. "pipe %c", pipe_name(pipe));
  11788. if (ret)
  11789. goto fail;
  11790. intel_crtc->pipe = pipe;
  11791. /* initialize shared scalers */
  11792. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11793. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
  11794. dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
  11795. dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
  11796. if (INTEL_GEN(dev_priv) < 9) {
  11797. enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
  11798. BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11799. dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
  11800. dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
  11801. }
  11802. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11803. intel_color_init(&intel_crtc->base);
  11804. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11805. return 0;
  11806. fail:
  11807. /*
  11808. * drm_mode_config_cleanup() will free up any
  11809. * crtcs/planes already initialized.
  11810. */
  11811. kfree(crtc_state);
  11812. kfree(intel_crtc);
  11813. return ret;
  11814. }
  11815. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11816. {
  11817. struct drm_device *dev = connector->base.dev;
  11818. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11819. if (!connector->base.state->crtc)
  11820. return INVALID_PIPE;
  11821. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11822. }
  11823. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11824. struct drm_file *file)
  11825. {
  11826. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11827. struct drm_crtc *drmmode_crtc;
  11828. struct intel_crtc *crtc;
  11829. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11830. if (!drmmode_crtc)
  11831. return -ENOENT;
  11832. crtc = to_intel_crtc(drmmode_crtc);
  11833. pipe_from_crtc_id->pipe = crtc->pipe;
  11834. return 0;
  11835. }
  11836. static int intel_encoder_clones(struct intel_encoder *encoder)
  11837. {
  11838. struct drm_device *dev = encoder->base.dev;
  11839. struct intel_encoder *source_encoder;
  11840. int index_mask = 0;
  11841. int entry = 0;
  11842. for_each_intel_encoder(dev, source_encoder) {
  11843. if (encoders_cloneable(encoder, source_encoder))
  11844. index_mask |= (1 << entry);
  11845. entry++;
  11846. }
  11847. return index_mask;
  11848. }
  11849. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11850. {
  11851. if (!IS_MOBILE(dev_priv))
  11852. return false;
  11853. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11854. return false;
  11855. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11856. return false;
  11857. return true;
  11858. }
  11859. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11860. {
  11861. if (INTEL_GEN(dev_priv) >= 9)
  11862. return false;
  11863. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11864. return false;
  11865. if (IS_CHERRYVIEW(dev_priv))
  11866. return false;
  11867. if (HAS_PCH_LPT_H(dev_priv) &&
  11868. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11869. return false;
  11870. /* DDI E can't be used if DDI A requires 4 lanes */
  11871. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11872. return false;
  11873. if (!dev_priv->vbt.int_crt_support)
  11874. return false;
  11875. return true;
  11876. }
  11877. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11878. {
  11879. int pps_num;
  11880. int pps_idx;
  11881. if (HAS_DDI(dev_priv))
  11882. return;
  11883. /*
  11884. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11885. * everywhere where registers can be write protected.
  11886. */
  11887. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11888. pps_num = 2;
  11889. else
  11890. pps_num = 1;
  11891. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11892. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11893. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11894. I915_WRITE(PP_CONTROL(pps_idx), val);
  11895. }
  11896. }
  11897. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11898. {
  11899. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11900. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11901. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11902. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11903. else
  11904. dev_priv->pps_mmio_base = PPS_BASE;
  11905. intel_pps_unlock_regs_wa(dev_priv);
  11906. }
  11907. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11908. {
  11909. struct intel_encoder *encoder;
  11910. bool dpd_is_edp = false;
  11911. intel_pps_init(dev_priv);
  11912. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  11913. return;
  11914. /*
  11915. * intel_edp_init_connector() depends on this completing first, to
  11916. * prevent the registeration of both eDP and LVDS and the incorrect
  11917. * sharing of the PPS.
  11918. */
  11919. intel_lvds_init(dev_priv);
  11920. if (intel_crt_present(dev_priv))
  11921. intel_crt_init(dev_priv);
  11922. if (IS_ICELAKE(dev_priv)) {
  11923. intel_ddi_init(dev_priv, PORT_A);
  11924. intel_ddi_init(dev_priv, PORT_B);
  11925. intel_ddi_init(dev_priv, PORT_C);
  11926. intel_ddi_init(dev_priv, PORT_D);
  11927. intel_ddi_init(dev_priv, PORT_E);
  11928. intel_ddi_init(dev_priv, PORT_F);
  11929. } else if (IS_GEN9_LP(dev_priv)) {
  11930. /*
  11931. * FIXME: Broxton doesn't support port detection via the
  11932. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11933. * detect the ports.
  11934. */
  11935. intel_ddi_init(dev_priv, PORT_A);
  11936. intel_ddi_init(dev_priv, PORT_B);
  11937. intel_ddi_init(dev_priv, PORT_C);
  11938. vlv_dsi_init(dev_priv);
  11939. } else if (HAS_DDI(dev_priv)) {
  11940. int found;
  11941. /*
  11942. * Haswell uses DDI functions to detect digital outputs.
  11943. * On SKL pre-D0 the strap isn't connected, so we assume
  11944. * it's there.
  11945. */
  11946. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11947. /* WaIgnoreDDIAStrap: skl */
  11948. if (found || IS_GEN9_BC(dev_priv))
  11949. intel_ddi_init(dev_priv, PORT_A);
  11950. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11951. * register */
  11952. found = I915_READ(SFUSE_STRAP);
  11953. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11954. intel_ddi_init(dev_priv, PORT_B);
  11955. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11956. intel_ddi_init(dev_priv, PORT_C);
  11957. if (found & SFUSE_STRAP_DDID_DETECTED)
  11958. intel_ddi_init(dev_priv, PORT_D);
  11959. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11960. intel_ddi_init(dev_priv, PORT_F);
  11961. /*
  11962. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11963. */
  11964. if (IS_GEN9_BC(dev_priv) &&
  11965. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11966. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11967. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11968. intel_ddi_init(dev_priv, PORT_E);
  11969. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11970. int found;
  11971. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11972. if (has_edp_a(dev_priv))
  11973. intel_dp_init(dev_priv, DP_A, PORT_A);
  11974. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11975. /* PCH SDVOB multiplex with HDMIB */
  11976. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11977. if (!found)
  11978. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11979. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11980. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11981. }
  11982. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11983. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11984. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11985. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11986. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11987. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11988. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11989. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11990. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11991. bool has_edp, has_port;
  11992. /*
  11993. * The DP_DETECTED bit is the latched state of the DDC
  11994. * SDA pin at boot. However since eDP doesn't require DDC
  11995. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11996. * eDP ports may have been muxed to an alternate function.
  11997. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11998. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11999. * detect eDP ports.
  12000. *
  12001. * Sadly the straps seem to be missing sometimes even for HDMI
  12002. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12003. * and VBT for the presence of the port. Additionally we can't
  12004. * trust the port type the VBT declares as we've seen at least
  12005. * HDMI ports that the VBT claim are DP or eDP.
  12006. */
  12007. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  12008. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12009. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12010. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  12011. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12012. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  12013. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  12014. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12015. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12016. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  12017. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12018. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  12019. if (IS_CHERRYVIEW(dev_priv)) {
  12020. /*
  12021. * eDP not supported on port D,
  12022. * so no need to worry about it
  12023. */
  12024. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12025. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12026. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  12027. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12028. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  12029. }
  12030. vlv_dsi_init(dev_priv);
  12031. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  12032. bool found = false;
  12033. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12034. DRM_DEBUG_KMS("probing SDVOB\n");
  12035. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  12036. if (!found && IS_G4X(dev_priv)) {
  12037. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12038. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  12039. }
  12040. if (!found && IS_G4X(dev_priv))
  12041. intel_dp_init(dev_priv, DP_B, PORT_B);
  12042. }
  12043. /* Before G4X SDVOC doesn't have its own detect register */
  12044. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12045. DRM_DEBUG_KMS("probing SDVOC\n");
  12046. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  12047. }
  12048. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12049. if (IS_G4X(dev_priv)) {
  12050. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12051. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  12052. }
  12053. if (IS_G4X(dev_priv))
  12054. intel_dp_init(dev_priv, DP_C, PORT_C);
  12055. }
  12056. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  12057. intel_dp_init(dev_priv, DP_D, PORT_D);
  12058. } else if (IS_GEN2(dev_priv))
  12059. intel_dvo_init(dev_priv);
  12060. if (SUPPORTS_TV(dev_priv))
  12061. intel_tv_init(dev_priv);
  12062. intel_psr_init(dev_priv);
  12063. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12064. encoder->base.possible_crtcs = encoder->crtc_mask;
  12065. encoder->base.possible_clones =
  12066. intel_encoder_clones(encoder);
  12067. }
  12068. intel_init_pch_refclk(dev_priv);
  12069. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  12070. }
  12071. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12072. {
  12073. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12074. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12075. drm_framebuffer_cleanup(fb);
  12076. i915_gem_object_lock(obj);
  12077. WARN_ON(!obj->framebuffer_references--);
  12078. i915_gem_object_unlock(obj);
  12079. i915_gem_object_put(obj);
  12080. kfree(intel_fb);
  12081. }
  12082. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12083. struct drm_file *file,
  12084. unsigned int *handle)
  12085. {
  12086. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12087. if (obj->userptr.mm) {
  12088. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12089. return -EINVAL;
  12090. }
  12091. return drm_gem_handle_create(file, &obj->base, handle);
  12092. }
  12093. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12094. struct drm_file *file,
  12095. unsigned flags, unsigned color,
  12096. struct drm_clip_rect *clips,
  12097. unsigned num_clips)
  12098. {
  12099. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12100. i915_gem_object_flush_if_display(obj);
  12101. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  12102. return 0;
  12103. }
  12104. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12105. .destroy = intel_user_framebuffer_destroy,
  12106. .create_handle = intel_user_framebuffer_create_handle,
  12107. .dirty = intel_user_framebuffer_dirty,
  12108. };
  12109. static
  12110. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  12111. uint64_t fb_modifier, uint32_t pixel_format)
  12112. {
  12113. struct intel_crtc *crtc;
  12114. struct intel_plane *plane;
  12115. /*
  12116. * We assume the primary plane for pipe A has
  12117. * the highest stride limits of them all.
  12118. */
  12119. crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
  12120. plane = to_intel_plane(crtc->base.primary);
  12121. return plane->max_stride(plane, pixel_format, fb_modifier,
  12122. DRM_MODE_ROTATE_0);
  12123. }
  12124. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  12125. struct drm_i915_gem_object *obj,
  12126. struct drm_mode_fb_cmd2 *mode_cmd)
  12127. {
  12128. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  12129. struct drm_framebuffer *fb = &intel_fb->base;
  12130. struct drm_format_name_buf format_name;
  12131. u32 pitch_limit;
  12132. unsigned int tiling, stride;
  12133. int ret = -EINVAL;
  12134. int i;
  12135. i915_gem_object_lock(obj);
  12136. obj->framebuffer_references++;
  12137. tiling = i915_gem_object_get_tiling(obj);
  12138. stride = i915_gem_object_get_stride(obj);
  12139. i915_gem_object_unlock(obj);
  12140. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12141. /*
  12142. * If there's a fence, enforce that
  12143. * the fb modifier and tiling mode match.
  12144. */
  12145. if (tiling != I915_TILING_NONE &&
  12146. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12147. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  12148. goto err;
  12149. }
  12150. } else {
  12151. if (tiling == I915_TILING_X) {
  12152. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12153. } else if (tiling == I915_TILING_Y) {
  12154. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  12155. goto err;
  12156. }
  12157. }
  12158. /* Passed in modifier sanity checking. */
  12159. switch (mode_cmd->modifier[0]) {
  12160. case I915_FORMAT_MOD_Y_TILED_CCS:
  12161. case I915_FORMAT_MOD_Yf_TILED_CCS:
  12162. switch (mode_cmd->pixel_format) {
  12163. case DRM_FORMAT_XBGR8888:
  12164. case DRM_FORMAT_ABGR8888:
  12165. case DRM_FORMAT_XRGB8888:
  12166. case DRM_FORMAT_ARGB8888:
  12167. break;
  12168. default:
  12169. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  12170. goto err;
  12171. }
  12172. /* fall through */
  12173. case I915_FORMAT_MOD_Y_TILED:
  12174. case I915_FORMAT_MOD_Yf_TILED:
  12175. if (INTEL_GEN(dev_priv) < 9) {
  12176. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  12177. mode_cmd->modifier[0]);
  12178. goto err;
  12179. }
  12180. case DRM_FORMAT_MOD_LINEAR:
  12181. case I915_FORMAT_MOD_X_TILED:
  12182. break;
  12183. default:
  12184. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  12185. mode_cmd->modifier[0]);
  12186. goto err;
  12187. }
  12188. /*
  12189. * gen2/3 display engine uses the fence if present,
  12190. * so the tiling mode must match the fb modifier exactly.
  12191. */
  12192. if (INTEL_GEN(dev_priv) < 4 &&
  12193. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12194. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  12195. goto err;
  12196. }
  12197. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  12198. mode_cmd->pixel_format);
  12199. if (mode_cmd->pitches[0] > pitch_limit) {
  12200. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  12201. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  12202. "tiled" : "linear",
  12203. mode_cmd->pitches[0], pitch_limit);
  12204. goto err;
  12205. }
  12206. /*
  12207. * If there's a fence, enforce that
  12208. * the fb pitch and fence stride match.
  12209. */
  12210. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  12211. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  12212. mode_cmd->pitches[0], stride);
  12213. goto err;
  12214. }
  12215. /* Reject formats not supported by any plane early. */
  12216. switch (mode_cmd->pixel_format) {
  12217. case DRM_FORMAT_C8:
  12218. case DRM_FORMAT_RGB565:
  12219. case DRM_FORMAT_XRGB8888:
  12220. case DRM_FORMAT_ARGB8888:
  12221. break;
  12222. case DRM_FORMAT_XRGB1555:
  12223. if (INTEL_GEN(dev_priv) > 3) {
  12224. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12225. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12226. goto err;
  12227. }
  12228. break;
  12229. case DRM_FORMAT_ABGR8888:
  12230. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12231. INTEL_GEN(dev_priv) < 9) {
  12232. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12233. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12234. goto err;
  12235. }
  12236. break;
  12237. case DRM_FORMAT_XBGR8888:
  12238. case DRM_FORMAT_XRGB2101010:
  12239. case DRM_FORMAT_XBGR2101010:
  12240. if (INTEL_GEN(dev_priv) < 4) {
  12241. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12242. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12243. goto err;
  12244. }
  12245. break;
  12246. case DRM_FORMAT_ABGR2101010:
  12247. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12248. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12249. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12250. goto err;
  12251. }
  12252. break;
  12253. case DRM_FORMAT_YUYV:
  12254. case DRM_FORMAT_UYVY:
  12255. case DRM_FORMAT_YVYU:
  12256. case DRM_FORMAT_VYUY:
  12257. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  12258. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12259. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12260. goto err;
  12261. }
  12262. break;
  12263. case DRM_FORMAT_NV12:
  12264. if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
  12265. IS_BROXTON(dev_priv) || INTEL_GEN(dev_priv) >= 11) {
  12266. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12267. drm_get_format_name(mode_cmd->pixel_format,
  12268. &format_name));
  12269. goto err;
  12270. }
  12271. break;
  12272. default:
  12273. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12274. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12275. goto err;
  12276. }
  12277. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12278. if (mode_cmd->offsets[0] != 0)
  12279. goto err;
  12280. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  12281. if (fb->format->format == DRM_FORMAT_NV12 &&
  12282. (fb->width < SKL_MIN_YUV_420_SRC_W ||
  12283. fb->height < SKL_MIN_YUV_420_SRC_H ||
  12284. (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
  12285. DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
  12286. goto err;
  12287. }
  12288. for (i = 0; i < fb->format->num_planes; i++) {
  12289. u32 stride_alignment;
  12290. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  12291. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  12292. goto err;
  12293. }
  12294. stride_alignment = intel_fb_stride_alignment(fb, i);
  12295. /*
  12296. * Display WA #0531: skl,bxt,kbl,glk
  12297. *
  12298. * Render decompression and plane width > 3840
  12299. * combined with horizontal panning requires the
  12300. * plane stride to be a multiple of 4. We'll just
  12301. * require the entire fb to accommodate that to avoid
  12302. * potential runtime errors at plane configuration time.
  12303. */
  12304. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  12305. is_ccs_modifier(fb->modifier))
  12306. stride_alignment *= 4;
  12307. if (fb->pitches[i] & (stride_alignment - 1)) {
  12308. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  12309. i, fb->pitches[i], stride_alignment);
  12310. goto err;
  12311. }
  12312. fb->obj[i] = &obj->base;
  12313. }
  12314. ret = intel_fill_fb_info(dev_priv, fb);
  12315. if (ret)
  12316. goto err;
  12317. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  12318. if (ret) {
  12319. DRM_ERROR("framebuffer init failed %d\n", ret);
  12320. goto err;
  12321. }
  12322. return 0;
  12323. err:
  12324. i915_gem_object_lock(obj);
  12325. obj->framebuffer_references--;
  12326. i915_gem_object_unlock(obj);
  12327. return ret;
  12328. }
  12329. static struct drm_framebuffer *
  12330. intel_user_framebuffer_create(struct drm_device *dev,
  12331. struct drm_file *filp,
  12332. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12333. {
  12334. struct drm_framebuffer *fb;
  12335. struct drm_i915_gem_object *obj;
  12336. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12337. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12338. if (!obj)
  12339. return ERR_PTR(-ENOENT);
  12340. fb = intel_framebuffer_create(obj, &mode_cmd);
  12341. if (IS_ERR(fb))
  12342. i915_gem_object_put(obj);
  12343. return fb;
  12344. }
  12345. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12346. {
  12347. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12348. drm_atomic_state_default_release(state);
  12349. i915_sw_fence_fini(&intel_state->commit_ready);
  12350. kfree(state);
  12351. }
  12352. static enum drm_mode_status
  12353. intel_mode_valid(struct drm_device *dev,
  12354. const struct drm_display_mode *mode)
  12355. {
  12356. struct drm_i915_private *dev_priv = to_i915(dev);
  12357. int hdisplay_max, htotal_max;
  12358. int vdisplay_max, vtotal_max;
  12359. /*
  12360. * Can't reject DBLSCAN here because Xorg ddxen can add piles
  12361. * of DBLSCAN modes to the output's mode list when they detect
  12362. * the scaling mode property on the connector. And they don't
  12363. * ask the kernel to validate those modes in any way until
  12364. * modeset time at which point the client gets a protocol error.
  12365. * So in order to not upset those clients we silently ignore the
  12366. * DBLSCAN flag on such connectors. For other connectors we will
  12367. * reject modes with the DBLSCAN flag in encoder->compute_config().
  12368. * And we always reject DBLSCAN modes in connector->mode_valid()
  12369. * as we never want such modes on the connector's mode list.
  12370. */
  12371. if (mode->vscan > 1)
  12372. return MODE_NO_VSCAN;
  12373. if (mode->flags & DRM_MODE_FLAG_HSKEW)
  12374. return MODE_H_ILLEGAL;
  12375. if (mode->flags & (DRM_MODE_FLAG_CSYNC |
  12376. DRM_MODE_FLAG_NCSYNC |
  12377. DRM_MODE_FLAG_PCSYNC))
  12378. return MODE_HSYNC;
  12379. if (mode->flags & (DRM_MODE_FLAG_BCAST |
  12380. DRM_MODE_FLAG_PIXMUX |
  12381. DRM_MODE_FLAG_CLKDIV2))
  12382. return MODE_BAD;
  12383. if (INTEL_GEN(dev_priv) >= 9 ||
  12384. IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
  12385. hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
  12386. vdisplay_max = 4096;
  12387. htotal_max = 8192;
  12388. vtotal_max = 8192;
  12389. } else if (INTEL_GEN(dev_priv) >= 3) {
  12390. hdisplay_max = 4096;
  12391. vdisplay_max = 4096;
  12392. htotal_max = 8192;
  12393. vtotal_max = 8192;
  12394. } else {
  12395. hdisplay_max = 2048;
  12396. vdisplay_max = 2048;
  12397. htotal_max = 4096;
  12398. vtotal_max = 4096;
  12399. }
  12400. if (mode->hdisplay > hdisplay_max ||
  12401. mode->hsync_start > htotal_max ||
  12402. mode->hsync_end > htotal_max ||
  12403. mode->htotal > htotal_max)
  12404. return MODE_H_ILLEGAL;
  12405. if (mode->vdisplay > vdisplay_max ||
  12406. mode->vsync_start > vtotal_max ||
  12407. mode->vsync_end > vtotal_max ||
  12408. mode->vtotal > vtotal_max)
  12409. return MODE_V_ILLEGAL;
  12410. return MODE_OK;
  12411. }
  12412. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12413. .fb_create = intel_user_framebuffer_create,
  12414. .get_format_info = intel_get_format_info,
  12415. .output_poll_changed = intel_fbdev_output_poll_changed,
  12416. .mode_valid = intel_mode_valid,
  12417. .atomic_check = intel_atomic_check,
  12418. .atomic_commit = intel_atomic_commit,
  12419. .atomic_state_alloc = intel_atomic_state_alloc,
  12420. .atomic_state_clear = intel_atomic_state_clear,
  12421. .atomic_state_free = intel_atomic_state_free,
  12422. };
  12423. /**
  12424. * intel_init_display_hooks - initialize the display modesetting hooks
  12425. * @dev_priv: device private
  12426. */
  12427. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12428. {
  12429. intel_init_cdclk_hooks(dev_priv);
  12430. if (INTEL_GEN(dev_priv) >= 9) {
  12431. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12432. dev_priv->display.get_initial_plane_config =
  12433. skylake_get_initial_plane_config;
  12434. dev_priv->display.crtc_compute_clock =
  12435. haswell_crtc_compute_clock;
  12436. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12437. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12438. } else if (HAS_DDI(dev_priv)) {
  12439. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12440. dev_priv->display.get_initial_plane_config =
  12441. i9xx_get_initial_plane_config;
  12442. dev_priv->display.crtc_compute_clock =
  12443. haswell_crtc_compute_clock;
  12444. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12445. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12446. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12447. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12448. dev_priv->display.get_initial_plane_config =
  12449. i9xx_get_initial_plane_config;
  12450. dev_priv->display.crtc_compute_clock =
  12451. ironlake_crtc_compute_clock;
  12452. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12453. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12454. } else if (IS_CHERRYVIEW(dev_priv)) {
  12455. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12456. dev_priv->display.get_initial_plane_config =
  12457. i9xx_get_initial_plane_config;
  12458. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12459. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12460. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12461. } else if (IS_VALLEYVIEW(dev_priv)) {
  12462. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12463. dev_priv->display.get_initial_plane_config =
  12464. i9xx_get_initial_plane_config;
  12465. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12466. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12467. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12468. } else if (IS_G4X(dev_priv)) {
  12469. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12470. dev_priv->display.get_initial_plane_config =
  12471. i9xx_get_initial_plane_config;
  12472. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12473. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12474. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12475. } else if (IS_PINEVIEW(dev_priv)) {
  12476. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12477. dev_priv->display.get_initial_plane_config =
  12478. i9xx_get_initial_plane_config;
  12479. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12480. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12481. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12482. } else if (!IS_GEN2(dev_priv)) {
  12483. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12484. dev_priv->display.get_initial_plane_config =
  12485. i9xx_get_initial_plane_config;
  12486. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12487. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12488. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12489. } else {
  12490. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12491. dev_priv->display.get_initial_plane_config =
  12492. i9xx_get_initial_plane_config;
  12493. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12494. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12495. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12496. }
  12497. if (IS_GEN5(dev_priv)) {
  12498. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12499. } else if (IS_GEN6(dev_priv)) {
  12500. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12501. } else if (IS_IVYBRIDGE(dev_priv)) {
  12502. /* FIXME: detect B0+ stepping and use auto training */
  12503. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12504. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12505. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12506. }
  12507. if (INTEL_GEN(dev_priv) >= 9)
  12508. dev_priv->display.update_crtcs = skl_update_crtcs;
  12509. else
  12510. dev_priv->display.update_crtcs = intel_update_crtcs;
  12511. }
  12512. /*
  12513. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12514. */
  12515. static void quirk_ssc_force_disable(struct drm_device *dev)
  12516. {
  12517. struct drm_i915_private *dev_priv = to_i915(dev);
  12518. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12519. DRM_INFO("applying lvds SSC disable quirk\n");
  12520. }
  12521. /*
  12522. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12523. * brightness value
  12524. */
  12525. static void quirk_invert_brightness(struct drm_device *dev)
  12526. {
  12527. struct drm_i915_private *dev_priv = to_i915(dev);
  12528. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12529. DRM_INFO("applying inverted panel brightness quirk\n");
  12530. }
  12531. /* Some VBT's incorrectly indicate no backlight is present */
  12532. static void quirk_backlight_present(struct drm_device *dev)
  12533. {
  12534. struct drm_i915_private *dev_priv = to_i915(dev);
  12535. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12536. DRM_INFO("applying backlight present quirk\n");
  12537. }
  12538. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  12539. * which is 300 ms greater than eDP spec T12 min.
  12540. */
  12541. static void quirk_increase_t12_delay(struct drm_device *dev)
  12542. {
  12543. struct drm_i915_private *dev_priv = to_i915(dev);
  12544. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  12545. DRM_INFO("Applying T12 delay quirk\n");
  12546. }
  12547. /*
  12548. * GeminiLake NUC HDMI outputs require additional off time
  12549. * this allows the onboard retimer to correctly sync to signal
  12550. */
  12551. static void quirk_increase_ddi_disabled_time(struct drm_device *dev)
  12552. {
  12553. struct drm_i915_private *dev_priv = to_i915(dev);
  12554. dev_priv->quirks |= QUIRK_INCREASE_DDI_DISABLED_TIME;
  12555. DRM_INFO("Applying Increase DDI Disabled quirk\n");
  12556. }
  12557. struct intel_quirk {
  12558. int device;
  12559. int subsystem_vendor;
  12560. int subsystem_device;
  12561. void (*hook)(struct drm_device *dev);
  12562. };
  12563. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12564. struct intel_dmi_quirk {
  12565. void (*hook)(struct drm_device *dev);
  12566. const struct dmi_system_id (*dmi_id_list)[];
  12567. };
  12568. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12569. {
  12570. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12571. return 1;
  12572. }
  12573. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12574. {
  12575. .dmi_id_list = &(const struct dmi_system_id[]) {
  12576. {
  12577. .callback = intel_dmi_reverse_brightness,
  12578. .ident = "NCR Corporation",
  12579. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12580. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12581. },
  12582. },
  12583. { } /* terminating entry */
  12584. },
  12585. .hook = quirk_invert_brightness,
  12586. },
  12587. };
  12588. static struct intel_quirk intel_quirks[] = {
  12589. /* Lenovo U160 cannot use SSC on LVDS */
  12590. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12591. /* Sony Vaio Y cannot use SSC on LVDS */
  12592. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12593. /* Acer Aspire 5734Z must invert backlight brightness */
  12594. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12595. /* Acer/eMachines G725 */
  12596. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12597. /* Acer/eMachines e725 */
  12598. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12599. /* Acer/Packard Bell NCL20 */
  12600. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12601. /* Acer Aspire 4736Z */
  12602. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12603. /* Acer Aspire 5336 */
  12604. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12605. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12606. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12607. /* Acer C720 Chromebook (Core i3 4005U) */
  12608. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12609. /* Apple Macbook 2,1 (Core 2 T7400) */
  12610. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12611. /* Apple Macbook 4,1 */
  12612. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12613. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12614. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12615. /* HP Chromebook 14 (Celeron 2955U) */
  12616. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12617. /* Dell Chromebook 11 */
  12618. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12619. /* Dell Chromebook 11 (2015 version) */
  12620. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12621. /* Toshiba Satellite P50-C-18C */
  12622. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12623. /* GeminiLake NUC */
  12624. { 0x3185, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
  12625. { 0x3184, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
  12626. /* ASRock ITX*/
  12627. { 0x3185, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
  12628. { 0x3184, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
  12629. };
  12630. static void intel_init_quirks(struct drm_device *dev)
  12631. {
  12632. struct pci_dev *d = dev->pdev;
  12633. int i;
  12634. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12635. struct intel_quirk *q = &intel_quirks[i];
  12636. if (d->device == q->device &&
  12637. (d->subsystem_vendor == q->subsystem_vendor ||
  12638. q->subsystem_vendor == PCI_ANY_ID) &&
  12639. (d->subsystem_device == q->subsystem_device ||
  12640. q->subsystem_device == PCI_ANY_ID))
  12641. q->hook(dev);
  12642. }
  12643. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12644. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12645. intel_dmi_quirks[i].hook(dev);
  12646. }
  12647. }
  12648. /* Disable the VGA plane that we never use */
  12649. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12650. {
  12651. struct pci_dev *pdev = dev_priv->drm.pdev;
  12652. u8 sr1;
  12653. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12654. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12655. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12656. outb(SR01, VGA_SR_INDEX);
  12657. sr1 = inb(VGA_SR_DATA);
  12658. outb(sr1 | 1<<5, VGA_SR_DATA);
  12659. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12660. udelay(300);
  12661. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12662. POSTING_READ(vga_reg);
  12663. }
  12664. void intel_modeset_init_hw(struct drm_device *dev)
  12665. {
  12666. struct drm_i915_private *dev_priv = to_i915(dev);
  12667. intel_update_cdclk(dev_priv);
  12668. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12669. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12670. }
  12671. /*
  12672. * Calculate what we think the watermarks should be for the state we've read
  12673. * out of the hardware and then immediately program those watermarks so that
  12674. * we ensure the hardware settings match our internal state.
  12675. *
  12676. * We can calculate what we think WM's should be by creating a duplicate of the
  12677. * current state (which was constructed during hardware readout) and running it
  12678. * through the atomic check code to calculate new watermark values in the
  12679. * state object.
  12680. */
  12681. static void sanitize_watermarks(struct drm_device *dev)
  12682. {
  12683. struct drm_i915_private *dev_priv = to_i915(dev);
  12684. struct drm_atomic_state *state;
  12685. struct intel_atomic_state *intel_state;
  12686. struct drm_crtc *crtc;
  12687. struct drm_crtc_state *cstate;
  12688. struct drm_modeset_acquire_ctx ctx;
  12689. int ret;
  12690. int i;
  12691. /* Only supported on platforms that use atomic watermark design */
  12692. if (!dev_priv->display.optimize_watermarks)
  12693. return;
  12694. /*
  12695. * We need to hold connection_mutex before calling duplicate_state so
  12696. * that the connector loop is protected.
  12697. */
  12698. drm_modeset_acquire_init(&ctx, 0);
  12699. retry:
  12700. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12701. if (ret == -EDEADLK) {
  12702. drm_modeset_backoff(&ctx);
  12703. goto retry;
  12704. } else if (WARN_ON(ret)) {
  12705. goto fail;
  12706. }
  12707. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12708. if (WARN_ON(IS_ERR(state)))
  12709. goto fail;
  12710. intel_state = to_intel_atomic_state(state);
  12711. /*
  12712. * Hardware readout is the only time we don't want to calculate
  12713. * intermediate watermarks (since we don't trust the current
  12714. * watermarks).
  12715. */
  12716. if (!HAS_GMCH_DISPLAY(dev_priv))
  12717. intel_state->skip_intermediate_wm = true;
  12718. ret = intel_atomic_check(dev, state);
  12719. if (ret) {
  12720. /*
  12721. * If we fail here, it means that the hardware appears to be
  12722. * programmed in a way that shouldn't be possible, given our
  12723. * understanding of watermark requirements. This might mean a
  12724. * mistake in the hardware readout code or a mistake in the
  12725. * watermark calculations for a given platform. Raise a WARN
  12726. * so that this is noticeable.
  12727. *
  12728. * If this actually happens, we'll have to just leave the
  12729. * BIOS-programmed watermarks untouched and hope for the best.
  12730. */
  12731. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12732. goto put_state;
  12733. }
  12734. /* Write calculated watermark values back */
  12735. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12736. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12737. cs->wm.need_postvbl_update = true;
  12738. dev_priv->display.optimize_watermarks(intel_state, cs);
  12739. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12740. }
  12741. put_state:
  12742. drm_atomic_state_put(state);
  12743. fail:
  12744. drm_modeset_drop_locks(&ctx);
  12745. drm_modeset_acquire_fini(&ctx);
  12746. }
  12747. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12748. {
  12749. if (IS_GEN5(dev_priv)) {
  12750. u32 fdi_pll_clk =
  12751. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12752. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12753. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12754. dev_priv->fdi_pll_freq = 270000;
  12755. } else {
  12756. return;
  12757. }
  12758. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12759. }
  12760. static int intel_initial_commit(struct drm_device *dev)
  12761. {
  12762. struct drm_atomic_state *state = NULL;
  12763. struct drm_modeset_acquire_ctx ctx;
  12764. struct drm_crtc *crtc;
  12765. struct drm_crtc_state *crtc_state;
  12766. int ret = 0;
  12767. state = drm_atomic_state_alloc(dev);
  12768. if (!state)
  12769. return -ENOMEM;
  12770. drm_modeset_acquire_init(&ctx, 0);
  12771. retry:
  12772. state->acquire_ctx = &ctx;
  12773. drm_for_each_crtc(crtc, dev) {
  12774. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12775. if (IS_ERR(crtc_state)) {
  12776. ret = PTR_ERR(crtc_state);
  12777. goto out;
  12778. }
  12779. if (crtc_state->active) {
  12780. ret = drm_atomic_add_affected_planes(state, crtc);
  12781. if (ret)
  12782. goto out;
  12783. /*
  12784. * FIXME hack to force a LUT update to avoid the
  12785. * plane update forcing the pipe gamma on without
  12786. * having a proper LUT loaded. Remove once we
  12787. * have readout for pipe gamma enable.
  12788. */
  12789. crtc_state->color_mgmt_changed = true;
  12790. }
  12791. }
  12792. ret = drm_atomic_commit(state);
  12793. out:
  12794. if (ret == -EDEADLK) {
  12795. drm_atomic_state_clear(state);
  12796. drm_modeset_backoff(&ctx);
  12797. goto retry;
  12798. }
  12799. drm_atomic_state_put(state);
  12800. drm_modeset_drop_locks(&ctx);
  12801. drm_modeset_acquire_fini(&ctx);
  12802. return ret;
  12803. }
  12804. int intel_modeset_init(struct drm_device *dev)
  12805. {
  12806. struct drm_i915_private *dev_priv = to_i915(dev);
  12807. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12808. enum pipe pipe;
  12809. struct intel_crtc *crtc;
  12810. int ret;
  12811. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12812. drm_mode_config_init(dev);
  12813. dev->mode_config.min_width = 0;
  12814. dev->mode_config.min_height = 0;
  12815. dev->mode_config.preferred_depth = 24;
  12816. dev->mode_config.prefer_shadow = 1;
  12817. dev->mode_config.allow_fb_modifiers = true;
  12818. dev->mode_config.funcs = &intel_mode_funcs;
  12819. init_llist_head(&dev_priv->atomic_helper.free_list);
  12820. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12821. intel_atomic_helper_free_state_worker);
  12822. intel_init_quirks(dev);
  12823. intel_init_pm(dev_priv);
  12824. /*
  12825. * There may be no VBT; and if the BIOS enabled SSC we can
  12826. * just keep using it to avoid unnecessary flicker. Whereas if the
  12827. * BIOS isn't using it, don't assume it will work even if the VBT
  12828. * indicates as much.
  12829. */
  12830. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12831. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12832. DREF_SSC1_ENABLE);
  12833. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12834. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12835. bios_lvds_use_ssc ? "en" : "dis",
  12836. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12837. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12838. }
  12839. }
  12840. /* maximum framebuffer dimensions */
  12841. if (IS_GEN2(dev_priv)) {
  12842. dev->mode_config.max_width = 2048;
  12843. dev->mode_config.max_height = 2048;
  12844. } else if (IS_GEN3(dev_priv)) {
  12845. dev->mode_config.max_width = 4096;
  12846. dev->mode_config.max_height = 4096;
  12847. } else {
  12848. dev->mode_config.max_width = 8192;
  12849. dev->mode_config.max_height = 8192;
  12850. }
  12851. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12852. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12853. dev->mode_config.cursor_height = 1023;
  12854. } else if (IS_GEN2(dev_priv)) {
  12855. dev->mode_config.cursor_width = 64;
  12856. dev->mode_config.cursor_height = 64;
  12857. } else {
  12858. dev->mode_config.cursor_width = 256;
  12859. dev->mode_config.cursor_height = 256;
  12860. }
  12861. dev->mode_config.fb_base = ggtt->gmadr.start;
  12862. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12863. INTEL_INFO(dev_priv)->num_pipes,
  12864. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12865. for_each_pipe(dev_priv, pipe) {
  12866. ret = intel_crtc_init(dev_priv, pipe);
  12867. if (ret) {
  12868. drm_mode_config_cleanup(dev);
  12869. return ret;
  12870. }
  12871. }
  12872. intel_shared_dpll_init(dev);
  12873. intel_update_fdi_pll_freq(dev_priv);
  12874. intel_update_czclk(dev_priv);
  12875. intel_modeset_init_hw(dev);
  12876. if (dev_priv->max_cdclk_freq == 0)
  12877. intel_update_max_cdclk(dev_priv);
  12878. /* Just disable it once at startup */
  12879. i915_disable_vga(dev_priv);
  12880. intel_setup_outputs(dev_priv);
  12881. drm_modeset_lock_all(dev);
  12882. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12883. drm_modeset_unlock_all(dev);
  12884. for_each_intel_crtc(dev, crtc) {
  12885. struct intel_initial_plane_config plane_config = {};
  12886. if (!crtc->active)
  12887. continue;
  12888. /*
  12889. * Note that reserving the BIOS fb up front prevents us
  12890. * from stuffing other stolen allocations like the ring
  12891. * on top. This prevents some ugliness at boot time, and
  12892. * can even allow for smooth boot transitions if the BIOS
  12893. * fb is large enough for the active pipe configuration.
  12894. */
  12895. dev_priv->display.get_initial_plane_config(crtc,
  12896. &plane_config);
  12897. /*
  12898. * If the fb is shared between multiple heads, we'll
  12899. * just get the first one.
  12900. */
  12901. intel_find_initial_plane_obj(crtc, &plane_config);
  12902. }
  12903. /*
  12904. * Make sure hardware watermarks really match the state we read out.
  12905. * Note that we need to do this after reconstructing the BIOS fb's
  12906. * since the watermark calculation done here will use pstate->fb.
  12907. */
  12908. if (!HAS_GMCH_DISPLAY(dev_priv))
  12909. sanitize_watermarks(dev);
  12910. /*
  12911. * Force all active planes to recompute their states. So that on
  12912. * mode_setcrtc after probe, all the intel_plane_state variables
  12913. * are already calculated and there is no assert_plane warnings
  12914. * during bootup.
  12915. */
  12916. ret = intel_initial_commit(dev);
  12917. if (ret)
  12918. DRM_DEBUG_KMS("Initial commit in probe failed.\n");
  12919. return 0;
  12920. }
  12921. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12922. {
  12923. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12924. /* 640x480@60Hz, ~25175 kHz */
  12925. struct dpll clock = {
  12926. .m1 = 18,
  12927. .m2 = 7,
  12928. .p1 = 13,
  12929. .p2 = 4,
  12930. .n = 2,
  12931. };
  12932. u32 dpll, fp;
  12933. int i;
  12934. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12935. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12936. pipe_name(pipe), clock.vco, clock.dot);
  12937. fp = i9xx_dpll_compute_fp(&clock);
  12938. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12939. DPLL_VGA_MODE_DIS |
  12940. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12941. PLL_P2_DIVIDE_BY_4 |
  12942. PLL_REF_INPUT_DREFCLK |
  12943. DPLL_VCO_ENABLE;
  12944. I915_WRITE(FP0(pipe), fp);
  12945. I915_WRITE(FP1(pipe), fp);
  12946. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12947. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12948. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12949. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12950. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12951. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12952. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12953. /*
  12954. * Apparently we need to have VGA mode enabled prior to changing
  12955. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12956. * dividers, even though the register value does change.
  12957. */
  12958. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12959. I915_WRITE(DPLL(pipe), dpll);
  12960. /* Wait for the clocks to stabilize. */
  12961. POSTING_READ(DPLL(pipe));
  12962. udelay(150);
  12963. /* The pixel multiplier can only be updated once the
  12964. * DPLL is enabled and the clocks are stable.
  12965. *
  12966. * So write it again.
  12967. */
  12968. I915_WRITE(DPLL(pipe), dpll);
  12969. /* We do this three times for luck */
  12970. for (i = 0; i < 3 ; i++) {
  12971. I915_WRITE(DPLL(pipe), dpll);
  12972. POSTING_READ(DPLL(pipe));
  12973. udelay(150); /* wait for warmup */
  12974. }
  12975. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12976. POSTING_READ(PIPECONF(pipe));
  12977. intel_wait_for_pipe_scanline_moving(crtc);
  12978. }
  12979. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12980. {
  12981. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12982. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12983. pipe_name(pipe));
  12984. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12985. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12986. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12987. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
  12988. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
  12989. I915_WRITE(PIPECONF(pipe), 0);
  12990. POSTING_READ(PIPECONF(pipe));
  12991. intel_wait_for_pipe_scanline_stopped(crtc);
  12992. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12993. POSTING_READ(DPLL(pipe));
  12994. }
  12995. static void
  12996. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12997. {
  12998. struct intel_crtc *crtc;
  12999. if (INTEL_GEN(dev_priv) >= 4)
  13000. return;
  13001. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13002. struct intel_plane *plane =
  13003. to_intel_plane(crtc->base.primary);
  13004. struct intel_crtc *plane_crtc;
  13005. enum pipe pipe;
  13006. if (!plane->get_hw_state(plane, &pipe))
  13007. continue;
  13008. if (pipe == crtc->pipe)
  13009. continue;
  13010. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  13011. plane->base.name);
  13012. plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13013. intel_plane_disable_noatomic(plane_crtc, plane);
  13014. }
  13015. }
  13016. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13017. {
  13018. struct drm_device *dev = crtc->base.dev;
  13019. struct intel_encoder *encoder;
  13020. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13021. return true;
  13022. return false;
  13023. }
  13024. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13025. {
  13026. struct drm_device *dev = encoder->base.dev;
  13027. struct intel_connector *connector;
  13028. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13029. return connector;
  13030. return NULL;
  13031. }
  13032. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13033. enum pipe pch_transcoder)
  13034. {
  13035. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13036. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  13037. }
  13038. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  13039. struct drm_modeset_acquire_ctx *ctx)
  13040. {
  13041. struct drm_device *dev = crtc->base.dev;
  13042. struct drm_i915_private *dev_priv = to_i915(dev);
  13043. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13044. /* Clear any frame start delays used for debugging left by the BIOS */
  13045. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  13046. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13047. I915_WRITE(reg,
  13048. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13049. }
  13050. if (crtc->active) {
  13051. struct intel_plane *plane;
  13052. /* Disable everything but the primary plane */
  13053. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13054. const struct intel_plane_state *plane_state =
  13055. to_intel_plane_state(plane->base.state);
  13056. if (plane_state->base.visible &&
  13057. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  13058. intel_plane_disable_noatomic(crtc, plane);
  13059. }
  13060. }
  13061. /* Adjust the state of the output pipe according to whether we
  13062. * have active connectors/encoders. */
  13063. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13064. intel_crtc_disable_noatomic(&crtc->base, ctx);
  13065. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  13066. /*
  13067. * We start out with underrun reporting disabled to avoid races.
  13068. * For correct bookkeeping mark this on active crtcs.
  13069. *
  13070. * Also on gmch platforms we dont have any hardware bits to
  13071. * disable the underrun reporting. Which means we need to start
  13072. * out with underrun reporting disabled also on inactive pipes,
  13073. * since otherwise we'll complain about the garbage we read when
  13074. * e.g. coming up after runtime pm.
  13075. *
  13076. * No protection against concurrent access is required - at
  13077. * worst a fifo underrun happens which also sets this to false.
  13078. */
  13079. crtc->cpu_fifo_underrun_disabled = true;
  13080. /*
  13081. * We track the PCH trancoder underrun reporting state
  13082. * within the crtc. With crtc for pipe A housing the underrun
  13083. * reporting state for PCH transcoder A, crtc for pipe B housing
  13084. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  13085. * and marking underrun reporting as disabled for the non-existing
  13086. * PCH transcoders B and C would prevent enabling the south
  13087. * error interrupt (see cpt_can_enable_serr_int()).
  13088. */
  13089. if (has_pch_trancoder(dev_priv, crtc->pipe))
  13090. crtc->pch_fifo_underrun_disabled = true;
  13091. }
  13092. }
  13093. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13094. {
  13095. struct intel_connector *connector;
  13096. /* We need to check both for a crtc link (meaning that the
  13097. * encoder is active and trying to read from a pipe) and the
  13098. * pipe itself being active. */
  13099. bool has_active_crtc = encoder->base.crtc &&
  13100. to_intel_crtc(encoder->base.crtc)->active;
  13101. connector = intel_encoder_find_connector(encoder);
  13102. if (connector && !has_active_crtc) {
  13103. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13104. encoder->base.base.id,
  13105. encoder->base.name);
  13106. /* Connector is active, but has no active pipe. This is
  13107. * fallout from our resume register restoring. Disable
  13108. * the encoder manually again. */
  13109. if (encoder->base.crtc) {
  13110. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  13111. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13112. encoder->base.base.id,
  13113. encoder->base.name);
  13114. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  13115. if (encoder->post_disable)
  13116. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  13117. }
  13118. encoder->base.crtc = NULL;
  13119. /* Inconsistent output/port/pipe state happens presumably due to
  13120. * a bug in one of the get_hw_state functions. Or someplace else
  13121. * in our code, like the register restore mess on resume. Clamp
  13122. * things to off as a safer default. */
  13123. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13124. connector->base.encoder = NULL;
  13125. }
  13126. /* notify opregion of the sanitized encoder state */
  13127. intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
  13128. }
  13129. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  13130. {
  13131. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13132. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13133. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13134. i915_disable_vga(dev_priv);
  13135. }
  13136. }
  13137. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  13138. {
  13139. /* This function can be called both from intel_modeset_setup_hw_state or
  13140. * at a very early point in our resume sequence, where the power well
  13141. * structures are not yet restored. Since this function is at a very
  13142. * paranoid "someone might have enabled VGA while we were not looking"
  13143. * level, just check if the power well is enabled instead of trying to
  13144. * follow the "don't touch the power well if we don't need it" policy
  13145. * the rest of the driver uses. */
  13146. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13147. return;
  13148. i915_redisable_vga_power_on(dev_priv);
  13149. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13150. }
  13151. /* FIXME read out full plane state for all planes */
  13152. static void readout_plane_state(struct drm_i915_private *dev_priv)
  13153. {
  13154. struct intel_plane *plane;
  13155. struct intel_crtc *crtc;
  13156. for_each_intel_plane(&dev_priv->drm, plane) {
  13157. struct intel_plane_state *plane_state =
  13158. to_intel_plane_state(plane->base.state);
  13159. struct intel_crtc_state *crtc_state;
  13160. enum pipe pipe = PIPE_A;
  13161. bool visible;
  13162. visible = plane->get_hw_state(plane, &pipe);
  13163. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13164. crtc_state = to_intel_crtc_state(crtc->base.state);
  13165. intel_set_plane_visible(crtc_state, plane_state, visible);
  13166. }
  13167. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13168. struct intel_crtc_state *crtc_state =
  13169. to_intel_crtc_state(crtc->base.state);
  13170. fixup_active_planes(crtc_state);
  13171. }
  13172. }
  13173. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13174. {
  13175. struct drm_i915_private *dev_priv = to_i915(dev);
  13176. enum pipe pipe;
  13177. struct intel_crtc *crtc;
  13178. struct intel_encoder *encoder;
  13179. struct intel_connector *connector;
  13180. struct drm_connector_list_iter conn_iter;
  13181. int i;
  13182. dev_priv->active_crtcs = 0;
  13183. for_each_intel_crtc(dev, crtc) {
  13184. struct intel_crtc_state *crtc_state =
  13185. to_intel_crtc_state(crtc->base.state);
  13186. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13187. memset(crtc_state, 0, sizeof(*crtc_state));
  13188. crtc_state->base.crtc = &crtc->base;
  13189. crtc_state->base.active = crtc_state->base.enable =
  13190. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13191. crtc->base.enabled = crtc_state->base.enable;
  13192. crtc->active = crtc_state->base.active;
  13193. if (crtc_state->base.active)
  13194. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13195. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13196. crtc->base.base.id, crtc->base.name,
  13197. enableddisabled(crtc_state->base.active));
  13198. }
  13199. readout_plane_state(dev_priv);
  13200. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13201. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13202. pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
  13203. &pll->state.hw_state);
  13204. pll->state.crtc_mask = 0;
  13205. for_each_intel_crtc(dev, crtc) {
  13206. struct intel_crtc_state *crtc_state =
  13207. to_intel_crtc_state(crtc->base.state);
  13208. if (crtc_state->base.active &&
  13209. crtc_state->shared_dpll == pll)
  13210. pll->state.crtc_mask |= 1 << crtc->pipe;
  13211. }
  13212. pll->active_mask = pll->state.crtc_mask;
  13213. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13214. pll->info->name, pll->state.crtc_mask, pll->on);
  13215. }
  13216. for_each_intel_encoder(dev, encoder) {
  13217. pipe = 0;
  13218. if (encoder->get_hw_state(encoder, &pipe)) {
  13219. struct intel_crtc_state *crtc_state;
  13220. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13221. crtc_state = to_intel_crtc_state(crtc->base.state);
  13222. encoder->base.crtc = &crtc->base;
  13223. encoder->get_config(encoder, crtc_state);
  13224. } else {
  13225. encoder->base.crtc = NULL;
  13226. }
  13227. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13228. encoder->base.base.id, encoder->base.name,
  13229. enableddisabled(encoder->base.crtc),
  13230. pipe_name(pipe));
  13231. }
  13232. drm_connector_list_iter_begin(dev, &conn_iter);
  13233. for_each_intel_connector_iter(connector, &conn_iter) {
  13234. if (connector->get_hw_state(connector)) {
  13235. connector->base.dpms = DRM_MODE_DPMS_ON;
  13236. encoder = connector->encoder;
  13237. connector->base.encoder = &encoder->base;
  13238. if (encoder->base.crtc &&
  13239. encoder->base.crtc->state->active) {
  13240. /*
  13241. * This has to be done during hardware readout
  13242. * because anything calling .crtc_disable may
  13243. * rely on the connector_mask being accurate.
  13244. */
  13245. encoder->base.crtc->state->connector_mask |=
  13246. drm_connector_mask(&connector->base);
  13247. encoder->base.crtc->state->encoder_mask |=
  13248. drm_encoder_mask(&encoder->base);
  13249. }
  13250. } else {
  13251. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13252. connector->base.encoder = NULL;
  13253. }
  13254. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13255. connector->base.base.id, connector->base.name,
  13256. enableddisabled(connector->base.encoder));
  13257. }
  13258. drm_connector_list_iter_end(&conn_iter);
  13259. for_each_intel_crtc(dev, crtc) {
  13260. struct intel_crtc_state *crtc_state =
  13261. to_intel_crtc_state(crtc->base.state);
  13262. int min_cdclk = 0;
  13263. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13264. if (crtc_state->base.active) {
  13265. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  13266. crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
  13267. crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
  13268. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  13269. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13270. /*
  13271. * The initial mode needs to be set in order to keep
  13272. * the atomic core happy. It wants a valid mode if the
  13273. * crtc's enabled, so we do the above call.
  13274. *
  13275. * But we don't set all the derived state fully, hence
  13276. * set a flag to indicate that a full recalculation is
  13277. * needed on the next commit.
  13278. */
  13279. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  13280. intel_crtc_compute_pixel_rate(crtc_state);
  13281. if (dev_priv->display.modeset_calc_cdclk) {
  13282. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  13283. if (WARN_ON(min_cdclk < 0))
  13284. min_cdclk = 0;
  13285. }
  13286. drm_calc_timestamping_constants(&crtc->base,
  13287. &crtc_state->base.adjusted_mode);
  13288. update_scanline_offset(crtc);
  13289. }
  13290. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  13291. dev_priv->min_voltage_level[crtc->pipe] =
  13292. crtc_state->min_voltage_level;
  13293. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  13294. }
  13295. }
  13296. static void
  13297. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  13298. {
  13299. struct intel_encoder *encoder;
  13300. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13301. u64 get_domains;
  13302. enum intel_display_power_domain domain;
  13303. struct intel_crtc_state *crtc_state;
  13304. if (!encoder->get_power_domains)
  13305. continue;
  13306. /*
  13307. * MST-primary and inactive encoders don't have a crtc state
  13308. * and neither of these require any power domain references.
  13309. */
  13310. if (!encoder->base.crtc)
  13311. continue;
  13312. crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
  13313. get_domains = encoder->get_power_domains(encoder, crtc_state);
  13314. for_each_power_domain(domain, get_domains)
  13315. intel_display_power_get(dev_priv, domain);
  13316. }
  13317. }
  13318. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  13319. {
  13320. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  13321. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  13322. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  13323. DARBF_GATING_DIS);
  13324. if (IS_HASWELL(dev_priv)) {
  13325. /*
  13326. * WaRsPkgCStateDisplayPMReq:hsw
  13327. * System hang if this isn't done before disabling all planes!
  13328. */
  13329. I915_WRITE(CHICKEN_PAR1_1,
  13330. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  13331. }
  13332. }
  13333. /* Scan out the current hw modeset state,
  13334. * and sanitizes it to the current state
  13335. */
  13336. static void
  13337. intel_modeset_setup_hw_state(struct drm_device *dev,
  13338. struct drm_modeset_acquire_ctx *ctx)
  13339. {
  13340. struct drm_i915_private *dev_priv = to_i915(dev);
  13341. struct intel_crtc *crtc;
  13342. struct intel_encoder *encoder;
  13343. int i;
  13344. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  13345. intel_early_display_was(dev_priv);
  13346. intel_modeset_readout_hw_state(dev);
  13347. /* HW state is read out, now we need to sanitize this mess. */
  13348. get_encoder_power_domains(dev_priv);
  13349. /*
  13350. * intel_sanitize_plane_mapping() may need to do vblank
  13351. * waits, so we need vblank interrupts restored beforehand.
  13352. */
  13353. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13354. drm_crtc_vblank_reset(&crtc->base);
  13355. if (crtc->active)
  13356. drm_crtc_vblank_on(&crtc->base);
  13357. }
  13358. intel_sanitize_plane_mapping(dev_priv);
  13359. for_each_intel_encoder(dev, encoder)
  13360. intel_sanitize_encoder(encoder);
  13361. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13362. intel_sanitize_crtc(crtc, ctx);
  13363. intel_dump_pipe_config(crtc, crtc->config,
  13364. "[setup_hw_state]");
  13365. }
  13366. intel_modeset_update_connector_atomic_state(dev);
  13367. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13368. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13369. if (!pll->on || pll->active_mask)
  13370. continue;
  13371. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
  13372. pll->info->name);
  13373. pll->info->funcs->disable(dev_priv, pll);
  13374. pll->on = false;
  13375. }
  13376. if (IS_G4X(dev_priv)) {
  13377. g4x_wm_get_hw_state(dev);
  13378. g4x_wm_sanitize(dev_priv);
  13379. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13380. vlv_wm_get_hw_state(dev);
  13381. vlv_wm_sanitize(dev_priv);
  13382. } else if (INTEL_GEN(dev_priv) >= 9) {
  13383. skl_wm_get_hw_state(dev);
  13384. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13385. ilk_wm_get_hw_state(dev);
  13386. }
  13387. for_each_intel_crtc(dev, crtc) {
  13388. u64 put_domains;
  13389. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13390. if (WARN_ON(put_domains))
  13391. modeset_put_power_domains(dev_priv, put_domains);
  13392. }
  13393. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  13394. intel_fbc_init_pipe_state(dev_priv);
  13395. }
  13396. void intel_display_resume(struct drm_device *dev)
  13397. {
  13398. struct drm_i915_private *dev_priv = to_i915(dev);
  13399. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13400. struct drm_modeset_acquire_ctx ctx;
  13401. int ret;
  13402. dev_priv->modeset_restore_state = NULL;
  13403. if (state)
  13404. state->acquire_ctx = &ctx;
  13405. drm_modeset_acquire_init(&ctx, 0);
  13406. while (1) {
  13407. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13408. if (ret != -EDEADLK)
  13409. break;
  13410. drm_modeset_backoff(&ctx);
  13411. }
  13412. if (!ret)
  13413. ret = __intel_display_resume(dev, state, &ctx);
  13414. intel_enable_ipc(dev_priv);
  13415. drm_modeset_drop_locks(&ctx);
  13416. drm_modeset_acquire_fini(&ctx);
  13417. if (ret)
  13418. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13419. if (state)
  13420. drm_atomic_state_put(state);
  13421. }
  13422. int intel_connector_register(struct drm_connector *connector)
  13423. {
  13424. struct intel_connector *intel_connector = to_intel_connector(connector);
  13425. int ret;
  13426. ret = intel_backlight_device_register(intel_connector);
  13427. if (ret)
  13428. goto err;
  13429. return 0;
  13430. err:
  13431. return ret;
  13432. }
  13433. void intel_connector_unregister(struct drm_connector *connector)
  13434. {
  13435. struct intel_connector *intel_connector = to_intel_connector(connector);
  13436. intel_backlight_device_unregister(intel_connector);
  13437. intel_panel_destroy_backlight(connector);
  13438. }
  13439. static void intel_hpd_poll_fini(struct drm_device *dev)
  13440. {
  13441. struct intel_connector *connector;
  13442. struct drm_connector_list_iter conn_iter;
  13443. /* Kill all the work that may have been queued by hpd. */
  13444. drm_connector_list_iter_begin(dev, &conn_iter);
  13445. for_each_intel_connector_iter(connector, &conn_iter) {
  13446. if (connector->modeset_retry_work.func)
  13447. cancel_work_sync(&connector->modeset_retry_work);
  13448. if (connector->hdcp_shim) {
  13449. cancel_delayed_work_sync(&connector->hdcp_check_work);
  13450. cancel_work_sync(&connector->hdcp_prop_work);
  13451. }
  13452. }
  13453. drm_connector_list_iter_end(&conn_iter);
  13454. }
  13455. void intel_modeset_cleanup(struct drm_device *dev)
  13456. {
  13457. struct drm_i915_private *dev_priv = to_i915(dev);
  13458. flush_workqueue(dev_priv->modeset_wq);
  13459. flush_work(&dev_priv->atomic_helper.free_work);
  13460. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13461. /*
  13462. * Interrupts and polling as the first thing to avoid creating havoc.
  13463. * Too much stuff here (turning of connectors, ...) would
  13464. * experience fancy races otherwise.
  13465. */
  13466. intel_irq_uninstall(dev_priv);
  13467. /*
  13468. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13469. * poll handlers. Hence disable polling after hpd handling is shut down.
  13470. */
  13471. intel_hpd_poll_fini(dev);
  13472. /* poll work can call into fbdev, hence clean that up afterwards */
  13473. intel_fbdev_fini(dev_priv);
  13474. intel_unregister_dsm_handler();
  13475. intel_fbc_global_disable(dev_priv);
  13476. /* flush any delayed tasks or pending work */
  13477. flush_scheduled_work();
  13478. drm_mode_config_cleanup(dev);
  13479. intel_cleanup_overlay(dev_priv);
  13480. intel_teardown_gmbus(dev_priv);
  13481. destroy_workqueue(dev_priv->modeset_wq);
  13482. }
  13483. void intel_connector_attach_encoder(struct intel_connector *connector,
  13484. struct intel_encoder *encoder)
  13485. {
  13486. connector->encoder = encoder;
  13487. drm_connector_attach_encoder(&connector->base, &encoder->base);
  13488. }
  13489. /*
  13490. * set vga decode state - true == enable VGA decode
  13491. */
  13492. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13493. {
  13494. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13495. u16 gmch_ctrl;
  13496. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13497. DRM_ERROR("failed to read control word\n");
  13498. return -EIO;
  13499. }
  13500. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13501. return 0;
  13502. if (state)
  13503. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13504. else
  13505. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13506. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13507. DRM_ERROR("failed to write control word\n");
  13508. return -EIO;
  13509. }
  13510. return 0;
  13511. }
  13512. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13513. struct intel_display_error_state {
  13514. u32 power_well_driver;
  13515. int num_transcoders;
  13516. struct intel_cursor_error_state {
  13517. u32 control;
  13518. u32 position;
  13519. u32 base;
  13520. u32 size;
  13521. } cursor[I915_MAX_PIPES];
  13522. struct intel_pipe_error_state {
  13523. bool power_domain_on;
  13524. u32 source;
  13525. u32 stat;
  13526. } pipe[I915_MAX_PIPES];
  13527. struct intel_plane_error_state {
  13528. u32 control;
  13529. u32 stride;
  13530. u32 size;
  13531. u32 pos;
  13532. u32 addr;
  13533. u32 surface;
  13534. u32 tile_offset;
  13535. } plane[I915_MAX_PIPES];
  13536. struct intel_transcoder_error_state {
  13537. bool power_domain_on;
  13538. enum transcoder cpu_transcoder;
  13539. u32 conf;
  13540. u32 htotal;
  13541. u32 hblank;
  13542. u32 hsync;
  13543. u32 vtotal;
  13544. u32 vblank;
  13545. u32 vsync;
  13546. } transcoder[4];
  13547. };
  13548. struct intel_display_error_state *
  13549. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13550. {
  13551. struct intel_display_error_state *error;
  13552. int transcoders[] = {
  13553. TRANSCODER_A,
  13554. TRANSCODER_B,
  13555. TRANSCODER_C,
  13556. TRANSCODER_EDP,
  13557. };
  13558. int i;
  13559. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13560. return NULL;
  13561. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13562. if (error == NULL)
  13563. return NULL;
  13564. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13565. error->power_well_driver = I915_READ(HSW_PWR_WELL_CTL2);
  13566. for_each_pipe(dev_priv, i) {
  13567. error->pipe[i].power_domain_on =
  13568. __intel_display_power_is_enabled(dev_priv,
  13569. POWER_DOMAIN_PIPE(i));
  13570. if (!error->pipe[i].power_domain_on)
  13571. continue;
  13572. error->cursor[i].control = I915_READ(CURCNTR(i));
  13573. error->cursor[i].position = I915_READ(CURPOS(i));
  13574. error->cursor[i].base = I915_READ(CURBASE(i));
  13575. error->plane[i].control = I915_READ(DSPCNTR(i));
  13576. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13577. if (INTEL_GEN(dev_priv) <= 3) {
  13578. error->plane[i].size = I915_READ(DSPSIZE(i));
  13579. error->plane[i].pos = I915_READ(DSPPOS(i));
  13580. }
  13581. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13582. error->plane[i].addr = I915_READ(DSPADDR(i));
  13583. if (INTEL_GEN(dev_priv) >= 4) {
  13584. error->plane[i].surface = I915_READ(DSPSURF(i));
  13585. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13586. }
  13587. error->pipe[i].source = I915_READ(PIPESRC(i));
  13588. if (HAS_GMCH_DISPLAY(dev_priv))
  13589. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13590. }
  13591. /* Note: this does not include DSI transcoders. */
  13592. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13593. if (HAS_DDI(dev_priv))
  13594. error->num_transcoders++; /* Account for eDP. */
  13595. for (i = 0; i < error->num_transcoders; i++) {
  13596. enum transcoder cpu_transcoder = transcoders[i];
  13597. error->transcoder[i].power_domain_on =
  13598. __intel_display_power_is_enabled(dev_priv,
  13599. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13600. if (!error->transcoder[i].power_domain_on)
  13601. continue;
  13602. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13603. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13604. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13605. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13606. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13607. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13608. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13609. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13610. }
  13611. return error;
  13612. }
  13613. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13614. void
  13615. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13616. struct intel_display_error_state *error)
  13617. {
  13618. struct drm_i915_private *dev_priv = m->i915;
  13619. int i;
  13620. if (!error)
  13621. return;
  13622. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13623. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13624. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13625. error->power_well_driver);
  13626. for_each_pipe(dev_priv, i) {
  13627. err_printf(m, "Pipe [%d]:\n", i);
  13628. err_printf(m, " Power: %s\n",
  13629. onoff(error->pipe[i].power_domain_on));
  13630. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13631. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13632. err_printf(m, "Plane [%d]:\n", i);
  13633. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13634. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13635. if (INTEL_GEN(dev_priv) <= 3) {
  13636. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13637. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13638. }
  13639. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13640. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13641. if (INTEL_GEN(dev_priv) >= 4) {
  13642. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13643. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13644. }
  13645. err_printf(m, "Cursor [%d]:\n", i);
  13646. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13647. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13648. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13649. }
  13650. for (i = 0; i < error->num_transcoders; i++) {
  13651. err_printf(m, "CPU transcoder: %s\n",
  13652. transcoder_name(error->transcoder[i].cpu_transcoder));
  13653. err_printf(m, " Power: %s\n",
  13654. onoff(error->transcoder[i].power_domain_on));
  13655. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13656. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13657. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13658. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13659. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13660. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13661. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13662. }
  13663. }
  13664. #endif