intel_display.c 449 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint32_t skl_pri_planar_formats[] = {
  86. DRM_FORMAT_C8,
  87. DRM_FORMAT_RGB565,
  88. DRM_FORMAT_XRGB8888,
  89. DRM_FORMAT_XBGR8888,
  90. DRM_FORMAT_ARGB8888,
  91. DRM_FORMAT_ABGR8888,
  92. DRM_FORMAT_XRGB2101010,
  93. DRM_FORMAT_XBGR2101010,
  94. DRM_FORMAT_YUYV,
  95. DRM_FORMAT_YVYU,
  96. DRM_FORMAT_UYVY,
  97. DRM_FORMAT_VYUY,
  98. DRM_FORMAT_NV12,
  99. };
  100. static const uint64_t skl_format_modifiers_noccs[] = {
  101. I915_FORMAT_MOD_Yf_TILED,
  102. I915_FORMAT_MOD_Y_TILED,
  103. I915_FORMAT_MOD_X_TILED,
  104. DRM_FORMAT_MOD_LINEAR,
  105. DRM_FORMAT_MOD_INVALID
  106. };
  107. static const uint64_t skl_format_modifiers_ccs[] = {
  108. I915_FORMAT_MOD_Yf_TILED_CCS,
  109. I915_FORMAT_MOD_Y_TILED_CCS,
  110. I915_FORMAT_MOD_Yf_TILED,
  111. I915_FORMAT_MOD_Y_TILED,
  112. I915_FORMAT_MOD_X_TILED,
  113. DRM_FORMAT_MOD_LINEAR,
  114. DRM_FORMAT_MOD_INVALID
  115. };
  116. /* Cursor formats */
  117. static const uint32_t intel_cursor_formats[] = {
  118. DRM_FORMAT_ARGB8888,
  119. };
  120. static const uint64_t cursor_format_modifiers[] = {
  121. DRM_FORMAT_MOD_LINEAR,
  122. DRM_FORMAT_MOD_INVALID
  123. };
  124. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  125. struct intel_crtc_state *pipe_config);
  126. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  127. struct intel_crtc_state *pipe_config);
  128. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  129. struct drm_i915_gem_object *obj,
  130. struct drm_mode_fb_cmd2 *mode_cmd);
  131. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  132. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  133. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  134. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  135. struct intel_link_m_n *m_n,
  136. struct intel_link_m_n *m2_n2);
  137. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  138. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  139. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  140. static void vlv_prepare_pll(struct intel_crtc *crtc,
  141. const struct intel_crtc_state *pipe_config);
  142. static void chv_prepare_pll(struct intel_crtc *crtc,
  143. const struct intel_crtc_state *pipe_config);
  144. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  145. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  146. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  147. struct intel_crtc_state *crtc_state);
  148. static void skylake_pfit_enable(struct intel_crtc *crtc);
  149. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  150. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  151. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  152. struct drm_modeset_acquire_ctx *ctx);
  153. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  154. struct intel_limit {
  155. struct {
  156. int min, max;
  157. } dot, vco, n, m, m1, m2, p, p1;
  158. struct {
  159. int dot_limit;
  160. int p2_slow, p2_fast;
  161. } p2;
  162. };
  163. /* returns HPLL frequency in kHz */
  164. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  165. {
  166. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  167. /* Obtain SKU information */
  168. mutex_lock(&dev_priv->sb_lock);
  169. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  170. CCK_FUSE_HPLL_FREQ_MASK;
  171. mutex_unlock(&dev_priv->sb_lock);
  172. return vco_freq[hpll_freq] * 1000;
  173. }
  174. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  175. const char *name, u32 reg, int ref_freq)
  176. {
  177. u32 val;
  178. int divider;
  179. mutex_lock(&dev_priv->sb_lock);
  180. val = vlv_cck_read(dev_priv, reg);
  181. mutex_unlock(&dev_priv->sb_lock);
  182. divider = val & CCK_FREQUENCY_VALUES;
  183. WARN((val & CCK_FREQUENCY_STATUS) !=
  184. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  185. "%s change in progress\n", name);
  186. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  187. }
  188. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  189. const char *name, u32 reg)
  190. {
  191. if (dev_priv->hpll_freq == 0)
  192. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  193. return vlv_get_cck_clock(dev_priv, name, reg,
  194. dev_priv->hpll_freq);
  195. }
  196. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  197. {
  198. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  199. return;
  200. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  201. CCK_CZ_CLOCK_CONTROL);
  202. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  203. }
  204. static inline u32 /* units of 100MHz */
  205. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  206. const struct intel_crtc_state *pipe_config)
  207. {
  208. if (HAS_DDI(dev_priv))
  209. return pipe_config->port_clock; /* SPLL */
  210. else
  211. return dev_priv->fdi_pll_freq;
  212. }
  213. static const struct intel_limit intel_limits_i8xx_dac = {
  214. .dot = { .min = 25000, .max = 350000 },
  215. .vco = { .min = 908000, .max = 1512000 },
  216. .n = { .min = 2, .max = 16 },
  217. .m = { .min = 96, .max = 140 },
  218. .m1 = { .min = 18, .max = 26 },
  219. .m2 = { .min = 6, .max = 16 },
  220. .p = { .min = 4, .max = 128 },
  221. .p1 = { .min = 2, .max = 33 },
  222. .p2 = { .dot_limit = 165000,
  223. .p2_slow = 4, .p2_fast = 2 },
  224. };
  225. static const struct intel_limit intel_limits_i8xx_dvo = {
  226. .dot = { .min = 25000, .max = 350000 },
  227. .vco = { .min = 908000, .max = 1512000 },
  228. .n = { .min = 2, .max = 16 },
  229. .m = { .min = 96, .max = 140 },
  230. .m1 = { .min = 18, .max = 26 },
  231. .m2 = { .min = 6, .max = 16 },
  232. .p = { .min = 4, .max = 128 },
  233. .p1 = { .min = 2, .max = 33 },
  234. .p2 = { .dot_limit = 165000,
  235. .p2_slow = 4, .p2_fast = 4 },
  236. };
  237. static const struct intel_limit intel_limits_i8xx_lvds = {
  238. .dot = { .min = 25000, .max = 350000 },
  239. .vco = { .min = 908000, .max = 1512000 },
  240. .n = { .min = 2, .max = 16 },
  241. .m = { .min = 96, .max = 140 },
  242. .m1 = { .min = 18, .max = 26 },
  243. .m2 = { .min = 6, .max = 16 },
  244. .p = { .min = 4, .max = 128 },
  245. .p1 = { .min = 1, .max = 6 },
  246. .p2 = { .dot_limit = 165000,
  247. .p2_slow = 14, .p2_fast = 7 },
  248. };
  249. static const struct intel_limit intel_limits_i9xx_sdvo = {
  250. .dot = { .min = 20000, .max = 400000 },
  251. .vco = { .min = 1400000, .max = 2800000 },
  252. .n = { .min = 1, .max = 6 },
  253. .m = { .min = 70, .max = 120 },
  254. .m1 = { .min = 8, .max = 18 },
  255. .m2 = { .min = 3, .max = 7 },
  256. .p = { .min = 5, .max = 80 },
  257. .p1 = { .min = 1, .max = 8 },
  258. .p2 = { .dot_limit = 200000,
  259. .p2_slow = 10, .p2_fast = 5 },
  260. };
  261. static const struct intel_limit intel_limits_i9xx_lvds = {
  262. .dot = { .min = 20000, .max = 400000 },
  263. .vco = { .min = 1400000, .max = 2800000 },
  264. .n = { .min = 1, .max = 6 },
  265. .m = { .min = 70, .max = 120 },
  266. .m1 = { .min = 8, .max = 18 },
  267. .m2 = { .min = 3, .max = 7 },
  268. .p = { .min = 7, .max = 98 },
  269. .p1 = { .min = 1, .max = 8 },
  270. .p2 = { .dot_limit = 112000,
  271. .p2_slow = 14, .p2_fast = 7 },
  272. };
  273. static const struct intel_limit intel_limits_g4x_sdvo = {
  274. .dot = { .min = 25000, .max = 270000 },
  275. .vco = { .min = 1750000, .max = 3500000},
  276. .n = { .min = 1, .max = 4 },
  277. .m = { .min = 104, .max = 138 },
  278. .m1 = { .min = 17, .max = 23 },
  279. .m2 = { .min = 5, .max = 11 },
  280. .p = { .min = 10, .max = 30 },
  281. .p1 = { .min = 1, .max = 3},
  282. .p2 = { .dot_limit = 270000,
  283. .p2_slow = 10,
  284. .p2_fast = 10
  285. },
  286. };
  287. static const struct intel_limit intel_limits_g4x_hdmi = {
  288. .dot = { .min = 22000, .max = 400000 },
  289. .vco = { .min = 1750000, .max = 3500000},
  290. .n = { .min = 1, .max = 4 },
  291. .m = { .min = 104, .max = 138 },
  292. .m1 = { .min = 16, .max = 23 },
  293. .m2 = { .min = 5, .max = 11 },
  294. .p = { .min = 5, .max = 80 },
  295. .p1 = { .min = 1, .max = 8},
  296. .p2 = { .dot_limit = 165000,
  297. .p2_slow = 10, .p2_fast = 5 },
  298. };
  299. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  300. .dot = { .min = 20000, .max = 115000 },
  301. .vco = { .min = 1750000, .max = 3500000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 104, .max = 138 },
  304. .m1 = { .min = 17, .max = 23 },
  305. .m2 = { .min = 5, .max = 11 },
  306. .p = { .min = 28, .max = 112 },
  307. .p1 = { .min = 2, .max = 8 },
  308. .p2 = { .dot_limit = 0,
  309. .p2_slow = 14, .p2_fast = 14
  310. },
  311. };
  312. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  313. .dot = { .min = 80000, .max = 224000 },
  314. .vco = { .min = 1750000, .max = 3500000 },
  315. .n = { .min = 1, .max = 3 },
  316. .m = { .min = 104, .max = 138 },
  317. .m1 = { .min = 17, .max = 23 },
  318. .m2 = { .min = 5, .max = 11 },
  319. .p = { .min = 14, .max = 42 },
  320. .p1 = { .min = 2, .max = 6 },
  321. .p2 = { .dot_limit = 0,
  322. .p2_slow = 7, .p2_fast = 7
  323. },
  324. };
  325. static const struct intel_limit intel_limits_pineview_sdvo = {
  326. .dot = { .min = 20000, .max = 400000},
  327. .vco = { .min = 1700000, .max = 3500000 },
  328. /* Pineview's Ncounter is a ring counter */
  329. .n = { .min = 3, .max = 6 },
  330. .m = { .min = 2, .max = 256 },
  331. /* Pineview only has one combined m divider, which we treat as m2. */
  332. .m1 = { .min = 0, .max = 0 },
  333. .m2 = { .min = 0, .max = 254 },
  334. .p = { .min = 5, .max = 80 },
  335. .p1 = { .min = 1, .max = 8 },
  336. .p2 = { .dot_limit = 200000,
  337. .p2_slow = 10, .p2_fast = 5 },
  338. };
  339. static const struct intel_limit intel_limits_pineview_lvds = {
  340. .dot = { .min = 20000, .max = 400000 },
  341. .vco = { .min = 1700000, .max = 3500000 },
  342. .n = { .min = 3, .max = 6 },
  343. .m = { .min = 2, .max = 256 },
  344. .m1 = { .min = 0, .max = 0 },
  345. .m2 = { .min = 0, .max = 254 },
  346. .p = { .min = 7, .max = 112 },
  347. .p1 = { .min = 1, .max = 8 },
  348. .p2 = { .dot_limit = 112000,
  349. .p2_slow = 14, .p2_fast = 14 },
  350. };
  351. /* Ironlake / Sandybridge
  352. *
  353. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  354. * the range value for them is (actual_value - 2).
  355. */
  356. static const struct intel_limit intel_limits_ironlake_dac = {
  357. .dot = { .min = 25000, .max = 350000 },
  358. .vco = { .min = 1760000, .max = 3510000 },
  359. .n = { .min = 1, .max = 5 },
  360. .m = { .min = 79, .max = 127 },
  361. .m1 = { .min = 12, .max = 22 },
  362. .m2 = { .min = 5, .max = 9 },
  363. .p = { .min = 5, .max = 80 },
  364. .p1 = { .min = 1, .max = 8 },
  365. .p2 = { .dot_limit = 225000,
  366. .p2_slow = 10, .p2_fast = 5 },
  367. };
  368. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  369. .dot = { .min = 25000, .max = 350000 },
  370. .vco = { .min = 1760000, .max = 3510000 },
  371. .n = { .min = 1, .max = 3 },
  372. .m = { .min = 79, .max = 118 },
  373. .m1 = { .min = 12, .max = 22 },
  374. .m2 = { .min = 5, .max = 9 },
  375. .p = { .min = 28, .max = 112 },
  376. .p1 = { .min = 2, .max = 8 },
  377. .p2 = { .dot_limit = 225000,
  378. .p2_slow = 14, .p2_fast = 14 },
  379. };
  380. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  381. .dot = { .min = 25000, .max = 350000 },
  382. .vco = { .min = 1760000, .max = 3510000 },
  383. .n = { .min = 1, .max = 3 },
  384. .m = { .min = 79, .max = 127 },
  385. .m1 = { .min = 12, .max = 22 },
  386. .m2 = { .min = 5, .max = 9 },
  387. .p = { .min = 14, .max = 56 },
  388. .p1 = { .min = 2, .max = 8 },
  389. .p2 = { .dot_limit = 225000,
  390. .p2_slow = 7, .p2_fast = 7 },
  391. };
  392. /* LVDS 100mhz refclk limits. */
  393. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  394. .dot = { .min = 25000, .max = 350000 },
  395. .vco = { .min = 1760000, .max = 3510000 },
  396. .n = { .min = 1, .max = 2 },
  397. .m = { .min = 79, .max = 126 },
  398. .m1 = { .min = 12, .max = 22 },
  399. .m2 = { .min = 5, .max = 9 },
  400. .p = { .min = 28, .max = 112 },
  401. .p1 = { .min = 2, .max = 8 },
  402. .p2 = { .dot_limit = 225000,
  403. .p2_slow = 14, .p2_fast = 14 },
  404. };
  405. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  406. .dot = { .min = 25000, .max = 350000 },
  407. .vco = { .min = 1760000, .max = 3510000 },
  408. .n = { .min = 1, .max = 3 },
  409. .m = { .min = 79, .max = 126 },
  410. .m1 = { .min = 12, .max = 22 },
  411. .m2 = { .min = 5, .max = 9 },
  412. .p = { .min = 14, .max = 42 },
  413. .p1 = { .min = 2, .max = 6 },
  414. .p2 = { .dot_limit = 225000,
  415. .p2_slow = 7, .p2_fast = 7 },
  416. };
  417. static const struct intel_limit intel_limits_vlv = {
  418. /*
  419. * These are the data rate limits (measured in fast clocks)
  420. * since those are the strictest limits we have. The fast
  421. * clock and actual rate limits are more relaxed, so checking
  422. * them would make no difference.
  423. */
  424. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  425. .vco = { .min = 4000000, .max = 6000000 },
  426. .n = { .min = 1, .max = 7 },
  427. .m1 = { .min = 2, .max = 3 },
  428. .m2 = { .min = 11, .max = 156 },
  429. .p1 = { .min = 2, .max = 3 },
  430. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  431. };
  432. static const struct intel_limit intel_limits_chv = {
  433. /*
  434. * These are the data rate limits (measured in fast clocks)
  435. * since those are the strictest limits we have. The fast
  436. * clock and actual rate limits are more relaxed, so checking
  437. * them would make no difference.
  438. */
  439. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  440. .vco = { .min = 4800000, .max = 6480000 },
  441. .n = { .min = 1, .max = 1 },
  442. .m1 = { .min = 2, .max = 2 },
  443. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  444. .p1 = { .min = 2, .max = 4 },
  445. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  446. };
  447. static const struct intel_limit intel_limits_bxt = {
  448. /* FIXME: find real dot limits */
  449. .dot = { .min = 0, .max = INT_MAX },
  450. .vco = { .min = 4800000, .max = 6700000 },
  451. .n = { .min = 1, .max = 1 },
  452. .m1 = { .min = 2, .max = 2 },
  453. /* FIXME: find real m2 limits */
  454. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  455. .p1 = { .min = 2, .max = 4 },
  456. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  457. };
  458. static void
  459. skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
  460. {
  461. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  462. return;
  463. if (enable)
  464. I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
  465. else
  466. I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
  467. }
  468. static void
  469. skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
  470. {
  471. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  472. return;
  473. if (enable)
  474. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  475. DUPS1_GATING_DIS | DUPS2_GATING_DIS);
  476. else
  477. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  478. I915_READ(CLKGATE_DIS_PSL(pipe)) &
  479. ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
  480. }
  481. static bool
  482. needs_modeset(const struct drm_crtc_state *state)
  483. {
  484. return drm_atomic_crtc_needs_modeset(state);
  485. }
  486. /*
  487. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  488. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  489. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  490. * The helpers' return value is the rate of the clock that is fed to the
  491. * display engine's pipe which can be the above fast dot clock rate or a
  492. * divided-down version of it.
  493. */
  494. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  495. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  496. {
  497. clock->m = clock->m2 + 2;
  498. clock->p = clock->p1 * clock->p2;
  499. if (WARN_ON(clock->n == 0 || clock->p == 0))
  500. return 0;
  501. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  502. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  503. return clock->dot;
  504. }
  505. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  506. {
  507. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  508. }
  509. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  510. {
  511. clock->m = i9xx_dpll_compute_m(clock);
  512. clock->p = clock->p1 * clock->p2;
  513. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  514. return 0;
  515. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  516. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  517. return clock->dot;
  518. }
  519. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  520. {
  521. clock->m = clock->m1 * clock->m2;
  522. clock->p = clock->p1 * clock->p2;
  523. if (WARN_ON(clock->n == 0 || clock->p == 0))
  524. return 0;
  525. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  526. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  527. return clock->dot / 5;
  528. }
  529. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  530. {
  531. clock->m = clock->m1 * clock->m2;
  532. clock->p = clock->p1 * clock->p2;
  533. if (WARN_ON(clock->n == 0 || clock->p == 0))
  534. return 0;
  535. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  536. clock->n << 22);
  537. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  538. return clock->dot / 5;
  539. }
  540. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  541. /*
  542. * Returns whether the given set of divisors are valid for a given refclk with
  543. * the given connectors.
  544. */
  545. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  546. const struct intel_limit *limit,
  547. const struct dpll *clock)
  548. {
  549. if (clock->n < limit->n.min || limit->n.max < clock->n)
  550. INTELPllInvalid("n out of range\n");
  551. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  552. INTELPllInvalid("p1 out of range\n");
  553. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  554. INTELPllInvalid("m2 out of range\n");
  555. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  556. INTELPllInvalid("m1 out of range\n");
  557. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  558. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  559. if (clock->m1 <= clock->m2)
  560. INTELPllInvalid("m1 <= m2\n");
  561. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  562. !IS_GEN9_LP(dev_priv)) {
  563. if (clock->p < limit->p.min || limit->p.max < clock->p)
  564. INTELPllInvalid("p out of range\n");
  565. if (clock->m < limit->m.min || limit->m.max < clock->m)
  566. INTELPllInvalid("m out of range\n");
  567. }
  568. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  569. INTELPllInvalid("vco out of range\n");
  570. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  571. * connector, etc., rather than just a single range.
  572. */
  573. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  574. INTELPllInvalid("dot out of range\n");
  575. return true;
  576. }
  577. static int
  578. i9xx_select_p2_div(const struct intel_limit *limit,
  579. const struct intel_crtc_state *crtc_state,
  580. int target)
  581. {
  582. struct drm_device *dev = crtc_state->base.crtc->dev;
  583. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  584. /*
  585. * For LVDS just rely on its current settings for dual-channel.
  586. * We haven't figured out how to reliably set up different
  587. * single/dual channel state, if we even can.
  588. */
  589. if (intel_is_dual_link_lvds(dev))
  590. return limit->p2.p2_fast;
  591. else
  592. return limit->p2.p2_slow;
  593. } else {
  594. if (target < limit->p2.dot_limit)
  595. return limit->p2.p2_slow;
  596. else
  597. return limit->p2.p2_fast;
  598. }
  599. }
  600. /*
  601. * Returns a set of divisors for the desired target clock with the given
  602. * refclk, or FALSE. The returned values represent the clock equation:
  603. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  604. *
  605. * Target and reference clocks are specified in kHz.
  606. *
  607. * If match_clock is provided, then best_clock P divider must match the P
  608. * divider from @match_clock used for LVDS downclocking.
  609. */
  610. static bool
  611. i9xx_find_best_dpll(const struct intel_limit *limit,
  612. struct intel_crtc_state *crtc_state,
  613. int target, int refclk, struct dpll *match_clock,
  614. struct dpll *best_clock)
  615. {
  616. struct drm_device *dev = crtc_state->base.crtc->dev;
  617. struct dpll clock;
  618. int err = target;
  619. memset(best_clock, 0, sizeof(*best_clock));
  620. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  621. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  622. clock.m1++) {
  623. for (clock.m2 = limit->m2.min;
  624. clock.m2 <= limit->m2.max; clock.m2++) {
  625. if (clock.m2 >= clock.m1)
  626. break;
  627. for (clock.n = limit->n.min;
  628. clock.n <= limit->n.max; clock.n++) {
  629. for (clock.p1 = limit->p1.min;
  630. clock.p1 <= limit->p1.max; clock.p1++) {
  631. int this_err;
  632. i9xx_calc_dpll_params(refclk, &clock);
  633. if (!intel_PLL_is_valid(to_i915(dev),
  634. limit,
  635. &clock))
  636. continue;
  637. if (match_clock &&
  638. clock.p != match_clock->p)
  639. continue;
  640. this_err = abs(clock.dot - target);
  641. if (this_err < err) {
  642. *best_clock = clock;
  643. err = this_err;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. return (err != target);
  650. }
  651. /*
  652. * Returns a set of divisors for the desired target clock with the given
  653. * refclk, or FALSE. The returned values represent the clock equation:
  654. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  655. *
  656. * Target and reference clocks are specified in kHz.
  657. *
  658. * If match_clock is provided, then best_clock P divider must match the P
  659. * divider from @match_clock used for LVDS downclocking.
  660. */
  661. static bool
  662. pnv_find_best_dpll(const struct intel_limit *limit,
  663. struct intel_crtc_state *crtc_state,
  664. int target, int refclk, struct dpll *match_clock,
  665. struct dpll *best_clock)
  666. {
  667. struct drm_device *dev = crtc_state->base.crtc->dev;
  668. struct dpll clock;
  669. int err = target;
  670. memset(best_clock, 0, sizeof(*best_clock));
  671. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  672. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  673. clock.m1++) {
  674. for (clock.m2 = limit->m2.min;
  675. clock.m2 <= limit->m2.max; clock.m2++) {
  676. for (clock.n = limit->n.min;
  677. clock.n <= limit->n.max; clock.n++) {
  678. for (clock.p1 = limit->p1.min;
  679. clock.p1 <= limit->p1.max; clock.p1++) {
  680. int this_err;
  681. pnv_calc_dpll_params(refclk, &clock);
  682. if (!intel_PLL_is_valid(to_i915(dev),
  683. limit,
  684. &clock))
  685. continue;
  686. if (match_clock &&
  687. clock.p != match_clock->p)
  688. continue;
  689. this_err = abs(clock.dot - target);
  690. if (this_err < err) {
  691. *best_clock = clock;
  692. err = this_err;
  693. }
  694. }
  695. }
  696. }
  697. }
  698. return (err != target);
  699. }
  700. /*
  701. * Returns a set of divisors for the desired target clock with the given
  702. * refclk, or FALSE. The returned values represent the clock equation:
  703. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  704. *
  705. * Target and reference clocks are specified in kHz.
  706. *
  707. * If match_clock is provided, then best_clock P divider must match the P
  708. * divider from @match_clock used for LVDS downclocking.
  709. */
  710. static bool
  711. g4x_find_best_dpll(const struct intel_limit *limit,
  712. struct intel_crtc_state *crtc_state,
  713. int target, int refclk, struct dpll *match_clock,
  714. struct dpll *best_clock)
  715. {
  716. struct drm_device *dev = crtc_state->base.crtc->dev;
  717. struct dpll clock;
  718. int max_n;
  719. bool found = false;
  720. /* approximately equals target * 0.00585 */
  721. int err_most = (target >> 8) + (target >> 9);
  722. memset(best_clock, 0, sizeof(*best_clock));
  723. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  724. max_n = limit->n.max;
  725. /* based on hardware requirement, prefer smaller n to precision */
  726. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  727. /* based on hardware requirement, prefere larger m1,m2 */
  728. for (clock.m1 = limit->m1.max;
  729. clock.m1 >= limit->m1.min; clock.m1--) {
  730. for (clock.m2 = limit->m2.max;
  731. clock.m2 >= limit->m2.min; clock.m2--) {
  732. for (clock.p1 = limit->p1.max;
  733. clock.p1 >= limit->p1.min; clock.p1--) {
  734. int this_err;
  735. i9xx_calc_dpll_params(refclk, &clock);
  736. if (!intel_PLL_is_valid(to_i915(dev),
  737. limit,
  738. &clock))
  739. continue;
  740. this_err = abs(clock.dot - target);
  741. if (this_err < err_most) {
  742. *best_clock = clock;
  743. err_most = this_err;
  744. max_n = clock.n;
  745. found = true;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. return found;
  752. }
  753. /*
  754. * Check if the calculated PLL configuration is more optimal compared to the
  755. * best configuration and error found so far. Return the calculated error.
  756. */
  757. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  758. const struct dpll *calculated_clock,
  759. const struct dpll *best_clock,
  760. unsigned int best_error_ppm,
  761. unsigned int *error_ppm)
  762. {
  763. /*
  764. * For CHV ignore the error and consider only the P value.
  765. * Prefer a bigger P value based on HW requirements.
  766. */
  767. if (IS_CHERRYVIEW(to_i915(dev))) {
  768. *error_ppm = 0;
  769. return calculated_clock->p > best_clock->p;
  770. }
  771. if (WARN_ON_ONCE(!target_freq))
  772. return false;
  773. *error_ppm = div_u64(1000000ULL *
  774. abs(target_freq - calculated_clock->dot),
  775. target_freq);
  776. /*
  777. * Prefer a better P value over a better (smaller) error if the error
  778. * is small. Ensure this preference for future configurations too by
  779. * setting the error to 0.
  780. */
  781. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  782. *error_ppm = 0;
  783. return true;
  784. }
  785. return *error_ppm + 10 < best_error_ppm;
  786. }
  787. /*
  788. * Returns a set of divisors for the desired target clock with the given
  789. * refclk, or FALSE. The returned values represent the clock equation:
  790. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  791. */
  792. static bool
  793. vlv_find_best_dpll(const struct intel_limit *limit,
  794. struct intel_crtc_state *crtc_state,
  795. int target, int refclk, struct dpll *match_clock,
  796. struct dpll *best_clock)
  797. {
  798. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  799. struct drm_device *dev = crtc->base.dev;
  800. struct dpll clock;
  801. unsigned int bestppm = 1000000;
  802. /* min update 19.2 MHz */
  803. int max_n = min(limit->n.max, refclk / 19200);
  804. bool found = false;
  805. target *= 5; /* fast clock */
  806. memset(best_clock, 0, sizeof(*best_clock));
  807. /* based on hardware requirement, prefer smaller n to precision */
  808. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  809. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  810. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  811. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  812. clock.p = clock.p1 * clock.p2;
  813. /* based on hardware requirement, prefer bigger m1,m2 values */
  814. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  815. unsigned int ppm;
  816. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  817. refclk * clock.m1);
  818. vlv_calc_dpll_params(refclk, &clock);
  819. if (!intel_PLL_is_valid(to_i915(dev),
  820. limit,
  821. &clock))
  822. continue;
  823. if (!vlv_PLL_is_optimal(dev, target,
  824. &clock,
  825. best_clock,
  826. bestppm, &ppm))
  827. continue;
  828. *best_clock = clock;
  829. bestppm = ppm;
  830. found = true;
  831. }
  832. }
  833. }
  834. }
  835. return found;
  836. }
  837. /*
  838. * Returns a set of divisors for the desired target clock with the given
  839. * refclk, or FALSE. The returned values represent the clock equation:
  840. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  841. */
  842. static bool
  843. chv_find_best_dpll(const struct intel_limit *limit,
  844. struct intel_crtc_state *crtc_state,
  845. int target, int refclk, struct dpll *match_clock,
  846. struct dpll *best_clock)
  847. {
  848. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  849. struct drm_device *dev = crtc->base.dev;
  850. unsigned int best_error_ppm;
  851. struct dpll clock;
  852. uint64_t m2;
  853. int found = false;
  854. memset(best_clock, 0, sizeof(*best_clock));
  855. best_error_ppm = 1000000;
  856. /*
  857. * Based on hardware doc, the n always set to 1, and m1 always
  858. * set to 2. If requires to support 200Mhz refclk, we need to
  859. * revisit this because n may not 1 anymore.
  860. */
  861. clock.n = 1, clock.m1 = 2;
  862. target *= 5; /* fast clock */
  863. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  864. for (clock.p2 = limit->p2.p2_fast;
  865. clock.p2 >= limit->p2.p2_slow;
  866. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  867. unsigned int error_ppm;
  868. clock.p = clock.p1 * clock.p2;
  869. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  870. clock.n) << 22, refclk * clock.m1);
  871. if (m2 > INT_MAX/clock.m1)
  872. continue;
  873. clock.m2 = m2;
  874. chv_calc_dpll_params(refclk, &clock);
  875. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  876. continue;
  877. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  878. best_error_ppm, &error_ppm))
  879. continue;
  880. *best_clock = clock;
  881. best_error_ppm = error_ppm;
  882. found = true;
  883. }
  884. }
  885. return found;
  886. }
  887. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  888. struct dpll *best_clock)
  889. {
  890. int refclk = 100000;
  891. const struct intel_limit *limit = &intel_limits_bxt;
  892. return chv_find_best_dpll(limit, crtc_state,
  893. target_clock, refclk, NULL, best_clock);
  894. }
  895. bool intel_crtc_active(struct intel_crtc *crtc)
  896. {
  897. /* Be paranoid as we can arrive here with only partial
  898. * state retrieved from the hardware during setup.
  899. *
  900. * We can ditch the adjusted_mode.crtc_clock check as soon
  901. * as Haswell has gained clock readout/fastboot support.
  902. *
  903. * We can ditch the crtc->primary->fb check as soon as we can
  904. * properly reconstruct framebuffers.
  905. *
  906. * FIXME: The intel_crtc->active here should be switched to
  907. * crtc->state->active once we have proper CRTC states wired up
  908. * for atomic.
  909. */
  910. return crtc->active && crtc->base.primary->state->fb &&
  911. crtc->config->base.adjusted_mode.crtc_clock;
  912. }
  913. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  914. enum pipe pipe)
  915. {
  916. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  917. return crtc->config->cpu_transcoder;
  918. }
  919. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  920. enum pipe pipe)
  921. {
  922. i915_reg_t reg = PIPEDSL(pipe);
  923. u32 line1, line2;
  924. u32 line_mask;
  925. if (IS_GEN2(dev_priv))
  926. line_mask = DSL_LINEMASK_GEN2;
  927. else
  928. line_mask = DSL_LINEMASK_GEN3;
  929. line1 = I915_READ(reg) & line_mask;
  930. msleep(5);
  931. line2 = I915_READ(reg) & line_mask;
  932. return line1 != line2;
  933. }
  934. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  935. {
  936. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  937. enum pipe pipe = crtc->pipe;
  938. /* Wait for the display line to settle/start moving */
  939. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  940. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  941. pipe_name(pipe), onoff(state));
  942. }
  943. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  944. {
  945. wait_for_pipe_scanline_moving(crtc, false);
  946. }
  947. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  948. {
  949. wait_for_pipe_scanline_moving(crtc, true);
  950. }
  951. static void
  952. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  953. {
  954. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  955. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  956. if (INTEL_GEN(dev_priv) >= 4) {
  957. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  958. i915_reg_t reg = PIPECONF(cpu_transcoder);
  959. /* Wait for the Pipe State to go off */
  960. if (intel_wait_for_register(dev_priv,
  961. reg, I965_PIPECONF_ACTIVE, 0,
  962. 100))
  963. WARN(1, "pipe_off wait timed out\n");
  964. } else {
  965. intel_wait_for_pipe_scanline_stopped(crtc);
  966. }
  967. }
  968. /* Only for pre-ILK configs */
  969. void assert_pll(struct drm_i915_private *dev_priv,
  970. enum pipe pipe, bool state)
  971. {
  972. u32 val;
  973. bool cur_state;
  974. val = I915_READ(DPLL(pipe));
  975. cur_state = !!(val & DPLL_VCO_ENABLE);
  976. I915_STATE_WARN(cur_state != state,
  977. "PLL state assertion failure (expected %s, current %s)\n",
  978. onoff(state), onoff(cur_state));
  979. }
  980. /* XXX: the dsi pll is shared between MIPI DSI ports */
  981. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. mutex_lock(&dev_priv->sb_lock);
  986. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  987. mutex_unlock(&dev_priv->sb_lock);
  988. cur_state = val & DSI_PLL_VCO_EN;
  989. I915_STATE_WARN(cur_state != state,
  990. "DSI PLL state assertion failure (expected %s, current %s)\n",
  991. onoff(state), onoff(cur_state));
  992. }
  993. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  994. enum pipe pipe, bool state)
  995. {
  996. bool cur_state;
  997. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  998. pipe);
  999. if (HAS_DDI(dev_priv)) {
  1000. /* DDI does not have a specific FDI_TX register */
  1001. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1002. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1003. } else {
  1004. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1005. cur_state = !!(val & FDI_TX_ENABLE);
  1006. }
  1007. I915_STATE_WARN(cur_state != state,
  1008. "FDI TX state assertion failure (expected %s, current %s)\n",
  1009. onoff(state), onoff(cur_state));
  1010. }
  1011. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1012. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1013. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1014. enum pipe pipe, bool state)
  1015. {
  1016. u32 val;
  1017. bool cur_state;
  1018. val = I915_READ(FDI_RX_CTL(pipe));
  1019. cur_state = !!(val & FDI_RX_ENABLE);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "FDI RX state assertion failure (expected %s, current %s)\n",
  1022. onoff(state), onoff(cur_state));
  1023. }
  1024. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1025. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1026. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1027. enum pipe pipe)
  1028. {
  1029. u32 val;
  1030. /* ILK FDI PLL is always enabled */
  1031. if (IS_GEN5(dev_priv))
  1032. return;
  1033. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1034. if (HAS_DDI(dev_priv))
  1035. return;
  1036. val = I915_READ(FDI_TX_CTL(pipe));
  1037. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1038. }
  1039. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1040. enum pipe pipe, bool state)
  1041. {
  1042. u32 val;
  1043. bool cur_state;
  1044. val = I915_READ(FDI_RX_CTL(pipe));
  1045. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1046. I915_STATE_WARN(cur_state != state,
  1047. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1048. onoff(state), onoff(cur_state));
  1049. }
  1050. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1051. {
  1052. i915_reg_t pp_reg;
  1053. u32 val;
  1054. enum pipe panel_pipe = PIPE_A;
  1055. bool locked = true;
  1056. if (WARN_ON(HAS_DDI(dev_priv)))
  1057. return;
  1058. if (HAS_PCH_SPLIT(dev_priv)) {
  1059. u32 port_sel;
  1060. pp_reg = PP_CONTROL(0);
  1061. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1062. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1063. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1064. panel_pipe = PIPE_B;
  1065. /* XXX: else fix for eDP */
  1066. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1067. /* presumably write lock depends on pipe, not port select */
  1068. pp_reg = PP_CONTROL(pipe);
  1069. panel_pipe = pipe;
  1070. } else {
  1071. pp_reg = PP_CONTROL(0);
  1072. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1073. panel_pipe = PIPE_B;
  1074. }
  1075. val = I915_READ(pp_reg);
  1076. if (!(val & PANEL_POWER_ON) ||
  1077. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1078. locked = false;
  1079. I915_STATE_WARN(panel_pipe == pipe && locked,
  1080. "panel assertion failure, pipe %c regs locked\n",
  1081. pipe_name(pipe));
  1082. }
  1083. void assert_pipe(struct drm_i915_private *dev_priv,
  1084. enum pipe pipe, bool state)
  1085. {
  1086. bool cur_state;
  1087. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1088. pipe);
  1089. enum intel_display_power_domain power_domain;
  1090. /* we keep both pipes enabled on 830 */
  1091. if (IS_I830(dev_priv))
  1092. state = true;
  1093. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1094. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1095. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1096. cur_state = !!(val & PIPECONF_ENABLE);
  1097. intel_display_power_put(dev_priv, power_domain);
  1098. } else {
  1099. cur_state = false;
  1100. }
  1101. I915_STATE_WARN(cur_state != state,
  1102. "pipe %c assertion failure (expected %s, current %s)\n",
  1103. pipe_name(pipe), onoff(state), onoff(cur_state));
  1104. }
  1105. static void assert_plane(struct intel_plane *plane, bool state)
  1106. {
  1107. bool cur_state = plane->get_hw_state(plane);
  1108. I915_STATE_WARN(cur_state != state,
  1109. "%s assertion failure (expected %s, current %s)\n",
  1110. plane->base.name, onoff(state), onoff(cur_state));
  1111. }
  1112. #define assert_plane_enabled(p) assert_plane(p, true)
  1113. #define assert_plane_disabled(p) assert_plane(p, false)
  1114. static void assert_planes_disabled(struct intel_crtc *crtc)
  1115. {
  1116. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1117. struct intel_plane *plane;
  1118. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1119. assert_plane_disabled(plane);
  1120. }
  1121. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1122. {
  1123. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1124. drm_crtc_vblank_put(crtc);
  1125. }
  1126. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1127. enum pipe pipe)
  1128. {
  1129. u32 val;
  1130. bool enabled;
  1131. val = I915_READ(PCH_TRANSCONF(pipe));
  1132. enabled = !!(val & TRANS_ENABLE);
  1133. I915_STATE_WARN(enabled,
  1134. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1135. pipe_name(pipe));
  1136. }
  1137. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1138. enum pipe pipe, u32 port_sel, u32 val)
  1139. {
  1140. if ((val & DP_PORT_EN) == 0)
  1141. return false;
  1142. if (HAS_PCH_CPT(dev_priv)) {
  1143. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1144. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1145. return false;
  1146. } else if (IS_CHERRYVIEW(dev_priv)) {
  1147. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1148. return false;
  1149. } else {
  1150. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1151. return false;
  1152. }
  1153. return true;
  1154. }
  1155. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1156. enum pipe pipe, u32 val)
  1157. {
  1158. if ((val & SDVO_ENABLE) == 0)
  1159. return false;
  1160. if (HAS_PCH_CPT(dev_priv)) {
  1161. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1162. return false;
  1163. } else if (IS_CHERRYVIEW(dev_priv)) {
  1164. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1165. return false;
  1166. } else {
  1167. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1168. return false;
  1169. }
  1170. return true;
  1171. }
  1172. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1173. enum pipe pipe, u32 val)
  1174. {
  1175. if ((val & LVDS_PORT_EN) == 0)
  1176. return false;
  1177. if (HAS_PCH_CPT(dev_priv)) {
  1178. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1179. return false;
  1180. } else {
  1181. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1182. return false;
  1183. }
  1184. return true;
  1185. }
  1186. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1187. enum pipe pipe, u32 val)
  1188. {
  1189. if ((val & ADPA_DAC_ENABLE) == 0)
  1190. return false;
  1191. if (HAS_PCH_CPT(dev_priv)) {
  1192. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1193. return false;
  1194. } else {
  1195. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1196. return false;
  1197. }
  1198. return true;
  1199. }
  1200. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, i915_reg_t reg,
  1202. u32 port_sel)
  1203. {
  1204. u32 val = I915_READ(reg);
  1205. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1206. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1207. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1208. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1209. && (val & DP_PIPEB_SELECT),
  1210. "IBX PCH dp port still using transcoder B\n");
  1211. }
  1212. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1213. enum pipe pipe, i915_reg_t reg)
  1214. {
  1215. u32 val = I915_READ(reg);
  1216. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1217. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1218. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1219. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1220. && (val & SDVO_PIPE_B_SELECT),
  1221. "IBX PCH hdmi port still using transcoder B\n");
  1222. }
  1223. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1224. enum pipe pipe)
  1225. {
  1226. u32 val;
  1227. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1228. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1229. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1230. val = I915_READ(PCH_ADPA);
  1231. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1232. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1233. pipe_name(pipe));
  1234. val = I915_READ(PCH_LVDS);
  1235. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1236. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1237. pipe_name(pipe));
  1238. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1239. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1240. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1241. }
  1242. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1243. const struct intel_crtc_state *pipe_config)
  1244. {
  1245. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1246. enum pipe pipe = crtc->pipe;
  1247. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1248. POSTING_READ(DPLL(pipe));
  1249. udelay(150);
  1250. if (intel_wait_for_register(dev_priv,
  1251. DPLL(pipe),
  1252. DPLL_LOCK_VLV,
  1253. DPLL_LOCK_VLV,
  1254. 1))
  1255. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1256. }
  1257. static void vlv_enable_pll(struct intel_crtc *crtc,
  1258. const struct intel_crtc_state *pipe_config)
  1259. {
  1260. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1261. enum pipe pipe = crtc->pipe;
  1262. assert_pipe_disabled(dev_priv, pipe);
  1263. /* PLL is protected by panel, make sure we can write it */
  1264. assert_panel_unlocked(dev_priv, pipe);
  1265. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1266. _vlv_enable_pll(crtc, pipe_config);
  1267. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1268. POSTING_READ(DPLL_MD(pipe));
  1269. }
  1270. static void _chv_enable_pll(struct intel_crtc *crtc,
  1271. const struct intel_crtc_state *pipe_config)
  1272. {
  1273. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1274. enum pipe pipe = crtc->pipe;
  1275. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1276. u32 tmp;
  1277. mutex_lock(&dev_priv->sb_lock);
  1278. /* Enable back the 10bit clock to display controller */
  1279. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1280. tmp |= DPIO_DCLKP_EN;
  1281. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1282. mutex_unlock(&dev_priv->sb_lock);
  1283. /*
  1284. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1285. */
  1286. udelay(1);
  1287. /* Enable PLL */
  1288. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1289. /* Check PLL is locked */
  1290. if (intel_wait_for_register(dev_priv,
  1291. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1292. 1))
  1293. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1294. }
  1295. static void chv_enable_pll(struct intel_crtc *crtc,
  1296. const struct intel_crtc_state *pipe_config)
  1297. {
  1298. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1299. enum pipe pipe = crtc->pipe;
  1300. assert_pipe_disabled(dev_priv, pipe);
  1301. /* PLL is protected by panel, make sure we can write it */
  1302. assert_panel_unlocked(dev_priv, pipe);
  1303. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1304. _chv_enable_pll(crtc, pipe_config);
  1305. if (pipe != PIPE_A) {
  1306. /*
  1307. * WaPixelRepeatModeFixForC0:chv
  1308. *
  1309. * DPLLCMD is AWOL. Use chicken bits to propagate
  1310. * the value from DPLLBMD to either pipe B or C.
  1311. */
  1312. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1313. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1314. I915_WRITE(CBR4_VLV, 0);
  1315. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1316. /*
  1317. * DPLLB VGA mode also seems to cause problems.
  1318. * We should always have it disabled.
  1319. */
  1320. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1321. } else {
  1322. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1323. POSTING_READ(DPLL_MD(pipe));
  1324. }
  1325. }
  1326. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1327. {
  1328. struct intel_crtc *crtc;
  1329. int count = 0;
  1330. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1331. count += crtc->base.state->active &&
  1332. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1333. }
  1334. return count;
  1335. }
  1336. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1337. const struct intel_crtc_state *crtc_state)
  1338. {
  1339. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1340. i915_reg_t reg = DPLL(crtc->pipe);
  1341. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1342. int i;
  1343. assert_pipe_disabled(dev_priv, crtc->pipe);
  1344. /* PLL is protected by panel, make sure we can write it */
  1345. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1346. assert_panel_unlocked(dev_priv, crtc->pipe);
  1347. /* Enable DVO 2x clock on both PLLs if necessary */
  1348. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1349. /*
  1350. * It appears to be important that we don't enable this
  1351. * for the current pipe before otherwise configuring the
  1352. * PLL. No idea how this should be handled if multiple
  1353. * DVO outputs are enabled simultaneosly.
  1354. */
  1355. dpll |= DPLL_DVO_2X_MODE;
  1356. I915_WRITE(DPLL(!crtc->pipe),
  1357. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1358. }
  1359. /*
  1360. * Apparently we need to have VGA mode enabled prior to changing
  1361. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1362. * dividers, even though the register value does change.
  1363. */
  1364. I915_WRITE(reg, 0);
  1365. I915_WRITE(reg, dpll);
  1366. /* Wait for the clocks to stabilize. */
  1367. POSTING_READ(reg);
  1368. udelay(150);
  1369. if (INTEL_GEN(dev_priv) >= 4) {
  1370. I915_WRITE(DPLL_MD(crtc->pipe),
  1371. crtc_state->dpll_hw_state.dpll_md);
  1372. } else {
  1373. /* The pixel multiplier can only be updated once the
  1374. * DPLL is enabled and the clocks are stable.
  1375. *
  1376. * So write it again.
  1377. */
  1378. I915_WRITE(reg, dpll);
  1379. }
  1380. /* We do this three times for luck */
  1381. for (i = 0; i < 3; i++) {
  1382. I915_WRITE(reg, dpll);
  1383. POSTING_READ(reg);
  1384. udelay(150); /* wait for warmup */
  1385. }
  1386. }
  1387. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1388. {
  1389. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1390. enum pipe pipe = crtc->pipe;
  1391. /* Disable DVO 2x clock on both PLLs if necessary */
  1392. if (IS_I830(dev_priv) &&
  1393. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1394. !intel_num_dvo_pipes(dev_priv)) {
  1395. I915_WRITE(DPLL(PIPE_B),
  1396. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1397. I915_WRITE(DPLL(PIPE_A),
  1398. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1399. }
  1400. /* Don't disable pipe or pipe PLLs if needed */
  1401. if (IS_I830(dev_priv))
  1402. return;
  1403. /* Make sure the pipe isn't still relying on us */
  1404. assert_pipe_disabled(dev_priv, pipe);
  1405. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1406. POSTING_READ(DPLL(pipe));
  1407. }
  1408. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1409. {
  1410. u32 val;
  1411. /* Make sure the pipe isn't still relying on us */
  1412. assert_pipe_disabled(dev_priv, pipe);
  1413. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1414. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1415. if (pipe != PIPE_A)
  1416. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1417. I915_WRITE(DPLL(pipe), val);
  1418. POSTING_READ(DPLL(pipe));
  1419. }
  1420. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1421. {
  1422. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1423. u32 val;
  1424. /* Make sure the pipe isn't still relying on us */
  1425. assert_pipe_disabled(dev_priv, pipe);
  1426. val = DPLL_SSC_REF_CLK_CHV |
  1427. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1428. if (pipe != PIPE_A)
  1429. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1430. I915_WRITE(DPLL(pipe), val);
  1431. POSTING_READ(DPLL(pipe));
  1432. mutex_lock(&dev_priv->sb_lock);
  1433. /* Disable 10bit clock to display controller */
  1434. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1435. val &= ~DPIO_DCLKP_EN;
  1436. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1437. mutex_unlock(&dev_priv->sb_lock);
  1438. }
  1439. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1440. struct intel_digital_port *dport,
  1441. unsigned int expected_mask)
  1442. {
  1443. u32 port_mask;
  1444. i915_reg_t dpll_reg;
  1445. switch (dport->base.port) {
  1446. case PORT_B:
  1447. port_mask = DPLL_PORTB_READY_MASK;
  1448. dpll_reg = DPLL(0);
  1449. break;
  1450. case PORT_C:
  1451. port_mask = DPLL_PORTC_READY_MASK;
  1452. dpll_reg = DPLL(0);
  1453. expected_mask <<= 4;
  1454. break;
  1455. case PORT_D:
  1456. port_mask = DPLL_PORTD_READY_MASK;
  1457. dpll_reg = DPIO_PHY_STATUS;
  1458. break;
  1459. default:
  1460. BUG();
  1461. }
  1462. if (intel_wait_for_register(dev_priv,
  1463. dpll_reg, port_mask, expected_mask,
  1464. 1000))
  1465. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1466. port_name(dport->base.port),
  1467. I915_READ(dpll_reg) & port_mask, expected_mask);
  1468. }
  1469. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1470. enum pipe pipe)
  1471. {
  1472. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1473. pipe);
  1474. i915_reg_t reg;
  1475. uint32_t val, pipeconf_val;
  1476. /* Make sure PCH DPLL is enabled */
  1477. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1478. /* FDI must be feeding us bits for PCH ports */
  1479. assert_fdi_tx_enabled(dev_priv, pipe);
  1480. assert_fdi_rx_enabled(dev_priv, pipe);
  1481. if (HAS_PCH_CPT(dev_priv)) {
  1482. /* Workaround: Set the timing override bit before enabling the
  1483. * pch transcoder. */
  1484. reg = TRANS_CHICKEN2(pipe);
  1485. val = I915_READ(reg);
  1486. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1487. I915_WRITE(reg, val);
  1488. }
  1489. reg = PCH_TRANSCONF(pipe);
  1490. val = I915_READ(reg);
  1491. pipeconf_val = I915_READ(PIPECONF(pipe));
  1492. if (HAS_PCH_IBX(dev_priv)) {
  1493. /*
  1494. * Make the BPC in transcoder be consistent with
  1495. * that in pipeconf reg. For HDMI we must use 8bpc
  1496. * here for both 8bpc and 12bpc.
  1497. */
  1498. val &= ~PIPECONF_BPC_MASK;
  1499. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1500. val |= PIPECONF_8BPC;
  1501. else
  1502. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1503. }
  1504. val &= ~TRANS_INTERLACE_MASK;
  1505. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1506. if (HAS_PCH_IBX(dev_priv) &&
  1507. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1508. val |= TRANS_LEGACY_INTERLACED_ILK;
  1509. else
  1510. val |= TRANS_INTERLACED;
  1511. else
  1512. val |= TRANS_PROGRESSIVE;
  1513. I915_WRITE(reg, val | TRANS_ENABLE);
  1514. if (intel_wait_for_register(dev_priv,
  1515. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1516. 100))
  1517. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1518. }
  1519. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1520. enum transcoder cpu_transcoder)
  1521. {
  1522. u32 val, pipeconf_val;
  1523. /* FDI must be feeding us bits for PCH ports */
  1524. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1525. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1526. /* Workaround: set timing override bit. */
  1527. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1528. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1529. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1530. val = TRANS_ENABLE;
  1531. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1532. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1533. PIPECONF_INTERLACED_ILK)
  1534. val |= TRANS_INTERLACED;
  1535. else
  1536. val |= TRANS_PROGRESSIVE;
  1537. I915_WRITE(LPT_TRANSCONF, val);
  1538. if (intel_wait_for_register(dev_priv,
  1539. LPT_TRANSCONF,
  1540. TRANS_STATE_ENABLE,
  1541. TRANS_STATE_ENABLE,
  1542. 100))
  1543. DRM_ERROR("Failed to enable PCH transcoder\n");
  1544. }
  1545. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1546. enum pipe pipe)
  1547. {
  1548. i915_reg_t reg;
  1549. uint32_t val;
  1550. /* FDI relies on the transcoder */
  1551. assert_fdi_tx_disabled(dev_priv, pipe);
  1552. assert_fdi_rx_disabled(dev_priv, pipe);
  1553. /* Ports must be off as well */
  1554. assert_pch_ports_disabled(dev_priv, pipe);
  1555. reg = PCH_TRANSCONF(pipe);
  1556. val = I915_READ(reg);
  1557. val &= ~TRANS_ENABLE;
  1558. I915_WRITE(reg, val);
  1559. /* wait for PCH transcoder off, transcoder state */
  1560. if (intel_wait_for_register(dev_priv,
  1561. reg, TRANS_STATE_ENABLE, 0,
  1562. 50))
  1563. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1564. if (HAS_PCH_CPT(dev_priv)) {
  1565. /* Workaround: Clear the timing override chicken bit again. */
  1566. reg = TRANS_CHICKEN2(pipe);
  1567. val = I915_READ(reg);
  1568. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1569. I915_WRITE(reg, val);
  1570. }
  1571. }
  1572. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1573. {
  1574. u32 val;
  1575. val = I915_READ(LPT_TRANSCONF);
  1576. val &= ~TRANS_ENABLE;
  1577. I915_WRITE(LPT_TRANSCONF, val);
  1578. /* wait for PCH transcoder off, transcoder state */
  1579. if (intel_wait_for_register(dev_priv,
  1580. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1581. 50))
  1582. DRM_ERROR("Failed to disable PCH transcoder\n");
  1583. /* Workaround: clear timing override bit. */
  1584. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1585. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1586. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1587. }
  1588. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1589. {
  1590. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1591. if (HAS_PCH_LPT(dev_priv))
  1592. return PIPE_A;
  1593. else
  1594. return crtc->pipe;
  1595. }
  1596. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1597. {
  1598. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1599. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1600. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1601. enum pipe pipe = crtc->pipe;
  1602. i915_reg_t reg;
  1603. u32 val;
  1604. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1605. assert_planes_disabled(crtc);
  1606. /*
  1607. * A pipe without a PLL won't actually be able to drive bits from
  1608. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1609. * need the check.
  1610. */
  1611. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1612. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1613. assert_dsi_pll_enabled(dev_priv);
  1614. else
  1615. assert_pll_enabled(dev_priv, pipe);
  1616. } else {
  1617. if (new_crtc_state->has_pch_encoder) {
  1618. /* if driving the PCH, we need FDI enabled */
  1619. assert_fdi_rx_pll_enabled(dev_priv,
  1620. intel_crtc_pch_transcoder(crtc));
  1621. assert_fdi_tx_pll_enabled(dev_priv,
  1622. (enum pipe) cpu_transcoder);
  1623. }
  1624. /* FIXME: assert CPU port conditions for SNB+ */
  1625. }
  1626. reg = PIPECONF(cpu_transcoder);
  1627. val = I915_READ(reg);
  1628. if (val & PIPECONF_ENABLE) {
  1629. /* we keep both pipes enabled on 830 */
  1630. WARN_ON(!IS_I830(dev_priv));
  1631. return;
  1632. }
  1633. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1634. POSTING_READ(reg);
  1635. /*
  1636. * Until the pipe starts PIPEDSL reads will return a stale value,
  1637. * which causes an apparent vblank timestamp jump when PIPEDSL
  1638. * resets to its proper value. That also messes up the frame count
  1639. * when it's derived from the timestamps. So let's wait for the
  1640. * pipe to start properly before we call drm_crtc_vblank_on()
  1641. */
  1642. if (dev_priv->drm.max_vblank_count == 0)
  1643. intel_wait_for_pipe_scanline_moving(crtc);
  1644. }
  1645. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1646. {
  1647. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1648. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1649. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1650. enum pipe pipe = crtc->pipe;
  1651. i915_reg_t reg;
  1652. u32 val;
  1653. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1654. /*
  1655. * Make sure planes won't keep trying to pump pixels to us,
  1656. * or we might hang the display.
  1657. */
  1658. assert_planes_disabled(crtc);
  1659. reg = PIPECONF(cpu_transcoder);
  1660. val = I915_READ(reg);
  1661. if ((val & PIPECONF_ENABLE) == 0)
  1662. return;
  1663. /*
  1664. * Double wide has implications for planes
  1665. * so best keep it disabled when not needed.
  1666. */
  1667. if (old_crtc_state->double_wide)
  1668. val &= ~PIPECONF_DOUBLE_WIDE;
  1669. /* Don't disable pipe or pipe PLLs if needed */
  1670. if (!IS_I830(dev_priv))
  1671. val &= ~PIPECONF_ENABLE;
  1672. I915_WRITE(reg, val);
  1673. if ((val & PIPECONF_ENABLE) == 0)
  1674. intel_wait_for_pipe_off(old_crtc_state);
  1675. }
  1676. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1677. {
  1678. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1679. }
  1680. static unsigned int
  1681. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1682. {
  1683. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1684. unsigned int cpp = fb->format->cpp[plane];
  1685. switch (fb->modifier) {
  1686. case DRM_FORMAT_MOD_LINEAR:
  1687. return cpp;
  1688. case I915_FORMAT_MOD_X_TILED:
  1689. if (IS_GEN2(dev_priv))
  1690. return 128;
  1691. else
  1692. return 512;
  1693. case I915_FORMAT_MOD_Y_TILED_CCS:
  1694. if (plane == 1)
  1695. return 128;
  1696. /* fall through */
  1697. case I915_FORMAT_MOD_Y_TILED:
  1698. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1699. return 128;
  1700. else
  1701. return 512;
  1702. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1703. if (plane == 1)
  1704. return 128;
  1705. /* fall through */
  1706. case I915_FORMAT_MOD_Yf_TILED:
  1707. switch (cpp) {
  1708. case 1:
  1709. return 64;
  1710. case 2:
  1711. case 4:
  1712. return 128;
  1713. case 8:
  1714. case 16:
  1715. return 256;
  1716. default:
  1717. MISSING_CASE(cpp);
  1718. return cpp;
  1719. }
  1720. break;
  1721. default:
  1722. MISSING_CASE(fb->modifier);
  1723. return cpp;
  1724. }
  1725. }
  1726. static unsigned int
  1727. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1728. {
  1729. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1730. return 1;
  1731. else
  1732. return intel_tile_size(to_i915(fb->dev)) /
  1733. intel_tile_width_bytes(fb, plane);
  1734. }
  1735. /* Return the tile dimensions in pixel units */
  1736. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1737. unsigned int *tile_width,
  1738. unsigned int *tile_height)
  1739. {
  1740. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1741. unsigned int cpp = fb->format->cpp[plane];
  1742. *tile_width = tile_width_bytes / cpp;
  1743. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1744. }
  1745. unsigned int
  1746. intel_fb_align_height(const struct drm_framebuffer *fb,
  1747. int plane, unsigned int height)
  1748. {
  1749. unsigned int tile_height = intel_tile_height(fb, plane);
  1750. return ALIGN(height, tile_height);
  1751. }
  1752. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1753. {
  1754. unsigned int size = 0;
  1755. int i;
  1756. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1757. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1758. return size;
  1759. }
  1760. static void
  1761. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1762. const struct drm_framebuffer *fb,
  1763. unsigned int rotation)
  1764. {
  1765. view->type = I915_GGTT_VIEW_NORMAL;
  1766. if (drm_rotation_90_or_270(rotation)) {
  1767. view->type = I915_GGTT_VIEW_ROTATED;
  1768. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1769. }
  1770. }
  1771. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1772. {
  1773. if (IS_I830(dev_priv))
  1774. return 16 * 1024;
  1775. else if (IS_I85X(dev_priv))
  1776. return 256;
  1777. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1778. return 32;
  1779. else
  1780. return 4 * 1024;
  1781. }
  1782. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1783. {
  1784. if (INTEL_GEN(dev_priv) >= 9)
  1785. return 256 * 1024;
  1786. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1787. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1788. return 128 * 1024;
  1789. else if (INTEL_GEN(dev_priv) >= 4)
  1790. return 4 * 1024;
  1791. else
  1792. return 0;
  1793. }
  1794. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1795. int plane)
  1796. {
  1797. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1798. /* AUX_DIST needs only 4K alignment */
  1799. if (plane == 1)
  1800. return 4096;
  1801. switch (fb->modifier) {
  1802. case DRM_FORMAT_MOD_LINEAR:
  1803. return intel_linear_alignment(dev_priv);
  1804. case I915_FORMAT_MOD_X_TILED:
  1805. if (INTEL_GEN(dev_priv) >= 9)
  1806. return 256 * 1024;
  1807. return 0;
  1808. case I915_FORMAT_MOD_Y_TILED_CCS:
  1809. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1810. case I915_FORMAT_MOD_Y_TILED:
  1811. case I915_FORMAT_MOD_Yf_TILED:
  1812. return 1 * 1024 * 1024;
  1813. default:
  1814. MISSING_CASE(fb->modifier);
  1815. return 0;
  1816. }
  1817. }
  1818. static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
  1819. {
  1820. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1821. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1822. return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
  1823. }
  1824. struct i915_vma *
  1825. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1826. unsigned int rotation,
  1827. bool uses_fence,
  1828. unsigned long *out_flags)
  1829. {
  1830. struct drm_device *dev = fb->dev;
  1831. struct drm_i915_private *dev_priv = to_i915(dev);
  1832. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1833. struct i915_ggtt_view view;
  1834. struct i915_vma *vma;
  1835. unsigned int pinctl;
  1836. u32 alignment;
  1837. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1838. alignment = intel_surf_alignment(fb, 0);
  1839. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1840. /* Note that the w/a also requires 64 PTE of padding following the
  1841. * bo. We currently fill all unused PTE with the shadow page and so
  1842. * we should always have valid PTE following the scanout preventing
  1843. * the VT-d warning.
  1844. */
  1845. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1846. alignment = 256 * 1024;
  1847. /*
  1848. * Global gtt pte registers are special registers which actually forward
  1849. * writes to a chunk of system memory. Which means that there is no risk
  1850. * that the register values disappear as soon as we call
  1851. * intel_runtime_pm_put(), so it is correct to wrap only the
  1852. * pin/unpin/fence and not more.
  1853. */
  1854. intel_runtime_pm_get(dev_priv);
  1855. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1856. pinctl = 0;
  1857. /* Valleyview is definitely limited to scanning out the first
  1858. * 512MiB. Lets presume this behaviour was inherited from the
  1859. * g4x display engine and that all earlier gen are similarly
  1860. * limited. Testing suggests that it is a little more
  1861. * complicated than this. For example, Cherryview appears quite
  1862. * happy to scanout from anywhere within its global aperture.
  1863. */
  1864. if (HAS_GMCH_DISPLAY(dev_priv))
  1865. pinctl |= PIN_MAPPABLE;
  1866. vma = i915_gem_object_pin_to_display_plane(obj,
  1867. alignment, &view, pinctl);
  1868. if (IS_ERR(vma))
  1869. goto err;
  1870. if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
  1871. int ret;
  1872. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1873. * fence, whereas 965+ only requires a fence if using
  1874. * framebuffer compression. For simplicity, we always, when
  1875. * possible, install a fence as the cost is not that onerous.
  1876. *
  1877. * If we fail to fence the tiled scanout, then either the
  1878. * modeset will reject the change (which is highly unlikely as
  1879. * the affected systems, all but one, do not have unmappable
  1880. * space) or we will not be able to enable full powersaving
  1881. * techniques (also likely not to apply due to various limits
  1882. * FBC and the like impose on the size of the buffer, which
  1883. * presumably we violated anyway with this unmappable buffer).
  1884. * Anyway, it is presumably better to stumble onwards with
  1885. * something and try to run the system in a "less than optimal"
  1886. * mode that matches the user configuration.
  1887. */
  1888. ret = i915_vma_pin_fence(vma);
  1889. if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
  1890. i915_gem_object_unpin_from_display_plane(vma);
  1891. vma = ERR_PTR(ret);
  1892. goto err;
  1893. }
  1894. if (ret == 0 && vma->fence)
  1895. *out_flags |= PLANE_HAS_FENCE;
  1896. }
  1897. i915_vma_get(vma);
  1898. err:
  1899. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1900. intel_runtime_pm_put(dev_priv);
  1901. return vma;
  1902. }
  1903. void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
  1904. {
  1905. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1906. if (flags & PLANE_HAS_FENCE)
  1907. i915_vma_unpin_fence(vma);
  1908. i915_gem_object_unpin_from_display_plane(vma);
  1909. i915_vma_put(vma);
  1910. }
  1911. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1912. unsigned int rotation)
  1913. {
  1914. if (drm_rotation_90_or_270(rotation))
  1915. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1916. else
  1917. return fb->pitches[plane];
  1918. }
  1919. /*
  1920. * Convert the x/y offsets into a linear offset.
  1921. * Only valid with 0/180 degree rotation, which is fine since linear
  1922. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1923. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1924. */
  1925. u32 intel_fb_xy_to_linear(int x, int y,
  1926. const struct intel_plane_state *state,
  1927. int plane)
  1928. {
  1929. const struct drm_framebuffer *fb = state->base.fb;
  1930. unsigned int cpp = fb->format->cpp[plane];
  1931. unsigned int pitch = fb->pitches[plane];
  1932. return y * pitch + x * cpp;
  1933. }
  1934. /*
  1935. * Add the x/y offsets derived from fb->offsets[] to the user
  1936. * specified plane src x/y offsets. The resulting x/y offsets
  1937. * specify the start of scanout from the beginning of the gtt mapping.
  1938. */
  1939. void intel_add_fb_offsets(int *x, int *y,
  1940. const struct intel_plane_state *state,
  1941. int plane)
  1942. {
  1943. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1944. unsigned int rotation = state->base.rotation;
  1945. if (drm_rotation_90_or_270(rotation)) {
  1946. *x += intel_fb->rotated[plane].x;
  1947. *y += intel_fb->rotated[plane].y;
  1948. } else {
  1949. *x += intel_fb->normal[plane].x;
  1950. *y += intel_fb->normal[plane].y;
  1951. }
  1952. }
  1953. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1954. unsigned int tile_width,
  1955. unsigned int tile_height,
  1956. unsigned int tile_size,
  1957. unsigned int pitch_tiles,
  1958. u32 old_offset,
  1959. u32 new_offset)
  1960. {
  1961. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1962. unsigned int tiles;
  1963. WARN_ON(old_offset & (tile_size - 1));
  1964. WARN_ON(new_offset & (tile_size - 1));
  1965. WARN_ON(new_offset > old_offset);
  1966. tiles = (old_offset - new_offset) / tile_size;
  1967. *y += tiles / pitch_tiles * tile_height;
  1968. *x += tiles % pitch_tiles * tile_width;
  1969. /* minimize x in case it got needlessly big */
  1970. *y += *x / pitch_pixels * tile_height;
  1971. *x %= pitch_pixels;
  1972. return new_offset;
  1973. }
  1974. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1975. const struct drm_framebuffer *fb, int plane,
  1976. unsigned int rotation,
  1977. u32 old_offset, u32 new_offset)
  1978. {
  1979. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1980. unsigned int cpp = fb->format->cpp[plane];
  1981. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1982. WARN_ON(new_offset > old_offset);
  1983. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1984. unsigned int tile_size, tile_width, tile_height;
  1985. unsigned int pitch_tiles;
  1986. tile_size = intel_tile_size(dev_priv);
  1987. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1988. if (drm_rotation_90_or_270(rotation)) {
  1989. pitch_tiles = pitch / tile_height;
  1990. swap(tile_width, tile_height);
  1991. } else {
  1992. pitch_tiles = pitch / (tile_width * cpp);
  1993. }
  1994. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1995. tile_size, pitch_tiles,
  1996. old_offset, new_offset);
  1997. } else {
  1998. old_offset += *y * pitch + *x * cpp;
  1999. *y = (old_offset - new_offset) / pitch;
  2000. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2001. }
  2002. return new_offset;
  2003. }
  2004. /*
  2005. * Adjust the tile offset by moving the difference into
  2006. * the x/y offsets.
  2007. */
  2008. static u32 intel_adjust_tile_offset(int *x, int *y,
  2009. const struct intel_plane_state *state, int plane,
  2010. u32 old_offset, u32 new_offset)
  2011. {
  2012. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  2013. state->base.rotation,
  2014. old_offset, new_offset);
  2015. }
  2016. /*
  2017. * Computes the linear offset to the base tile and adjusts
  2018. * x, y. bytes per pixel is assumed to be a power-of-two.
  2019. *
  2020. * In the 90/270 rotated case, x and y are assumed
  2021. * to be already rotated to match the rotated GTT view, and
  2022. * pitch is the tile_height aligned framebuffer height.
  2023. *
  2024. * This function is used when computing the derived information
  2025. * under intel_framebuffer, so using any of that information
  2026. * here is not allowed. Anything under drm_framebuffer can be
  2027. * used. This is why the user has to pass in the pitch since it
  2028. * is specified in the rotated orientation.
  2029. */
  2030. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2031. int *x, int *y,
  2032. const struct drm_framebuffer *fb, int plane,
  2033. unsigned int pitch,
  2034. unsigned int rotation,
  2035. u32 alignment)
  2036. {
  2037. uint64_t fb_modifier = fb->modifier;
  2038. unsigned int cpp = fb->format->cpp[plane];
  2039. u32 offset, offset_aligned;
  2040. if (alignment)
  2041. alignment--;
  2042. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2043. unsigned int tile_size, tile_width, tile_height;
  2044. unsigned int tile_rows, tiles, pitch_tiles;
  2045. tile_size = intel_tile_size(dev_priv);
  2046. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2047. if (drm_rotation_90_or_270(rotation)) {
  2048. pitch_tiles = pitch / tile_height;
  2049. swap(tile_width, tile_height);
  2050. } else {
  2051. pitch_tiles = pitch / (tile_width * cpp);
  2052. }
  2053. tile_rows = *y / tile_height;
  2054. *y %= tile_height;
  2055. tiles = *x / tile_width;
  2056. *x %= tile_width;
  2057. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2058. offset_aligned = offset & ~alignment;
  2059. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2060. tile_size, pitch_tiles,
  2061. offset, offset_aligned);
  2062. } else {
  2063. offset = *y * pitch + *x * cpp;
  2064. offset_aligned = offset & ~alignment;
  2065. *y = (offset & alignment) / pitch;
  2066. *x = ((offset & alignment) - *y * pitch) / cpp;
  2067. }
  2068. return offset_aligned;
  2069. }
  2070. u32 intel_compute_tile_offset(int *x, int *y,
  2071. const struct intel_plane_state *state,
  2072. int plane)
  2073. {
  2074. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2075. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2076. const struct drm_framebuffer *fb = state->base.fb;
  2077. unsigned int rotation = state->base.rotation;
  2078. int pitch = intel_fb_pitch(fb, plane, rotation);
  2079. u32 alignment;
  2080. if (intel_plane->id == PLANE_CURSOR)
  2081. alignment = intel_cursor_alignment(dev_priv);
  2082. else
  2083. alignment = intel_surf_alignment(fb, plane);
  2084. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2085. rotation, alignment);
  2086. }
  2087. /* Convert the fb->offset[] into x/y offsets */
  2088. static int intel_fb_offset_to_xy(int *x, int *y,
  2089. const struct drm_framebuffer *fb, int plane)
  2090. {
  2091. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2092. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2093. fb->offsets[plane] % intel_tile_size(dev_priv))
  2094. return -EINVAL;
  2095. *x = 0;
  2096. *y = 0;
  2097. _intel_adjust_tile_offset(x, y,
  2098. fb, plane, DRM_MODE_ROTATE_0,
  2099. fb->offsets[plane], 0);
  2100. return 0;
  2101. }
  2102. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2103. {
  2104. switch (fb_modifier) {
  2105. case I915_FORMAT_MOD_X_TILED:
  2106. return I915_TILING_X;
  2107. case I915_FORMAT_MOD_Y_TILED:
  2108. case I915_FORMAT_MOD_Y_TILED_CCS:
  2109. return I915_TILING_Y;
  2110. default:
  2111. return I915_TILING_NONE;
  2112. }
  2113. }
  2114. /*
  2115. * From the Sky Lake PRM:
  2116. * "The Color Control Surface (CCS) contains the compression status of
  2117. * the cache-line pairs. The compression state of the cache-line pair
  2118. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2119. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2120. * cache-line-pairs. CCS is always Y tiled."
  2121. *
  2122. * Since cache line pairs refers to horizontally adjacent cache lines,
  2123. * each cache line in the CCS corresponds to an area of 32x16 cache
  2124. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2125. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2126. * main surface.
  2127. */
  2128. static const struct drm_format_info ccs_formats[] = {
  2129. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2130. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2131. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2132. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2133. };
  2134. static const struct drm_format_info *
  2135. lookup_format_info(const struct drm_format_info formats[],
  2136. int num_formats, u32 format)
  2137. {
  2138. int i;
  2139. for (i = 0; i < num_formats; i++) {
  2140. if (formats[i].format == format)
  2141. return &formats[i];
  2142. }
  2143. return NULL;
  2144. }
  2145. static const struct drm_format_info *
  2146. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2147. {
  2148. switch (cmd->modifier[0]) {
  2149. case I915_FORMAT_MOD_Y_TILED_CCS:
  2150. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2151. return lookup_format_info(ccs_formats,
  2152. ARRAY_SIZE(ccs_formats),
  2153. cmd->pixel_format);
  2154. default:
  2155. return NULL;
  2156. }
  2157. }
  2158. static int
  2159. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2160. struct drm_framebuffer *fb)
  2161. {
  2162. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2163. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2164. u32 gtt_offset_rotated = 0;
  2165. unsigned int max_size = 0;
  2166. int i, num_planes = fb->format->num_planes;
  2167. unsigned int tile_size = intel_tile_size(dev_priv);
  2168. for (i = 0; i < num_planes; i++) {
  2169. unsigned int width, height;
  2170. unsigned int cpp, size;
  2171. u32 offset;
  2172. int x, y;
  2173. int ret;
  2174. cpp = fb->format->cpp[i];
  2175. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2176. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2177. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2178. if (ret) {
  2179. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2180. i, fb->offsets[i]);
  2181. return ret;
  2182. }
  2183. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2184. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2185. int hsub = fb->format->hsub;
  2186. int vsub = fb->format->vsub;
  2187. int tile_width, tile_height;
  2188. int main_x, main_y;
  2189. int ccs_x, ccs_y;
  2190. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2191. tile_width *= hsub;
  2192. tile_height *= vsub;
  2193. ccs_x = (x * hsub) % tile_width;
  2194. ccs_y = (y * vsub) % tile_height;
  2195. main_x = intel_fb->normal[0].x % tile_width;
  2196. main_y = intel_fb->normal[0].y % tile_height;
  2197. /*
  2198. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2199. * x/y offsets must match between CCS and the main surface.
  2200. */
  2201. if (main_x != ccs_x || main_y != ccs_y) {
  2202. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2203. main_x, main_y,
  2204. ccs_x, ccs_y,
  2205. intel_fb->normal[0].x,
  2206. intel_fb->normal[0].y,
  2207. x, y);
  2208. return -EINVAL;
  2209. }
  2210. }
  2211. /*
  2212. * The fence (if used) is aligned to the start of the object
  2213. * so having the framebuffer wrap around across the edge of the
  2214. * fenced region doesn't really work. We have no API to configure
  2215. * the fence start offset within the object (nor could we probably
  2216. * on gen2/3). So it's just easier if we just require that the
  2217. * fb layout agrees with the fence layout. We already check that the
  2218. * fb stride matches the fence stride elsewhere.
  2219. */
  2220. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2221. (x + width) * cpp > fb->pitches[i]) {
  2222. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2223. i, fb->offsets[i]);
  2224. return -EINVAL;
  2225. }
  2226. /*
  2227. * First pixel of the framebuffer from
  2228. * the start of the normal gtt mapping.
  2229. */
  2230. intel_fb->normal[i].x = x;
  2231. intel_fb->normal[i].y = y;
  2232. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2233. fb, i, fb->pitches[i],
  2234. DRM_MODE_ROTATE_0, tile_size);
  2235. offset /= tile_size;
  2236. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2237. unsigned int tile_width, tile_height;
  2238. unsigned int pitch_tiles;
  2239. struct drm_rect r;
  2240. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2241. rot_info->plane[i].offset = offset;
  2242. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2243. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2244. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2245. intel_fb->rotated[i].pitch =
  2246. rot_info->plane[i].height * tile_height;
  2247. /* how many tiles does this plane need */
  2248. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2249. /*
  2250. * If the plane isn't horizontally tile aligned,
  2251. * we need one more tile.
  2252. */
  2253. if (x != 0)
  2254. size++;
  2255. /* rotate the x/y offsets to match the GTT view */
  2256. r.x1 = x;
  2257. r.y1 = y;
  2258. r.x2 = x + width;
  2259. r.y2 = y + height;
  2260. drm_rect_rotate(&r,
  2261. rot_info->plane[i].width * tile_width,
  2262. rot_info->plane[i].height * tile_height,
  2263. DRM_MODE_ROTATE_270);
  2264. x = r.x1;
  2265. y = r.y1;
  2266. /* rotate the tile dimensions to match the GTT view */
  2267. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2268. swap(tile_width, tile_height);
  2269. /*
  2270. * We only keep the x/y offsets, so push all of the
  2271. * gtt offset into the x/y offsets.
  2272. */
  2273. __intel_adjust_tile_offset(&x, &y,
  2274. tile_width, tile_height,
  2275. tile_size, pitch_tiles,
  2276. gtt_offset_rotated * tile_size, 0);
  2277. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2278. /*
  2279. * First pixel of the framebuffer from
  2280. * the start of the rotated gtt mapping.
  2281. */
  2282. intel_fb->rotated[i].x = x;
  2283. intel_fb->rotated[i].y = y;
  2284. } else {
  2285. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2286. x * cpp, tile_size);
  2287. }
  2288. /* how many tiles in total needed in the bo */
  2289. max_size = max(max_size, offset + size);
  2290. }
  2291. if (max_size * tile_size > intel_fb->obj->base.size) {
  2292. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2293. max_size * tile_size, intel_fb->obj->base.size);
  2294. return -EINVAL;
  2295. }
  2296. return 0;
  2297. }
  2298. static int i9xx_format_to_fourcc(int format)
  2299. {
  2300. switch (format) {
  2301. case DISPPLANE_8BPP:
  2302. return DRM_FORMAT_C8;
  2303. case DISPPLANE_BGRX555:
  2304. return DRM_FORMAT_XRGB1555;
  2305. case DISPPLANE_BGRX565:
  2306. return DRM_FORMAT_RGB565;
  2307. default:
  2308. case DISPPLANE_BGRX888:
  2309. return DRM_FORMAT_XRGB8888;
  2310. case DISPPLANE_RGBX888:
  2311. return DRM_FORMAT_XBGR8888;
  2312. case DISPPLANE_BGRX101010:
  2313. return DRM_FORMAT_XRGB2101010;
  2314. case DISPPLANE_RGBX101010:
  2315. return DRM_FORMAT_XBGR2101010;
  2316. }
  2317. }
  2318. int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2319. {
  2320. switch (format) {
  2321. case PLANE_CTL_FORMAT_RGB_565:
  2322. return DRM_FORMAT_RGB565;
  2323. case PLANE_CTL_FORMAT_NV12:
  2324. return DRM_FORMAT_NV12;
  2325. default:
  2326. case PLANE_CTL_FORMAT_XRGB_8888:
  2327. if (rgb_order) {
  2328. if (alpha)
  2329. return DRM_FORMAT_ABGR8888;
  2330. else
  2331. return DRM_FORMAT_XBGR8888;
  2332. } else {
  2333. if (alpha)
  2334. return DRM_FORMAT_ARGB8888;
  2335. else
  2336. return DRM_FORMAT_XRGB8888;
  2337. }
  2338. case PLANE_CTL_FORMAT_XRGB_2101010:
  2339. if (rgb_order)
  2340. return DRM_FORMAT_XBGR2101010;
  2341. else
  2342. return DRM_FORMAT_XRGB2101010;
  2343. }
  2344. }
  2345. static bool
  2346. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2347. struct intel_initial_plane_config *plane_config)
  2348. {
  2349. struct drm_device *dev = crtc->base.dev;
  2350. struct drm_i915_private *dev_priv = to_i915(dev);
  2351. struct drm_i915_gem_object *obj = NULL;
  2352. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2353. struct drm_framebuffer *fb = &plane_config->fb->base;
  2354. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2355. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2356. PAGE_SIZE);
  2357. size_aligned -= base_aligned;
  2358. if (plane_config->size == 0)
  2359. return false;
  2360. /* If the FB is too big, just don't use it since fbdev is not very
  2361. * important and we should probably use that space with FBC or other
  2362. * features. */
  2363. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2364. return false;
  2365. mutex_lock(&dev->struct_mutex);
  2366. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2367. base_aligned,
  2368. base_aligned,
  2369. size_aligned);
  2370. mutex_unlock(&dev->struct_mutex);
  2371. if (!obj)
  2372. return false;
  2373. if (plane_config->tiling == I915_TILING_X)
  2374. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2375. mode_cmd.pixel_format = fb->format->format;
  2376. mode_cmd.width = fb->width;
  2377. mode_cmd.height = fb->height;
  2378. mode_cmd.pitches[0] = fb->pitches[0];
  2379. mode_cmd.modifier[0] = fb->modifier;
  2380. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2381. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2382. DRM_DEBUG_KMS("intel fb init failed\n");
  2383. goto out_unref_obj;
  2384. }
  2385. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2386. return true;
  2387. out_unref_obj:
  2388. i915_gem_object_put(obj);
  2389. return false;
  2390. }
  2391. static void
  2392. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2393. struct intel_plane_state *plane_state,
  2394. bool visible)
  2395. {
  2396. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2397. plane_state->base.visible = visible;
  2398. /* FIXME pre-g4x don't work like this */
  2399. if (visible) {
  2400. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2401. crtc_state->active_planes |= BIT(plane->id);
  2402. } else {
  2403. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2404. crtc_state->active_planes &= ~BIT(plane->id);
  2405. }
  2406. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2407. crtc_state->base.crtc->name,
  2408. crtc_state->active_planes);
  2409. }
  2410. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2411. struct intel_plane *plane)
  2412. {
  2413. struct intel_crtc_state *crtc_state =
  2414. to_intel_crtc_state(crtc->base.state);
  2415. struct intel_plane_state *plane_state =
  2416. to_intel_plane_state(plane->base.state);
  2417. intel_set_plane_visible(crtc_state, plane_state, false);
  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 drm_crtc_state *crtc_state = intel_crtc->base.state;
  2434. struct intel_plane *intel_plane = to_intel_plane(primary);
  2435. struct intel_plane_state *intel_state =
  2436. to_intel_plane_state(plane_state);
  2437. struct drm_framebuffer *fb;
  2438. if (!plane_config->fb)
  2439. return;
  2440. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2441. fb = &plane_config->fb->base;
  2442. goto valid_fb;
  2443. }
  2444. kfree(plane_config->fb);
  2445. /*
  2446. * Failed to alloc the obj, check to see if we should share
  2447. * an fb with another CRTC instead
  2448. */
  2449. for_each_crtc(dev, c) {
  2450. struct intel_plane_state *state;
  2451. if (c == &intel_crtc->base)
  2452. continue;
  2453. if (!to_intel_crtc(c)->active)
  2454. continue;
  2455. state = to_intel_plane_state(c->primary->state);
  2456. if (!state->vma)
  2457. continue;
  2458. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2459. fb = state->base.fb;
  2460. drm_framebuffer_get(fb);
  2461. goto valid_fb;
  2462. }
  2463. }
  2464. /*
  2465. * We've failed to reconstruct the BIOS FB. Current display state
  2466. * indicates that the primary plane is visible, but has a NULL FB,
  2467. * which will lead to problems later if we don't fix it up. The
  2468. * simplest solution is to just disable the primary plane now and
  2469. * pretend the BIOS never had it enabled.
  2470. */
  2471. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2472. return;
  2473. valid_fb:
  2474. mutex_lock(&dev->struct_mutex);
  2475. intel_state->vma =
  2476. intel_pin_and_fence_fb_obj(fb,
  2477. primary->state->rotation,
  2478. intel_plane_uses_fence(intel_state),
  2479. &intel_state->flags);
  2480. mutex_unlock(&dev->struct_mutex);
  2481. if (IS_ERR(intel_state->vma)) {
  2482. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2483. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2484. intel_state->vma = NULL;
  2485. drm_framebuffer_put(fb);
  2486. return;
  2487. }
  2488. obj = intel_fb_obj(fb);
  2489. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  2490. plane_state->src_x = 0;
  2491. plane_state->src_y = 0;
  2492. plane_state->src_w = fb->width << 16;
  2493. plane_state->src_h = fb->height << 16;
  2494. plane_state->crtc_x = 0;
  2495. plane_state->crtc_y = 0;
  2496. plane_state->crtc_w = fb->width;
  2497. plane_state->crtc_h = fb->height;
  2498. intel_state->base.src = drm_plane_state_src(plane_state);
  2499. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2500. if (i915_gem_object_is_tiled(obj))
  2501. dev_priv->preserve_bios_swizzle = true;
  2502. drm_framebuffer_get(fb);
  2503. primary->fb = primary->state->fb = fb;
  2504. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2505. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2506. to_intel_plane_state(plane_state),
  2507. true);
  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, int plane,
  2512. unsigned int rotation)
  2513. {
  2514. int cpp = fb->format->cpp[plane];
  2515. switch (fb->modifier) {
  2516. case DRM_FORMAT_MOD_LINEAR:
  2517. case I915_FORMAT_MOD_X_TILED:
  2518. switch (cpp) {
  2519. case 8:
  2520. return 4096;
  2521. case 4:
  2522. case 2:
  2523. case 1:
  2524. return 8192;
  2525. default:
  2526. MISSING_CASE(cpp);
  2527. break;
  2528. }
  2529. break;
  2530. case I915_FORMAT_MOD_Y_TILED_CCS:
  2531. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2532. /* FIXME AUX plane? */
  2533. case I915_FORMAT_MOD_Y_TILED:
  2534. case I915_FORMAT_MOD_Yf_TILED:
  2535. switch (cpp) {
  2536. case 8:
  2537. return 2048;
  2538. case 4:
  2539. return 4096;
  2540. case 2:
  2541. case 1:
  2542. return 8192;
  2543. default:
  2544. MISSING_CASE(cpp);
  2545. break;
  2546. }
  2547. break;
  2548. default:
  2549. MISSING_CASE(fb->modifier);
  2550. }
  2551. return 2048;
  2552. }
  2553. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2554. int main_x, int main_y, u32 main_offset)
  2555. {
  2556. const struct drm_framebuffer *fb = plane_state->base.fb;
  2557. int hsub = fb->format->hsub;
  2558. int vsub = fb->format->vsub;
  2559. int aux_x = plane_state->aux.x;
  2560. int aux_y = plane_state->aux.y;
  2561. u32 aux_offset = plane_state->aux.offset;
  2562. u32 alignment = intel_surf_alignment(fb, 1);
  2563. while (aux_offset >= main_offset && aux_y <= main_y) {
  2564. int x, y;
  2565. if (aux_x == main_x && aux_y == main_y)
  2566. break;
  2567. if (aux_offset == 0)
  2568. break;
  2569. x = aux_x / hsub;
  2570. y = aux_y / vsub;
  2571. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2572. aux_offset, aux_offset - alignment);
  2573. aux_x = x * hsub + aux_x % hsub;
  2574. aux_y = y * vsub + aux_y % vsub;
  2575. }
  2576. if (aux_x != main_x || aux_y != main_y)
  2577. return false;
  2578. plane_state->aux.offset = aux_offset;
  2579. plane_state->aux.x = aux_x;
  2580. plane_state->aux.y = aux_y;
  2581. return true;
  2582. }
  2583. static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
  2584. struct intel_plane_state *plane_state)
  2585. {
  2586. struct drm_i915_private *dev_priv =
  2587. to_i915(plane_state->base.plane->dev);
  2588. const struct drm_framebuffer *fb = plane_state->base.fb;
  2589. unsigned int rotation = plane_state->base.rotation;
  2590. int x = plane_state->base.src.x1 >> 16;
  2591. int y = plane_state->base.src.y1 >> 16;
  2592. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2593. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2594. int dst_x = plane_state->base.dst.x1;
  2595. int pipe_src_w = crtc_state->pipe_src_w;
  2596. int max_width = skl_max_plane_width(fb, 0, rotation);
  2597. int max_height = 4096;
  2598. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2599. if (w > max_width || h > max_height) {
  2600. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2601. w, h, max_width, max_height);
  2602. return -EINVAL;
  2603. }
  2604. /*
  2605. * Display WA #1175: cnl,glk
  2606. * Planes other than the cursor may cause FIFO underflow and display
  2607. * corruption if starting less than 4 pixels from the right edge of
  2608. * the screen.
  2609. * Besides the above WA fix the similar problem, where planes other
  2610. * than the cursor ending less than 4 pixels from the left edge of the
  2611. * screen may cause FIFO underflow and display corruption.
  2612. */
  2613. if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  2614. (dst_x + w < 4 || dst_x > pipe_src_w - 4)) {
  2615. DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
  2616. dst_x + w < 4 ? "end" : "start",
  2617. dst_x + w < 4 ? dst_x + w : dst_x,
  2618. 4, pipe_src_w - 4);
  2619. return -ERANGE;
  2620. }
  2621. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2622. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2623. alignment = intel_surf_alignment(fb, 0);
  2624. /*
  2625. * AUX surface offset is specified as the distance from the
  2626. * main surface offset, and it must be non-negative. Make
  2627. * sure that is what we will get.
  2628. */
  2629. if (offset > aux_offset)
  2630. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2631. offset, aux_offset & ~(alignment - 1));
  2632. /*
  2633. * When using an X-tiled surface, the plane blows up
  2634. * if the x offset + width exceed the stride.
  2635. *
  2636. * TODO: linear and Y-tiled seem fine, Yf untested,
  2637. */
  2638. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2639. int cpp = fb->format->cpp[0];
  2640. while ((x + w) * cpp > fb->pitches[0]) {
  2641. if (offset == 0) {
  2642. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2643. return -EINVAL;
  2644. }
  2645. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2646. offset, offset - alignment);
  2647. }
  2648. }
  2649. /*
  2650. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2651. * they match with the main surface x/y offsets.
  2652. */
  2653. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2654. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2655. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2656. if (offset == 0)
  2657. break;
  2658. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2659. offset, offset - alignment);
  2660. }
  2661. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2662. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2663. return -EINVAL;
  2664. }
  2665. }
  2666. plane_state->main.offset = offset;
  2667. plane_state->main.x = x;
  2668. plane_state->main.y = y;
  2669. return 0;
  2670. }
  2671. static int
  2672. skl_check_nv12_surface(const struct intel_crtc_state *crtc_state,
  2673. struct intel_plane_state *plane_state)
  2674. {
  2675. /* Display WA #1106 */
  2676. if (plane_state->base.rotation !=
  2677. (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
  2678. plane_state->base.rotation != DRM_MODE_ROTATE_270)
  2679. return 0;
  2680. /*
  2681. * src coordinates are rotated here.
  2682. * We check height but report it as width
  2683. */
  2684. if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
  2685. DRM_DEBUG_KMS("src width must be multiple "
  2686. "of 4 for rotated NV12\n");
  2687. return -EINVAL;
  2688. }
  2689. return 0;
  2690. }
  2691. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2692. {
  2693. const struct drm_framebuffer *fb = plane_state->base.fb;
  2694. unsigned int rotation = plane_state->base.rotation;
  2695. int max_width = skl_max_plane_width(fb, 1, rotation);
  2696. int max_height = 4096;
  2697. int x = plane_state->base.src.x1 >> 17;
  2698. int y = plane_state->base.src.y1 >> 17;
  2699. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2700. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2701. u32 offset;
  2702. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2703. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2704. /* FIXME not quite sure how/if these apply to the chroma plane */
  2705. if (w > max_width || h > max_height) {
  2706. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2707. w, h, max_width, max_height);
  2708. return -EINVAL;
  2709. }
  2710. plane_state->aux.offset = offset;
  2711. plane_state->aux.x = x;
  2712. plane_state->aux.y = y;
  2713. return 0;
  2714. }
  2715. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2716. {
  2717. const struct drm_framebuffer *fb = plane_state->base.fb;
  2718. int src_x = plane_state->base.src.x1 >> 16;
  2719. int src_y = plane_state->base.src.y1 >> 16;
  2720. int hsub = fb->format->hsub;
  2721. int vsub = fb->format->vsub;
  2722. int x = src_x / hsub;
  2723. int y = src_y / vsub;
  2724. u32 offset;
  2725. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2726. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2727. plane_state->base.rotation);
  2728. return -EINVAL;
  2729. }
  2730. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2731. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2732. plane_state->aux.offset = offset;
  2733. plane_state->aux.x = x * hsub + src_x % hsub;
  2734. plane_state->aux.y = y * vsub + src_y % vsub;
  2735. return 0;
  2736. }
  2737. int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
  2738. struct intel_plane_state *plane_state)
  2739. {
  2740. const struct drm_framebuffer *fb = plane_state->base.fb;
  2741. unsigned int rotation = plane_state->base.rotation;
  2742. int ret;
  2743. if (rotation & DRM_MODE_REFLECT_X &&
  2744. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2745. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2746. return -EINVAL;
  2747. }
  2748. if (!plane_state->base.visible)
  2749. return 0;
  2750. /* Rotate src coordinates to match rotated GTT view */
  2751. if (drm_rotation_90_or_270(rotation))
  2752. drm_rect_rotate(&plane_state->base.src,
  2753. fb->width << 16, fb->height << 16,
  2754. DRM_MODE_ROTATE_270);
  2755. /*
  2756. * Handle the AUX surface first since
  2757. * the main surface setup depends on it.
  2758. */
  2759. if (fb->format->format == DRM_FORMAT_NV12) {
  2760. ret = skl_check_nv12_surface(crtc_state, plane_state);
  2761. if (ret)
  2762. return ret;
  2763. ret = skl_check_nv12_aux_surface(plane_state);
  2764. if (ret)
  2765. return ret;
  2766. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2767. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2768. ret = skl_check_ccs_aux_surface(plane_state);
  2769. if (ret)
  2770. return ret;
  2771. } else {
  2772. plane_state->aux.offset = ~0xfff;
  2773. plane_state->aux.x = 0;
  2774. plane_state->aux.y = 0;
  2775. }
  2776. ret = skl_check_main_surface(crtc_state, plane_state);
  2777. if (ret)
  2778. return ret;
  2779. return 0;
  2780. }
  2781. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2782. const struct intel_plane_state *plane_state)
  2783. {
  2784. struct drm_i915_private *dev_priv =
  2785. to_i915(plane_state->base.plane->dev);
  2786. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2787. const struct drm_framebuffer *fb = plane_state->base.fb;
  2788. unsigned int rotation = plane_state->base.rotation;
  2789. u32 dspcntr;
  2790. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2791. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2792. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2793. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2794. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2795. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2796. if (INTEL_GEN(dev_priv) < 5)
  2797. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2798. switch (fb->format->format) {
  2799. case DRM_FORMAT_C8:
  2800. dspcntr |= DISPPLANE_8BPP;
  2801. break;
  2802. case DRM_FORMAT_XRGB1555:
  2803. dspcntr |= DISPPLANE_BGRX555;
  2804. break;
  2805. case DRM_FORMAT_RGB565:
  2806. dspcntr |= DISPPLANE_BGRX565;
  2807. break;
  2808. case DRM_FORMAT_XRGB8888:
  2809. dspcntr |= DISPPLANE_BGRX888;
  2810. break;
  2811. case DRM_FORMAT_XBGR8888:
  2812. dspcntr |= DISPPLANE_RGBX888;
  2813. break;
  2814. case DRM_FORMAT_XRGB2101010:
  2815. dspcntr |= DISPPLANE_BGRX101010;
  2816. break;
  2817. case DRM_FORMAT_XBGR2101010:
  2818. dspcntr |= DISPPLANE_RGBX101010;
  2819. break;
  2820. default:
  2821. MISSING_CASE(fb->format->format);
  2822. return 0;
  2823. }
  2824. if (INTEL_GEN(dev_priv) >= 4 &&
  2825. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2826. dspcntr |= DISPPLANE_TILED;
  2827. if (rotation & DRM_MODE_ROTATE_180)
  2828. dspcntr |= DISPPLANE_ROTATE_180;
  2829. if (rotation & DRM_MODE_REFLECT_X)
  2830. dspcntr |= DISPPLANE_MIRROR;
  2831. return dspcntr;
  2832. }
  2833. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2834. {
  2835. struct drm_i915_private *dev_priv =
  2836. to_i915(plane_state->base.plane->dev);
  2837. int src_x = plane_state->base.src.x1 >> 16;
  2838. int src_y = plane_state->base.src.y1 >> 16;
  2839. u32 offset;
  2840. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2841. if (INTEL_GEN(dev_priv) >= 4)
  2842. offset = intel_compute_tile_offset(&src_x, &src_y,
  2843. plane_state, 0);
  2844. else
  2845. offset = 0;
  2846. /* HSW/BDW do this automagically in hardware */
  2847. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2848. unsigned int rotation = plane_state->base.rotation;
  2849. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2850. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2851. if (rotation & DRM_MODE_ROTATE_180) {
  2852. src_x += src_w - 1;
  2853. src_y += src_h - 1;
  2854. } else if (rotation & DRM_MODE_REFLECT_X) {
  2855. src_x += src_w - 1;
  2856. }
  2857. }
  2858. plane_state->main.offset = offset;
  2859. plane_state->main.x = src_x;
  2860. plane_state->main.y = src_y;
  2861. return 0;
  2862. }
  2863. static void i9xx_update_plane(struct intel_plane *plane,
  2864. const struct intel_crtc_state *crtc_state,
  2865. const struct intel_plane_state *plane_state)
  2866. {
  2867. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2868. const struct drm_framebuffer *fb = plane_state->base.fb;
  2869. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2870. u32 linear_offset;
  2871. u32 dspcntr = plane_state->ctl;
  2872. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2873. int x = plane_state->main.x;
  2874. int y = plane_state->main.y;
  2875. unsigned long irqflags;
  2876. u32 dspaddr_offset;
  2877. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2878. if (INTEL_GEN(dev_priv) >= 4)
  2879. dspaddr_offset = plane_state->main.offset;
  2880. else
  2881. dspaddr_offset = linear_offset;
  2882. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2883. if (INTEL_GEN(dev_priv) < 4) {
  2884. /* pipesrc and dspsize control the size that is scaled from,
  2885. * which should always be the user's requested size.
  2886. */
  2887. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2888. ((crtc_state->pipe_src_h - 1) << 16) |
  2889. (crtc_state->pipe_src_w - 1));
  2890. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2891. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2892. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2893. ((crtc_state->pipe_src_h - 1) << 16) |
  2894. (crtc_state->pipe_src_w - 1));
  2895. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2896. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2897. }
  2898. I915_WRITE_FW(reg, dspcntr);
  2899. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2900. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2901. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2902. intel_plane_ggtt_offset(plane_state) +
  2903. dspaddr_offset);
  2904. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2905. } else if (INTEL_GEN(dev_priv) >= 4) {
  2906. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2907. intel_plane_ggtt_offset(plane_state) +
  2908. dspaddr_offset);
  2909. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2910. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2911. } else {
  2912. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2913. intel_plane_ggtt_offset(plane_state) +
  2914. dspaddr_offset);
  2915. }
  2916. POSTING_READ_FW(reg);
  2917. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2918. }
  2919. static void i9xx_disable_plane(struct intel_plane *plane,
  2920. struct intel_crtc *crtc)
  2921. {
  2922. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2923. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2924. unsigned long irqflags;
  2925. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2926. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2927. if (INTEL_GEN(dev_priv) >= 4)
  2928. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2929. else
  2930. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2931. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2932. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2933. }
  2934. static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
  2935. {
  2936. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2937. enum intel_display_power_domain power_domain;
  2938. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2939. enum pipe pipe = plane->pipe;
  2940. bool ret;
  2941. /*
  2942. * Not 100% correct for planes that can move between pipes,
  2943. * but that's only the case for gen2-4 which don't have any
  2944. * display power wells.
  2945. */
  2946. power_domain = POWER_DOMAIN_PIPE(pipe);
  2947. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2948. return false;
  2949. ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
  2950. intel_display_power_put(dev_priv, power_domain);
  2951. return ret;
  2952. }
  2953. static u32
  2954. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2955. {
  2956. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2957. return 64;
  2958. else
  2959. return intel_tile_width_bytes(fb, plane);
  2960. }
  2961. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2962. {
  2963. struct drm_device *dev = intel_crtc->base.dev;
  2964. struct drm_i915_private *dev_priv = to_i915(dev);
  2965. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2966. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2967. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2968. }
  2969. /*
  2970. * This function detaches (aka. unbinds) unused scalers in hardware
  2971. */
  2972. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2973. {
  2974. struct intel_crtc_scaler_state *scaler_state;
  2975. int i;
  2976. scaler_state = &intel_crtc->config->scaler_state;
  2977. /* loop through and disable scalers that aren't in use */
  2978. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2979. if (!scaler_state->scalers[i].in_use)
  2980. skl_detach_scaler(intel_crtc, i);
  2981. }
  2982. }
  2983. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2984. unsigned int rotation)
  2985. {
  2986. u32 stride;
  2987. if (plane >= fb->format->num_planes)
  2988. return 0;
  2989. stride = intel_fb_pitch(fb, plane, rotation);
  2990. /*
  2991. * The stride is either expressed as a multiple of 64 bytes chunks for
  2992. * linear buffers or in number of tiles for tiled buffers.
  2993. */
  2994. if (drm_rotation_90_or_270(rotation))
  2995. stride /= intel_tile_height(fb, plane);
  2996. else
  2997. stride /= intel_fb_stride_alignment(fb, plane);
  2998. return stride;
  2999. }
  3000. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  3001. {
  3002. switch (pixel_format) {
  3003. case DRM_FORMAT_C8:
  3004. return PLANE_CTL_FORMAT_INDEXED;
  3005. case DRM_FORMAT_RGB565:
  3006. return PLANE_CTL_FORMAT_RGB_565;
  3007. case DRM_FORMAT_XBGR8888:
  3008. case DRM_FORMAT_ABGR8888:
  3009. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  3010. case DRM_FORMAT_XRGB8888:
  3011. case DRM_FORMAT_ARGB8888:
  3012. return PLANE_CTL_FORMAT_XRGB_8888;
  3013. case DRM_FORMAT_XRGB2101010:
  3014. return PLANE_CTL_FORMAT_XRGB_2101010;
  3015. case DRM_FORMAT_XBGR2101010:
  3016. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  3017. case DRM_FORMAT_YUYV:
  3018. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  3019. case DRM_FORMAT_YVYU:
  3020. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  3021. case DRM_FORMAT_UYVY:
  3022. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  3023. case DRM_FORMAT_VYUY:
  3024. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  3025. case DRM_FORMAT_NV12:
  3026. return PLANE_CTL_FORMAT_NV12;
  3027. default:
  3028. MISSING_CASE(pixel_format);
  3029. }
  3030. return 0;
  3031. }
  3032. /*
  3033. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  3034. * to be already pre-multiplied. We need to add a knob (or a different
  3035. * DRM_FORMAT) for user-space to configure that.
  3036. */
  3037. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  3038. {
  3039. switch (pixel_format) {
  3040. case DRM_FORMAT_ABGR8888:
  3041. case DRM_FORMAT_ARGB8888:
  3042. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  3043. default:
  3044. return PLANE_CTL_ALPHA_DISABLE;
  3045. }
  3046. }
  3047. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  3048. {
  3049. switch (pixel_format) {
  3050. case DRM_FORMAT_ABGR8888:
  3051. case DRM_FORMAT_ARGB8888:
  3052. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  3053. default:
  3054. return PLANE_COLOR_ALPHA_DISABLE;
  3055. }
  3056. }
  3057. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  3058. {
  3059. switch (fb_modifier) {
  3060. case DRM_FORMAT_MOD_LINEAR:
  3061. break;
  3062. case I915_FORMAT_MOD_X_TILED:
  3063. return PLANE_CTL_TILED_X;
  3064. case I915_FORMAT_MOD_Y_TILED:
  3065. return PLANE_CTL_TILED_Y;
  3066. case I915_FORMAT_MOD_Y_TILED_CCS:
  3067. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  3068. case I915_FORMAT_MOD_Yf_TILED:
  3069. return PLANE_CTL_TILED_YF;
  3070. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3071. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  3072. default:
  3073. MISSING_CASE(fb_modifier);
  3074. }
  3075. return 0;
  3076. }
  3077. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  3078. {
  3079. switch (rotate) {
  3080. case DRM_MODE_ROTATE_0:
  3081. break;
  3082. /*
  3083. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3084. * while i915 HW rotation is clockwise, thats why this swapping.
  3085. */
  3086. case DRM_MODE_ROTATE_90:
  3087. return PLANE_CTL_ROTATE_270;
  3088. case DRM_MODE_ROTATE_180:
  3089. return PLANE_CTL_ROTATE_180;
  3090. case DRM_MODE_ROTATE_270:
  3091. return PLANE_CTL_ROTATE_90;
  3092. default:
  3093. MISSING_CASE(rotate);
  3094. }
  3095. return 0;
  3096. }
  3097. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3098. {
  3099. switch (reflect) {
  3100. case 0:
  3101. break;
  3102. case DRM_MODE_REFLECT_X:
  3103. return PLANE_CTL_FLIP_HORIZONTAL;
  3104. case DRM_MODE_REFLECT_Y:
  3105. default:
  3106. MISSING_CASE(reflect);
  3107. }
  3108. return 0;
  3109. }
  3110. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3111. const struct intel_plane_state *plane_state)
  3112. {
  3113. struct drm_i915_private *dev_priv =
  3114. to_i915(plane_state->base.plane->dev);
  3115. const struct drm_framebuffer *fb = plane_state->base.fb;
  3116. unsigned int rotation = plane_state->base.rotation;
  3117. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3118. u32 plane_ctl;
  3119. plane_ctl = PLANE_CTL_ENABLE;
  3120. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3121. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3122. plane_ctl |=
  3123. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3124. PLANE_CTL_PIPE_CSC_ENABLE |
  3125. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3126. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3127. plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
  3128. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3129. plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
  3130. }
  3131. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3132. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3133. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3134. if (INTEL_GEN(dev_priv) >= 10)
  3135. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3136. DRM_MODE_REFLECT_MASK);
  3137. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3138. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3139. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3140. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3141. return plane_ctl;
  3142. }
  3143. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3144. const struct intel_plane_state *plane_state)
  3145. {
  3146. struct drm_i915_private *dev_priv =
  3147. to_i915(plane_state->base.plane->dev);
  3148. const struct drm_framebuffer *fb = plane_state->base.fb;
  3149. u32 plane_color_ctl = 0;
  3150. if (INTEL_GEN(dev_priv) < 11) {
  3151. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3152. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3153. }
  3154. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3155. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3156. if (intel_format_is_yuv(fb->format->format)) {
  3157. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3158. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3159. else
  3160. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
  3161. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3162. plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
  3163. }
  3164. return plane_color_ctl;
  3165. }
  3166. static int
  3167. __intel_display_resume(struct drm_device *dev,
  3168. struct drm_atomic_state *state,
  3169. struct drm_modeset_acquire_ctx *ctx)
  3170. {
  3171. struct drm_crtc_state *crtc_state;
  3172. struct drm_crtc *crtc;
  3173. int i, ret;
  3174. intel_modeset_setup_hw_state(dev, ctx);
  3175. i915_redisable_vga(to_i915(dev));
  3176. if (!state)
  3177. return 0;
  3178. /*
  3179. * We've duplicated the state, pointers to the old state are invalid.
  3180. *
  3181. * Don't attempt to use the old state until we commit the duplicated state.
  3182. */
  3183. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3184. /*
  3185. * Force recalculation even if we restore
  3186. * current state. With fast modeset this may not result
  3187. * in a modeset when the state is compatible.
  3188. */
  3189. crtc_state->mode_changed = true;
  3190. }
  3191. /* ignore any reset values/BIOS leftovers in the WM registers */
  3192. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3193. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3194. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3195. WARN_ON(ret == -EDEADLK);
  3196. return ret;
  3197. }
  3198. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3199. {
  3200. return intel_has_gpu_reset(dev_priv) &&
  3201. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3202. }
  3203. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3204. {
  3205. struct drm_device *dev = &dev_priv->drm;
  3206. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3207. struct drm_atomic_state *state;
  3208. int ret;
  3209. /* reset doesn't touch the display */
  3210. if (!i915_modparams.force_reset_modeset_test &&
  3211. !gpu_reset_clobbers_display(dev_priv))
  3212. return;
  3213. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3214. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3215. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3216. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3217. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3218. i915_gem_set_wedged(dev_priv);
  3219. }
  3220. /*
  3221. * Need mode_config.mutex so that we don't
  3222. * trample ongoing ->detect() and whatnot.
  3223. */
  3224. mutex_lock(&dev->mode_config.mutex);
  3225. drm_modeset_acquire_init(ctx, 0);
  3226. while (1) {
  3227. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3228. if (ret != -EDEADLK)
  3229. break;
  3230. drm_modeset_backoff(ctx);
  3231. }
  3232. /*
  3233. * Disabling the crtcs gracefully seems nicer. Also the
  3234. * g33 docs say we should at least disable all the planes.
  3235. */
  3236. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3237. if (IS_ERR(state)) {
  3238. ret = PTR_ERR(state);
  3239. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3240. return;
  3241. }
  3242. ret = drm_atomic_helper_disable_all(dev, ctx);
  3243. if (ret) {
  3244. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3245. drm_atomic_state_put(state);
  3246. return;
  3247. }
  3248. dev_priv->modeset_restore_state = state;
  3249. state->acquire_ctx = ctx;
  3250. }
  3251. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3252. {
  3253. struct drm_device *dev = &dev_priv->drm;
  3254. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3255. struct drm_atomic_state *state;
  3256. int ret;
  3257. /* reset doesn't touch the display */
  3258. if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  3259. return;
  3260. state = fetch_and_zero(&dev_priv->modeset_restore_state);
  3261. if (!state)
  3262. goto unlock;
  3263. /* reset doesn't touch the display */
  3264. if (!gpu_reset_clobbers_display(dev_priv)) {
  3265. /* for testing only restore the display */
  3266. ret = __intel_display_resume(dev, state, ctx);
  3267. if (ret)
  3268. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3269. } else {
  3270. /*
  3271. * The display has been reset as well,
  3272. * so need a full re-initialization.
  3273. */
  3274. intel_runtime_pm_disable_interrupts(dev_priv);
  3275. intel_runtime_pm_enable_interrupts(dev_priv);
  3276. intel_pps_unlock_regs_wa(dev_priv);
  3277. intel_modeset_init_hw(dev);
  3278. intel_init_clock_gating(dev_priv);
  3279. spin_lock_irq(&dev_priv->irq_lock);
  3280. if (dev_priv->display.hpd_irq_setup)
  3281. dev_priv->display.hpd_irq_setup(dev_priv);
  3282. spin_unlock_irq(&dev_priv->irq_lock);
  3283. ret = __intel_display_resume(dev, state, ctx);
  3284. if (ret)
  3285. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3286. intel_hpd_init(dev_priv);
  3287. }
  3288. drm_atomic_state_put(state);
  3289. unlock:
  3290. drm_modeset_drop_locks(ctx);
  3291. drm_modeset_acquire_fini(ctx);
  3292. mutex_unlock(&dev->mode_config.mutex);
  3293. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3294. }
  3295. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3296. const struct intel_crtc_state *new_crtc_state)
  3297. {
  3298. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3299. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3300. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3301. crtc->base.mode = new_crtc_state->base.mode;
  3302. /*
  3303. * Update pipe size and adjust fitter if needed: the reason for this is
  3304. * that in compute_mode_changes we check the native mode (not the pfit
  3305. * mode) to see if we can flip rather than do a full mode set. In the
  3306. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3307. * pfit state, we'll end up with a big fb scanned out into the wrong
  3308. * sized surface.
  3309. */
  3310. I915_WRITE(PIPESRC(crtc->pipe),
  3311. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3312. (new_crtc_state->pipe_src_h - 1));
  3313. /* on skylake this is done by detaching scalers */
  3314. if (INTEL_GEN(dev_priv) >= 9) {
  3315. skl_detach_scalers(crtc);
  3316. if (new_crtc_state->pch_pfit.enabled)
  3317. skylake_pfit_enable(crtc);
  3318. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3319. if (new_crtc_state->pch_pfit.enabled)
  3320. ironlake_pfit_enable(crtc);
  3321. else if (old_crtc_state->pch_pfit.enabled)
  3322. ironlake_pfit_disable(crtc, true);
  3323. }
  3324. }
  3325. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3326. {
  3327. struct drm_device *dev = crtc->base.dev;
  3328. struct drm_i915_private *dev_priv = to_i915(dev);
  3329. int pipe = crtc->pipe;
  3330. i915_reg_t reg;
  3331. u32 temp;
  3332. /* enable normal train */
  3333. reg = FDI_TX_CTL(pipe);
  3334. temp = I915_READ(reg);
  3335. if (IS_IVYBRIDGE(dev_priv)) {
  3336. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3337. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3338. } else {
  3339. temp &= ~FDI_LINK_TRAIN_NONE;
  3340. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3341. }
  3342. I915_WRITE(reg, temp);
  3343. reg = FDI_RX_CTL(pipe);
  3344. temp = I915_READ(reg);
  3345. if (HAS_PCH_CPT(dev_priv)) {
  3346. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3347. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3348. } else {
  3349. temp &= ~FDI_LINK_TRAIN_NONE;
  3350. temp |= FDI_LINK_TRAIN_NONE;
  3351. }
  3352. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3353. /* wait one idle pattern time */
  3354. POSTING_READ(reg);
  3355. udelay(1000);
  3356. /* IVB wants error correction enabled */
  3357. if (IS_IVYBRIDGE(dev_priv))
  3358. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3359. FDI_FE_ERRC_ENABLE);
  3360. }
  3361. /* The FDI link training functions for ILK/Ibexpeak. */
  3362. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3363. const struct intel_crtc_state *crtc_state)
  3364. {
  3365. struct drm_device *dev = crtc->base.dev;
  3366. struct drm_i915_private *dev_priv = to_i915(dev);
  3367. int pipe = crtc->pipe;
  3368. i915_reg_t reg;
  3369. u32 temp, tries;
  3370. /* FDI needs bits from pipe first */
  3371. assert_pipe_enabled(dev_priv, pipe);
  3372. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3373. for train result */
  3374. reg = FDI_RX_IMR(pipe);
  3375. temp = I915_READ(reg);
  3376. temp &= ~FDI_RX_SYMBOL_LOCK;
  3377. temp &= ~FDI_RX_BIT_LOCK;
  3378. I915_WRITE(reg, temp);
  3379. I915_READ(reg);
  3380. udelay(150);
  3381. /* enable CPU FDI TX and PCH FDI RX */
  3382. reg = FDI_TX_CTL(pipe);
  3383. temp = I915_READ(reg);
  3384. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3385. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3386. temp &= ~FDI_LINK_TRAIN_NONE;
  3387. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3388. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3389. reg = FDI_RX_CTL(pipe);
  3390. temp = I915_READ(reg);
  3391. temp &= ~FDI_LINK_TRAIN_NONE;
  3392. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3393. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3394. POSTING_READ(reg);
  3395. udelay(150);
  3396. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3397. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3398. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3399. FDI_RX_PHASE_SYNC_POINTER_EN);
  3400. reg = FDI_RX_IIR(pipe);
  3401. for (tries = 0; tries < 5; tries++) {
  3402. temp = I915_READ(reg);
  3403. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3404. if ((temp & FDI_RX_BIT_LOCK)) {
  3405. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3406. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3407. break;
  3408. }
  3409. }
  3410. if (tries == 5)
  3411. DRM_ERROR("FDI train 1 fail!\n");
  3412. /* Train 2 */
  3413. reg = FDI_TX_CTL(pipe);
  3414. temp = I915_READ(reg);
  3415. temp &= ~FDI_LINK_TRAIN_NONE;
  3416. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3417. I915_WRITE(reg, temp);
  3418. reg = FDI_RX_CTL(pipe);
  3419. temp = I915_READ(reg);
  3420. temp &= ~FDI_LINK_TRAIN_NONE;
  3421. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3422. I915_WRITE(reg, temp);
  3423. POSTING_READ(reg);
  3424. udelay(150);
  3425. reg = FDI_RX_IIR(pipe);
  3426. for (tries = 0; tries < 5; tries++) {
  3427. temp = I915_READ(reg);
  3428. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3429. if (temp & FDI_RX_SYMBOL_LOCK) {
  3430. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3431. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3432. break;
  3433. }
  3434. }
  3435. if (tries == 5)
  3436. DRM_ERROR("FDI train 2 fail!\n");
  3437. DRM_DEBUG_KMS("FDI train done\n");
  3438. }
  3439. static const int snb_b_fdi_train_param[] = {
  3440. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3441. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3442. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3443. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3444. };
  3445. /* The FDI link training functions for SNB/Cougarpoint. */
  3446. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3447. const struct intel_crtc_state *crtc_state)
  3448. {
  3449. struct drm_device *dev = crtc->base.dev;
  3450. struct drm_i915_private *dev_priv = to_i915(dev);
  3451. int pipe = crtc->pipe;
  3452. i915_reg_t reg;
  3453. u32 temp, i, retry;
  3454. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3455. for train result */
  3456. reg = FDI_RX_IMR(pipe);
  3457. temp = I915_READ(reg);
  3458. temp &= ~FDI_RX_SYMBOL_LOCK;
  3459. temp &= ~FDI_RX_BIT_LOCK;
  3460. I915_WRITE(reg, temp);
  3461. POSTING_READ(reg);
  3462. udelay(150);
  3463. /* enable CPU FDI TX and PCH FDI RX */
  3464. reg = FDI_TX_CTL(pipe);
  3465. temp = I915_READ(reg);
  3466. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3467. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3468. temp &= ~FDI_LINK_TRAIN_NONE;
  3469. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3470. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3471. /* SNB-B */
  3472. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3473. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3474. I915_WRITE(FDI_RX_MISC(pipe),
  3475. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3476. reg = FDI_RX_CTL(pipe);
  3477. temp = I915_READ(reg);
  3478. if (HAS_PCH_CPT(dev_priv)) {
  3479. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3480. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3481. } else {
  3482. temp &= ~FDI_LINK_TRAIN_NONE;
  3483. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3484. }
  3485. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3486. POSTING_READ(reg);
  3487. udelay(150);
  3488. for (i = 0; i < 4; i++) {
  3489. reg = FDI_TX_CTL(pipe);
  3490. temp = I915_READ(reg);
  3491. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3492. temp |= snb_b_fdi_train_param[i];
  3493. I915_WRITE(reg, temp);
  3494. POSTING_READ(reg);
  3495. udelay(500);
  3496. for (retry = 0; retry < 5; retry++) {
  3497. reg = FDI_RX_IIR(pipe);
  3498. temp = I915_READ(reg);
  3499. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3500. if (temp & FDI_RX_BIT_LOCK) {
  3501. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3502. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3503. break;
  3504. }
  3505. udelay(50);
  3506. }
  3507. if (retry < 5)
  3508. break;
  3509. }
  3510. if (i == 4)
  3511. DRM_ERROR("FDI train 1 fail!\n");
  3512. /* Train 2 */
  3513. reg = FDI_TX_CTL(pipe);
  3514. temp = I915_READ(reg);
  3515. temp &= ~FDI_LINK_TRAIN_NONE;
  3516. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3517. if (IS_GEN6(dev_priv)) {
  3518. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3519. /* SNB-B */
  3520. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3521. }
  3522. I915_WRITE(reg, temp);
  3523. reg = FDI_RX_CTL(pipe);
  3524. temp = I915_READ(reg);
  3525. if (HAS_PCH_CPT(dev_priv)) {
  3526. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3527. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3528. } else {
  3529. temp &= ~FDI_LINK_TRAIN_NONE;
  3530. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3531. }
  3532. I915_WRITE(reg, temp);
  3533. POSTING_READ(reg);
  3534. udelay(150);
  3535. for (i = 0; i < 4; i++) {
  3536. reg = FDI_TX_CTL(pipe);
  3537. temp = I915_READ(reg);
  3538. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3539. temp |= snb_b_fdi_train_param[i];
  3540. I915_WRITE(reg, temp);
  3541. POSTING_READ(reg);
  3542. udelay(500);
  3543. for (retry = 0; retry < 5; retry++) {
  3544. reg = FDI_RX_IIR(pipe);
  3545. temp = I915_READ(reg);
  3546. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3547. if (temp & FDI_RX_SYMBOL_LOCK) {
  3548. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3549. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3550. break;
  3551. }
  3552. udelay(50);
  3553. }
  3554. if (retry < 5)
  3555. break;
  3556. }
  3557. if (i == 4)
  3558. DRM_ERROR("FDI train 2 fail!\n");
  3559. DRM_DEBUG_KMS("FDI train done.\n");
  3560. }
  3561. /* Manual link training for Ivy Bridge A0 parts */
  3562. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3563. const struct intel_crtc_state *crtc_state)
  3564. {
  3565. struct drm_device *dev = crtc->base.dev;
  3566. struct drm_i915_private *dev_priv = to_i915(dev);
  3567. int pipe = crtc->pipe;
  3568. i915_reg_t reg;
  3569. u32 temp, i, j;
  3570. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3571. for train result */
  3572. reg = FDI_RX_IMR(pipe);
  3573. temp = I915_READ(reg);
  3574. temp &= ~FDI_RX_SYMBOL_LOCK;
  3575. temp &= ~FDI_RX_BIT_LOCK;
  3576. I915_WRITE(reg, temp);
  3577. POSTING_READ(reg);
  3578. udelay(150);
  3579. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3580. I915_READ(FDI_RX_IIR(pipe)));
  3581. /* Try each vswing and preemphasis setting twice before moving on */
  3582. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3583. /* disable first in case we need to retry */
  3584. reg = FDI_TX_CTL(pipe);
  3585. temp = I915_READ(reg);
  3586. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3587. temp &= ~FDI_TX_ENABLE;
  3588. I915_WRITE(reg, temp);
  3589. reg = FDI_RX_CTL(pipe);
  3590. temp = I915_READ(reg);
  3591. temp &= ~FDI_LINK_TRAIN_AUTO;
  3592. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3593. temp &= ~FDI_RX_ENABLE;
  3594. I915_WRITE(reg, temp);
  3595. /* enable CPU FDI TX and PCH FDI RX */
  3596. reg = FDI_TX_CTL(pipe);
  3597. temp = I915_READ(reg);
  3598. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3599. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3600. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3601. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3602. temp |= snb_b_fdi_train_param[j/2];
  3603. temp |= FDI_COMPOSITE_SYNC;
  3604. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3605. I915_WRITE(FDI_RX_MISC(pipe),
  3606. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3607. reg = FDI_RX_CTL(pipe);
  3608. temp = I915_READ(reg);
  3609. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3610. temp |= FDI_COMPOSITE_SYNC;
  3611. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3612. POSTING_READ(reg);
  3613. udelay(1); /* should be 0.5us */
  3614. for (i = 0; i < 4; i++) {
  3615. reg = FDI_RX_IIR(pipe);
  3616. temp = I915_READ(reg);
  3617. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3618. if (temp & FDI_RX_BIT_LOCK ||
  3619. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3620. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3621. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3622. i);
  3623. break;
  3624. }
  3625. udelay(1); /* should be 0.5us */
  3626. }
  3627. if (i == 4) {
  3628. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3629. continue;
  3630. }
  3631. /* Train 2 */
  3632. reg = FDI_TX_CTL(pipe);
  3633. temp = I915_READ(reg);
  3634. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3635. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3636. I915_WRITE(reg, temp);
  3637. reg = FDI_RX_CTL(pipe);
  3638. temp = I915_READ(reg);
  3639. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3640. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3641. I915_WRITE(reg, temp);
  3642. POSTING_READ(reg);
  3643. udelay(2); /* should be 1.5us */
  3644. for (i = 0; i < 4; i++) {
  3645. reg = FDI_RX_IIR(pipe);
  3646. temp = I915_READ(reg);
  3647. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3648. if (temp & FDI_RX_SYMBOL_LOCK ||
  3649. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3650. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3651. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3652. i);
  3653. goto train_done;
  3654. }
  3655. udelay(2); /* should be 1.5us */
  3656. }
  3657. if (i == 4)
  3658. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3659. }
  3660. train_done:
  3661. DRM_DEBUG_KMS("FDI train done.\n");
  3662. }
  3663. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3664. {
  3665. struct drm_device *dev = intel_crtc->base.dev;
  3666. struct drm_i915_private *dev_priv = to_i915(dev);
  3667. int pipe = intel_crtc->pipe;
  3668. i915_reg_t reg;
  3669. u32 temp;
  3670. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3671. reg = FDI_RX_CTL(pipe);
  3672. temp = I915_READ(reg);
  3673. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3674. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3675. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3676. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3677. POSTING_READ(reg);
  3678. udelay(200);
  3679. /* Switch from Rawclk to PCDclk */
  3680. temp = I915_READ(reg);
  3681. I915_WRITE(reg, temp | FDI_PCDCLK);
  3682. POSTING_READ(reg);
  3683. udelay(200);
  3684. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3685. reg = FDI_TX_CTL(pipe);
  3686. temp = I915_READ(reg);
  3687. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3688. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3689. POSTING_READ(reg);
  3690. udelay(100);
  3691. }
  3692. }
  3693. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3694. {
  3695. struct drm_device *dev = intel_crtc->base.dev;
  3696. struct drm_i915_private *dev_priv = to_i915(dev);
  3697. int pipe = intel_crtc->pipe;
  3698. i915_reg_t reg;
  3699. u32 temp;
  3700. /* Switch from PCDclk to Rawclk */
  3701. reg = FDI_RX_CTL(pipe);
  3702. temp = I915_READ(reg);
  3703. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3704. /* Disable CPU FDI TX PLL */
  3705. reg = FDI_TX_CTL(pipe);
  3706. temp = I915_READ(reg);
  3707. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3708. POSTING_READ(reg);
  3709. udelay(100);
  3710. reg = FDI_RX_CTL(pipe);
  3711. temp = I915_READ(reg);
  3712. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3713. /* Wait for the clocks to turn off. */
  3714. POSTING_READ(reg);
  3715. udelay(100);
  3716. }
  3717. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3718. {
  3719. struct drm_device *dev = crtc->dev;
  3720. struct drm_i915_private *dev_priv = to_i915(dev);
  3721. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3722. int pipe = intel_crtc->pipe;
  3723. i915_reg_t reg;
  3724. u32 temp;
  3725. /* disable CPU FDI tx and PCH FDI rx */
  3726. reg = FDI_TX_CTL(pipe);
  3727. temp = I915_READ(reg);
  3728. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3729. POSTING_READ(reg);
  3730. reg = FDI_RX_CTL(pipe);
  3731. temp = I915_READ(reg);
  3732. temp &= ~(0x7 << 16);
  3733. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3734. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3735. POSTING_READ(reg);
  3736. udelay(100);
  3737. /* Ironlake workaround, disable clock pointer after downing FDI */
  3738. if (HAS_PCH_IBX(dev_priv))
  3739. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3740. /* still set train pattern 1 */
  3741. reg = FDI_TX_CTL(pipe);
  3742. temp = I915_READ(reg);
  3743. temp &= ~FDI_LINK_TRAIN_NONE;
  3744. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3745. I915_WRITE(reg, temp);
  3746. reg = FDI_RX_CTL(pipe);
  3747. temp = I915_READ(reg);
  3748. if (HAS_PCH_CPT(dev_priv)) {
  3749. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3750. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3751. } else {
  3752. temp &= ~FDI_LINK_TRAIN_NONE;
  3753. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3754. }
  3755. /* BPC in FDI rx is consistent with that in PIPECONF */
  3756. temp &= ~(0x07 << 16);
  3757. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3758. I915_WRITE(reg, temp);
  3759. POSTING_READ(reg);
  3760. udelay(100);
  3761. }
  3762. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3763. {
  3764. struct drm_crtc *crtc;
  3765. bool cleanup_done;
  3766. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3767. struct drm_crtc_commit *commit;
  3768. spin_lock(&crtc->commit_lock);
  3769. commit = list_first_entry_or_null(&crtc->commit_list,
  3770. struct drm_crtc_commit, commit_entry);
  3771. cleanup_done = commit ?
  3772. try_wait_for_completion(&commit->cleanup_done) : true;
  3773. spin_unlock(&crtc->commit_lock);
  3774. if (cleanup_done)
  3775. continue;
  3776. drm_crtc_wait_one_vblank(crtc);
  3777. return true;
  3778. }
  3779. return false;
  3780. }
  3781. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3782. {
  3783. u32 temp;
  3784. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3785. mutex_lock(&dev_priv->sb_lock);
  3786. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3787. temp |= SBI_SSCCTL_DISABLE;
  3788. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3789. mutex_unlock(&dev_priv->sb_lock);
  3790. }
  3791. /* Program iCLKIP clock to the desired frequency */
  3792. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3793. {
  3794. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3795. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3796. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3797. u32 temp;
  3798. lpt_disable_iclkip(dev_priv);
  3799. /* The iCLK virtual clock root frequency is in MHz,
  3800. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3801. * divisors, it is necessary to divide one by another, so we
  3802. * convert the virtual clock precision to KHz here for higher
  3803. * precision.
  3804. */
  3805. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3806. u32 iclk_virtual_root_freq = 172800 * 1000;
  3807. u32 iclk_pi_range = 64;
  3808. u32 desired_divisor;
  3809. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3810. clock << auxdiv);
  3811. divsel = (desired_divisor / iclk_pi_range) - 2;
  3812. phaseinc = desired_divisor % iclk_pi_range;
  3813. /*
  3814. * Near 20MHz is a corner case which is
  3815. * out of range for the 7-bit divisor
  3816. */
  3817. if (divsel <= 0x7f)
  3818. break;
  3819. }
  3820. /* This should not happen with any sane values */
  3821. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3822. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3823. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3824. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3825. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3826. clock,
  3827. auxdiv,
  3828. divsel,
  3829. phasedir,
  3830. phaseinc);
  3831. mutex_lock(&dev_priv->sb_lock);
  3832. /* Program SSCDIVINTPHASE6 */
  3833. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3834. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3835. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3836. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3837. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3838. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3839. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3840. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3841. /* Program SSCAUXDIV */
  3842. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3843. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3844. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3845. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3846. /* Enable modulator and associated divider */
  3847. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3848. temp &= ~SBI_SSCCTL_DISABLE;
  3849. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3850. mutex_unlock(&dev_priv->sb_lock);
  3851. /* Wait for initialization time */
  3852. udelay(24);
  3853. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3854. }
  3855. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3856. {
  3857. u32 divsel, phaseinc, auxdiv;
  3858. u32 iclk_virtual_root_freq = 172800 * 1000;
  3859. u32 iclk_pi_range = 64;
  3860. u32 desired_divisor;
  3861. u32 temp;
  3862. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3863. return 0;
  3864. mutex_lock(&dev_priv->sb_lock);
  3865. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3866. if (temp & SBI_SSCCTL_DISABLE) {
  3867. mutex_unlock(&dev_priv->sb_lock);
  3868. return 0;
  3869. }
  3870. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3871. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3872. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3873. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3874. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3875. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3876. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3877. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3878. mutex_unlock(&dev_priv->sb_lock);
  3879. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3880. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3881. desired_divisor << auxdiv);
  3882. }
  3883. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3884. enum pipe pch_transcoder)
  3885. {
  3886. struct drm_device *dev = crtc->base.dev;
  3887. struct drm_i915_private *dev_priv = to_i915(dev);
  3888. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3889. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3890. I915_READ(HTOTAL(cpu_transcoder)));
  3891. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3892. I915_READ(HBLANK(cpu_transcoder)));
  3893. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3894. I915_READ(HSYNC(cpu_transcoder)));
  3895. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3896. I915_READ(VTOTAL(cpu_transcoder)));
  3897. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3898. I915_READ(VBLANK(cpu_transcoder)));
  3899. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3900. I915_READ(VSYNC(cpu_transcoder)));
  3901. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3902. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3903. }
  3904. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3905. {
  3906. struct drm_i915_private *dev_priv = to_i915(dev);
  3907. uint32_t temp;
  3908. temp = I915_READ(SOUTH_CHICKEN1);
  3909. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3910. return;
  3911. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3912. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3913. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3914. if (enable)
  3915. temp |= FDI_BC_BIFURCATION_SELECT;
  3916. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3917. I915_WRITE(SOUTH_CHICKEN1, temp);
  3918. POSTING_READ(SOUTH_CHICKEN1);
  3919. }
  3920. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3921. {
  3922. struct drm_device *dev = intel_crtc->base.dev;
  3923. switch (intel_crtc->pipe) {
  3924. case PIPE_A:
  3925. break;
  3926. case PIPE_B:
  3927. if (intel_crtc->config->fdi_lanes > 2)
  3928. cpt_set_fdi_bc_bifurcation(dev, false);
  3929. else
  3930. cpt_set_fdi_bc_bifurcation(dev, true);
  3931. break;
  3932. case PIPE_C:
  3933. cpt_set_fdi_bc_bifurcation(dev, true);
  3934. break;
  3935. default:
  3936. BUG();
  3937. }
  3938. }
  3939. /* Return which DP Port should be selected for Transcoder DP control */
  3940. static enum port
  3941. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3942. {
  3943. struct drm_device *dev = crtc->base.dev;
  3944. struct intel_encoder *encoder;
  3945. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3946. if (encoder->type == INTEL_OUTPUT_DP ||
  3947. encoder->type == INTEL_OUTPUT_EDP)
  3948. return encoder->port;
  3949. }
  3950. return -1;
  3951. }
  3952. /*
  3953. * Enable PCH resources required for PCH ports:
  3954. * - PCH PLLs
  3955. * - FDI training & RX/TX
  3956. * - update transcoder timings
  3957. * - DP transcoding bits
  3958. * - transcoder
  3959. */
  3960. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3961. {
  3962. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3963. struct drm_device *dev = crtc->base.dev;
  3964. struct drm_i915_private *dev_priv = to_i915(dev);
  3965. int pipe = crtc->pipe;
  3966. u32 temp;
  3967. assert_pch_transcoder_disabled(dev_priv, pipe);
  3968. if (IS_IVYBRIDGE(dev_priv))
  3969. ivybridge_update_fdi_bc_bifurcation(crtc);
  3970. /* Write the TU size bits before fdi link training, so that error
  3971. * detection works. */
  3972. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3973. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3974. /* For PCH output, training FDI link */
  3975. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3976. /* We need to program the right clock selection before writing the pixel
  3977. * mutliplier into the DPLL. */
  3978. if (HAS_PCH_CPT(dev_priv)) {
  3979. u32 sel;
  3980. temp = I915_READ(PCH_DPLL_SEL);
  3981. temp |= TRANS_DPLL_ENABLE(pipe);
  3982. sel = TRANS_DPLLB_SEL(pipe);
  3983. if (crtc_state->shared_dpll ==
  3984. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3985. temp |= sel;
  3986. else
  3987. temp &= ~sel;
  3988. I915_WRITE(PCH_DPLL_SEL, temp);
  3989. }
  3990. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3991. * transcoder, and we actually should do this to not upset any PCH
  3992. * transcoder that already use the clock when we share it.
  3993. *
  3994. * Note that enable_shared_dpll tries to do the right thing, but
  3995. * get_shared_dpll unconditionally resets the pll - we need that to have
  3996. * the right LVDS enable sequence. */
  3997. intel_enable_shared_dpll(crtc);
  3998. /* set transcoder timing, panel must allow it */
  3999. assert_panel_unlocked(dev_priv, pipe);
  4000. ironlake_pch_transcoder_set_timings(crtc, pipe);
  4001. intel_fdi_normal_train(crtc);
  4002. /* For PCH DP, enable TRANS_DP_CTL */
  4003. if (HAS_PCH_CPT(dev_priv) &&
  4004. intel_crtc_has_dp_encoder(crtc_state)) {
  4005. const struct drm_display_mode *adjusted_mode =
  4006. &crtc_state->base.adjusted_mode;
  4007. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  4008. i915_reg_t reg = TRANS_DP_CTL(pipe);
  4009. temp = I915_READ(reg);
  4010. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  4011. TRANS_DP_SYNC_MASK |
  4012. TRANS_DP_BPC_MASK);
  4013. temp |= TRANS_DP_OUTPUT_ENABLE;
  4014. temp |= bpc << 9; /* same format but at 11:9 */
  4015. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  4016. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  4017. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  4018. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  4019. switch (intel_trans_dp_port_sel(crtc)) {
  4020. case PORT_B:
  4021. temp |= TRANS_DP_PORT_SEL_B;
  4022. break;
  4023. case PORT_C:
  4024. temp |= TRANS_DP_PORT_SEL_C;
  4025. break;
  4026. case PORT_D:
  4027. temp |= TRANS_DP_PORT_SEL_D;
  4028. break;
  4029. default:
  4030. BUG();
  4031. }
  4032. I915_WRITE(reg, temp);
  4033. }
  4034. ironlake_enable_pch_transcoder(dev_priv, pipe);
  4035. }
  4036. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  4037. {
  4038. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4039. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4040. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  4041. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4042. lpt_program_iclkip(crtc);
  4043. /* Set transcoder timing. */
  4044. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4045. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4046. }
  4047. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4048. {
  4049. struct drm_i915_private *dev_priv = to_i915(dev);
  4050. i915_reg_t dslreg = PIPEDSL(pipe);
  4051. u32 temp;
  4052. temp = I915_READ(dslreg);
  4053. udelay(500);
  4054. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4055. if (wait_for(I915_READ(dslreg) != temp, 5))
  4056. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4057. }
  4058. }
  4059. static int
  4060. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4061. unsigned int scaler_user, int *scaler_id,
  4062. int src_w, int src_h, int dst_w, int dst_h,
  4063. bool plane_scaler_check,
  4064. uint32_t pixel_format)
  4065. {
  4066. struct intel_crtc_scaler_state *scaler_state =
  4067. &crtc_state->scaler_state;
  4068. struct intel_crtc *intel_crtc =
  4069. to_intel_crtc(crtc_state->base.crtc);
  4070. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4071. const struct drm_display_mode *adjusted_mode =
  4072. &crtc_state->base.adjusted_mode;
  4073. int need_scaling;
  4074. /*
  4075. * Src coordinates are already rotated by 270 degrees for
  4076. * the 90/270 degree plane rotation cases (to match the
  4077. * GTT mapping), hence no need to account for rotation here.
  4078. */
  4079. need_scaling = src_w != dst_w || src_h != dst_h;
  4080. if (plane_scaler_check)
  4081. if (pixel_format == DRM_FORMAT_NV12)
  4082. need_scaling = true;
  4083. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4084. need_scaling = true;
  4085. /*
  4086. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4087. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4088. * Once NV12 is enabled, handle it here while allocating scaler
  4089. * for NV12.
  4090. */
  4091. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4092. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4093. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4094. return -EINVAL;
  4095. }
  4096. /*
  4097. * if plane is being disabled or scaler is no more required or force detach
  4098. * - free scaler binded to this plane/crtc
  4099. * - in order to do this, update crtc->scaler_usage
  4100. *
  4101. * Here scaler state in crtc_state is set free so that
  4102. * scaler can be assigned to other user. Actual register
  4103. * update to free the scaler is done in plane/panel-fit programming.
  4104. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4105. */
  4106. if (force_detach || !need_scaling) {
  4107. if (*scaler_id >= 0) {
  4108. scaler_state->scaler_users &= ~(1 << scaler_user);
  4109. scaler_state->scalers[*scaler_id].in_use = 0;
  4110. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4111. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4112. intel_crtc->pipe, scaler_user, *scaler_id,
  4113. scaler_state->scaler_users);
  4114. *scaler_id = -1;
  4115. }
  4116. return 0;
  4117. }
  4118. if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
  4119. (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
  4120. DRM_DEBUG_KMS("NV12: src dimensions not met\n");
  4121. return -EINVAL;
  4122. }
  4123. /* range checks */
  4124. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4125. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4126. (IS_GEN11(dev_priv) &&
  4127. (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
  4128. dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
  4129. (!IS_GEN11(dev_priv) &&
  4130. (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4131. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
  4132. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4133. "size is out of scaler range\n",
  4134. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4135. return -EINVAL;
  4136. }
  4137. /* mark this plane as a scaler user in crtc_state */
  4138. scaler_state->scaler_users |= (1 << scaler_user);
  4139. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4140. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4141. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4142. scaler_state->scaler_users);
  4143. return 0;
  4144. }
  4145. /**
  4146. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4147. *
  4148. * @state: crtc's scaler state
  4149. *
  4150. * Return
  4151. * 0 - scaler_usage updated successfully
  4152. * error - requested scaling cannot be supported or other error condition
  4153. */
  4154. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4155. {
  4156. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4157. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4158. &state->scaler_state.scaler_id,
  4159. state->pipe_src_w, state->pipe_src_h,
  4160. adjusted_mode->crtc_hdisplay,
  4161. adjusted_mode->crtc_vdisplay, false, 0);
  4162. }
  4163. /**
  4164. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4165. * @crtc_state: crtc's scaler state
  4166. * @plane_state: atomic plane state to update
  4167. *
  4168. * Return
  4169. * 0 - scaler_usage updated successfully
  4170. * error - requested scaling cannot be supported or other error condition
  4171. */
  4172. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4173. struct intel_plane_state *plane_state)
  4174. {
  4175. struct intel_plane *intel_plane =
  4176. to_intel_plane(plane_state->base.plane);
  4177. struct drm_framebuffer *fb = plane_state->base.fb;
  4178. int ret;
  4179. bool force_detach = !fb || !plane_state->base.visible;
  4180. ret = skl_update_scaler(crtc_state, force_detach,
  4181. drm_plane_index(&intel_plane->base),
  4182. &plane_state->scaler_id,
  4183. drm_rect_width(&plane_state->base.src) >> 16,
  4184. drm_rect_height(&plane_state->base.src) >> 16,
  4185. drm_rect_width(&plane_state->base.dst),
  4186. drm_rect_height(&plane_state->base.dst),
  4187. fb ? true : false, fb ? fb->format->format : 0);
  4188. if (ret || plane_state->scaler_id < 0)
  4189. return ret;
  4190. /* check colorkey */
  4191. if (plane_state->ckey.flags) {
  4192. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4193. intel_plane->base.base.id,
  4194. intel_plane->base.name);
  4195. return -EINVAL;
  4196. }
  4197. /* Check src format */
  4198. switch (fb->format->format) {
  4199. case DRM_FORMAT_RGB565:
  4200. case DRM_FORMAT_XBGR8888:
  4201. case DRM_FORMAT_XRGB8888:
  4202. case DRM_FORMAT_ABGR8888:
  4203. case DRM_FORMAT_ARGB8888:
  4204. case DRM_FORMAT_XRGB2101010:
  4205. case DRM_FORMAT_XBGR2101010:
  4206. case DRM_FORMAT_YUYV:
  4207. case DRM_FORMAT_YVYU:
  4208. case DRM_FORMAT_UYVY:
  4209. case DRM_FORMAT_VYUY:
  4210. case DRM_FORMAT_NV12:
  4211. break;
  4212. default:
  4213. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4214. intel_plane->base.base.id, intel_plane->base.name,
  4215. fb->base.id, fb->format->format);
  4216. return -EINVAL;
  4217. }
  4218. return 0;
  4219. }
  4220. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4221. {
  4222. int i;
  4223. for (i = 0; i < crtc->num_scalers; i++)
  4224. skl_detach_scaler(crtc, i);
  4225. }
  4226. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4227. {
  4228. struct drm_device *dev = crtc->base.dev;
  4229. struct drm_i915_private *dev_priv = to_i915(dev);
  4230. int pipe = crtc->pipe;
  4231. struct intel_crtc_scaler_state *scaler_state =
  4232. &crtc->config->scaler_state;
  4233. if (crtc->config->pch_pfit.enabled) {
  4234. int id;
  4235. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4236. return;
  4237. id = scaler_state->scaler_id;
  4238. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4239. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4240. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4241. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4242. }
  4243. }
  4244. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4245. {
  4246. struct drm_device *dev = crtc->base.dev;
  4247. struct drm_i915_private *dev_priv = to_i915(dev);
  4248. int pipe = crtc->pipe;
  4249. if (crtc->config->pch_pfit.enabled) {
  4250. /* Force use of hard-coded filter coefficients
  4251. * as some pre-programmed values are broken,
  4252. * e.g. x201.
  4253. */
  4254. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4255. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4256. PF_PIPE_SEL_IVB(pipe));
  4257. else
  4258. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4259. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4260. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4261. }
  4262. }
  4263. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4264. {
  4265. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4266. struct drm_device *dev = crtc->base.dev;
  4267. struct drm_i915_private *dev_priv = to_i915(dev);
  4268. if (!crtc_state->ips_enabled)
  4269. return;
  4270. /*
  4271. * We can only enable IPS after we enable a plane and wait for a vblank
  4272. * This function is called from post_plane_update, which is run after
  4273. * a vblank wait.
  4274. */
  4275. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4276. if (IS_BROADWELL(dev_priv)) {
  4277. mutex_lock(&dev_priv->pcu_lock);
  4278. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4279. IPS_ENABLE | IPS_PCODE_CONTROL));
  4280. mutex_unlock(&dev_priv->pcu_lock);
  4281. /* Quoting Art Runyan: "its not safe to expect any particular
  4282. * value in IPS_CTL bit 31 after enabling IPS through the
  4283. * mailbox." Moreover, the mailbox may return a bogus state,
  4284. * so we need to just enable it and continue on.
  4285. */
  4286. } else {
  4287. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4288. /* The bit only becomes 1 in the next vblank, so this wait here
  4289. * is essentially intel_wait_for_vblank. If we don't have this
  4290. * and don't wait for vblanks until the end of crtc_enable, then
  4291. * the HW state readout code will complain that the expected
  4292. * IPS_CTL value is not the one we read. */
  4293. if (intel_wait_for_register(dev_priv,
  4294. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4295. 50))
  4296. DRM_ERROR("Timed out waiting for IPS enable\n");
  4297. }
  4298. }
  4299. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4300. {
  4301. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4302. struct drm_device *dev = crtc->base.dev;
  4303. struct drm_i915_private *dev_priv = to_i915(dev);
  4304. if (!crtc_state->ips_enabled)
  4305. return;
  4306. if (IS_BROADWELL(dev_priv)) {
  4307. mutex_lock(&dev_priv->pcu_lock);
  4308. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4309. mutex_unlock(&dev_priv->pcu_lock);
  4310. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4311. if (intel_wait_for_register(dev_priv,
  4312. IPS_CTL, IPS_ENABLE, 0,
  4313. 42))
  4314. DRM_ERROR("Timed out waiting for IPS disable\n");
  4315. } else {
  4316. I915_WRITE(IPS_CTL, 0);
  4317. POSTING_READ(IPS_CTL);
  4318. }
  4319. /* We need to wait for a vblank before we can disable the plane. */
  4320. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4321. }
  4322. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4323. {
  4324. if (intel_crtc->overlay) {
  4325. struct drm_device *dev = intel_crtc->base.dev;
  4326. mutex_lock(&dev->struct_mutex);
  4327. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4328. mutex_unlock(&dev->struct_mutex);
  4329. }
  4330. /* Let userspace switch the overlay on again. In most cases userspace
  4331. * has to recompute where to put it anyway.
  4332. */
  4333. }
  4334. /**
  4335. * intel_post_enable_primary - Perform operations after enabling primary plane
  4336. * @crtc: the CRTC whose primary plane was just enabled
  4337. * @new_crtc_state: the enabling state
  4338. *
  4339. * Performs potentially sleeping operations that must be done after the primary
  4340. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4341. * called due to an explicit primary plane update, or due to an implicit
  4342. * re-enable that is caused when a sprite plane is updated to no longer
  4343. * completely hide the primary plane.
  4344. */
  4345. static void
  4346. intel_post_enable_primary(struct drm_crtc *crtc,
  4347. const struct intel_crtc_state *new_crtc_state)
  4348. {
  4349. struct drm_device *dev = crtc->dev;
  4350. struct drm_i915_private *dev_priv = to_i915(dev);
  4351. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4352. int pipe = intel_crtc->pipe;
  4353. /*
  4354. * Gen2 reports pipe underruns whenever all planes are disabled.
  4355. * So don't enable underrun reporting before at least some planes
  4356. * are enabled.
  4357. * FIXME: Need to fix the logic to work when we turn off all planes
  4358. * but leave the pipe running.
  4359. */
  4360. if (IS_GEN2(dev_priv))
  4361. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4362. /* Underruns don't always raise interrupts, so check manually. */
  4363. intel_check_cpu_fifo_underruns(dev_priv);
  4364. intel_check_pch_fifo_underruns(dev_priv);
  4365. }
  4366. /* FIXME get rid of this and use pre_plane_update */
  4367. static void
  4368. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4369. {
  4370. struct drm_device *dev = crtc->dev;
  4371. struct drm_i915_private *dev_priv = to_i915(dev);
  4372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4373. int pipe = intel_crtc->pipe;
  4374. /*
  4375. * Gen2 reports pipe underruns whenever all planes are disabled.
  4376. * So disable underrun reporting before all the planes get disabled.
  4377. */
  4378. if (IS_GEN2(dev_priv))
  4379. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4380. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4381. /*
  4382. * Vblank time updates from the shadow to live plane control register
  4383. * are blocked if the memory self-refresh mode is active at that
  4384. * moment. So to make sure the plane gets truly disabled, disable
  4385. * first the self-refresh mode. The self-refresh enable bit in turn
  4386. * will be checked/applied by the HW only at the next frame start
  4387. * event which is after the vblank start event, so we need to have a
  4388. * wait-for-vblank between disabling the plane and the pipe.
  4389. */
  4390. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4391. intel_set_memory_cxsr(dev_priv, false))
  4392. intel_wait_for_vblank(dev_priv, pipe);
  4393. }
  4394. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4395. const struct intel_crtc_state *new_crtc_state)
  4396. {
  4397. if (!old_crtc_state->ips_enabled)
  4398. return false;
  4399. if (needs_modeset(&new_crtc_state->base))
  4400. return true;
  4401. return !new_crtc_state->ips_enabled;
  4402. }
  4403. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4404. const struct intel_crtc_state *new_crtc_state)
  4405. {
  4406. if (!new_crtc_state->ips_enabled)
  4407. return false;
  4408. if (needs_modeset(&new_crtc_state->base))
  4409. return true;
  4410. /*
  4411. * We can't read out IPS on broadwell, assume the worst and
  4412. * forcibly enable IPS on the first fastset.
  4413. */
  4414. if (new_crtc_state->update_pipe &&
  4415. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4416. return true;
  4417. return !old_crtc_state->ips_enabled;
  4418. }
  4419. static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
  4420. const struct intel_crtc_state *crtc_state)
  4421. {
  4422. if (!crtc_state->nv12_planes)
  4423. return false;
  4424. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  4425. return false;
  4426. if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
  4427. IS_CANNONLAKE(dev_priv))
  4428. return true;
  4429. return false;
  4430. }
  4431. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4432. {
  4433. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4434. struct drm_device *dev = crtc->base.dev;
  4435. struct drm_i915_private *dev_priv = to_i915(dev);
  4436. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4437. struct intel_crtc_state *pipe_config =
  4438. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4439. crtc);
  4440. struct drm_plane *primary = crtc->base.primary;
  4441. struct drm_plane_state *old_primary_state =
  4442. drm_atomic_get_old_plane_state(old_state, primary);
  4443. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4444. if (pipe_config->update_wm_post && pipe_config->base.active)
  4445. intel_update_watermarks(crtc);
  4446. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4447. hsw_enable_ips(pipe_config);
  4448. if (old_primary_state) {
  4449. struct drm_plane_state *new_primary_state =
  4450. drm_atomic_get_new_plane_state(old_state, primary);
  4451. intel_fbc_post_update(crtc);
  4452. if (new_primary_state->visible &&
  4453. (needs_modeset(&pipe_config->base) ||
  4454. !old_primary_state->visible))
  4455. intel_post_enable_primary(&crtc->base, pipe_config);
  4456. }
  4457. /* Display WA 827 */
  4458. if (needs_nv12_wa(dev_priv, old_crtc_state) &&
  4459. !needs_nv12_wa(dev_priv, pipe_config)) {
  4460. skl_wa_clkgate(dev_priv, crtc->pipe, false);
  4461. skl_wa_528(dev_priv, crtc->pipe, false);
  4462. }
  4463. }
  4464. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4465. struct intel_crtc_state *pipe_config)
  4466. {
  4467. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4468. struct drm_device *dev = crtc->base.dev;
  4469. struct drm_i915_private *dev_priv = to_i915(dev);
  4470. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4471. struct drm_plane *primary = crtc->base.primary;
  4472. struct drm_plane_state *old_primary_state =
  4473. drm_atomic_get_old_plane_state(old_state, primary);
  4474. bool modeset = needs_modeset(&pipe_config->base);
  4475. struct intel_atomic_state *old_intel_state =
  4476. to_intel_atomic_state(old_state);
  4477. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4478. hsw_disable_ips(old_crtc_state);
  4479. if (old_primary_state) {
  4480. struct intel_plane_state *new_primary_state =
  4481. intel_atomic_get_new_plane_state(old_intel_state,
  4482. to_intel_plane(primary));
  4483. intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
  4484. /*
  4485. * Gen2 reports pipe underruns whenever all planes are disabled.
  4486. * So disable underrun reporting before all the planes get disabled.
  4487. */
  4488. if (IS_GEN2(dev_priv) && old_primary_state->visible &&
  4489. (modeset || !new_primary_state->base.visible))
  4490. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4491. }
  4492. /* Display WA 827 */
  4493. if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
  4494. needs_nv12_wa(dev_priv, pipe_config)) {
  4495. skl_wa_clkgate(dev_priv, crtc->pipe, true);
  4496. skl_wa_528(dev_priv, crtc->pipe, true);
  4497. }
  4498. /*
  4499. * Vblank time updates from the shadow to live plane control register
  4500. * are blocked if the memory self-refresh mode is active at that
  4501. * moment. So to make sure the plane gets truly disabled, disable
  4502. * first the self-refresh mode. The self-refresh enable bit in turn
  4503. * will be checked/applied by the HW only at the next frame start
  4504. * event which is after the vblank start event, so we need to have a
  4505. * wait-for-vblank between disabling the plane and the pipe.
  4506. */
  4507. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4508. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4509. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4510. /*
  4511. * IVB workaround: must disable low power watermarks for at least
  4512. * one frame before enabling scaling. LP watermarks can be re-enabled
  4513. * when scaling is disabled.
  4514. *
  4515. * WaCxSRDisabledForSpriteScaling:ivb
  4516. */
  4517. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4518. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4519. /*
  4520. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4521. * watermark programming here.
  4522. */
  4523. if (needs_modeset(&pipe_config->base))
  4524. return;
  4525. /*
  4526. * For platforms that support atomic watermarks, program the
  4527. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4528. * will be the intermediate values that are safe for both pre- and
  4529. * post- vblank; when vblank happens, the 'active' values will be set
  4530. * to the final 'target' values and we'll do this again to get the
  4531. * optimal watermarks. For gen9+ platforms, the values we program here
  4532. * will be the final target values which will get automatically latched
  4533. * at vblank time; no further programming will be necessary.
  4534. *
  4535. * If a platform hasn't been transitioned to atomic watermarks yet,
  4536. * we'll continue to update watermarks the old way, if flags tell
  4537. * us to.
  4538. */
  4539. if (dev_priv->display.initial_watermarks != NULL)
  4540. dev_priv->display.initial_watermarks(old_intel_state,
  4541. pipe_config);
  4542. else if (pipe_config->update_wm_pre)
  4543. intel_update_watermarks(crtc);
  4544. }
  4545. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4546. {
  4547. struct drm_device *dev = crtc->dev;
  4548. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4549. struct drm_plane *p;
  4550. int pipe = intel_crtc->pipe;
  4551. intel_crtc_dpms_overlay_disable(intel_crtc);
  4552. drm_for_each_plane_mask(p, dev, plane_mask)
  4553. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4554. /*
  4555. * FIXME: Once we grow proper nuclear flip support out of this we need
  4556. * to compute the mask of flip planes precisely. For the time being
  4557. * consider this a flip to a NULL plane.
  4558. */
  4559. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4560. }
  4561. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4562. struct intel_crtc_state *crtc_state,
  4563. struct drm_atomic_state *old_state)
  4564. {
  4565. struct drm_connector_state *conn_state;
  4566. struct drm_connector *conn;
  4567. int i;
  4568. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4569. struct intel_encoder *encoder =
  4570. to_intel_encoder(conn_state->best_encoder);
  4571. if (conn_state->crtc != crtc)
  4572. continue;
  4573. if (encoder->pre_pll_enable)
  4574. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4575. }
  4576. }
  4577. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4578. struct intel_crtc_state *crtc_state,
  4579. struct drm_atomic_state *old_state)
  4580. {
  4581. struct drm_connector_state *conn_state;
  4582. struct drm_connector *conn;
  4583. int i;
  4584. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4585. struct intel_encoder *encoder =
  4586. to_intel_encoder(conn_state->best_encoder);
  4587. if (conn_state->crtc != crtc)
  4588. continue;
  4589. if (encoder->pre_enable)
  4590. encoder->pre_enable(encoder, crtc_state, conn_state);
  4591. }
  4592. }
  4593. static void intel_encoders_enable(struct drm_crtc *crtc,
  4594. struct intel_crtc_state *crtc_state,
  4595. struct drm_atomic_state *old_state)
  4596. {
  4597. struct drm_connector_state *conn_state;
  4598. struct drm_connector *conn;
  4599. int i;
  4600. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4601. struct intel_encoder *encoder =
  4602. to_intel_encoder(conn_state->best_encoder);
  4603. if (conn_state->crtc != crtc)
  4604. continue;
  4605. encoder->enable(encoder, crtc_state, conn_state);
  4606. intel_opregion_notify_encoder(encoder, true);
  4607. }
  4608. }
  4609. static void intel_encoders_disable(struct drm_crtc *crtc,
  4610. struct intel_crtc_state *old_crtc_state,
  4611. struct drm_atomic_state *old_state)
  4612. {
  4613. struct drm_connector_state *old_conn_state;
  4614. struct drm_connector *conn;
  4615. int i;
  4616. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4617. struct intel_encoder *encoder =
  4618. to_intel_encoder(old_conn_state->best_encoder);
  4619. if (old_conn_state->crtc != crtc)
  4620. continue;
  4621. intel_opregion_notify_encoder(encoder, false);
  4622. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4623. }
  4624. }
  4625. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4626. struct intel_crtc_state *old_crtc_state,
  4627. struct drm_atomic_state *old_state)
  4628. {
  4629. struct drm_connector_state *old_conn_state;
  4630. struct drm_connector *conn;
  4631. int i;
  4632. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4633. struct intel_encoder *encoder =
  4634. to_intel_encoder(old_conn_state->best_encoder);
  4635. if (old_conn_state->crtc != crtc)
  4636. continue;
  4637. if (encoder->post_disable)
  4638. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4639. }
  4640. }
  4641. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4642. struct intel_crtc_state *old_crtc_state,
  4643. struct drm_atomic_state *old_state)
  4644. {
  4645. struct drm_connector_state *old_conn_state;
  4646. struct drm_connector *conn;
  4647. int i;
  4648. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4649. struct intel_encoder *encoder =
  4650. to_intel_encoder(old_conn_state->best_encoder);
  4651. if (old_conn_state->crtc != crtc)
  4652. continue;
  4653. if (encoder->post_pll_disable)
  4654. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4655. }
  4656. }
  4657. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4658. struct drm_atomic_state *old_state)
  4659. {
  4660. struct drm_crtc *crtc = pipe_config->base.crtc;
  4661. struct drm_device *dev = crtc->dev;
  4662. struct drm_i915_private *dev_priv = to_i915(dev);
  4663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4664. int pipe = intel_crtc->pipe;
  4665. struct intel_atomic_state *old_intel_state =
  4666. to_intel_atomic_state(old_state);
  4667. if (WARN_ON(intel_crtc->active))
  4668. return;
  4669. /*
  4670. * Sometimes spurious CPU pipe underruns happen during FDI
  4671. * training, at least with VGA+HDMI cloning. Suppress them.
  4672. *
  4673. * On ILK we get an occasional spurious CPU pipe underruns
  4674. * between eDP port A enable and vdd enable. Also PCH port
  4675. * enable seems to result in the occasional CPU pipe underrun.
  4676. *
  4677. * Spurious PCH underruns also occur during PCH enabling.
  4678. */
  4679. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4680. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4681. if (intel_crtc->config->has_pch_encoder)
  4682. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4683. if (intel_crtc->config->has_pch_encoder)
  4684. intel_prepare_shared_dpll(intel_crtc);
  4685. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4686. intel_dp_set_m_n(intel_crtc, M1_N1);
  4687. intel_set_pipe_timings(intel_crtc);
  4688. intel_set_pipe_src_size(intel_crtc);
  4689. if (intel_crtc->config->has_pch_encoder) {
  4690. intel_cpu_transcoder_set_m_n(intel_crtc,
  4691. &intel_crtc->config->fdi_m_n, NULL);
  4692. }
  4693. ironlake_set_pipeconf(crtc);
  4694. intel_crtc->active = true;
  4695. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4696. if (intel_crtc->config->has_pch_encoder) {
  4697. /* Note: FDI PLL enabling _must_ be done before we enable the
  4698. * cpu pipes, hence this is separate from all the other fdi/pch
  4699. * enabling. */
  4700. ironlake_fdi_pll_enable(intel_crtc);
  4701. } else {
  4702. assert_fdi_tx_disabled(dev_priv, pipe);
  4703. assert_fdi_rx_disabled(dev_priv, pipe);
  4704. }
  4705. ironlake_pfit_enable(intel_crtc);
  4706. /*
  4707. * On ILK+ LUT must be loaded before the pipe is running but with
  4708. * clocks enabled
  4709. */
  4710. intel_color_load_luts(&pipe_config->base);
  4711. if (dev_priv->display.initial_watermarks != NULL)
  4712. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4713. intel_enable_pipe(pipe_config);
  4714. if (intel_crtc->config->has_pch_encoder)
  4715. ironlake_pch_enable(pipe_config);
  4716. assert_vblank_disabled(crtc);
  4717. drm_crtc_vblank_on(crtc);
  4718. intel_encoders_enable(crtc, pipe_config, old_state);
  4719. if (HAS_PCH_CPT(dev_priv))
  4720. cpt_verify_modeset(dev, intel_crtc->pipe);
  4721. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4722. if (intel_crtc->config->has_pch_encoder)
  4723. intel_wait_for_vblank(dev_priv, pipe);
  4724. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4725. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4726. }
  4727. /* IPS only exists on ULT machines and is tied to pipe A. */
  4728. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4729. {
  4730. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4731. }
  4732. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4733. enum pipe pipe, bool apply)
  4734. {
  4735. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4736. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4737. if (apply)
  4738. val |= mask;
  4739. else
  4740. val &= ~mask;
  4741. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4742. }
  4743. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4744. {
  4745. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4746. enum pipe pipe = crtc->pipe;
  4747. uint32_t val;
  4748. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4749. /* Program B credit equally to all pipes */
  4750. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4751. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4752. }
  4753. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4754. struct drm_atomic_state *old_state)
  4755. {
  4756. struct drm_crtc *crtc = pipe_config->base.crtc;
  4757. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4759. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4760. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4761. struct intel_atomic_state *old_intel_state =
  4762. to_intel_atomic_state(old_state);
  4763. bool psl_clkgate_wa;
  4764. if (WARN_ON(intel_crtc->active))
  4765. return;
  4766. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4767. if (intel_crtc->config->shared_dpll)
  4768. intel_enable_shared_dpll(intel_crtc);
  4769. if (INTEL_GEN(dev_priv) >= 11)
  4770. icl_map_plls_to_ports(crtc, pipe_config, old_state);
  4771. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4772. intel_dp_set_m_n(intel_crtc, M1_N1);
  4773. if (!transcoder_is_dsi(cpu_transcoder))
  4774. intel_set_pipe_timings(intel_crtc);
  4775. intel_set_pipe_src_size(intel_crtc);
  4776. if (cpu_transcoder != TRANSCODER_EDP &&
  4777. !transcoder_is_dsi(cpu_transcoder)) {
  4778. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4779. intel_crtc->config->pixel_multiplier - 1);
  4780. }
  4781. if (intel_crtc->config->has_pch_encoder) {
  4782. intel_cpu_transcoder_set_m_n(intel_crtc,
  4783. &intel_crtc->config->fdi_m_n, NULL);
  4784. }
  4785. if (!transcoder_is_dsi(cpu_transcoder))
  4786. haswell_set_pipeconf(crtc);
  4787. haswell_set_pipemisc(crtc);
  4788. intel_color_set_csc(&pipe_config->base);
  4789. intel_crtc->active = true;
  4790. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4791. if (!transcoder_is_dsi(cpu_transcoder))
  4792. intel_ddi_enable_pipe_clock(pipe_config);
  4793. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4794. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4795. intel_crtc->config->pch_pfit.enabled;
  4796. if (psl_clkgate_wa)
  4797. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4798. if (INTEL_GEN(dev_priv) >= 9)
  4799. skylake_pfit_enable(intel_crtc);
  4800. else
  4801. ironlake_pfit_enable(intel_crtc);
  4802. /*
  4803. * On ILK+ LUT must be loaded before the pipe is running but with
  4804. * clocks enabled
  4805. */
  4806. intel_color_load_luts(&pipe_config->base);
  4807. intel_ddi_set_pipe_settings(pipe_config);
  4808. if (!transcoder_is_dsi(cpu_transcoder))
  4809. intel_ddi_enable_transcoder_func(pipe_config);
  4810. if (dev_priv->display.initial_watermarks != NULL)
  4811. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4812. if (INTEL_GEN(dev_priv) >= 11)
  4813. icl_pipe_mbus_enable(intel_crtc);
  4814. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4815. if (!transcoder_is_dsi(cpu_transcoder))
  4816. intel_enable_pipe(pipe_config);
  4817. if (intel_crtc->config->has_pch_encoder)
  4818. lpt_pch_enable(pipe_config);
  4819. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4820. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4821. assert_vblank_disabled(crtc);
  4822. drm_crtc_vblank_on(crtc);
  4823. intel_encoders_enable(crtc, pipe_config, old_state);
  4824. if (psl_clkgate_wa) {
  4825. intel_wait_for_vblank(dev_priv, pipe);
  4826. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4827. }
  4828. /* If we change the relative order between pipe/planes enabling, we need
  4829. * to change the workaround. */
  4830. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4831. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4832. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4833. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4834. }
  4835. }
  4836. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4837. {
  4838. struct drm_device *dev = crtc->base.dev;
  4839. struct drm_i915_private *dev_priv = to_i915(dev);
  4840. int pipe = crtc->pipe;
  4841. /* To avoid upsetting the power well on haswell only disable the pfit if
  4842. * it's in use. The hw state code will make sure we get this right. */
  4843. if (force || crtc->config->pch_pfit.enabled) {
  4844. I915_WRITE(PF_CTL(pipe), 0);
  4845. I915_WRITE(PF_WIN_POS(pipe), 0);
  4846. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4847. }
  4848. }
  4849. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4850. struct drm_atomic_state *old_state)
  4851. {
  4852. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4853. struct drm_device *dev = crtc->dev;
  4854. struct drm_i915_private *dev_priv = to_i915(dev);
  4855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4856. int pipe = intel_crtc->pipe;
  4857. /*
  4858. * Sometimes spurious CPU pipe underruns happen when the
  4859. * pipe is already disabled, but FDI RX/TX is still enabled.
  4860. * Happens at least with VGA+HDMI cloning. Suppress them.
  4861. */
  4862. if (intel_crtc->config->has_pch_encoder) {
  4863. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4864. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4865. }
  4866. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4867. drm_crtc_vblank_off(crtc);
  4868. assert_vblank_disabled(crtc);
  4869. intel_disable_pipe(old_crtc_state);
  4870. ironlake_pfit_disable(intel_crtc, false);
  4871. if (intel_crtc->config->has_pch_encoder)
  4872. ironlake_fdi_disable(crtc);
  4873. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4874. if (intel_crtc->config->has_pch_encoder) {
  4875. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4876. if (HAS_PCH_CPT(dev_priv)) {
  4877. i915_reg_t reg;
  4878. u32 temp;
  4879. /* disable TRANS_DP_CTL */
  4880. reg = TRANS_DP_CTL(pipe);
  4881. temp = I915_READ(reg);
  4882. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4883. TRANS_DP_PORT_SEL_MASK);
  4884. temp |= TRANS_DP_PORT_SEL_NONE;
  4885. I915_WRITE(reg, temp);
  4886. /* disable DPLL_SEL */
  4887. temp = I915_READ(PCH_DPLL_SEL);
  4888. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4889. I915_WRITE(PCH_DPLL_SEL, temp);
  4890. }
  4891. ironlake_fdi_pll_disable(intel_crtc);
  4892. }
  4893. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4894. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4895. }
  4896. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4897. struct drm_atomic_state *old_state)
  4898. {
  4899. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4900. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4902. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4903. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4904. drm_crtc_vblank_off(crtc);
  4905. assert_vblank_disabled(crtc);
  4906. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4907. if (!transcoder_is_dsi(cpu_transcoder))
  4908. intel_disable_pipe(old_crtc_state);
  4909. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4910. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4911. if (!transcoder_is_dsi(cpu_transcoder))
  4912. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4913. if (INTEL_GEN(dev_priv) >= 9)
  4914. skylake_scaler_disable(intel_crtc);
  4915. else
  4916. ironlake_pfit_disable(intel_crtc, false);
  4917. if (!transcoder_is_dsi(cpu_transcoder))
  4918. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4919. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4920. if (INTEL_GEN(dev_priv) >= 11)
  4921. icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
  4922. }
  4923. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4924. {
  4925. struct drm_device *dev = crtc->base.dev;
  4926. struct drm_i915_private *dev_priv = to_i915(dev);
  4927. struct intel_crtc_state *pipe_config = crtc->config;
  4928. if (!pipe_config->gmch_pfit.control)
  4929. return;
  4930. /*
  4931. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4932. * according to register description and PRM.
  4933. */
  4934. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4935. assert_pipe_disabled(dev_priv, crtc->pipe);
  4936. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4937. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4938. /* Border color in case we don't scale up to the full screen. Black by
  4939. * default, change to something else for debugging. */
  4940. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4941. }
  4942. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4943. {
  4944. switch (port) {
  4945. case PORT_A:
  4946. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4947. case PORT_B:
  4948. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4949. case PORT_C:
  4950. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4951. case PORT_D:
  4952. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4953. case PORT_E:
  4954. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4955. case PORT_F:
  4956. return POWER_DOMAIN_PORT_DDI_F_LANES;
  4957. default:
  4958. MISSING_CASE(port);
  4959. return POWER_DOMAIN_PORT_OTHER;
  4960. }
  4961. }
  4962. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4963. struct intel_crtc_state *crtc_state)
  4964. {
  4965. struct drm_device *dev = crtc->dev;
  4966. struct drm_i915_private *dev_priv = to_i915(dev);
  4967. struct drm_encoder *encoder;
  4968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4969. enum pipe pipe = intel_crtc->pipe;
  4970. u64 mask;
  4971. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4972. if (!crtc_state->base.active)
  4973. return 0;
  4974. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  4975. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  4976. if (crtc_state->pch_pfit.enabled ||
  4977. crtc_state->pch_pfit.force_thru)
  4978. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4979. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4980. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4981. mask |= BIT_ULL(intel_encoder->power_domain);
  4982. }
  4983. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4984. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  4985. if (crtc_state->shared_dpll)
  4986. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4987. return mask;
  4988. }
  4989. static u64
  4990. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4991. struct intel_crtc_state *crtc_state)
  4992. {
  4993. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4995. enum intel_display_power_domain domain;
  4996. u64 domains, new_domains, old_domains;
  4997. old_domains = intel_crtc->enabled_power_domains;
  4998. intel_crtc->enabled_power_domains = new_domains =
  4999. get_crtc_power_domains(crtc, crtc_state);
  5000. domains = new_domains & ~old_domains;
  5001. for_each_power_domain(domain, domains)
  5002. intel_display_power_get(dev_priv, domain);
  5003. return old_domains & ~new_domains;
  5004. }
  5005. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  5006. u64 domains)
  5007. {
  5008. enum intel_display_power_domain domain;
  5009. for_each_power_domain(domain, domains)
  5010. intel_display_power_put(dev_priv, domain);
  5011. }
  5012. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5013. struct drm_atomic_state *old_state)
  5014. {
  5015. struct intel_atomic_state *old_intel_state =
  5016. to_intel_atomic_state(old_state);
  5017. struct drm_crtc *crtc = pipe_config->base.crtc;
  5018. struct drm_device *dev = crtc->dev;
  5019. struct drm_i915_private *dev_priv = to_i915(dev);
  5020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5021. int pipe = intel_crtc->pipe;
  5022. if (WARN_ON(intel_crtc->active))
  5023. return;
  5024. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5025. intel_dp_set_m_n(intel_crtc, M1_N1);
  5026. intel_set_pipe_timings(intel_crtc);
  5027. intel_set_pipe_src_size(intel_crtc);
  5028. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5029. struct drm_i915_private *dev_priv = to_i915(dev);
  5030. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5031. I915_WRITE(CHV_CANVAS(pipe), 0);
  5032. }
  5033. i9xx_set_pipeconf(intel_crtc);
  5034. intel_crtc->active = true;
  5035. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5036. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5037. if (IS_CHERRYVIEW(dev_priv)) {
  5038. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5039. chv_enable_pll(intel_crtc, intel_crtc->config);
  5040. } else {
  5041. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5042. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5043. }
  5044. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5045. i9xx_pfit_enable(intel_crtc);
  5046. intel_color_load_luts(&pipe_config->base);
  5047. dev_priv->display.initial_watermarks(old_intel_state,
  5048. pipe_config);
  5049. intel_enable_pipe(pipe_config);
  5050. assert_vblank_disabled(crtc);
  5051. drm_crtc_vblank_on(crtc);
  5052. intel_encoders_enable(crtc, pipe_config, old_state);
  5053. }
  5054. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5055. {
  5056. struct drm_device *dev = crtc->base.dev;
  5057. struct drm_i915_private *dev_priv = to_i915(dev);
  5058. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5059. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5060. }
  5061. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5062. struct drm_atomic_state *old_state)
  5063. {
  5064. struct intel_atomic_state *old_intel_state =
  5065. to_intel_atomic_state(old_state);
  5066. struct drm_crtc *crtc = pipe_config->base.crtc;
  5067. struct drm_device *dev = crtc->dev;
  5068. struct drm_i915_private *dev_priv = to_i915(dev);
  5069. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5070. enum pipe pipe = intel_crtc->pipe;
  5071. if (WARN_ON(intel_crtc->active))
  5072. return;
  5073. i9xx_set_pll_dividers(intel_crtc);
  5074. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5075. intel_dp_set_m_n(intel_crtc, M1_N1);
  5076. intel_set_pipe_timings(intel_crtc);
  5077. intel_set_pipe_src_size(intel_crtc);
  5078. i9xx_set_pipeconf(intel_crtc);
  5079. intel_crtc->active = true;
  5080. if (!IS_GEN2(dev_priv))
  5081. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5082. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5083. i9xx_enable_pll(intel_crtc, pipe_config);
  5084. i9xx_pfit_enable(intel_crtc);
  5085. intel_color_load_luts(&pipe_config->base);
  5086. if (dev_priv->display.initial_watermarks != NULL)
  5087. dev_priv->display.initial_watermarks(old_intel_state,
  5088. intel_crtc->config);
  5089. else
  5090. intel_update_watermarks(intel_crtc);
  5091. intel_enable_pipe(pipe_config);
  5092. assert_vblank_disabled(crtc);
  5093. drm_crtc_vblank_on(crtc);
  5094. intel_encoders_enable(crtc, pipe_config, old_state);
  5095. }
  5096. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5097. {
  5098. struct drm_device *dev = crtc->base.dev;
  5099. struct drm_i915_private *dev_priv = to_i915(dev);
  5100. if (!crtc->config->gmch_pfit.control)
  5101. return;
  5102. assert_pipe_disabled(dev_priv, crtc->pipe);
  5103. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5104. I915_READ(PFIT_CONTROL));
  5105. I915_WRITE(PFIT_CONTROL, 0);
  5106. }
  5107. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5108. struct drm_atomic_state *old_state)
  5109. {
  5110. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5111. struct drm_device *dev = crtc->dev;
  5112. struct drm_i915_private *dev_priv = to_i915(dev);
  5113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5114. int pipe = intel_crtc->pipe;
  5115. /*
  5116. * On gen2 planes are double buffered but the pipe isn't, so we must
  5117. * wait for planes to fully turn off before disabling the pipe.
  5118. */
  5119. if (IS_GEN2(dev_priv))
  5120. intel_wait_for_vblank(dev_priv, pipe);
  5121. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5122. drm_crtc_vblank_off(crtc);
  5123. assert_vblank_disabled(crtc);
  5124. intel_disable_pipe(old_crtc_state);
  5125. i9xx_pfit_disable(intel_crtc);
  5126. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5127. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5128. if (IS_CHERRYVIEW(dev_priv))
  5129. chv_disable_pll(dev_priv, pipe);
  5130. else if (IS_VALLEYVIEW(dev_priv))
  5131. vlv_disable_pll(dev_priv, pipe);
  5132. else
  5133. i9xx_disable_pll(intel_crtc);
  5134. }
  5135. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5136. if (!IS_GEN2(dev_priv))
  5137. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5138. if (!dev_priv->display.initial_watermarks)
  5139. intel_update_watermarks(intel_crtc);
  5140. /* clock the pipe down to 640x480@60 to potentially save power */
  5141. if (IS_I830(dev_priv))
  5142. i830_enable_pipe(dev_priv, pipe);
  5143. }
  5144. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5145. struct drm_modeset_acquire_ctx *ctx)
  5146. {
  5147. struct intel_encoder *encoder;
  5148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5149. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5150. enum intel_display_power_domain domain;
  5151. struct intel_plane *plane;
  5152. u64 domains;
  5153. struct drm_atomic_state *state;
  5154. struct intel_crtc_state *crtc_state;
  5155. int ret;
  5156. if (!intel_crtc->active)
  5157. return;
  5158. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5159. const struct intel_plane_state *plane_state =
  5160. to_intel_plane_state(plane->base.state);
  5161. if (plane_state->base.visible)
  5162. intel_plane_disable_noatomic(intel_crtc, plane);
  5163. }
  5164. state = drm_atomic_state_alloc(crtc->dev);
  5165. if (!state) {
  5166. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5167. crtc->base.id, crtc->name);
  5168. return;
  5169. }
  5170. state->acquire_ctx = ctx;
  5171. /* Everything's already locked, -EDEADLK can't happen. */
  5172. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5173. ret = drm_atomic_add_affected_connectors(state, crtc);
  5174. WARN_ON(IS_ERR(crtc_state) || ret);
  5175. dev_priv->display.crtc_disable(crtc_state, state);
  5176. drm_atomic_state_put(state);
  5177. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5178. crtc->base.id, crtc->name);
  5179. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5180. crtc->state->active = false;
  5181. intel_crtc->active = false;
  5182. crtc->enabled = false;
  5183. crtc->state->connector_mask = 0;
  5184. crtc->state->encoder_mask = 0;
  5185. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5186. encoder->base.crtc = NULL;
  5187. intel_fbc_disable(intel_crtc);
  5188. intel_update_watermarks(intel_crtc);
  5189. intel_disable_shared_dpll(intel_crtc);
  5190. domains = intel_crtc->enabled_power_domains;
  5191. for_each_power_domain(domain, domains)
  5192. intel_display_power_put(dev_priv, domain);
  5193. intel_crtc->enabled_power_domains = 0;
  5194. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5195. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5196. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5197. }
  5198. /*
  5199. * turn all crtc's off, but do not adjust state
  5200. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5201. */
  5202. int intel_display_suspend(struct drm_device *dev)
  5203. {
  5204. struct drm_i915_private *dev_priv = to_i915(dev);
  5205. struct drm_atomic_state *state;
  5206. int ret;
  5207. state = drm_atomic_helper_suspend(dev);
  5208. ret = PTR_ERR_OR_ZERO(state);
  5209. if (ret)
  5210. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5211. else
  5212. dev_priv->modeset_restore_state = state;
  5213. return ret;
  5214. }
  5215. void intel_encoder_destroy(struct drm_encoder *encoder)
  5216. {
  5217. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5218. drm_encoder_cleanup(encoder);
  5219. kfree(intel_encoder);
  5220. }
  5221. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5222. * internal consistency). */
  5223. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5224. struct drm_connector_state *conn_state)
  5225. {
  5226. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5227. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5228. connector->base.base.id,
  5229. connector->base.name);
  5230. if (connector->get_hw_state(connector)) {
  5231. struct intel_encoder *encoder = connector->encoder;
  5232. I915_STATE_WARN(!crtc_state,
  5233. "connector enabled without attached crtc\n");
  5234. if (!crtc_state)
  5235. return;
  5236. I915_STATE_WARN(!crtc_state->active,
  5237. "connector is active, but attached crtc isn't\n");
  5238. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5239. return;
  5240. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5241. "atomic encoder doesn't match attached encoder\n");
  5242. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5243. "attached encoder crtc differs from connector crtc\n");
  5244. } else {
  5245. I915_STATE_WARN(crtc_state && crtc_state->active,
  5246. "attached crtc is active, but connector isn't\n");
  5247. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5248. "best encoder set without crtc!\n");
  5249. }
  5250. }
  5251. int intel_connector_init(struct intel_connector *connector)
  5252. {
  5253. struct intel_digital_connector_state *conn_state;
  5254. /*
  5255. * Allocate enough memory to hold intel_digital_connector_state,
  5256. * This might be a few bytes too many, but for connectors that don't
  5257. * need it we'll free the state and allocate a smaller one on the first
  5258. * succesful commit anyway.
  5259. */
  5260. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5261. if (!conn_state)
  5262. return -ENOMEM;
  5263. __drm_atomic_helper_connector_reset(&connector->base,
  5264. &conn_state->base);
  5265. return 0;
  5266. }
  5267. struct intel_connector *intel_connector_alloc(void)
  5268. {
  5269. struct intel_connector *connector;
  5270. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5271. if (!connector)
  5272. return NULL;
  5273. if (intel_connector_init(connector) < 0) {
  5274. kfree(connector);
  5275. return NULL;
  5276. }
  5277. return connector;
  5278. }
  5279. /*
  5280. * Free the bits allocated by intel_connector_alloc.
  5281. * This should only be used after intel_connector_alloc has returned
  5282. * successfully, and before drm_connector_init returns successfully.
  5283. * Otherwise the destroy callbacks for the connector and the state should
  5284. * take care of proper cleanup/free
  5285. */
  5286. void intel_connector_free(struct intel_connector *connector)
  5287. {
  5288. kfree(to_intel_digital_connector_state(connector->base.state));
  5289. kfree(connector);
  5290. }
  5291. /* Simple connector->get_hw_state implementation for encoders that support only
  5292. * one connector and no cloning and hence the encoder state determines the state
  5293. * of the connector. */
  5294. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5295. {
  5296. enum pipe pipe = 0;
  5297. struct intel_encoder *encoder = connector->encoder;
  5298. return encoder->get_hw_state(encoder, &pipe);
  5299. }
  5300. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5301. {
  5302. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5303. return crtc_state->fdi_lanes;
  5304. return 0;
  5305. }
  5306. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5307. struct intel_crtc_state *pipe_config)
  5308. {
  5309. struct drm_i915_private *dev_priv = to_i915(dev);
  5310. struct drm_atomic_state *state = pipe_config->base.state;
  5311. struct intel_crtc *other_crtc;
  5312. struct intel_crtc_state *other_crtc_state;
  5313. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5314. pipe_name(pipe), pipe_config->fdi_lanes);
  5315. if (pipe_config->fdi_lanes > 4) {
  5316. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5317. pipe_name(pipe), pipe_config->fdi_lanes);
  5318. return -EINVAL;
  5319. }
  5320. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5321. if (pipe_config->fdi_lanes > 2) {
  5322. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5323. pipe_config->fdi_lanes);
  5324. return -EINVAL;
  5325. } else {
  5326. return 0;
  5327. }
  5328. }
  5329. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5330. return 0;
  5331. /* Ivybridge 3 pipe is really complicated */
  5332. switch (pipe) {
  5333. case PIPE_A:
  5334. return 0;
  5335. case PIPE_B:
  5336. if (pipe_config->fdi_lanes <= 2)
  5337. return 0;
  5338. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5339. other_crtc_state =
  5340. intel_atomic_get_crtc_state(state, other_crtc);
  5341. if (IS_ERR(other_crtc_state))
  5342. return PTR_ERR(other_crtc_state);
  5343. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5344. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5345. pipe_name(pipe), pipe_config->fdi_lanes);
  5346. return -EINVAL;
  5347. }
  5348. return 0;
  5349. case PIPE_C:
  5350. if (pipe_config->fdi_lanes > 2) {
  5351. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5352. pipe_name(pipe), pipe_config->fdi_lanes);
  5353. return -EINVAL;
  5354. }
  5355. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5356. other_crtc_state =
  5357. intel_atomic_get_crtc_state(state, other_crtc);
  5358. if (IS_ERR(other_crtc_state))
  5359. return PTR_ERR(other_crtc_state);
  5360. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5361. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5362. return -EINVAL;
  5363. }
  5364. return 0;
  5365. default:
  5366. BUG();
  5367. }
  5368. }
  5369. #define RETRY 1
  5370. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5371. struct intel_crtc_state *pipe_config)
  5372. {
  5373. struct drm_device *dev = intel_crtc->base.dev;
  5374. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5375. int lane, link_bw, fdi_dotclock, ret;
  5376. bool needs_recompute = false;
  5377. retry:
  5378. /* FDI is a binary signal running at ~2.7GHz, encoding
  5379. * each output octet as 10 bits. The actual frequency
  5380. * is stored as a divider into a 100MHz clock, and the
  5381. * mode pixel clock is stored in units of 1KHz.
  5382. * Hence the bw of each lane in terms of the mode signal
  5383. * is:
  5384. */
  5385. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5386. fdi_dotclock = adjusted_mode->crtc_clock;
  5387. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5388. pipe_config->pipe_bpp);
  5389. pipe_config->fdi_lanes = lane;
  5390. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5391. link_bw, &pipe_config->fdi_m_n, false);
  5392. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5393. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5394. pipe_config->pipe_bpp -= 2*3;
  5395. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5396. pipe_config->pipe_bpp);
  5397. needs_recompute = true;
  5398. pipe_config->bw_constrained = true;
  5399. goto retry;
  5400. }
  5401. if (needs_recompute)
  5402. return RETRY;
  5403. return ret;
  5404. }
  5405. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5406. {
  5407. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5408. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5409. /* IPS only exists on ULT machines and is tied to pipe A. */
  5410. if (!hsw_crtc_supports_ips(crtc))
  5411. return false;
  5412. if (!i915_modparams.enable_ips)
  5413. return false;
  5414. if (crtc_state->pipe_bpp > 24)
  5415. return false;
  5416. /*
  5417. * We compare against max which means we must take
  5418. * the increased cdclk requirement into account when
  5419. * calculating the new cdclk.
  5420. *
  5421. * Should measure whether using a lower cdclk w/o IPS
  5422. */
  5423. if (IS_BROADWELL(dev_priv) &&
  5424. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5425. return false;
  5426. return true;
  5427. }
  5428. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5429. {
  5430. struct drm_i915_private *dev_priv =
  5431. to_i915(crtc_state->base.crtc->dev);
  5432. struct intel_atomic_state *intel_state =
  5433. to_intel_atomic_state(crtc_state->base.state);
  5434. if (!hsw_crtc_state_ips_capable(crtc_state))
  5435. return false;
  5436. if (crtc_state->ips_force_disable)
  5437. return false;
  5438. /* IPS should be fine as long as at least one plane is enabled. */
  5439. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5440. return false;
  5441. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5442. if (IS_BROADWELL(dev_priv) &&
  5443. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5444. return false;
  5445. return true;
  5446. }
  5447. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5448. {
  5449. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5450. /* GDG double wide on either pipe, otherwise pipe A only */
  5451. return INTEL_GEN(dev_priv) < 4 &&
  5452. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5453. }
  5454. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5455. {
  5456. uint32_t pixel_rate;
  5457. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5458. /*
  5459. * We only use IF-ID interlacing. If we ever use
  5460. * PF-ID we'll need to adjust the pixel_rate here.
  5461. */
  5462. if (pipe_config->pch_pfit.enabled) {
  5463. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5464. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5465. pipe_w = pipe_config->pipe_src_w;
  5466. pipe_h = pipe_config->pipe_src_h;
  5467. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5468. pfit_h = pfit_size & 0xFFFF;
  5469. if (pipe_w < pfit_w)
  5470. pipe_w = pfit_w;
  5471. if (pipe_h < pfit_h)
  5472. pipe_h = pfit_h;
  5473. if (WARN_ON(!pfit_w || !pfit_h))
  5474. return pixel_rate;
  5475. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5476. pfit_w * pfit_h);
  5477. }
  5478. return pixel_rate;
  5479. }
  5480. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5481. {
  5482. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5483. if (HAS_GMCH_DISPLAY(dev_priv))
  5484. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5485. crtc_state->pixel_rate =
  5486. crtc_state->base.adjusted_mode.crtc_clock;
  5487. else
  5488. crtc_state->pixel_rate =
  5489. ilk_pipe_pixel_rate(crtc_state);
  5490. }
  5491. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5492. struct intel_crtc_state *pipe_config)
  5493. {
  5494. struct drm_device *dev = crtc->base.dev;
  5495. struct drm_i915_private *dev_priv = to_i915(dev);
  5496. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5497. int clock_limit = dev_priv->max_dotclk_freq;
  5498. if (INTEL_GEN(dev_priv) < 4) {
  5499. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5500. /*
  5501. * Enable double wide mode when the dot clock
  5502. * is > 90% of the (display) core speed.
  5503. */
  5504. if (intel_crtc_supports_double_wide(crtc) &&
  5505. adjusted_mode->crtc_clock > clock_limit) {
  5506. clock_limit = dev_priv->max_dotclk_freq;
  5507. pipe_config->double_wide = true;
  5508. }
  5509. }
  5510. if (adjusted_mode->crtc_clock > clock_limit) {
  5511. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5512. adjusted_mode->crtc_clock, clock_limit,
  5513. yesno(pipe_config->double_wide));
  5514. return -EINVAL;
  5515. }
  5516. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5517. /*
  5518. * There is only one pipe CSC unit per pipe, and we need that
  5519. * for output conversion from RGB->YCBCR. So if CTM is already
  5520. * applied we can't support YCBCR420 output.
  5521. */
  5522. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5523. return -EINVAL;
  5524. }
  5525. /*
  5526. * Pipe horizontal size must be even in:
  5527. * - DVO ganged mode
  5528. * - LVDS dual channel mode
  5529. * - Double wide pipe
  5530. */
  5531. if (pipe_config->pipe_src_w & 1) {
  5532. if (pipe_config->double_wide) {
  5533. DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
  5534. return -EINVAL;
  5535. }
  5536. if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5537. intel_is_dual_link_lvds(dev)) {
  5538. DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
  5539. return -EINVAL;
  5540. }
  5541. }
  5542. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5543. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5544. */
  5545. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5546. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5547. return -EINVAL;
  5548. intel_crtc_compute_pixel_rate(pipe_config);
  5549. if (pipe_config->has_pch_encoder)
  5550. return ironlake_fdi_compute_config(crtc, pipe_config);
  5551. return 0;
  5552. }
  5553. static void
  5554. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5555. {
  5556. while (*num > DATA_LINK_M_N_MASK ||
  5557. *den > DATA_LINK_M_N_MASK) {
  5558. *num >>= 1;
  5559. *den >>= 1;
  5560. }
  5561. }
  5562. static void compute_m_n(unsigned int m, unsigned int n,
  5563. uint32_t *ret_m, uint32_t *ret_n,
  5564. bool reduce_m_n)
  5565. {
  5566. /*
  5567. * Reduce M/N as much as possible without loss in precision. Several DP
  5568. * dongles in particular seem to be fussy about too large *link* M/N
  5569. * values. The passed in values are more likely to have the least
  5570. * significant bits zero than M after rounding below, so do this first.
  5571. */
  5572. if (reduce_m_n) {
  5573. while ((m & 1) == 0 && (n & 1) == 0) {
  5574. m >>= 1;
  5575. n >>= 1;
  5576. }
  5577. }
  5578. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5579. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5580. intel_reduce_m_n_ratio(ret_m, ret_n);
  5581. }
  5582. void
  5583. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5584. int pixel_clock, int link_clock,
  5585. struct intel_link_m_n *m_n,
  5586. bool reduce_m_n)
  5587. {
  5588. m_n->tu = 64;
  5589. compute_m_n(bits_per_pixel * pixel_clock,
  5590. link_clock * nlanes * 8,
  5591. &m_n->gmch_m, &m_n->gmch_n,
  5592. reduce_m_n);
  5593. compute_m_n(pixel_clock, link_clock,
  5594. &m_n->link_m, &m_n->link_n,
  5595. reduce_m_n);
  5596. }
  5597. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5598. {
  5599. if (i915_modparams.panel_use_ssc >= 0)
  5600. return i915_modparams.panel_use_ssc != 0;
  5601. return dev_priv->vbt.lvds_use_ssc
  5602. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5603. }
  5604. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5605. {
  5606. return (1 << dpll->n) << 16 | dpll->m2;
  5607. }
  5608. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5609. {
  5610. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5611. }
  5612. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5613. struct intel_crtc_state *crtc_state,
  5614. struct dpll *reduced_clock)
  5615. {
  5616. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5617. u32 fp, fp2 = 0;
  5618. if (IS_PINEVIEW(dev_priv)) {
  5619. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5620. if (reduced_clock)
  5621. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5622. } else {
  5623. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5624. if (reduced_clock)
  5625. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5626. }
  5627. crtc_state->dpll_hw_state.fp0 = fp;
  5628. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5629. reduced_clock) {
  5630. crtc_state->dpll_hw_state.fp1 = fp2;
  5631. } else {
  5632. crtc_state->dpll_hw_state.fp1 = fp;
  5633. }
  5634. }
  5635. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5636. pipe)
  5637. {
  5638. u32 reg_val;
  5639. /*
  5640. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5641. * and set it to a reasonable value instead.
  5642. */
  5643. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5644. reg_val &= 0xffffff00;
  5645. reg_val |= 0x00000030;
  5646. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5647. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5648. reg_val &= 0x00ffffff;
  5649. reg_val |= 0x8c000000;
  5650. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5651. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5652. reg_val &= 0xffffff00;
  5653. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5654. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5655. reg_val &= 0x00ffffff;
  5656. reg_val |= 0xb0000000;
  5657. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5658. }
  5659. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5660. struct intel_link_m_n *m_n)
  5661. {
  5662. struct drm_device *dev = crtc->base.dev;
  5663. struct drm_i915_private *dev_priv = to_i915(dev);
  5664. int pipe = crtc->pipe;
  5665. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5666. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5667. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5668. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5669. }
  5670. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5671. struct intel_link_m_n *m_n,
  5672. struct intel_link_m_n *m2_n2)
  5673. {
  5674. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5675. int pipe = crtc->pipe;
  5676. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5677. if (INTEL_GEN(dev_priv) >= 5) {
  5678. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5679. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5680. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5681. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5682. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5683. * for gen < 8) and if DRRS is supported (to make sure the
  5684. * registers are not unnecessarily accessed).
  5685. */
  5686. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5687. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5688. I915_WRITE(PIPE_DATA_M2(transcoder),
  5689. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5690. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5691. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5692. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5693. }
  5694. } else {
  5695. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5696. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5697. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5698. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5699. }
  5700. }
  5701. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5702. {
  5703. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5704. if (m_n == M1_N1) {
  5705. dp_m_n = &crtc->config->dp_m_n;
  5706. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5707. } else if (m_n == M2_N2) {
  5708. /*
  5709. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5710. * needs to be programmed into M1_N1.
  5711. */
  5712. dp_m_n = &crtc->config->dp_m2_n2;
  5713. } else {
  5714. DRM_ERROR("Unsupported divider value\n");
  5715. return;
  5716. }
  5717. if (crtc->config->has_pch_encoder)
  5718. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5719. else
  5720. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5721. }
  5722. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5723. struct intel_crtc_state *pipe_config)
  5724. {
  5725. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5726. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5727. if (crtc->pipe != PIPE_A)
  5728. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5729. /* DPLL not used with DSI, but still need the rest set up */
  5730. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5731. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5732. DPLL_EXT_BUFFER_ENABLE_VLV;
  5733. pipe_config->dpll_hw_state.dpll_md =
  5734. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5735. }
  5736. static void chv_compute_dpll(struct intel_crtc *crtc,
  5737. struct intel_crtc_state *pipe_config)
  5738. {
  5739. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5740. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5741. if (crtc->pipe != PIPE_A)
  5742. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5743. /* DPLL not used with DSI, but still need the rest set up */
  5744. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5745. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5746. pipe_config->dpll_hw_state.dpll_md =
  5747. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5748. }
  5749. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5750. const struct intel_crtc_state *pipe_config)
  5751. {
  5752. struct drm_device *dev = crtc->base.dev;
  5753. struct drm_i915_private *dev_priv = to_i915(dev);
  5754. enum pipe pipe = crtc->pipe;
  5755. u32 mdiv;
  5756. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5757. u32 coreclk, reg_val;
  5758. /* Enable Refclk */
  5759. I915_WRITE(DPLL(pipe),
  5760. pipe_config->dpll_hw_state.dpll &
  5761. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5762. /* No need to actually set up the DPLL with DSI */
  5763. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5764. return;
  5765. mutex_lock(&dev_priv->sb_lock);
  5766. bestn = pipe_config->dpll.n;
  5767. bestm1 = pipe_config->dpll.m1;
  5768. bestm2 = pipe_config->dpll.m2;
  5769. bestp1 = pipe_config->dpll.p1;
  5770. bestp2 = pipe_config->dpll.p2;
  5771. /* See eDP HDMI DPIO driver vbios notes doc */
  5772. /* PLL B needs special handling */
  5773. if (pipe == PIPE_B)
  5774. vlv_pllb_recal_opamp(dev_priv, pipe);
  5775. /* Set up Tx target for periodic Rcomp update */
  5776. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5777. /* Disable target IRef on PLL */
  5778. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5779. reg_val &= 0x00ffffff;
  5780. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5781. /* Disable fast lock */
  5782. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5783. /* Set idtafcrecal before PLL is enabled */
  5784. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5785. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5786. mdiv |= ((bestn << DPIO_N_SHIFT));
  5787. mdiv |= (1 << DPIO_K_SHIFT);
  5788. /*
  5789. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5790. * but we don't support that).
  5791. * Note: don't use the DAC post divider as it seems unstable.
  5792. */
  5793. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5794. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5795. mdiv |= DPIO_ENABLE_CALIBRATION;
  5796. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5797. /* Set HBR and RBR LPF coefficients */
  5798. if (pipe_config->port_clock == 162000 ||
  5799. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5800. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5801. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5802. 0x009f0003);
  5803. else
  5804. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5805. 0x00d0000f);
  5806. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5807. /* Use SSC source */
  5808. if (pipe == PIPE_A)
  5809. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5810. 0x0df40000);
  5811. else
  5812. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5813. 0x0df70000);
  5814. } else { /* HDMI or VGA */
  5815. /* Use bend source */
  5816. if (pipe == PIPE_A)
  5817. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5818. 0x0df70000);
  5819. else
  5820. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5821. 0x0df40000);
  5822. }
  5823. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5824. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5825. if (intel_crtc_has_dp_encoder(crtc->config))
  5826. coreclk |= 0x01000000;
  5827. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5828. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5829. mutex_unlock(&dev_priv->sb_lock);
  5830. }
  5831. static void chv_prepare_pll(struct intel_crtc *crtc,
  5832. const struct intel_crtc_state *pipe_config)
  5833. {
  5834. struct drm_device *dev = crtc->base.dev;
  5835. struct drm_i915_private *dev_priv = to_i915(dev);
  5836. enum pipe pipe = crtc->pipe;
  5837. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5838. u32 loopfilter, tribuf_calcntr;
  5839. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5840. u32 dpio_val;
  5841. int vco;
  5842. /* Enable Refclk and SSC */
  5843. I915_WRITE(DPLL(pipe),
  5844. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5845. /* No need to actually set up the DPLL with DSI */
  5846. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5847. return;
  5848. bestn = pipe_config->dpll.n;
  5849. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5850. bestm1 = pipe_config->dpll.m1;
  5851. bestm2 = pipe_config->dpll.m2 >> 22;
  5852. bestp1 = pipe_config->dpll.p1;
  5853. bestp2 = pipe_config->dpll.p2;
  5854. vco = pipe_config->dpll.vco;
  5855. dpio_val = 0;
  5856. loopfilter = 0;
  5857. mutex_lock(&dev_priv->sb_lock);
  5858. /* p1 and p2 divider */
  5859. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5860. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5861. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5862. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5863. 1 << DPIO_CHV_K_DIV_SHIFT);
  5864. /* Feedback post-divider - m2 */
  5865. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5866. /* Feedback refclk divider - n and m1 */
  5867. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5868. DPIO_CHV_M1_DIV_BY_2 |
  5869. 1 << DPIO_CHV_N_DIV_SHIFT);
  5870. /* M2 fraction division */
  5871. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5872. /* M2 fraction division enable */
  5873. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5874. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5875. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5876. if (bestm2_frac)
  5877. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5878. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5879. /* Program digital lock detect threshold */
  5880. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5881. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5882. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5883. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5884. if (!bestm2_frac)
  5885. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5886. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5887. /* Loop filter */
  5888. if (vco == 5400000) {
  5889. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5890. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5891. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5892. tribuf_calcntr = 0x9;
  5893. } else if (vco <= 6200000) {
  5894. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5895. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5896. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5897. tribuf_calcntr = 0x9;
  5898. } else if (vco <= 6480000) {
  5899. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5900. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5901. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5902. tribuf_calcntr = 0x8;
  5903. } else {
  5904. /* Not supported. Apply the same limits as in the max case */
  5905. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5906. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5907. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5908. tribuf_calcntr = 0;
  5909. }
  5910. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5911. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5912. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5913. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5914. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5915. /* AFC Recal */
  5916. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5917. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5918. DPIO_AFC_RECAL);
  5919. mutex_unlock(&dev_priv->sb_lock);
  5920. }
  5921. /**
  5922. * vlv_force_pll_on - forcibly enable just the PLL
  5923. * @dev_priv: i915 private structure
  5924. * @pipe: pipe PLL to enable
  5925. * @dpll: PLL configuration
  5926. *
  5927. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5928. * in cases where we need the PLL enabled even when @pipe is not going to
  5929. * be enabled.
  5930. */
  5931. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5932. const struct dpll *dpll)
  5933. {
  5934. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5935. struct intel_crtc_state *pipe_config;
  5936. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5937. if (!pipe_config)
  5938. return -ENOMEM;
  5939. pipe_config->base.crtc = &crtc->base;
  5940. pipe_config->pixel_multiplier = 1;
  5941. pipe_config->dpll = *dpll;
  5942. if (IS_CHERRYVIEW(dev_priv)) {
  5943. chv_compute_dpll(crtc, pipe_config);
  5944. chv_prepare_pll(crtc, pipe_config);
  5945. chv_enable_pll(crtc, pipe_config);
  5946. } else {
  5947. vlv_compute_dpll(crtc, pipe_config);
  5948. vlv_prepare_pll(crtc, pipe_config);
  5949. vlv_enable_pll(crtc, pipe_config);
  5950. }
  5951. kfree(pipe_config);
  5952. return 0;
  5953. }
  5954. /**
  5955. * vlv_force_pll_off - forcibly disable just the PLL
  5956. * @dev_priv: i915 private structure
  5957. * @pipe: pipe PLL to disable
  5958. *
  5959. * Disable the PLL for @pipe. To be used in cases where we need
  5960. * the PLL enabled even when @pipe is not going to be enabled.
  5961. */
  5962. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5963. {
  5964. if (IS_CHERRYVIEW(dev_priv))
  5965. chv_disable_pll(dev_priv, pipe);
  5966. else
  5967. vlv_disable_pll(dev_priv, pipe);
  5968. }
  5969. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5970. struct intel_crtc_state *crtc_state,
  5971. struct dpll *reduced_clock)
  5972. {
  5973. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5974. u32 dpll;
  5975. struct dpll *clock = &crtc_state->dpll;
  5976. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5977. dpll = DPLL_VGA_MODE_DIS;
  5978. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5979. dpll |= DPLLB_MODE_LVDS;
  5980. else
  5981. dpll |= DPLLB_MODE_DAC_SERIAL;
  5982. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5983. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5984. dpll |= (crtc_state->pixel_multiplier - 1)
  5985. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5986. }
  5987. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5988. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5989. dpll |= DPLL_SDVO_HIGH_SPEED;
  5990. if (intel_crtc_has_dp_encoder(crtc_state))
  5991. dpll |= DPLL_SDVO_HIGH_SPEED;
  5992. /* compute bitmask from p1 value */
  5993. if (IS_PINEVIEW(dev_priv))
  5994. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5995. else {
  5996. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5997. if (IS_G4X(dev_priv) && reduced_clock)
  5998. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5999. }
  6000. switch (clock->p2) {
  6001. case 5:
  6002. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6003. break;
  6004. case 7:
  6005. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6006. break;
  6007. case 10:
  6008. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6009. break;
  6010. case 14:
  6011. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6012. break;
  6013. }
  6014. if (INTEL_GEN(dev_priv) >= 4)
  6015. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6016. if (crtc_state->sdvo_tv_clock)
  6017. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6018. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6019. intel_panel_use_ssc(dev_priv))
  6020. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6021. else
  6022. dpll |= PLL_REF_INPUT_DREFCLK;
  6023. dpll |= DPLL_VCO_ENABLE;
  6024. crtc_state->dpll_hw_state.dpll = dpll;
  6025. if (INTEL_GEN(dev_priv) >= 4) {
  6026. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6027. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6028. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6029. }
  6030. }
  6031. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6032. struct intel_crtc_state *crtc_state,
  6033. struct dpll *reduced_clock)
  6034. {
  6035. struct drm_device *dev = crtc->base.dev;
  6036. struct drm_i915_private *dev_priv = to_i915(dev);
  6037. u32 dpll;
  6038. struct dpll *clock = &crtc_state->dpll;
  6039. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6040. dpll = DPLL_VGA_MODE_DIS;
  6041. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6042. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6043. } else {
  6044. if (clock->p1 == 2)
  6045. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6046. else
  6047. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6048. if (clock->p2 == 4)
  6049. dpll |= PLL_P2_DIVIDE_BY_4;
  6050. }
  6051. if (!IS_I830(dev_priv) &&
  6052. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6053. dpll |= DPLL_DVO_2X_MODE;
  6054. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6055. intel_panel_use_ssc(dev_priv))
  6056. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6057. else
  6058. dpll |= PLL_REF_INPUT_DREFCLK;
  6059. dpll |= DPLL_VCO_ENABLE;
  6060. crtc_state->dpll_hw_state.dpll = dpll;
  6061. }
  6062. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6063. {
  6064. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6065. enum pipe pipe = intel_crtc->pipe;
  6066. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6067. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6068. uint32_t crtc_vtotal, crtc_vblank_end;
  6069. int vsyncshift = 0;
  6070. /* We need to be careful not to changed the adjusted mode, for otherwise
  6071. * the hw state checker will get angry at the mismatch. */
  6072. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6073. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6074. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6075. /* the chip adds 2 halflines automatically */
  6076. crtc_vtotal -= 1;
  6077. crtc_vblank_end -= 1;
  6078. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6079. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6080. else
  6081. vsyncshift = adjusted_mode->crtc_hsync_start -
  6082. adjusted_mode->crtc_htotal / 2;
  6083. if (vsyncshift < 0)
  6084. vsyncshift += adjusted_mode->crtc_htotal;
  6085. }
  6086. if (INTEL_GEN(dev_priv) > 3)
  6087. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6088. I915_WRITE(HTOTAL(cpu_transcoder),
  6089. (adjusted_mode->crtc_hdisplay - 1) |
  6090. ((adjusted_mode->crtc_htotal - 1) << 16));
  6091. I915_WRITE(HBLANK(cpu_transcoder),
  6092. (adjusted_mode->crtc_hblank_start - 1) |
  6093. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6094. I915_WRITE(HSYNC(cpu_transcoder),
  6095. (adjusted_mode->crtc_hsync_start - 1) |
  6096. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6097. I915_WRITE(VTOTAL(cpu_transcoder),
  6098. (adjusted_mode->crtc_vdisplay - 1) |
  6099. ((crtc_vtotal - 1) << 16));
  6100. I915_WRITE(VBLANK(cpu_transcoder),
  6101. (adjusted_mode->crtc_vblank_start - 1) |
  6102. ((crtc_vblank_end - 1) << 16));
  6103. I915_WRITE(VSYNC(cpu_transcoder),
  6104. (adjusted_mode->crtc_vsync_start - 1) |
  6105. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6106. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6107. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6108. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6109. * bits. */
  6110. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6111. (pipe == PIPE_B || pipe == PIPE_C))
  6112. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6113. }
  6114. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6115. {
  6116. struct drm_device *dev = intel_crtc->base.dev;
  6117. struct drm_i915_private *dev_priv = to_i915(dev);
  6118. enum pipe pipe = intel_crtc->pipe;
  6119. /* pipesrc controls the size that is scaled from, which should
  6120. * always be the user's requested size.
  6121. */
  6122. I915_WRITE(PIPESRC(pipe),
  6123. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6124. (intel_crtc->config->pipe_src_h - 1));
  6125. }
  6126. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6127. struct intel_crtc_state *pipe_config)
  6128. {
  6129. struct drm_device *dev = crtc->base.dev;
  6130. struct drm_i915_private *dev_priv = to_i915(dev);
  6131. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6132. uint32_t tmp;
  6133. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6134. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6135. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6136. tmp = I915_READ(HBLANK(cpu_transcoder));
  6137. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6138. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6139. tmp = I915_READ(HSYNC(cpu_transcoder));
  6140. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6141. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6142. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6143. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6144. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6145. tmp = I915_READ(VBLANK(cpu_transcoder));
  6146. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6147. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6148. tmp = I915_READ(VSYNC(cpu_transcoder));
  6149. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6150. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6151. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6152. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6153. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6154. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6155. }
  6156. }
  6157. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6158. struct intel_crtc_state *pipe_config)
  6159. {
  6160. struct drm_device *dev = crtc->base.dev;
  6161. struct drm_i915_private *dev_priv = to_i915(dev);
  6162. u32 tmp;
  6163. tmp = I915_READ(PIPESRC(crtc->pipe));
  6164. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6165. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6166. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6167. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6168. }
  6169. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6170. struct intel_crtc_state *pipe_config)
  6171. {
  6172. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6173. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6174. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6175. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6176. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6177. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6178. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6179. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6180. mode->flags = pipe_config->base.adjusted_mode.flags;
  6181. mode->type = DRM_MODE_TYPE_DRIVER;
  6182. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6183. mode->hsync = drm_mode_hsync(mode);
  6184. mode->vrefresh = drm_mode_vrefresh(mode);
  6185. drm_mode_set_name(mode);
  6186. }
  6187. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6188. {
  6189. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6190. uint32_t pipeconf;
  6191. pipeconf = 0;
  6192. /* we keep both pipes enabled on 830 */
  6193. if (IS_I830(dev_priv))
  6194. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6195. if (intel_crtc->config->double_wide)
  6196. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6197. /* only g4x and later have fancy bpc/dither controls */
  6198. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6199. IS_CHERRYVIEW(dev_priv)) {
  6200. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6201. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6202. pipeconf |= PIPECONF_DITHER_EN |
  6203. PIPECONF_DITHER_TYPE_SP;
  6204. switch (intel_crtc->config->pipe_bpp) {
  6205. case 18:
  6206. pipeconf |= PIPECONF_6BPC;
  6207. break;
  6208. case 24:
  6209. pipeconf |= PIPECONF_8BPC;
  6210. break;
  6211. case 30:
  6212. pipeconf |= PIPECONF_10BPC;
  6213. break;
  6214. default:
  6215. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6216. BUG();
  6217. }
  6218. }
  6219. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6220. if (INTEL_GEN(dev_priv) < 4 ||
  6221. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6222. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6223. else
  6224. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6225. } else
  6226. pipeconf |= PIPECONF_PROGRESSIVE;
  6227. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6228. intel_crtc->config->limited_color_range)
  6229. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6230. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6231. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6232. }
  6233. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6234. struct intel_crtc_state *crtc_state)
  6235. {
  6236. struct drm_device *dev = crtc->base.dev;
  6237. struct drm_i915_private *dev_priv = to_i915(dev);
  6238. const struct intel_limit *limit;
  6239. int refclk = 48000;
  6240. memset(&crtc_state->dpll_hw_state, 0,
  6241. sizeof(crtc_state->dpll_hw_state));
  6242. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6243. if (intel_panel_use_ssc(dev_priv)) {
  6244. refclk = dev_priv->vbt.lvds_ssc_freq;
  6245. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6246. }
  6247. limit = &intel_limits_i8xx_lvds;
  6248. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6249. limit = &intel_limits_i8xx_dvo;
  6250. } else {
  6251. limit = &intel_limits_i8xx_dac;
  6252. }
  6253. if (!crtc_state->clock_set &&
  6254. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6255. refclk, NULL, &crtc_state->dpll)) {
  6256. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6257. return -EINVAL;
  6258. }
  6259. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6260. return 0;
  6261. }
  6262. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6263. struct intel_crtc_state *crtc_state)
  6264. {
  6265. struct drm_device *dev = crtc->base.dev;
  6266. struct drm_i915_private *dev_priv = to_i915(dev);
  6267. const struct intel_limit *limit;
  6268. int refclk = 96000;
  6269. memset(&crtc_state->dpll_hw_state, 0,
  6270. sizeof(crtc_state->dpll_hw_state));
  6271. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6272. if (intel_panel_use_ssc(dev_priv)) {
  6273. refclk = dev_priv->vbt.lvds_ssc_freq;
  6274. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6275. }
  6276. if (intel_is_dual_link_lvds(dev))
  6277. limit = &intel_limits_g4x_dual_channel_lvds;
  6278. else
  6279. limit = &intel_limits_g4x_single_channel_lvds;
  6280. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6281. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6282. limit = &intel_limits_g4x_hdmi;
  6283. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6284. limit = &intel_limits_g4x_sdvo;
  6285. } else {
  6286. /* The option is for other outputs */
  6287. limit = &intel_limits_i9xx_sdvo;
  6288. }
  6289. if (!crtc_state->clock_set &&
  6290. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6291. refclk, NULL, &crtc_state->dpll)) {
  6292. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6293. return -EINVAL;
  6294. }
  6295. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6296. return 0;
  6297. }
  6298. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6299. struct intel_crtc_state *crtc_state)
  6300. {
  6301. struct drm_device *dev = crtc->base.dev;
  6302. struct drm_i915_private *dev_priv = to_i915(dev);
  6303. const struct intel_limit *limit;
  6304. int refclk = 96000;
  6305. memset(&crtc_state->dpll_hw_state, 0,
  6306. sizeof(crtc_state->dpll_hw_state));
  6307. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6308. if (intel_panel_use_ssc(dev_priv)) {
  6309. refclk = dev_priv->vbt.lvds_ssc_freq;
  6310. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6311. }
  6312. limit = &intel_limits_pineview_lvds;
  6313. } else {
  6314. limit = &intel_limits_pineview_sdvo;
  6315. }
  6316. if (!crtc_state->clock_set &&
  6317. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6318. refclk, NULL, &crtc_state->dpll)) {
  6319. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6320. return -EINVAL;
  6321. }
  6322. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6323. return 0;
  6324. }
  6325. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6326. struct intel_crtc_state *crtc_state)
  6327. {
  6328. struct drm_device *dev = crtc->base.dev;
  6329. struct drm_i915_private *dev_priv = to_i915(dev);
  6330. const struct intel_limit *limit;
  6331. int refclk = 96000;
  6332. memset(&crtc_state->dpll_hw_state, 0,
  6333. sizeof(crtc_state->dpll_hw_state));
  6334. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6335. if (intel_panel_use_ssc(dev_priv)) {
  6336. refclk = dev_priv->vbt.lvds_ssc_freq;
  6337. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6338. }
  6339. limit = &intel_limits_i9xx_lvds;
  6340. } else {
  6341. limit = &intel_limits_i9xx_sdvo;
  6342. }
  6343. if (!crtc_state->clock_set &&
  6344. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6345. refclk, NULL, &crtc_state->dpll)) {
  6346. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6347. return -EINVAL;
  6348. }
  6349. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6350. return 0;
  6351. }
  6352. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6353. struct intel_crtc_state *crtc_state)
  6354. {
  6355. int refclk = 100000;
  6356. const struct intel_limit *limit = &intel_limits_chv;
  6357. memset(&crtc_state->dpll_hw_state, 0,
  6358. sizeof(crtc_state->dpll_hw_state));
  6359. if (!crtc_state->clock_set &&
  6360. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6361. refclk, NULL, &crtc_state->dpll)) {
  6362. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6363. return -EINVAL;
  6364. }
  6365. chv_compute_dpll(crtc, crtc_state);
  6366. return 0;
  6367. }
  6368. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6369. struct intel_crtc_state *crtc_state)
  6370. {
  6371. int refclk = 100000;
  6372. const struct intel_limit *limit = &intel_limits_vlv;
  6373. memset(&crtc_state->dpll_hw_state, 0,
  6374. sizeof(crtc_state->dpll_hw_state));
  6375. if (!crtc_state->clock_set &&
  6376. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6377. refclk, NULL, &crtc_state->dpll)) {
  6378. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6379. return -EINVAL;
  6380. }
  6381. vlv_compute_dpll(crtc, crtc_state);
  6382. return 0;
  6383. }
  6384. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6385. struct intel_crtc_state *pipe_config)
  6386. {
  6387. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6388. uint32_t tmp;
  6389. if (INTEL_GEN(dev_priv) <= 3 &&
  6390. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6391. return;
  6392. tmp = I915_READ(PFIT_CONTROL);
  6393. if (!(tmp & PFIT_ENABLE))
  6394. return;
  6395. /* Check whether the pfit is attached to our pipe. */
  6396. if (INTEL_GEN(dev_priv) < 4) {
  6397. if (crtc->pipe != PIPE_B)
  6398. return;
  6399. } else {
  6400. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6401. return;
  6402. }
  6403. pipe_config->gmch_pfit.control = tmp;
  6404. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6405. }
  6406. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6407. struct intel_crtc_state *pipe_config)
  6408. {
  6409. struct drm_device *dev = crtc->base.dev;
  6410. struct drm_i915_private *dev_priv = to_i915(dev);
  6411. int pipe = pipe_config->cpu_transcoder;
  6412. struct dpll clock;
  6413. u32 mdiv;
  6414. int refclk = 100000;
  6415. /* In case of DSI, DPLL will not be used */
  6416. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6417. return;
  6418. mutex_lock(&dev_priv->sb_lock);
  6419. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6420. mutex_unlock(&dev_priv->sb_lock);
  6421. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6422. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6423. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6424. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6425. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6426. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6427. }
  6428. static void
  6429. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6430. struct intel_initial_plane_config *plane_config)
  6431. {
  6432. struct drm_device *dev = crtc->base.dev;
  6433. struct drm_i915_private *dev_priv = to_i915(dev);
  6434. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6435. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6436. enum pipe pipe = crtc->pipe;
  6437. u32 val, base, offset;
  6438. int fourcc, pixel_format;
  6439. unsigned int aligned_height;
  6440. struct drm_framebuffer *fb;
  6441. struct intel_framebuffer *intel_fb;
  6442. if (!plane->get_hw_state(plane))
  6443. return;
  6444. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6445. if (!intel_fb) {
  6446. DRM_DEBUG_KMS("failed to alloc fb\n");
  6447. return;
  6448. }
  6449. fb = &intel_fb->base;
  6450. fb->dev = dev;
  6451. val = I915_READ(DSPCNTR(i9xx_plane));
  6452. if (INTEL_GEN(dev_priv) >= 4) {
  6453. if (val & DISPPLANE_TILED) {
  6454. plane_config->tiling = I915_TILING_X;
  6455. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6456. }
  6457. }
  6458. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6459. fourcc = i9xx_format_to_fourcc(pixel_format);
  6460. fb->format = drm_format_info(fourcc);
  6461. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6462. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6463. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6464. } else if (INTEL_GEN(dev_priv) >= 4) {
  6465. if (plane_config->tiling)
  6466. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6467. else
  6468. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6469. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6470. } else {
  6471. base = I915_READ(DSPADDR(i9xx_plane));
  6472. }
  6473. plane_config->base = base;
  6474. val = I915_READ(PIPESRC(pipe));
  6475. fb->width = ((val >> 16) & 0xfff) + 1;
  6476. fb->height = ((val >> 0) & 0xfff) + 1;
  6477. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6478. fb->pitches[0] = val & 0xffffffc0;
  6479. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6480. plane_config->size = fb->pitches[0] * aligned_height;
  6481. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6482. crtc->base.name, plane->base.name, fb->width, fb->height,
  6483. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6484. plane_config->size);
  6485. plane_config->fb = intel_fb;
  6486. }
  6487. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6488. struct intel_crtc_state *pipe_config)
  6489. {
  6490. struct drm_device *dev = crtc->base.dev;
  6491. struct drm_i915_private *dev_priv = to_i915(dev);
  6492. int pipe = pipe_config->cpu_transcoder;
  6493. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6494. struct dpll clock;
  6495. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6496. int refclk = 100000;
  6497. /* In case of DSI, DPLL will not be used */
  6498. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6499. return;
  6500. mutex_lock(&dev_priv->sb_lock);
  6501. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6502. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6503. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6504. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6505. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6506. mutex_unlock(&dev_priv->sb_lock);
  6507. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6508. clock.m2 = (pll_dw0 & 0xff) << 22;
  6509. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6510. clock.m2 |= pll_dw2 & 0x3fffff;
  6511. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6512. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6513. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6514. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6515. }
  6516. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6517. struct intel_crtc_state *pipe_config)
  6518. {
  6519. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6520. enum intel_display_power_domain power_domain;
  6521. uint32_t tmp;
  6522. bool ret;
  6523. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6524. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6525. return false;
  6526. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6527. pipe_config->shared_dpll = NULL;
  6528. ret = false;
  6529. tmp = I915_READ(PIPECONF(crtc->pipe));
  6530. if (!(tmp & PIPECONF_ENABLE))
  6531. goto out;
  6532. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6533. IS_CHERRYVIEW(dev_priv)) {
  6534. switch (tmp & PIPECONF_BPC_MASK) {
  6535. case PIPECONF_6BPC:
  6536. pipe_config->pipe_bpp = 18;
  6537. break;
  6538. case PIPECONF_8BPC:
  6539. pipe_config->pipe_bpp = 24;
  6540. break;
  6541. case PIPECONF_10BPC:
  6542. pipe_config->pipe_bpp = 30;
  6543. break;
  6544. default:
  6545. break;
  6546. }
  6547. }
  6548. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6549. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6550. pipe_config->limited_color_range = true;
  6551. if (INTEL_GEN(dev_priv) < 4)
  6552. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6553. intel_get_pipe_timings(crtc, pipe_config);
  6554. intel_get_pipe_src_size(crtc, pipe_config);
  6555. i9xx_get_pfit_config(crtc, pipe_config);
  6556. if (INTEL_GEN(dev_priv) >= 4) {
  6557. /* No way to read it out on pipes B and C */
  6558. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6559. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6560. else
  6561. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6562. pipe_config->pixel_multiplier =
  6563. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6564. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6565. pipe_config->dpll_hw_state.dpll_md = tmp;
  6566. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6567. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6568. tmp = I915_READ(DPLL(crtc->pipe));
  6569. pipe_config->pixel_multiplier =
  6570. ((tmp & SDVO_MULTIPLIER_MASK)
  6571. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6572. } else {
  6573. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6574. * port and will be fixed up in the encoder->get_config
  6575. * function. */
  6576. pipe_config->pixel_multiplier = 1;
  6577. }
  6578. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6579. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6580. /*
  6581. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6582. * on 830. Filter it out here so that we don't
  6583. * report errors due to that.
  6584. */
  6585. if (IS_I830(dev_priv))
  6586. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6587. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6588. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6589. } else {
  6590. /* Mask out read-only status bits. */
  6591. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6592. DPLL_PORTC_READY_MASK |
  6593. DPLL_PORTB_READY_MASK);
  6594. }
  6595. if (IS_CHERRYVIEW(dev_priv))
  6596. chv_crtc_clock_get(crtc, pipe_config);
  6597. else if (IS_VALLEYVIEW(dev_priv))
  6598. vlv_crtc_clock_get(crtc, pipe_config);
  6599. else
  6600. i9xx_crtc_clock_get(crtc, pipe_config);
  6601. /*
  6602. * Normally the dotclock is filled in by the encoder .get_config()
  6603. * but in case the pipe is enabled w/o any ports we need a sane
  6604. * default.
  6605. */
  6606. pipe_config->base.adjusted_mode.crtc_clock =
  6607. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6608. ret = true;
  6609. out:
  6610. intel_display_power_put(dev_priv, power_domain);
  6611. return ret;
  6612. }
  6613. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6614. {
  6615. struct intel_encoder *encoder;
  6616. int i;
  6617. u32 val, final;
  6618. bool has_lvds = false;
  6619. bool has_cpu_edp = false;
  6620. bool has_panel = false;
  6621. bool has_ck505 = false;
  6622. bool can_ssc = false;
  6623. bool using_ssc_source = false;
  6624. /* We need to take the global config into account */
  6625. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6626. switch (encoder->type) {
  6627. case INTEL_OUTPUT_LVDS:
  6628. has_panel = true;
  6629. has_lvds = true;
  6630. break;
  6631. case INTEL_OUTPUT_EDP:
  6632. has_panel = true;
  6633. if (encoder->port == PORT_A)
  6634. has_cpu_edp = true;
  6635. break;
  6636. default:
  6637. break;
  6638. }
  6639. }
  6640. if (HAS_PCH_IBX(dev_priv)) {
  6641. has_ck505 = dev_priv->vbt.display_clock_mode;
  6642. can_ssc = has_ck505;
  6643. } else {
  6644. has_ck505 = false;
  6645. can_ssc = true;
  6646. }
  6647. /* Check if any DPLLs are using the SSC source */
  6648. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6649. u32 temp = I915_READ(PCH_DPLL(i));
  6650. if (!(temp & DPLL_VCO_ENABLE))
  6651. continue;
  6652. if ((temp & PLL_REF_INPUT_MASK) ==
  6653. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6654. using_ssc_source = true;
  6655. break;
  6656. }
  6657. }
  6658. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6659. has_panel, has_lvds, has_ck505, using_ssc_source);
  6660. /* Ironlake: try to setup display ref clock before DPLL
  6661. * enabling. This is only under driver's control after
  6662. * PCH B stepping, previous chipset stepping should be
  6663. * ignoring this setting.
  6664. */
  6665. val = I915_READ(PCH_DREF_CONTROL);
  6666. /* As we must carefully and slowly disable/enable each source in turn,
  6667. * compute the final state we want first and check if we need to
  6668. * make any changes at all.
  6669. */
  6670. final = val;
  6671. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6672. if (has_ck505)
  6673. final |= DREF_NONSPREAD_CK505_ENABLE;
  6674. else
  6675. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6676. final &= ~DREF_SSC_SOURCE_MASK;
  6677. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6678. final &= ~DREF_SSC1_ENABLE;
  6679. if (has_panel) {
  6680. final |= DREF_SSC_SOURCE_ENABLE;
  6681. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6682. final |= DREF_SSC1_ENABLE;
  6683. if (has_cpu_edp) {
  6684. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6685. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6686. else
  6687. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6688. } else
  6689. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6690. } else if (using_ssc_source) {
  6691. final |= DREF_SSC_SOURCE_ENABLE;
  6692. final |= DREF_SSC1_ENABLE;
  6693. }
  6694. if (final == val)
  6695. return;
  6696. /* Always enable nonspread source */
  6697. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6698. if (has_ck505)
  6699. val |= DREF_NONSPREAD_CK505_ENABLE;
  6700. else
  6701. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6702. if (has_panel) {
  6703. val &= ~DREF_SSC_SOURCE_MASK;
  6704. val |= DREF_SSC_SOURCE_ENABLE;
  6705. /* SSC must be turned on before enabling the CPU output */
  6706. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6707. DRM_DEBUG_KMS("Using SSC on panel\n");
  6708. val |= DREF_SSC1_ENABLE;
  6709. } else
  6710. val &= ~DREF_SSC1_ENABLE;
  6711. /* Get SSC going before enabling the outputs */
  6712. I915_WRITE(PCH_DREF_CONTROL, val);
  6713. POSTING_READ(PCH_DREF_CONTROL);
  6714. udelay(200);
  6715. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6716. /* Enable CPU source on CPU attached eDP */
  6717. if (has_cpu_edp) {
  6718. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6719. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6720. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6721. } else
  6722. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6723. } else
  6724. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6725. I915_WRITE(PCH_DREF_CONTROL, val);
  6726. POSTING_READ(PCH_DREF_CONTROL);
  6727. udelay(200);
  6728. } else {
  6729. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6730. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6731. /* Turn off CPU output */
  6732. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6733. I915_WRITE(PCH_DREF_CONTROL, val);
  6734. POSTING_READ(PCH_DREF_CONTROL);
  6735. udelay(200);
  6736. if (!using_ssc_source) {
  6737. DRM_DEBUG_KMS("Disabling SSC source\n");
  6738. /* Turn off the SSC source */
  6739. val &= ~DREF_SSC_SOURCE_MASK;
  6740. val |= DREF_SSC_SOURCE_DISABLE;
  6741. /* Turn off SSC1 */
  6742. val &= ~DREF_SSC1_ENABLE;
  6743. I915_WRITE(PCH_DREF_CONTROL, val);
  6744. POSTING_READ(PCH_DREF_CONTROL);
  6745. udelay(200);
  6746. }
  6747. }
  6748. BUG_ON(val != final);
  6749. }
  6750. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6751. {
  6752. uint32_t tmp;
  6753. tmp = I915_READ(SOUTH_CHICKEN2);
  6754. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6755. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6756. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6757. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6758. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6759. tmp = I915_READ(SOUTH_CHICKEN2);
  6760. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6761. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6762. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6763. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6764. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6765. }
  6766. /* WaMPhyProgramming:hsw */
  6767. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6768. {
  6769. uint32_t tmp;
  6770. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6771. tmp &= ~(0xFF << 24);
  6772. tmp |= (0x12 << 24);
  6773. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6774. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6775. tmp |= (1 << 11);
  6776. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6777. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6778. tmp |= (1 << 11);
  6779. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6780. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6781. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6782. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6783. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6784. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6785. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6786. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6787. tmp &= ~(7 << 13);
  6788. tmp |= (5 << 13);
  6789. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6790. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6791. tmp &= ~(7 << 13);
  6792. tmp |= (5 << 13);
  6793. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6794. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6795. tmp &= ~0xFF;
  6796. tmp |= 0x1C;
  6797. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6798. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6799. tmp &= ~0xFF;
  6800. tmp |= 0x1C;
  6801. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6802. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6803. tmp &= ~(0xFF << 16);
  6804. tmp |= (0x1C << 16);
  6805. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6806. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6807. tmp &= ~(0xFF << 16);
  6808. tmp |= (0x1C << 16);
  6809. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6810. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6811. tmp |= (1 << 27);
  6812. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6813. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6814. tmp |= (1 << 27);
  6815. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6816. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6817. tmp &= ~(0xF << 28);
  6818. tmp |= (4 << 28);
  6819. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6820. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6821. tmp &= ~(0xF << 28);
  6822. tmp |= (4 << 28);
  6823. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6824. }
  6825. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6826. * Programming" based on the parameters passed:
  6827. * - Sequence to enable CLKOUT_DP
  6828. * - Sequence to enable CLKOUT_DP without spread
  6829. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6830. */
  6831. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6832. bool with_spread, bool with_fdi)
  6833. {
  6834. uint32_t reg, tmp;
  6835. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6836. with_spread = true;
  6837. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6838. with_fdi, "LP PCH doesn't have FDI\n"))
  6839. with_fdi = false;
  6840. mutex_lock(&dev_priv->sb_lock);
  6841. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6842. tmp &= ~SBI_SSCCTL_DISABLE;
  6843. tmp |= SBI_SSCCTL_PATHALT;
  6844. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6845. udelay(24);
  6846. if (with_spread) {
  6847. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6848. tmp &= ~SBI_SSCCTL_PATHALT;
  6849. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6850. if (with_fdi) {
  6851. lpt_reset_fdi_mphy(dev_priv);
  6852. lpt_program_fdi_mphy(dev_priv);
  6853. }
  6854. }
  6855. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6856. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6857. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6858. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6859. mutex_unlock(&dev_priv->sb_lock);
  6860. }
  6861. /* Sequence to disable CLKOUT_DP */
  6862. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6863. {
  6864. uint32_t reg, tmp;
  6865. mutex_lock(&dev_priv->sb_lock);
  6866. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6867. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6868. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6869. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6870. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6871. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6872. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6873. tmp |= SBI_SSCCTL_PATHALT;
  6874. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6875. udelay(32);
  6876. }
  6877. tmp |= SBI_SSCCTL_DISABLE;
  6878. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6879. }
  6880. mutex_unlock(&dev_priv->sb_lock);
  6881. }
  6882. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6883. static const uint16_t sscdivintphase[] = {
  6884. [BEND_IDX( 50)] = 0x3B23,
  6885. [BEND_IDX( 45)] = 0x3B23,
  6886. [BEND_IDX( 40)] = 0x3C23,
  6887. [BEND_IDX( 35)] = 0x3C23,
  6888. [BEND_IDX( 30)] = 0x3D23,
  6889. [BEND_IDX( 25)] = 0x3D23,
  6890. [BEND_IDX( 20)] = 0x3E23,
  6891. [BEND_IDX( 15)] = 0x3E23,
  6892. [BEND_IDX( 10)] = 0x3F23,
  6893. [BEND_IDX( 5)] = 0x3F23,
  6894. [BEND_IDX( 0)] = 0x0025,
  6895. [BEND_IDX( -5)] = 0x0025,
  6896. [BEND_IDX(-10)] = 0x0125,
  6897. [BEND_IDX(-15)] = 0x0125,
  6898. [BEND_IDX(-20)] = 0x0225,
  6899. [BEND_IDX(-25)] = 0x0225,
  6900. [BEND_IDX(-30)] = 0x0325,
  6901. [BEND_IDX(-35)] = 0x0325,
  6902. [BEND_IDX(-40)] = 0x0425,
  6903. [BEND_IDX(-45)] = 0x0425,
  6904. [BEND_IDX(-50)] = 0x0525,
  6905. };
  6906. /*
  6907. * Bend CLKOUT_DP
  6908. * steps -50 to 50 inclusive, in steps of 5
  6909. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6910. * change in clock period = -(steps / 10) * 5.787 ps
  6911. */
  6912. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6913. {
  6914. uint32_t tmp;
  6915. int idx = BEND_IDX(steps);
  6916. if (WARN_ON(steps % 5 != 0))
  6917. return;
  6918. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6919. return;
  6920. mutex_lock(&dev_priv->sb_lock);
  6921. if (steps % 10 != 0)
  6922. tmp = 0xAAAAAAAB;
  6923. else
  6924. tmp = 0x00000000;
  6925. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6926. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6927. tmp &= 0xffff0000;
  6928. tmp |= sscdivintphase[idx];
  6929. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6930. mutex_unlock(&dev_priv->sb_lock);
  6931. }
  6932. #undef BEND_IDX
  6933. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6934. {
  6935. struct intel_encoder *encoder;
  6936. bool has_vga = false;
  6937. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6938. switch (encoder->type) {
  6939. case INTEL_OUTPUT_ANALOG:
  6940. has_vga = true;
  6941. break;
  6942. default:
  6943. break;
  6944. }
  6945. }
  6946. if (has_vga) {
  6947. lpt_bend_clkout_dp(dev_priv, 0);
  6948. lpt_enable_clkout_dp(dev_priv, true, true);
  6949. } else {
  6950. lpt_disable_clkout_dp(dev_priv);
  6951. }
  6952. }
  6953. /*
  6954. * Initialize reference clocks when the driver loads
  6955. */
  6956. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6957. {
  6958. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6959. ironlake_init_pch_refclk(dev_priv);
  6960. else if (HAS_PCH_LPT(dev_priv))
  6961. lpt_init_pch_refclk(dev_priv);
  6962. }
  6963. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6964. {
  6965. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6966. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6967. int pipe = intel_crtc->pipe;
  6968. uint32_t val;
  6969. val = 0;
  6970. switch (intel_crtc->config->pipe_bpp) {
  6971. case 18:
  6972. val |= PIPECONF_6BPC;
  6973. break;
  6974. case 24:
  6975. val |= PIPECONF_8BPC;
  6976. break;
  6977. case 30:
  6978. val |= PIPECONF_10BPC;
  6979. break;
  6980. case 36:
  6981. val |= PIPECONF_12BPC;
  6982. break;
  6983. default:
  6984. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6985. BUG();
  6986. }
  6987. if (intel_crtc->config->dither)
  6988. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6989. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6990. val |= PIPECONF_INTERLACED_ILK;
  6991. else
  6992. val |= PIPECONF_PROGRESSIVE;
  6993. if (intel_crtc->config->limited_color_range)
  6994. val |= PIPECONF_COLOR_RANGE_SELECT;
  6995. I915_WRITE(PIPECONF(pipe), val);
  6996. POSTING_READ(PIPECONF(pipe));
  6997. }
  6998. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6999. {
  7000. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7002. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7003. u32 val = 0;
  7004. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7005. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7006. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7007. val |= PIPECONF_INTERLACED_ILK;
  7008. else
  7009. val |= PIPECONF_PROGRESSIVE;
  7010. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7011. POSTING_READ(PIPECONF(cpu_transcoder));
  7012. }
  7013. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7014. {
  7015. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7017. struct intel_crtc_state *config = intel_crtc->config;
  7018. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7019. u32 val = 0;
  7020. switch (intel_crtc->config->pipe_bpp) {
  7021. case 18:
  7022. val |= PIPEMISC_DITHER_6_BPC;
  7023. break;
  7024. case 24:
  7025. val |= PIPEMISC_DITHER_8_BPC;
  7026. break;
  7027. case 30:
  7028. val |= PIPEMISC_DITHER_10_BPC;
  7029. break;
  7030. case 36:
  7031. val |= PIPEMISC_DITHER_12_BPC;
  7032. break;
  7033. default:
  7034. /* Case prevented by pipe_config_set_bpp. */
  7035. BUG();
  7036. }
  7037. if (intel_crtc->config->dither)
  7038. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7039. if (config->ycbcr420) {
  7040. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  7041. PIPEMISC_YUV420_ENABLE |
  7042. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7043. }
  7044. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7045. }
  7046. }
  7047. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7048. {
  7049. /*
  7050. * Account for spread spectrum to avoid
  7051. * oversubscribing the link. Max center spread
  7052. * is 2.5%; use 5% for safety's sake.
  7053. */
  7054. u32 bps = target_clock * bpp * 21 / 20;
  7055. return DIV_ROUND_UP(bps, link_bw * 8);
  7056. }
  7057. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7058. {
  7059. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7060. }
  7061. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7062. struct intel_crtc_state *crtc_state,
  7063. struct dpll *reduced_clock)
  7064. {
  7065. struct drm_crtc *crtc = &intel_crtc->base;
  7066. struct drm_device *dev = crtc->dev;
  7067. struct drm_i915_private *dev_priv = to_i915(dev);
  7068. u32 dpll, fp, fp2;
  7069. int factor;
  7070. /* Enable autotuning of the PLL clock (if permissible) */
  7071. factor = 21;
  7072. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7073. if ((intel_panel_use_ssc(dev_priv) &&
  7074. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7075. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7076. factor = 25;
  7077. } else if (crtc_state->sdvo_tv_clock)
  7078. factor = 20;
  7079. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7080. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7081. fp |= FP_CB_TUNE;
  7082. if (reduced_clock) {
  7083. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7084. if (reduced_clock->m < factor * reduced_clock->n)
  7085. fp2 |= FP_CB_TUNE;
  7086. } else {
  7087. fp2 = fp;
  7088. }
  7089. dpll = 0;
  7090. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7091. dpll |= DPLLB_MODE_LVDS;
  7092. else
  7093. dpll |= DPLLB_MODE_DAC_SERIAL;
  7094. dpll |= (crtc_state->pixel_multiplier - 1)
  7095. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7096. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7097. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7098. dpll |= DPLL_SDVO_HIGH_SPEED;
  7099. if (intel_crtc_has_dp_encoder(crtc_state))
  7100. dpll |= DPLL_SDVO_HIGH_SPEED;
  7101. /*
  7102. * The high speed IO clock is only really required for
  7103. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7104. * possible to share the DPLL between CRT and HDMI. Enabling
  7105. * the clock needlessly does no real harm, except use up a
  7106. * bit of power potentially.
  7107. *
  7108. * We'll limit this to IVB with 3 pipes, since it has only two
  7109. * DPLLs and so DPLL sharing is the only way to get three pipes
  7110. * driving PCH ports at the same time. On SNB we could do this,
  7111. * and potentially avoid enabling the second DPLL, but it's not
  7112. * clear if it''s a win or loss power wise. No point in doing
  7113. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7114. */
  7115. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7116. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7117. dpll |= DPLL_SDVO_HIGH_SPEED;
  7118. /* compute bitmask from p1 value */
  7119. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7120. /* also FPA1 */
  7121. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7122. switch (crtc_state->dpll.p2) {
  7123. case 5:
  7124. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7125. break;
  7126. case 7:
  7127. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7128. break;
  7129. case 10:
  7130. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7131. break;
  7132. case 14:
  7133. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7134. break;
  7135. }
  7136. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7137. intel_panel_use_ssc(dev_priv))
  7138. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7139. else
  7140. dpll |= PLL_REF_INPUT_DREFCLK;
  7141. dpll |= DPLL_VCO_ENABLE;
  7142. crtc_state->dpll_hw_state.dpll = dpll;
  7143. crtc_state->dpll_hw_state.fp0 = fp;
  7144. crtc_state->dpll_hw_state.fp1 = fp2;
  7145. }
  7146. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7147. struct intel_crtc_state *crtc_state)
  7148. {
  7149. struct drm_device *dev = crtc->base.dev;
  7150. struct drm_i915_private *dev_priv = to_i915(dev);
  7151. const struct intel_limit *limit;
  7152. int refclk = 120000;
  7153. memset(&crtc_state->dpll_hw_state, 0,
  7154. sizeof(crtc_state->dpll_hw_state));
  7155. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7156. if (!crtc_state->has_pch_encoder)
  7157. return 0;
  7158. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7159. if (intel_panel_use_ssc(dev_priv)) {
  7160. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7161. dev_priv->vbt.lvds_ssc_freq);
  7162. refclk = dev_priv->vbt.lvds_ssc_freq;
  7163. }
  7164. if (intel_is_dual_link_lvds(dev)) {
  7165. if (refclk == 100000)
  7166. limit = &intel_limits_ironlake_dual_lvds_100m;
  7167. else
  7168. limit = &intel_limits_ironlake_dual_lvds;
  7169. } else {
  7170. if (refclk == 100000)
  7171. limit = &intel_limits_ironlake_single_lvds_100m;
  7172. else
  7173. limit = &intel_limits_ironlake_single_lvds;
  7174. }
  7175. } else {
  7176. limit = &intel_limits_ironlake_dac;
  7177. }
  7178. if (!crtc_state->clock_set &&
  7179. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7180. refclk, NULL, &crtc_state->dpll)) {
  7181. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7182. return -EINVAL;
  7183. }
  7184. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7185. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7186. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7187. pipe_name(crtc->pipe));
  7188. return -EINVAL;
  7189. }
  7190. return 0;
  7191. }
  7192. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7193. struct intel_link_m_n *m_n)
  7194. {
  7195. struct drm_device *dev = crtc->base.dev;
  7196. struct drm_i915_private *dev_priv = to_i915(dev);
  7197. enum pipe pipe = crtc->pipe;
  7198. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7199. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7200. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7201. & ~TU_SIZE_MASK;
  7202. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7203. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7204. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7205. }
  7206. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7207. enum transcoder transcoder,
  7208. struct intel_link_m_n *m_n,
  7209. struct intel_link_m_n *m2_n2)
  7210. {
  7211. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7212. enum pipe pipe = crtc->pipe;
  7213. if (INTEL_GEN(dev_priv) >= 5) {
  7214. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7215. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7216. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7217. & ~TU_SIZE_MASK;
  7218. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7219. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7220. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7221. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7222. * gen < 8) and if DRRS is supported (to make sure the
  7223. * registers are not unnecessarily read).
  7224. */
  7225. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7226. crtc->config->has_drrs) {
  7227. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7228. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7229. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7230. & ~TU_SIZE_MASK;
  7231. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7232. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7233. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7234. }
  7235. } else {
  7236. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7237. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7238. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7239. & ~TU_SIZE_MASK;
  7240. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7241. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7242. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7243. }
  7244. }
  7245. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7246. struct intel_crtc_state *pipe_config)
  7247. {
  7248. if (pipe_config->has_pch_encoder)
  7249. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7250. else
  7251. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7252. &pipe_config->dp_m_n,
  7253. &pipe_config->dp_m2_n2);
  7254. }
  7255. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7256. struct intel_crtc_state *pipe_config)
  7257. {
  7258. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7259. &pipe_config->fdi_m_n, NULL);
  7260. }
  7261. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7262. struct intel_crtc_state *pipe_config)
  7263. {
  7264. struct drm_device *dev = crtc->base.dev;
  7265. struct drm_i915_private *dev_priv = to_i915(dev);
  7266. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7267. uint32_t ps_ctrl = 0;
  7268. int id = -1;
  7269. int i;
  7270. /* find scaler attached to this pipe */
  7271. for (i = 0; i < crtc->num_scalers; i++) {
  7272. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7273. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7274. id = i;
  7275. pipe_config->pch_pfit.enabled = true;
  7276. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7277. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7278. break;
  7279. }
  7280. }
  7281. scaler_state->scaler_id = id;
  7282. if (id >= 0) {
  7283. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7284. } else {
  7285. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7286. }
  7287. }
  7288. static void
  7289. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7290. struct intel_initial_plane_config *plane_config)
  7291. {
  7292. struct drm_device *dev = crtc->base.dev;
  7293. struct drm_i915_private *dev_priv = to_i915(dev);
  7294. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7295. enum plane_id plane_id = plane->id;
  7296. enum pipe pipe = crtc->pipe;
  7297. u32 val, base, offset, stride_mult, tiling, alpha;
  7298. int fourcc, pixel_format;
  7299. unsigned int aligned_height;
  7300. struct drm_framebuffer *fb;
  7301. struct intel_framebuffer *intel_fb;
  7302. if (!plane->get_hw_state(plane))
  7303. return;
  7304. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7305. if (!intel_fb) {
  7306. DRM_DEBUG_KMS("failed to alloc fb\n");
  7307. return;
  7308. }
  7309. fb = &intel_fb->base;
  7310. fb->dev = dev;
  7311. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7312. if (INTEL_GEN(dev_priv) >= 11)
  7313. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7314. else
  7315. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7316. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7317. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7318. alpha &= PLANE_COLOR_ALPHA_MASK;
  7319. } else {
  7320. alpha = val & PLANE_CTL_ALPHA_MASK;
  7321. }
  7322. fourcc = skl_format_to_fourcc(pixel_format,
  7323. val & PLANE_CTL_ORDER_RGBX, alpha);
  7324. fb->format = drm_format_info(fourcc);
  7325. tiling = val & PLANE_CTL_TILED_MASK;
  7326. switch (tiling) {
  7327. case PLANE_CTL_TILED_LINEAR:
  7328. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7329. break;
  7330. case PLANE_CTL_TILED_X:
  7331. plane_config->tiling = I915_TILING_X;
  7332. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7333. break;
  7334. case PLANE_CTL_TILED_Y:
  7335. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7336. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7337. else
  7338. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7339. break;
  7340. case PLANE_CTL_TILED_YF:
  7341. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7342. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7343. else
  7344. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7345. break;
  7346. default:
  7347. MISSING_CASE(tiling);
  7348. goto error;
  7349. }
  7350. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7351. plane_config->base = base;
  7352. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7353. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7354. fb->height = ((val >> 16) & 0xfff) + 1;
  7355. fb->width = ((val >> 0) & 0x1fff) + 1;
  7356. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7357. stride_mult = intel_fb_stride_alignment(fb, 0);
  7358. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7359. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7360. plane_config->size = fb->pitches[0] * aligned_height;
  7361. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7362. crtc->base.name, plane->base.name, fb->width, fb->height,
  7363. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7364. plane_config->size);
  7365. plane_config->fb = intel_fb;
  7366. return;
  7367. error:
  7368. kfree(intel_fb);
  7369. }
  7370. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7371. struct intel_crtc_state *pipe_config)
  7372. {
  7373. struct drm_device *dev = crtc->base.dev;
  7374. struct drm_i915_private *dev_priv = to_i915(dev);
  7375. uint32_t tmp;
  7376. tmp = I915_READ(PF_CTL(crtc->pipe));
  7377. if (tmp & PF_ENABLE) {
  7378. pipe_config->pch_pfit.enabled = true;
  7379. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7380. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7381. /* We currently do not free assignements of panel fitters on
  7382. * ivb/hsw (since we don't use the higher upscaling modes which
  7383. * differentiates them) so just WARN about this case for now. */
  7384. if (IS_GEN7(dev_priv)) {
  7385. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7386. PF_PIPE_SEL_IVB(crtc->pipe));
  7387. }
  7388. }
  7389. }
  7390. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7391. struct intel_crtc_state *pipe_config)
  7392. {
  7393. struct drm_device *dev = crtc->base.dev;
  7394. struct drm_i915_private *dev_priv = to_i915(dev);
  7395. enum intel_display_power_domain power_domain;
  7396. uint32_t tmp;
  7397. bool ret;
  7398. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7399. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7400. return false;
  7401. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7402. pipe_config->shared_dpll = NULL;
  7403. ret = false;
  7404. tmp = I915_READ(PIPECONF(crtc->pipe));
  7405. if (!(tmp & PIPECONF_ENABLE))
  7406. goto out;
  7407. switch (tmp & PIPECONF_BPC_MASK) {
  7408. case PIPECONF_6BPC:
  7409. pipe_config->pipe_bpp = 18;
  7410. break;
  7411. case PIPECONF_8BPC:
  7412. pipe_config->pipe_bpp = 24;
  7413. break;
  7414. case PIPECONF_10BPC:
  7415. pipe_config->pipe_bpp = 30;
  7416. break;
  7417. case PIPECONF_12BPC:
  7418. pipe_config->pipe_bpp = 36;
  7419. break;
  7420. default:
  7421. break;
  7422. }
  7423. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7424. pipe_config->limited_color_range = true;
  7425. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7426. struct intel_shared_dpll *pll;
  7427. enum intel_dpll_id pll_id;
  7428. pipe_config->has_pch_encoder = true;
  7429. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7430. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7431. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7432. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7433. if (HAS_PCH_IBX(dev_priv)) {
  7434. /*
  7435. * The pipe->pch transcoder and pch transcoder->pll
  7436. * mapping is fixed.
  7437. */
  7438. pll_id = (enum intel_dpll_id) crtc->pipe;
  7439. } else {
  7440. tmp = I915_READ(PCH_DPLL_SEL);
  7441. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7442. pll_id = DPLL_ID_PCH_PLL_B;
  7443. else
  7444. pll_id= DPLL_ID_PCH_PLL_A;
  7445. }
  7446. pipe_config->shared_dpll =
  7447. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7448. pll = pipe_config->shared_dpll;
  7449. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7450. &pipe_config->dpll_hw_state));
  7451. tmp = pipe_config->dpll_hw_state.dpll;
  7452. pipe_config->pixel_multiplier =
  7453. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7454. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7455. ironlake_pch_clock_get(crtc, pipe_config);
  7456. } else {
  7457. pipe_config->pixel_multiplier = 1;
  7458. }
  7459. intel_get_pipe_timings(crtc, pipe_config);
  7460. intel_get_pipe_src_size(crtc, pipe_config);
  7461. ironlake_get_pfit_config(crtc, pipe_config);
  7462. ret = true;
  7463. out:
  7464. intel_display_power_put(dev_priv, power_domain);
  7465. return ret;
  7466. }
  7467. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7468. {
  7469. struct drm_device *dev = &dev_priv->drm;
  7470. struct intel_crtc *crtc;
  7471. for_each_intel_crtc(dev, crtc)
  7472. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7473. pipe_name(crtc->pipe));
  7474. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7475. "Display power well on\n");
  7476. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7477. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7478. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7479. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7480. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7481. "CPU PWM1 enabled\n");
  7482. if (IS_HASWELL(dev_priv))
  7483. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7484. "CPU PWM2 enabled\n");
  7485. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7486. "PCH PWM1 enabled\n");
  7487. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7488. "Utility pin enabled\n");
  7489. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7490. /*
  7491. * In theory we can still leave IRQs enabled, as long as only the HPD
  7492. * interrupts remain enabled. We used to check for that, but since it's
  7493. * gen-specific and since we only disable LCPLL after we fully disable
  7494. * the interrupts, the check below should be enough.
  7495. */
  7496. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7497. }
  7498. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7499. {
  7500. if (IS_HASWELL(dev_priv))
  7501. return I915_READ(D_COMP_HSW);
  7502. else
  7503. return I915_READ(D_COMP_BDW);
  7504. }
  7505. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7506. {
  7507. if (IS_HASWELL(dev_priv)) {
  7508. mutex_lock(&dev_priv->pcu_lock);
  7509. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7510. val))
  7511. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7512. mutex_unlock(&dev_priv->pcu_lock);
  7513. } else {
  7514. I915_WRITE(D_COMP_BDW, val);
  7515. POSTING_READ(D_COMP_BDW);
  7516. }
  7517. }
  7518. /*
  7519. * This function implements pieces of two sequences from BSpec:
  7520. * - Sequence for display software to disable LCPLL
  7521. * - Sequence for display software to allow package C8+
  7522. * The steps implemented here are just the steps that actually touch the LCPLL
  7523. * register. Callers should take care of disabling all the display engine
  7524. * functions, doing the mode unset, fixing interrupts, etc.
  7525. */
  7526. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7527. bool switch_to_fclk, bool allow_power_down)
  7528. {
  7529. uint32_t val;
  7530. assert_can_disable_lcpll(dev_priv);
  7531. val = I915_READ(LCPLL_CTL);
  7532. if (switch_to_fclk) {
  7533. val |= LCPLL_CD_SOURCE_FCLK;
  7534. I915_WRITE(LCPLL_CTL, val);
  7535. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7536. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7537. DRM_ERROR("Switching to FCLK failed\n");
  7538. val = I915_READ(LCPLL_CTL);
  7539. }
  7540. val |= LCPLL_PLL_DISABLE;
  7541. I915_WRITE(LCPLL_CTL, val);
  7542. POSTING_READ(LCPLL_CTL);
  7543. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7544. DRM_ERROR("LCPLL still locked\n");
  7545. val = hsw_read_dcomp(dev_priv);
  7546. val |= D_COMP_COMP_DISABLE;
  7547. hsw_write_dcomp(dev_priv, val);
  7548. ndelay(100);
  7549. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7550. 1))
  7551. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7552. if (allow_power_down) {
  7553. val = I915_READ(LCPLL_CTL);
  7554. val |= LCPLL_POWER_DOWN_ALLOW;
  7555. I915_WRITE(LCPLL_CTL, val);
  7556. POSTING_READ(LCPLL_CTL);
  7557. }
  7558. }
  7559. /*
  7560. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7561. * source.
  7562. */
  7563. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7564. {
  7565. uint32_t val;
  7566. val = I915_READ(LCPLL_CTL);
  7567. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7568. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7569. return;
  7570. /*
  7571. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7572. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7573. */
  7574. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7575. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7576. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7577. I915_WRITE(LCPLL_CTL, val);
  7578. POSTING_READ(LCPLL_CTL);
  7579. }
  7580. val = hsw_read_dcomp(dev_priv);
  7581. val |= D_COMP_COMP_FORCE;
  7582. val &= ~D_COMP_COMP_DISABLE;
  7583. hsw_write_dcomp(dev_priv, val);
  7584. val = I915_READ(LCPLL_CTL);
  7585. val &= ~LCPLL_PLL_DISABLE;
  7586. I915_WRITE(LCPLL_CTL, val);
  7587. if (intel_wait_for_register(dev_priv,
  7588. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7589. 5))
  7590. DRM_ERROR("LCPLL not locked yet\n");
  7591. if (val & LCPLL_CD_SOURCE_FCLK) {
  7592. val = I915_READ(LCPLL_CTL);
  7593. val &= ~LCPLL_CD_SOURCE_FCLK;
  7594. I915_WRITE(LCPLL_CTL, val);
  7595. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7596. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7597. DRM_ERROR("Switching back to LCPLL failed\n");
  7598. }
  7599. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7600. intel_update_cdclk(dev_priv);
  7601. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7602. }
  7603. /*
  7604. * Package states C8 and deeper are really deep PC states that can only be
  7605. * reached when all the devices on the system allow it, so even if the graphics
  7606. * device allows PC8+, it doesn't mean the system will actually get to these
  7607. * states. Our driver only allows PC8+ when going into runtime PM.
  7608. *
  7609. * The requirements for PC8+ are that all the outputs are disabled, the power
  7610. * well is disabled and most interrupts are disabled, and these are also
  7611. * requirements for runtime PM. When these conditions are met, we manually do
  7612. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7613. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7614. * hang the machine.
  7615. *
  7616. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7617. * the state of some registers, so when we come back from PC8+ we need to
  7618. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7619. * need to take care of the registers kept by RC6. Notice that this happens even
  7620. * if we don't put the device in PCI D3 state (which is what currently happens
  7621. * because of the runtime PM support).
  7622. *
  7623. * For more, read "Display Sequences for Package C8" on the hardware
  7624. * documentation.
  7625. */
  7626. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7627. {
  7628. uint32_t val;
  7629. DRM_DEBUG_KMS("Enabling package C8+\n");
  7630. if (HAS_PCH_LPT_LP(dev_priv)) {
  7631. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7632. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7633. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7634. }
  7635. lpt_disable_clkout_dp(dev_priv);
  7636. hsw_disable_lcpll(dev_priv, true, true);
  7637. }
  7638. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7639. {
  7640. uint32_t val;
  7641. DRM_DEBUG_KMS("Disabling package C8+\n");
  7642. hsw_restore_lcpll(dev_priv);
  7643. lpt_init_pch_refclk(dev_priv);
  7644. if (HAS_PCH_LPT_LP(dev_priv)) {
  7645. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7646. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7647. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7648. }
  7649. }
  7650. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7651. struct intel_crtc_state *crtc_state)
  7652. {
  7653. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7654. struct intel_encoder *encoder =
  7655. intel_ddi_get_crtc_new_encoder(crtc_state);
  7656. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7657. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7658. pipe_name(crtc->pipe));
  7659. return -EINVAL;
  7660. }
  7661. }
  7662. return 0;
  7663. }
  7664. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7665. enum port port,
  7666. struct intel_crtc_state *pipe_config)
  7667. {
  7668. enum intel_dpll_id id;
  7669. u32 temp;
  7670. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7671. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7672. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7673. return;
  7674. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7675. }
  7676. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7677. enum port port,
  7678. struct intel_crtc_state *pipe_config)
  7679. {
  7680. enum intel_dpll_id id;
  7681. switch (port) {
  7682. case PORT_A:
  7683. id = DPLL_ID_SKL_DPLL0;
  7684. break;
  7685. case PORT_B:
  7686. id = DPLL_ID_SKL_DPLL1;
  7687. break;
  7688. case PORT_C:
  7689. id = DPLL_ID_SKL_DPLL2;
  7690. break;
  7691. default:
  7692. DRM_ERROR("Incorrect port type\n");
  7693. return;
  7694. }
  7695. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7696. }
  7697. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7698. enum port port,
  7699. struct intel_crtc_state *pipe_config)
  7700. {
  7701. enum intel_dpll_id id;
  7702. u32 temp;
  7703. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7704. id = temp >> (port * 3 + 1);
  7705. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7706. return;
  7707. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7708. }
  7709. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7710. enum port port,
  7711. struct intel_crtc_state *pipe_config)
  7712. {
  7713. enum intel_dpll_id id;
  7714. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7715. switch (ddi_pll_sel) {
  7716. case PORT_CLK_SEL_WRPLL1:
  7717. id = DPLL_ID_WRPLL1;
  7718. break;
  7719. case PORT_CLK_SEL_WRPLL2:
  7720. id = DPLL_ID_WRPLL2;
  7721. break;
  7722. case PORT_CLK_SEL_SPLL:
  7723. id = DPLL_ID_SPLL;
  7724. break;
  7725. case PORT_CLK_SEL_LCPLL_810:
  7726. id = DPLL_ID_LCPLL_810;
  7727. break;
  7728. case PORT_CLK_SEL_LCPLL_1350:
  7729. id = DPLL_ID_LCPLL_1350;
  7730. break;
  7731. case PORT_CLK_SEL_LCPLL_2700:
  7732. id = DPLL_ID_LCPLL_2700;
  7733. break;
  7734. default:
  7735. MISSING_CASE(ddi_pll_sel);
  7736. /* fall through */
  7737. case PORT_CLK_SEL_NONE:
  7738. return;
  7739. }
  7740. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7741. }
  7742. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7743. struct intel_crtc_state *pipe_config,
  7744. u64 *power_domain_mask)
  7745. {
  7746. struct drm_device *dev = crtc->base.dev;
  7747. struct drm_i915_private *dev_priv = to_i915(dev);
  7748. enum intel_display_power_domain power_domain;
  7749. u32 tmp;
  7750. /*
  7751. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7752. * transcoder handled below.
  7753. */
  7754. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7755. /*
  7756. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7757. * consistency and less surprising code; it's in always on power).
  7758. */
  7759. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7760. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7761. enum pipe trans_edp_pipe;
  7762. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7763. default:
  7764. WARN(1, "unknown pipe linked to edp transcoder\n");
  7765. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7766. case TRANS_DDI_EDP_INPUT_A_ON:
  7767. trans_edp_pipe = PIPE_A;
  7768. break;
  7769. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7770. trans_edp_pipe = PIPE_B;
  7771. break;
  7772. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7773. trans_edp_pipe = PIPE_C;
  7774. break;
  7775. }
  7776. if (trans_edp_pipe == crtc->pipe)
  7777. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7778. }
  7779. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7780. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7781. return false;
  7782. *power_domain_mask |= BIT_ULL(power_domain);
  7783. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7784. return tmp & PIPECONF_ENABLE;
  7785. }
  7786. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7787. struct intel_crtc_state *pipe_config,
  7788. u64 *power_domain_mask)
  7789. {
  7790. struct drm_device *dev = crtc->base.dev;
  7791. struct drm_i915_private *dev_priv = to_i915(dev);
  7792. enum intel_display_power_domain power_domain;
  7793. enum port port;
  7794. enum transcoder cpu_transcoder;
  7795. u32 tmp;
  7796. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7797. if (port == PORT_A)
  7798. cpu_transcoder = TRANSCODER_DSI_A;
  7799. else
  7800. cpu_transcoder = TRANSCODER_DSI_C;
  7801. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7802. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7803. continue;
  7804. *power_domain_mask |= BIT_ULL(power_domain);
  7805. /*
  7806. * The PLL needs to be enabled with a valid divider
  7807. * configuration, otherwise accessing DSI registers will hang
  7808. * the machine. See BSpec North Display Engine
  7809. * registers/MIPI[BXT]. We can break out here early, since we
  7810. * need the same DSI PLL to be enabled for both DSI ports.
  7811. */
  7812. if (!intel_dsi_pll_is_enabled(dev_priv))
  7813. break;
  7814. /* XXX: this works for video mode only */
  7815. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7816. if (!(tmp & DPI_ENABLE))
  7817. continue;
  7818. tmp = I915_READ(MIPI_CTRL(port));
  7819. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7820. continue;
  7821. pipe_config->cpu_transcoder = cpu_transcoder;
  7822. break;
  7823. }
  7824. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7825. }
  7826. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7827. struct intel_crtc_state *pipe_config)
  7828. {
  7829. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7830. struct intel_shared_dpll *pll;
  7831. enum port port;
  7832. uint32_t tmp;
  7833. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7834. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7835. if (IS_CANNONLAKE(dev_priv))
  7836. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7837. else if (IS_GEN9_BC(dev_priv))
  7838. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7839. else if (IS_GEN9_LP(dev_priv))
  7840. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7841. else
  7842. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7843. pll = pipe_config->shared_dpll;
  7844. if (pll) {
  7845. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7846. &pipe_config->dpll_hw_state));
  7847. }
  7848. /*
  7849. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7850. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7851. * the PCH transcoder is on.
  7852. */
  7853. if (INTEL_GEN(dev_priv) < 9 &&
  7854. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7855. pipe_config->has_pch_encoder = true;
  7856. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7857. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7858. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7859. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7860. }
  7861. }
  7862. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7863. struct intel_crtc_state *pipe_config)
  7864. {
  7865. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7866. enum intel_display_power_domain power_domain;
  7867. u64 power_domain_mask;
  7868. bool active;
  7869. intel_crtc_init_scalers(crtc, pipe_config);
  7870. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7871. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7872. return false;
  7873. power_domain_mask = BIT_ULL(power_domain);
  7874. pipe_config->shared_dpll = NULL;
  7875. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7876. if (IS_GEN9_LP(dev_priv) &&
  7877. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7878. WARN_ON(active);
  7879. active = true;
  7880. }
  7881. if (!active)
  7882. goto out;
  7883. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7884. haswell_get_ddi_port_state(crtc, pipe_config);
  7885. intel_get_pipe_timings(crtc, pipe_config);
  7886. }
  7887. intel_get_pipe_src_size(crtc, pipe_config);
  7888. pipe_config->gamma_mode =
  7889. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7890. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7891. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7892. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7893. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7894. bool blend_mode_420 = tmp &
  7895. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7896. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7897. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7898. pipe_config->ycbcr420 != blend_mode_420)
  7899. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7900. } else if (clrspace_yuv) {
  7901. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7902. }
  7903. }
  7904. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7905. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7906. power_domain_mask |= BIT_ULL(power_domain);
  7907. if (INTEL_GEN(dev_priv) >= 9)
  7908. skylake_get_pfit_config(crtc, pipe_config);
  7909. else
  7910. ironlake_get_pfit_config(crtc, pipe_config);
  7911. }
  7912. if (hsw_crtc_supports_ips(crtc)) {
  7913. if (IS_HASWELL(dev_priv))
  7914. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7915. else {
  7916. /*
  7917. * We cannot readout IPS state on broadwell, set to
  7918. * true so we can set it to a defined state on first
  7919. * commit.
  7920. */
  7921. pipe_config->ips_enabled = true;
  7922. }
  7923. }
  7924. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7925. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7926. pipe_config->pixel_multiplier =
  7927. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7928. } else {
  7929. pipe_config->pixel_multiplier = 1;
  7930. }
  7931. out:
  7932. for_each_power_domain(power_domain, power_domain_mask)
  7933. intel_display_power_put(dev_priv, power_domain);
  7934. return active;
  7935. }
  7936. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7937. {
  7938. struct drm_i915_private *dev_priv =
  7939. to_i915(plane_state->base.plane->dev);
  7940. const struct drm_framebuffer *fb = plane_state->base.fb;
  7941. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7942. u32 base;
  7943. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7944. base = obj->phys_handle->busaddr;
  7945. else
  7946. base = intel_plane_ggtt_offset(plane_state);
  7947. base += plane_state->main.offset;
  7948. /* ILK+ do this automagically */
  7949. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7950. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7951. base += (plane_state->base.crtc_h *
  7952. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7953. return base;
  7954. }
  7955. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7956. {
  7957. int x = plane_state->base.crtc_x;
  7958. int y = plane_state->base.crtc_y;
  7959. u32 pos = 0;
  7960. if (x < 0) {
  7961. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7962. x = -x;
  7963. }
  7964. pos |= x << CURSOR_X_SHIFT;
  7965. if (y < 0) {
  7966. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7967. y = -y;
  7968. }
  7969. pos |= y << CURSOR_Y_SHIFT;
  7970. return pos;
  7971. }
  7972. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7973. {
  7974. const struct drm_mode_config *config =
  7975. &plane_state->base.plane->dev->mode_config;
  7976. int width = plane_state->base.crtc_w;
  7977. int height = plane_state->base.crtc_h;
  7978. return width > 0 && width <= config->cursor_width &&
  7979. height > 0 && height <= config->cursor_height;
  7980. }
  7981. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7982. struct intel_plane_state *plane_state)
  7983. {
  7984. const struct drm_framebuffer *fb = plane_state->base.fb;
  7985. int src_x, src_y;
  7986. u32 offset;
  7987. int ret;
  7988. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7989. &crtc_state->base,
  7990. DRM_PLANE_HELPER_NO_SCALING,
  7991. DRM_PLANE_HELPER_NO_SCALING,
  7992. true, true);
  7993. if (ret)
  7994. return ret;
  7995. if (!fb)
  7996. return 0;
  7997. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7998. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7999. return -EINVAL;
  8000. }
  8001. src_x = plane_state->base.src_x >> 16;
  8002. src_y = plane_state->base.src_y >> 16;
  8003. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  8004. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  8005. if (src_x != 0 || src_y != 0) {
  8006. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  8007. return -EINVAL;
  8008. }
  8009. plane_state->main.offset = offset;
  8010. return 0;
  8011. }
  8012. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8013. const struct intel_plane_state *plane_state)
  8014. {
  8015. const struct drm_framebuffer *fb = plane_state->base.fb;
  8016. return CURSOR_ENABLE |
  8017. CURSOR_GAMMA_ENABLE |
  8018. CURSOR_FORMAT_ARGB |
  8019. CURSOR_STRIDE(fb->pitches[0]);
  8020. }
  8021. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  8022. {
  8023. int width = plane_state->base.crtc_w;
  8024. /*
  8025. * 845g/865g are only limited by the width of their cursors,
  8026. * the height is arbitrary up to the precision of the register.
  8027. */
  8028. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  8029. }
  8030. static int i845_check_cursor(struct intel_plane *plane,
  8031. struct intel_crtc_state *crtc_state,
  8032. struct intel_plane_state *plane_state)
  8033. {
  8034. const struct drm_framebuffer *fb = plane_state->base.fb;
  8035. int ret;
  8036. ret = intel_check_cursor(crtc_state, plane_state);
  8037. if (ret)
  8038. return ret;
  8039. /* if we want to turn off the cursor ignore width and height */
  8040. if (!fb)
  8041. return 0;
  8042. /* Check for which cursor types we support */
  8043. if (!i845_cursor_size_ok(plane_state)) {
  8044. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8045. plane_state->base.crtc_w,
  8046. plane_state->base.crtc_h);
  8047. return -EINVAL;
  8048. }
  8049. switch (fb->pitches[0]) {
  8050. case 256:
  8051. case 512:
  8052. case 1024:
  8053. case 2048:
  8054. break;
  8055. default:
  8056. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  8057. fb->pitches[0]);
  8058. return -EINVAL;
  8059. }
  8060. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  8061. return 0;
  8062. }
  8063. static void i845_update_cursor(struct intel_plane *plane,
  8064. const struct intel_crtc_state *crtc_state,
  8065. const struct intel_plane_state *plane_state)
  8066. {
  8067. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8068. u32 cntl = 0, base = 0, pos = 0, size = 0;
  8069. unsigned long irqflags;
  8070. if (plane_state && plane_state->base.visible) {
  8071. unsigned int width = plane_state->base.crtc_w;
  8072. unsigned int height = plane_state->base.crtc_h;
  8073. cntl = plane_state->ctl;
  8074. size = (height << 12) | width;
  8075. base = intel_cursor_base(plane_state);
  8076. pos = intel_cursor_position(plane_state);
  8077. }
  8078. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8079. /* On these chipsets we can only modify the base/size/stride
  8080. * whilst the cursor is disabled.
  8081. */
  8082. if (plane->cursor.base != base ||
  8083. plane->cursor.size != size ||
  8084. plane->cursor.cntl != cntl) {
  8085. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  8086. I915_WRITE_FW(CURBASE(PIPE_A), base);
  8087. I915_WRITE_FW(CURSIZE, size);
  8088. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8089. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  8090. plane->cursor.base = base;
  8091. plane->cursor.size = size;
  8092. plane->cursor.cntl = cntl;
  8093. } else {
  8094. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8095. }
  8096. POSTING_READ_FW(CURCNTR(PIPE_A));
  8097. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8098. }
  8099. static void i845_disable_cursor(struct intel_plane *plane,
  8100. struct intel_crtc *crtc)
  8101. {
  8102. i845_update_cursor(plane, NULL, NULL);
  8103. }
  8104. static bool i845_cursor_get_hw_state(struct intel_plane *plane)
  8105. {
  8106. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8107. enum intel_display_power_domain power_domain;
  8108. bool ret;
  8109. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  8110. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8111. return false;
  8112. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  8113. intel_display_power_put(dev_priv, power_domain);
  8114. return ret;
  8115. }
  8116. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8117. const struct intel_plane_state *plane_state)
  8118. {
  8119. struct drm_i915_private *dev_priv =
  8120. to_i915(plane_state->base.plane->dev);
  8121. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8122. u32 cntl;
  8123. cntl = MCURSOR_GAMMA_ENABLE;
  8124. if (HAS_DDI(dev_priv))
  8125. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8126. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8127. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8128. switch (plane_state->base.crtc_w) {
  8129. case 64:
  8130. cntl |= CURSOR_MODE_64_ARGB_AX;
  8131. break;
  8132. case 128:
  8133. cntl |= CURSOR_MODE_128_ARGB_AX;
  8134. break;
  8135. case 256:
  8136. cntl |= CURSOR_MODE_256_ARGB_AX;
  8137. break;
  8138. default:
  8139. MISSING_CASE(plane_state->base.crtc_w);
  8140. return 0;
  8141. }
  8142. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8143. cntl |= CURSOR_ROTATE_180;
  8144. return cntl;
  8145. }
  8146. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8147. {
  8148. struct drm_i915_private *dev_priv =
  8149. to_i915(plane_state->base.plane->dev);
  8150. int width = plane_state->base.crtc_w;
  8151. int height = plane_state->base.crtc_h;
  8152. if (!intel_cursor_size_ok(plane_state))
  8153. return false;
  8154. /* Cursor width is limited to a few power-of-two sizes */
  8155. switch (width) {
  8156. case 256:
  8157. case 128:
  8158. case 64:
  8159. break;
  8160. default:
  8161. return false;
  8162. }
  8163. /*
  8164. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8165. * height from 8 lines up to the cursor width, when the
  8166. * cursor is not rotated. Everything else requires square
  8167. * cursors.
  8168. */
  8169. if (HAS_CUR_FBC(dev_priv) &&
  8170. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8171. if (height < 8 || height > width)
  8172. return false;
  8173. } else {
  8174. if (height != width)
  8175. return false;
  8176. }
  8177. return true;
  8178. }
  8179. static int i9xx_check_cursor(struct intel_plane *plane,
  8180. struct intel_crtc_state *crtc_state,
  8181. struct intel_plane_state *plane_state)
  8182. {
  8183. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8184. const struct drm_framebuffer *fb = plane_state->base.fb;
  8185. enum pipe pipe = plane->pipe;
  8186. int ret;
  8187. ret = intel_check_cursor(crtc_state, plane_state);
  8188. if (ret)
  8189. return ret;
  8190. /* if we want to turn off the cursor ignore width and height */
  8191. if (!fb)
  8192. return 0;
  8193. /* Check for which cursor types we support */
  8194. if (!i9xx_cursor_size_ok(plane_state)) {
  8195. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8196. plane_state->base.crtc_w,
  8197. plane_state->base.crtc_h);
  8198. return -EINVAL;
  8199. }
  8200. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8201. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8202. fb->pitches[0], plane_state->base.crtc_w);
  8203. return -EINVAL;
  8204. }
  8205. /*
  8206. * There's something wrong with the cursor on CHV pipe C.
  8207. * If it straddles the left edge of the screen then
  8208. * moving it away from the edge or disabling it often
  8209. * results in a pipe underrun, and often that can lead to
  8210. * dead pipe (constant underrun reported, and it scans
  8211. * out just a solid color). To recover from that, the
  8212. * display power well must be turned off and on again.
  8213. * Refuse the put the cursor into that compromised position.
  8214. */
  8215. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8216. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8217. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8218. return -EINVAL;
  8219. }
  8220. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8221. return 0;
  8222. }
  8223. static void i9xx_update_cursor(struct intel_plane *plane,
  8224. const struct intel_crtc_state *crtc_state,
  8225. const struct intel_plane_state *plane_state)
  8226. {
  8227. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8228. enum pipe pipe = plane->pipe;
  8229. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8230. unsigned long irqflags;
  8231. if (plane_state && plane_state->base.visible) {
  8232. cntl = plane_state->ctl;
  8233. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8234. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8235. base = intel_cursor_base(plane_state);
  8236. pos = intel_cursor_position(plane_state);
  8237. }
  8238. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8239. /*
  8240. * On some platforms writing CURCNTR first will also
  8241. * cause CURPOS to be armed by the CURBASE write.
  8242. * Without the CURCNTR write the CURPOS write would
  8243. * arm itself. Thus we always start the full update
  8244. * with a CURCNTR write.
  8245. *
  8246. * On other platforms CURPOS always requires the
  8247. * CURBASE write to arm the update. Additonally
  8248. * a write to any of the cursor register will cancel
  8249. * an already armed cursor update. Thus leaving out
  8250. * the CURBASE write after CURPOS could lead to a
  8251. * cursor that doesn't appear to move, or even change
  8252. * shape. Thus we always write CURBASE.
  8253. *
  8254. * CURCNTR and CUR_FBC_CTL are always
  8255. * armed by the CURBASE write only.
  8256. */
  8257. if (plane->cursor.base != base ||
  8258. plane->cursor.size != fbc_ctl ||
  8259. plane->cursor.cntl != cntl) {
  8260. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8261. if (HAS_CUR_FBC(dev_priv))
  8262. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8263. I915_WRITE_FW(CURPOS(pipe), pos);
  8264. I915_WRITE_FW(CURBASE(pipe), base);
  8265. plane->cursor.base = base;
  8266. plane->cursor.size = fbc_ctl;
  8267. plane->cursor.cntl = cntl;
  8268. } else {
  8269. I915_WRITE_FW(CURPOS(pipe), pos);
  8270. I915_WRITE_FW(CURBASE(pipe), base);
  8271. }
  8272. POSTING_READ_FW(CURBASE(pipe));
  8273. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8274. }
  8275. static void i9xx_disable_cursor(struct intel_plane *plane,
  8276. struct intel_crtc *crtc)
  8277. {
  8278. i9xx_update_cursor(plane, NULL, NULL);
  8279. }
  8280. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
  8281. {
  8282. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8283. enum intel_display_power_domain power_domain;
  8284. enum pipe pipe = plane->pipe;
  8285. bool ret;
  8286. /*
  8287. * Not 100% correct for planes that can move between pipes,
  8288. * but that's only the case for gen2-3 which don't have any
  8289. * display power wells.
  8290. */
  8291. power_domain = POWER_DOMAIN_PIPE(pipe);
  8292. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8293. return false;
  8294. ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  8295. intel_display_power_put(dev_priv, power_domain);
  8296. return ret;
  8297. }
  8298. /* VESA 640x480x72Hz mode to set on the pipe */
  8299. static const struct drm_display_mode load_detect_mode = {
  8300. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8301. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8302. };
  8303. struct drm_framebuffer *
  8304. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8305. struct drm_mode_fb_cmd2 *mode_cmd)
  8306. {
  8307. struct intel_framebuffer *intel_fb;
  8308. int ret;
  8309. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8310. if (!intel_fb)
  8311. return ERR_PTR(-ENOMEM);
  8312. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8313. if (ret)
  8314. goto err;
  8315. return &intel_fb->base;
  8316. err:
  8317. kfree(intel_fb);
  8318. return ERR_PTR(ret);
  8319. }
  8320. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8321. struct drm_crtc *crtc)
  8322. {
  8323. struct drm_plane *plane;
  8324. struct drm_plane_state *plane_state;
  8325. int ret, i;
  8326. ret = drm_atomic_add_affected_planes(state, crtc);
  8327. if (ret)
  8328. return ret;
  8329. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8330. if (plane_state->crtc != crtc)
  8331. continue;
  8332. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8333. if (ret)
  8334. return ret;
  8335. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8336. }
  8337. return 0;
  8338. }
  8339. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8340. const struct drm_display_mode *mode,
  8341. struct intel_load_detect_pipe *old,
  8342. struct drm_modeset_acquire_ctx *ctx)
  8343. {
  8344. struct intel_crtc *intel_crtc;
  8345. struct intel_encoder *intel_encoder =
  8346. intel_attached_encoder(connector);
  8347. struct drm_crtc *possible_crtc;
  8348. struct drm_encoder *encoder = &intel_encoder->base;
  8349. struct drm_crtc *crtc = NULL;
  8350. struct drm_device *dev = encoder->dev;
  8351. struct drm_i915_private *dev_priv = to_i915(dev);
  8352. struct drm_mode_config *config = &dev->mode_config;
  8353. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8354. struct drm_connector_state *connector_state;
  8355. struct intel_crtc_state *crtc_state;
  8356. int ret, i = -1;
  8357. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8358. connector->base.id, connector->name,
  8359. encoder->base.id, encoder->name);
  8360. old->restore_state = NULL;
  8361. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8362. /*
  8363. * Algorithm gets a little messy:
  8364. *
  8365. * - if the connector already has an assigned crtc, use it (but make
  8366. * sure it's on first)
  8367. *
  8368. * - try to find the first unused crtc that can drive this connector,
  8369. * and use that if we find one
  8370. */
  8371. /* See if we already have a CRTC for this connector */
  8372. if (connector->state->crtc) {
  8373. crtc = connector->state->crtc;
  8374. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8375. if (ret)
  8376. goto fail;
  8377. /* Make sure the crtc and connector are running */
  8378. goto found;
  8379. }
  8380. /* Find an unused one (if possible) */
  8381. for_each_crtc(dev, possible_crtc) {
  8382. i++;
  8383. if (!(encoder->possible_crtcs & (1 << i)))
  8384. continue;
  8385. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8386. if (ret)
  8387. goto fail;
  8388. if (possible_crtc->state->enable) {
  8389. drm_modeset_unlock(&possible_crtc->mutex);
  8390. continue;
  8391. }
  8392. crtc = possible_crtc;
  8393. break;
  8394. }
  8395. /*
  8396. * If we didn't find an unused CRTC, don't use any.
  8397. */
  8398. if (!crtc) {
  8399. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8400. ret = -ENODEV;
  8401. goto fail;
  8402. }
  8403. found:
  8404. intel_crtc = to_intel_crtc(crtc);
  8405. state = drm_atomic_state_alloc(dev);
  8406. restore_state = drm_atomic_state_alloc(dev);
  8407. if (!state || !restore_state) {
  8408. ret = -ENOMEM;
  8409. goto fail;
  8410. }
  8411. state->acquire_ctx = ctx;
  8412. restore_state->acquire_ctx = ctx;
  8413. connector_state = drm_atomic_get_connector_state(state, connector);
  8414. if (IS_ERR(connector_state)) {
  8415. ret = PTR_ERR(connector_state);
  8416. goto fail;
  8417. }
  8418. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8419. if (ret)
  8420. goto fail;
  8421. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8422. if (IS_ERR(crtc_state)) {
  8423. ret = PTR_ERR(crtc_state);
  8424. goto fail;
  8425. }
  8426. crtc_state->base.active = crtc_state->base.enable = true;
  8427. if (!mode)
  8428. mode = &load_detect_mode;
  8429. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8430. if (ret)
  8431. goto fail;
  8432. ret = intel_modeset_disable_planes(state, crtc);
  8433. if (ret)
  8434. goto fail;
  8435. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8436. if (!ret)
  8437. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8438. if (!ret)
  8439. ret = drm_atomic_add_affected_planes(restore_state, crtc);
  8440. if (ret) {
  8441. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8442. goto fail;
  8443. }
  8444. ret = drm_atomic_commit(state);
  8445. if (ret) {
  8446. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8447. goto fail;
  8448. }
  8449. old->restore_state = restore_state;
  8450. drm_atomic_state_put(state);
  8451. /* let the connector get through one full cycle before testing */
  8452. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8453. return true;
  8454. fail:
  8455. if (state) {
  8456. drm_atomic_state_put(state);
  8457. state = NULL;
  8458. }
  8459. if (restore_state) {
  8460. drm_atomic_state_put(restore_state);
  8461. restore_state = NULL;
  8462. }
  8463. if (ret == -EDEADLK)
  8464. return ret;
  8465. return false;
  8466. }
  8467. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8468. struct intel_load_detect_pipe *old,
  8469. struct drm_modeset_acquire_ctx *ctx)
  8470. {
  8471. struct intel_encoder *intel_encoder =
  8472. intel_attached_encoder(connector);
  8473. struct drm_encoder *encoder = &intel_encoder->base;
  8474. struct drm_atomic_state *state = old->restore_state;
  8475. int ret;
  8476. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8477. connector->base.id, connector->name,
  8478. encoder->base.id, encoder->name);
  8479. if (!state)
  8480. return;
  8481. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8482. if (ret)
  8483. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8484. drm_atomic_state_put(state);
  8485. }
  8486. static int i9xx_pll_refclk(struct drm_device *dev,
  8487. const struct intel_crtc_state *pipe_config)
  8488. {
  8489. struct drm_i915_private *dev_priv = to_i915(dev);
  8490. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8491. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8492. return dev_priv->vbt.lvds_ssc_freq;
  8493. else if (HAS_PCH_SPLIT(dev_priv))
  8494. return 120000;
  8495. else if (!IS_GEN2(dev_priv))
  8496. return 96000;
  8497. else
  8498. return 48000;
  8499. }
  8500. /* Returns the clock of the currently programmed mode of the given pipe. */
  8501. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8502. struct intel_crtc_state *pipe_config)
  8503. {
  8504. struct drm_device *dev = crtc->base.dev;
  8505. struct drm_i915_private *dev_priv = to_i915(dev);
  8506. int pipe = pipe_config->cpu_transcoder;
  8507. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8508. u32 fp;
  8509. struct dpll clock;
  8510. int port_clock;
  8511. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8512. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8513. fp = pipe_config->dpll_hw_state.fp0;
  8514. else
  8515. fp = pipe_config->dpll_hw_state.fp1;
  8516. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8517. if (IS_PINEVIEW(dev_priv)) {
  8518. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8519. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8520. } else {
  8521. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8522. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8523. }
  8524. if (!IS_GEN2(dev_priv)) {
  8525. if (IS_PINEVIEW(dev_priv))
  8526. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8527. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8528. else
  8529. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8530. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8531. switch (dpll & DPLL_MODE_MASK) {
  8532. case DPLLB_MODE_DAC_SERIAL:
  8533. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8534. 5 : 10;
  8535. break;
  8536. case DPLLB_MODE_LVDS:
  8537. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8538. 7 : 14;
  8539. break;
  8540. default:
  8541. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8542. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8543. return;
  8544. }
  8545. if (IS_PINEVIEW(dev_priv))
  8546. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8547. else
  8548. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8549. } else {
  8550. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8551. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8552. if (is_lvds) {
  8553. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8554. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8555. if (lvds & LVDS_CLKB_POWER_UP)
  8556. clock.p2 = 7;
  8557. else
  8558. clock.p2 = 14;
  8559. } else {
  8560. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8561. clock.p1 = 2;
  8562. else {
  8563. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8564. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8565. }
  8566. if (dpll & PLL_P2_DIVIDE_BY_4)
  8567. clock.p2 = 4;
  8568. else
  8569. clock.p2 = 2;
  8570. }
  8571. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8572. }
  8573. /*
  8574. * This value includes pixel_multiplier. We will use
  8575. * port_clock to compute adjusted_mode.crtc_clock in the
  8576. * encoder's get_config() function.
  8577. */
  8578. pipe_config->port_clock = port_clock;
  8579. }
  8580. int intel_dotclock_calculate(int link_freq,
  8581. const struct intel_link_m_n *m_n)
  8582. {
  8583. /*
  8584. * The calculation for the data clock is:
  8585. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8586. * But we want to avoid losing precison if possible, so:
  8587. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8588. *
  8589. * and the link clock is simpler:
  8590. * link_clock = (m * link_clock) / n
  8591. */
  8592. if (!m_n->link_n)
  8593. return 0;
  8594. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8595. }
  8596. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8597. struct intel_crtc_state *pipe_config)
  8598. {
  8599. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8600. /* read out port_clock from the DPLL */
  8601. i9xx_crtc_clock_get(crtc, pipe_config);
  8602. /*
  8603. * In case there is an active pipe without active ports,
  8604. * we may need some idea for the dotclock anyway.
  8605. * Calculate one based on the FDI configuration.
  8606. */
  8607. pipe_config->base.adjusted_mode.crtc_clock =
  8608. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8609. &pipe_config->fdi_m_n);
  8610. }
  8611. /* Returns the currently programmed mode of the given encoder. */
  8612. struct drm_display_mode *
  8613. intel_encoder_current_mode(struct intel_encoder *encoder)
  8614. {
  8615. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8616. struct intel_crtc_state *crtc_state;
  8617. struct drm_display_mode *mode;
  8618. struct intel_crtc *crtc;
  8619. enum pipe pipe;
  8620. if (!encoder->get_hw_state(encoder, &pipe))
  8621. return NULL;
  8622. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8623. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8624. if (!mode)
  8625. return NULL;
  8626. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8627. if (!crtc_state) {
  8628. kfree(mode);
  8629. return NULL;
  8630. }
  8631. crtc_state->base.crtc = &crtc->base;
  8632. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8633. kfree(crtc_state);
  8634. kfree(mode);
  8635. return NULL;
  8636. }
  8637. encoder->get_config(encoder, crtc_state);
  8638. intel_mode_from_pipe_config(mode, crtc_state);
  8639. kfree(crtc_state);
  8640. return mode;
  8641. }
  8642. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8643. {
  8644. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8645. drm_crtc_cleanup(crtc);
  8646. kfree(intel_crtc);
  8647. }
  8648. /**
  8649. * intel_wm_need_update - Check whether watermarks need updating
  8650. * @plane: drm plane
  8651. * @state: new plane state
  8652. *
  8653. * Check current plane state versus the new one to determine whether
  8654. * watermarks need to be recalculated.
  8655. *
  8656. * Returns true or false.
  8657. */
  8658. static bool intel_wm_need_update(struct drm_plane *plane,
  8659. struct drm_plane_state *state)
  8660. {
  8661. struct intel_plane_state *new = to_intel_plane_state(state);
  8662. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8663. /* Update watermarks on tiling or size changes. */
  8664. if (new->base.visible != cur->base.visible)
  8665. return true;
  8666. if (!cur->base.fb || !new->base.fb)
  8667. return false;
  8668. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8669. cur->base.rotation != new->base.rotation ||
  8670. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8671. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8672. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8673. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8674. return true;
  8675. return false;
  8676. }
  8677. static bool needs_scaling(const struct intel_plane_state *state)
  8678. {
  8679. int src_w = drm_rect_width(&state->base.src) >> 16;
  8680. int src_h = drm_rect_height(&state->base.src) >> 16;
  8681. int dst_w = drm_rect_width(&state->base.dst);
  8682. int dst_h = drm_rect_height(&state->base.dst);
  8683. return (src_w != dst_w || src_h != dst_h);
  8684. }
  8685. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8686. struct drm_crtc_state *crtc_state,
  8687. const struct intel_plane_state *old_plane_state,
  8688. struct drm_plane_state *plane_state)
  8689. {
  8690. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8691. struct drm_crtc *crtc = crtc_state->crtc;
  8692. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8693. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8694. struct drm_device *dev = crtc->dev;
  8695. struct drm_i915_private *dev_priv = to_i915(dev);
  8696. bool mode_changed = needs_modeset(crtc_state);
  8697. bool was_crtc_enabled = old_crtc_state->base.active;
  8698. bool is_crtc_enabled = crtc_state->active;
  8699. bool turn_off, turn_on, visible, was_visible;
  8700. struct drm_framebuffer *fb = plane_state->fb;
  8701. int ret;
  8702. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8703. ret = skl_update_scaler_plane(
  8704. to_intel_crtc_state(crtc_state),
  8705. to_intel_plane_state(plane_state));
  8706. if (ret)
  8707. return ret;
  8708. }
  8709. was_visible = old_plane_state->base.visible;
  8710. visible = plane_state->visible;
  8711. if (!was_crtc_enabled && WARN_ON(was_visible))
  8712. was_visible = false;
  8713. /*
  8714. * Visibility is calculated as if the crtc was on, but
  8715. * after scaler setup everything depends on it being off
  8716. * when the crtc isn't active.
  8717. *
  8718. * FIXME this is wrong for watermarks. Watermarks should also
  8719. * be computed as if the pipe would be active. Perhaps move
  8720. * per-plane wm computation to the .check_plane() hook, and
  8721. * only combine the results from all planes in the current place?
  8722. */
  8723. if (!is_crtc_enabled) {
  8724. plane_state->visible = visible = false;
  8725. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8726. }
  8727. if (!was_visible && !visible)
  8728. return 0;
  8729. if (fb != old_plane_state->base.fb)
  8730. pipe_config->fb_changed = true;
  8731. turn_off = was_visible && (!visible || mode_changed);
  8732. turn_on = visible && (!was_visible || mode_changed);
  8733. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8734. intel_crtc->base.base.id, intel_crtc->base.name,
  8735. plane->base.base.id, plane->base.name,
  8736. fb ? fb->base.id : -1);
  8737. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8738. plane->base.base.id, plane->base.name,
  8739. was_visible, visible,
  8740. turn_off, turn_on, mode_changed);
  8741. if (turn_on) {
  8742. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8743. pipe_config->update_wm_pre = true;
  8744. /* must disable cxsr around plane enable/disable */
  8745. if (plane->id != PLANE_CURSOR)
  8746. pipe_config->disable_cxsr = true;
  8747. } else if (turn_off) {
  8748. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8749. pipe_config->update_wm_post = true;
  8750. /* must disable cxsr around plane enable/disable */
  8751. if (plane->id != PLANE_CURSOR)
  8752. pipe_config->disable_cxsr = true;
  8753. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8754. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8755. /* FIXME bollocks */
  8756. pipe_config->update_wm_pre = true;
  8757. pipe_config->update_wm_post = true;
  8758. }
  8759. }
  8760. if (visible || was_visible)
  8761. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8762. /*
  8763. * WaCxSRDisabledForSpriteScaling:ivb
  8764. *
  8765. * cstate->update_wm was already set above, so this flag will
  8766. * take effect when we commit and program watermarks.
  8767. */
  8768. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8769. needs_scaling(to_intel_plane_state(plane_state)) &&
  8770. !needs_scaling(old_plane_state))
  8771. pipe_config->disable_lp_wm = true;
  8772. return 0;
  8773. }
  8774. static bool encoders_cloneable(const struct intel_encoder *a,
  8775. const struct intel_encoder *b)
  8776. {
  8777. /* masks could be asymmetric, so check both ways */
  8778. return a == b || (a->cloneable & (1 << b->type) &&
  8779. b->cloneable & (1 << a->type));
  8780. }
  8781. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8782. struct intel_crtc *crtc,
  8783. struct intel_encoder *encoder)
  8784. {
  8785. struct intel_encoder *source_encoder;
  8786. struct drm_connector *connector;
  8787. struct drm_connector_state *connector_state;
  8788. int i;
  8789. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8790. if (connector_state->crtc != &crtc->base)
  8791. continue;
  8792. source_encoder =
  8793. to_intel_encoder(connector_state->best_encoder);
  8794. if (!encoders_cloneable(encoder, source_encoder))
  8795. return false;
  8796. }
  8797. return true;
  8798. }
  8799. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8800. struct drm_crtc_state *crtc_state)
  8801. {
  8802. struct drm_device *dev = crtc->dev;
  8803. struct drm_i915_private *dev_priv = to_i915(dev);
  8804. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8805. struct intel_crtc_state *pipe_config =
  8806. to_intel_crtc_state(crtc_state);
  8807. struct drm_atomic_state *state = crtc_state->state;
  8808. int ret;
  8809. bool mode_changed = needs_modeset(crtc_state);
  8810. if (mode_changed && !crtc_state->active)
  8811. pipe_config->update_wm_post = true;
  8812. if (mode_changed && crtc_state->enable &&
  8813. dev_priv->display.crtc_compute_clock &&
  8814. !WARN_ON(pipe_config->shared_dpll)) {
  8815. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8816. pipe_config);
  8817. if (ret)
  8818. return ret;
  8819. }
  8820. if (crtc_state->color_mgmt_changed) {
  8821. ret = intel_color_check(crtc, crtc_state);
  8822. if (ret)
  8823. return ret;
  8824. /*
  8825. * Changing color management on Intel hardware is
  8826. * handled as part of planes update.
  8827. */
  8828. crtc_state->planes_changed = true;
  8829. }
  8830. ret = 0;
  8831. if (dev_priv->display.compute_pipe_wm) {
  8832. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8833. if (ret) {
  8834. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8835. return ret;
  8836. }
  8837. }
  8838. if (dev_priv->display.compute_intermediate_wm &&
  8839. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8840. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8841. return 0;
  8842. /*
  8843. * Calculate 'intermediate' watermarks that satisfy both the
  8844. * old state and the new state. We can program these
  8845. * immediately.
  8846. */
  8847. ret = dev_priv->display.compute_intermediate_wm(dev,
  8848. intel_crtc,
  8849. pipe_config);
  8850. if (ret) {
  8851. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8852. return ret;
  8853. }
  8854. } else if (dev_priv->display.compute_intermediate_wm) {
  8855. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8856. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8857. }
  8858. if (INTEL_GEN(dev_priv) >= 9) {
  8859. if (mode_changed)
  8860. ret = skl_update_scaler_crtc(pipe_config);
  8861. if (!ret)
  8862. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8863. pipe_config);
  8864. if (!ret)
  8865. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8866. pipe_config);
  8867. }
  8868. if (HAS_IPS(dev_priv))
  8869. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8870. return ret;
  8871. }
  8872. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8873. .atomic_begin = intel_begin_crtc_commit,
  8874. .atomic_flush = intel_finish_crtc_commit,
  8875. .atomic_check = intel_crtc_atomic_check,
  8876. };
  8877. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8878. {
  8879. struct intel_connector *connector;
  8880. struct drm_connector_list_iter conn_iter;
  8881. drm_connector_list_iter_begin(dev, &conn_iter);
  8882. for_each_intel_connector_iter(connector, &conn_iter) {
  8883. if (connector->base.state->crtc)
  8884. drm_connector_unreference(&connector->base);
  8885. if (connector->base.encoder) {
  8886. connector->base.state->best_encoder =
  8887. connector->base.encoder;
  8888. connector->base.state->crtc =
  8889. connector->base.encoder->crtc;
  8890. drm_connector_reference(&connector->base);
  8891. } else {
  8892. connector->base.state->best_encoder = NULL;
  8893. connector->base.state->crtc = NULL;
  8894. }
  8895. }
  8896. drm_connector_list_iter_end(&conn_iter);
  8897. }
  8898. static void
  8899. connected_sink_compute_bpp(struct intel_connector *connector,
  8900. struct intel_crtc_state *pipe_config)
  8901. {
  8902. const struct drm_display_info *info = &connector->base.display_info;
  8903. int bpp = pipe_config->pipe_bpp;
  8904. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8905. connector->base.base.id,
  8906. connector->base.name);
  8907. /* Don't use an invalid EDID bpc value */
  8908. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8909. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8910. bpp, info->bpc * 3);
  8911. pipe_config->pipe_bpp = info->bpc * 3;
  8912. }
  8913. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8914. if (info->bpc == 0 && bpp > 24) {
  8915. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8916. bpp);
  8917. pipe_config->pipe_bpp = 24;
  8918. }
  8919. }
  8920. static int
  8921. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8922. struct intel_crtc_state *pipe_config)
  8923. {
  8924. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8925. struct drm_atomic_state *state;
  8926. struct drm_connector *connector;
  8927. struct drm_connector_state *connector_state;
  8928. int bpp, i;
  8929. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8930. IS_CHERRYVIEW(dev_priv)))
  8931. bpp = 10*3;
  8932. else if (INTEL_GEN(dev_priv) >= 5)
  8933. bpp = 12*3;
  8934. else
  8935. bpp = 8*3;
  8936. pipe_config->pipe_bpp = bpp;
  8937. state = pipe_config->base.state;
  8938. /* Clamp display bpp to EDID value */
  8939. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8940. if (connector_state->crtc != &crtc->base)
  8941. continue;
  8942. connected_sink_compute_bpp(to_intel_connector(connector),
  8943. pipe_config);
  8944. }
  8945. return bpp;
  8946. }
  8947. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8948. {
  8949. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8950. "type: 0x%x flags: 0x%x\n",
  8951. mode->crtc_clock,
  8952. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8953. mode->crtc_hsync_end, mode->crtc_htotal,
  8954. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8955. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8956. }
  8957. static inline void
  8958. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8959. unsigned int lane_count, struct intel_link_m_n *m_n)
  8960. {
  8961. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8962. id, lane_count,
  8963. m_n->gmch_m, m_n->gmch_n,
  8964. m_n->link_m, m_n->link_n, m_n->tu);
  8965. }
  8966. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8967. static const char * const output_type_str[] = {
  8968. OUTPUT_TYPE(UNUSED),
  8969. OUTPUT_TYPE(ANALOG),
  8970. OUTPUT_TYPE(DVO),
  8971. OUTPUT_TYPE(SDVO),
  8972. OUTPUT_TYPE(LVDS),
  8973. OUTPUT_TYPE(TVOUT),
  8974. OUTPUT_TYPE(HDMI),
  8975. OUTPUT_TYPE(DP),
  8976. OUTPUT_TYPE(EDP),
  8977. OUTPUT_TYPE(DSI),
  8978. OUTPUT_TYPE(DDI),
  8979. OUTPUT_TYPE(DP_MST),
  8980. };
  8981. #undef OUTPUT_TYPE
  8982. static void snprintf_output_types(char *buf, size_t len,
  8983. unsigned int output_types)
  8984. {
  8985. char *str = buf;
  8986. int i;
  8987. str[0] = '\0';
  8988. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8989. int r;
  8990. if ((output_types & BIT(i)) == 0)
  8991. continue;
  8992. r = snprintf(str, len, "%s%s",
  8993. str != buf ? "," : "", output_type_str[i]);
  8994. if (r >= len)
  8995. break;
  8996. str += r;
  8997. len -= r;
  8998. output_types &= ~BIT(i);
  8999. }
  9000. WARN_ON_ONCE(output_types != 0);
  9001. }
  9002. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9003. struct intel_crtc_state *pipe_config,
  9004. const char *context)
  9005. {
  9006. struct drm_device *dev = crtc->base.dev;
  9007. struct drm_i915_private *dev_priv = to_i915(dev);
  9008. struct drm_plane *plane;
  9009. struct intel_plane *intel_plane;
  9010. struct intel_plane_state *state;
  9011. struct drm_framebuffer *fb;
  9012. char buf[64];
  9013. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9014. crtc->base.base.id, crtc->base.name, context);
  9015. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  9016. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  9017. buf, pipe_config->output_types);
  9018. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9019. transcoder_name(pipe_config->cpu_transcoder),
  9020. pipe_config->pipe_bpp, pipe_config->dither);
  9021. if (pipe_config->has_pch_encoder)
  9022. intel_dump_m_n_config(pipe_config, "fdi",
  9023. pipe_config->fdi_lanes,
  9024. &pipe_config->fdi_m_n);
  9025. if (pipe_config->ycbcr420)
  9026. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  9027. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9028. intel_dump_m_n_config(pipe_config, "dp m_n",
  9029. pipe_config->lane_count, &pipe_config->dp_m_n);
  9030. if (pipe_config->has_drrs)
  9031. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9032. pipe_config->lane_count,
  9033. &pipe_config->dp_m2_n2);
  9034. }
  9035. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9036. pipe_config->has_audio, pipe_config->has_infoframe);
  9037. DRM_DEBUG_KMS("requested mode:\n");
  9038. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9039. DRM_DEBUG_KMS("adjusted mode:\n");
  9040. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9041. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9042. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9043. pipe_config->port_clock,
  9044. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9045. pipe_config->pixel_rate);
  9046. if (INTEL_GEN(dev_priv) >= 9)
  9047. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9048. crtc->num_scalers,
  9049. pipe_config->scaler_state.scaler_users,
  9050. pipe_config->scaler_state.scaler_id);
  9051. if (HAS_GMCH_DISPLAY(dev_priv))
  9052. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9053. pipe_config->gmch_pfit.control,
  9054. pipe_config->gmch_pfit.pgm_ratios,
  9055. pipe_config->gmch_pfit.lvds_border_bits);
  9056. else
  9057. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9058. pipe_config->pch_pfit.pos,
  9059. pipe_config->pch_pfit.size,
  9060. enableddisabled(pipe_config->pch_pfit.enabled));
  9061. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9062. pipe_config->ips_enabled, pipe_config->double_wide);
  9063. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9064. DRM_DEBUG_KMS("planes on this crtc\n");
  9065. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9066. struct drm_format_name_buf format_name;
  9067. intel_plane = to_intel_plane(plane);
  9068. if (intel_plane->pipe != crtc->pipe)
  9069. continue;
  9070. state = to_intel_plane_state(plane->state);
  9071. fb = state->base.fb;
  9072. if (!fb) {
  9073. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9074. plane->base.id, plane->name, state->scaler_id);
  9075. continue;
  9076. }
  9077. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9078. plane->base.id, plane->name,
  9079. fb->base.id, fb->width, fb->height,
  9080. drm_get_format_name(fb->format->format, &format_name));
  9081. if (INTEL_GEN(dev_priv) >= 9)
  9082. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9083. state->scaler_id,
  9084. state->base.src.x1 >> 16,
  9085. state->base.src.y1 >> 16,
  9086. drm_rect_width(&state->base.src) >> 16,
  9087. drm_rect_height(&state->base.src) >> 16,
  9088. state->base.dst.x1, state->base.dst.y1,
  9089. drm_rect_width(&state->base.dst),
  9090. drm_rect_height(&state->base.dst));
  9091. }
  9092. }
  9093. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9094. {
  9095. struct drm_device *dev = state->dev;
  9096. struct drm_connector *connector;
  9097. struct drm_connector_list_iter conn_iter;
  9098. unsigned int used_ports = 0;
  9099. unsigned int used_mst_ports = 0;
  9100. bool ret = true;
  9101. /*
  9102. * Walk the connector list instead of the encoder
  9103. * list to detect the problem on ddi platforms
  9104. * where there's just one encoder per digital port.
  9105. */
  9106. drm_connector_list_iter_begin(dev, &conn_iter);
  9107. drm_for_each_connector_iter(connector, &conn_iter) {
  9108. struct drm_connector_state *connector_state;
  9109. struct intel_encoder *encoder;
  9110. connector_state = drm_atomic_get_new_connector_state(state, connector);
  9111. if (!connector_state)
  9112. connector_state = connector->state;
  9113. if (!connector_state->best_encoder)
  9114. continue;
  9115. encoder = to_intel_encoder(connector_state->best_encoder);
  9116. WARN_ON(!connector_state->crtc);
  9117. switch (encoder->type) {
  9118. unsigned int port_mask;
  9119. case INTEL_OUTPUT_DDI:
  9120. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9121. break;
  9122. case INTEL_OUTPUT_DP:
  9123. case INTEL_OUTPUT_HDMI:
  9124. case INTEL_OUTPUT_EDP:
  9125. port_mask = 1 << encoder->port;
  9126. /* the same port mustn't appear more than once */
  9127. if (used_ports & port_mask)
  9128. ret = false;
  9129. used_ports |= port_mask;
  9130. break;
  9131. case INTEL_OUTPUT_DP_MST:
  9132. used_mst_ports |=
  9133. 1 << encoder->port;
  9134. break;
  9135. default:
  9136. break;
  9137. }
  9138. }
  9139. drm_connector_list_iter_end(&conn_iter);
  9140. /* can't mix MST and SST/HDMI on the same port */
  9141. if (used_ports & used_mst_ports)
  9142. return false;
  9143. return ret;
  9144. }
  9145. static void
  9146. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9147. {
  9148. struct drm_i915_private *dev_priv =
  9149. to_i915(crtc_state->base.crtc->dev);
  9150. struct intel_crtc_scaler_state scaler_state;
  9151. struct intel_dpll_hw_state dpll_hw_state;
  9152. struct intel_shared_dpll *shared_dpll;
  9153. struct intel_crtc_wm_state wm_state;
  9154. bool force_thru, ips_force_disable;
  9155. /* FIXME: before the switch to atomic started, a new pipe_config was
  9156. * kzalloc'd. Code that depends on any field being zero should be
  9157. * fixed, so that the crtc_state can be safely duplicated. For now,
  9158. * only fields that are know to not cause problems are preserved. */
  9159. scaler_state = crtc_state->scaler_state;
  9160. shared_dpll = crtc_state->shared_dpll;
  9161. dpll_hw_state = crtc_state->dpll_hw_state;
  9162. force_thru = crtc_state->pch_pfit.force_thru;
  9163. ips_force_disable = crtc_state->ips_force_disable;
  9164. if (IS_G4X(dev_priv) ||
  9165. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9166. wm_state = crtc_state->wm;
  9167. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9168. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9169. memset(&crtc_state->base + 1, 0,
  9170. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9171. crtc_state->scaler_state = scaler_state;
  9172. crtc_state->shared_dpll = shared_dpll;
  9173. crtc_state->dpll_hw_state = dpll_hw_state;
  9174. crtc_state->pch_pfit.force_thru = force_thru;
  9175. crtc_state->ips_force_disable = ips_force_disable;
  9176. if (IS_G4X(dev_priv) ||
  9177. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9178. crtc_state->wm = wm_state;
  9179. }
  9180. static int
  9181. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9182. struct intel_crtc_state *pipe_config)
  9183. {
  9184. struct drm_atomic_state *state = pipe_config->base.state;
  9185. struct intel_encoder *encoder;
  9186. struct drm_connector *connector;
  9187. struct drm_connector_state *connector_state;
  9188. int base_bpp, ret = -EINVAL;
  9189. int i;
  9190. bool retry = true;
  9191. clear_intel_crtc_state(pipe_config);
  9192. pipe_config->cpu_transcoder =
  9193. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9194. /*
  9195. * Sanitize sync polarity flags based on requested ones. If neither
  9196. * positive or negative polarity is requested, treat this as meaning
  9197. * negative polarity.
  9198. */
  9199. if (!(pipe_config->base.adjusted_mode.flags &
  9200. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9201. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9202. if (!(pipe_config->base.adjusted_mode.flags &
  9203. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9204. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9205. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9206. pipe_config);
  9207. if (base_bpp < 0)
  9208. goto fail;
  9209. /*
  9210. * Determine the real pipe dimensions. Note that stereo modes can
  9211. * increase the actual pipe size due to the frame doubling and
  9212. * insertion of additional space for blanks between the frame. This
  9213. * is stored in the crtc timings. We use the requested mode to do this
  9214. * computation to clearly distinguish it from the adjusted mode, which
  9215. * can be changed by the connectors in the below retry loop.
  9216. */
  9217. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9218. &pipe_config->pipe_src_w,
  9219. &pipe_config->pipe_src_h);
  9220. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9221. if (connector_state->crtc != crtc)
  9222. continue;
  9223. encoder = to_intel_encoder(connector_state->best_encoder);
  9224. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9225. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9226. goto fail;
  9227. }
  9228. /*
  9229. * Determine output_types before calling the .compute_config()
  9230. * hooks so that the hooks can use this information safely.
  9231. */
  9232. if (encoder->compute_output_type)
  9233. pipe_config->output_types |=
  9234. BIT(encoder->compute_output_type(encoder, pipe_config,
  9235. connector_state));
  9236. else
  9237. pipe_config->output_types |= BIT(encoder->type);
  9238. }
  9239. encoder_retry:
  9240. /* Ensure the port clock defaults are reset when retrying. */
  9241. pipe_config->port_clock = 0;
  9242. pipe_config->pixel_multiplier = 1;
  9243. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9244. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9245. CRTC_STEREO_DOUBLE);
  9246. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9247. * adjust it according to limitations or connector properties, and also
  9248. * a chance to reject the mode entirely.
  9249. */
  9250. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9251. if (connector_state->crtc != crtc)
  9252. continue;
  9253. encoder = to_intel_encoder(connector_state->best_encoder);
  9254. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9255. DRM_DEBUG_KMS("Encoder config failure\n");
  9256. goto fail;
  9257. }
  9258. }
  9259. /* Set default port clock if not overwritten by the encoder. Needs to be
  9260. * done afterwards in case the encoder adjusts the mode. */
  9261. if (!pipe_config->port_clock)
  9262. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9263. * pipe_config->pixel_multiplier;
  9264. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9265. if (ret < 0) {
  9266. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9267. goto fail;
  9268. }
  9269. if (ret == RETRY) {
  9270. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9271. ret = -EINVAL;
  9272. goto fail;
  9273. }
  9274. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9275. retry = false;
  9276. goto encoder_retry;
  9277. }
  9278. /* Dithering seems to not pass-through bits correctly when it should, so
  9279. * only enable it on 6bpc panels and when its not a compliance
  9280. * test requesting 6bpc video pattern.
  9281. */
  9282. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9283. !pipe_config->dither_force_disable;
  9284. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9285. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9286. fail:
  9287. return ret;
  9288. }
  9289. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9290. {
  9291. int diff;
  9292. if (clock1 == clock2)
  9293. return true;
  9294. if (!clock1 || !clock2)
  9295. return false;
  9296. diff = abs(clock1 - clock2);
  9297. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9298. return true;
  9299. return false;
  9300. }
  9301. static bool
  9302. intel_compare_m_n(unsigned int m, unsigned int n,
  9303. unsigned int m2, unsigned int n2,
  9304. bool exact)
  9305. {
  9306. if (m == m2 && n == n2)
  9307. return true;
  9308. if (exact || !m || !n || !m2 || !n2)
  9309. return false;
  9310. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9311. if (n > n2) {
  9312. while (n > n2) {
  9313. m2 <<= 1;
  9314. n2 <<= 1;
  9315. }
  9316. } else if (n < n2) {
  9317. while (n < n2) {
  9318. m <<= 1;
  9319. n <<= 1;
  9320. }
  9321. }
  9322. if (n != n2)
  9323. return false;
  9324. return intel_fuzzy_clock_check(m, m2);
  9325. }
  9326. static bool
  9327. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9328. struct intel_link_m_n *m2_n2,
  9329. bool adjust)
  9330. {
  9331. if (m_n->tu == m2_n2->tu &&
  9332. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9333. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9334. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9335. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9336. if (adjust)
  9337. *m2_n2 = *m_n;
  9338. return true;
  9339. }
  9340. return false;
  9341. }
  9342. static void __printf(3, 4)
  9343. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9344. {
  9345. struct va_format vaf;
  9346. va_list args;
  9347. va_start(args, format);
  9348. vaf.fmt = format;
  9349. vaf.va = &args;
  9350. if (adjust)
  9351. drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
  9352. else
  9353. drm_err("mismatch in %s %pV", name, &vaf);
  9354. va_end(args);
  9355. }
  9356. static bool
  9357. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9358. struct intel_crtc_state *current_config,
  9359. struct intel_crtc_state *pipe_config,
  9360. bool adjust)
  9361. {
  9362. bool ret = true;
  9363. bool fixup_inherited = adjust &&
  9364. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9365. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9366. #define PIPE_CONF_CHECK_X(name) do { \
  9367. if (current_config->name != pipe_config->name) { \
  9368. pipe_config_err(adjust, __stringify(name), \
  9369. "(expected 0x%08x, found 0x%08x)\n", \
  9370. current_config->name, \
  9371. pipe_config->name); \
  9372. ret = false; \
  9373. } \
  9374. } while (0)
  9375. #define PIPE_CONF_CHECK_I(name) do { \
  9376. if (current_config->name != pipe_config->name) { \
  9377. pipe_config_err(adjust, __stringify(name), \
  9378. "(expected %i, found %i)\n", \
  9379. current_config->name, \
  9380. pipe_config->name); \
  9381. ret = false; \
  9382. } \
  9383. } while (0)
  9384. #define PIPE_CONF_CHECK_BOOL(name) do { \
  9385. if (current_config->name != pipe_config->name) { \
  9386. pipe_config_err(adjust, __stringify(name), \
  9387. "(expected %s, found %s)\n", \
  9388. yesno(current_config->name), \
  9389. yesno(pipe_config->name)); \
  9390. ret = false; \
  9391. } \
  9392. } while (0)
  9393. /*
  9394. * Checks state where we only read out the enabling, but not the entire
  9395. * state itself (like full infoframes or ELD for audio). These states
  9396. * require a full modeset on bootup to fix up.
  9397. */
  9398. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
  9399. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9400. PIPE_CONF_CHECK_BOOL(name); \
  9401. } else { \
  9402. pipe_config_err(adjust, __stringify(name), \
  9403. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9404. yesno(current_config->name), \
  9405. yesno(pipe_config->name)); \
  9406. ret = false; \
  9407. } \
  9408. } while (0)
  9409. #define PIPE_CONF_CHECK_P(name) do { \
  9410. if (current_config->name != pipe_config->name) { \
  9411. pipe_config_err(adjust, __stringify(name), \
  9412. "(expected %p, found %p)\n", \
  9413. current_config->name, \
  9414. pipe_config->name); \
  9415. ret = false; \
  9416. } \
  9417. } while (0)
  9418. #define PIPE_CONF_CHECK_M_N(name) do { \
  9419. if (!intel_compare_link_m_n(&current_config->name, \
  9420. &pipe_config->name,\
  9421. adjust)) { \
  9422. pipe_config_err(adjust, __stringify(name), \
  9423. "(expected tu %i gmch %i/%i link %i/%i, " \
  9424. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9425. current_config->name.tu, \
  9426. current_config->name.gmch_m, \
  9427. current_config->name.gmch_n, \
  9428. current_config->name.link_m, \
  9429. current_config->name.link_n, \
  9430. pipe_config->name.tu, \
  9431. pipe_config->name.gmch_m, \
  9432. pipe_config->name.gmch_n, \
  9433. pipe_config->name.link_m, \
  9434. pipe_config->name.link_n); \
  9435. ret = false; \
  9436. } \
  9437. } while (0)
  9438. /* This is required for BDW+ where there is only one set of registers for
  9439. * switching between high and low RR.
  9440. * This macro can be used whenever a comparison has to be made between one
  9441. * hw state and multiple sw state variables.
  9442. */
  9443. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
  9444. if (!intel_compare_link_m_n(&current_config->name, \
  9445. &pipe_config->name, adjust) && \
  9446. !intel_compare_link_m_n(&current_config->alt_name, \
  9447. &pipe_config->name, adjust)) { \
  9448. pipe_config_err(adjust, __stringify(name), \
  9449. "(expected tu %i gmch %i/%i link %i/%i, " \
  9450. "or tu %i gmch %i/%i link %i/%i, " \
  9451. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9452. current_config->name.tu, \
  9453. current_config->name.gmch_m, \
  9454. current_config->name.gmch_n, \
  9455. current_config->name.link_m, \
  9456. current_config->name.link_n, \
  9457. current_config->alt_name.tu, \
  9458. current_config->alt_name.gmch_m, \
  9459. current_config->alt_name.gmch_n, \
  9460. current_config->alt_name.link_m, \
  9461. current_config->alt_name.link_n, \
  9462. pipe_config->name.tu, \
  9463. pipe_config->name.gmch_m, \
  9464. pipe_config->name.gmch_n, \
  9465. pipe_config->name.link_m, \
  9466. pipe_config->name.link_n); \
  9467. ret = false; \
  9468. } \
  9469. } while (0)
  9470. #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
  9471. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9472. pipe_config_err(adjust, __stringify(name), \
  9473. "(%x) (expected %i, found %i)\n", \
  9474. (mask), \
  9475. current_config->name & (mask), \
  9476. pipe_config->name & (mask)); \
  9477. ret = false; \
  9478. } \
  9479. } while (0)
  9480. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
  9481. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9482. pipe_config_err(adjust, __stringify(name), \
  9483. "(expected %i, found %i)\n", \
  9484. current_config->name, \
  9485. pipe_config->name); \
  9486. ret = false; \
  9487. } \
  9488. } while (0)
  9489. #define PIPE_CONF_QUIRK(quirk) \
  9490. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9491. PIPE_CONF_CHECK_I(cpu_transcoder);
  9492. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9493. PIPE_CONF_CHECK_I(fdi_lanes);
  9494. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9495. PIPE_CONF_CHECK_I(lane_count);
  9496. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9497. if (INTEL_GEN(dev_priv) < 8) {
  9498. PIPE_CONF_CHECK_M_N(dp_m_n);
  9499. if (current_config->has_drrs)
  9500. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9501. } else
  9502. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9503. PIPE_CONF_CHECK_X(output_types);
  9504. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9505. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9506. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9507. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9508. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9509. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9510. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9511. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9512. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9513. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9514. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9515. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9516. PIPE_CONF_CHECK_I(pixel_multiplier);
  9517. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9518. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9519. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9520. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9521. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9522. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9523. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9524. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9525. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9526. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9527. DRM_MODE_FLAG_INTERLACE);
  9528. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9529. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9530. DRM_MODE_FLAG_PHSYNC);
  9531. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9532. DRM_MODE_FLAG_NHSYNC);
  9533. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9534. DRM_MODE_FLAG_PVSYNC);
  9535. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9536. DRM_MODE_FLAG_NVSYNC);
  9537. }
  9538. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9539. /* pfit ratios are autocomputed by the hw on gen4+ */
  9540. if (INTEL_GEN(dev_priv) < 4)
  9541. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9542. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9543. if (!adjust) {
  9544. PIPE_CONF_CHECK_I(pipe_src_w);
  9545. PIPE_CONF_CHECK_I(pipe_src_h);
  9546. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9547. if (current_config->pch_pfit.enabled) {
  9548. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9549. PIPE_CONF_CHECK_X(pch_pfit.size);
  9550. }
  9551. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9552. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9553. }
  9554. PIPE_CONF_CHECK_BOOL(double_wide);
  9555. PIPE_CONF_CHECK_P(shared_dpll);
  9556. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9557. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9558. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9559. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9560. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9561. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9562. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9563. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9564. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9565. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9566. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9567. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9568. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9569. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9570. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9571. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9572. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9573. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9574. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9575. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9576. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9577. PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
  9578. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
  9579. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
  9580. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
  9581. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
  9582. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
  9583. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
  9584. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
  9585. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
  9586. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
  9587. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9588. PIPE_CONF_CHECK_X(dsi_pll.div);
  9589. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9590. PIPE_CONF_CHECK_I(pipe_bpp);
  9591. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9592. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9593. PIPE_CONF_CHECK_I(min_voltage_level);
  9594. #undef PIPE_CONF_CHECK_X
  9595. #undef PIPE_CONF_CHECK_I
  9596. #undef PIPE_CONF_CHECK_BOOL
  9597. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9598. #undef PIPE_CONF_CHECK_P
  9599. #undef PIPE_CONF_CHECK_FLAGS
  9600. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9601. #undef PIPE_CONF_QUIRK
  9602. return ret;
  9603. }
  9604. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9605. const struct intel_crtc_state *pipe_config)
  9606. {
  9607. if (pipe_config->has_pch_encoder) {
  9608. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9609. &pipe_config->fdi_m_n);
  9610. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9611. /*
  9612. * FDI already provided one idea for the dotclock.
  9613. * Yell if the encoder disagrees.
  9614. */
  9615. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9616. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9617. fdi_dotclock, dotclock);
  9618. }
  9619. }
  9620. static void verify_wm_state(struct drm_crtc *crtc,
  9621. struct drm_crtc_state *new_state)
  9622. {
  9623. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9624. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9625. struct skl_pipe_wm hw_wm, *sw_wm;
  9626. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9627. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9628. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9629. const enum pipe pipe = intel_crtc->pipe;
  9630. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9631. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9632. return;
  9633. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9634. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9635. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9636. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9637. if (INTEL_GEN(dev_priv) >= 11)
  9638. if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
  9639. DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
  9640. sw_ddb->enabled_slices,
  9641. hw_ddb.enabled_slices);
  9642. /* planes */
  9643. for_each_universal_plane(dev_priv, pipe, plane) {
  9644. hw_plane_wm = &hw_wm.planes[plane];
  9645. sw_plane_wm = &sw_wm->planes[plane];
  9646. /* Watermarks */
  9647. for (level = 0; level <= max_level; level++) {
  9648. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9649. &sw_plane_wm->wm[level]))
  9650. continue;
  9651. 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",
  9652. pipe_name(pipe), plane + 1, level,
  9653. sw_plane_wm->wm[level].plane_en,
  9654. sw_plane_wm->wm[level].plane_res_b,
  9655. sw_plane_wm->wm[level].plane_res_l,
  9656. hw_plane_wm->wm[level].plane_en,
  9657. hw_plane_wm->wm[level].plane_res_b,
  9658. hw_plane_wm->wm[level].plane_res_l);
  9659. }
  9660. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9661. &sw_plane_wm->trans_wm)) {
  9662. 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",
  9663. pipe_name(pipe), plane + 1,
  9664. sw_plane_wm->trans_wm.plane_en,
  9665. sw_plane_wm->trans_wm.plane_res_b,
  9666. sw_plane_wm->trans_wm.plane_res_l,
  9667. hw_plane_wm->trans_wm.plane_en,
  9668. hw_plane_wm->trans_wm.plane_res_b,
  9669. hw_plane_wm->trans_wm.plane_res_l);
  9670. }
  9671. /* DDB */
  9672. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9673. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9674. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9675. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9676. pipe_name(pipe), plane + 1,
  9677. sw_ddb_entry->start, sw_ddb_entry->end,
  9678. hw_ddb_entry->start, hw_ddb_entry->end);
  9679. }
  9680. }
  9681. /*
  9682. * cursor
  9683. * If the cursor plane isn't active, we may not have updated it's ddb
  9684. * allocation. In that case since the ddb allocation will be updated
  9685. * once the plane becomes visible, we can skip this check
  9686. */
  9687. if (1) {
  9688. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9689. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9690. /* Watermarks */
  9691. for (level = 0; level <= max_level; level++) {
  9692. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9693. &sw_plane_wm->wm[level]))
  9694. continue;
  9695. 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",
  9696. pipe_name(pipe), level,
  9697. sw_plane_wm->wm[level].plane_en,
  9698. sw_plane_wm->wm[level].plane_res_b,
  9699. sw_plane_wm->wm[level].plane_res_l,
  9700. hw_plane_wm->wm[level].plane_en,
  9701. hw_plane_wm->wm[level].plane_res_b,
  9702. hw_plane_wm->wm[level].plane_res_l);
  9703. }
  9704. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9705. &sw_plane_wm->trans_wm)) {
  9706. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9707. pipe_name(pipe),
  9708. sw_plane_wm->trans_wm.plane_en,
  9709. sw_plane_wm->trans_wm.plane_res_b,
  9710. sw_plane_wm->trans_wm.plane_res_l,
  9711. hw_plane_wm->trans_wm.plane_en,
  9712. hw_plane_wm->trans_wm.plane_res_b,
  9713. hw_plane_wm->trans_wm.plane_res_l);
  9714. }
  9715. /* DDB */
  9716. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9717. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9718. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9719. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9720. pipe_name(pipe),
  9721. sw_ddb_entry->start, sw_ddb_entry->end,
  9722. hw_ddb_entry->start, hw_ddb_entry->end);
  9723. }
  9724. }
  9725. }
  9726. static void
  9727. verify_connector_state(struct drm_device *dev,
  9728. struct drm_atomic_state *state,
  9729. struct drm_crtc *crtc)
  9730. {
  9731. struct drm_connector *connector;
  9732. struct drm_connector_state *new_conn_state;
  9733. int i;
  9734. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9735. struct drm_encoder *encoder = connector->encoder;
  9736. struct drm_crtc_state *crtc_state = NULL;
  9737. if (new_conn_state->crtc != crtc)
  9738. continue;
  9739. if (crtc)
  9740. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9741. intel_connector_verify_state(crtc_state, new_conn_state);
  9742. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9743. "connector's atomic encoder doesn't match legacy encoder\n");
  9744. }
  9745. }
  9746. static void
  9747. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9748. {
  9749. struct intel_encoder *encoder;
  9750. struct drm_connector *connector;
  9751. struct drm_connector_state *old_conn_state, *new_conn_state;
  9752. int i;
  9753. for_each_intel_encoder(dev, encoder) {
  9754. bool enabled = false, found = false;
  9755. enum pipe pipe;
  9756. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9757. encoder->base.base.id,
  9758. encoder->base.name);
  9759. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9760. new_conn_state, i) {
  9761. if (old_conn_state->best_encoder == &encoder->base)
  9762. found = true;
  9763. if (new_conn_state->best_encoder != &encoder->base)
  9764. continue;
  9765. found = enabled = true;
  9766. I915_STATE_WARN(new_conn_state->crtc !=
  9767. encoder->base.crtc,
  9768. "connector's crtc doesn't match encoder crtc\n");
  9769. }
  9770. if (!found)
  9771. continue;
  9772. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9773. "encoder's enabled state mismatch "
  9774. "(expected %i, found %i)\n",
  9775. !!encoder->base.crtc, enabled);
  9776. if (!encoder->base.crtc) {
  9777. bool active;
  9778. active = encoder->get_hw_state(encoder, &pipe);
  9779. I915_STATE_WARN(active,
  9780. "encoder detached but still enabled on pipe %c.\n",
  9781. pipe_name(pipe));
  9782. }
  9783. }
  9784. }
  9785. static void
  9786. verify_crtc_state(struct drm_crtc *crtc,
  9787. struct drm_crtc_state *old_crtc_state,
  9788. struct drm_crtc_state *new_crtc_state)
  9789. {
  9790. struct drm_device *dev = crtc->dev;
  9791. struct drm_i915_private *dev_priv = to_i915(dev);
  9792. struct intel_encoder *encoder;
  9793. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9794. struct intel_crtc_state *pipe_config, *sw_config;
  9795. struct drm_atomic_state *old_state;
  9796. bool active;
  9797. old_state = old_crtc_state->state;
  9798. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9799. pipe_config = to_intel_crtc_state(old_crtc_state);
  9800. memset(pipe_config, 0, sizeof(*pipe_config));
  9801. pipe_config->base.crtc = crtc;
  9802. pipe_config->base.state = old_state;
  9803. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9804. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9805. /* we keep both pipes enabled on 830 */
  9806. if (IS_I830(dev_priv))
  9807. active = new_crtc_state->active;
  9808. I915_STATE_WARN(new_crtc_state->active != active,
  9809. "crtc active state doesn't match with hw state "
  9810. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9811. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9812. "transitional active state does not match atomic hw state "
  9813. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9814. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9815. enum pipe pipe;
  9816. active = encoder->get_hw_state(encoder, &pipe);
  9817. I915_STATE_WARN(active != new_crtc_state->active,
  9818. "[ENCODER:%i] active %i with crtc active %i\n",
  9819. encoder->base.base.id, active, new_crtc_state->active);
  9820. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9821. "Encoder connected to wrong pipe %c\n",
  9822. pipe_name(pipe));
  9823. if (active)
  9824. encoder->get_config(encoder, pipe_config);
  9825. }
  9826. intel_crtc_compute_pixel_rate(pipe_config);
  9827. if (!new_crtc_state->active)
  9828. return;
  9829. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9830. sw_config = to_intel_crtc_state(new_crtc_state);
  9831. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9832. pipe_config, false)) {
  9833. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9834. intel_dump_pipe_config(intel_crtc, pipe_config,
  9835. "[hw state]");
  9836. intel_dump_pipe_config(intel_crtc, sw_config,
  9837. "[sw state]");
  9838. }
  9839. }
  9840. static void
  9841. intel_verify_planes(struct intel_atomic_state *state)
  9842. {
  9843. struct intel_plane *plane;
  9844. const struct intel_plane_state *plane_state;
  9845. int i;
  9846. for_each_new_intel_plane_in_state(state, plane,
  9847. plane_state, i)
  9848. assert_plane(plane, plane_state->base.visible);
  9849. }
  9850. static void
  9851. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9852. struct intel_shared_dpll *pll,
  9853. struct drm_crtc *crtc,
  9854. struct drm_crtc_state *new_state)
  9855. {
  9856. struct intel_dpll_hw_state dpll_hw_state;
  9857. unsigned crtc_mask;
  9858. bool active;
  9859. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9860. DRM_DEBUG_KMS("%s\n", pll->info->name);
  9861. active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9862. if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
  9863. I915_STATE_WARN(!pll->on && pll->active_mask,
  9864. "pll in active use but not on in sw tracking\n");
  9865. I915_STATE_WARN(pll->on && !pll->active_mask,
  9866. "pll is on but not used by any active crtc\n");
  9867. I915_STATE_WARN(pll->on != active,
  9868. "pll on state mismatch (expected %i, found %i)\n",
  9869. pll->on, active);
  9870. }
  9871. if (!crtc) {
  9872. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9873. "more active pll users than references: %x vs %x\n",
  9874. pll->active_mask, pll->state.crtc_mask);
  9875. return;
  9876. }
  9877. crtc_mask = 1 << drm_crtc_index(crtc);
  9878. if (new_state->active)
  9879. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9880. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9881. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9882. else
  9883. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9884. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9885. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9886. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9887. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9888. crtc_mask, pll->state.crtc_mask);
  9889. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9890. &dpll_hw_state,
  9891. sizeof(dpll_hw_state)),
  9892. "pll hw state mismatch\n");
  9893. }
  9894. static void
  9895. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9896. struct drm_crtc_state *old_crtc_state,
  9897. struct drm_crtc_state *new_crtc_state)
  9898. {
  9899. struct drm_i915_private *dev_priv = to_i915(dev);
  9900. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9901. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9902. if (new_state->shared_dpll)
  9903. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9904. if (old_state->shared_dpll &&
  9905. old_state->shared_dpll != new_state->shared_dpll) {
  9906. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9907. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9908. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9909. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9910. pipe_name(drm_crtc_index(crtc)));
  9911. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9912. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9913. pipe_name(drm_crtc_index(crtc)));
  9914. }
  9915. }
  9916. static void
  9917. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9918. struct drm_atomic_state *state,
  9919. struct drm_crtc_state *old_state,
  9920. struct drm_crtc_state *new_state)
  9921. {
  9922. if (!needs_modeset(new_state) &&
  9923. !to_intel_crtc_state(new_state)->update_pipe)
  9924. return;
  9925. verify_wm_state(crtc, new_state);
  9926. verify_connector_state(crtc->dev, state, crtc);
  9927. verify_crtc_state(crtc, old_state, new_state);
  9928. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9929. }
  9930. static void
  9931. verify_disabled_dpll_state(struct drm_device *dev)
  9932. {
  9933. struct drm_i915_private *dev_priv = to_i915(dev);
  9934. int i;
  9935. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9936. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9937. }
  9938. static void
  9939. intel_modeset_verify_disabled(struct drm_device *dev,
  9940. struct drm_atomic_state *state)
  9941. {
  9942. verify_encoder_state(dev, state);
  9943. verify_connector_state(dev, state, NULL);
  9944. verify_disabled_dpll_state(dev);
  9945. }
  9946. static void update_scanline_offset(struct intel_crtc *crtc)
  9947. {
  9948. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9949. /*
  9950. * The scanline counter increments at the leading edge of hsync.
  9951. *
  9952. * On most platforms it starts counting from vtotal-1 on the
  9953. * first active line. That means the scanline counter value is
  9954. * always one less than what we would expect. Ie. just after
  9955. * start of vblank, which also occurs at start of hsync (on the
  9956. * last active line), the scanline counter will read vblank_start-1.
  9957. *
  9958. * On gen2 the scanline counter starts counting from 1 instead
  9959. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9960. * to keep the value positive), instead of adding one.
  9961. *
  9962. * On HSW+ the behaviour of the scanline counter depends on the output
  9963. * type. For DP ports it behaves like most other platforms, but on HDMI
  9964. * there's an extra 1 line difference. So we need to add two instead of
  9965. * one to the value.
  9966. *
  9967. * On VLV/CHV DSI the scanline counter would appear to increment
  9968. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9969. * that means we can't tell whether we're in vblank or not while
  9970. * we're on that particular line. We must still set scanline_offset
  9971. * to 1 so that the vblank timestamps come out correct when we query
  9972. * the scanline counter from within the vblank interrupt handler.
  9973. * However if queried just before the start of vblank we'll get an
  9974. * answer that's slightly in the future.
  9975. */
  9976. if (IS_GEN2(dev_priv)) {
  9977. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9978. int vtotal;
  9979. vtotal = adjusted_mode->crtc_vtotal;
  9980. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9981. vtotal /= 2;
  9982. crtc->scanline_offset = vtotal - 1;
  9983. } else if (HAS_DDI(dev_priv) &&
  9984. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9985. crtc->scanline_offset = 2;
  9986. } else
  9987. crtc->scanline_offset = 1;
  9988. }
  9989. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9990. {
  9991. struct drm_device *dev = state->dev;
  9992. struct drm_i915_private *dev_priv = to_i915(dev);
  9993. struct drm_crtc *crtc;
  9994. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9995. int i;
  9996. if (!dev_priv->display.crtc_compute_clock)
  9997. return;
  9998. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9999. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10000. struct intel_shared_dpll *old_dpll =
  10001. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10002. if (!needs_modeset(new_crtc_state))
  10003. continue;
  10004. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10005. if (!old_dpll)
  10006. continue;
  10007. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10008. }
  10009. }
  10010. /*
  10011. * This implements the workaround described in the "notes" section of the mode
  10012. * set sequence documentation. When going from no pipes or single pipe to
  10013. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10014. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10015. */
  10016. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10017. {
  10018. struct drm_crtc_state *crtc_state;
  10019. struct intel_crtc *intel_crtc;
  10020. struct drm_crtc *crtc;
  10021. struct intel_crtc_state *first_crtc_state = NULL;
  10022. struct intel_crtc_state *other_crtc_state = NULL;
  10023. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10024. int i;
  10025. /* look at all crtc's that are going to be enabled in during modeset */
  10026. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10027. intel_crtc = to_intel_crtc(crtc);
  10028. if (!crtc_state->active || !needs_modeset(crtc_state))
  10029. continue;
  10030. if (first_crtc_state) {
  10031. other_crtc_state = to_intel_crtc_state(crtc_state);
  10032. break;
  10033. } else {
  10034. first_crtc_state = to_intel_crtc_state(crtc_state);
  10035. first_pipe = intel_crtc->pipe;
  10036. }
  10037. }
  10038. /* No workaround needed? */
  10039. if (!first_crtc_state)
  10040. return 0;
  10041. /* w/a possibly needed, check how many crtc's are already enabled. */
  10042. for_each_intel_crtc(state->dev, intel_crtc) {
  10043. struct intel_crtc_state *pipe_config;
  10044. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10045. if (IS_ERR(pipe_config))
  10046. return PTR_ERR(pipe_config);
  10047. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10048. if (!pipe_config->base.active ||
  10049. needs_modeset(&pipe_config->base))
  10050. continue;
  10051. /* 2 or more enabled crtcs means no need for w/a */
  10052. if (enabled_pipe != INVALID_PIPE)
  10053. return 0;
  10054. enabled_pipe = intel_crtc->pipe;
  10055. }
  10056. if (enabled_pipe != INVALID_PIPE)
  10057. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10058. else if (other_crtc_state)
  10059. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10060. return 0;
  10061. }
  10062. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10063. {
  10064. struct drm_crtc *crtc;
  10065. /* Add all pipes to the state */
  10066. for_each_crtc(state->dev, crtc) {
  10067. struct drm_crtc_state *crtc_state;
  10068. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10069. if (IS_ERR(crtc_state))
  10070. return PTR_ERR(crtc_state);
  10071. }
  10072. return 0;
  10073. }
  10074. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10075. {
  10076. struct drm_crtc *crtc;
  10077. /*
  10078. * Add all pipes to the state, and force
  10079. * a modeset on all the active ones.
  10080. */
  10081. for_each_crtc(state->dev, crtc) {
  10082. struct drm_crtc_state *crtc_state;
  10083. int ret;
  10084. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10085. if (IS_ERR(crtc_state))
  10086. return PTR_ERR(crtc_state);
  10087. if (!crtc_state->active || needs_modeset(crtc_state))
  10088. continue;
  10089. crtc_state->mode_changed = true;
  10090. ret = drm_atomic_add_affected_connectors(state, crtc);
  10091. if (ret)
  10092. return ret;
  10093. ret = drm_atomic_add_affected_planes(state, crtc);
  10094. if (ret)
  10095. return ret;
  10096. }
  10097. return 0;
  10098. }
  10099. static int intel_modeset_checks(struct drm_atomic_state *state)
  10100. {
  10101. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10102. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10103. struct drm_crtc *crtc;
  10104. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10105. int ret = 0, i;
  10106. if (!check_digital_port_conflicts(state)) {
  10107. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10108. return -EINVAL;
  10109. }
  10110. intel_state->modeset = true;
  10111. intel_state->active_crtcs = dev_priv->active_crtcs;
  10112. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10113. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10114. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10115. if (new_crtc_state->active)
  10116. intel_state->active_crtcs |= 1 << i;
  10117. else
  10118. intel_state->active_crtcs &= ~(1 << i);
  10119. if (old_crtc_state->active != new_crtc_state->active)
  10120. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10121. }
  10122. /*
  10123. * See if the config requires any additional preparation, e.g.
  10124. * to adjust global state with pipes off. We need to do this
  10125. * here so we can get the modeset_pipe updated config for the new
  10126. * mode set on this crtc. For other crtcs we need to use the
  10127. * adjusted_mode bits in the crtc directly.
  10128. */
  10129. if (dev_priv->display.modeset_calc_cdclk) {
  10130. ret = dev_priv->display.modeset_calc_cdclk(state);
  10131. if (ret < 0)
  10132. return ret;
  10133. /*
  10134. * Writes to dev_priv->cdclk.logical must protected by
  10135. * holding all the crtc locks, even if we don't end up
  10136. * touching the hardware
  10137. */
  10138. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10139. &intel_state->cdclk.logical)) {
  10140. ret = intel_lock_all_pipes(state);
  10141. if (ret < 0)
  10142. return ret;
  10143. }
  10144. /* All pipes must be switched off while we change the cdclk. */
  10145. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10146. &intel_state->cdclk.actual)) {
  10147. ret = intel_modeset_all_pipes(state);
  10148. if (ret < 0)
  10149. return ret;
  10150. }
  10151. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10152. intel_state->cdclk.logical.cdclk,
  10153. intel_state->cdclk.actual.cdclk);
  10154. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10155. intel_state->cdclk.logical.voltage_level,
  10156. intel_state->cdclk.actual.voltage_level);
  10157. } else {
  10158. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10159. }
  10160. intel_modeset_clear_plls(state);
  10161. if (IS_HASWELL(dev_priv))
  10162. return haswell_mode_set_planes_workaround(state);
  10163. return 0;
  10164. }
  10165. /*
  10166. * Handle calculation of various watermark data at the end of the atomic check
  10167. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10168. * handlers to ensure that all derived state has been updated.
  10169. */
  10170. static int calc_watermark_data(struct drm_atomic_state *state)
  10171. {
  10172. struct drm_device *dev = state->dev;
  10173. struct drm_i915_private *dev_priv = to_i915(dev);
  10174. /* Is there platform-specific watermark information to calculate? */
  10175. if (dev_priv->display.compute_global_watermarks)
  10176. return dev_priv->display.compute_global_watermarks(state);
  10177. return 0;
  10178. }
  10179. /**
  10180. * intel_atomic_check - validate state object
  10181. * @dev: drm device
  10182. * @state: state to validate
  10183. */
  10184. static int intel_atomic_check(struct drm_device *dev,
  10185. struct drm_atomic_state *state)
  10186. {
  10187. struct drm_i915_private *dev_priv = to_i915(dev);
  10188. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10189. struct drm_crtc *crtc;
  10190. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10191. int ret, i;
  10192. bool any_ms = false;
  10193. /* Catch I915_MODE_FLAG_INHERITED */
  10194. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  10195. crtc_state, i) {
  10196. if (crtc_state->mode.private_flags !=
  10197. old_crtc_state->mode.private_flags)
  10198. crtc_state->mode_changed = true;
  10199. }
  10200. ret = drm_atomic_helper_check_modeset(dev, state);
  10201. if (ret)
  10202. return ret;
  10203. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10204. struct intel_crtc_state *pipe_config =
  10205. to_intel_crtc_state(crtc_state);
  10206. if (!needs_modeset(crtc_state))
  10207. continue;
  10208. if (!crtc_state->enable) {
  10209. any_ms = true;
  10210. continue;
  10211. }
  10212. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10213. if (ret) {
  10214. intel_dump_pipe_config(to_intel_crtc(crtc),
  10215. pipe_config, "[failed]");
  10216. return ret;
  10217. }
  10218. if (i915_modparams.fastboot &&
  10219. intel_pipe_config_compare(dev_priv,
  10220. to_intel_crtc_state(old_crtc_state),
  10221. pipe_config, true)) {
  10222. crtc_state->mode_changed = false;
  10223. pipe_config->update_pipe = true;
  10224. }
  10225. if (needs_modeset(crtc_state))
  10226. any_ms = true;
  10227. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10228. needs_modeset(crtc_state) ?
  10229. "[modeset]" : "[fastset]");
  10230. }
  10231. if (any_ms) {
  10232. ret = intel_modeset_checks(state);
  10233. if (ret)
  10234. return ret;
  10235. } else {
  10236. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10237. }
  10238. ret = drm_atomic_helper_check_planes(dev, state);
  10239. if (ret)
  10240. return ret;
  10241. intel_fbc_choose_crtc(dev_priv, intel_state);
  10242. return calc_watermark_data(state);
  10243. }
  10244. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10245. struct drm_atomic_state *state)
  10246. {
  10247. return drm_atomic_helper_prepare_planes(dev, state);
  10248. }
  10249. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10250. {
  10251. struct drm_device *dev = crtc->base.dev;
  10252. if (!dev->max_vblank_count)
  10253. return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
  10254. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10255. }
  10256. static void intel_update_crtc(struct drm_crtc *crtc,
  10257. struct drm_atomic_state *state,
  10258. struct drm_crtc_state *old_crtc_state,
  10259. struct drm_crtc_state *new_crtc_state)
  10260. {
  10261. struct drm_device *dev = crtc->dev;
  10262. struct drm_i915_private *dev_priv = to_i915(dev);
  10263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10264. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10265. bool modeset = needs_modeset(new_crtc_state);
  10266. struct intel_plane_state *new_plane_state =
  10267. intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
  10268. to_intel_plane(crtc->primary));
  10269. if (modeset) {
  10270. update_scanline_offset(intel_crtc);
  10271. dev_priv->display.crtc_enable(pipe_config, state);
  10272. /* vblanks work again, re-enable pipe CRC. */
  10273. intel_crtc_enable_pipe_crc(intel_crtc);
  10274. } else {
  10275. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10276. pipe_config);
  10277. }
  10278. if (new_plane_state)
  10279. intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
  10280. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10281. }
  10282. static void intel_update_crtcs(struct drm_atomic_state *state)
  10283. {
  10284. struct drm_crtc *crtc;
  10285. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10286. int i;
  10287. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10288. if (!new_crtc_state->active)
  10289. continue;
  10290. intel_update_crtc(crtc, state, old_crtc_state,
  10291. new_crtc_state);
  10292. }
  10293. }
  10294. static void skl_update_crtcs(struct drm_atomic_state *state)
  10295. {
  10296. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10297. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10298. struct drm_crtc *crtc;
  10299. struct intel_crtc *intel_crtc;
  10300. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10301. struct intel_crtc_state *cstate;
  10302. unsigned int updated = 0;
  10303. bool progress;
  10304. enum pipe pipe;
  10305. int i;
  10306. u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
  10307. u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
  10308. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10309. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10310. /* ignore allocations for crtc's that have been turned off. */
  10311. if (new_crtc_state->active)
  10312. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10313. /* If 2nd DBuf slice required, enable it here */
  10314. if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
  10315. icl_dbuf_slices_update(dev_priv, required_slices);
  10316. /*
  10317. * Whenever the number of active pipes changes, we need to make sure we
  10318. * update the pipes in the right order so that their ddb allocations
  10319. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10320. * cause pipe underruns and other bad stuff.
  10321. */
  10322. do {
  10323. progress = false;
  10324. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10325. bool vbl_wait = false;
  10326. unsigned int cmask = drm_crtc_mask(crtc);
  10327. intel_crtc = to_intel_crtc(crtc);
  10328. cstate = to_intel_crtc_state(new_crtc_state);
  10329. pipe = intel_crtc->pipe;
  10330. if (updated & cmask || !cstate->base.active)
  10331. continue;
  10332. if (skl_ddb_allocation_overlaps(dev_priv,
  10333. entries,
  10334. &cstate->wm.skl.ddb,
  10335. i))
  10336. continue;
  10337. updated |= cmask;
  10338. entries[i] = &cstate->wm.skl.ddb;
  10339. /*
  10340. * If this is an already active pipe, it's DDB changed,
  10341. * and this isn't the last pipe that needs updating
  10342. * then we need to wait for a vblank to pass for the
  10343. * new ddb allocation to take effect.
  10344. */
  10345. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10346. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10347. !new_crtc_state->active_changed &&
  10348. intel_state->wm_results.dirty_pipes != updated)
  10349. vbl_wait = true;
  10350. intel_update_crtc(crtc, state, old_crtc_state,
  10351. new_crtc_state);
  10352. if (vbl_wait)
  10353. intel_wait_for_vblank(dev_priv, pipe);
  10354. progress = true;
  10355. }
  10356. } while (progress);
  10357. /* If 2nd DBuf slice is no more required disable it */
  10358. if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
  10359. icl_dbuf_slices_update(dev_priv, required_slices);
  10360. }
  10361. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10362. {
  10363. struct intel_atomic_state *state, *next;
  10364. struct llist_node *freed;
  10365. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10366. llist_for_each_entry_safe(state, next, freed, freed)
  10367. drm_atomic_state_put(&state->base);
  10368. }
  10369. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10370. {
  10371. struct drm_i915_private *dev_priv =
  10372. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10373. intel_atomic_helper_free_state(dev_priv);
  10374. }
  10375. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10376. {
  10377. struct wait_queue_entry wait_fence, wait_reset;
  10378. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10379. init_wait_entry(&wait_fence, 0);
  10380. init_wait_entry(&wait_reset, 0);
  10381. for (;;) {
  10382. prepare_to_wait(&intel_state->commit_ready.wait,
  10383. &wait_fence, TASK_UNINTERRUPTIBLE);
  10384. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10385. &wait_reset, TASK_UNINTERRUPTIBLE);
  10386. if (i915_sw_fence_done(&intel_state->commit_ready)
  10387. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10388. break;
  10389. schedule();
  10390. }
  10391. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10392. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10393. }
  10394. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10395. {
  10396. struct drm_device *dev = state->dev;
  10397. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10398. struct drm_i915_private *dev_priv = to_i915(dev);
  10399. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10400. struct drm_crtc *crtc;
  10401. struct intel_crtc_state *intel_cstate;
  10402. u64 put_domains[I915_MAX_PIPES] = {};
  10403. int i;
  10404. intel_atomic_commit_fence_wait(intel_state);
  10405. drm_atomic_helper_wait_for_dependencies(state);
  10406. if (intel_state->modeset)
  10407. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10408. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10410. if (needs_modeset(new_crtc_state) ||
  10411. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10412. put_domains[to_intel_crtc(crtc)->pipe] =
  10413. modeset_get_crtc_power_domains(crtc,
  10414. to_intel_crtc_state(new_crtc_state));
  10415. }
  10416. if (!needs_modeset(new_crtc_state))
  10417. continue;
  10418. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10419. to_intel_crtc_state(new_crtc_state));
  10420. if (old_crtc_state->active) {
  10421. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10422. /*
  10423. * We need to disable pipe CRC before disabling the pipe,
  10424. * or we race against vblank off.
  10425. */
  10426. intel_crtc_disable_pipe_crc(intel_crtc);
  10427. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10428. intel_crtc->active = false;
  10429. intel_fbc_disable(intel_crtc);
  10430. intel_disable_shared_dpll(intel_crtc);
  10431. /*
  10432. * Underruns don't always raise
  10433. * interrupts, so check manually.
  10434. */
  10435. intel_check_cpu_fifo_underruns(dev_priv);
  10436. intel_check_pch_fifo_underruns(dev_priv);
  10437. if (!new_crtc_state->active) {
  10438. /*
  10439. * Make sure we don't call initial_watermarks
  10440. * for ILK-style watermark updates.
  10441. *
  10442. * No clue what this is supposed to achieve.
  10443. */
  10444. if (INTEL_GEN(dev_priv) >= 9)
  10445. dev_priv->display.initial_watermarks(intel_state,
  10446. to_intel_crtc_state(new_crtc_state));
  10447. }
  10448. }
  10449. }
  10450. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10451. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10452. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10453. if (intel_state->modeset) {
  10454. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10455. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10456. /*
  10457. * SKL workaround: bspec recommends we disable the SAGV when we
  10458. * have more then one pipe enabled
  10459. */
  10460. if (!intel_can_enable_sagv(state))
  10461. intel_disable_sagv(dev_priv);
  10462. intel_modeset_verify_disabled(dev, state);
  10463. }
  10464. /* Complete the events for pipes that have now been disabled */
  10465. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10466. bool modeset = needs_modeset(new_crtc_state);
  10467. /* Complete events for now disable pipes here. */
  10468. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10469. spin_lock_irq(&dev->event_lock);
  10470. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10471. spin_unlock_irq(&dev->event_lock);
  10472. new_crtc_state->event = NULL;
  10473. }
  10474. }
  10475. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10476. dev_priv->display.update_crtcs(state);
  10477. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10478. * already, but still need the state for the delayed optimization. To
  10479. * fix this:
  10480. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10481. * - schedule that vblank worker _before_ calling hw_done
  10482. * - at the start of commit_tail, cancel it _synchrously
  10483. * - switch over to the vblank wait helper in the core after that since
  10484. * we don't need out special handling any more.
  10485. */
  10486. drm_atomic_helper_wait_for_flip_done(dev, state);
  10487. /*
  10488. * Now that the vblank has passed, we can go ahead and program the
  10489. * optimal watermarks on platforms that need two-step watermark
  10490. * programming.
  10491. *
  10492. * TODO: Move this (and other cleanup) to an async worker eventually.
  10493. */
  10494. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10495. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10496. if (dev_priv->display.optimize_watermarks)
  10497. dev_priv->display.optimize_watermarks(intel_state,
  10498. intel_cstate);
  10499. }
  10500. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10501. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10502. if (put_domains[i])
  10503. modeset_put_power_domains(dev_priv, put_domains[i]);
  10504. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10505. }
  10506. if (intel_state->modeset)
  10507. intel_verify_planes(intel_state);
  10508. if (intel_state->modeset && intel_can_enable_sagv(state))
  10509. intel_enable_sagv(dev_priv);
  10510. drm_atomic_helper_commit_hw_done(state);
  10511. if (intel_state->modeset) {
  10512. /* As one of the primary mmio accessors, KMS has a high
  10513. * likelihood of triggering bugs in unclaimed access. After we
  10514. * finish modesetting, see if an error has been flagged, and if
  10515. * so enable debugging for the next modeset - and hope we catch
  10516. * the culprit.
  10517. */
  10518. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10519. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10520. }
  10521. drm_atomic_helper_cleanup_planes(dev, state);
  10522. drm_atomic_helper_commit_cleanup_done(state);
  10523. drm_atomic_state_put(state);
  10524. intel_atomic_helper_free_state(dev_priv);
  10525. }
  10526. static void intel_atomic_commit_work(struct work_struct *work)
  10527. {
  10528. struct drm_atomic_state *state =
  10529. container_of(work, struct drm_atomic_state, commit_work);
  10530. intel_atomic_commit_tail(state);
  10531. }
  10532. static int __i915_sw_fence_call
  10533. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10534. enum i915_sw_fence_notify notify)
  10535. {
  10536. struct intel_atomic_state *state =
  10537. container_of(fence, struct intel_atomic_state, commit_ready);
  10538. switch (notify) {
  10539. case FENCE_COMPLETE:
  10540. /* we do blocking waits in the worker, nothing to do here */
  10541. break;
  10542. case FENCE_FREE:
  10543. {
  10544. struct intel_atomic_helper *helper =
  10545. &to_i915(state->base.dev)->atomic_helper;
  10546. if (llist_add(&state->freed, &helper->free_list))
  10547. schedule_work(&helper->free_work);
  10548. break;
  10549. }
  10550. }
  10551. return NOTIFY_DONE;
  10552. }
  10553. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10554. {
  10555. struct drm_plane_state *old_plane_state, *new_plane_state;
  10556. struct drm_plane *plane;
  10557. int i;
  10558. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10559. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10560. intel_fb_obj(new_plane_state->fb),
  10561. to_intel_plane(plane)->frontbuffer_bit);
  10562. }
  10563. /**
  10564. * intel_atomic_commit - commit validated state object
  10565. * @dev: DRM device
  10566. * @state: the top-level driver state object
  10567. * @nonblock: nonblocking commit
  10568. *
  10569. * This function commits a top-level state object that has been validated
  10570. * with drm_atomic_helper_check().
  10571. *
  10572. * RETURNS
  10573. * Zero for success or -errno.
  10574. */
  10575. static int intel_atomic_commit(struct drm_device *dev,
  10576. struct drm_atomic_state *state,
  10577. bool nonblock)
  10578. {
  10579. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10580. struct drm_i915_private *dev_priv = to_i915(dev);
  10581. int ret = 0;
  10582. drm_atomic_state_get(state);
  10583. i915_sw_fence_init(&intel_state->commit_ready,
  10584. intel_atomic_commit_ready);
  10585. /*
  10586. * The intel_legacy_cursor_update() fast path takes care
  10587. * of avoiding the vblank waits for simple cursor
  10588. * movement and flips. For cursor on/off and size changes,
  10589. * we want to perform the vblank waits so that watermark
  10590. * updates happen during the correct frames. Gen9+ have
  10591. * double buffered watermarks and so shouldn't need this.
  10592. *
  10593. * Unset state->legacy_cursor_update before the call to
  10594. * drm_atomic_helper_setup_commit() because otherwise
  10595. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10596. * we get FIFO underruns because we didn't wait
  10597. * for vblank.
  10598. *
  10599. * FIXME doing watermarks and fb cleanup from a vblank worker
  10600. * (assuming we had any) would solve these problems.
  10601. */
  10602. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10603. struct intel_crtc_state *new_crtc_state;
  10604. struct intel_crtc *crtc;
  10605. int i;
  10606. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10607. if (new_crtc_state->wm.need_postvbl_update ||
  10608. new_crtc_state->update_wm_post)
  10609. state->legacy_cursor_update = false;
  10610. }
  10611. ret = intel_atomic_prepare_commit(dev, state);
  10612. if (ret) {
  10613. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10614. i915_sw_fence_commit(&intel_state->commit_ready);
  10615. return ret;
  10616. }
  10617. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10618. if (!ret)
  10619. ret = drm_atomic_helper_swap_state(state, true);
  10620. if (ret) {
  10621. i915_sw_fence_commit(&intel_state->commit_ready);
  10622. drm_atomic_helper_cleanup_planes(dev, state);
  10623. return ret;
  10624. }
  10625. dev_priv->wm.distrust_bios_wm = false;
  10626. intel_shared_dpll_swap_state(state);
  10627. intel_atomic_track_fbs(state);
  10628. if (intel_state->modeset) {
  10629. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10630. sizeof(intel_state->min_cdclk));
  10631. memcpy(dev_priv->min_voltage_level,
  10632. intel_state->min_voltage_level,
  10633. sizeof(intel_state->min_voltage_level));
  10634. dev_priv->active_crtcs = intel_state->active_crtcs;
  10635. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10636. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10637. }
  10638. drm_atomic_state_get(state);
  10639. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10640. i915_sw_fence_commit(&intel_state->commit_ready);
  10641. if (nonblock && intel_state->modeset) {
  10642. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10643. } else if (nonblock) {
  10644. queue_work(system_unbound_wq, &state->commit_work);
  10645. } else {
  10646. if (intel_state->modeset)
  10647. flush_workqueue(dev_priv->modeset_wq);
  10648. intel_atomic_commit_tail(state);
  10649. }
  10650. return 0;
  10651. }
  10652. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10653. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10654. .set_config = drm_atomic_helper_set_config,
  10655. .destroy = intel_crtc_destroy,
  10656. .page_flip = drm_atomic_helper_page_flip,
  10657. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10658. .atomic_destroy_state = intel_crtc_destroy_state,
  10659. .set_crc_source = intel_crtc_set_crc_source,
  10660. };
  10661. struct wait_rps_boost {
  10662. struct wait_queue_entry wait;
  10663. struct drm_crtc *crtc;
  10664. struct i915_request *request;
  10665. };
  10666. static int do_rps_boost(struct wait_queue_entry *_wait,
  10667. unsigned mode, int sync, void *key)
  10668. {
  10669. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10670. struct i915_request *rq = wait->request;
  10671. /*
  10672. * If we missed the vblank, but the request is already running it
  10673. * is reasonable to assume that it will complete before the next
  10674. * vblank without our intervention, so leave RPS alone.
  10675. */
  10676. if (!i915_request_started(rq))
  10677. gen6_rps_boost(rq, NULL);
  10678. i915_request_put(rq);
  10679. drm_crtc_vblank_put(wait->crtc);
  10680. list_del(&wait->wait.entry);
  10681. kfree(wait);
  10682. return 1;
  10683. }
  10684. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10685. struct dma_fence *fence)
  10686. {
  10687. struct wait_rps_boost *wait;
  10688. if (!dma_fence_is_i915(fence))
  10689. return;
  10690. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10691. return;
  10692. if (drm_crtc_vblank_get(crtc))
  10693. return;
  10694. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10695. if (!wait) {
  10696. drm_crtc_vblank_put(crtc);
  10697. return;
  10698. }
  10699. wait->request = to_request(dma_fence_get(fence));
  10700. wait->crtc = crtc;
  10701. wait->wait.func = do_rps_boost;
  10702. wait->wait.flags = 0;
  10703. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10704. }
  10705. static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
  10706. {
  10707. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  10708. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10709. struct drm_framebuffer *fb = plane_state->base.fb;
  10710. struct i915_vma *vma;
  10711. if (plane->id == PLANE_CURSOR &&
  10712. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10713. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10714. const int align = intel_cursor_alignment(dev_priv);
  10715. return i915_gem_object_attach_phys(obj, align);
  10716. }
  10717. vma = intel_pin_and_fence_fb_obj(fb,
  10718. plane_state->base.rotation,
  10719. intel_plane_uses_fence(plane_state),
  10720. &plane_state->flags);
  10721. if (IS_ERR(vma))
  10722. return PTR_ERR(vma);
  10723. plane_state->vma = vma;
  10724. return 0;
  10725. }
  10726. static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
  10727. {
  10728. struct i915_vma *vma;
  10729. vma = fetch_and_zero(&old_plane_state->vma);
  10730. if (vma)
  10731. intel_unpin_fb_vma(vma, old_plane_state->flags);
  10732. }
  10733. static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
  10734. {
  10735. struct i915_sched_attr attr = {
  10736. .priority = I915_PRIORITY_DISPLAY,
  10737. };
  10738. i915_gem_object_wait_priority(obj, 0, &attr);
  10739. }
  10740. /**
  10741. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10742. * @plane: drm plane to prepare for
  10743. * @new_state: the plane state being prepared
  10744. *
  10745. * Prepares a framebuffer for usage on a display plane. Generally this
  10746. * involves pinning the underlying object and updating the frontbuffer tracking
  10747. * bits. Some older platforms need special physical address handling for
  10748. * cursor planes.
  10749. *
  10750. * Must be called with struct_mutex held.
  10751. *
  10752. * Returns 0 on success, negative error code on failure.
  10753. */
  10754. int
  10755. intel_prepare_plane_fb(struct drm_plane *plane,
  10756. struct drm_plane_state *new_state)
  10757. {
  10758. struct intel_atomic_state *intel_state =
  10759. to_intel_atomic_state(new_state->state);
  10760. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10761. struct drm_framebuffer *fb = new_state->fb;
  10762. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10763. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10764. int ret;
  10765. if (old_obj) {
  10766. struct drm_crtc_state *crtc_state =
  10767. drm_atomic_get_new_crtc_state(new_state->state,
  10768. plane->state->crtc);
  10769. /* Big Hammer, we also need to ensure that any pending
  10770. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10771. * current scanout is retired before unpinning the old
  10772. * framebuffer. Note that we rely on userspace rendering
  10773. * into the buffer attached to the pipe they are waiting
  10774. * on. If not, userspace generates a GPU hang with IPEHR
  10775. * point to the MI_WAIT_FOR_EVENT.
  10776. *
  10777. * This should only fail upon a hung GPU, in which case we
  10778. * can safely continue.
  10779. */
  10780. if (needs_modeset(crtc_state)) {
  10781. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10782. old_obj->resv, NULL,
  10783. false, 0,
  10784. GFP_KERNEL);
  10785. if (ret < 0)
  10786. return ret;
  10787. }
  10788. }
  10789. if (new_state->fence) { /* explicit fencing */
  10790. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10791. new_state->fence,
  10792. I915_FENCE_TIMEOUT,
  10793. GFP_KERNEL);
  10794. if (ret < 0)
  10795. return ret;
  10796. }
  10797. if (!obj)
  10798. return 0;
  10799. ret = i915_gem_object_pin_pages(obj);
  10800. if (ret)
  10801. return ret;
  10802. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10803. if (ret) {
  10804. i915_gem_object_unpin_pages(obj);
  10805. return ret;
  10806. }
  10807. ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
  10808. fb_obj_bump_render_priority(obj);
  10809. mutex_unlock(&dev_priv->drm.struct_mutex);
  10810. i915_gem_object_unpin_pages(obj);
  10811. if (ret)
  10812. return ret;
  10813. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  10814. if (!new_state->fence) { /* implicit fencing */
  10815. struct dma_fence *fence;
  10816. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10817. obj->resv, NULL,
  10818. false, I915_FENCE_TIMEOUT,
  10819. GFP_KERNEL);
  10820. if (ret < 0)
  10821. return ret;
  10822. fence = reservation_object_get_excl_rcu(obj->resv);
  10823. if (fence) {
  10824. add_rps_boost_after_vblank(new_state->crtc, fence);
  10825. dma_fence_put(fence);
  10826. }
  10827. } else {
  10828. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10829. }
  10830. return 0;
  10831. }
  10832. /**
  10833. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10834. * @plane: drm plane to clean up for
  10835. * @old_state: the state from the previous modeset
  10836. *
  10837. * Cleans up a framebuffer that has just been removed from a plane.
  10838. *
  10839. * Must be called with struct_mutex held.
  10840. */
  10841. void
  10842. intel_cleanup_plane_fb(struct drm_plane *plane,
  10843. struct drm_plane_state *old_state)
  10844. {
  10845. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10846. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10847. mutex_lock(&dev_priv->drm.struct_mutex);
  10848. intel_plane_unpin_fb(to_intel_plane_state(old_state));
  10849. mutex_unlock(&dev_priv->drm.struct_mutex);
  10850. }
  10851. int
  10852. skl_max_scale(struct intel_crtc *intel_crtc,
  10853. struct intel_crtc_state *crtc_state,
  10854. uint32_t pixel_format)
  10855. {
  10856. struct drm_i915_private *dev_priv;
  10857. int max_scale, mult;
  10858. int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
  10859. if (!intel_crtc || !crtc_state->base.enable)
  10860. return DRM_PLANE_HELPER_NO_SCALING;
  10861. dev_priv = to_i915(intel_crtc->base.dev);
  10862. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10863. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10864. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10865. max_dotclk *= 2;
  10866. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10867. return DRM_PLANE_HELPER_NO_SCALING;
  10868. /*
  10869. * skl max scale is lower of:
  10870. * close to 3 but not 3, -1 is for that purpose
  10871. * or
  10872. * cdclk/crtc_clock
  10873. */
  10874. mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
  10875. tmpclk1 = (1 << 16) * mult - 1;
  10876. tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
  10877. max_scale = min(tmpclk1, tmpclk2);
  10878. return max_scale;
  10879. }
  10880. static int
  10881. intel_check_primary_plane(struct intel_plane *plane,
  10882. struct intel_crtc_state *crtc_state,
  10883. struct intel_plane_state *state)
  10884. {
  10885. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10886. struct drm_crtc *crtc = state->base.crtc;
  10887. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10888. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10889. bool can_position = false;
  10890. int ret;
  10891. uint32_t pixel_format = 0;
  10892. if (INTEL_GEN(dev_priv) >= 9) {
  10893. /* use scaler when colorkey is not required */
  10894. if (!state->ckey.flags) {
  10895. min_scale = 1;
  10896. if (state->base.fb)
  10897. pixel_format = state->base.fb->format->format;
  10898. max_scale = skl_max_scale(to_intel_crtc(crtc),
  10899. crtc_state, pixel_format);
  10900. }
  10901. can_position = true;
  10902. }
  10903. ret = drm_atomic_helper_check_plane_state(&state->base,
  10904. &crtc_state->base,
  10905. min_scale, max_scale,
  10906. can_position, true);
  10907. if (ret)
  10908. return ret;
  10909. if (!state->base.fb)
  10910. return 0;
  10911. if (INTEL_GEN(dev_priv) >= 9) {
  10912. ret = skl_check_plane_surface(crtc_state, state);
  10913. if (ret)
  10914. return ret;
  10915. state->ctl = skl_plane_ctl(crtc_state, state);
  10916. } else {
  10917. ret = i9xx_check_plane_surface(state);
  10918. if (ret)
  10919. return ret;
  10920. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10921. }
  10922. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10923. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10924. return 0;
  10925. }
  10926. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10927. struct drm_crtc_state *old_crtc_state)
  10928. {
  10929. struct drm_device *dev = crtc->dev;
  10930. struct drm_i915_private *dev_priv = to_i915(dev);
  10931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10932. struct intel_crtc_state *old_intel_cstate =
  10933. to_intel_crtc_state(old_crtc_state);
  10934. struct intel_atomic_state *old_intel_state =
  10935. to_intel_atomic_state(old_crtc_state->state);
  10936. struct intel_crtc_state *intel_cstate =
  10937. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10938. bool modeset = needs_modeset(&intel_cstate->base);
  10939. if (!modeset &&
  10940. (intel_cstate->base.color_mgmt_changed ||
  10941. intel_cstate->update_pipe)) {
  10942. intel_color_set_csc(&intel_cstate->base);
  10943. intel_color_load_luts(&intel_cstate->base);
  10944. }
  10945. /* Perform vblank evasion around commit operation */
  10946. intel_pipe_update_start(intel_cstate);
  10947. if (modeset)
  10948. goto out;
  10949. if (intel_cstate->update_pipe)
  10950. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10951. else if (INTEL_GEN(dev_priv) >= 9)
  10952. skl_detach_scalers(intel_crtc);
  10953. out:
  10954. if (dev_priv->display.atomic_update_watermarks)
  10955. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10956. intel_cstate);
  10957. }
  10958. void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
  10959. struct intel_crtc_state *crtc_state)
  10960. {
  10961. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10962. if (!IS_GEN2(dev_priv))
  10963. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
  10964. if (crtc_state->has_pch_encoder) {
  10965. enum pipe pch_transcoder =
  10966. intel_crtc_pch_transcoder(crtc);
  10967. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10968. }
  10969. }
  10970. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10971. struct drm_crtc_state *old_crtc_state)
  10972. {
  10973. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10974. struct intel_atomic_state *old_intel_state =
  10975. to_intel_atomic_state(old_crtc_state->state);
  10976. struct intel_crtc_state *new_crtc_state =
  10977. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10978. intel_pipe_update_end(new_crtc_state);
  10979. if (new_crtc_state->update_pipe &&
  10980. !needs_modeset(&new_crtc_state->base) &&
  10981. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
  10982. intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
  10983. }
  10984. /**
  10985. * intel_plane_destroy - destroy a plane
  10986. * @plane: plane to destroy
  10987. *
  10988. * Common destruction function for all types of planes (primary, cursor,
  10989. * sprite).
  10990. */
  10991. void intel_plane_destroy(struct drm_plane *plane)
  10992. {
  10993. drm_plane_cleanup(plane);
  10994. kfree(to_intel_plane(plane));
  10995. }
  10996. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10997. {
  10998. switch (format) {
  10999. case DRM_FORMAT_C8:
  11000. case DRM_FORMAT_RGB565:
  11001. case DRM_FORMAT_XRGB1555:
  11002. case DRM_FORMAT_XRGB8888:
  11003. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11004. modifier == I915_FORMAT_MOD_X_TILED;
  11005. default:
  11006. return false;
  11007. }
  11008. }
  11009. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  11010. {
  11011. switch (format) {
  11012. case DRM_FORMAT_C8:
  11013. case DRM_FORMAT_RGB565:
  11014. case DRM_FORMAT_XRGB8888:
  11015. case DRM_FORMAT_XBGR8888:
  11016. case DRM_FORMAT_XRGB2101010:
  11017. case DRM_FORMAT_XBGR2101010:
  11018. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11019. modifier == I915_FORMAT_MOD_X_TILED;
  11020. default:
  11021. return false;
  11022. }
  11023. }
  11024. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  11025. {
  11026. switch (format) {
  11027. case DRM_FORMAT_XRGB8888:
  11028. case DRM_FORMAT_XBGR8888:
  11029. case DRM_FORMAT_ARGB8888:
  11030. case DRM_FORMAT_ABGR8888:
  11031. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  11032. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  11033. return true;
  11034. /* fall through */
  11035. case DRM_FORMAT_RGB565:
  11036. case DRM_FORMAT_XRGB2101010:
  11037. case DRM_FORMAT_XBGR2101010:
  11038. case DRM_FORMAT_YUYV:
  11039. case DRM_FORMAT_YVYU:
  11040. case DRM_FORMAT_UYVY:
  11041. case DRM_FORMAT_VYUY:
  11042. case DRM_FORMAT_NV12:
  11043. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  11044. return true;
  11045. /* fall through */
  11046. case DRM_FORMAT_C8:
  11047. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  11048. modifier == I915_FORMAT_MOD_X_TILED ||
  11049. modifier == I915_FORMAT_MOD_Y_TILED)
  11050. return true;
  11051. /* fall through */
  11052. default:
  11053. return false;
  11054. }
  11055. }
  11056. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  11057. uint32_t format,
  11058. uint64_t modifier)
  11059. {
  11060. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11061. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  11062. return false;
  11063. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  11064. modifier != DRM_FORMAT_MOD_LINEAR)
  11065. return false;
  11066. if (INTEL_GEN(dev_priv) >= 9)
  11067. return skl_mod_supported(format, modifier);
  11068. else if (INTEL_GEN(dev_priv) >= 4)
  11069. return i965_mod_supported(format, modifier);
  11070. else
  11071. return i8xx_mod_supported(format, modifier);
  11072. }
  11073. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  11074. uint32_t format,
  11075. uint64_t modifier)
  11076. {
  11077. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  11078. return false;
  11079. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  11080. }
  11081. static struct drm_plane_funcs intel_plane_funcs = {
  11082. .update_plane = drm_atomic_helper_update_plane,
  11083. .disable_plane = drm_atomic_helper_disable_plane,
  11084. .destroy = intel_plane_destroy,
  11085. .atomic_get_property = intel_plane_atomic_get_property,
  11086. .atomic_set_property = intel_plane_atomic_set_property,
  11087. .atomic_duplicate_state = intel_plane_duplicate_state,
  11088. .atomic_destroy_state = intel_plane_destroy_state,
  11089. .format_mod_supported = intel_primary_plane_format_mod_supported,
  11090. };
  11091. static int
  11092. intel_legacy_cursor_update(struct drm_plane *plane,
  11093. struct drm_crtc *crtc,
  11094. struct drm_framebuffer *fb,
  11095. int crtc_x, int crtc_y,
  11096. unsigned int crtc_w, unsigned int crtc_h,
  11097. uint32_t src_x, uint32_t src_y,
  11098. uint32_t src_w, uint32_t src_h,
  11099. struct drm_modeset_acquire_ctx *ctx)
  11100. {
  11101. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11102. int ret;
  11103. struct drm_plane_state *old_plane_state, *new_plane_state;
  11104. struct intel_plane *intel_plane = to_intel_plane(plane);
  11105. struct drm_framebuffer *old_fb;
  11106. struct drm_crtc_state *crtc_state = crtc->state;
  11107. /*
  11108. * When crtc is inactive or there is a modeset pending,
  11109. * wait for it to complete in the slowpath
  11110. */
  11111. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11112. to_intel_crtc_state(crtc_state)->update_pipe)
  11113. goto slow;
  11114. old_plane_state = plane->state;
  11115. /*
  11116. * Don't do an async update if there is an outstanding commit modifying
  11117. * the plane. This prevents our async update's changes from getting
  11118. * overridden by a previous synchronous update's state.
  11119. */
  11120. if (old_plane_state->commit &&
  11121. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  11122. goto slow;
  11123. /*
  11124. * If any parameters change that may affect watermarks,
  11125. * take the slowpath. Only changing fb or position should be
  11126. * in the fastpath.
  11127. */
  11128. if (old_plane_state->crtc != crtc ||
  11129. old_plane_state->src_w != src_w ||
  11130. old_plane_state->src_h != src_h ||
  11131. old_plane_state->crtc_w != crtc_w ||
  11132. old_plane_state->crtc_h != crtc_h ||
  11133. !old_plane_state->fb != !fb)
  11134. goto slow;
  11135. new_plane_state = intel_plane_duplicate_state(plane);
  11136. if (!new_plane_state)
  11137. return -ENOMEM;
  11138. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11139. new_plane_state->src_x = src_x;
  11140. new_plane_state->src_y = src_y;
  11141. new_plane_state->src_w = src_w;
  11142. new_plane_state->src_h = src_h;
  11143. new_plane_state->crtc_x = crtc_x;
  11144. new_plane_state->crtc_y = crtc_y;
  11145. new_plane_state->crtc_w = crtc_w;
  11146. new_plane_state->crtc_h = crtc_h;
  11147. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11148. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  11149. to_intel_plane_state(plane->state),
  11150. to_intel_plane_state(new_plane_state));
  11151. if (ret)
  11152. goto out_free;
  11153. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11154. if (ret)
  11155. goto out_free;
  11156. ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
  11157. if (ret)
  11158. goto out_unlock;
  11159. intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
  11160. old_fb = old_plane_state->fb;
  11161. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11162. intel_plane->frontbuffer_bit);
  11163. /* Swap plane state */
  11164. plane->state = new_plane_state;
  11165. if (plane->state->visible) {
  11166. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11167. intel_plane->update_plane(intel_plane,
  11168. to_intel_crtc_state(crtc->state),
  11169. to_intel_plane_state(plane->state));
  11170. } else {
  11171. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11172. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11173. }
  11174. intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
  11175. out_unlock:
  11176. mutex_unlock(&dev_priv->drm.struct_mutex);
  11177. out_free:
  11178. if (ret)
  11179. intel_plane_destroy_state(plane, new_plane_state);
  11180. else
  11181. intel_plane_destroy_state(plane, old_plane_state);
  11182. return ret;
  11183. slow:
  11184. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11185. crtc_x, crtc_y, crtc_w, crtc_h,
  11186. src_x, src_y, src_w, src_h, ctx);
  11187. }
  11188. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11189. .update_plane = intel_legacy_cursor_update,
  11190. .disable_plane = drm_atomic_helper_disable_plane,
  11191. .destroy = intel_plane_destroy,
  11192. .atomic_get_property = intel_plane_atomic_get_property,
  11193. .atomic_set_property = intel_plane_atomic_set_property,
  11194. .atomic_duplicate_state = intel_plane_duplicate_state,
  11195. .atomic_destroy_state = intel_plane_destroy_state,
  11196. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  11197. };
  11198. static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
  11199. enum i9xx_plane_id i9xx_plane)
  11200. {
  11201. if (!HAS_FBC(dev_priv))
  11202. return false;
  11203. if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  11204. return i9xx_plane == PLANE_A; /* tied to pipe A */
  11205. else if (IS_IVYBRIDGE(dev_priv))
  11206. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
  11207. i9xx_plane == PLANE_C;
  11208. else if (INTEL_GEN(dev_priv) >= 4)
  11209. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
  11210. else
  11211. return i9xx_plane == PLANE_A;
  11212. }
  11213. static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
  11214. enum pipe pipe, enum plane_id plane_id)
  11215. {
  11216. if (!HAS_FBC(dev_priv))
  11217. return false;
  11218. return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
  11219. }
  11220. bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
  11221. enum pipe pipe, enum plane_id plane_id)
  11222. {
  11223. if (plane_id == PLANE_PRIMARY) {
  11224. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11225. return false;
  11226. else if ((INTEL_GEN(dev_priv) == 9 && pipe == PIPE_C) &&
  11227. !IS_GEMINILAKE(dev_priv))
  11228. return false;
  11229. } else if (plane_id >= PLANE_SPRITE0) {
  11230. if (plane_id == PLANE_CURSOR)
  11231. return false;
  11232. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) == 10) {
  11233. if (plane_id != PLANE_SPRITE0)
  11234. return false;
  11235. } else {
  11236. if (plane_id != PLANE_SPRITE0 || pipe == PIPE_C ||
  11237. IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11238. return false;
  11239. }
  11240. }
  11241. return true;
  11242. }
  11243. static struct intel_plane *
  11244. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11245. {
  11246. struct intel_plane *primary = NULL;
  11247. struct intel_plane_state *state = NULL;
  11248. const uint32_t *intel_primary_formats;
  11249. unsigned int supported_rotations;
  11250. unsigned int num_formats;
  11251. const uint64_t *modifiers;
  11252. int ret;
  11253. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11254. if (!primary) {
  11255. ret = -ENOMEM;
  11256. goto fail;
  11257. }
  11258. state = intel_create_plane_state(&primary->base);
  11259. if (!state) {
  11260. ret = -ENOMEM;
  11261. goto fail;
  11262. }
  11263. primary->base.state = &state->base;
  11264. primary->can_scale = false;
  11265. primary->max_downscale = 1;
  11266. if (INTEL_GEN(dev_priv) >= 9) {
  11267. primary->can_scale = true;
  11268. state->scaler_id = -1;
  11269. }
  11270. primary->pipe = pipe;
  11271. /*
  11272. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11273. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11274. */
  11275. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11276. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11277. else
  11278. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11279. primary->id = PLANE_PRIMARY;
  11280. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11281. if (INTEL_GEN(dev_priv) >= 9)
  11282. primary->has_fbc = skl_plane_has_fbc(dev_priv,
  11283. primary->pipe,
  11284. primary->id);
  11285. else
  11286. primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
  11287. primary->i9xx_plane);
  11288. if (primary->has_fbc) {
  11289. struct intel_fbc *fbc = &dev_priv->fbc;
  11290. fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
  11291. }
  11292. primary->check_plane = intel_check_primary_plane;
  11293. if (INTEL_GEN(dev_priv) >= 9) {
  11294. if (skl_plane_has_planar(dev_priv, pipe, PLANE_PRIMARY)) {
  11295. intel_primary_formats = skl_pri_planar_formats;
  11296. num_formats = ARRAY_SIZE(skl_pri_planar_formats);
  11297. } else {
  11298. intel_primary_formats = skl_primary_formats;
  11299. num_formats = ARRAY_SIZE(skl_primary_formats);
  11300. }
  11301. if (skl_plane_has_ccs(dev_priv, pipe, PLANE_PRIMARY))
  11302. modifiers = skl_format_modifiers_ccs;
  11303. else
  11304. modifiers = skl_format_modifiers_noccs;
  11305. primary->update_plane = skl_update_plane;
  11306. primary->disable_plane = skl_disable_plane;
  11307. primary->get_hw_state = skl_plane_get_hw_state;
  11308. } else if (INTEL_GEN(dev_priv) >= 4) {
  11309. intel_primary_formats = i965_primary_formats;
  11310. num_formats = ARRAY_SIZE(i965_primary_formats);
  11311. modifiers = i9xx_format_modifiers;
  11312. primary->update_plane = i9xx_update_plane;
  11313. primary->disable_plane = i9xx_disable_plane;
  11314. primary->get_hw_state = i9xx_plane_get_hw_state;
  11315. } else {
  11316. intel_primary_formats = i8xx_primary_formats;
  11317. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11318. modifiers = i9xx_format_modifiers;
  11319. primary->update_plane = i9xx_update_plane;
  11320. primary->disable_plane = i9xx_disable_plane;
  11321. primary->get_hw_state = i9xx_plane_get_hw_state;
  11322. }
  11323. if (INTEL_GEN(dev_priv) >= 9)
  11324. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11325. 0, &intel_plane_funcs,
  11326. intel_primary_formats, num_formats,
  11327. modifiers,
  11328. DRM_PLANE_TYPE_PRIMARY,
  11329. "plane 1%c", pipe_name(pipe));
  11330. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11331. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11332. 0, &intel_plane_funcs,
  11333. intel_primary_formats, num_formats,
  11334. modifiers,
  11335. DRM_PLANE_TYPE_PRIMARY,
  11336. "primary %c", pipe_name(pipe));
  11337. else
  11338. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11339. 0, &intel_plane_funcs,
  11340. intel_primary_formats, num_formats,
  11341. modifiers,
  11342. DRM_PLANE_TYPE_PRIMARY,
  11343. "plane %c",
  11344. plane_name(primary->i9xx_plane));
  11345. if (ret)
  11346. goto fail;
  11347. if (INTEL_GEN(dev_priv) >= 10) {
  11348. supported_rotations =
  11349. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11350. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11351. DRM_MODE_REFLECT_X;
  11352. } else if (INTEL_GEN(dev_priv) >= 9) {
  11353. supported_rotations =
  11354. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11355. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11356. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11357. supported_rotations =
  11358. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11359. DRM_MODE_REFLECT_X;
  11360. } else if (INTEL_GEN(dev_priv) >= 4) {
  11361. supported_rotations =
  11362. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11363. } else {
  11364. supported_rotations = DRM_MODE_ROTATE_0;
  11365. }
  11366. if (INTEL_GEN(dev_priv) >= 4)
  11367. drm_plane_create_rotation_property(&primary->base,
  11368. DRM_MODE_ROTATE_0,
  11369. supported_rotations);
  11370. if (INTEL_GEN(dev_priv) >= 9)
  11371. drm_plane_create_color_properties(&primary->base,
  11372. BIT(DRM_COLOR_YCBCR_BT601) |
  11373. BIT(DRM_COLOR_YCBCR_BT709),
  11374. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  11375. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  11376. DRM_COLOR_YCBCR_BT709,
  11377. DRM_COLOR_YCBCR_LIMITED_RANGE);
  11378. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11379. return primary;
  11380. fail:
  11381. kfree(state);
  11382. kfree(primary);
  11383. return ERR_PTR(ret);
  11384. }
  11385. static struct intel_plane *
  11386. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11387. enum pipe pipe)
  11388. {
  11389. struct intel_plane *cursor = NULL;
  11390. struct intel_plane_state *state = NULL;
  11391. int ret;
  11392. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11393. if (!cursor) {
  11394. ret = -ENOMEM;
  11395. goto fail;
  11396. }
  11397. state = intel_create_plane_state(&cursor->base);
  11398. if (!state) {
  11399. ret = -ENOMEM;
  11400. goto fail;
  11401. }
  11402. cursor->base.state = &state->base;
  11403. cursor->can_scale = false;
  11404. cursor->max_downscale = 1;
  11405. cursor->pipe = pipe;
  11406. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11407. cursor->id = PLANE_CURSOR;
  11408. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11409. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11410. cursor->update_plane = i845_update_cursor;
  11411. cursor->disable_plane = i845_disable_cursor;
  11412. cursor->get_hw_state = i845_cursor_get_hw_state;
  11413. cursor->check_plane = i845_check_cursor;
  11414. } else {
  11415. cursor->update_plane = i9xx_update_cursor;
  11416. cursor->disable_plane = i9xx_disable_cursor;
  11417. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11418. cursor->check_plane = i9xx_check_cursor;
  11419. }
  11420. cursor->cursor.base = ~0;
  11421. cursor->cursor.cntl = ~0;
  11422. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11423. cursor->cursor.size = ~0;
  11424. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11425. 0, &intel_cursor_plane_funcs,
  11426. intel_cursor_formats,
  11427. ARRAY_SIZE(intel_cursor_formats),
  11428. cursor_format_modifiers,
  11429. DRM_PLANE_TYPE_CURSOR,
  11430. "cursor %c", pipe_name(pipe));
  11431. if (ret)
  11432. goto fail;
  11433. if (INTEL_GEN(dev_priv) >= 4)
  11434. drm_plane_create_rotation_property(&cursor->base,
  11435. DRM_MODE_ROTATE_0,
  11436. DRM_MODE_ROTATE_0 |
  11437. DRM_MODE_ROTATE_180);
  11438. if (INTEL_GEN(dev_priv) >= 9)
  11439. state->scaler_id = -1;
  11440. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11441. return cursor;
  11442. fail:
  11443. kfree(state);
  11444. kfree(cursor);
  11445. return ERR_PTR(ret);
  11446. }
  11447. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11448. struct intel_crtc_state *crtc_state)
  11449. {
  11450. struct intel_crtc_scaler_state *scaler_state =
  11451. &crtc_state->scaler_state;
  11452. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11453. int i;
  11454. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11455. if (!crtc->num_scalers)
  11456. return;
  11457. for (i = 0; i < crtc->num_scalers; i++) {
  11458. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11459. scaler->in_use = 0;
  11460. scaler->mode = PS_SCALER_MODE_DYN;
  11461. }
  11462. scaler_state->scaler_id = -1;
  11463. }
  11464. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11465. {
  11466. struct intel_crtc *intel_crtc;
  11467. struct intel_crtc_state *crtc_state = NULL;
  11468. struct intel_plane *primary = NULL;
  11469. struct intel_plane *cursor = NULL;
  11470. int sprite, ret;
  11471. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11472. if (!intel_crtc)
  11473. return -ENOMEM;
  11474. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11475. if (!crtc_state) {
  11476. ret = -ENOMEM;
  11477. goto fail;
  11478. }
  11479. intel_crtc->config = crtc_state;
  11480. intel_crtc->base.state = &crtc_state->base;
  11481. crtc_state->base.crtc = &intel_crtc->base;
  11482. primary = intel_primary_plane_create(dev_priv, pipe);
  11483. if (IS_ERR(primary)) {
  11484. ret = PTR_ERR(primary);
  11485. goto fail;
  11486. }
  11487. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11488. for_each_sprite(dev_priv, pipe, sprite) {
  11489. struct intel_plane *plane;
  11490. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11491. if (IS_ERR(plane)) {
  11492. ret = PTR_ERR(plane);
  11493. goto fail;
  11494. }
  11495. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11496. }
  11497. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11498. if (IS_ERR(cursor)) {
  11499. ret = PTR_ERR(cursor);
  11500. goto fail;
  11501. }
  11502. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11503. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11504. &primary->base, &cursor->base,
  11505. &intel_crtc_funcs,
  11506. "pipe %c", pipe_name(pipe));
  11507. if (ret)
  11508. goto fail;
  11509. intel_crtc->pipe = pipe;
  11510. /* initialize shared scalers */
  11511. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11512. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
  11513. dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
  11514. dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
  11515. if (INTEL_GEN(dev_priv) < 9) {
  11516. enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
  11517. BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11518. dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
  11519. dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
  11520. }
  11521. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11522. intel_color_init(&intel_crtc->base);
  11523. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11524. return 0;
  11525. fail:
  11526. /*
  11527. * drm_mode_config_cleanup() will free up any
  11528. * crtcs/planes already initialized.
  11529. */
  11530. kfree(crtc_state);
  11531. kfree(intel_crtc);
  11532. return ret;
  11533. }
  11534. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11535. {
  11536. struct drm_device *dev = connector->base.dev;
  11537. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11538. if (!connector->base.state->crtc)
  11539. return INVALID_PIPE;
  11540. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11541. }
  11542. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11543. struct drm_file *file)
  11544. {
  11545. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11546. struct drm_crtc *drmmode_crtc;
  11547. struct intel_crtc *crtc;
  11548. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11549. if (!drmmode_crtc)
  11550. return -ENOENT;
  11551. crtc = to_intel_crtc(drmmode_crtc);
  11552. pipe_from_crtc_id->pipe = crtc->pipe;
  11553. return 0;
  11554. }
  11555. static int intel_encoder_clones(struct intel_encoder *encoder)
  11556. {
  11557. struct drm_device *dev = encoder->base.dev;
  11558. struct intel_encoder *source_encoder;
  11559. int index_mask = 0;
  11560. int entry = 0;
  11561. for_each_intel_encoder(dev, source_encoder) {
  11562. if (encoders_cloneable(encoder, source_encoder))
  11563. index_mask |= (1 << entry);
  11564. entry++;
  11565. }
  11566. return index_mask;
  11567. }
  11568. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11569. {
  11570. if (!IS_MOBILE(dev_priv))
  11571. return false;
  11572. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11573. return false;
  11574. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11575. return false;
  11576. return true;
  11577. }
  11578. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11579. {
  11580. if (INTEL_GEN(dev_priv) >= 9)
  11581. return false;
  11582. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11583. return false;
  11584. if (IS_CHERRYVIEW(dev_priv))
  11585. return false;
  11586. if (HAS_PCH_LPT_H(dev_priv) &&
  11587. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11588. return false;
  11589. /* DDI E can't be used if DDI A requires 4 lanes */
  11590. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11591. return false;
  11592. if (!dev_priv->vbt.int_crt_support)
  11593. return false;
  11594. return true;
  11595. }
  11596. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11597. {
  11598. int pps_num;
  11599. int pps_idx;
  11600. if (HAS_DDI(dev_priv))
  11601. return;
  11602. /*
  11603. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11604. * everywhere where registers can be write protected.
  11605. */
  11606. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11607. pps_num = 2;
  11608. else
  11609. pps_num = 1;
  11610. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11611. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11612. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11613. I915_WRITE(PP_CONTROL(pps_idx), val);
  11614. }
  11615. }
  11616. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11617. {
  11618. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11619. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11620. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11621. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11622. else
  11623. dev_priv->pps_mmio_base = PPS_BASE;
  11624. intel_pps_unlock_regs_wa(dev_priv);
  11625. }
  11626. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11627. {
  11628. struct intel_encoder *encoder;
  11629. bool dpd_is_edp = false;
  11630. intel_pps_init(dev_priv);
  11631. /*
  11632. * intel_edp_init_connector() depends on this completing first, to
  11633. * prevent the registeration of both eDP and LVDS and the incorrect
  11634. * sharing of the PPS.
  11635. */
  11636. intel_lvds_init(dev_priv);
  11637. if (intel_crt_present(dev_priv))
  11638. intel_crt_init(dev_priv);
  11639. if (IS_GEN9_LP(dev_priv)) {
  11640. /*
  11641. * FIXME: Broxton doesn't support port detection via the
  11642. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11643. * detect the ports.
  11644. */
  11645. intel_ddi_init(dev_priv, PORT_A);
  11646. intel_ddi_init(dev_priv, PORT_B);
  11647. intel_ddi_init(dev_priv, PORT_C);
  11648. intel_dsi_init(dev_priv);
  11649. } else if (HAS_DDI(dev_priv)) {
  11650. int found;
  11651. /*
  11652. * Haswell uses DDI functions to detect digital outputs.
  11653. * On SKL pre-D0 the strap isn't connected, so we assume
  11654. * it's there.
  11655. */
  11656. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11657. /* WaIgnoreDDIAStrap: skl */
  11658. if (found || IS_GEN9_BC(dev_priv))
  11659. intel_ddi_init(dev_priv, PORT_A);
  11660. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11661. * register */
  11662. found = I915_READ(SFUSE_STRAP);
  11663. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11664. intel_ddi_init(dev_priv, PORT_B);
  11665. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11666. intel_ddi_init(dev_priv, PORT_C);
  11667. if (found & SFUSE_STRAP_DDID_DETECTED)
  11668. intel_ddi_init(dev_priv, PORT_D);
  11669. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11670. intel_ddi_init(dev_priv, PORT_F);
  11671. /*
  11672. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11673. */
  11674. if (IS_GEN9_BC(dev_priv) &&
  11675. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11676. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11677. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11678. intel_ddi_init(dev_priv, PORT_E);
  11679. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11680. int found;
  11681. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11682. if (has_edp_a(dev_priv))
  11683. intel_dp_init(dev_priv, DP_A, PORT_A);
  11684. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11685. /* PCH SDVOB multiplex with HDMIB */
  11686. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11687. if (!found)
  11688. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11689. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11690. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11691. }
  11692. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11693. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11694. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11695. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11696. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11697. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11698. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11699. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11700. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11701. bool has_edp, has_port;
  11702. /*
  11703. * The DP_DETECTED bit is the latched state of the DDC
  11704. * SDA pin at boot. However since eDP doesn't require DDC
  11705. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11706. * eDP ports may have been muxed to an alternate function.
  11707. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11708. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11709. * detect eDP ports.
  11710. *
  11711. * Sadly the straps seem to be missing sometimes even for HDMI
  11712. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11713. * and VBT for the presence of the port. Additionally we can't
  11714. * trust the port type the VBT declares as we've seen at least
  11715. * HDMI ports that the VBT claim are DP or eDP.
  11716. */
  11717. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11718. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11719. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11720. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11721. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11722. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11723. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11724. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11725. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11726. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11727. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11728. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11729. if (IS_CHERRYVIEW(dev_priv)) {
  11730. /*
  11731. * eDP not supported on port D,
  11732. * so no need to worry about it
  11733. */
  11734. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11735. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11736. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11737. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11738. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11739. }
  11740. intel_dsi_init(dev_priv);
  11741. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11742. bool found = false;
  11743. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11744. DRM_DEBUG_KMS("probing SDVOB\n");
  11745. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11746. if (!found && IS_G4X(dev_priv)) {
  11747. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11748. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11749. }
  11750. if (!found && IS_G4X(dev_priv))
  11751. intel_dp_init(dev_priv, DP_B, PORT_B);
  11752. }
  11753. /* Before G4X SDVOC doesn't have its own detect register */
  11754. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11755. DRM_DEBUG_KMS("probing SDVOC\n");
  11756. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11757. }
  11758. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11759. if (IS_G4X(dev_priv)) {
  11760. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11761. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11762. }
  11763. if (IS_G4X(dev_priv))
  11764. intel_dp_init(dev_priv, DP_C, PORT_C);
  11765. }
  11766. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11767. intel_dp_init(dev_priv, DP_D, PORT_D);
  11768. } else if (IS_GEN2(dev_priv))
  11769. intel_dvo_init(dev_priv);
  11770. if (SUPPORTS_TV(dev_priv))
  11771. intel_tv_init(dev_priv);
  11772. intel_psr_init(dev_priv);
  11773. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11774. encoder->base.possible_crtcs = encoder->crtc_mask;
  11775. encoder->base.possible_clones =
  11776. intel_encoder_clones(encoder);
  11777. }
  11778. intel_init_pch_refclk(dev_priv);
  11779. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11780. }
  11781. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11782. {
  11783. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11784. drm_framebuffer_cleanup(fb);
  11785. i915_gem_object_lock(intel_fb->obj);
  11786. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11787. i915_gem_object_unlock(intel_fb->obj);
  11788. i915_gem_object_put(intel_fb->obj);
  11789. kfree(intel_fb);
  11790. }
  11791. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11792. struct drm_file *file,
  11793. unsigned int *handle)
  11794. {
  11795. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11796. struct drm_i915_gem_object *obj = intel_fb->obj;
  11797. if (obj->userptr.mm) {
  11798. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11799. return -EINVAL;
  11800. }
  11801. return drm_gem_handle_create(file, &obj->base, handle);
  11802. }
  11803. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11804. struct drm_file *file,
  11805. unsigned flags, unsigned color,
  11806. struct drm_clip_rect *clips,
  11807. unsigned num_clips)
  11808. {
  11809. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11810. i915_gem_object_flush_if_display(obj);
  11811. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11812. return 0;
  11813. }
  11814. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11815. .destroy = intel_user_framebuffer_destroy,
  11816. .create_handle = intel_user_framebuffer_create_handle,
  11817. .dirty = intel_user_framebuffer_dirty,
  11818. };
  11819. static
  11820. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11821. uint64_t fb_modifier, uint32_t pixel_format)
  11822. {
  11823. u32 gen = INTEL_GEN(dev_priv);
  11824. if (gen >= 9) {
  11825. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11826. /* "The stride in bytes must not exceed the of the size of 8K
  11827. * pixels and 32K bytes."
  11828. */
  11829. return min(8192 * cpp, 32768);
  11830. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11831. return 32*1024;
  11832. } else if (gen >= 4) {
  11833. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11834. return 16*1024;
  11835. else
  11836. return 32*1024;
  11837. } else if (gen >= 3) {
  11838. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11839. return 8*1024;
  11840. else
  11841. return 16*1024;
  11842. } else {
  11843. /* XXX DSPC is limited to 4k tiled */
  11844. return 8*1024;
  11845. }
  11846. }
  11847. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11848. struct drm_i915_gem_object *obj,
  11849. struct drm_mode_fb_cmd2 *mode_cmd)
  11850. {
  11851. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11852. struct drm_framebuffer *fb = &intel_fb->base;
  11853. struct drm_format_name_buf format_name;
  11854. u32 pitch_limit;
  11855. unsigned int tiling, stride;
  11856. int ret = -EINVAL;
  11857. int i;
  11858. i915_gem_object_lock(obj);
  11859. obj->framebuffer_references++;
  11860. tiling = i915_gem_object_get_tiling(obj);
  11861. stride = i915_gem_object_get_stride(obj);
  11862. i915_gem_object_unlock(obj);
  11863. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11864. /*
  11865. * If there's a fence, enforce that
  11866. * the fb modifier and tiling mode match.
  11867. */
  11868. if (tiling != I915_TILING_NONE &&
  11869. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11870. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11871. goto err;
  11872. }
  11873. } else {
  11874. if (tiling == I915_TILING_X) {
  11875. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11876. } else if (tiling == I915_TILING_Y) {
  11877. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11878. goto err;
  11879. }
  11880. }
  11881. /* Passed in modifier sanity checking. */
  11882. switch (mode_cmd->modifier[0]) {
  11883. case I915_FORMAT_MOD_Y_TILED_CCS:
  11884. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11885. switch (mode_cmd->pixel_format) {
  11886. case DRM_FORMAT_XBGR8888:
  11887. case DRM_FORMAT_ABGR8888:
  11888. case DRM_FORMAT_XRGB8888:
  11889. case DRM_FORMAT_ARGB8888:
  11890. break;
  11891. default:
  11892. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11893. goto err;
  11894. }
  11895. /* fall through */
  11896. case I915_FORMAT_MOD_Y_TILED:
  11897. case I915_FORMAT_MOD_Yf_TILED:
  11898. if (INTEL_GEN(dev_priv) < 9) {
  11899. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11900. mode_cmd->modifier[0]);
  11901. goto err;
  11902. }
  11903. case DRM_FORMAT_MOD_LINEAR:
  11904. case I915_FORMAT_MOD_X_TILED:
  11905. break;
  11906. default:
  11907. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11908. mode_cmd->modifier[0]);
  11909. goto err;
  11910. }
  11911. /*
  11912. * gen2/3 display engine uses the fence if present,
  11913. * so the tiling mode must match the fb modifier exactly.
  11914. */
  11915. if (INTEL_GEN(dev_priv) < 4 &&
  11916. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11917. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11918. goto err;
  11919. }
  11920. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11921. mode_cmd->pixel_format);
  11922. if (mode_cmd->pitches[0] > pitch_limit) {
  11923. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11924. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11925. "tiled" : "linear",
  11926. mode_cmd->pitches[0], pitch_limit);
  11927. goto err;
  11928. }
  11929. /*
  11930. * If there's a fence, enforce that
  11931. * the fb pitch and fence stride match.
  11932. */
  11933. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11934. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11935. mode_cmd->pitches[0], stride);
  11936. goto err;
  11937. }
  11938. /* Reject formats not supported by any plane early. */
  11939. switch (mode_cmd->pixel_format) {
  11940. case DRM_FORMAT_C8:
  11941. case DRM_FORMAT_RGB565:
  11942. case DRM_FORMAT_XRGB8888:
  11943. case DRM_FORMAT_ARGB8888:
  11944. break;
  11945. case DRM_FORMAT_XRGB1555:
  11946. if (INTEL_GEN(dev_priv) > 3) {
  11947. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11948. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11949. goto err;
  11950. }
  11951. break;
  11952. case DRM_FORMAT_ABGR8888:
  11953. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11954. INTEL_GEN(dev_priv) < 9) {
  11955. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11956. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11957. goto err;
  11958. }
  11959. break;
  11960. case DRM_FORMAT_XBGR8888:
  11961. case DRM_FORMAT_XRGB2101010:
  11962. case DRM_FORMAT_XBGR2101010:
  11963. if (INTEL_GEN(dev_priv) < 4) {
  11964. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11965. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11966. goto err;
  11967. }
  11968. break;
  11969. case DRM_FORMAT_ABGR2101010:
  11970. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11971. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11972. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11973. goto err;
  11974. }
  11975. break;
  11976. case DRM_FORMAT_YUYV:
  11977. case DRM_FORMAT_UYVY:
  11978. case DRM_FORMAT_YVYU:
  11979. case DRM_FORMAT_VYUY:
  11980. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11981. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11982. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11983. goto err;
  11984. }
  11985. break;
  11986. case DRM_FORMAT_NV12:
  11987. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_Y_TILED_CCS ||
  11988. mode_cmd->modifier[0] == I915_FORMAT_MOD_Yf_TILED_CCS) {
  11989. DRM_DEBUG_KMS("RC not to be enabled with NV12\n");
  11990. goto err;
  11991. }
  11992. if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
  11993. IS_BROXTON(dev_priv)) {
  11994. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11995. drm_get_format_name(mode_cmd->pixel_format,
  11996. &format_name));
  11997. goto err;
  11998. }
  11999. break;
  12000. default:
  12001. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12002. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12003. goto err;
  12004. }
  12005. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12006. if (mode_cmd->offsets[0] != 0)
  12007. goto err;
  12008. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  12009. if (fb->format->format == DRM_FORMAT_NV12 &&
  12010. (fb->width < SKL_MIN_YUV_420_SRC_W ||
  12011. fb->height < SKL_MIN_YUV_420_SRC_H ||
  12012. (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
  12013. DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
  12014. return -EINVAL;
  12015. }
  12016. for (i = 0; i < fb->format->num_planes; i++) {
  12017. u32 stride_alignment;
  12018. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  12019. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  12020. goto err;
  12021. }
  12022. stride_alignment = intel_fb_stride_alignment(fb, i);
  12023. /*
  12024. * Display WA #0531: skl,bxt,kbl,glk
  12025. *
  12026. * Render decompression and plane width > 3840
  12027. * combined with horizontal panning requires the
  12028. * plane stride to be a multiple of 4. We'll just
  12029. * require the entire fb to accommodate that to avoid
  12030. * potential runtime errors at plane configuration time.
  12031. */
  12032. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  12033. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  12034. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  12035. stride_alignment *= 4;
  12036. if (fb->pitches[i] & (stride_alignment - 1)) {
  12037. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  12038. i, fb->pitches[i], stride_alignment);
  12039. goto err;
  12040. }
  12041. }
  12042. intel_fb->obj = obj;
  12043. ret = intel_fill_fb_info(dev_priv, fb);
  12044. if (ret)
  12045. goto err;
  12046. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  12047. if (ret) {
  12048. DRM_ERROR("framebuffer init failed %d\n", ret);
  12049. goto err;
  12050. }
  12051. return 0;
  12052. err:
  12053. i915_gem_object_lock(obj);
  12054. obj->framebuffer_references--;
  12055. i915_gem_object_unlock(obj);
  12056. return ret;
  12057. }
  12058. static struct drm_framebuffer *
  12059. intel_user_framebuffer_create(struct drm_device *dev,
  12060. struct drm_file *filp,
  12061. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12062. {
  12063. struct drm_framebuffer *fb;
  12064. struct drm_i915_gem_object *obj;
  12065. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12066. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12067. if (!obj)
  12068. return ERR_PTR(-ENOENT);
  12069. fb = intel_framebuffer_create(obj, &mode_cmd);
  12070. if (IS_ERR(fb))
  12071. i915_gem_object_put(obj);
  12072. return fb;
  12073. }
  12074. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12075. {
  12076. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12077. drm_atomic_state_default_release(state);
  12078. i915_sw_fence_fini(&intel_state->commit_ready);
  12079. kfree(state);
  12080. }
  12081. static enum drm_mode_status
  12082. intel_mode_valid(struct drm_device *dev,
  12083. const struct drm_display_mode *mode)
  12084. {
  12085. if (mode->vscan > 1)
  12086. return MODE_NO_VSCAN;
  12087. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  12088. return MODE_NO_DBLESCAN;
  12089. if (mode->flags & DRM_MODE_FLAG_HSKEW)
  12090. return MODE_H_ILLEGAL;
  12091. if (mode->flags & (DRM_MODE_FLAG_CSYNC |
  12092. DRM_MODE_FLAG_NCSYNC |
  12093. DRM_MODE_FLAG_PCSYNC))
  12094. return MODE_HSYNC;
  12095. if (mode->flags & (DRM_MODE_FLAG_BCAST |
  12096. DRM_MODE_FLAG_PIXMUX |
  12097. DRM_MODE_FLAG_CLKDIV2))
  12098. return MODE_BAD;
  12099. return MODE_OK;
  12100. }
  12101. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12102. .fb_create = intel_user_framebuffer_create,
  12103. .get_format_info = intel_get_format_info,
  12104. .output_poll_changed = intel_fbdev_output_poll_changed,
  12105. .mode_valid = intel_mode_valid,
  12106. .atomic_check = intel_atomic_check,
  12107. .atomic_commit = intel_atomic_commit,
  12108. .atomic_state_alloc = intel_atomic_state_alloc,
  12109. .atomic_state_clear = intel_atomic_state_clear,
  12110. .atomic_state_free = intel_atomic_state_free,
  12111. };
  12112. /**
  12113. * intel_init_display_hooks - initialize the display modesetting hooks
  12114. * @dev_priv: device private
  12115. */
  12116. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12117. {
  12118. intel_init_cdclk_hooks(dev_priv);
  12119. if (INTEL_GEN(dev_priv) >= 9) {
  12120. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12121. dev_priv->display.get_initial_plane_config =
  12122. skylake_get_initial_plane_config;
  12123. dev_priv->display.crtc_compute_clock =
  12124. haswell_crtc_compute_clock;
  12125. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12126. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12127. } else if (HAS_DDI(dev_priv)) {
  12128. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12129. dev_priv->display.get_initial_plane_config =
  12130. i9xx_get_initial_plane_config;
  12131. dev_priv->display.crtc_compute_clock =
  12132. haswell_crtc_compute_clock;
  12133. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12134. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12135. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12136. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12137. dev_priv->display.get_initial_plane_config =
  12138. i9xx_get_initial_plane_config;
  12139. dev_priv->display.crtc_compute_clock =
  12140. ironlake_crtc_compute_clock;
  12141. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12142. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12143. } else if (IS_CHERRYVIEW(dev_priv)) {
  12144. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12145. dev_priv->display.get_initial_plane_config =
  12146. i9xx_get_initial_plane_config;
  12147. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12148. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12149. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12150. } else if (IS_VALLEYVIEW(dev_priv)) {
  12151. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12152. dev_priv->display.get_initial_plane_config =
  12153. i9xx_get_initial_plane_config;
  12154. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12155. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12156. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12157. } else if (IS_G4X(dev_priv)) {
  12158. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12159. dev_priv->display.get_initial_plane_config =
  12160. i9xx_get_initial_plane_config;
  12161. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12162. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12163. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12164. } else if (IS_PINEVIEW(dev_priv)) {
  12165. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12166. dev_priv->display.get_initial_plane_config =
  12167. i9xx_get_initial_plane_config;
  12168. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12169. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12170. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12171. } else if (!IS_GEN2(dev_priv)) {
  12172. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12173. dev_priv->display.get_initial_plane_config =
  12174. i9xx_get_initial_plane_config;
  12175. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12176. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12177. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12178. } else {
  12179. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12180. dev_priv->display.get_initial_plane_config =
  12181. i9xx_get_initial_plane_config;
  12182. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12183. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12184. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12185. }
  12186. if (IS_GEN5(dev_priv)) {
  12187. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12188. } else if (IS_GEN6(dev_priv)) {
  12189. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12190. } else if (IS_IVYBRIDGE(dev_priv)) {
  12191. /* FIXME: detect B0+ stepping and use auto training */
  12192. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12193. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12194. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12195. }
  12196. if (INTEL_GEN(dev_priv) >= 9)
  12197. dev_priv->display.update_crtcs = skl_update_crtcs;
  12198. else
  12199. dev_priv->display.update_crtcs = intel_update_crtcs;
  12200. }
  12201. /*
  12202. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12203. */
  12204. static void quirk_ssc_force_disable(struct drm_device *dev)
  12205. {
  12206. struct drm_i915_private *dev_priv = to_i915(dev);
  12207. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12208. DRM_INFO("applying lvds SSC disable quirk\n");
  12209. }
  12210. /*
  12211. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12212. * brightness value
  12213. */
  12214. static void quirk_invert_brightness(struct drm_device *dev)
  12215. {
  12216. struct drm_i915_private *dev_priv = to_i915(dev);
  12217. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12218. DRM_INFO("applying inverted panel brightness quirk\n");
  12219. }
  12220. /* Some VBT's incorrectly indicate no backlight is present */
  12221. static void quirk_backlight_present(struct drm_device *dev)
  12222. {
  12223. struct drm_i915_private *dev_priv = to_i915(dev);
  12224. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12225. DRM_INFO("applying backlight present quirk\n");
  12226. }
  12227. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  12228. * which is 300 ms greater than eDP spec T12 min.
  12229. */
  12230. static void quirk_increase_t12_delay(struct drm_device *dev)
  12231. {
  12232. struct drm_i915_private *dev_priv = to_i915(dev);
  12233. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  12234. DRM_INFO("Applying T12 delay quirk\n");
  12235. }
  12236. struct intel_quirk {
  12237. int device;
  12238. int subsystem_vendor;
  12239. int subsystem_device;
  12240. void (*hook)(struct drm_device *dev);
  12241. };
  12242. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12243. struct intel_dmi_quirk {
  12244. void (*hook)(struct drm_device *dev);
  12245. const struct dmi_system_id (*dmi_id_list)[];
  12246. };
  12247. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12248. {
  12249. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12250. return 1;
  12251. }
  12252. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12253. {
  12254. .dmi_id_list = &(const struct dmi_system_id[]) {
  12255. {
  12256. .callback = intel_dmi_reverse_brightness,
  12257. .ident = "NCR Corporation",
  12258. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12259. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12260. },
  12261. },
  12262. { } /* terminating entry */
  12263. },
  12264. .hook = quirk_invert_brightness,
  12265. },
  12266. };
  12267. static struct intel_quirk intel_quirks[] = {
  12268. /* Lenovo U160 cannot use SSC on LVDS */
  12269. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12270. /* Sony Vaio Y cannot use SSC on LVDS */
  12271. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12272. /* Acer Aspire 5734Z must invert backlight brightness */
  12273. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12274. /* Acer/eMachines G725 */
  12275. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12276. /* Acer/eMachines e725 */
  12277. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12278. /* Acer/Packard Bell NCL20 */
  12279. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12280. /* Acer Aspire 4736Z */
  12281. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12282. /* Acer Aspire 5336 */
  12283. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12284. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12285. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12286. /* Acer C720 Chromebook (Core i3 4005U) */
  12287. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12288. /* Apple Macbook 2,1 (Core 2 T7400) */
  12289. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12290. /* Apple Macbook 4,1 */
  12291. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12292. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12293. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12294. /* HP Chromebook 14 (Celeron 2955U) */
  12295. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12296. /* Dell Chromebook 11 */
  12297. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12298. /* Dell Chromebook 11 (2015 version) */
  12299. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12300. /* Toshiba Satellite P50-C-18C */
  12301. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12302. };
  12303. static void intel_init_quirks(struct drm_device *dev)
  12304. {
  12305. struct pci_dev *d = dev->pdev;
  12306. int i;
  12307. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12308. struct intel_quirk *q = &intel_quirks[i];
  12309. if (d->device == q->device &&
  12310. (d->subsystem_vendor == q->subsystem_vendor ||
  12311. q->subsystem_vendor == PCI_ANY_ID) &&
  12312. (d->subsystem_device == q->subsystem_device ||
  12313. q->subsystem_device == PCI_ANY_ID))
  12314. q->hook(dev);
  12315. }
  12316. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12317. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12318. intel_dmi_quirks[i].hook(dev);
  12319. }
  12320. }
  12321. /* Disable the VGA plane that we never use */
  12322. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12323. {
  12324. struct pci_dev *pdev = dev_priv->drm.pdev;
  12325. u8 sr1;
  12326. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12327. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12328. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12329. outb(SR01, VGA_SR_INDEX);
  12330. sr1 = inb(VGA_SR_DATA);
  12331. outb(sr1 | 1<<5, VGA_SR_DATA);
  12332. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12333. udelay(300);
  12334. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12335. POSTING_READ(vga_reg);
  12336. }
  12337. void intel_modeset_init_hw(struct drm_device *dev)
  12338. {
  12339. struct drm_i915_private *dev_priv = to_i915(dev);
  12340. intel_update_cdclk(dev_priv);
  12341. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12342. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12343. }
  12344. /*
  12345. * Calculate what we think the watermarks should be for the state we've read
  12346. * out of the hardware and then immediately program those watermarks so that
  12347. * we ensure the hardware settings match our internal state.
  12348. *
  12349. * We can calculate what we think WM's should be by creating a duplicate of the
  12350. * current state (which was constructed during hardware readout) and running it
  12351. * through the atomic check code to calculate new watermark values in the
  12352. * state object.
  12353. */
  12354. static void sanitize_watermarks(struct drm_device *dev)
  12355. {
  12356. struct drm_i915_private *dev_priv = to_i915(dev);
  12357. struct drm_atomic_state *state;
  12358. struct intel_atomic_state *intel_state;
  12359. struct drm_crtc *crtc;
  12360. struct drm_crtc_state *cstate;
  12361. struct drm_modeset_acquire_ctx ctx;
  12362. int ret;
  12363. int i;
  12364. /* Only supported on platforms that use atomic watermark design */
  12365. if (!dev_priv->display.optimize_watermarks)
  12366. return;
  12367. /*
  12368. * We need to hold connection_mutex before calling duplicate_state so
  12369. * that the connector loop is protected.
  12370. */
  12371. drm_modeset_acquire_init(&ctx, 0);
  12372. retry:
  12373. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12374. if (ret == -EDEADLK) {
  12375. drm_modeset_backoff(&ctx);
  12376. goto retry;
  12377. } else if (WARN_ON(ret)) {
  12378. goto fail;
  12379. }
  12380. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12381. if (WARN_ON(IS_ERR(state)))
  12382. goto fail;
  12383. intel_state = to_intel_atomic_state(state);
  12384. /*
  12385. * Hardware readout is the only time we don't want to calculate
  12386. * intermediate watermarks (since we don't trust the current
  12387. * watermarks).
  12388. */
  12389. if (!HAS_GMCH_DISPLAY(dev_priv))
  12390. intel_state->skip_intermediate_wm = true;
  12391. ret = intel_atomic_check(dev, state);
  12392. if (ret) {
  12393. /*
  12394. * If we fail here, it means that the hardware appears to be
  12395. * programmed in a way that shouldn't be possible, given our
  12396. * understanding of watermark requirements. This might mean a
  12397. * mistake in the hardware readout code or a mistake in the
  12398. * watermark calculations for a given platform. Raise a WARN
  12399. * so that this is noticeable.
  12400. *
  12401. * If this actually happens, we'll have to just leave the
  12402. * BIOS-programmed watermarks untouched and hope for the best.
  12403. */
  12404. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12405. goto put_state;
  12406. }
  12407. /* Write calculated watermark values back */
  12408. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12409. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12410. cs->wm.need_postvbl_update = true;
  12411. dev_priv->display.optimize_watermarks(intel_state, cs);
  12412. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12413. }
  12414. put_state:
  12415. drm_atomic_state_put(state);
  12416. fail:
  12417. drm_modeset_drop_locks(&ctx);
  12418. drm_modeset_acquire_fini(&ctx);
  12419. }
  12420. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12421. {
  12422. if (IS_GEN5(dev_priv)) {
  12423. u32 fdi_pll_clk =
  12424. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12425. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12426. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12427. dev_priv->fdi_pll_freq = 270000;
  12428. } else {
  12429. return;
  12430. }
  12431. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12432. }
  12433. int intel_modeset_init(struct drm_device *dev)
  12434. {
  12435. struct drm_i915_private *dev_priv = to_i915(dev);
  12436. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12437. enum pipe pipe;
  12438. struct intel_crtc *crtc;
  12439. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12440. drm_mode_config_init(dev);
  12441. dev->mode_config.min_width = 0;
  12442. dev->mode_config.min_height = 0;
  12443. dev->mode_config.preferred_depth = 24;
  12444. dev->mode_config.prefer_shadow = 1;
  12445. dev->mode_config.allow_fb_modifiers = true;
  12446. dev->mode_config.funcs = &intel_mode_funcs;
  12447. init_llist_head(&dev_priv->atomic_helper.free_list);
  12448. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12449. intel_atomic_helper_free_state_worker);
  12450. intel_init_quirks(dev);
  12451. intel_init_pm(dev_priv);
  12452. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12453. return 0;
  12454. /*
  12455. * There may be no VBT; and if the BIOS enabled SSC we can
  12456. * just keep using it to avoid unnecessary flicker. Whereas if the
  12457. * BIOS isn't using it, don't assume it will work even if the VBT
  12458. * indicates as much.
  12459. */
  12460. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12461. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12462. DREF_SSC1_ENABLE);
  12463. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12464. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12465. bios_lvds_use_ssc ? "en" : "dis",
  12466. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12467. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12468. }
  12469. }
  12470. if (IS_GEN2(dev_priv)) {
  12471. dev->mode_config.max_width = 2048;
  12472. dev->mode_config.max_height = 2048;
  12473. } else if (IS_GEN3(dev_priv)) {
  12474. dev->mode_config.max_width = 4096;
  12475. dev->mode_config.max_height = 4096;
  12476. } else {
  12477. dev->mode_config.max_width = 8192;
  12478. dev->mode_config.max_height = 8192;
  12479. }
  12480. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12481. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12482. dev->mode_config.cursor_height = 1023;
  12483. } else if (IS_GEN2(dev_priv)) {
  12484. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12485. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12486. } else {
  12487. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12488. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12489. }
  12490. dev->mode_config.fb_base = ggtt->gmadr.start;
  12491. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12492. INTEL_INFO(dev_priv)->num_pipes,
  12493. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12494. for_each_pipe(dev_priv, pipe) {
  12495. int ret;
  12496. ret = intel_crtc_init(dev_priv, pipe);
  12497. if (ret) {
  12498. drm_mode_config_cleanup(dev);
  12499. return ret;
  12500. }
  12501. }
  12502. intel_shared_dpll_init(dev);
  12503. intel_update_fdi_pll_freq(dev_priv);
  12504. intel_update_czclk(dev_priv);
  12505. intel_modeset_init_hw(dev);
  12506. if (dev_priv->max_cdclk_freq == 0)
  12507. intel_update_max_cdclk(dev_priv);
  12508. /* Just disable it once at startup */
  12509. i915_disable_vga(dev_priv);
  12510. intel_setup_outputs(dev_priv);
  12511. drm_modeset_lock_all(dev);
  12512. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12513. drm_modeset_unlock_all(dev);
  12514. for_each_intel_crtc(dev, crtc) {
  12515. struct intel_initial_plane_config plane_config = {};
  12516. if (!crtc->active)
  12517. continue;
  12518. /*
  12519. * Note that reserving the BIOS fb up front prevents us
  12520. * from stuffing other stolen allocations like the ring
  12521. * on top. This prevents some ugliness at boot time, and
  12522. * can even allow for smooth boot transitions if the BIOS
  12523. * fb is large enough for the active pipe configuration.
  12524. */
  12525. dev_priv->display.get_initial_plane_config(crtc,
  12526. &plane_config);
  12527. /*
  12528. * If the fb is shared between multiple heads, we'll
  12529. * just get the first one.
  12530. */
  12531. intel_find_initial_plane_obj(crtc, &plane_config);
  12532. }
  12533. /*
  12534. * Make sure hardware watermarks really match the state we read out.
  12535. * Note that we need to do this after reconstructing the BIOS fb's
  12536. * since the watermark calculation done here will use pstate->fb.
  12537. */
  12538. if (!HAS_GMCH_DISPLAY(dev_priv))
  12539. sanitize_watermarks(dev);
  12540. return 0;
  12541. }
  12542. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12543. {
  12544. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12545. /* 640x480@60Hz, ~25175 kHz */
  12546. struct dpll clock = {
  12547. .m1 = 18,
  12548. .m2 = 7,
  12549. .p1 = 13,
  12550. .p2 = 4,
  12551. .n = 2,
  12552. };
  12553. u32 dpll, fp;
  12554. int i;
  12555. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12556. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12557. pipe_name(pipe), clock.vco, clock.dot);
  12558. fp = i9xx_dpll_compute_fp(&clock);
  12559. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12560. DPLL_VGA_MODE_DIS |
  12561. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12562. PLL_P2_DIVIDE_BY_4 |
  12563. PLL_REF_INPUT_DREFCLK |
  12564. DPLL_VCO_ENABLE;
  12565. I915_WRITE(FP0(pipe), fp);
  12566. I915_WRITE(FP1(pipe), fp);
  12567. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12568. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12569. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12570. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12571. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12572. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12573. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12574. /*
  12575. * Apparently we need to have VGA mode enabled prior to changing
  12576. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12577. * dividers, even though the register value does change.
  12578. */
  12579. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12580. I915_WRITE(DPLL(pipe), dpll);
  12581. /* Wait for the clocks to stabilize. */
  12582. POSTING_READ(DPLL(pipe));
  12583. udelay(150);
  12584. /* The pixel multiplier can only be updated once the
  12585. * DPLL is enabled and the clocks are stable.
  12586. *
  12587. * So write it again.
  12588. */
  12589. I915_WRITE(DPLL(pipe), dpll);
  12590. /* We do this three times for luck */
  12591. for (i = 0; i < 3 ; i++) {
  12592. I915_WRITE(DPLL(pipe), dpll);
  12593. POSTING_READ(DPLL(pipe));
  12594. udelay(150); /* wait for warmup */
  12595. }
  12596. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12597. POSTING_READ(PIPECONF(pipe));
  12598. intel_wait_for_pipe_scanline_moving(crtc);
  12599. }
  12600. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12601. {
  12602. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12603. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12604. pipe_name(pipe));
  12605. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12606. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12607. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12608. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
  12609. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
  12610. I915_WRITE(PIPECONF(pipe), 0);
  12611. POSTING_READ(PIPECONF(pipe));
  12612. intel_wait_for_pipe_scanline_stopped(crtc);
  12613. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12614. POSTING_READ(DPLL(pipe));
  12615. }
  12616. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12617. struct intel_plane *plane)
  12618. {
  12619. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12620. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  12621. u32 val = I915_READ(DSPCNTR(i9xx_plane));
  12622. return (val & DISPLAY_PLANE_ENABLE) == 0 ||
  12623. (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
  12624. }
  12625. static void
  12626. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12627. {
  12628. struct intel_crtc *crtc;
  12629. if (INTEL_GEN(dev_priv) >= 4)
  12630. return;
  12631. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12632. struct intel_plane *plane =
  12633. to_intel_plane(crtc->base.primary);
  12634. if (intel_plane_mapping_ok(crtc, plane))
  12635. continue;
  12636. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12637. plane->base.name);
  12638. intel_plane_disable_noatomic(crtc, plane);
  12639. }
  12640. }
  12641. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12642. {
  12643. struct drm_device *dev = crtc->base.dev;
  12644. struct intel_encoder *encoder;
  12645. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12646. return true;
  12647. return false;
  12648. }
  12649. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12650. {
  12651. struct drm_device *dev = encoder->base.dev;
  12652. struct intel_connector *connector;
  12653. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12654. return connector;
  12655. return NULL;
  12656. }
  12657. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12658. enum pipe pch_transcoder)
  12659. {
  12660. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12661. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12662. }
  12663. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12664. struct drm_modeset_acquire_ctx *ctx)
  12665. {
  12666. struct drm_device *dev = crtc->base.dev;
  12667. struct drm_i915_private *dev_priv = to_i915(dev);
  12668. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12669. /* Clear any frame start delays used for debugging left by the BIOS */
  12670. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12671. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12672. I915_WRITE(reg,
  12673. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12674. }
  12675. /* restore vblank interrupts to correct state */
  12676. drm_crtc_vblank_reset(&crtc->base);
  12677. if (crtc->active) {
  12678. struct intel_plane *plane;
  12679. drm_crtc_vblank_on(&crtc->base);
  12680. /* Disable everything but the primary plane */
  12681. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12682. const struct intel_plane_state *plane_state =
  12683. to_intel_plane_state(plane->base.state);
  12684. if (plane_state->base.visible &&
  12685. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12686. intel_plane_disable_noatomic(crtc, plane);
  12687. }
  12688. }
  12689. /* Adjust the state of the output pipe according to whether we
  12690. * have active connectors/encoders. */
  12691. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12692. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12693. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12694. /*
  12695. * We start out with underrun reporting disabled to avoid races.
  12696. * For correct bookkeeping mark this on active crtcs.
  12697. *
  12698. * Also on gmch platforms we dont have any hardware bits to
  12699. * disable the underrun reporting. Which means we need to start
  12700. * out with underrun reporting disabled also on inactive pipes,
  12701. * since otherwise we'll complain about the garbage we read when
  12702. * e.g. coming up after runtime pm.
  12703. *
  12704. * No protection against concurrent access is required - at
  12705. * worst a fifo underrun happens which also sets this to false.
  12706. */
  12707. crtc->cpu_fifo_underrun_disabled = true;
  12708. /*
  12709. * We track the PCH trancoder underrun reporting state
  12710. * within the crtc. With crtc for pipe A housing the underrun
  12711. * reporting state for PCH transcoder A, crtc for pipe B housing
  12712. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12713. * and marking underrun reporting as disabled for the non-existing
  12714. * PCH transcoders B and C would prevent enabling the south
  12715. * error interrupt (see cpt_can_enable_serr_int()).
  12716. */
  12717. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12718. crtc->pch_fifo_underrun_disabled = true;
  12719. }
  12720. }
  12721. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12722. {
  12723. struct intel_connector *connector;
  12724. /* We need to check both for a crtc link (meaning that the
  12725. * encoder is active and trying to read from a pipe) and the
  12726. * pipe itself being active. */
  12727. bool has_active_crtc = encoder->base.crtc &&
  12728. to_intel_crtc(encoder->base.crtc)->active;
  12729. connector = intel_encoder_find_connector(encoder);
  12730. if (connector && !has_active_crtc) {
  12731. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12732. encoder->base.base.id,
  12733. encoder->base.name);
  12734. /* Connector is active, but has no active pipe. This is
  12735. * fallout from our resume register restoring. Disable
  12736. * the encoder manually again. */
  12737. if (encoder->base.crtc) {
  12738. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12739. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12740. encoder->base.base.id,
  12741. encoder->base.name);
  12742. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12743. if (encoder->post_disable)
  12744. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12745. }
  12746. encoder->base.crtc = NULL;
  12747. /* Inconsistent output/port/pipe state happens presumably due to
  12748. * a bug in one of the get_hw_state functions. Or someplace else
  12749. * in our code, like the register restore mess on resume. Clamp
  12750. * things to off as a safer default. */
  12751. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12752. connector->base.encoder = NULL;
  12753. }
  12754. }
  12755. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12756. {
  12757. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12758. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12759. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12760. i915_disable_vga(dev_priv);
  12761. }
  12762. }
  12763. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12764. {
  12765. /* This function can be called both from intel_modeset_setup_hw_state or
  12766. * at a very early point in our resume sequence, where the power well
  12767. * structures are not yet restored. Since this function is at a very
  12768. * paranoid "someone might have enabled VGA while we were not looking"
  12769. * level, just check if the power well is enabled instead of trying to
  12770. * follow the "don't touch the power well if we don't need it" policy
  12771. * the rest of the driver uses. */
  12772. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12773. return;
  12774. i915_redisable_vga_power_on(dev_priv);
  12775. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12776. }
  12777. /* FIXME read out full plane state for all planes */
  12778. static void readout_plane_state(struct intel_crtc *crtc)
  12779. {
  12780. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12781. struct intel_crtc_state *crtc_state =
  12782. to_intel_crtc_state(crtc->base.state);
  12783. struct intel_plane *plane;
  12784. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12785. struct intel_plane_state *plane_state =
  12786. to_intel_plane_state(plane->base.state);
  12787. bool visible = plane->get_hw_state(plane);
  12788. intel_set_plane_visible(crtc_state, plane_state, visible);
  12789. }
  12790. }
  12791. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12792. {
  12793. struct drm_i915_private *dev_priv = to_i915(dev);
  12794. enum pipe pipe;
  12795. struct intel_crtc *crtc;
  12796. struct intel_encoder *encoder;
  12797. struct intel_connector *connector;
  12798. struct drm_connector_list_iter conn_iter;
  12799. int i;
  12800. dev_priv->active_crtcs = 0;
  12801. for_each_intel_crtc(dev, crtc) {
  12802. struct intel_crtc_state *crtc_state =
  12803. to_intel_crtc_state(crtc->base.state);
  12804. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12805. memset(crtc_state, 0, sizeof(*crtc_state));
  12806. crtc_state->base.crtc = &crtc->base;
  12807. crtc_state->base.active = crtc_state->base.enable =
  12808. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12809. crtc->base.enabled = crtc_state->base.enable;
  12810. crtc->active = crtc_state->base.active;
  12811. if (crtc_state->base.active)
  12812. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12813. readout_plane_state(crtc);
  12814. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12815. crtc->base.base.id, crtc->base.name,
  12816. enableddisabled(crtc_state->base.active));
  12817. }
  12818. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12819. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12820. pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
  12821. &pll->state.hw_state);
  12822. pll->state.crtc_mask = 0;
  12823. for_each_intel_crtc(dev, crtc) {
  12824. struct intel_crtc_state *crtc_state =
  12825. to_intel_crtc_state(crtc->base.state);
  12826. if (crtc_state->base.active &&
  12827. crtc_state->shared_dpll == pll)
  12828. pll->state.crtc_mask |= 1 << crtc->pipe;
  12829. }
  12830. pll->active_mask = pll->state.crtc_mask;
  12831. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12832. pll->info->name, pll->state.crtc_mask, pll->on);
  12833. }
  12834. for_each_intel_encoder(dev, encoder) {
  12835. pipe = 0;
  12836. if (encoder->get_hw_state(encoder, &pipe)) {
  12837. struct intel_crtc_state *crtc_state;
  12838. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12839. crtc_state = to_intel_crtc_state(crtc->base.state);
  12840. encoder->base.crtc = &crtc->base;
  12841. encoder->get_config(encoder, crtc_state);
  12842. } else {
  12843. encoder->base.crtc = NULL;
  12844. }
  12845. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12846. encoder->base.base.id, encoder->base.name,
  12847. enableddisabled(encoder->base.crtc),
  12848. pipe_name(pipe));
  12849. }
  12850. drm_connector_list_iter_begin(dev, &conn_iter);
  12851. for_each_intel_connector_iter(connector, &conn_iter) {
  12852. if (connector->get_hw_state(connector)) {
  12853. connector->base.dpms = DRM_MODE_DPMS_ON;
  12854. encoder = connector->encoder;
  12855. connector->base.encoder = &encoder->base;
  12856. if (encoder->base.crtc &&
  12857. encoder->base.crtc->state->active) {
  12858. /*
  12859. * This has to be done during hardware readout
  12860. * because anything calling .crtc_disable may
  12861. * rely on the connector_mask being accurate.
  12862. */
  12863. encoder->base.crtc->state->connector_mask |=
  12864. 1 << drm_connector_index(&connector->base);
  12865. encoder->base.crtc->state->encoder_mask |=
  12866. 1 << drm_encoder_index(&encoder->base);
  12867. }
  12868. } else {
  12869. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12870. connector->base.encoder = NULL;
  12871. }
  12872. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12873. connector->base.base.id, connector->base.name,
  12874. enableddisabled(connector->base.encoder));
  12875. }
  12876. drm_connector_list_iter_end(&conn_iter);
  12877. for_each_intel_crtc(dev, crtc) {
  12878. struct intel_crtc_state *crtc_state =
  12879. to_intel_crtc_state(crtc->base.state);
  12880. int min_cdclk = 0;
  12881. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12882. if (crtc_state->base.active) {
  12883. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12884. crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
  12885. crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
  12886. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12887. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12888. /*
  12889. * The initial mode needs to be set in order to keep
  12890. * the atomic core happy. It wants a valid mode if the
  12891. * crtc's enabled, so we do the above call.
  12892. *
  12893. * But we don't set all the derived state fully, hence
  12894. * set a flag to indicate that a full recalculation is
  12895. * needed on the next commit.
  12896. */
  12897. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12898. intel_crtc_compute_pixel_rate(crtc_state);
  12899. if (dev_priv->display.modeset_calc_cdclk) {
  12900. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12901. if (WARN_ON(min_cdclk < 0))
  12902. min_cdclk = 0;
  12903. }
  12904. drm_calc_timestamping_constants(&crtc->base,
  12905. &crtc_state->base.adjusted_mode);
  12906. update_scanline_offset(crtc);
  12907. }
  12908. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12909. dev_priv->min_voltage_level[crtc->pipe] =
  12910. crtc_state->min_voltage_level;
  12911. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12912. }
  12913. }
  12914. static void
  12915. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12916. {
  12917. struct intel_encoder *encoder;
  12918. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12919. u64 get_domains;
  12920. enum intel_display_power_domain domain;
  12921. if (!encoder->get_power_domains)
  12922. continue;
  12923. get_domains = encoder->get_power_domains(encoder);
  12924. for_each_power_domain(domain, get_domains)
  12925. intel_display_power_get(dev_priv, domain);
  12926. }
  12927. }
  12928. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12929. {
  12930. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12931. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12932. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12933. DARBF_GATING_DIS);
  12934. if (IS_HASWELL(dev_priv)) {
  12935. /*
  12936. * WaRsPkgCStateDisplayPMReq:hsw
  12937. * System hang if this isn't done before disabling all planes!
  12938. */
  12939. I915_WRITE(CHICKEN_PAR1_1,
  12940. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12941. }
  12942. }
  12943. /* Scan out the current hw modeset state,
  12944. * and sanitizes it to the current state
  12945. */
  12946. static void
  12947. intel_modeset_setup_hw_state(struct drm_device *dev,
  12948. struct drm_modeset_acquire_ctx *ctx)
  12949. {
  12950. struct drm_i915_private *dev_priv = to_i915(dev);
  12951. enum pipe pipe;
  12952. struct intel_crtc *crtc;
  12953. struct intel_encoder *encoder;
  12954. int i;
  12955. intel_early_display_was(dev_priv);
  12956. intel_modeset_readout_hw_state(dev);
  12957. /* HW state is read out, now we need to sanitize this mess. */
  12958. get_encoder_power_domains(dev_priv);
  12959. intel_sanitize_plane_mapping(dev_priv);
  12960. for_each_intel_encoder(dev, encoder) {
  12961. intel_sanitize_encoder(encoder);
  12962. }
  12963. for_each_pipe(dev_priv, pipe) {
  12964. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12965. intel_sanitize_crtc(crtc, ctx);
  12966. intel_dump_pipe_config(crtc, crtc->config,
  12967. "[setup_hw_state]");
  12968. }
  12969. intel_modeset_update_connector_atomic_state(dev);
  12970. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12971. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12972. if (!pll->on || pll->active_mask)
  12973. continue;
  12974. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
  12975. pll->info->name);
  12976. pll->info->funcs->disable(dev_priv, pll);
  12977. pll->on = false;
  12978. }
  12979. if (IS_G4X(dev_priv)) {
  12980. g4x_wm_get_hw_state(dev);
  12981. g4x_wm_sanitize(dev_priv);
  12982. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12983. vlv_wm_get_hw_state(dev);
  12984. vlv_wm_sanitize(dev_priv);
  12985. } else if (INTEL_GEN(dev_priv) >= 9) {
  12986. skl_wm_get_hw_state(dev);
  12987. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12988. ilk_wm_get_hw_state(dev);
  12989. }
  12990. for_each_intel_crtc(dev, crtc) {
  12991. u64 put_domains;
  12992. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12993. if (WARN_ON(put_domains))
  12994. modeset_put_power_domains(dev_priv, put_domains);
  12995. }
  12996. intel_display_set_init_power(dev_priv, false);
  12997. intel_power_domains_verify_state(dev_priv);
  12998. intel_fbc_init_pipe_state(dev_priv);
  12999. }
  13000. void intel_display_resume(struct drm_device *dev)
  13001. {
  13002. struct drm_i915_private *dev_priv = to_i915(dev);
  13003. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13004. struct drm_modeset_acquire_ctx ctx;
  13005. int ret;
  13006. dev_priv->modeset_restore_state = NULL;
  13007. if (state)
  13008. state->acquire_ctx = &ctx;
  13009. drm_modeset_acquire_init(&ctx, 0);
  13010. while (1) {
  13011. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13012. if (ret != -EDEADLK)
  13013. break;
  13014. drm_modeset_backoff(&ctx);
  13015. }
  13016. if (!ret)
  13017. ret = __intel_display_resume(dev, state, &ctx);
  13018. intel_enable_ipc(dev_priv);
  13019. drm_modeset_drop_locks(&ctx);
  13020. drm_modeset_acquire_fini(&ctx);
  13021. if (ret)
  13022. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13023. if (state)
  13024. drm_atomic_state_put(state);
  13025. }
  13026. int intel_connector_register(struct drm_connector *connector)
  13027. {
  13028. struct intel_connector *intel_connector = to_intel_connector(connector);
  13029. int ret;
  13030. ret = intel_backlight_device_register(intel_connector);
  13031. if (ret)
  13032. goto err;
  13033. return 0;
  13034. err:
  13035. return ret;
  13036. }
  13037. void intel_connector_unregister(struct drm_connector *connector)
  13038. {
  13039. struct intel_connector *intel_connector = to_intel_connector(connector);
  13040. intel_backlight_device_unregister(intel_connector);
  13041. intel_panel_destroy_backlight(connector);
  13042. }
  13043. static void intel_hpd_poll_fini(struct drm_device *dev)
  13044. {
  13045. struct intel_connector *connector;
  13046. struct drm_connector_list_iter conn_iter;
  13047. /* Kill all the work that may have been queued by hpd. */
  13048. drm_connector_list_iter_begin(dev, &conn_iter);
  13049. for_each_intel_connector_iter(connector, &conn_iter) {
  13050. if (connector->modeset_retry_work.func)
  13051. cancel_work_sync(&connector->modeset_retry_work);
  13052. if (connector->hdcp_shim) {
  13053. cancel_delayed_work_sync(&connector->hdcp_check_work);
  13054. cancel_work_sync(&connector->hdcp_prop_work);
  13055. }
  13056. }
  13057. drm_connector_list_iter_end(&conn_iter);
  13058. }
  13059. void intel_modeset_cleanup(struct drm_device *dev)
  13060. {
  13061. struct drm_i915_private *dev_priv = to_i915(dev);
  13062. flush_work(&dev_priv->atomic_helper.free_work);
  13063. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13064. intel_disable_gt_powersave(dev_priv);
  13065. /*
  13066. * Interrupts and polling as the first thing to avoid creating havoc.
  13067. * Too much stuff here (turning of connectors, ...) would
  13068. * experience fancy races otherwise.
  13069. */
  13070. intel_irq_uninstall(dev_priv);
  13071. /*
  13072. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13073. * poll handlers. Hence disable polling after hpd handling is shut down.
  13074. */
  13075. intel_hpd_poll_fini(dev);
  13076. /* poll work can call into fbdev, hence clean that up afterwards */
  13077. intel_fbdev_fini(dev_priv);
  13078. intel_unregister_dsm_handler();
  13079. intel_fbc_global_disable(dev_priv);
  13080. /* flush any delayed tasks or pending work */
  13081. flush_scheduled_work();
  13082. drm_mode_config_cleanup(dev);
  13083. intel_cleanup_overlay(dev_priv);
  13084. intel_cleanup_gt_powersave(dev_priv);
  13085. intel_teardown_gmbus(dev_priv);
  13086. destroy_workqueue(dev_priv->modeset_wq);
  13087. }
  13088. void intel_connector_attach_encoder(struct intel_connector *connector,
  13089. struct intel_encoder *encoder)
  13090. {
  13091. connector->encoder = encoder;
  13092. drm_mode_connector_attach_encoder(&connector->base,
  13093. &encoder->base);
  13094. }
  13095. /*
  13096. * set vga decode state - true == enable VGA decode
  13097. */
  13098. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13099. {
  13100. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13101. u16 gmch_ctrl;
  13102. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13103. DRM_ERROR("failed to read control word\n");
  13104. return -EIO;
  13105. }
  13106. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13107. return 0;
  13108. if (state)
  13109. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13110. else
  13111. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13112. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13113. DRM_ERROR("failed to write control word\n");
  13114. return -EIO;
  13115. }
  13116. return 0;
  13117. }
  13118. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13119. struct intel_display_error_state {
  13120. u32 power_well_driver;
  13121. int num_transcoders;
  13122. struct intel_cursor_error_state {
  13123. u32 control;
  13124. u32 position;
  13125. u32 base;
  13126. u32 size;
  13127. } cursor[I915_MAX_PIPES];
  13128. struct intel_pipe_error_state {
  13129. bool power_domain_on;
  13130. u32 source;
  13131. u32 stat;
  13132. } pipe[I915_MAX_PIPES];
  13133. struct intel_plane_error_state {
  13134. u32 control;
  13135. u32 stride;
  13136. u32 size;
  13137. u32 pos;
  13138. u32 addr;
  13139. u32 surface;
  13140. u32 tile_offset;
  13141. } plane[I915_MAX_PIPES];
  13142. struct intel_transcoder_error_state {
  13143. bool power_domain_on;
  13144. enum transcoder cpu_transcoder;
  13145. u32 conf;
  13146. u32 htotal;
  13147. u32 hblank;
  13148. u32 hsync;
  13149. u32 vtotal;
  13150. u32 vblank;
  13151. u32 vsync;
  13152. } transcoder[4];
  13153. };
  13154. struct intel_display_error_state *
  13155. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13156. {
  13157. struct intel_display_error_state *error;
  13158. int transcoders[] = {
  13159. TRANSCODER_A,
  13160. TRANSCODER_B,
  13161. TRANSCODER_C,
  13162. TRANSCODER_EDP,
  13163. };
  13164. int i;
  13165. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13166. return NULL;
  13167. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13168. if (error == NULL)
  13169. return NULL;
  13170. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13171. error->power_well_driver =
  13172. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  13173. for_each_pipe(dev_priv, i) {
  13174. error->pipe[i].power_domain_on =
  13175. __intel_display_power_is_enabled(dev_priv,
  13176. POWER_DOMAIN_PIPE(i));
  13177. if (!error->pipe[i].power_domain_on)
  13178. continue;
  13179. error->cursor[i].control = I915_READ(CURCNTR(i));
  13180. error->cursor[i].position = I915_READ(CURPOS(i));
  13181. error->cursor[i].base = I915_READ(CURBASE(i));
  13182. error->plane[i].control = I915_READ(DSPCNTR(i));
  13183. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13184. if (INTEL_GEN(dev_priv) <= 3) {
  13185. error->plane[i].size = I915_READ(DSPSIZE(i));
  13186. error->plane[i].pos = I915_READ(DSPPOS(i));
  13187. }
  13188. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13189. error->plane[i].addr = I915_READ(DSPADDR(i));
  13190. if (INTEL_GEN(dev_priv) >= 4) {
  13191. error->plane[i].surface = I915_READ(DSPSURF(i));
  13192. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13193. }
  13194. error->pipe[i].source = I915_READ(PIPESRC(i));
  13195. if (HAS_GMCH_DISPLAY(dev_priv))
  13196. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13197. }
  13198. /* Note: this does not include DSI transcoders. */
  13199. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13200. if (HAS_DDI(dev_priv))
  13201. error->num_transcoders++; /* Account for eDP. */
  13202. for (i = 0; i < error->num_transcoders; i++) {
  13203. enum transcoder cpu_transcoder = transcoders[i];
  13204. error->transcoder[i].power_domain_on =
  13205. __intel_display_power_is_enabled(dev_priv,
  13206. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13207. if (!error->transcoder[i].power_domain_on)
  13208. continue;
  13209. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13210. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13211. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13212. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13213. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13214. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13215. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13216. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13217. }
  13218. return error;
  13219. }
  13220. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13221. void
  13222. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13223. struct intel_display_error_state *error)
  13224. {
  13225. struct drm_i915_private *dev_priv = m->i915;
  13226. int i;
  13227. if (!error)
  13228. return;
  13229. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13230. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13231. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13232. error->power_well_driver);
  13233. for_each_pipe(dev_priv, i) {
  13234. err_printf(m, "Pipe [%d]:\n", i);
  13235. err_printf(m, " Power: %s\n",
  13236. onoff(error->pipe[i].power_domain_on));
  13237. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13238. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13239. err_printf(m, "Plane [%d]:\n", i);
  13240. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13241. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13242. if (INTEL_GEN(dev_priv) <= 3) {
  13243. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13244. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13245. }
  13246. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13247. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13248. if (INTEL_GEN(dev_priv) >= 4) {
  13249. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13250. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13251. }
  13252. err_printf(m, "Cursor [%d]:\n", i);
  13253. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13254. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13255. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13256. }
  13257. for (i = 0; i < error->num_transcoders; i++) {
  13258. err_printf(m, "CPU transcoder: %s\n",
  13259. transcoder_name(error->transcoder[i].cpu_transcoder));
  13260. err_printf(m, " Power: %s\n",
  13261. onoff(error->transcoder[i].power_domain_on));
  13262. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13263. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13264. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13265. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13266. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13267. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13268. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13269. }
  13270. }
  13271. #endif