intel_display.c 442 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664
  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 uint64_t skl_format_modifiers_noccs[] = {
  86. I915_FORMAT_MOD_Yf_TILED,
  87. I915_FORMAT_MOD_Y_TILED,
  88. I915_FORMAT_MOD_X_TILED,
  89. DRM_FORMAT_MOD_LINEAR,
  90. DRM_FORMAT_MOD_INVALID
  91. };
  92. static const uint64_t skl_format_modifiers_ccs[] = {
  93. I915_FORMAT_MOD_Yf_TILED_CCS,
  94. I915_FORMAT_MOD_Y_TILED_CCS,
  95. I915_FORMAT_MOD_Yf_TILED,
  96. I915_FORMAT_MOD_Y_TILED,
  97. I915_FORMAT_MOD_X_TILED,
  98. DRM_FORMAT_MOD_LINEAR,
  99. DRM_FORMAT_MOD_INVALID
  100. };
  101. /* Cursor formats */
  102. static const uint32_t intel_cursor_formats[] = {
  103. DRM_FORMAT_ARGB8888,
  104. };
  105. static const uint64_t cursor_format_modifiers[] = {
  106. DRM_FORMAT_MOD_LINEAR,
  107. DRM_FORMAT_MOD_INVALID
  108. };
  109. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  110. struct intel_crtc_state *pipe_config);
  111. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  112. struct intel_crtc_state *pipe_config);
  113. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  114. struct drm_i915_gem_object *obj,
  115. struct drm_mode_fb_cmd2 *mode_cmd);
  116. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  117. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  118. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  119. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  120. struct intel_link_m_n *m_n,
  121. struct intel_link_m_n *m2_n2);
  122. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  123. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  124. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  125. static void vlv_prepare_pll(struct intel_crtc *crtc,
  126. const struct intel_crtc_state *pipe_config);
  127. static void chv_prepare_pll(struct intel_crtc *crtc,
  128. const struct intel_crtc_state *pipe_config);
  129. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  130. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  131. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  132. struct intel_crtc_state *crtc_state);
  133. static void skylake_pfit_enable(struct intel_crtc *crtc);
  134. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  135. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  136. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  137. struct drm_modeset_acquire_ctx *ctx);
  138. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  139. struct intel_limit {
  140. struct {
  141. int min, max;
  142. } dot, vco, n, m, m1, m2, p, p1;
  143. struct {
  144. int dot_limit;
  145. int p2_slow, p2_fast;
  146. } p2;
  147. };
  148. /* returns HPLL frequency in kHz */
  149. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  150. {
  151. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  152. /* Obtain SKU information */
  153. mutex_lock(&dev_priv->sb_lock);
  154. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  155. CCK_FUSE_HPLL_FREQ_MASK;
  156. mutex_unlock(&dev_priv->sb_lock);
  157. return vco_freq[hpll_freq] * 1000;
  158. }
  159. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  160. const char *name, u32 reg, int ref_freq)
  161. {
  162. u32 val;
  163. int divider;
  164. mutex_lock(&dev_priv->sb_lock);
  165. val = vlv_cck_read(dev_priv, reg);
  166. mutex_unlock(&dev_priv->sb_lock);
  167. divider = val & CCK_FREQUENCY_VALUES;
  168. WARN((val & CCK_FREQUENCY_STATUS) !=
  169. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  170. "%s change in progress\n", name);
  171. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  172. }
  173. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  174. const char *name, u32 reg)
  175. {
  176. if (dev_priv->hpll_freq == 0)
  177. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  178. return vlv_get_cck_clock(dev_priv, name, reg,
  179. dev_priv->hpll_freq);
  180. }
  181. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  182. {
  183. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  184. return;
  185. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  186. CCK_CZ_CLOCK_CONTROL);
  187. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  188. }
  189. static inline u32 /* units of 100MHz */
  190. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  191. const struct intel_crtc_state *pipe_config)
  192. {
  193. if (HAS_DDI(dev_priv))
  194. return pipe_config->port_clock; /* SPLL */
  195. else
  196. return dev_priv->fdi_pll_freq;
  197. }
  198. static const struct intel_limit intel_limits_i8xx_dac = {
  199. .dot = { .min = 25000, .max = 350000 },
  200. .vco = { .min = 908000, .max = 1512000 },
  201. .n = { .min = 2, .max = 16 },
  202. .m = { .min = 96, .max = 140 },
  203. .m1 = { .min = 18, .max = 26 },
  204. .m2 = { .min = 6, .max = 16 },
  205. .p = { .min = 4, .max = 128 },
  206. .p1 = { .min = 2, .max = 33 },
  207. .p2 = { .dot_limit = 165000,
  208. .p2_slow = 4, .p2_fast = 2 },
  209. };
  210. static const struct intel_limit intel_limits_i8xx_dvo = {
  211. .dot = { .min = 25000, .max = 350000 },
  212. .vco = { .min = 908000, .max = 1512000 },
  213. .n = { .min = 2, .max = 16 },
  214. .m = { .min = 96, .max = 140 },
  215. .m1 = { .min = 18, .max = 26 },
  216. .m2 = { .min = 6, .max = 16 },
  217. .p = { .min = 4, .max = 128 },
  218. .p1 = { .min = 2, .max = 33 },
  219. .p2 = { .dot_limit = 165000,
  220. .p2_slow = 4, .p2_fast = 4 },
  221. };
  222. static const struct intel_limit intel_limits_i8xx_lvds = {
  223. .dot = { .min = 25000, .max = 350000 },
  224. .vco = { .min = 908000, .max = 1512000 },
  225. .n = { .min = 2, .max = 16 },
  226. .m = { .min = 96, .max = 140 },
  227. .m1 = { .min = 18, .max = 26 },
  228. .m2 = { .min = 6, .max = 16 },
  229. .p = { .min = 4, .max = 128 },
  230. .p1 = { .min = 1, .max = 6 },
  231. .p2 = { .dot_limit = 165000,
  232. .p2_slow = 14, .p2_fast = 7 },
  233. };
  234. static const struct intel_limit intel_limits_i9xx_sdvo = {
  235. .dot = { .min = 20000, .max = 400000 },
  236. .vco = { .min = 1400000, .max = 2800000 },
  237. .n = { .min = 1, .max = 6 },
  238. .m = { .min = 70, .max = 120 },
  239. .m1 = { .min = 8, .max = 18 },
  240. .m2 = { .min = 3, .max = 7 },
  241. .p = { .min = 5, .max = 80 },
  242. .p1 = { .min = 1, .max = 8 },
  243. .p2 = { .dot_limit = 200000,
  244. .p2_slow = 10, .p2_fast = 5 },
  245. };
  246. static const struct intel_limit intel_limits_i9xx_lvds = {
  247. .dot = { .min = 20000, .max = 400000 },
  248. .vco = { .min = 1400000, .max = 2800000 },
  249. .n = { .min = 1, .max = 6 },
  250. .m = { .min = 70, .max = 120 },
  251. .m1 = { .min = 8, .max = 18 },
  252. .m2 = { .min = 3, .max = 7 },
  253. .p = { .min = 7, .max = 98 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 112000,
  256. .p2_slow = 14, .p2_fast = 7 },
  257. };
  258. static const struct intel_limit intel_limits_g4x_sdvo = {
  259. .dot = { .min = 25000, .max = 270000 },
  260. .vco = { .min = 1750000, .max = 3500000},
  261. .n = { .min = 1, .max = 4 },
  262. .m = { .min = 104, .max = 138 },
  263. .m1 = { .min = 17, .max = 23 },
  264. .m2 = { .min = 5, .max = 11 },
  265. .p = { .min = 10, .max = 30 },
  266. .p1 = { .min = 1, .max = 3},
  267. .p2 = { .dot_limit = 270000,
  268. .p2_slow = 10,
  269. .p2_fast = 10
  270. },
  271. };
  272. static const struct intel_limit intel_limits_g4x_hdmi = {
  273. .dot = { .min = 22000, .max = 400000 },
  274. .vco = { .min = 1750000, .max = 3500000},
  275. .n = { .min = 1, .max = 4 },
  276. .m = { .min = 104, .max = 138 },
  277. .m1 = { .min = 16, .max = 23 },
  278. .m2 = { .min = 5, .max = 11 },
  279. .p = { .min = 5, .max = 80 },
  280. .p1 = { .min = 1, .max = 8},
  281. .p2 = { .dot_limit = 165000,
  282. .p2_slow = 10, .p2_fast = 5 },
  283. };
  284. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  285. .dot = { .min = 20000, .max = 115000 },
  286. .vco = { .min = 1750000, .max = 3500000 },
  287. .n = { .min = 1, .max = 3 },
  288. .m = { .min = 104, .max = 138 },
  289. .m1 = { .min = 17, .max = 23 },
  290. .m2 = { .min = 5, .max = 11 },
  291. .p = { .min = 28, .max = 112 },
  292. .p1 = { .min = 2, .max = 8 },
  293. .p2 = { .dot_limit = 0,
  294. .p2_slow = 14, .p2_fast = 14
  295. },
  296. };
  297. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  298. .dot = { .min = 80000, .max = 224000 },
  299. .vco = { .min = 1750000, .max = 3500000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 104, .max = 138 },
  302. .m1 = { .min = 17, .max = 23 },
  303. .m2 = { .min = 5, .max = 11 },
  304. .p = { .min = 14, .max = 42 },
  305. .p1 = { .min = 2, .max = 6 },
  306. .p2 = { .dot_limit = 0,
  307. .p2_slow = 7, .p2_fast = 7
  308. },
  309. };
  310. static const struct intel_limit intel_limits_pineview_sdvo = {
  311. .dot = { .min = 20000, .max = 400000},
  312. .vco = { .min = 1700000, .max = 3500000 },
  313. /* Pineview's Ncounter is a ring counter */
  314. .n = { .min = 3, .max = 6 },
  315. .m = { .min = 2, .max = 256 },
  316. /* Pineview only has one combined m divider, which we treat as m2. */
  317. .m1 = { .min = 0, .max = 0 },
  318. .m2 = { .min = 0, .max = 254 },
  319. .p = { .min = 5, .max = 80 },
  320. .p1 = { .min = 1, .max = 8 },
  321. .p2 = { .dot_limit = 200000,
  322. .p2_slow = 10, .p2_fast = 5 },
  323. };
  324. static const struct intel_limit intel_limits_pineview_lvds = {
  325. .dot = { .min = 20000, .max = 400000 },
  326. .vco = { .min = 1700000, .max = 3500000 },
  327. .n = { .min = 3, .max = 6 },
  328. .m = { .min = 2, .max = 256 },
  329. .m1 = { .min = 0, .max = 0 },
  330. .m2 = { .min = 0, .max = 254 },
  331. .p = { .min = 7, .max = 112 },
  332. .p1 = { .min = 1, .max = 8 },
  333. .p2 = { .dot_limit = 112000,
  334. .p2_slow = 14, .p2_fast = 14 },
  335. };
  336. /* Ironlake / Sandybridge
  337. *
  338. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  339. * the range value for them is (actual_value - 2).
  340. */
  341. static const struct intel_limit intel_limits_ironlake_dac = {
  342. .dot = { .min = 25000, .max = 350000 },
  343. .vco = { .min = 1760000, .max = 3510000 },
  344. .n = { .min = 1, .max = 5 },
  345. .m = { .min = 79, .max = 127 },
  346. .m1 = { .min = 12, .max = 22 },
  347. .m2 = { .min = 5, .max = 9 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 225000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  354. .dot = { .min = 25000, .max = 350000 },
  355. .vco = { .min = 1760000, .max = 3510000 },
  356. .n = { .min = 1, .max = 3 },
  357. .m = { .min = 79, .max = 118 },
  358. .m1 = { .min = 12, .max = 22 },
  359. .m2 = { .min = 5, .max = 9 },
  360. .p = { .min = 28, .max = 112 },
  361. .p1 = { .min = 2, .max = 8 },
  362. .p2 = { .dot_limit = 225000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  366. .dot = { .min = 25000, .max = 350000 },
  367. .vco = { .min = 1760000, .max = 3510000 },
  368. .n = { .min = 1, .max = 3 },
  369. .m = { .min = 79, .max = 127 },
  370. .m1 = { .min = 12, .max = 22 },
  371. .m2 = { .min = 5, .max = 9 },
  372. .p = { .min = 14, .max = 56 },
  373. .p1 = { .min = 2, .max = 8 },
  374. .p2 = { .dot_limit = 225000,
  375. .p2_slow = 7, .p2_fast = 7 },
  376. };
  377. /* LVDS 100mhz refclk limits. */
  378. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  379. .dot = { .min = 25000, .max = 350000 },
  380. .vco = { .min = 1760000, .max = 3510000 },
  381. .n = { .min = 1, .max = 2 },
  382. .m = { .min = 79, .max = 126 },
  383. .m1 = { .min = 12, .max = 22 },
  384. .m2 = { .min = 5, .max = 9 },
  385. .p = { .min = 28, .max = 112 },
  386. .p1 = { .min = 2, .max = 8 },
  387. .p2 = { .dot_limit = 225000,
  388. .p2_slow = 14, .p2_fast = 14 },
  389. };
  390. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  391. .dot = { .min = 25000, .max = 350000 },
  392. .vco = { .min = 1760000, .max = 3510000 },
  393. .n = { .min = 1, .max = 3 },
  394. .m = { .min = 79, .max = 126 },
  395. .m1 = { .min = 12, .max = 22 },
  396. .m2 = { .min = 5, .max = 9 },
  397. .p = { .min = 14, .max = 42 },
  398. .p1 = { .min = 2, .max = 6 },
  399. .p2 = { .dot_limit = 225000,
  400. .p2_slow = 7, .p2_fast = 7 },
  401. };
  402. static const struct intel_limit intel_limits_vlv = {
  403. /*
  404. * These are the data rate limits (measured in fast clocks)
  405. * since those are the strictest limits we have. The fast
  406. * clock and actual rate limits are more relaxed, so checking
  407. * them would make no difference.
  408. */
  409. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  410. .vco = { .min = 4000000, .max = 6000000 },
  411. .n = { .min = 1, .max = 7 },
  412. .m1 = { .min = 2, .max = 3 },
  413. .m2 = { .min = 11, .max = 156 },
  414. .p1 = { .min = 2, .max = 3 },
  415. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  416. };
  417. static const struct intel_limit intel_limits_chv = {
  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 = 540000 * 5},
  425. .vco = { .min = 4800000, .max = 6480000 },
  426. .n = { .min = 1, .max = 1 },
  427. .m1 = { .min = 2, .max = 2 },
  428. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  429. .p1 = { .min = 2, .max = 4 },
  430. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  431. };
  432. static const struct intel_limit intel_limits_bxt = {
  433. /* FIXME: find real dot limits */
  434. .dot = { .min = 0, .max = INT_MAX },
  435. .vco = { .min = 4800000, .max = 6700000 },
  436. .n = { .min = 1, .max = 1 },
  437. .m1 = { .min = 2, .max = 2 },
  438. /* FIXME: find real m2 limits */
  439. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  440. .p1 = { .min = 2, .max = 4 },
  441. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  442. };
  443. static bool
  444. needs_modeset(const struct drm_crtc_state *state)
  445. {
  446. return drm_atomic_crtc_needs_modeset(state);
  447. }
  448. /*
  449. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  450. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  451. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  452. * The helpers' return value is the rate of the clock that is fed to the
  453. * display engine's pipe which can be the above fast dot clock rate or a
  454. * divided-down version of it.
  455. */
  456. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  457. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  458. {
  459. clock->m = clock->m2 + 2;
  460. clock->p = clock->p1 * clock->p2;
  461. if (WARN_ON(clock->n == 0 || clock->p == 0))
  462. return 0;
  463. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  464. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  465. return clock->dot;
  466. }
  467. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  468. {
  469. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  470. }
  471. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = i9xx_dpll_compute_m(clock);
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  478. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  479. return clock->dot;
  480. }
  481. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  482. {
  483. clock->m = clock->m1 * clock->m2;
  484. clock->p = clock->p1 * clock->p2;
  485. if (WARN_ON(clock->n == 0 || clock->p == 0))
  486. return 0;
  487. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  488. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  489. return clock->dot / 5;
  490. }
  491. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m1 * clock->m2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  498. clock->n << 22);
  499. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  500. return clock->dot / 5;
  501. }
  502. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  503. /*
  504. * Returns whether the given set of divisors are valid for a given refclk with
  505. * the given connectors.
  506. */
  507. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  508. const struct intel_limit *limit,
  509. const struct dpll *clock)
  510. {
  511. if (clock->n < limit->n.min || limit->n.max < clock->n)
  512. INTELPllInvalid("n out of range\n");
  513. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  514. INTELPllInvalid("p1 out of range\n");
  515. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  516. INTELPllInvalid("m2 out of range\n");
  517. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  518. INTELPllInvalid("m1 out of range\n");
  519. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  520. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  521. if (clock->m1 <= clock->m2)
  522. INTELPllInvalid("m1 <= m2\n");
  523. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  524. !IS_GEN9_LP(dev_priv)) {
  525. if (clock->p < limit->p.min || limit->p.max < clock->p)
  526. INTELPllInvalid("p out of range\n");
  527. if (clock->m < limit->m.min || limit->m.max < clock->m)
  528. INTELPllInvalid("m out of range\n");
  529. }
  530. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  531. INTELPllInvalid("vco out of range\n");
  532. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  533. * connector, etc., rather than just a single range.
  534. */
  535. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  536. INTELPllInvalid("dot out of range\n");
  537. return true;
  538. }
  539. static int
  540. i9xx_select_p2_div(const struct intel_limit *limit,
  541. const struct intel_crtc_state *crtc_state,
  542. int target)
  543. {
  544. struct drm_device *dev = crtc_state->base.crtc->dev;
  545. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  546. /*
  547. * For LVDS just rely on its current settings for dual-channel.
  548. * We haven't figured out how to reliably set up different
  549. * single/dual channel state, if we even can.
  550. */
  551. if (intel_is_dual_link_lvds(dev))
  552. return limit->p2.p2_fast;
  553. else
  554. return limit->p2.p2_slow;
  555. } else {
  556. if (target < limit->p2.dot_limit)
  557. return limit->p2.p2_slow;
  558. else
  559. return limit->p2.p2_fast;
  560. }
  561. }
  562. /*
  563. * Returns a set of divisors for the desired target clock with the given
  564. * refclk, or FALSE. The returned values represent the clock equation:
  565. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  566. *
  567. * Target and reference clocks are specified in kHz.
  568. *
  569. * If match_clock is provided, then best_clock P divider must match the P
  570. * divider from @match_clock used for LVDS downclocking.
  571. */
  572. static bool
  573. i9xx_find_best_dpll(const struct intel_limit *limit,
  574. struct intel_crtc_state *crtc_state,
  575. int target, int refclk, struct dpll *match_clock,
  576. struct dpll *best_clock)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. struct dpll clock;
  580. int err = target;
  581. memset(best_clock, 0, sizeof(*best_clock));
  582. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  583. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  584. clock.m1++) {
  585. for (clock.m2 = limit->m2.min;
  586. clock.m2 <= limit->m2.max; clock.m2++) {
  587. if (clock.m2 >= clock.m1)
  588. break;
  589. for (clock.n = limit->n.min;
  590. clock.n <= limit->n.max; clock.n++) {
  591. for (clock.p1 = limit->p1.min;
  592. clock.p1 <= limit->p1.max; clock.p1++) {
  593. int this_err;
  594. i9xx_calc_dpll_params(refclk, &clock);
  595. if (!intel_PLL_is_valid(to_i915(dev),
  596. limit,
  597. &clock))
  598. continue;
  599. if (match_clock &&
  600. clock.p != match_clock->p)
  601. continue;
  602. this_err = abs(clock.dot - target);
  603. if (this_err < err) {
  604. *best_clock = clock;
  605. err = this_err;
  606. }
  607. }
  608. }
  609. }
  610. }
  611. return (err != target);
  612. }
  613. /*
  614. * Returns a set of divisors for the desired target clock with the given
  615. * refclk, or FALSE. The returned values represent the clock equation:
  616. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  617. *
  618. * Target and reference clocks are specified in kHz.
  619. *
  620. * If match_clock is provided, then best_clock P divider must match the P
  621. * divider from @match_clock used for LVDS downclocking.
  622. */
  623. static bool
  624. pnv_find_best_dpll(const struct intel_limit *limit,
  625. struct intel_crtc_state *crtc_state,
  626. int target, int refclk, struct dpll *match_clock,
  627. struct dpll *best_clock)
  628. {
  629. struct drm_device *dev = crtc_state->base.crtc->dev;
  630. struct dpll clock;
  631. int err = target;
  632. memset(best_clock, 0, sizeof(*best_clock));
  633. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  634. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  635. clock.m1++) {
  636. for (clock.m2 = limit->m2.min;
  637. clock.m2 <= limit->m2.max; clock.m2++) {
  638. for (clock.n = limit->n.min;
  639. clock.n <= limit->n.max; clock.n++) {
  640. for (clock.p1 = limit->p1.min;
  641. clock.p1 <= limit->p1.max; clock.p1++) {
  642. int this_err;
  643. pnv_calc_dpll_params(refclk, &clock);
  644. if (!intel_PLL_is_valid(to_i915(dev),
  645. limit,
  646. &clock))
  647. continue;
  648. if (match_clock &&
  649. clock.p != match_clock->p)
  650. continue;
  651. this_err = abs(clock.dot - target);
  652. if (this_err < err) {
  653. *best_clock = clock;
  654. err = this_err;
  655. }
  656. }
  657. }
  658. }
  659. }
  660. return (err != target);
  661. }
  662. /*
  663. * Returns a set of divisors for the desired target clock with the given
  664. * refclk, or FALSE. The returned values represent the clock equation:
  665. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  666. *
  667. * Target and reference clocks are specified in kHz.
  668. *
  669. * If match_clock is provided, then best_clock P divider must match the P
  670. * divider from @match_clock used for LVDS downclocking.
  671. */
  672. static bool
  673. g4x_find_best_dpll(const struct intel_limit *limit,
  674. struct intel_crtc_state *crtc_state,
  675. int target, int refclk, struct dpll *match_clock,
  676. struct dpll *best_clock)
  677. {
  678. struct drm_device *dev = crtc_state->base.crtc->dev;
  679. struct dpll clock;
  680. int max_n;
  681. bool found = false;
  682. /* approximately equals target * 0.00585 */
  683. int err_most = (target >> 8) + (target >> 9);
  684. memset(best_clock, 0, sizeof(*best_clock));
  685. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  686. max_n = limit->n.max;
  687. /* based on hardware requirement, prefer smaller n to precision */
  688. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  689. /* based on hardware requirement, prefere larger m1,m2 */
  690. for (clock.m1 = limit->m1.max;
  691. clock.m1 >= limit->m1.min; clock.m1--) {
  692. for (clock.m2 = limit->m2.max;
  693. clock.m2 >= limit->m2.min; clock.m2--) {
  694. for (clock.p1 = limit->p1.max;
  695. clock.p1 >= limit->p1.min; clock.p1--) {
  696. int this_err;
  697. i9xx_calc_dpll_params(refclk, &clock);
  698. if (!intel_PLL_is_valid(to_i915(dev),
  699. limit,
  700. &clock))
  701. continue;
  702. this_err = abs(clock.dot - target);
  703. if (this_err < err_most) {
  704. *best_clock = clock;
  705. err_most = this_err;
  706. max_n = clock.n;
  707. found = true;
  708. }
  709. }
  710. }
  711. }
  712. }
  713. return found;
  714. }
  715. /*
  716. * Check if the calculated PLL configuration is more optimal compared to the
  717. * best configuration and error found so far. Return the calculated error.
  718. */
  719. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  720. const struct dpll *calculated_clock,
  721. const struct dpll *best_clock,
  722. unsigned int best_error_ppm,
  723. unsigned int *error_ppm)
  724. {
  725. /*
  726. * For CHV ignore the error and consider only the P value.
  727. * Prefer a bigger P value based on HW requirements.
  728. */
  729. if (IS_CHERRYVIEW(to_i915(dev))) {
  730. *error_ppm = 0;
  731. return calculated_clock->p > best_clock->p;
  732. }
  733. if (WARN_ON_ONCE(!target_freq))
  734. return false;
  735. *error_ppm = div_u64(1000000ULL *
  736. abs(target_freq - calculated_clock->dot),
  737. target_freq);
  738. /*
  739. * Prefer a better P value over a better (smaller) error if the error
  740. * is small. Ensure this preference for future configurations too by
  741. * setting the error to 0.
  742. */
  743. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  744. *error_ppm = 0;
  745. return true;
  746. }
  747. return *error_ppm + 10 < best_error_ppm;
  748. }
  749. /*
  750. * Returns a set of divisors for the desired target clock with the given
  751. * refclk, or FALSE. The returned values represent the clock equation:
  752. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  753. */
  754. static bool
  755. vlv_find_best_dpll(const struct intel_limit *limit,
  756. struct intel_crtc_state *crtc_state,
  757. int target, int refclk, struct dpll *match_clock,
  758. struct dpll *best_clock)
  759. {
  760. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  761. struct drm_device *dev = crtc->base.dev;
  762. struct dpll clock;
  763. unsigned int bestppm = 1000000;
  764. /* min update 19.2 MHz */
  765. int max_n = min(limit->n.max, refclk / 19200);
  766. bool found = false;
  767. target *= 5; /* fast clock */
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. /* based on hardware requirement, prefer smaller n to precision */
  770. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  771. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  772. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  773. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  774. clock.p = clock.p1 * clock.p2;
  775. /* based on hardware requirement, prefer bigger m1,m2 values */
  776. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  777. unsigned int ppm;
  778. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  779. refclk * clock.m1);
  780. vlv_calc_dpll_params(refclk, &clock);
  781. if (!intel_PLL_is_valid(to_i915(dev),
  782. limit,
  783. &clock))
  784. continue;
  785. if (!vlv_PLL_is_optimal(dev, target,
  786. &clock,
  787. best_clock,
  788. bestppm, &ppm))
  789. continue;
  790. *best_clock = clock;
  791. bestppm = ppm;
  792. found = true;
  793. }
  794. }
  795. }
  796. }
  797. return found;
  798. }
  799. /*
  800. * Returns a set of divisors for the desired target clock with the given
  801. * refclk, or FALSE. The returned values represent the clock equation:
  802. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  803. */
  804. static bool
  805. chv_find_best_dpll(const struct intel_limit *limit,
  806. struct intel_crtc_state *crtc_state,
  807. int target, int refclk, struct dpll *match_clock,
  808. struct dpll *best_clock)
  809. {
  810. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  811. struct drm_device *dev = crtc->base.dev;
  812. unsigned int best_error_ppm;
  813. struct dpll clock;
  814. uint64_t m2;
  815. int found = false;
  816. memset(best_clock, 0, sizeof(*best_clock));
  817. best_error_ppm = 1000000;
  818. /*
  819. * Based on hardware doc, the n always set to 1, and m1 always
  820. * set to 2. If requires to support 200Mhz refclk, we need to
  821. * revisit this because n may not 1 anymore.
  822. */
  823. clock.n = 1, clock.m1 = 2;
  824. target *= 5; /* fast clock */
  825. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  826. for (clock.p2 = limit->p2.p2_fast;
  827. clock.p2 >= limit->p2.p2_slow;
  828. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  829. unsigned int error_ppm;
  830. clock.p = clock.p1 * clock.p2;
  831. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  832. clock.n) << 22, refclk * clock.m1);
  833. if (m2 > INT_MAX/clock.m1)
  834. continue;
  835. clock.m2 = m2;
  836. chv_calc_dpll_params(refclk, &clock);
  837. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  838. continue;
  839. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  840. best_error_ppm, &error_ppm))
  841. continue;
  842. *best_clock = clock;
  843. best_error_ppm = error_ppm;
  844. found = true;
  845. }
  846. }
  847. return found;
  848. }
  849. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  850. struct dpll *best_clock)
  851. {
  852. int refclk = 100000;
  853. const struct intel_limit *limit = &intel_limits_bxt;
  854. return chv_find_best_dpll(limit, crtc_state,
  855. target_clock, refclk, NULL, best_clock);
  856. }
  857. bool intel_crtc_active(struct intel_crtc *crtc)
  858. {
  859. /* Be paranoid as we can arrive here with only partial
  860. * state retrieved from the hardware during setup.
  861. *
  862. * We can ditch the adjusted_mode.crtc_clock check as soon
  863. * as Haswell has gained clock readout/fastboot support.
  864. *
  865. * We can ditch the crtc->primary->fb check as soon as we can
  866. * properly reconstruct framebuffers.
  867. *
  868. * FIXME: The intel_crtc->active here should be switched to
  869. * crtc->state->active once we have proper CRTC states wired up
  870. * for atomic.
  871. */
  872. return crtc->active && crtc->base.primary->state->fb &&
  873. crtc->config->base.adjusted_mode.crtc_clock;
  874. }
  875. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  876. enum pipe pipe)
  877. {
  878. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  879. return crtc->config->cpu_transcoder;
  880. }
  881. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  882. enum pipe pipe)
  883. {
  884. i915_reg_t reg = PIPEDSL(pipe);
  885. u32 line1, line2;
  886. u32 line_mask;
  887. if (IS_GEN2(dev_priv))
  888. line_mask = DSL_LINEMASK_GEN2;
  889. else
  890. line_mask = DSL_LINEMASK_GEN3;
  891. line1 = I915_READ(reg) & line_mask;
  892. msleep(5);
  893. line2 = I915_READ(reg) & line_mask;
  894. return line1 != line2;
  895. }
  896. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  897. {
  898. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  899. enum pipe pipe = crtc->pipe;
  900. /* Wait for the display line to settle/start moving */
  901. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  902. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  903. pipe_name(pipe), onoff(state));
  904. }
  905. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  906. {
  907. wait_for_pipe_scanline_moving(crtc, false);
  908. }
  909. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  910. {
  911. wait_for_pipe_scanline_moving(crtc, true);
  912. }
  913. static void
  914. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  915. {
  916. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  917. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  918. if (INTEL_GEN(dev_priv) >= 4) {
  919. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  920. i915_reg_t reg = PIPECONF(cpu_transcoder);
  921. /* Wait for the Pipe State to go off */
  922. if (intel_wait_for_register(dev_priv,
  923. reg, I965_PIPECONF_ACTIVE, 0,
  924. 100))
  925. WARN(1, "pipe_off wait timed out\n");
  926. } else {
  927. intel_wait_for_pipe_scanline_stopped(crtc);
  928. }
  929. }
  930. /* Only for pre-ILK configs */
  931. void assert_pll(struct drm_i915_private *dev_priv,
  932. enum pipe pipe, bool state)
  933. {
  934. u32 val;
  935. bool cur_state;
  936. val = I915_READ(DPLL(pipe));
  937. cur_state = !!(val & DPLL_VCO_ENABLE);
  938. I915_STATE_WARN(cur_state != state,
  939. "PLL state assertion failure (expected %s, current %s)\n",
  940. onoff(state), onoff(cur_state));
  941. }
  942. /* XXX: the dsi pll is shared between MIPI DSI ports */
  943. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  944. {
  945. u32 val;
  946. bool cur_state;
  947. mutex_lock(&dev_priv->sb_lock);
  948. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  949. mutex_unlock(&dev_priv->sb_lock);
  950. cur_state = val & DSI_PLL_VCO_EN;
  951. I915_STATE_WARN(cur_state != state,
  952. "DSI PLL state assertion failure (expected %s, current %s)\n",
  953. onoff(state), onoff(cur_state));
  954. }
  955. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  956. enum pipe pipe, bool state)
  957. {
  958. bool cur_state;
  959. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  960. pipe);
  961. if (HAS_DDI(dev_priv)) {
  962. /* DDI does not have a specific FDI_TX register */
  963. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  964. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  965. } else {
  966. u32 val = I915_READ(FDI_TX_CTL(pipe));
  967. cur_state = !!(val & FDI_TX_ENABLE);
  968. }
  969. I915_STATE_WARN(cur_state != state,
  970. "FDI TX state assertion failure (expected %s, current %s)\n",
  971. onoff(state), onoff(cur_state));
  972. }
  973. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  974. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  975. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  976. enum pipe pipe, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. val = I915_READ(FDI_RX_CTL(pipe));
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. I915_STATE_WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. onoff(state), onoff(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. u32 val;
  992. /* ILK FDI PLL is always enabled */
  993. if (IS_GEN5(dev_priv))
  994. return;
  995. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  996. if (HAS_DDI(dev_priv))
  997. return;
  998. val = I915_READ(FDI_TX_CTL(pipe));
  999. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1000. }
  1001. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1002. enum pipe pipe, bool state)
  1003. {
  1004. u32 val;
  1005. bool cur_state;
  1006. val = I915_READ(FDI_RX_CTL(pipe));
  1007. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1008. I915_STATE_WARN(cur_state != state,
  1009. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1010. onoff(state), onoff(cur_state));
  1011. }
  1012. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1013. {
  1014. i915_reg_t pp_reg;
  1015. u32 val;
  1016. enum pipe panel_pipe = PIPE_A;
  1017. bool locked = true;
  1018. if (WARN_ON(HAS_DDI(dev_priv)))
  1019. return;
  1020. if (HAS_PCH_SPLIT(dev_priv)) {
  1021. u32 port_sel;
  1022. pp_reg = PP_CONTROL(0);
  1023. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1024. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1025. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1026. panel_pipe = PIPE_B;
  1027. /* XXX: else fix for eDP */
  1028. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1029. /* presumably write lock depends on pipe, not port select */
  1030. pp_reg = PP_CONTROL(pipe);
  1031. panel_pipe = pipe;
  1032. } else {
  1033. pp_reg = PP_CONTROL(0);
  1034. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. }
  1037. val = I915_READ(pp_reg);
  1038. if (!(val & PANEL_POWER_ON) ||
  1039. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1040. locked = false;
  1041. I915_STATE_WARN(panel_pipe == pipe && locked,
  1042. "panel assertion failure, pipe %c regs locked\n",
  1043. pipe_name(pipe));
  1044. }
  1045. void assert_pipe(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, bool state)
  1047. {
  1048. bool cur_state;
  1049. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1050. pipe);
  1051. enum intel_display_power_domain power_domain;
  1052. /* we keep both pipes enabled on 830 */
  1053. if (IS_I830(dev_priv))
  1054. state = true;
  1055. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1056. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1057. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1058. cur_state = !!(val & PIPECONF_ENABLE);
  1059. intel_display_power_put(dev_priv, power_domain);
  1060. } else {
  1061. cur_state = false;
  1062. }
  1063. I915_STATE_WARN(cur_state != state,
  1064. "pipe %c assertion failure (expected %s, current %s)\n",
  1065. pipe_name(pipe), onoff(state), onoff(cur_state));
  1066. }
  1067. static void assert_plane(struct intel_plane *plane, bool state)
  1068. {
  1069. bool cur_state = plane->get_hw_state(plane);
  1070. I915_STATE_WARN(cur_state != state,
  1071. "%s assertion failure (expected %s, current %s)\n",
  1072. plane->base.name, onoff(state), onoff(cur_state));
  1073. }
  1074. #define assert_plane_enabled(p) assert_plane(p, true)
  1075. #define assert_plane_disabled(p) assert_plane(p, false)
  1076. static void assert_planes_disabled(struct intel_crtc *crtc)
  1077. {
  1078. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1079. struct intel_plane *plane;
  1080. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1081. assert_plane_disabled(plane);
  1082. }
  1083. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1084. {
  1085. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1086. drm_crtc_vblank_put(crtc);
  1087. }
  1088. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. u32 val;
  1092. bool enabled;
  1093. val = I915_READ(PCH_TRANSCONF(pipe));
  1094. enabled = !!(val & TRANS_ENABLE);
  1095. I915_STATE_WARN(enabled,
  1096. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1097. pipe_name(pipe));
  1098. }
  1099. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1100. enum pipe pipe, u32 port_sel, u32 val)
  1101. {
  1102. if ((val & DP_PORT_EN) == 0)
  1103. return false;
  1104. if (HAS_PCH_CPT(dev_priv)) {
  1105. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1106. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1107. return false;
  1108. } else if (IS_CHERRYVIEW(dev_priv)) {
  1109. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1110. return false;
  1111. } else {
  1112. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1113. return false;
  1114. }
  1115. return true;
  1116. }
  1117. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe, u32 val)
  1119. {
  1120. if ((val & SDVO_ENABLE) == 0)
  1121. return false;
  1122. if (HAS_PCH_CPT(dev_priv)) {
  1123. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1124. return false;
  1125. } else if (IS_CHERRYVIEW(dev_priv)) {
  1126. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1127. return false;
  1128. } else {
  1129. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1130. return false;
  1131. }
  1132. return true;
  1133. }
  1134. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1135. enum pipe pipe, u32 val)
  1136. {
  1137. if ((val & LVDS_PORT_EN) == 0)
  1138. return false;
  1139. if (HAS_PCH_CPT(dev_priv)) {
  1140. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1141. return false;
  1142. } else {
  1143. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1144. return false;
  1145. }
  1146. return true;
  1147. }
  1148. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1149. enum pipe pipe, u32 val)
  1150. {
  1151. if ((val & ADPA_DAC_ENABLE) == 0)
  1152. return false;
  1153. if (HAS_PCH_CPT(dev_priv)) {
  1154. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1155. return false;
  1156. } else {
  1157. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1158. return false;
  1159. }
  1160. return true;
  1161. }
  1162. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe, i915_reg_t reg,
  1164. u32 port_sel)
  1165. {
  1166. u32 val = I915_READ(reg);
  1167. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1168. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1169. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1170. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1171. && (val & DP_PIPEB_SELECT),
  1172. "IBX PCH dp port still using transcoder B\n");
  1173. }
  1174. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1175. enum pipe pipe, i915_reg_t reg)
  1176. {
  1177. u32 val = I915_READ(reg);
  1178. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1179. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1180. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1181. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1182. && (val & SDVO_PIPE_B_SELECT),
  1183. "IBX PCH hdmi port still using transcoder B\n");
  1184. }
  1185. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1190. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1191. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1192. val = I915_READ(PCH_ADPA);
  1193. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1194. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1195. pipe_name(pipe));
  1196. val = I915_READ(PCH_LVDS);
  1197. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1198. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1199. pipe_name(pipe));
  1200. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1201. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1202. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1203. }
  1204. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1205. const struct intel_crtc_state *pipe_config)
  1206. {
  1207. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1208. enum pipe pipe = crtc->pipe;
  1209. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1210. POSTING_READ(DPLL(pipe));
  1211. udelay(150);
  1212. if (intel_wait_for_register(dev_priv,
  1213. DPLL(pipe),
  1214. DPLL_LOCK_VLV,
  1215. DPLL_LOCK_VLV,
  1216. 1))
  1217. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1218. }
  1219. static void vlv_enable_pll(struct intel_crtc *crtc,
  1220. const struct intel_crtc_state *pipe_config)
  1221. {
  1222. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1223. enum pipe pipe = crtc->pipe;
  1224. assert_pipe_disabled(dev_priv, pipe);
  1225. /* PLL is protected by panel, make sure we can write it */
  1226. assert_panel_unlocked(dev_priv, pipe);
  1227. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1228. _vlv_enable_pll(crtc, pipe_config);
  1229. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1230. POSTING_READ(DPLL_MD(pipe));
  1231. }
  1232. static void _chv_enable_pll(struct intel_crtc *crtc,
  1233. const struct intel_crtc_state *pipe_config)
  1234. {
  1235. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1236. enum pipe pipe = crtc->pipe;
  1237. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1238. u32 tmp;
  1239. mutex_lock(&dev_priv->sb_lock);
  1240. /* Enable back the 10bit clock to display controller */
  1241. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1242. tmp |= DPIO_DCLKP_EN;
  1243. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1244. mutex_unlock(&dev_priv->sb_lock);
  1245. /*
  1246. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1247. */
  1248. udelay(1);
  1249. /* Enable PLL */
  1250. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1251. /* Check PLL is locked */
  1252. if (intel_wait_for_register(dev_priv,
  1253. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1254. 1))
  1255. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1256. }
  1257. static void chv_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. _chv_enable_pll(crtc, pipe_config);
  1267. if (pipe != PIPE_A) {
  1268. /*
  1269. * WaPixelRepeatModeFixForC0:chv
  1270. *
  1271. * DPLLCMD is AWOL. Use chicken bits to propagate
  1272. * the value from DPLLBMD to either pipe B or C.
  1273. */
  1274. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1275. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1276. I915_WRITE(CBR4_VLV, 0);
  1277. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1278. /*
  1279. * DPLLB VGA mode also seems to cause problems.
  1280. * We should always have it disabled.
  1281. */
  1282. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1283. } else {
  1284. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1285. POSTING_READ(DPLL_MD(pipe));
  1286. }
  1287. }
  1288. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1289. {
  1290. struct intel_crtc *crtc;
  1291. int count = 0;
  1292. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1293. count += crtc->base.state->active &&
  1294. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1295. }
  1296. return count;
  1297. }
  1298. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1299. const struct intel_crtc_state *crtc_state)
  1300. {
  1301. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1302. i915_reg_t reg = DPLL(crtc->pipe);
  1303. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1304. int i;
  1305. assert_pipe_disabled(dev_priv, crtc->pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1308. assert_panel_unlocked(dev_priv, crtc->pipe);
  1309. /* Enable DVO 2x clock on both PLLs if necessary */
  1310. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1311. /*
  1312. * It appears to be important that we don't enable this
  1313. * for the current pipe before otherwise configuring the
  1314. * PLL. No idea how this should be handled if multiple
  1315. * DVO outputs are enabled simultaneosly.
  1316. */
  1317. dpll |= DPLL_DVO_2X_MODE;
  1318. I915_WRITE(DPLL(!crtc->pipe),
  1319. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1320. }
  1321. /*
  1322. * Apparently we need to have VGA mode enabled prior to changing
  1323. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1324. * dividers, even though the register value does change.
  1325. */
  1326. I915_WRITE(reg, 0);
  1327. I915_WRITE(reg, dpll);
  1328. /* Wait for the clocks to stabilize. */
  1329. POSTING_READ(reg);
  1330. udelay(150);
  1331. if (INTEL_GEN(dev_priv) >= 4) {
  1332. I915_WRITE(DPLL_MD(crtc->pipe),
  1333. crtc_state->dpll_hw_state.dpll_md);
  1334. } else {
  1335. /* The pixel multiplier can only be updated once the
  1336. * DPLL is enabled and the clocks are stable.
  1337. *
  1338. * So write it again.
  1339. */
  1340. I915_WRITE(reg, dpll);
  1341. }
  1342. /* We do this three times for luck */
  1343. for (i = 0; i < 3; i++) {
  1344. I915_WRITE(reg, dpll);
  1345. POSTING_READ(reg);
  1346. udelay(150); /* wait for warmup */
  1347. }
  1348. }
  1349. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1350. {
  1351. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1352. enum pipe pipe = crtc->pipe;
  1353. /* Disable DVO 2x clock on both PLLs if necessary */
  1354. if (IS_I830(dev_priv) &&
  1355. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1356. !intel_num_dvo_pipes(dev_priv)) {
  1357. I915_WRITE(DPLL(PIPE_B),
  1358. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1359. I915_WRITE(DPLL(PIPE_A),
  1360. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1361. }
  1362. /* Don't disable pipe or pipe PLLs if needed */
  1363. if (IS_I830(dev_priv))
  1364. return;
  1365. /* Make sure the pipe isn't still relying on us */
  1366. assert_pipe_disabled(dev_priv, pipe);
  1367. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1368. POSTING_READ(DPLL(pipe));
  1369. }
  1370. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1371. {
  1372. u32 val;
  1373. /* Make sure the pipe isn't still relying on us */
  1374. assert_pipe_disabled(dev_priv, pipe);
  1375. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1376. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1377. if (pipe != PIPE_A)
  1378. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1379. I915_WRITE(DPLL(pipe), val);
  1380. POSTING_READ(DPLL(pipe));
  1381. }
  1382. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1383. {
  1384. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1385. u32 val;
  1386. /* Make sure the pipe isn't still relying on us */
  1387. assert_pipe_disabled(dev_priv, pipe);
  1388. val = DPLL_SSC_REF_CLK_CHV |
  1389. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1390. if (pipe != PIPE_A)
  1391. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1392. I915_WRITE(DPLL(pipe), val);
  1393. POSTING_READ(DPLL(pipe));
  1394. mutex_lock(&dev_priv->sb_lock);
  1395. /* Disable 10bit clock to display controller */
  1396. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1397. val &= ~DPIO_DCLKP_EN;
  1398. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1399. mutex_unlock(&dev_priv->sb_lock);
  1400. }
  1401. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1402. struct intel_digital_port *dport,
  1403. unsigned int expected_mask)
  1404. {
  1405. u32 port_mask;
  1406. i915_reg_t dpll_reg;
  1407. switch (dport->base.port) {
  1408. case PORT_B:
  1409. port_mask = DPLL_PORTB_READY_MASK;
  1410. dpll_reg = DPLL(0);
  1411. break;
  1412. case PORT_C:
  1413. port_mask = DPLL_PORTC_READY_MASK;
  1414. dpll_reg = DPLL(0);
  1415. expected_mask <<= 4;
  1416. break;
  1417. case PORT_D:
  1418. port_mask = DPLL_PORTD_READY_MASK;
  1419. dpll_reg = DPIO_PHY_STATUS;
  1420. break;
  1421. default:
  1422. BUG();
  1423. }
  1424. if (intel_wait_for_register(dev_priv,
  1425. dpll_reg, port_mask, expected_mask,
  1426. 1000))
  1427. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1428. port_name(dport->base.port),
  1429. I915_READ(dpll_reg) & port_mask, expected_mask);
  1430. }
  1431. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1432. enum pipe pipe)
  1433. {
  1434. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1435. pipe);
  1436. i915_reg_t reg;
  1437. uint32_t val, pipeconf_val;
  1438. /* Make sure PCH DPLL is enabled */
  1439. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1440. /* FDI must be feeding us bits for PCH ports */
  1441. assert_fdi_tx_enabled(dev_priv, pipe);
  1442. assert_fdi_rx_enabled(dev_priv, pipe);
  1443. if (HAS_PCH_CPT(dev_priv)) {
  1444. /* Workaround: Set the timing override bit before enabling the
  1445. * pch transcoder. */
  1446. reg = TRANS_CHICKEN2(pipe);
  1447. val = I915_READ(reg);
  1448. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1449. I915_WRITE(reg, val);
  1450. }
  1451. reg = PCH_TRANSCONF(pipe);
  1452. val = I915_READ(reg);
  1453. pipeconf_val = I915_READ(PIPECONF(pipe));
  1454. if (HAS_PCH_IBX(dev_priv)) {
  1455. /*
  1456. * Make the BPC in transcoder be consistent with
  1457. * that in pipeconf reg. For HDMI we must use 8bpc
  1458. * here for both 8bpc and 12bpc.
  1459. */
  1460. val &= ~PIPECONF_BPC_MASK;
  1461. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1462. val |= PIPECONF_8BPC;
  1463. else
  1464. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1465. }
  1466. val &= ~TRANS_INTERLACE_MASK;
  1467. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1468. if (HAS_PCH_IBX(dev_priv) &&
  1469. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1470. val |= TRANS_LEGACY_INTERLACED_ILK;
  1471. else
  1472. val |= TRANS_INTERLACED;
  1473. else
  1474. val |= TRANS_PROGRESSIVE;
  1475. I915_WRITE(reg, val | TRANS_ENABLE);
  1476. if (intel_wait_for_register(dev_priv,
  1477. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1478. 100))
  1479. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1480. }
  1481. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1482. enum transcoder cpu_transcoder)
  1483. {
  1484. u32 val, pipeconf_val;
  1485. /* FDI must be feeding us bits for PCH ports */
  1486. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1487. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1488. /* Workaround: set timing override bit. */
  1489. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1490. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1491. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1492. val = TRANS_ENABLE;
  1493. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1494. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1495. PIPECONF_INTERLACED_ILK)
  1496. val |= TRANS_INTERLACED;
  1497. else
  1498. val |= TRANS_PROGRESSIVE;
  1499. I915_WRITE(LPT_TRANSCONF, val);
  1500. if (intel_wait_for_register(dev_priv,
  1501. LPT_TRANSCONF,
  1502. TRANS_STATE_ENABLE,
  1503. TRANS_STATE_ENABLE,
  1504. 100))
  1505. DRM_ERROR("Failed to enable PCH transcoder\n");
  1506. }
  1507. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1508. enum pipe pipe)
  1509. {
  1510. i915_reg_t reg;
  1511. uint32_t val;
  1512. /* FDI relies on the transcoder */
  1513. assert_fdi_tx_disabled(dev_priv, pipe);
  1514. assert_fdi_rx_disabled(dev_priv, pipe);
  1515. /* Ports must be off as well */
  1516. assert_pch_ports_disabled(dev_priv, pipe);
  1517. reg = PCH_TRANSCONF(pipe);
  1518. val = I915_READ(reg);
  1519. val &= ~TRANS_ENABLE;
  1520. I915_WRITE(reg, val);
  1521. /* wait for PCH transcoder off, transcoder state */
  1522. if (intel_wait_for_register(dev_priv,
  1523. reg, TRANS_STATE_ENABLE, 0,
  1524. 50))
  1525. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1526. if (HAS_PCH_CPT(dev_priv)) {
  1527. /* Workaround: Clear the timing override chicken bit again. */
  1528. reg = TRANS_CHICKEN2(pipe);
  1529. val = I915_READ(reg);
  1530. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1531. I915_WRITE(reg, val);
  1532. }
  1533. }
  1534. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1535. {
  1536. u32 val;
  1537. val = I915_READ(LPT_TRANSCONF);
  1538. val &= ~TRANS_ENABLE;
  1539. I915_WRITE(LPT_TRANSCONF, val);
  1540. /* wait for PCH transcoder off, transcoder state */
  1541. if (intel_wait_for_register(dev_priv,
  1542. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1543. 50))
  1544. DRM_ERROR("Failed to disable PCH transcoder\n");
  1545. /* Workaround: clear timing override bit. */
  1546. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1547. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1548. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1549. }
  1550. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1551. {
  1552. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1553. if (HAS_PCH_LPT(dev_priv))
  1554. return PIPE_A;
  1555. else
  1556. return crtc->pipe;
  1557. }
  1558. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1559. {
  1560. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1562. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1563. enum pipe pipe = crtc->pipe;
  1564. i915_reg_t reg;
  1565. u32 val;
  1566. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1567. assert_planes_disabled(crtc);
  1568. /*
  1569. * A pipe without a PLL won't actually be able to drive bits from
  1570. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1571. * need the check.
  1572. */
  1573. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1574. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1575. assert_dsi_pll_enabled(dev_priv);
  1576. else
  1577. assert_pll_enabled(dev_priv, pipe);
  1578. } else {
  1579. if (new_crtc_state->has_pch_encoder) {
  1580. /* if driving the PCH, we need FDI enabled */
  1581. assert_fdi_rx_pll_enabled(dev_priv,
  1582. intel_crtc_pch_transcoder(crtc));
  1583. assert_fdi_tx_pll_enabled(dev_priv,
  1584. (enum pipe) cpu_transcoder);
  1585. }
  1586. /* FIXME: assert CPU port conditions for SNB+ */
  1587. }
  1588. reg = PIPECONF(cpu_transcoder);
  1589. val = I915_READ(reg);
  1590. if (val & PIPECONF_ENABLE) {
  1591. /* we keep both pipes enabled on 830 */
  1592. WARN_ON(!IS_I830(dev_priv));
  1593. return;
  1594. }
  1595. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1596. POSTING_READ(reg);
  1597. /*
  1598. * Until the pipe starts PIPEDSL reads will return a stale value,
  1599. * which causes an apparent vblank timestamp jump when PIPEDSL
  1600. * resets to its proper value. That also messes up the frame count
  1601. * when it's derived from the timestamps. So let's wait for the
  1602. * pipe to start properly before we call drm_crtc_vblank_on()
  1603. */
  1604. if (dev_priv->drm.max_vblank_count == 0)
  1605. intel_wait_for_pipe_scanline_moving(crtc);
  1606. }
  1607. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1608. {
  1609. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1610. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1611. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1612. enum pipe pipe = crtc->pipe;
  1613. i915_reg_t reg;
  1614. u32 val;
  1615. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1616. /*
  1617. * Make sure planes won't keep trying to pump pixels to us,
  1618. * or we might hang the display.
  1619. */
  1620. assert_planes_disabled(crtc);
  1621. reg = PIPECONF(cpu_transcoder);
  1622. val = I915_READ(reg);
  1623. if ((val & PIPECONF_ENABLE) == 0)
  1624. return;
  1625. /*
  1626. * Double wide has implications for planes
  1627. * so best keep it disabled when not needed.
  1628. */
  1629. if (old_crtc_state->double_wide)
  1630. val &= ~PIPECONF_DOUBLE_WIDE;
  1631. /* Don't disable pipe or pipe PLLs if needed */
  1632. if (!IS_I830(dev_priv))
  1633. val &= ~PIPECONF_ENABLE;
  1634. I915_WRITE(reg, val);
  1635. if ((val & PIPECONF_ENABLE) == 0)
  1636. intel_wait_for_pipe_off(old_crtc_state);
  1637. }
  1638. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1639. {
  1640. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1641. }
  1642. static unsigned int
  1643. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1644. {
  1645. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1646. unsigned int cpp = fb->format->cpp[plane];
  1647. switch (fb->modifier) {
  1648. case DRM_FORMAT_MOD_LINEAR:
  1649. return cpp;
  1650. case I915_FORMAT_MOD_X_TILED:
  1651. if (IS_GEN2(dev_priv))
  1652. return 128;
  1653. else
  1654. return 512;
  1655. case I915_FORMAT_MOD_Y_TILED_CCS:
  1656. if (plane == 1)
  1657. return 128;
  1658. /* fall through */
  1659. case I915_FORMAT_MOD_Y_TILED:
  1660. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1661. return 128;
  1662. else
  1663. return 512;
  1664. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1665. if (plane == 1)
  1666. return 128;
  1667. /* fall through */
  1668. case I915_FORMAT_MOD_Yf_TILED:
  1669. switch (cpp) {
  1670. case 1:
  1671. return 64;
  1672. case 2:
  1673. case 4:
  1674. return 128;
  1675. case 8:
  1676. case 16:
  1677. return 256;
  1678. default:
  1679. MISSING_CASE(cpp);
  1680. return cpp;
  1681. }
  1682. break;
  1683. default:
  1684. MISSING_CASE(fb->modifier);
  1685. return cpp;
  1686. }
  1687. }
  1688. static unsigned int
  1689. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1690. {
  1691. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1692. return 1;
  1693. else
  1694. return intel_tile_size(to_i915(fb->dev)) /
  1695. intel_tile_width_bytes(fb, plane);
  1696. }
  1697. /* Return the tile dimensions in pixel units */
  1698. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1699. unsigned int *tile_width,
  1700. unsigned int *tile_height)
  1701. {
  1702. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1703. unsigned int cpp = fb->format->cpp[plane];
  1704. *tile_width = tile_width_bytes / cpp;
  1705. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1706. }
  1707. unsigned int
  1708. intel_fb_align_height(const struct drm_framebuffer *fb,
  1709. int plane, unsigned int height)
  1710. {
  1711. unsigned int tile_height = intel_tile_height(fb, plane);
  1712. return ALIGN(height, tile_height);
  1713. }
  1714. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1715. {
  1716. unsigned int size = 0;
  1717. int i;
  1718. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1719. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1720. return size;
  1721. }
  1722. static void
  1723. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1724. const struct drm_framebuffer *fb,
  1725. unsigned int rotation)
  1726. {
  1727. view->type = I915_GGTT_VIEW_NORMAL;
  1728. if (drm_rotation_90_or_270(rotation)) {
  1729. view->type = I915_GGTT_VIEW_ROTATED;
  1730. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1731. }
  1732. }
  1733. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1734. {
  1735. if (IS_I830(dev_priv))
  1736. return 16 * 1024;
  1737. else if (IS_I85X(dev_priv))
  1738. return 256;
  1739. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1740. return 32;
  1741. else
  1742. return 4 * 1024;
  1743. }
  1744. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1745. {
  1746. if (INTEL_GEN(dev_priv) >= 9)
  1747. return 256 * 1024;
  1748. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1749. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1750. return 128 * 1024;
  1751. else if (INTEL_GEN(dev_priv) >= 4)
  1752. return 4 * 1024;
  1753. else
  1754. return 0;
  1755. }
  1756. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1757. int plane)
  1758. {
  1759. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1760. /* AUX_DIST needs only 4K alignment */
  1761. if (plane == 1)
  1762. return 4096;
  1763. switch (fb->modifier) {
  1764. case DRM_FORMAT_MOD_LINEAR:
  1765. return intel_linear_alignment(dev_priv);
  1766. case I915_FORMAT_MOD_X_TILED:
  1767. if (INTEL_GEN(dev_priv) >= 9)
  1768. return 256 * 1024;
  1769. return 0;
  1770. case I915_FORMAT_MOD_Y_TILED_CCS:
  1771. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1772. case I915_FORMAT_MOD_Y_TILED:
  1773. case I915_FORMAT_MOD_Yf_TILED:
  1774. return 1 * 1024 * 1024;
  1775. default:
  1776. MISSING_CASE(fb->modifier);
  1777. return 0;
  1778. }
  1779. }
  1780. static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
  1781. {
  1782. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1783. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1784. return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
  1785. }
  1786. struct i915_vma *
  1787. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1788. unsigned int rotation,
  1789. bool uses_fence,
  1790. unsigned long *out_flags)
  1791. {
  1792. struct drm_device *dev = fb->dev;
  1793. struct drm_i915_private *dev_priv = to_i915(dev);
  1794. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1795. struct i915_ggtt_view view;
  1796. struct i915_vma *vma;
  1797. unsigned int pinctl;
  1798. u32 alignment;
  1799. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1800. alignment = intel_surf_alignment(fb, 0);
  1801. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1802. /* Note that the w/a also requires 64 PTE of padding following the
  1803. * bo. We currently fill all unused PTE with the shadow page and so
  1804. * we should always have valid PTE following the scanout preventing
  1805. * the VT-d warning.
  1806. */
  1807. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1808. alignment = 256 * 1024;
  1809. /*
  1810. * Global gtt pte registers are special registers which actually forward
  1811. * writes to a chunk of system memory. Which means that there is no risk
  1812. * that the register values disappear as soon as we call
  1813. * intel_runtime_pm_put(), so it is correct to wrap only the
  1814. * pin/unpin/fence and not more.
  1815. */
  1816. intel_runtime_pm_get(dev_priv);
  1817. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1818. pinctl = 0;
  1819. /* Valleyview is definitely limited to scanning out the first
  1820. * 512MiB. Lets presume this behaviour was inherited from the
  1821. * g4x display engine and that all earlier gen are similarly
  1822. * limited. Testing suggests that it is a little more
  1823. * complicated than this. For example, Cherryview appears quite
  1824. * happy to scanout from anywhere within its global aperture.
  1825. */
  1826. if (HAS_GMCH_DISPLAY(dev_priv))
  1827. pinctl |= PIN_MAPPABLE;
  1828. vma = i915_gem_object_pin_to_display_plane(obj,
  1829. alignment, &view, pinctl);
  1830. if (IS_ERR(vma))
  1831. goto err;
  1832. if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
  1833. int ret;
  1834. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1835. * fence, whereas 965+ only requires a fence if using
  1836. * framebuffer compression. For simplicity, we always, when
  1837. * possible, install a fence as the cost is not that onerous.
  1838. *
  1839. * If we fail to fence the tiled scanout, then either the
  1840. * modeset will reject the change (which is highly unlikely as
  1841. * the affected systems, all but one, do not have unmappable
  1842. * space) or we will not be able to enable full powersaving
  1843. * techniques (also likely not to apply due to various limits
  1844. * FBC and the like impose on the size of the buffer, which
  1845. * presumably we violated anyway with this unmappable buffer).
  1846. * Anyway, it is presumably better to stumble onwards with
  1847. * something and try to run the system in a "less than optimal"
  1848. * mode that matches the user configuration.
  1849. */
  1850. ret = i915_vma_pin_fence(vma);
  1851. if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
  1852. i915_gem_object_unpin_from_display_plane(vma);
  1853. vma = ERR_PTR(ret);
  1854. goto err;
  1855. }
  1856. if (ret == 0 && vma->fence)
  1857. *out_flags |= PLANE_HAS_FENCE;
  1858. }
  1859. i915_vma_get(vma);
  1860. err:
  1861. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1862. intel_runtime_pm_put(dev_priv);
  1863. return vma;
  1864. }
  1865. void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
  1866. {
  1867. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1868. if (flags & PLANE_HAS_FENCE)
  1869. i915_vma_unpin_fence(vma);
  1870. i915_gem_object_unpin_from_display_plane(vma);
  1871. i915_vma_put(vma);
  1872. }
  1873. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1874. unsigned int rotation)
  1875. {
  1876. if (drm_rotation_90_or_270(rotation))
  1877. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1878. else
  1879. return fb->pitches[plane];
  1880. }
  1881. /*
  1882. * Convert the x/y offsets into a linear offset.
  1883. * Only valid with 0/180 degree rotation, which is fine since linear
  1884. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1885. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1886. */
  1887. u32 intel_fb_xy_to_linear(int x, int y,
  1888. const struct intel_plane_state *state,
  1889. int plane)
  1890. {
  1891. const struct drm_framebuffer *fb = state->base.fb;
  1892. unsigned int cpp = fb->format->cpp[plane];
  1893. unsigned int pitch = fb->pitches[plane];
  1894. return y * pitch + x * cpp;
  1895. }
  1896. /*
  1897. * Add the x/y offsets derived from fb->offsets[] to the user
  1898. * specified plane src x/y offsets. The resulting x/y offsets
  1899. * specify the start of scanout from the beginning of the gtt mapping.
  1900. */
  1901. void intel_add_fb_offsets(int *x, int *y,
  1902. const struct intel_plane_state *state,
  1903. int plane)
  1904. {
  1905. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1906. unsigned int rotation = state->base.rotation;
  1907. if (drm_rotation_90_or_270(rotation)) {
  1908. *x += intel_fb->rotated[plane].x;
  1909. *y += intel_fb->rotated[plane].y;
  1910. } else {
  1911. *x += intel_fb->normal[plane].x;
  1912. *y += intel_fb->normal[plane].y;
  1913. }
  1914. }
  1915. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1916. unsigned int tile_width,
  1917. unsigned int tile_height,
  1918. unsigned int tile_size,
  1919. unsigned int pitch_tiles,
  1920. u32 old_offset,
  1921. u32 new_offset)
  1922. {
  1923. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1924. unsigned int tiles;
  1925. WARN_ON(old_offset & (tile_size - 1));
  1926. WARN_ON(new_offset & (tile_size - 1));
  1927. WARN_ON(new_offset > old_offset);
  1928. tiles = (old_offset - new_offset) / tile_size;
  1929. *y += tiles / pitch_tiles * tile_height;
  1930. *x += tiles % pitch_tiles * tile_width;
  1931. /* minimize x in case it got needlessly big */
  1932. *y += *x / pitch_pixels * tile_height;
  1933. *x %= pitch_pixels;
  1934. return new_offset;
  1935. }
  1936. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1937. const struct drm_framebuffer *fb, int plane,
  1938. unsigned int rotation,
  1939. u32 old_offset, u32 new_offset)
  1940. {
  1941. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1942. unsigned int cpp = fb->format->cpp[plane];
  1943. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1944. WARN_ON(new_offset > old_offset);
  1945. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1946. unsigned int tile_size, tile_width, tile_height;
  1947. unsigned int pitch_tiles;
  1948. tile_size = intel_tile_size(dev_priv);
  1949. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1950. if (drm_rotation_90_or_270(rotation)) {
  1951. pitch_tiles = pitch / tile_height;
  1952. swap(tile_width, tile_height);
  1953. } else {
  1954. pitch_tiles = pitch / (tile_width * cpp);
  1955. }
  1956. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1957. tile_size, pitch_tiles,
  1958. old_offset, new_offset);
  1959. } else {
  1960. old_offset += *y * pitch + *x * cpp;
  1961. *y = (old_offset - new_offset) / pitch;
  1962. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1963. }
  1964. return new_offset;
  1965. }
  1966. /*
  1967. * Adjust the tile offset by moving the difference into
  1968. * the x/y offsets.
  1969. */
  1970. static u32 intel_adjust_tile_offset(int *x, int *y,
  1971. const struct intel_plane_state *state, int plane,
  1972. u32 old_offset, u32 new_offset)
  1973. {
  1974. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  1975. state->base.rotation,
  1976. old_offset, new_offset);
  1977. }
  1978. /*
  1979. * Computes the linear offset to the base tile and adjusts
  1980. * x, y. bytes per pixel is assumed to be a power-of-two.
  1981. *
  1982. * In the 90/270 rotated case, x and y are assumed
  1983. * to be already rotated to match the rotated GTT view, and
  1984. * pitch is the tile_height aligned framebuffer height.
  1985. *
  1986. * This function is used when computing the derived information
  1987. * under intel_framebuffer, so using any of that information
  1988. * here is not allowed. Anything under drm_framebuffer can be
  1989. * used. This is why the user has to pass in the pitch since it
  1990. * is specified in the rotated orientation.
  1991. */
  1992. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  1993. int *x, int *y,
  1994. const struct drm_framebuffer *fb, int plane,
  1995. unsigned int pitch,
  1996. unsigned int rotation,
  1997. u32 alignment)
  1998. {
  1999. uint64_t fb_modifier = fb->modifier;
  2000. unsigned int cpp = fb->format->cpp[plane];
  2001. u32 offset, offset_aligned;
  2002. if (alignment)
  2003. alignment--;
  2004. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2005. unsigned int tile_size, tile_width, tile_height;
  2006. unsigned int tile_rows, tiles, pitch_tiles;
  2007. tile_size = intel_tile_size(dev_priv);
  2008. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2009. if (drm_rotation_90_or_270(rotation)) {
  2010. pitch_tiles = pitch / tile_height;
  2011. swap(tile_width, tile_height);
  2012. } else {
  2013. pitch_tiles = pitch / (tile_width * cpp);
  2014. }
  2015. tile_rows = *y / tile_height;
  2016. *y %= tile_height;
  2017. tiles = *x / tile_width;
  2018. *x %= tile_width;
  2019. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2020. offset_aligned = offset & ~alignment;
  2021. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2022. tile_size, pitch_tiles,
  2023. offset, offset_aligned);
  2024. } else {
  2025. offset = *y * pitch + *x * cpp;
  2026. offset_aligned = offset & ~alignment;
  2027. *y = (offset & alignment) / pitch;
  2028. *x = ((offset & alignment) - *y * pitch) / cpp;
  2029. }
  2030. return offset_aligned;
  2031. }
  2032. u32 intel_compute_tile_offset(int *x, int *y,
  2033. const struct intel_plane_state *state,
  2034. int plane)
  2035. {
  2036. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2037. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2038. const struct drm_framebuffer *fb = state->base.fb;
  2039. unsigned int rotation = state->base.rotation;
  2040. int pitch = intel_fb_pitch(fb, plane, rotation);
  2041. u32 alignment;
  2042. if (intel_plane->id == PLANE_CURSOR)
  2043. alignment = intel_cursor_alignment(dev_priv);
  2044. else
  2045. alignment = intel_surf_alignment(fb, plane);
  2046. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2047. rotation, alignment);
  2048. }
  2049. /* Convert the fb->offset[] into x/y offsets */
  2050. static int intel_fb_offset_to_xy(int *x, int *y,
  2051. const struct drm_framebuffer *fb, int plane)
  2052. {
  2053. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2054. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2055. fb->offsets[plane] % intel_tile_size(dev_priv))
  2056. return -EINVAL;
  2057. *x = 0;
  2058. *y = 0;
  2059. _intel_adjust_tile_offset(x, y,
  2060. fb, plane, DRM_MODE_ROTATE_0,
  2061. fb->offsets[plane], 0);
  2062. return 0;
  2063. }
  2064. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2065. {
  2066. switch (fb_modifier) {
  2067. case I915_FORMAT_MOD_X_TILED:
  2068. return I915_TILING_X;
  2069. case I915_FORMAT_MOD_Y_TILED:
  2070. case I915_FORMAT_MOD_Y_TILED_CCS:
  2071. return I915_TILING_Y;
  2072. default:
  2073. return I915_TILING_NONE;
  2074. }
  2075. }
  2076. /*
  2077. * From the Sky Lake PRM:
  2078. * "The Color Control Surface (CCS) contains the compression status of
  2079. * the cache-line pairs. The compression state of the cache-line pair
  2080. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2081. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2082. * cache-line-pairs. CCS is always Y tiled."
  2083. *
  2084. * Since cache line pairs refers to horizontally adjacent cache lines,
  2085. * each cache line in the CCS corresponds to an area of 32x16 cache
  2086. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2087. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2088. * main surface.
  2089. */
  2090. static const struct drm_format_info ccs_formats[] = {
  2091. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2092. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2093. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2094. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2095. };
  2096. static const struct drm_format_info *
  2097. lookup_format_info(const struct drm_format_info formats[],
  2098. int num_formats, u32 format)
  2099. {
  2100. int i;
  2101. for (i = 0; i < num_formats; i++) {
  2102. if (formats[i].format == format)
  2103. return &formats[i];
  2104. }
  2105. return NULL;
  2106. }
  2107. static const struct drm_format_info *
  2108. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2109. {
  2110. switch (cmd->modifier[0]) {
  2111. case I915_FORMAT_MOD_Y_TILED_CCS:
  2112. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2113. return lookup_format_info(ccs_formats,
  2114. ARRAY_SIZE(ccs_formats),
  2115. cmd->pixel_format);
  2116. default:
  2117. return NULL;
  2118. }
  2119. }
  2120. static int
  2121. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2122. struct drm_framebuffer *fb)
  2123. {
  2124. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2125. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2126. u32 gtt_offset_rotated = 0;
  2127. unsigned int max_size = 0;
  2128. int i, num_planes = fb->format->num_planes;
  2129. unsigned int tile_size = intel_tile_size(dev_priv);
  2130. for (i = 0; i < num_planes; i++) {
  2131. unsigned int width, height;
  2132. unsigned int cpp, size;
  2133. u32 offset;
  2134. int x, y;
  2135. int ret;
  2136. cpp = fb->format->cpp[i];
  2137. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2138. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2139. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2140. if (ret) {
  2141. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2142. i, fb->offsets[i]);
  2143. return ret;
  2144. }
  2145. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2146. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2147. int hsub = fb->format->hsub;
  2148. int vsub = fb->format->vsub;
  2149. int tile_width, tile_height;
  2150. int main_x, main_y;
  2151. int ccs_x, ccs_y;
  2152. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2153. tile_width *= hsub;
  2154. tile_height *= vsub;
  2155. ccs_x = (x * hsub) % tile_width;
  2156. ccs_y = (y * vsub) % tile_height;
  2157. main_x = intel_fb->normal[0].x % tile_width;
  2158. main_y = intel_fb->normal[0].y % tile_height;
  2159. /*
  2160. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2161. * x/y offsets must match between CCS and the main surface.
  2162. */
  2163. if (main_x != ccs_x || main_y != ccs_y) {
  2164. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2165. main_x, main_y,
  2166. ccs_x, ccs_y,
  2167. intel_fb->normal[0].x,
  2168. intel_fb->normal[0].y,
  2169. x, y);
  2170. return -EINVAL;
  2171. }
  2172. }
  2173. /*
  2174. * The fence (if used) is aligned to the start of the object
  2175. * so having the framebuffer wrap around across the edge of the
  2176. * fenced region doesn't really work. We have no API to configure
  2177. * the fence start offset within the object (nor could we probably
  2178. * on gen2/3). So it's just easier if we just require that the
  2179. * fb layout agrees with the fence layout. We already check that the
  2180. * fb stride matches the fence stride elsewhere.
  2181. */
  2182. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2183. (x + width) * cpp > fb->pitches[i]) {
  2184. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2185. i, fb->offsets[i]);
  2186. return -EINVAL;
  2187. }
  2188. /*
  2189. * First pixel of the framebuffer from
  2190. * the start of the normal gtt mapping.
  2191. */
  2192. intel_fb->normal[i].x = x;
  2193. intel_fb->normal[i].y = y;
  2194. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2195. fb, i, fb->pitches[i],
  2196. DRM_MODE_ROTATE_0, tile_size);
  2197. offset /= tile_size;
  2198. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2199. unsigned int tile_width, tile_height;
  2200. unsigned int pitch_tiles;
  2201. struct drm_rect r;
  2202. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2203. rot_info->plane[i].offset = offset;
  2204. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2205. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2206. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2207. intel_fb->rotated[i].pitch =
  2208. rot_info->plane[i].height * tile_height;
  2209. /* how many tiles does this plane need */
  2210. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2211. /*
  2212. * If the plane isn't horizontally tile aligned,
  2213. * we need one more tile.
  2214. */
  2215. if (x != 0)
  2216. size++;
  2217. /* rotate the x/y offsets to match the GTT view */
  2218. r.x1 = x;
  2219. r.y1 = y;
  2220. r.x2 = x + width;
  2221. r.y2 = y + height;
  2222. drm_rect_rotate(&r,
  2223. rot_info->plane[i].width * tile_width,
  2224. rot_info->plane[i].height * tile_height,
  2225. DRM_MODE_ROTATE_270);
  2226. x = r.x1;
  2227. y = r.y1;
  2228. /* rotate the tile dimensions to match the GTT view */
  2229. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2230. swap(tile_width, tile_height);
  2231. /*
  2232. * We only keep the x/y offsets, so push all of the
  2233. * gtt offset into the x/y offsets.
  2234. */
  2235. __intel_adjust_tile_offset(&x, &y,
  2236. tile_width, tile_height,
  2237. tile_size, pitch_tiles,
  2238. gtt_offset_rotated * tile_size, 0);
  2239. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2240. /*
  2241. * First pixel of the framebuffer from
  2242. * the start of the rotated gtt mapping.
  2243. */
  2244. intel_fb->rotated[i].x = x;
  2245. intel_fb->rotated[i].y = y;
  2246. } else {
  2247. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2248. x * cpp, tile_size);
  2249. }
  2250. /* how many tiles in total needed in the bo */
  2251. max_size = max(max_size, offset + size);
  2252. }
  2253. if (max_size * tile_size > intel_fb->obj->base.size) {
  2254. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2255. max_size * tile_size, intel_fb->obj->base.size);
  2256. return -EINVAL;
  2257. }
  2258. return 0;
  2259. }
  2260. static int i9xx_format_to_fourcc(int format)
  2261. {
  2262. switch (format) {
  2263. case DISPPLANE_8BPP:
  2264. return DRM_FORMAT_C8;
  2265. case DISPPLANE_BGRX555:
  2266. return DRM_FORMAT_XRGB1555;
  2267. case DISPPLANE_BGRX565:
  2268. return DRM_FORMAT_RGB565;
  2269. default:
  2270. case DISPPLANE_BGRX888:
  2271. return DRM_FORMAT_XRGB8888;
  2272. case DISPPLANE_RGBX888:
  2273. return DRM_FORMAT_XBGR8888;
  2274. case DISPPLANE_BGRX101010:
  2275. return DRM_FORMAT_XRGB2101010;
  2276. case DISPPLANE_RGBX101010:
  2277. return DRM_FORMAT_XBGR2101010;
  2278. }
  2279. }
  2280. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2281. {
  2282. switch (format) {
  2283. case PLANE_CTL_FORMAT_RGB_565:
  2284. return DRM_FORMAT_RGB565;
  2285. default:
  2286. case PLANE_CTL_FORMAT_XRGB_8888:
  2287. if (rgb_order) {
  2288. if (alpha)
  2289. return DRM_FORMAT_ABGR8888;
  2290. else
  2291. return DRM_FORMAT_XBGR8888;
  2292. } else {
  2293. if (alpha)
  2294. return DRM_FORMAT_ARGB8888;
  2295. else
  2296. return DRM_FORMAT_XRGB8888;
  2297. }
  2298. case PLANE_CTL_FORMAT_XRGB_2101010:
  2299. if (rgb_order)
  2300. return DRM_FORMAT_XBGR2101010;
  2301. else
  2302. return DRM_FORMAT_XRGB2101010;
  2303. }
  2304. }
  2305. static bool
  2306. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2307. struct intel_initial_plane_config *plane_config)
  2308. {
  2309. struct drm_device *dev = crtc->base.dev;
  2310. struct drm_i915_private *dev_priv = to_i915(dev);
  2311. struct drm_i915_gem_object *obj = NULL;
  2312. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2313. struct drm_framebuffer *fb = &plane_config->fb->base;
  2314. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2315. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2316. PAGE_SIZE);
  2317. size_aligned -= base_aligned;
  2318. if (plane_config->size == 0)
  2319. return false;
  2320. /* If the FB is too big, just don't use it since fbdev is not very
  2321. * important and we should probably use that space with FBC or other
  2322. * features. */
  2323. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2324. return false;
  2325. mutex_lock(&dev->struct_mutex);
  2326. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2327. base_aligned,
  2328. base_aligned,
  2329. size_aligned);
  2330. mutex_unlock(&dev->struct_mutex);
  2331. if (!obj)
  2332. return false;
  2333. if (plane_config->tiling == I915_TILING_X)
  2334. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2335. mode_cmd.pixel_format = fb->format->format;
  2336. mode_cmd.width = fb->width;
  2337. mode_cmd.height = fb->height;
  2338. mode_cmd.pitches[0] = fb->pitches[0];
  2339. mode_cmd.modifier[0] = fb->modifier;
  2340. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2341. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2342. DRM_DEBUG_KMS("intel fb init failed\n");
  2343. goto out_unref_obj;
  2344. }
  2345. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2346. return true;
  2347. out_unref_obj:
  2348. i915_gem_object_put(obj);
  2349. return false;
  2350. }
  2351. static void
  2352. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2353. struct intel_plane_state *plane_state,
  2354. bool visible)
  2355. {
  2356. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2357. plane_state->base.visible = visible;
  2358. /* FIXME pre-g4x don't work like this */
  2359. if (visible) {
  2360. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2361. crtc_state->active_planes |= BIT(plane->id);
  2362. } else {
  2363. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2364. crtc_state->active_planes &= ~BIT(plane->id);
  2365. }
  2366. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2367. crtc_state->base.crtc->name,
  2368. crtc_state->active_planes);
  2369. }
  2370. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2371. struct intel_plane *plane)
  2372. {
  2373. struct intel_crtc_state *crtc_state =
  2374. to_intel_crtc_state(crtc->base.state);
  2375. struct intel_plane_state *plane_state =
  2376. to_intel_plane_state(plane->base.state);
  2377. intel_set_plane_visible(crtc_state, plane_state, false);
  2378. if (plane->id == PLANE_PRIMARY)
  2379. intel_pre_disable_primary_noatomic(&crtc->base);
  2380. trace_intel_disable_plane(&plane->base, crtc);
  2381. plane->disable_plane(plane, crtc);
  2382. }
  2383. static void
  2384. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2385. struct intel_initial_plane_config *plane_config)
  2386. {
  2387. struct drm_device *dev = intel_crtc->base.dev;
  2388. struct drm_i915_private *dev_priv = to_i915(dev);
  2389. struct drm_crtc *c;
  2390. struct drm_i915_gem_object *obj;
  2391. struct drm_plane *primary = intel_crtc->base.primary;
  2392. struct drm_plane_state *plane_state = primary->state;
  2393. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2394. struct intel_plane *intel_plane = to_intel_plane(primary);
  2395. struct intel_plane_state *intel_state =
  2396. to_intel_plane_state(plane_state);
  2397. struct drm_framebuffer *fb;
  2398. if (!plane_config->fb)
  2399. return;
  2400. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2401. fb = &plane_config->fb->base;
  2402. goto valid_fb;
  2403. }
  2404. kfree(plane_config->fb);
  2405. /*
  2406. * Failed to alloc the obj, check to see if we should share
  2407. * an fb with another CRTC instead
  2408. */
  2409. for_each_crtc(dev, c) {
  2410. struct intel_plane_state *state;
  2411. if (c == &intel_crtc->base)
  2412. continue;
  2413. if (!to_intel_crtc(c)->active)
  2414. continue;
  2415. state = to_intel_plane_state(c->primary->state);
  2416. if (!state->vma)
  2417. continue;
  2418. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2419. fb = c->primary->fb;
  2420. drm_framebuffer_get(fb);
  2421. goto valid_fb;
  2422. }
  2423. }
  2424. /*
  2425. * We've failed to reconstruct the BIOS FB. Current display state
  2426. * indicates that the primary plane is visible, but has a NULL FB,
  2427. * which will lead to problems later if we don't fix it up. The
  2428. * simplest solution is to just disable the primary plane now and
  2429. * pretend the BIOS never had it enabled.
  2430. */
  2431. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2432. return;
  2433. valid_fb:
  2434. mutex_lock(&dev->struct_mutex);
  2435. intel_state->vma =
  2436. intel_pin_and_fence_fb_obj(fb,
  2437. primary->state->rotation,
  2438. intel_plane_uses_fence(intel_state),
  2439. &intel_state->flags);
  2440. mutex_unlock(&dev->struct_mutex);
  2441. if (IS_ERR(intel_state->vma)) {
  2442. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2443. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2444. intel_state->vma = NULL;
  2445. drm_framebuffer_put(fb);
  2446. return;
  2447. }
  2448. plane_state->src_x = 0;
  2449. plane_state->src_y = 0;
  2450. plane_state->src_w = fb->width << 16;
  2451. plane_state->src_h = fb->height << 16;
  2452. plane_state->crtc_x = 0;
  2453. plane_state->crtc_y = 0;
  2454. plane_state->crtc_w = fb->width;
  2455. plane_state->crtc_h = fb->height;
  2456. intel_state->base.src = drm_plane_state_src(plane_state);
  2457. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2458. obj = intel_fb_obj(fb);
  2459. if (i915_gem_object_is_tiled(obj))
  2460. dev_priv->preserve_bios_swizzle = true;
  2461. drm_framebuffer_get(fb);
  2462. primary->fb = primary->state->fb = fb;
  2463. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2464. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2465. to_intel_plane_state(plane_state),
  2466. true);
  2467. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2468. &obj->frontbuffer_bits);
  2469. }
  2470. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2471. unsigned int rotation)
  2472. {
  2473. int cpp = fb->format->cpp[plane];
  2474. switch (fb->modifier) {
  2475. case DRM_FORMAT_MOD_LINEAR:
  2476. case I915_FORMAT_MOD_X_TILED:
  2477. switch (cpp) {
  2478. case 8:
  2479. return 4096;
  2480. case 4:
  2481. case 2:
  2482. case 1:
  2483. return 8192;
  2484. default:
  2485. MISSING_CASE(cpp);
  2486. break;
  2487. }
  2488. break;
  2489. case I915_FORMAT_MOD_Y_TILED_CCS:
  2490. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2491. /* FIXME AUX plane? */
  2492. case I915_FORMAT_MOD_Y_TILED:
  2493. case I915_FORMAT_MOD_Yf_TILED:
  2494. switch (cpp) {
  2495. case 8:
  2496. return 2048;
  2497. case 4:
  2498. return 4096;
  2499. case 2:
  2500. case 1:
  2501. return 8192;
  2502. default:
  2503. MISSING_CASE(cpp);
  2504. break;
  2505. }
  2506. break;
  2507. default:
  2508. MISSING_CASE(fb->modifier);
  2509. }
  2510. return 2048;
  2511. }
  2512. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2513. int main_x, int main_y, u32 main_offset)
  2514. {
  2515. const struct drm_framebuffer *fb = plane_state->base.fb;
  2516. int hsub = fb->format->hsub;
  2517. int vsub = fb->format->vsub;
  2518. int aux_x = plane_state->aux.x;
  2519. int aux_y = plane_state->aux.y;
  2520. u32 aux_offset = plane_state->aux.offset;
  2521. u32 alignment = intel_surf_alignment(fb, 1);
  2522. while (aux_offset >= main_offset && aux_y <= main_y) {
  2523. int x, y;
  2524. if (aux_x == main_x && aux_y == main_y)
  2525. break;
  2526. if (aux_offset == 0)
  2527. break;
  2528. x = aux_x / hsub;
  2529. y = aux_y / vsub;
  2530. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2531. aux_offset, aux_offset - alignment);
  2532. aux_x = x * hsub + aux_x % hsub;
  2533. aux_y = y * vsub + aux_y % vsub;
  2534. }
  2535. if (aux_x != main_x || aux_y != main_y)
  2536. return false;
  2537. plane_state->aux.offset = aux_offset;
  2538. plane_state->aux.x = aux_x;
  2539. plane_state->aux.y = aux_y;
  2540. return true;
  2541. }
  2542. static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
  2543. struct intel_plane_state *plane_state)
  2544. {
  2545. struct drm_i915_private *dev_priv =
  2546. to_i915(plane_state->base.plane->dev);
  2547. const struct drm_framebuffer *fb = plane_state->base.fb;
  2548. unsigned int rotation = plane_state->base.rotation;
  2549. int x = plane_state->base.src.x1 >> 16;
  2550. int y = plane_state->base.src.y1 >> 16;
  2551. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2552. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2553. int dst_x = plane_state->base.dst.x1;
  2554. int pipe_src_w = crtc_state->pipe_src_w;
  2555. int max_width = skl_max_plane_width(fb, 0, rotation);
  2556. int max_height = 4096;
  2557. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2558. if (w > max_width || h > max_height) {
  2559. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2560. w, h, max_width, max_height);
  2561. return -EINVAL;
  2562. }
  2563. /*
  2564. * Display WA #1175: cnl,glk
  2565. * Planes other than the cursor may cause FIFO underflow and display
  2566. * corruption if starting less than 4 pixels from the right edge of
  2567. * the screen.
  2568. * Besides the above WA fix the similar problem, where planes other
  2569. * than the cursor ending less than 4 pixels from the left edge of the
  2570. * screen may cause FIFO underflow and display corruption.
  2571. */
  2572. if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  2573. (dst_x + w < 4 || dst_x > pipe_src_w - 4)) {
  2574. DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
  2575. dst_x + w < 4 ? "end" : "start",
  2576. dst_x + w < 4 ? dst_x + w : dst_x,
  2577. 4, pipe_src_w - 4);
  2578. return -ERANGE;
  2579. }
  2580. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2581. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2582. alignment = intel_surf_alignment(fb, 0);
  2583. /*
  2584. * AUX surface offset is specified as the distance from the
  2585. * main surface offset, and it must be non-negative. Make
  2586. * sure that is what we will get.
  2587. */
  2588. if (offset > aux_offset)
  2589. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2590. offset, aux_offset & ~(alignment - 1));
  2591. /*
  2592. * When using an X-tiled surface, the plane blows up
  2593. * if the x offset + width exceed the stride.
  2594. *
  2595. * TODO: linear and Y-tiled seem fine, Yf untested,
  2596. */
  2597. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2598. int cpp = fb->format->cpp[0];
  2599. while ((x + w) * cpp > fb->pitches[0]) {
  2600. if (offset == 0) {
  2601. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2602. return -EINVAL;
  2603. }
  2604. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2605. offset, offset - alignment);
  2606. }
  2607. }
  2608. /*
  2609. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2610. * they match with the main surface x/y offsets.
  2611. */
  2612. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2613. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2614. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2615. if (offset == 0)
  2616. break;
  2617. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2618. offset, offset - alignment);
  2619. }
  2620. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2621. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2622. return -EINVAL;
  2623. }
  2624. }
  2625. plane_state->main.offset = offset;
  2626. plane_state->main.x = x;
  2627. plane_state->main.y = y;
  2628. return 0;
  2629. }
  2630. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2631. {
  2632. const struct drm_framebuffer *fb = plane_state->base.fb;
  2633. unsigned int rotation = plane_state->base.rotation;
  2634. int max_width = skl_max_plane_width(fb, 1, rotation);
  2635. int max_height = 4096;
  2636. int x = plane_state->base.src.x1 >> 17;
  2637. int y = plane_state->base.src.y1 >> 17;
  2638. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2639. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2640. u32 offset;
  2641. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2642. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2643. /* FIXME not quite sure how/if these apply to the chroma plane */
  2644. if (w > max_width || h > max_height) {
  2645. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2646. w, h, max_width, max_height);
  2647. return -EINVAL;
  2648. }
  2649. plane_state->aux.offset = offset;
  2650. plane_state->aux.x = x;
  2651. plane_state->aux.y = y;
  2652. return 0;
  2653. }
  2654. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2655. {
  2656. const struct drm_framebuffer *fb = plane_state->base.fb;
  2657. int src_x = plane_state->base.src.x1 >> 16;
  2658. int src_y = plane_state->base.src.y1 >> 16;
  2659. int hsub = fb->format->hsub;
  2660. int vsub = fb->format->vsub;
  2661. int x = src_x / hsub;
  2662. int y = src_y / vsub;
  2663. u32 offset;
  2664. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2665. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2666. plane_state->base.rotation);
  2667. return -EINVAL;
  2668. }
  2669. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2670. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2671. plane_state->aux.offset = offset;
  2672. plane_state->aux.x = x * hsub + src_x % hsub;
  2673. plane_state->aux.y = y * vsub + src_y % vsub;
  2674. return 0;
  2675. }
  2676. int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
  2677. struct intel_plane_state *plane_state)
  2678. {
  2679. const struct drm_framebuffer *fb = plane_state->base.fb;
  2680. unsigned int rotation = plane_state->base.rotation;
  2681. int ret;
  2682. if (rotation & DRM_MODE_REFLECT_X &&
  2683. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2684. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2685. return -EINVAL;
  2686. }
  2687. if (!plane_state->base.visible)
  2688. return 0;
  2689. /* Rotate src coordinates to match rotated GTT view */
  2690. if (drm_rotation_90_or_270(rotation))
  2691. drm_rect_rotate(&plane_state->base.src,
  2692. fb->width << 16, fb->height << 16,
  2693. DRM_MODE_ROTATE_270);
  2694. /*
  2695. * Handle the AUX surface first since
  2696. * the main surface setup depends on it.
  2697. */
  2698. if (fb->format->format == DRM_FORMAT_NV12) {
  2699. ret = skl_check_nv12_aux_surface(plane_state);
  2700. if (ret)
  2701. return ret;
  2702. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2703. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2704. ret = skl_check_ccs_aux_surface(plane_state);
  2705. if (ret)
  2706. return ret;
  2707. } else {
  2708. plane_state->aux.offset = ~0xfff;
  2709. plane_state->aux.x = 0;
  2710. plane_state->aux.y = 0;
  2711. }
  2712. ret = skl_check_main_surface(crtc_state, plane_state);
  2713. if (ret)
  2714. return ret;
  2715. return 0;
  2716. }
  2717. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2718. const struct intel_plane_state *plane_state)
  2719. {
  2720. struct drm_i915_private *dev_priv =
  2721. to_i915(plane_state->base.plane->dev);
  2722. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2723. const struct drm_framebuffer *fb = plane_state->base.fb;
  2724. unsigned int rotation = plane_state->base.rotation;
  2725. u32 dspcntr;
  2726. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2727. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2728. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2729. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2730. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2731. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2732. if (INTEL_GEN(dev_priv) < 5)
  2733. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2734. switch (fb->format->format) {
  2735. case DRM_FORMAT_C8:
  2736. dspcntr |= DISPPLANE_8BPP;
  2737. break;
  2738. case DRM_FORMAT_XRGB1555:
  2739. dspcntr |= DISPPLANE_BGRX555;
  2740. break;
  2741. case DRM_FORMAT_RGB565:
  2742. dspcntr |= DISPPLANE_BGRX565;
  2743. break;
  2744. case DRM_FORMAT_XRGB8888:
  2745. dspcntr |= DISPPLANE_BGRX888;
  2746. break;
  2747. case DRM_FORMAT_XBGR8888:
  2748. dspcntr |= DISPPLANE_RGBX888;
  2749. break;
  2750. case DRM_FORMAT_XRGB2101010:
  2751. dspcntr |= DISPPLANE_BGRX101010;
  2752. break;
  2753. case DRM_FORMAT_XBGR2101010:
  2754. dspcntr |= DISPPLANE_RGBX101010;
  2755. break;
  2756. default:
  2757. MISSING_CASE(fb->format->format);
  2758. return 0;
  2759. }
  2760. if (INTEL_GEN(dev_priv) >= 4 &&
  2761. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2762. dspcntr |= DISPPLANE_TILED;
  2763. if (rotation & DRM_MODE_ROTATE_180)
  2764. dspcntr |= DISPPLANE_ROTATE_180;
  2765. if (rotation & DRM_MODE_REFLECT_X)
  2766. dspcntr |= DISPPLANE_MIRROR;
  2767. return dspcntr;
  2768. }
  2769. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2770. {
  2771. struct drm_i915_private *dev_priv =
  2772. to_i915(plane_state->base.plane->dev);
  2773. int src_x = plane_state->base.src.x1 >> 16;
  2774. int src_y = plane_state->base.src.y1 >> 16;
  2775. u32 offset;
  2776. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2777. if (INTEL_GEN(dev_priv) >= 4)
  2778. offset = intel_compute_tile_offset(&src_x, &src_y,
  2779. plane_state, 0);
  2780. else
  2781. offset = 0;
  2782. /* HSW/BDW do this automagically in hardware */
  2783. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2784. unsigned int rotation = plane_state->base.rotation;
  2785. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2786. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2787. if (rotation & DRM_MODE_ROTATE_180) {
  2788. src_x += src_w - 1;
  2789. src_y += src_h - 1;
  2790. } else if (rotation & DRM_MODE_REFLECT_X) {
  2791. src_x += src_w - 1;
  2792. }
  2793. }
  2794. plane_state->main.offset = offset;
  2795. plane_state->main.x = src_x;
  2796. plane_state->main.y = src_y;
  2797. return 0;
  2798. }
  2799. static void i9xx_update_plane(struct intel_plane *plane,
  2800. const struct intel_crtc_state *crtc_state,
  2801. const struct intel_plane_state *plane_state)
  2802. {
  2803. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2804. const struct drm_framebuffer *fb = plane_state->base.fb;
  2805. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2806. u32 linear_offset;
  2807. u32 dspcntr = plane_state->ctl;
  2808. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2809. int x = plane_state->main.x;
  2810. int y = plane_state->main.y;
  2811. unsigned long irqflags;
  2812. u32 dspaddr_offset;
  2813. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2814. if (INTEL_GEN(dev_priv) >= 4)
  2815. dspaddr_offset = plane_state->main.offset;
  2816. else
  2817. dspaddr_offset = linear_offset;
  2818. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2819. if (INTEL_GEN(dev_priv) < 4) {
  2820. /* pipesrc and dspsize control the size that is scaled from,
  2821. * which should always be the user's requested size.
  2822. */
  2823. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2824. ((crtc_state->pipe_src_h - 1) << 16) |
  2825. (crtc_state->pipe_src_w - 1));
  2826. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2827. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2828. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2829. ((crtc_state->pipe_src_h - 1) << 16) |
  2830. (crtc_state->pipe_src_w - 1));
  2831. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2832. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2833. }
  2834. I915_WRITE_FW(reg, dspcntr);
  2835. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2836. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2837. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2838. intel_plane_ggtt_offset(plane_state) +
  2839. dspaddr_offset);
  2840. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2841. } else if (INTEL_GEN(dev_priv) >= 4) {
  2842. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2843. intel_plane_ggtt_offset(plane_state) +
  2844. dspaddr_offset);
  2845. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2846. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2847. } else {
  2848. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2849. intel_plane_ggtt_offset(plane_state) +
  2850. dspaddr_offset);
  2851. }
  2852. POSTING_READ_FW(reg);
  2853. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2854. }
  2855. static void i9xx_disable_plane(struct intel_plane *plane,
  2856. struct intel_crtc *crtc)
  2857. {
  2858. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2859. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2860. unsigned long irqflags;
  2861. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2862. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2863. if (INTEL_GEN(dev_priv) >= 4)
  2864. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2865. else
  2866. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2867. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2868. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2869. }
  2870. static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
  2871. {
  2872. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2873. enum intel_display_power_domain power_domain;
  2874. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2875. enum pipe pipe = plane->pipe;
  2876. bool ret;
  2877. /*
  2878. * Not 100% correct for planes that can move between pipes,
  2879. * but that's only the case for gen2-4 which don't have any
  2880. * display power wells.
  2881. */
  2882. power_domain = POWER_DOMAIN_PIPE(pipe);
  2883. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2884. return false;
  2885. ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
  2886. intel_display_power_put(dev_priv, power_domain);
  2887. return ret;
  2888. }
  2889. static u32
  2890. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2891. {
  2892. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2893. return 64;
  2894. else
  2895. return intel_tile_width_bytes(fb, plane);
  2896. }
  2897. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2898. {
  2899. struct drm_device *dev = intel_crtc->base.dev;
  2900. struct drm_i915_private *dev_priv = to_i915(dev);
  2901. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2902. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2903. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2904. }
  2905. /*
  2906. * This function detaches (aka. unbinds) unused scalers in hardware
  2907. */
  2908. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2909. {
  2910. struct intel_crtc_scaler_state *scaler_state;
  2911. int i;
  2912. scaler_state = &intel_crtc->config->scaler_state;
  2913. /* loop through and disable scalers that aren't in use */
  2914. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2915. if (!scaler_state->scalers[i].in_use)
  2916. skl_detach_scaler(intel_crtc, i);
  2917. }
  2918. }
  2919. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2920. unsigned int rotation)
  2921. {
  2922. u32 stride;
  2923. if (plane >= fb->format->num_planes)
  2924. return 0;
  2925. stride = intel_fb_pitch(fb, plane, rotation);
  2926. /*
  2927. * The stride is either expressed as a multiple of 64 bytes chunks for
  2928. * linear buffers or in number of tiles for tiled buffers.
  2929. */
  2930. if (drm_rotation_90_or_270(rotation))
  2931. stride /= intel_tile_height(fb, plane);
  2932. else
  2933. stride /= intel_fb_stride_alignment(fb, plane);
  2934. return stride;
  2935. }
  2936. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2937. {
  2938. switch (pixel_format) {
  2939. case DRM_FORMAT_C8:
  2940. return PLANE_CTL_FORMAT_INDEXED;
  2941. case DRM_FORMAT_RGB565:
  2942. return PLANE_CTL_FORMAT_RGB_565;
  2943. case DRM_FORMAT_XBGR8888:
  2944. case DRM_FORMAT_ABGR8888:
  2945. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2946. case DRM_FORMAT_XRGB8888:
  2947. case DRM_FORMAT_ARGB8888:
  2948. return PLANE_CTL_FORMAT_XRGB_8888;
  2949. case DRM_FORMAT_XRGB2101010:
  2950. return PLANE_CTL_FORMAT_XRGB_2101010;
  2951. case DRM_FORMAT_XBGR2101010:
  2952. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2953. case DRM_FORMAT_YUYV:
  2954. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2955. case DRM_FORMAT_YVYU:
  2956. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2957. case DRM_FORMAT_UYVY:
  2958. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2959. case DRM_FORMAT_VYUY:
  2960. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2961. default:
  2962. MISSING_CASE(pixel_format);
  2963. }
  2964. return 0;
  2965. }
  2966. /*
  2967. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2968. * to be already pre-multiplied. We need to add a knob (or a different
  2969. * DRM_FORMAT) for user-space to configure that.
  2970. */
  2971. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  2972. {
  2973. switch (pixel_format) {
  2974. case DRM_FORMAT_ABGR8888:
  2975. case DRM_FORMAT_ARGB8888:
  2976. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2977. default:
  2978. return PLANE_CTL_ALPHA_DISABLE;
  2979. }
  2980. }
  2981. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  2982. {
  2983. switch (pixel_format) {
  2984. case DRM_FORMAT_ABGR8888:
  2985. case DRM_FORMAT_ARGB8888:
  2986. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  2987. default:
  2988. return PLANE_COLOR_ALPHA_DISABLE;
  2989. }
  2990. }
  2991. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2992. {
  2993. switch (fb_modifier) {
  2994. case DRM_FORMAT_MOD_LINEAR:
  2995. break;
  2996. case I915_FORMAT_MOD_X_TILED:
  2997. return PLANE_CTL_TILED_X;
  2998. case I915_FORMAT_MOD_Y_TILED:
  2999. return PLANE_CTL_TILED_Y;
  3000. case I915_FORMAT_MOD_Y_TILED_CCS:
  3001. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  3002. case I915_FORMAT_MOD_Yf_TILED:
  3003. return PLANE_CTL_TILED_YF;
  3004. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3005. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  3006. default:
  3007. MISSING_CASE(fb_modifier);
  3008. }
  3009. return 0;
  3010. }
  3011. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  3012. {
  3013. switch (rotate) {
  3014. case DRM_MODE_ROTATE_0:
  3015. break;
  3016. /*
  3017. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3018. * while i915 HW rotation is clockwise, thats why this swapping.
  3019. */
  3020. case DRM_MODE_ROTATE_90:
  3021. return PLANE_CTL_ROTATE_270;
  3022. case DRM_MODE_ROTATE_180:
  3023. return PLANE_CTL_ROTATE_180;
  3024. case DRM_MODE_ROTATE_270:
  3025. return PLANE_CTL_ROTATE_90;
  3026. default:
  3027. MISSING_CASE(rotate);
  3028. }
  3029. return 0;
  3030. }
  3031. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3032. {
  3033. switch (reflect) {
  3034. case 0:
  3035. break;
  3036. case DRM_MODE_REFLECT_X:
  3037. return PLANE_CTL_FLIP_HORIZONTAL;
  3038. case DRM_MODE_REFLECT_Y:
  3039. default:
  3040. MISSING_CASE(reflect);
  3041. }
  3042. return 0;
  3043. }
  3044. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3045. const struct intel_plane_state *plane_state)
  3046. {
  3047. struct drm_i915_private *dev_priv =
  3048. to_i915(plane_state->base.plane->dev);
  3049. const struct drm_framebuffer *fb = plane_state->base.fb;
  3050. unsigned int rotation = plane_state->base.rotation;
  3051. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3052. u32 plane_ctl;
  3053. plane_ctl = PLANE_CTL_ENABLE;
  3054. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3055. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3056. plane_ctl |=
  3057. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3058. PLANE_CTL_PIPE_CSC_ENABLE |
  3059. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3060. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3061. plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
  3062. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3063. plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
  3064. }
  3065. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3066. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3067. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3068. if (INTEL_GEN(dev_priv) >= 10)
  3069. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3070. DRM_MODE_REFLECT_MASK);
  3071. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3072. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3073. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3074. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3075. return plane_ctl;
  3076. }
  3077. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3078. const struct intel_plane_state *plane_state)
  3079. {
  3080. const struct drm_framebuffer *fb = plane_state->base.fb;
  3081. u32 plane_color_ctl = 0;
  3082. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3083. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3084. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3085. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3086. if (intel_format_is_yuv(fb->format->format)) {
  3087. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3088. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3089. else
  3090. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
  3091. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3092. plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
  3093. }
  3094. return plane_color_ctl;
  3095. }
  3096. static int
  3097. __intel_display_resume(struct drm_device *dev,
  3098. struct drm_atomic_state *state,
  3099. struct drm_modeset_acquire_ctx *ctx)
  3100. {
  3101. struct drm_crtc_state *crtc_state;
  3102. struct drm_crtc *crtc;
  3103. int i, ret;
  3104. intel_modeset_setup_hw_state(dev, ctx);
  3105. i915_redisable_vga(to_i915(dev));
  3106. if (!state)
  3107. return 0;
  3108. /*
  3109. * We've duplicated the state, pointers to the old state are invalid.
  3110. *
  3111. * Don't attempt to use the old state until we commit the duplicated state.
  3112. */
  3113. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3114. /*
  3115. * Force recalculation even if we restore
  3116. * current state. With fast modeset this may not result
  3117. * in a modeset when the state is compatible.
  3118. */
  3119. crtc_state->mode_changed = true;
  3120. }
  3121. /* ignore any reset values/BIOS leftovers in the WM registers */
  3122. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3123. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3124. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3125. WARN_ON(ret == -EDEADLK);
  3126. return ret;
  3127. }
  3128. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3129. {
  3130. return intel_has_gpu_reset(dev_priv) &&
  3131. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3132. }
  3133. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3134. {
  3135. struct drm_device *dev = &dev_priv->drm;
  3136. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3137. struct drm_atomic_state *state;
  3138. int ret;
  3139. /* reset doesn't touch the display */
  3140. if (!i915_modparams.force_reset_modeset_test &&
  3141. !gpu_reset_clobbers_display(dev_priv))
  3142. return;
  3143. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3144. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3145. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3146. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3147. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3148. i915_gem_set_wedged(dev_priv);
  3149. }
  3150. /*
  3151. * Need mode_config.mutex so that we don't
  3152. * trample ongoing ->detect() and whatnot.
  3153. */
  3154. mutex_lock(&dev->mode_config.mutex);
  3155. drm_modeset_acquire_init(ctx, 0);
  3156. while (1) {
  3157. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3158. if (ret != -EDEADLK)
  3159. break;
  3160. drm_modeset_backoff(ctx);
  3161. }
  3162. /*
  3163. * Disabling the crtcs gracefully seems nicer. Also the
  3164. * g33 docs say we should at least disable all the planes.
  3165. */
  3166. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3167. if (IS_ERR(state)) {
  3168. ret = PTR_ERR(state);
  3169. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3170. return;
  3171. }
  3172. ret = drm_atomic_helper_disable_all(dev, ctx);
  3173. if (ret) {
  3174. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3175. drm_atomic_state_put(state);
  3176. return;
  3177. }
  3178. dev_priv->modeset_restore_state = state;
  3179. state->acquire_ctx = ctx;
  3180. }
  3181. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3182. {
  3183. struct drm_device *dev = &dev_priv->drm;
  3184. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3185. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3186. int ret;
  3187. /* reset doesn't touch the display */
  3188. if (!i915_modparams.force_reset_modeset_test &&
  3189. !gpu_reset_clobbers_display(dev_priv))
  3190. return;
  3191. if (!state)
  3192. goto unlock;
  3193. dev_priv->modeset_restore_state = NULL;
  3194. /* reset doesn't touch the display */
  3195. if (!gpu_reset_clobbers_display(dev_priv)) {
  3196. /* for testing only restore the display */
  3197. ret = __intel_display_resume(dev, state, ctx);
  3198. if (ret)
  3199. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3200. } else {
  3201. /*
  3202. * The display has been reset as well,
  3203. * so need a full re-initialization.
  3204. */
  3205. intel_runtime_pm_disable_interrupts(dev_priv);
  3206. intel_runtime_pm_enable_interrupts(dev_priv);
  3207. intel_pps_unlock_regs_wa(dev_priv);
  3208. intel_modeset_init_hw(dev);
  3209. intel_init_clock_gating(dev_priv);
  3210. spin_lock_irq(&dev_priv->irq_lock);
  3211. if (dev_priv->display.hpd_irq_setup)
  3212. dev_priv->display.hpd_irq_setup(dev_priv);
  3213. spin_unlock_irq(&dev_priv->irq_lock);
  3214. ret = __intel_display_resume(dev, state, ctx);
  3215. if (ret)
  3216. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3217. intel_hpd_init(dev_priv);
  3218. }
  3219. drm_atomic_state_put(state);
  3220. unlock:
  3221. drm_modeset_drop_locks(ctx);
  3222. drm_modeset_acquire_fini(ctx);
  3223. mutex_unlock(&dev->mode_config.mutex);
  3224. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3225. }
  3226. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3227. const struct intel_crtc_state *new_crtc_state)
  3228. {
  3229. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3230. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3231. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3232. crtc->base.mode = new_crtc_state->base.mode;
  3233. /*
  3234. * Update pipe size and adjust fitter if needed: the reason for this is
  3235. * that in compute_mode_changes we check the native mode (not the pfit
  3236. * mode) to see if we can flip rather than do a full mode set. In the
  3237. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3238. * pfit state, we'll end up with a big fb scanned out into the wrong
  3239. * sized surface.
  3240. */
  3241. I915_WRITE(PIPESRC(crtc->pipe),
  3242. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3243. (new_crtc_state->pipe_src_h - 1));
  3244. /* on skylake this is done by detaching scalers */
  3245. if (INTEL_GEN(dev_priv) >= 9) {
  3246. skl_detach_scalers(crtc);
  3247. if (new_crtc_state->pch_pfit.enabled)
  3248. skylake_pfit_enable(crtc);
  3249. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3250. if (new_crtc_state->pch_pfit.enabled)
  3251. ironlake_pfit_enable(crtc);
  3252. else if (old_crtc_state->pch_pfit.enabled)
  3253. ironlake_pfit_disable(crtc, true);
  3254. }
  3255. }
  3256. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3257. {
  3258. struct drm_device *dev = crtc->base.dev;
  3259. struct drm_i915_private *dev_priv = to_i915(dev);
  3260. int pipe = crtc->pipe;
  3261. i915_reg_t reg;
  3262. u32 temp;
  3263. /* enable normal train */
  3264. reg = FDI_TX_CTL(pipe);
  3265. temp = I915_READ(reg);
  3266. if (IS_IVYBRIDGE(dev_priv)) {
  3267. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3268. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3269. } else {
  3270. temp &= ~FDI_LINK_TRAIN_NONE;
  3271. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3272. }
  3273. I915_WRITE(reg, temp);
  3274. reg = FDI_RX_CTL(pipe);
  3275. temp = I915_READ(reg);
  3276. if (HAS_PCH_CPT(dev_priv)) {
  3277. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3278. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3279. } else {
  3280. temp &= ~FDI_LINK_TRAIN_NONE;
  3281. temp |= FDI_LINK_TRAIN_NONE;
  3282. }
  3283. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3284. /* wait one idle pattern time */
  3285. POSTING_READ(reg);
  3286. udelay(1000);
  3287. /* IVB wants error correction enabled */
  3288. if (IS_IVYBRIDGE(dev_priv))
  3289. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3290. FDI_FE_ERRC_ENABLE);
  3291. }
  3292. /* The FDI link training functions for ILK/Ibexpeak. */
  3293. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3294. const struct intel_crtc_state *crtc_state)
  3295. {
  3296. struct drm_device *dev = crtc->base.dev;
  3297. struct drm_i915_private *dev_priv = to_i915(dev);
  3298. int pipe = crtc->pipe;
  3299. i915_reg_t reg;
  3300. u32 temp, tries;
  3301. /* FDI needs bits from pipe first */
  3302. assert_pipe_enabled(dev_priv, pipe);
  3303. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3304. for train result */
  3305. reg = FDI_RX_IMR(pipe);
  3306. temp = I915_READ(reg);
  3307. temp &= ~FDI_RX_SYMBOL_LOCK;
  3308. temp &= ~FDI_RX_BIT_LOCK;
  3309. I915_WRITE(reg, temp);
  3310. I915_READ(reg);
  3311. udelay(150);
  3312. /* enable CPU FDI TX and PCH FDI RX */
  3313. reg = FDI_TX_CTL(pipe);
  3314. temp = I915_READ(reg);
  3315. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3316. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3317. temp &= ~FDI_LINK_TRAIN_NONE;
  3318. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3319. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3320. reg = FDI_RX_CTL(pipe);
  3321. temp = I915_READ(reg);
  3322. temp &= ~FDI_LINK_TRAIN_NONE;
  3323. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3324. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3325. POSTING_READ(reg);
  3326. udelay(150);
  3327. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3328. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3329. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3330. FDI_RX_PHASE_SYNC_POINTER_EN);
  3331. reg = FDI_RX_IIR(pipe);
  3332. for (tries = 0; tries < 5; tries++) {
  3333. temp = I915_READ(reg);
  3334. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3335. if ((temp & FDI_RX_BIT_LOCK)) {
  3336. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3337. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3338. break;
  3339. }
  3340. }
  3341. if (tries == 5)
  3342. DRM_ERROR("FDI train 1 fail!\n");
  3343. /* Train 2 */
  3344. reg = FDI_TX_CTL(pipe);
  3345. temp = I915_READ(reg);
  3346. temp &= ~FDI_LINK_TRAIN_NONE;
  3347. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3348. I915_WRITE(reg, temp);
  3349. reg = FDI_RX_CTL(pipe);
  3350. temp = I915_READ(reg);
  3351. temp &= ~FDI_LINK_TRAIN_NONE;
  3352. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3353. I915_WRITE(reg, temp);
  3354. POSTING_READ(reg);
  3355. udelay(150);
  3356. reg = FDI_RX_IIR(pipe);
  3357. for (tries = 0; tries < 5; tries++) {
  3358. temp = I915_READ(reg);
  3359. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3360. if (temp & FDI_RX_SYMBOL_LOCK) {
  3361. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3362. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3363. break;
  3364. }
  3365. }
  3366. if (tries == 5)
  3367. DRM_ERROR("FDI train 2 fail!\n");
  3368. DRM_DEBUG_KMS("FDI train done\n");
  3369. }
  3370. static const int snb_b_fdi_train_param[] = {
  3371. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3372. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3373. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3374. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3375. };
  3376. /* The FDI link training functions for SNB/Cougarpoint. */
  3377. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3378. const struct intel_crtc_state *crtc_state)
  3379. {
  3380. struct drm_device *dev = crtc->base.dev;
  3381. struct drm_i915_private *dev_priv = to_i915(dev);
  3382. int pipe = crtc->pipe;
  3383. i915_reg_t reg;
  3384. u32 temp, i, retry;
  3385. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3386. for train result */
  3387. reg = FDI_RX_IMR(pipe);
  3388. temp = I915_READ(reg);
  3389. temp &= ~FDI_RX_SYMBOL_LOCK;
  3390. temp &= ~FDI_RX_BIT_LOCK;
  3391. I915_WRITE(reg, temp);
  3392. POSTING_READ(reg);
  3393. udelay(150);
  3394. /* enable CPU FDI TX and PCH FDI RX */
  3395. reg = FDI_TX_CTL(pipe);
  3396. temp = I915_READ(reg);
  3397. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3398. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3399. temp &= ~FDI_LINK_TRAIN_NONE;
  3400. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3401. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3402. /* SNB-B */
  3403. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3404. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3405. I915_WRITE(FDI_RX_MISC(pipe),
  3406. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3407. reg = FDI_RX_CTL(pipe);
  3408. temp = I915_READ(reg);
  3409. if (HAS_PCH_CPT(dev_priv)) {
  3410. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3411. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3412. } else {
  3413. temp &= ~FDI_LINK_TRAIN_NONE;
  3414. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3415. }
  3416. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3417. POSTING_READ(reg);
  3418. udelay(150);
  3419. for (i = 0; i < 4; i++) {
  3420. reg = FDI_TX_CTL(pipe);
  3421. temp = I915_READ(reg);
  3422. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3423. temp |= snb_b_fdi_train_param[i];
  3424. I915_WRITE(reg, temp);
  3425. POSTING_READ(reg);
  3426. udelay(500);
  3427. for (retry = 0; retry < 5; retry++) {
  3428. reg = FDI_RX_IIR(pipe);
  3429. temp = I915_READ(reg);
  3430. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3431. if (temp & FDI_RX_BIT_LOCK) {
  3432. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3433. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3434. break;
  3435. }
  3436. udelay(50);
  3437. }
  3438. if (retry < 5)
  3439. break;
  3440. }
  3441. if (i == 4)
  3442. DRM_ERROR("FDI train 1 fail!\n");
  3443. /* Train 2 */
  3444. reg = FDI_TX_CTL(pipe);
  3445. temp = I915_READ(reg);
  3446. temp &= ~FDI_LINK_TRAIN_NONE;
  3447. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3448. if (IS_GEN6(dev_priv)) {
  3449. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3450. /* SNB-B */
  3451. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3452. }
  3453. I915_WRITE(reg, temp);
  3454. reg = FDI_RX_CTL(pipe);
  3455. temp = I915_READ(reg);
  3456. if (HAS_PCH_CPT(dev_priv)) {
  3457. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3458. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3459. } else {
  3460. temp &= ~FDI_LINK_TRAIN_NONE;
  3461. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3462. }
  3463. I915_WRITE(reg, temp);
  3464. POSTING_READ(reg);
  3465. udelay(150);
  3466. for (i = 0; i < 4; i++) {
  3467. reg = FDI_TX_CTL(pipe);
  3468. temp = I915_READ(reg);
  3469. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3470. temp |= snb_b_fdi_train_param[i];
  3471. I915_WRITE(reg, temp);
  3472. POSTING_READ(reg);
  3473. udelay(500);
  3474. for (retry = 0; retry < 5; retry++) {
  3475. reg = FDI_RX_IIR(pipe);
  3476. temp = I915_READ(reg);
  3477. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3478. if (temp & FDI_RX_SYMBOL_LOCK) {
  3479. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3480. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3481. break;
  3482. }
  3483. udelay(50);
  3484. }
  3485. if (retry < 5)
  3486. break;
  3487. }
  3488. if (i == 4)
  3489. DRM_ERROR("FDI train 2 fail!\n");
  3490. DRM_DEBUG_KMS("FDI train done.\n");
  3491. }
  3492. /* Manual link training for Ivy Bridge A0 parts */
  3493. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3494. const struct intel_crtc_state *crtc_state)
  3495. {
  3496. struct drm_device *dev = crtc->base.dev;
  3497. struct drm_i915_private *dev_priv = to_i915(dev);
  3498. int pipe = crtc->pipe;
  3499. i915_reg_t reg;
  3500. u32 temp, i, j;
  3501. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3502. for train result */
  3503. reg = FDI_RX_IMR(pipe);
  3504. temp = I915_READ(reg);
  3505. temp &= ~FDI_RX_SYMBOL_LOCK;
  3506. temp &= ~FDI_RX_BIT_LOCK;
  3507. I915_WRITE(reg, temp);
  3508. POSTING_READ(reg);
  3509. udelay(150);
  3510. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3511. I915_READ(FDI_RX_IIR(pipe)));
  3512. /* Try each vswing and preemphasis setting twice before moving on */
  3513. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3514. /* disable first in case we need to retry */
  3515. reg = FDI_TX_CTL(pipe);
  3516. temp = I915_READ(reg);
  3517. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3518. temp &= ~FDI_TX_ENABLE;
  3519. I915_WRITE(reg, temp);
  3520. reg = FDI_RX_CTL(pipe);
  3521. temp = I915_READ(reg);
  3522. temp &= ~FDI_LINK_TRAIN_AUTO;
  3523. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3524. temp &= ~FDI_RX_ENABLE;
  3525. I915_WRITE(reg, temp);
  3526. /* enable CPU FDI TX and PCH FDI RX */
  3527. reg = FDI_TX_CTL(pipe);
  3528. temp = I915_READ(reg);
  3529. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3530. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3531. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3532. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3533. temp |= snb_b_fdi_train_param[j/2];
  3534. temp |= FDI_COMPOSITE_SYNC;
  3535. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3536. I915_WRITE(FDI_RX_MISC(pipe),
  3537. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3538. reg = FDI_RX_CTL(pipe);
  3539. temp = I915_READ(reg);
  3540. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3541. temp |= FDI_COMPOSITE_SYNC;
  3542. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3543. POSTING_READ(reg);
  3544. udelay(1); /* should be 0.5us */
  3545. for (i = 0; i < 4; i++) {
  3546. reg = FDI_RX_IIR(pipe);
  3547. temp = I915_READ(reg);
  3548. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3549. if (temp & FDI_RX_BIT_LOCK ||
  3550. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3551. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3552. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3553. i);
  3554. break;
  3555. }
  3556. udelay(1); /* should be 0.5us */
  3557. }
  3558. if (i == 4) {
  3559. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3560. continue;
  3561. }
  3562. /* Train 2 */
  3563. reg = FDI_TX_CTL(pipe);
  3564. temp = I915_READ(reg);
  3565. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3566. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3567. I915_WRITE(reg, temp);
  3568. reg = FDI_RX_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3571. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3572. I915_WRITE(reg, temp);
  3573. POSTING_READ(reg);
  3574. udelay(2); /* should be 1.5us */
  3575. for (i = 0; i < 4; i++) {
  3576. reg = FDI_RX_IIR(pipe);
  3577. temp = I915_READ(reg);
  3578. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3579. if (temp & FDI_RX_SYMBOL_LOCK ||
  3580. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3581. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3582. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3583. i);
  3584. goto train_done;
  3585. }
  3586. udelay(2); /* should be 1.5us */
  3587. }
  3588. if (i == 4)
  3589. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3590. }
  3591. train_done:
  3592. DRM_DEBUG_KMS("FDI train done.\n");
  3593. }
  3594. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3595. {
  3596. struct drm_device *dev = intel_crtc->base.dev;
  3597. struct drm_i915_private *dev_priv = to_i915(dev);
  3598. int pipe = intel_crtc->pipe;
  3599. i915_reg_t reg;
  3600. u32 temp;
  3601. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3602. reg = FDI_RX_CTL(pipe);
  3603. temp = I915_READ(reg);
  3604. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3605. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3606. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3607. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3608. POSTING_READ(reg);
  3609. udelay(200);
  3610. /* Switch from Rawclk to PCDclk */
  3611. temp = I915_READ(reg);
  3612. I915_WRITE(reg, temp | FDI_PCDCLK);
  3613. POSTING_READ(reg);
  3614. udelay(200);
  3615. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3616. reg = FDI_TX_CTL(pipe);
  3617. temp = I915_READ(reg);
  3618. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3619. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3620. POSTING_READ(reg);
  3621. udelay(100);
  3622. }
  3623. }
  3624. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3625. {
  3626. struct drm_device *dev = intel_crtc->base.dev;
  3627. struct drm_i915_private *dev_priv = to_i915(dev);
  3628. int pipe = intel_crtc->pipe;
  3629. i915_reg_t reg;
  3630. u32 temp;
  3631. /* Switch from PCDclk to Rawclk */
  3632. reg = FDI_RX_CTL(pipe);
  3633. temp = I915_READ(reg);
  3634. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3635. /* Disable CPU FDI TX PLL */
  3636. reg = FDI_TX_CTL(pipe);
  3637. temp = I915_READ(reg);
  3638. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3639. POSTING_READ(reg);
  3640. udelay(100);
  3641. reg = FDI_RX_CTL(pipe);
  3642. temp = I915_READ(reg);
  3643. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3644. /* Wait for the clocks to turn off. */
  3645. POSTING_READ(reg);
  3646. udelay(100);
  3647. }
  3648. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3649. {
  3650. struct drm_device *dev = crtc->dev;
  3651. struct drm_i915_private *dev_priv = to_i915(dev);
  3652. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3653. int pipe = intel_crtc->pipe;
  3654. i915_reg_t reg;
  3655. u32 temp;
  3656. /* disable CPU FDI tx and PCH FDI rx */
  3657. reg = FDI_TX_CTL(pipe);
  3658. temp = I915_READ(reg);
  3659. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3660. POSTING_READ(reg);
  3661. reg = FDI_RX_CTL(pipe);
  3662. temp = I915_READ(reg);
  3663. temp &= ~(0x7 << 16);
  3664. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3665. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3666. POSTING_READ(reg);
  3667. udelay(100);
  3668. /* Ironlake workaround, disable clock pointer after downing FDI */
  3669. if (HAS_PCH_IBX(dev_priv))
  3670. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3671. /* still set train pattern 1 */
  3672. reg = FDI_TX_CTL(pipe);
  3673. temp = I915_READ(reg);
  3674. temp &= ~FDI_LINK_TRAIN_NONE;
  3675. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3676. I915_WRITE(reg, temp);
  3677. reg = FDI_RX_CTL(pipe);
  3678. temp = I915_READ(reg);
  3679. if (HAS_PCH_CPT(dev_priv)) {
  3680. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3681. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3682. } else {
  3683. temp &= ~FDI_LINK_TRAIN_NONE;
  3684. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3685. }
  3686. /* BPC in FDI rx is consistent with that in PIPECONF */
  3687. temp &= ~(0x07 << 16);
  3688. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3689. I915_WRITE(reg, temp);
  3690. POSTING_READ(reg);
  3691. udelay(100);
  3692. }
  3693. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3694. {
  3695. struct drm_crtc *crtc;
  3696. bool cleanup_done;
  3697. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3698. struct drm_crtc_commit *commit;
  3699. spin_lock(&crtc->commit_lock);
  3700. commit = list_first_entry_or_null(&crtc->commit_list,
  3701. struct drm_crtc_commit, commit_entry);
  3702. cleanup_done = commit ?
  3703. try_wait_for_completion(&commit->cleanup_done) : true;
  3704. spin_unlock(&crtc->commit_lock);
  3705. if (cleanup_done)
  3706. continue;
  3707. drm_crtc_wait_one_vblank(crtc);
  3708. return true;
  3709. }
  3710. return false;
  3711. }
  3712. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3713. {
  3714. u32 temp;
  3715. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3716. mutex_lock(&dev_priv->sb_lock);
  3717. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3718. temp |= SBI_SSCCTL_DISABLE;
  3719. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3720. mutex_unlock(&dev_priv->sb_lock);
  3721. }
  3722. /* Program iCLKIP clock to the desired frequency */
  3723. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3724. {
  3725. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3726. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3727. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3728. u32 temp;
  3729. lpt_disable_iclkip(dev_priv);
  3730. /* The iCLK virtual clock root frequency is in MHz,
  3731. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3732. * divisors, it is necessary to divide one by another, so we
  3733. * convert the virtual clock precision to KHz here for higher
  3734. * precision.
  3735. */
  3736. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3737. u32 iclk_virtual_root_freq = 172800 * 1000;
  3738. u32 iclk_pi_range = 64;
  3739. u32 desired_divisor;
  3740. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3741. clock << auxdiv);
  3742. divsel = (desired_divisor / iclk_pi_range) - 2;
  3743. phaseinc = desired_divisor % iclk_pi_range;
  3744. /*
  3745. * Near 20MHz is a corner case which is
  3746. * out of range for the 7-bit divisor
  3747. */
  3748. if (divsel <= 0x7f)
  3749. break;
  3750. }
  3751. /* This should not happen with any sane values */
  3752. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3753. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3754. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3755. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3756. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3757. clock,
  3758. auxdiv,
  3759. divsel,
  3760. phasedir,
  3761. phaseinc);
  3762. mutex_lock(&dev_priv->sb_lock);
  3763. /* Program SSCDIVINTPHASE6 */
  3764. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3765. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3766. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3767. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3768. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3769. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3770. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3771. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3772. /* Program SSCAUXDIV */
  3773. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3774. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3775. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3776. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3777. /* Enable modulator and associated divider */
  3778. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3779. temp &= ~SBI_SSCCTL_DISABLE;
  3780. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3781. mutex_unlock(&dev_priv->sb_lock);
  3782. /* Wait for initialization time */
  3783. udelay(24);
  3784. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3785. }
  3786. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3787. {
  3788. u32 divsel, phaseinc, auxdiv;
  3789. u32 iclk_virtual_root_freq = 172800 * 1000;
  3790. u32 iclk_pi_range = 64;
  3791. u32 desired_divisor;
  3792. u32 temp;
  3793. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3794. return 0;
  3795. mutex_lock(&dev_priv->sb_lock);
  3796. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3797. if (temp & SBI_SSCCTL_DISABLE) {
  3798. mutex_unlock(&dev_priv->sb_lock);
  3799. return 0;
  3800. }
  3801. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3802. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3803. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3804. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3805. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3806. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3807. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3808. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3809. mutex_unlock(&dev_priv->sb_lock);
  3810. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3811. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3812. desired_divisor << auxdiv);
  3813. }
  3814. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3815. enum pipe pch_transcoder)
  3816. {
  3817. struct drm_device *dev = crtc->base.dev;
  3818. struct drm_i915_private *dev_priv = to_i915(dev);
  3819. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3820. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3821. I915_READ(HTOTAL(cpu_transcoder)));
  3822. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3823. I915_READ(HBLANK(cpu_transcoder)));
  3824. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3825. I915_READ(HSYNC(cpu_transcoder)));
  3826. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3827. I915_READ(VTOTAL(cpu_transcoder)));
  3828. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3829. I915_READ(VBLANK(cpu_transcoder)));
  3830. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3831. I915_READ(VSYNC(cpu_transcoder)));
  3832. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3833. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3834. }
  3835. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3836. {
  3837. struct drm_i915_private *dev_priv = to_i915(dev);
  3838. uint32_t temp;
  3839. temp = I915_READ(SOUTH_CHICKEN1);
  3840. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3841. return;
  3842. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3843. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3844. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3845. if (enable)
  3846. temp |= FDI_BC_BIFURCATION_SELECT;
  3847. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3848. I915_WRITE(SOUTH_CHICKEN1, temp);
  3849. POSTING_READ(SOUTH_CHICKEN1);
  3850. }
  3851. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3852. {
  3853. struct drm_device *dev = intel_crtc->base.dev;
  3854. switch (intel_crtc->pipe) {
  3855. case PIPE_A:
  3856. break;
  3857. case PIPE_B:
  3858. if (intel_crtc->config->fdi_lanes > 2)
  3859. cpt_set_fdi_bc_bifurcation(dev, false);
  3860. else
  3861. cpt_set_fdi_bc_bifurcation(dev, true);
  3862. break;
  3863. case PIPE_C:
  3864. cpt_set_fdi_bc_bifurcation(dev, true);
  3865. break;
  3866. default:
  3867. BUG();
  3868. }
  3869. }
  3870. /* Return which DP Port should be selected for Transcoder DP control */
  3871. static enum port
  3872. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3873. {
  3874. struct drm_device *dev = crtc->base.dev;
  3875. struct intel_encoder *encoder;
  3876. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3877. if (encoder->type == INTEL_OUTPUT_DP ||
  3878. encoder->type == INTEL_OUTPUT_EDP)
  3879. return encoder->port;
  3880. }
  3881. return -1;
  3882. }
  3883. /*
  3884. * Enable PCH resources required for PCH ports:
  3885. * - PCH PLLs
  3886. * - FDI training & RX/TX
  3887. * - update transcoder timings
  3888. * - DP transcoding bits
  3889. * - transcoder
  3890. */
  3891. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3892. {
  3893. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3894. struct drm_device *dev = crtc->base.dev;
  3895. struct drm_i915_private *dev_priv = to_i915(dev);
  3896. int pipe = crtc->pipe;
  3897. u32 temp;
  3898. assert_pch_transcoder_disabled(dev_priv, pipe);
  3899. if (IS_IVYBRIDGE(dev_priv))
  3900. ivybridge_update_fdi_bc_bifurcation(crtc);
  3901. /* Write the TU size bits before fdi link training, so that error
  3902. * detection works. */
  3903. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3904. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3905. /* For PCH output, training FDI link */
  3906. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3907. /* We need to program the right clock selection before writing the pixel
  3908. * mutliplier into the DPLL. */
  3909. if (HAS_PCH_CPT(dev_priv)) {
  3910. u32 sel;
  3911. temp = I915_READ(PCH_DPLL_SEL);
  3912. temp |= TRANS_DPLL_ENABLE(pipe);
  3913. sel = TRANS_DPLLB_SEL(pipe);
  3914. if (crtc_state->shared_dpll ==
  3915. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3916. temp |= sel;
  3917. else
  3918. temp &= ~sel;
  3919. I915_WRITE(PCH_DPLL_SEL, temp);
  3920. }
  3921. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3922. * transcoder, and we actually should do this to not upset any PCH
  3923. * transcoder that already use the clock when we share it.
  3924. *
  3925. * Note that enable_shared_dpll tries to do the right thing, but
  3926. * get_shared_dpll unconditionally resets the pll - we need that to have
  3927. * the right LVDS enable sequence. */
  3928. intel_enable_shared_dpll(crtc);
  3929. /* set transcoder timing, panel must allow it */
  3930. assert_panel_unlocked(dev_priv, pipe);
  3931. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3932. intel_fdi_normal_train(crtc);
  3933. /* For PCH DP, enable TRANS_DP_CTL */
  3934. if (HAS_PCH_CPT(dev_priv) &&
  3935. intel_crtc_has_dp_encoder(crtc_state)) {
  3936. const struct drm_display_mode *adjusted_mode =
  3937. &crtc_state->base.adjusted_mode;
  3938. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3939. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3940. temp = I915_READ(reg);
  3941. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3942. TRANS_DP_SYNC_MASK |
  3943. TRANS_DP_BPC_MASK);
  3944. temp |= TRANS_DP_OUTPUT_ENABLE;
  3945. temp |= bpc << 9; /* same format but at 11:9 */
  3946. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3947. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3948. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3949. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3950. switch (intel_trans_dp_port_sel(crtc)) {
  3951. case PORT_B:
  3952. temp |= TRANS_DP_PORT_SEL_B;
  3953. break;
  3954. case PORT_C:
  3955. temp |= TRANS_DP_PORT_SEL_C;
  3956. break;
  3957. case PORT_D:
  3958. temp |= TRANS_DP_PORT_SEL_D;
  3959. break;
  3960. default:
  3961. BUG();
  3962. }
  3963. I915_WRITE(reg, temp);
  3964. }
  3965. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3966. }
  3967. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3968. {
  3969. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3970. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3971. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3972. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3973. lpt_program_iclkip(crtc);
  3974. /* Set transcoder timing. */
  3975. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3976. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3977. }
  3978. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3979. {
  3980. struct drm_i915_private *dev_priv = to_i915(dev);
  3981. i915_reg_t dslreg = PIPEDSL(pipe);
  3982. u32 temp;
  3983. temp = I915_READ(dslreg);
  3984. udelay(500);
  3985. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3986. if (wait_for(I915_READ(dslreg) != temp, 5))
  3987. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3988. }
  3989. }
  3990. static int
  3991. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3992. unsigned int scaler_user, int *scaler_id,
  3993. int src_w, int src_h, int dst_w, int dst_h)
  3994. {
  3995. struct intel_crtc_scaler_state *scaler_state =
  3996. &crtc_state->scaler_state;
  3997. struct intel_crtc *intel_crtc =
  3998. to_intel_crtc(crtc_state->base.crtc);
  3999. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4000. const struct drm_display_mode *adjusted_mode =
  4001. &crtc_state->base.adjusted_mode;
  4002. int need_scaling;
  4003. /*
  4004. * Src coordinates are already rotated by 270 degrees for
  4005. * the 90/270 degree plane rotation cases (to match the
  4006. * GTT mapping), hence no need to account for rotation here.
  4007. */
  4008. need_scaling = src_w != dst_w || src_h != dst_h;
  4009. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4010. need_scaling = true;
  4011. /*
  4012. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4013. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4014. * Once NV12 is enabled, handle it here while allocating scaler
  4015. * for NV12.
  4016. */
  4017. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4018. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4019. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4020. return -EINVAL;
  4021. }
  4022. /*
  4023. * if plane is being disabled or scaler is no more required or force detach
  4024. * - free scaler binded to this plane/crtc
  4025. * - in order to do this, update crtc->scaler_usage
  4026. *
  4027. * Here scaler state in crtc_state is set free so that
  4028. * scaler can be assigned to other user. Actual register
  4029. * update to free the scaler is done in plane/panel-fit programming.
  4030. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4031. */
  4032. if (force_detach || !need_scaling) {
  4033. if (*scaler_id >= 0) {
  4034. scaler_state->scaler_users &= ~(1 << scaler_user);
  4035. scaler_state->scalers[*scaler_id].in_use = 0;
  4036. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4037. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4038. intel_crtc->pipe, scaler_user, *scaler_id,
  4039. scaler_state->scaler_users);
  4040. *scaler_id = -1;
  4041. }
  4042. return 0;
  4043. }
  4044. /* range checks */
  4045. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4046. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4047. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4048. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4049. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4050. "size is out of scaler range\n",
  4051. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4052. return -EINVAL;
  4053. }
  4054. /* mark this plane as a scaler user in crtc_state */
  4055. scaler_state->scaler_users |= (1 << scaler_user);
  4056. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4057. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4058. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4059. scaler_state->scaler_users);
  4060. return 0;
  4061. }
  4062. /**
  4063. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4064. *
  4065. * @state: crtc's scaler state
  4066. *
  4067. * Return
  4068. * 0 - scaler_usage updated successfully
  4069. * error - requested scaling cannot be supported or other error condition
  4070. */
  4071. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4072. {
  4073. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4074. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4075. &state->scaler_state.scaler_id,
  4076. state->pipe_src_w, state->pipe_src_h,
  4077. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4078. }
  4079. /**
  4080. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4081. * @crtc_state: crtc's scaler state
  4082. * @plane_state: atomic plane state to update
  4083. *
  4084. * Return
  4085. * 0 - scaler_usage updated successfully
  4086. * error - requested scaling cannot be supported or other error condition
  4087. */
  4088. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4089. struct intel_plane_state *plane_state)
  4090. {
  4091. struct intel_plane *intel_plane =
  4092. to_intel_plane(plane_state->base.plane);
  4093. struct drm_framebuffer *fb = plane_state->base.fb;
  4094. int ret;
  4095. bool force_detach = !fb || !plane_state->base.visible;
  4096. ret = skl_update_scaler(crtc_state, force_detach,
  4097. drm_plane_index(&intel_plane->base),
  4098. &plane_state->scaler_id,
  4099. drm_rect_width(&plane_state->base.src) >> 16,
  4100. drm_rect_height(&plane_state->base.src) >> 16,
  4101. drm_rect_width(&plane_state->base.dst),
  4102. drm_rect_height(&plane_state->base.dst));
  4103. if (ret || plane_state->scaler_id < 0)
  4104. return ret;
  4105. /* check colorkey */
  4106. if (plane_state->ckey.flags) {
  4107. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4108. intel_plane->base.base.id,
  4109. intel_plane->base.name);
  4110. return -EINVAL;
  4111. }
  4112. /* Check src format */
  4113. switch (fb->format->format) {
  4114. case DRM_FORMAT_RGB565:
  4115. case DRM_FORMAT_XBGR8888:
  4116. case DRM_FORMAT_XRGB8888:
  4117. case DRM_FORMAT_ABGR8888:
  4118. case DRM_FORMAT_ARGB8888:
  4119. case DRM_FORMAT_XRGB2101010:
  4120. case DRM_FORMAT_XBGR2101010:
  4121. case DRM_FORMAT_YUYV:
  4122. case DRM_FORMAT_YVYU:
  4123. case DRM_FORMAT_UYVY:
  4124. case DRM_FORMAT_VYUY:
  4125. break;
  4126. default:
  4127. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4128. intel_plane->base.base.id, intel_plane->base.name,
  4129. fb->base.id, fb->format->format);
  4130. return -EINVAL;
  4131. }
  4132. return 0;
  4133. }
  4134. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4135. {
  4136. int i;
  4137. for (i = 0; i < crtc->num_scalers; i++)
  4138. skl_detach_scaler(crtc, i);
  4139. }
  4140. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4141. {
  4142. struct drm_device *dev = crtc->base.dev;
  4143. struct drm_i915_private *dev_priv = to_i915(dev);
  4144. int pipe = crtc->pipe;
  4145. struct intel_crtc_scaler_state *scaler_state =
  4146. &crtc->config->scaler_state;
  4147. if (crtc->config->pch_pfit.enabled) {
  4148. int id;
  4149. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4150. return;
  4151. id = scaler_state->scaler_id;
  4152. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4153. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4154. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4155. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4156. }
  4157. }
  4158. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4159. {
  4160. struct drm_device *dev = crtc->base.dev;
  4161. struct drm_i915_private *dev_priv = to_i915(dev);
  4162. int pipe = crtc->pipe;
  4163. if (crtc->config->pch_pfit.enabled) {
  4164. /* Force use of hard-coded filter coefficients
  4165. * as some pre-programmed values are broken,
  4166. * e.g. x201.
  4167. */
  4168. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4169. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4170. PF_PIPE_SEL_IVB(pipe));
  4171. else
  4172. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4173. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4174. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4175. }
  4176. }
  4177. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4178. {
  4179. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4180. struct drm_device *dev = crtc->base.dev;
  4181. struct drm_i915_private *dev_priv = to_i915(dev);
  4182. if (!crtc_state->ips_enabled)
  4183. return;
  4184. /*
  4185. * We can only enable IPS after we enable a plane and wait for a vblank
  4186. * This function is called from post_plane_update, which is run after
  4187. * a vblank wait.
  4188. */
  4189. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4190. if (IS_BROADWELL(dev_priv)) {
  4191. mutex_lock(&dev_priv->pcu_lock);
  4192. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4193. IPS_ENABLE | IPS_PCODE_CONTROL));
  4194. mutex_unlock(&dev_priv->pcu_lock);
  4195. /* Quoting Art Runyan: "its not safe to expect any particular
  4196. * value in IPS_CTL bit 31 after enabling IPS through the
  4197. * mailbox." Moreover, the mailbox may return a bogus state,
  4198. * so we need to just enable it and continue on.
  4199. */
  4200. } else {
  4201. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4202. /* The bit only becomes 1 in the next vblank, so this wait here
  4203. * is essentially intel_wait_for_vblank. If we don't have this
  4204. * and don't wait for vblanks until the end of crtc_enable, then
  4205. * the HW state readout code will complain that the expected
  4206. * IPS_CTL value is not the one we read. */
  4207. if (intel_wait_for_register(dev_priv,
  4208. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4209. 50))
  4210. DRM_ERROR("Timed out waiting for IPS enable\n");
  4211. }
  4212. }
  4213. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4214. {
  4215. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4216. struct drm_device *dev = crtc->base.dev;
  4217. struct drm_i915_private *dev_priv = to_i915(dev);
  4218. if (!crtc_state->ips_enabled)
  4219. return;
  4220. if (IS_BROADWELL(dev_priv)) {
  4221. mutex_lock(&dev_priv->pcu_lock);
  4222. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4223. mutex_unlock(&dev_priv->pcu_lock);
  4224. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4225. if (intel_wait_for_register(dev_priv,
  4226. IPS_CTL, IPS_ENABLE, 0,
  4227. 42))
  4228. DRM_ERROR("Timed out waiting for IPS disable\n");
  4229. } else {
  4230. I915_WRITE(IPS_CTL, 0);
  4231. POSTING_READ(IPS_CTL);
  4232. }
  4233. /* We need to wait for a vblank before we can disable the plane. */
  4234. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4235. }
  4236. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4237. {
  4238. if (intel_crtc->overlay) {
  4239. struct drm_device *dev = intel_crtc->base.dev;
  4240. mutex_lock(&dev->struct_mutex);
  4241. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4242. mutex_unlock(&dev->struct_mutex);
  4243. }
  4244. /* Let userspace switch the overlay on again. In most cases userspace
  4245. * has to recompute where to put it anyway.
  4246. */
  4247. }
  4248. /**
  4249. * intel_post_enable_primary - Perform operations after enabling primary plane
  4250. * @crtc: the CRTC whose primary plane was just enabled
  4251. * @new_crtc_state: the enabling state
  4252. *
  4253. * Performs potentially sleeping operations that must be done after the primary
  4254. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4255. * called due to an explicit primary plane update, or due to an implicit
  4256. * re-enable that is caused when a sprite plane is updated to no longer
  4257. * completely hide the primary plane.
  4258. */
  4259. static void
  4260. intel_post_enable_primary(struct drm_crtc *crtc,
  4261. const struct intel_crtc_state *new_crtc_state)
  4262. {
  4263. struct drm_device *dev = crtc->dev;
  4264. struct drm_i915_private *dev_priv = to_i915(dev);
  4265. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4266. int pipe = intel_crtc->pipe;
  4267. /*
  4268. * Gen2 reports pipe underruns whenever all planes are disabled.
  4269. * So don't enable underrun reporting before at least some planes
  4270. * are enabled.
  4271. * FIXME: Need to fix the logic to work when we turn off all planes
  4272. * but leave the pipe running.
  4273. */
  4274. if (IS_GEN2(dev_priv))
  4275. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4276. /* Underruns don't always raise interrupts, so check manually. */
  4277. intel_check_cpu_fifo_underruns(dev_priv);
  4278. intel_check_pch_fifo_underruns(dev_priv);
  4279. }
  4280. /* FIXME get rid of this and use pre_plane_update */
  4281. static void
  4282. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4283. {
  4284. struct drm_device *dev = crtc->dev;
  4285. struct drm_i915_private *dev_priv = to_i915(dev);
  4286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4287. int pipe = intel_crtc->pipe;
  4288. /*
  4289. * Gen2 reports pipe underruns whenever all planes are disabled.
  4290. * So disable underrun reporting before all the planes get disabled.
  4291. */
  4292. if (IS_GEN2(dev_priv))
  4293. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4294. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4295. /*
  4296. * Vblank time updates from the shadow to live plane control register
  4297. * are blocked if the memory self-refresh mode is active at that
  4298. * moment. So to make sure the plane gets truly disabled, disable
  4299. * first the self-refresh mode. The self-refresh enable bit in turn
  4300. * will be checked/applied by the HW only at the next frame start
  4301. * event which is after the vblank start event, so we need to have a
  4302. * wait-for-vblank between disabling the plane and the pipe.
  4303. */
  4304. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4305. intel_set_memory_cxsr(dev_priv, false))
  4306. intel_wait_for_vblank(dev_priv, pipe);
  4307. }
  4308. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4309. const struct intel_crtc_state *new_crtc_state)
  4310. {
  4311. if (!old_crtc_state->ips_enabled)
  4312. return false;
  4313. if (needs_modeset(&new_crtc_state->base))
  4314. return true;
  4315. return !new_crtc_state->ips_enabled;
  4316. }
  4317. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4318. const struct intel_crtc_state *new_crtc_state)
  4319. {
  4320. if (!new_crtc_state->ips_enabled)
  4321. return false;
  4322. if (needs_modeset(&new_crtc_state->base))
  4323. return true;
  4324. /*
  4325. * We can't read out IPS on broadwell, assume the worst and
  4326. * forcibly enable IPS on the first fastset.
  4327. */
  4328. if (new_crtc_state->update_pipe &&
  4329. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4330. return true;
  4331. return !old_crtc_state->ips_enabled;
  4332. }
  4333. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4334. {
  4335. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4336. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4337. struct intel_crtc_state *pipe_config =
  4338. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4339. crtc);
  4340. struct drm_plane *primary = crtc->base.primary;
  4341. struct drm_plane_state *old_pri_state =
  4342. drm_atomic_get_existing_plane_state(old_state, primary);
  4343. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4344. if (pipe_config->update_wm_post && pipe_config->base.active)
  4345. intel_update_watermarks(crtc);
  4346. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4347. hsw_enable_ips(pipe_config);
  4348. if (old_pri_state) {
  4349. struct intel_plane_state *primary_state =
  4350. intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
  4351. to_intel_plane(primary));
  4352. struct intel_plane_state *old_primary_state =
  4353. to_intel_plane_state(old_pri_state);
  4354. intel_fbc_post_update(crtc);
  4355. if (primary_state->base.visible &&
  4356. (needs_modeset(&pipe_config->base) ||
  4357. !old_primary_state->base.visible))
  4358. intel_post_enable_primary(&crtc->base, pipe_config);
  4359. }
  4360. }
  4361. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4362. struct intel_crtc_state *pipe_config)
  4363. {
  4364. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4365. struct drm_device *dev = crtc->base.dev;
  4366. struct drm_i915_private *dev_priv = to_i915(dev);
  4367. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4368. struct drm_plane *primary = crtc->base.primary;
  4369. struct drm_plane_state *old_pri_state =
  4370. drm_atomic_get_existing_plane_state(old_state, primary);
  4371. bool modeset = needs_modeset(&pipe_config->base);
  4372. struct intel_atomic_state *old_intel_state =
  4373. to_intel_atomic_state(old_state);
  4374. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4375. hsw_disable_ips(old_crtc_state);
  4376. if (old_pri_state) {
  4377. struct intel_plane_state *primary_state =
  4378. intel_atomic_get_new_plane_state(old_intel_state,
  4379. to_intel_plane(primary));
  4380. struct intel_plane_state *old_primary_state =
  4381. to_intel_plane_state(old_pri_state);
  4382. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4383. /*
  4384. * Gen2 reports pipe underruns whenever all planes are disabled.
  4385. * So disable underrun reporting before all the planes get disabled.
  4386. */
  4387. if (IS_GEN2(dev_priv) && old_primary_state->base.visible &&
  4388. (modeset || !primary_state->base.visible))
  4389. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4390. }
  4391. /*
  4392. * Vblank time updates from the shadow to live plane control register
  4393. * are blocked if the memory self-refresh mode is active at that
  4394. * moment. So to make sure the plane gets truly disabled, disable
  4395. * first the self-refresh mode. The self-refresh enable bit in turn
  4396. * will be checked/applied by the HW only at the next frame start
  4397. * event which is after the vblank start event, so we need to have a
  4398. * wait-for-vblank between disabling the plane and the pipe.
  4399. */
  4400. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4401. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4402. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4403. /*
  4404. * IVB workaround: must disable low power watermarks for at least
  4405. * one frame before enabling scaling. LP watermarks can be re-enabled
  4406. * when scaling is disabled.
  4407. *
  4408. * WaCxSRDisabledForSpriteScaling:ivb
  4409. */
  4410. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4411. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4412. /*
  4413. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4414. * watermark programming here.
  4415. */
  4416. if (needs_modeset(&pipe_config->base))
  4417. return;
  4418. /*
  4419. * For platforms that support atomic watermarks, program the
  4420. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4421. * will be the intermediate values that are safe for both pre- and
  4422. * post- vblank; when vblank happens, the 'active' values will be set
  4423. * to the final 'target' values and we'll do this again to get the
  4424. * optimal watermarks. For gen9+ platforms, the values we program here
  4425. * will be the final target values which will get automatically latched
  4426. * at vblank time; no further programming will be necessary.
  4427. *
  4428. * If a platform hasn't been transitioned to atomic watermarks yet,
  4429. * we'll continue to update watermarks the old way, if flags tell
  4430. * us to.
  4431. */
  4432. if (dev_priv->display.initial_watermarks != NULL)
  4433. dev_priv->display.initial_watermarks(old_intel_state,
  4434. pipe_config);
  4435. else if (pipe_config->update_wm_pre)
  4436. intel_update_watermarks(crtc);
  4437. }
  4438. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4439. {
  4440. struct drm_device *dev = crtc->dev;
  4441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4442. struct drm_plane *p;
  4443. int pipe = intel_crtc->pipe;
  4444. intel_crtc_dpms_overlay_disable(intel_crtc);
  4445. drm_for_each_plane_mask(p, dev, plane_mask)
  4446. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4447. /*
  4448. * FIXME: Once we grow proper nuclear flip support out of this we need
  4449. * to compute the mask of flip planes precisely. For the time being
  4450. * consider this a flip to a NULL plane.
  4451. */
  4452. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4453. }
  4454. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4455. struct intel_crtc_state *crtc_state,
  4456. struct drm_atomic_state *old_state)
  4457. {
  4458. struct drm_connector_state *conn_state;
  4459. struct drm_connector *conn;
  4460. int i;
  4461. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4462. struct intel_encoder *encoder =
  4463. to_intel_encoder(conn_state->best_encoder);
  4464. if (conn_state->crtc != crtc)
  4465. continue;
  4466. if (encoder->pre_pll_enable)
  4467. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4468. }
  4469. }
  4470. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4471. struct intel_crtc_state *crtc_state,
  4472. struct drm_atomic_state *old_state)
  4473. {
  4474. struct drm_connector_state *conn_state;
  4475. struct drm_connector *conn;
  4476. int i;
  4477. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4478. struct intel_encoder *encoder =
  4479. to_intel_encoder(conn_state->best_encoder);
  4480. if (conn_state->crtc != crtc)
  4481. continue;
  4482. if (encoder->pre_enable)
  4483. encoder->pre_enable(encoder, crtc_state, conn_state);
  4484. }
  4485. }
  4486. static void intel_encoders_enable(struct drm_crtc *crtc,
  4487. struct intel_crtc_state *crtc_state,
  4488. struct drm_atomic_state *old_state)
  4489. {
  4490. struct drm_connector_state *conn_state;
  4491. struct drm_connector *conn;
  4492. int i;
  4493. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4494. struct intel_encoder *encoder =
  4495. to_intel_encoder(conn_state->best_encoder);
  4496. if (conn_state->crtc != crtc)
  4497. continue;
  4498. encoder->enable(encoder, crtc_state, conn_state);
  4499. intel_opregion_notify_encoder(encoder, true);
  4500. }
  4501. }
  4502. static void intel_encoders_disable(struct drm_crtc *crtc,
  4503. struct intel_crtc_state *old_crtc_state,
  4504. struct drm_atomic_state *old_state)
  4505. {
  4506. struct drm_connector_state *old_conn_state;
  4507. struct drm_connector *conn;
  4508. int i;
  4509. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4510. struct intel_encoder *encoder =
  4511. to_intel_encoder(old_conn_state->best_encoder);
  4512. if (old_conn_state->crtc != crtc)
  4513. continue;
  4514. intel_opregion_notify_encoder(encoder, false);
  4515. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4516. }
  4517. }
  4518. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4519. struct intel_crtc_state *old_crtc_state,
  4520. struct drm_atomic_state *old_state)
  4521. {
  4522. struct drm_connector_state *old_conn_state;
  4523. struct drm_connector *conn;
  4524. int i;
  4525. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4526. struct intel_encoder *encoder =
  4527. to_intel_encoder(old_conn_state->best_encoder);
  4528. if (old_conn_state->crtc != crtc)
  4529. continue;
  4530. if (encoder->post_disable)
  4531. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4532. }
  4533. }
  4534. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4535. struct intel_crtc_state *old_crtc_state,
  4536. struct drm_atomic_state *old_state)
  4537. {
  4538. struct drm_connector_state *old_conn_state;
  4539. struct drm_connector *conn;
  4540. int i;
  4541. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4542. struct intel_encoder *encoder =
  4543. to_intel_encoder(old_conn_state->best_encoder);
  4544. if (old_conn_state->crtc != crtc)
  4545. continue;
  4546. if (encoder->post_pll_disable)
  4547. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4548. }
  4549. }
  4550. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4551. struct drm_atomic_state *old_state)
  4552. {
  4553. struct drm_crtc *crtc = pipe_config->base.crtc;
  4554. struct drm_device *dev = crtc->dev;
  4555. struct drm_i915_private *dev_priv = to_i915(dev);
  4556. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4557. int pipe = intel_crtc->pipe;
  4558. struct intel_atomic_state *old_intel_state =
  4559. to_intel_atomic_state(old_state);
  4560. if (WARN_ON(intel_crtc->active))
  4561. return;
  4562. /*
  4563. * Sometimes spurious CPU pipe underruns happen during FDI
  4564. * training, at least with VGA+HDMI cloning. Suppress them.
  4565. *
  4566. * On ILK we get an occasional spurious CPU pipe underruns
  4567. * between eDP port A enable and vdd enable. Also PCH port
  4568. * enable seems to result in the occasional CPU pipe underrun.
  4569. *
  4570. * Spurious PCH underruns also occur during PCH enabling.
  4571. */
  4572. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4573. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4574. if (intel_crtc->config->has_pch_encoder)
  4575. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4576. if (intel_crtc->config->has_pch_encoder)
  4577. intel_prepare_shared_dpll(intel_crtc);
  4578. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4579. intel_dp_set_m_n(intel_crtc, M1_N1);
  4580. intel_set_pipe_timings(intel_crtc);
  4581. intel_set_pipe_src_size(intel_crtc);
  4582. if (intel_crtc->config->has_pch_encoder) {
  4583. intel_cpu_transcoder_set_m_n(intel_crtc,
  4584. &intel_crtc->config->fdi_m_n, NULL);
  4585. }
  4586. ironlake_set_pipeconf(crtc);
  4587. intel_crtc->active = true;
  4588. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4589. if (intel_crtc->config->has_pch_encoder) {
  4590. /* Note: FDI PLL enabling _must_ be done before we enable the
  4591. * cpu pipes, hence this is separate from all the other fdi/pch
  4592. * enabling. */
  4593. ironlake_fdi_pll_enable(intel_crtc);
  4594. } else {
  4595. assert_fdi_tx_disabled(dev_priv, pipe);
  4596. assert_fdi_rx_disabled(dev_priv, pipe);
  4597. }
  4598. ironlake_pfit_enable(intel_crtc);
  4599. /*
  4600. * On ILK+ LUT must be loaded before the pipe is running but with
  4601. * clocks enabled
  4602. */
  4603. intel_color_load_luts(&pipe_config->base);
  4604. if (dev_priv->display.initial_watermarks != NULL)
  4605. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4606. intel_enable_pipe(pipe_config);
  4607. if (intel_crtc->config->has_pch_encoder)
  4608. ironlake_pch_enable(pipe_config);
  4609. assert_vblank_disabled(crtc);
  4610. drm_crtc_vblank_on(crtc);
  4611. intel_encoders_enable(crtc, pipe_config, old_state);
  4612. if (HAS_PCH_CPT(dev_priv))
  4613. cpt_verify_modeset(dev, intel_crtc->pipe);
  4614. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4615. if (intel_crtc->config->has_pch_encoder)
  4616. intel_wait_for_vblank(dev_priv, pipe);
  4617. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4618. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4619. }
  4620. /* IPS only exists on ULT machines and is tied to pipe A. */
  4621. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4622. {
  4623. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4624. }
  4625. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4626. enum pipe pipe, bool apply)
  4627. {
  4628. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4629. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4630. if (apply)
  4631. val |= mask;
  4632. else
  4633. val &= ~mask;
  4634. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4635. }
  4636. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4637. {
  4638. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4639. enum pipe pipe = crtc->pipe;
  4640. uint32_t val;
  4641. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4642. /* Program B credit equally to all pipes */
  4643. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4644. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4645. }
  4646. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4647. struct drm_atomic_state *old_state)
  4648. {
  4649. struct drm_crtc *crtc = pipe_config->base.crtc;
  4650. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4651. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4652. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4653. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4654. struct intel_atomic_state *old_intel_state =
  4655. to_intel_atomic_state(old_state);
  4656. bool psl_clkgate_wa;
  4657. if (WARN_ON(intel_crtc->active))
  4658. return;
  4659. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4660. if (intel_crtc->config->shared_dpll)
  4661. intel_enable_shared_dpll(intel_crtc);
  4662. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4663. intel_dp_set_m_n(intel_crtc, M1_N1);
  4664. if (!transcoder_is_dsi(cpu_transcoder))
  4665. intel_set_pipe_timings(intel_crtc);
  4666. intel_set_pipe_src_size(intel_crtc);
  4667. if (cpu_transcoder != TRANSCODER_EDP &&
  4668. !transcoder_is_dsi(cpu_transcoder)) {
  4669. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4670. intel_crtc->config->pixel_multiplier - 1);
  4671. }
  4672. if (intel_crtc->config->has_pch_encoder) {
  4673. intel_cpu_transcoder_set_m_n(intel_crtc,
  4674. &intel_crtc->config->fdi_m_n, NULL);
  4675. }
  4676. if (!transcoder_is_dsi(cpu_transcoder))
  4677. haswell_set_pipeconf(crtc);
  4678. haswell_set_pipemisc(crtc);
  4679. intel_color_set_csc(&pipe_config->base);
  4680. intel_crtc->active = true;
  4681. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4682. if (!transcoder_is_dsi(cpu_transcoder))
  4683. intel_ddi_enable_pipe_clock(pipe_config);
  4684. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4685. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4686. intel_crtc->config->pch_pfit.enabled;
  4687. if (psl_clkgate_wa)
  4688. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4689. if (INTEL_GEN(dev_priv) >= 9)
  4690. skylake_pfit_enable(intel_crtc);
  4691. else
  4692. ironlake_pfit_enable(intel_crtc);
  4693. /*
  4694. * On ILK+ LUT must be loaded before the pipe is running but with
  4695. * clocks enabled
  4696. */
  4697. intel_color_load_luts(&pipe_config->base);
  4698. intel_ddi_set_pipe_settings(pipe_config);
  4699. if (!transcoder_is_dsi(cpu_transcoder))
  4700. intel_ddi_enable_transcoder_func(pipe_config);
  4701. if (dev_priv->display.initial_watermarks != NULL)
  4702. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4703. if (INTEL_GEN(dev_priv) >= 11)
  4704. icl_pipe_mbus_enable(intel_crtc);
  4705. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4706. if (!transcoder_is_dsi(cpu_transcoder))
  4707. intel_enable_pipe(pipe_config);
  4708. if (intel_crtc->config->has_pch_encoder)
  4709. lpt_pch_enable(pipe_config);
  4710. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4711. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4712. assert_vblank_disabled(crtc);
  4713. drm_crtc_vblank_on(crtc);
  4714. intel_encoders_enable(crtc, pipe_config, old_state);
  4715. if (psl_clkgate_wa) {
  4716. intel_wait_for_vblank(dev_priv, pipe);
  4717. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4718. }
  4719. /* If we change the relative order between pipe/planes enabling, we need
  4720. * to change the workaround. */
  4721. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4722. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4723. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4724. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4725. }
  4726. }
  4727. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4728. {
  4729. struct drm_device *dev = crtc->base.dev;
  4730. struct drm_i915_private *dev_priv = to_i915(dev);
  4731. int pipe = crtc->pipe;
  4732. /* To avoid upsetting the power well on haswell only disable the pfit if
  4733. * it's in use. The hw state code will make sure we get this right. */
  4734. if (force || crtc->config->pch_pfit.enabled) {
  4735. I915_WRITE(PF_CTL(pipe), 0);
  4736. I915_WRITE(PF_WIN_POS(pipe), 0);
  4737. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4738. }
  4739. }
  4740. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4741. struct drm_atomic_state *old_state)
  4742. {
  4743. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4744. struct drm_device *dev = crtc->dev;
  4745. struct drm_i915_private *dev_priv = to_i915(dev);
  4746. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4747. int pipe = intel_crtc->pipe;
  4748. /*
  4749. * Sometimes spurious CPU pipe underruns happen when the
  4750. * pipe is already disabled, but FDI RX/TX is still enabled.
  4751. * Happens at least with VGA+HDMI cloning. Suppress them.
  4752. */
  4753. if (intel_crtc->config->has_pch_encoder) {
  4754. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4755. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4756. }
  4757. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4758. drm_crtc_vblank_off(crtc);
  4759. assert_vblank_disabled(crtc);
  4760. intel_disable_pipe(old_crtc_state);
  4761. ironlake_pfit_disable(intel_crtc, false);
  4762. if (intel_crtc->config->has_pch_encoder)
  4763. ironlake_fdi_disable(crtc);
  4764. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4765. if (intel_crtc->config->has_pch_encoder) {
  4766. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4767. if (HAS_PCH_CPT(dev_priv)) {
  4768. i915_reg_t reg;
  4769. u32 temp;
  4770. /* disable TRANS_DP_CTL */
  4771. reg = TRANS_DP_CTL(pipe);
  4772. temp = I915_READ(reg);
  4773. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4774. TRANS_DP_PORT_SEL_MASK);
  4775. temp |= TRANS_DP_PORT_SEL_NONE;
  4776. I915_WRITE(reg, temp);
  4777. /* disable DPLL_SEL */
  4778. temp = I915_READ(PCH_DPLL_SEL);
  4779. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4780. I915_WRITE(PCH_DPLL_SEL, temp);
  4781. }
  4782. ironlake_fdi_pll_disable(intel_crtc);
  4783. }
  4784. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4785. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4786. }
  4787. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4788. struct drm_atomic_state *old_state)
  4789. {
  4790. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4791. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4792. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4793. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4794. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4795. drm_crtc_vblank_off(crtc);
  4796. assert_vblank_disabled(crtc);
  4797. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4798. if (!transcoder_is_dsi(cpu_transcoder))
  4799. intel_disable_pipe(old_crtc_state);
  4800. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4801. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4802. if (!transcoder_is_dsi(cpu_transcoder))
  4803. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4804. if (INTEL_GEN(dev_priv) >= 9)
  4805. skylake_scaler_disable(intel_crtc);
  4806. else
  4807. ironlake_pfit_disable(intel_crtc, false);
  4808. if (!transcoder_is_dsi(cpu_transcoder))
  4809. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4810. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4811. }
  4812. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4813. {
  4814. struct drm_device *dev = crtc->base.dev;
  4815. struct drm_i915_private *dev_priv = to_i915(dev);
  4816. struct intel_crtc_state *pipe_config = crtc->config;
  4817. if (!pipe_config->gmch_pfit.control)
  4818. return;
  4819. /*
  4820. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4821. * according to register description and PRM.
  4822. */
  4823. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4824. assert_pipe_disabled(dev_priv, crtc->pipe);
  4825. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4826. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4827. /* Border color in case we don't scale up to the full screen. Black by
  4828. * default, change to something else for debugging. */
  4829. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4830. }
  4831. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4832. {
  4833. switch (port) {
  4834. case PORT_A:
  4835. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4836. case PORT_B:
  4837. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4838. case PORT_C:
  4839. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4840. case PORT_D:
  4841. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4842. case PORT_E:
  4843. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4844. case PORT_F:
  4845. return POWER_DOMAIN_PORT_DDI_F_LANES;
  4846. default:
  4847. MISSING_CASE(port);
  4848. return POWER_DOMAIN_PORT_OTHER;
  4849. }
  4850. }
  4851. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4852. struct intel_crtc_state *crtc_state)
  4853. {
  4854. struct drm_device *dev = crtc->dev;
  4855. struct drm_i915_private *dev_priv = to_i915(dev);
  4856. struct drm_encoder *encoder;
  4857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4858. enum pipe pipe = intel_crtc->pipe;
  4859. u64 mask;
  4860. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4861. if (!crtc_state->base.active)
  4862. return 0;
  4863. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  4864. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  4865. if (crtc_state->pch_pfit.enabled ||
  4866. crtc_state->pch_pfit.force_thru)
  4867. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4868. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4869. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4870. mask |= BIT_ULL(intel_encoder->power_domain);
  4871. }
  4872. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4873. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  4874. if (crtc_state->shared_dpll)
  4875. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4876. return mask;
  4877. }
  4878. static u64
  4879. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4880. struct intel_crtc_state *crtc_state)
  4881. {
  4882. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4884. enum intel_display_power_domain domain;
  4885. u64 domains, new_domains, old_domains;
  4886. old_domains = intel_crtc->enabled_power_domains;
  4887. intel_crtc->enabled_power_domains = new_domains =
  4888. get_crtc_power_domains(crtc, crtc_state);
  4889. domains = new_domains & ~old_domains;
  4890. for_each_power_domain(domain, domains)
  4891. intel_display_power_get(dev_priv, domain);
  4892. return old_domains & ~new_domains;
  4893. }
  4894. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4895. u64 domains)
  4896. {
  4897. enum intel_display_power_domain domain;
  4898. for_each_power_domain(domain, domains)
  4899. intel_display_power_put(dev_priv, domain);
  4900. }
  4901. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4902. struct drm_atomic_state *old_state)
  4903. {
  4904. struct intel_atomic_state *old_intel_state =
  4905. to_intel_atomic_state(old_state);
  4906. struct drm_crtc *crtc = pipe_config->base.crtc;
  4907. struct drm_device *dev = crtc->dev;
  4908. struct drm_i915_private *dev_priv = to_i915(dev);
  4909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4910. int pipe = intel_crtc->pipe;
  4911. if (WARN_ON(intel_crtc->active))
  4912. return;
  4913. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4914. intel_dp_set_m_n(intel_crtc, M1_N1);
  4915. intel_set_pipe_timings(intel_crtc);
  4916. intel_set_pipe_src_size(intel_crtc);
  4917. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4918. struct drm_i915_private *dev_priv = to_i915(dev);
  4919. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4920. I915_WRITE(CHV_CANVAS(pipe), 0);
  4921. }
  4922. i9xx_set_pipeconf(intel_crtc);
  4923. intel_crtc->active = true;
  4924. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4925. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4926. if (IS_CHERRYVIEW(dev_priv)) {
  4927. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4928. chv_enable_pll(intel_crtc, intel_crtc->config);
  4929. } else {
  4930. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4931. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4932. }
  4933. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4934. i9xx_pfit_enable(intel_crtc);
  4935. intel_color_load_luts(&pipe_config->base);
  4936. dev_priv->display.initial_watermarks(old_intel_state,
  4937. pipe_config);
  4938. intel_enable_pipe(pipe_config);
  4939. assert_vblank_disabled(crtc);
  4940. drm_crtc_vblank_on(crtc);
  4941. intel_encoders_enable(crtc, pipe_config, old_state);
  4942. }
  4943. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4944. {
  4945. struct drm_device *dev = crtc->base.dev;
  4946. struct drm_i915_private *dev_priv = to_i915(dev);
  4947. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4948. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4949. }
  4950. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4951. struct drm_atomic_state *old_state)
  4952. {
  4953. struct intel_atomic_state *old_intel_state =
  4954. to_intel_atomic_state(old_state);
  4955. struct drm_crtc *crtc = pipe_config->base.crtc;
  4956. struct drm_device *dev = crtc->dev;
  4957. struct drm_i915_private *dev_priv = to_i915(dev);
  4958. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4959. enum pipe pipe = intel_crtc->pipe;
  4960. if (WARN_ON(intel_crtc->active))
  4961. return;
  4962. i9xx_set_pll_dividers(intel_crtc);
  4963. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4964. intel_dp_set_m_n(intel_crtc, M1_N1);
  4965. intel_set_pipe_timings(intel_crtc);
  4966. intel_set_pipe_src_size(intel_crtc);
  4967. i9xx_set_pipeconf(intel_crtc);
  4968. intel_crtc->active = true;
  4969. if (!IS_GEN2(dev_priv))
  4970. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4971. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4972. i9xx_enable_pll(intel_crtc, pipe_config);
  4973. i9xx_pfit_enable(intel_crtc);
  4974. intel_color_load_luts(&pipe_config->base);
  4975. if (dev_priv->display.initial_watermarks != NULL)
  4976. dev_priv->display.initial_watermarks(old_intel_state,
  4977. intel_crtc->config);
  4978. else
  4979. intel_update_watermarks(intel_crtc);
  4980. intel_enable_pipe(pipe_config);
  4981. assert_vblank_disabled(crtc);
  4982. drm_crtc_vblank_on(crtc);
  4983. intel_encoders_enable(crtc, pipe_config, old_state);
  4984. }
  4985. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4986. {
  4987. struct drm_device *dev = crtc->base.dev;
  4988. struct drm_i915_private *dev_priv = to_i915(dev);
  4989. if (!crtc->config->gmch_pfit.control)
  4990. return;
  4991. assert_pipe_disabled(dev_priv, crtc->pipe);
  4992. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4993. I915_READ(PFIT_CONTROL));
  4994. I915_WRITE(PFIT_CONTROL, 0);
  4995. }
  4996. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4997. struct drm_atomic_state *old_state)
  4998. {
  4999. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5000. struct drm_device *dev = crtc->dev;
  5001. struct drm_i915_private *dev_priv = to_i915(dev);
  5002. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5003. int pipe = intel_crtc->pipe;
  5004. /*
  5005. * On gen2 planes are double buffered but the pipe isn't, so we must
  5006. * wait for planes to fully turn off before disabling the pipe.
  5007. */
  5008. if (IS_GEN2(dev_priv))
  5009. intel_wait_for_vblank(dev_priv, pipe);
  5010. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5011. drm_crtc_vblank_off(crtc);
  5012. assert_vblank_disabled(crtc);
  5013. intel_disable_pipe(old_crtc_state);
  5014. i9xx_pfit_disable(intel_crtc);
  5015. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5016. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5017. if (IS_CHERRYVIEW(dev_priv))
  5018. chv_disable_pll(dev_priv, pipe);
  5019. else if (IS_VALLEYVIEW(dev_priv))
  5020. vlv_disable_pll(dev_priv, pipe);
  5021. else
  5022. i9xx_disable_pll(intel_crtc);
  5023. }
  5024. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5025. if (!IS_GEN2(dev_priv))
  5026. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5027. if (!dev_priv->display.initial_watermarks)
  5028. intel_update_watermarks(intel_crtc);
  5029. /* clock the pipe down to 640x480@60 to potentially save power */
  5030. if (IS_I830(dev_priv))
  5031. i830_enable_pipe(dev_priv, pipe);
  5032. }
  5033. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5034. struct drm_modeset_acquire_ctx *ctx)
  5035. {
  5036. struct intel_encoder *encoder;
  5037. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5038. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5039. enum intel_display_power_domain domain;
  5040. struct intel_plane *plane;
  5041. u64 domains;
  5042. struct drm_atomic_state *state;
  5043. struct intel_crtc_state *crtc_state;
  5044. int ret;
  5045. if (!intel_crtc->active)
  5046. return;
  5047. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5048. const struct intel_plane_state *plane_state =
  5049. to_intel_plane_state(plane->base.state);
  5050. if (plane_state->base.visible)
  5051. intel_plane_disable_noatomic(intel_crtc, plane);
  5052. }
  5053. state = drm_atomic_state_alloc(crtc->dev);
  5054. if (!state) {
  5055. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5056. crtc->base.id, crtc->name);
  5057. return;
  5058. }
  5059. state->acquire_ctx = ctx;
  5060. /* Everything's already locked, -EDEADLK can't happen. */
  5061. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5062. ret = drm_atomic_add_affected_connectors(state, crtc);
  5063. WARN_ON(IS_ERR(crtc_state) || ret);
  5064. dev_priv->display.crtc_disable(crtc_state, state);
  5065. drm_atomic_state_put(state);
  5066. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5067. crtc->base.id, crtc->name);
  5068. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5069. crtc->state->active = false;
  5070. intel_crtc->active = false;
  5071. crtc->enabled = false;
  5072. crtc->state->connector_mask = 0;
  5073. crtc->state->encoder_mask = 0;
  5074. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5075. encoder->base.crtc = NULL;
  5076. intel_fbc_disable(intel_crtc);
  5077. intel_update_watermarks(intel_crtc);
  5078. intel_disable_shared_dpll(intel_crtc);
  5079. domains = intel_crtc->enabled_power_domains;
  5080. for_each_power_domain(domain, domains)
  5081. intel_display_power_put(dev_priv, domain);
  5082. intel_crtc->enabled_power_domains = 0;
  5083. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5084. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5085. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5086. }
  5087. /*
  5088. * turn all crtc's off, but do not adjust state
  5089. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5090. */
  5091. int intel_display_suspend(struct drm_device *dev)
  5092. {
  5093. struct drm_i915_private *dev_priv = to_i915(dev);
  5094. struct drm_atomic_state *state;
  5095. int ret;
  5096. state = drm_atomic_helper_suspend(dev);
  5097. ret = PTR_ERR_OR_ZERO(state);
  5098. if (ret)
  5099. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5100. else
  5101. dev_priv->modeset_restore_state = state;
  5102. return ret;
  5103. }
  5104. void intel_encoder_destroy(struct drm_encoder *encoder)
  5105. {
  5106. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5107. drm_encoder_cleanup(encoder);
  5108. kfree(intel_encoder);
  5109. }
  5110. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5111. * internal consistency). */
  5112. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5113. struct drm_connector_state *conn_state)
  5114. {
  5115. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5116. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5117. connector->base.base.id,
  5118. connector->base.name);
  5119. if (connector->get_hw_state(connector)) {
  5120. struct intel_encoder *encoder = connector->encoder;
  5121. I915_STATE_WARN(!crtc_state,
  5122. "connector enabled without attached crtc\n");
  5123. if (!crtc_state)
  5124. return;
  5125. I915_STATE_WARN(!crtc_state->active,
  5126. "connector is active, but attached crtc isn't\n");
  5127. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5128. return;
  5129. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5130. "atomic encoder doesn't match attached encoder\n");
  5131. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5132. "attached encoder crtc differs from connector crtc\n");
  5133. } else {
  5134. I915_STATE_WARN(crtc_state && crtc_state->active,
  5135. "attached crtc is active, but connector isn't\n");
  5136. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5137. "best encoder set without crtc!\n");
  5138. }
  5139. }
  5140. int intel_connector_init(struct intel_connector *connector)
  5141. {
  5142. struct intel_digital_connector_state *conn_state;
  5143. /*
  5144. * Allocate enough memory to hold intel_digital_connector_state,
  5145. * This might be a few bytes too many, but for connectors that don't
  5146. * need it we'll free the state and allocate a smaller one on the first
  5147. * succesful commit anyway.
  5148. */
  5149. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5150. if (!conn_state)
  5151. return -ENOMEM;
  5152. __drm_atomic_helper_connector_reset(&connector->base,
  5153. &conn_state->base);
  5154. return 0;
  5155. }
  5156. struct intel_connector *intel_connector_alloc(void)
  5157. {
  5158. struct intel_connector *connector;
  5159. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5160. if (!connector)
  5161. return NULL;
  5162. if (intel_connector_init(connector) < 0) {
  5163. kfree(connector);
  5164. return NULL;
  5165. }
  5166. return connector;
  5167. }
  5168. /*
  5169. * Free the bits allocated by intel_connector_alloc.
  5170. * This should only be used after intel_connector_alloc has returned
  5171. * successfully, and before drm_connector_init returns successfully.
  5172. * Otherwise the destroy callbacks for the connector and the state should
  5173. * take care of proper cleanup/free
  5174. */
  5175. void intel_connector_free(struct intel_connector *connector)
  5176. {
  5177. kfree(to_intel_digital_connector_state(connector->base.state));
  5178. kfree(connector);
  5179. }
  5180. /* Simple connector->get_hw_state implementation for encoders that support only
  5181. * one connector and no cloning and hence the encoder state determines the state
  5182. * of the connector. */
  5183. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5184. {
  5185. enum pipe pipe = 0;
  5186. struct intel_encoder *encoder = connector->encoder;
  5187. return encoder->get_hw_state(encoder, &pipe);
  5188. }
  5189. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5190. {
  5191. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5192. return crtc_state->fdi_lanes;
  5193. return 0;
  5194. }
  5195. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5196. struct intel_crtc_state *pipe_config)
  5197. {
  5198. struct drm_i915_private *dev_priv = to_i915(dev);
  5199. struct drm_atomic_state *state = pipe_config->base.state;
  5200. struct intel_crtc *other_crtc;
  5201. struct intel_crtc_state *other_crtc_state;
  5202. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5203. pipe_name(pipe), pipe_config->fdi_lanes);
  5204. if (pipe_config->fdi_lanes > 4) {
  5205. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5206. pipe_name(pipe), pipe_config->fdi_lanes);
  5207. return -EINVAL;
  5208. }
  5209. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5210. if (pipe_config->fdi_lanes > 2) {
  5211. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5212. pipe_config->fdi_lanes);
  5213. return -EINVAL;
  5214. } else {
  5215. return 0;
  5216. }
  5217. }
  5218. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5219. return 0;
  5220. /* Ivybridge 3 pipe is really complicated */
  5221. switch (pipe) {
  5222. case PIPE_A:
  5223. return 0;
  5224. case PIPE_B:
  5225. if (pipe_config->fdi_lanes <= 2)
  5226. return 0;
  5227. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5228. other_crtc_state =
  5229. intel_atomic_get_crtc_state(state, other_crtc);
  5230. if (IS_ERR(other_crtc_state))
  5231. return PTR_ERR(other_crtc_state);
  5232. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5233. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5234. pipe_name(pipe), pipe_config->fdi_lanes);
  5235. return -EINVAL;
  5236. }
  5237. return 0;
  5238. case PIPE_C:
  5239. if (pipe_config->fdi_lanes > 2) {
  5240. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5241. pipe_name(pipe), pipe_config->fdi_lanes);
  5242. return -EINVAL;
  5243. }
  5244. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5245. other_crtc_state =
  5246. intel_atomic_get_crtc_state(state, other_crtc);
  5247. if (IS_ERR(other_crtc_state))
  5248. return PTR_ERR(other_crtc_state);
  5249. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5250. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5251. return -EINVAL;
  5252. }
  5253. return 0;
  5254. default:
  5255. BUG();
  5256. }
  5257. }
  5258. #define RETRY 1
  5259. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5260. struct intel_crtc_state *pipe_config)
  5261. {
  5262. struct drm_device *dev = intel_crtc->base.dev;
  5263. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5264. int lane, link_bw, fdi_dotclock, ret;
  5265. bool needs_recompute = false;
  5266. retry:
  5267. /* FDI is a binary signal running at ~2.7GHz, encoding
  5268. * each output octet as 10 bits. The actual frequency
  5269. * is stored as a divider into a 100MHz clock, and the
  5270. * mode pixel clock is stored in units of 1KHz.
  5271. * Hence the bw of each lane in terms of the mode signal
  5272. * is:
  5273. */
  5274. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5275. fdi_dotclock = adjusted_mode->crtc_clock;
  5276. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5277. pipe_config->pipe_bpp);
  5278. pipe_config->fdi_lanes = lane;
  5279. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5280. link_bw, &pipe_config->fdi_m_n, false);
  5281. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5282. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5283. pipe_config->pipe_bpp -= 2*3;
  5284. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5285. pipe_config->pipe_bpp);
  5286. needs_recompute = true;
  5287. pipe_config->bw_constrained = true;
  5288. goto retry;
  5289. }
  5290. if (needs_recompute)
  5291. return RETRY;
  5292. return ret;
  5293. }
  5294. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5295. {
  5296. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5297. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5298. /* IPS only exists on ULT machines and is tied to pipe A. */
  5299. if (!hsw_crtc_supports_ips(crtc))
  5300. return false;
  5301. if (!i915_modparams.enable_ips)
  5302. return false;
  5303. if (crtc_state->pipe_bpp > 24)
  5304. return false;
  5305. /*
  5306. * We compare against max which means we must take
  5307. * the increased cdclk requirement into account when
  5308. * calculating the new cdclk.
  5309. *
  5310. * Should measure whether using a lower cdclk w/o IPS
  5311. */
  5312. if (IS_BROADWELL(dev_priv) &&
  5313. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5314. return false;
  5315. return true;
  5316. }
  5317. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5318. {
  5319. struct drm_i915_private *dev_priv =
  5320. to_i915(crtc_state->base.crtc->dev);
  5321. struct intel_atomic_state *intel_state =
  5322. to_intel_atomic_state(crtc_state->base.state);
  5323. if (!hsw_crtc_state_ips_capable(crtc_state))
  5324. return false;
  5325. if (crtc_state->ips_force_disable)
  5326. return false;
  5327. /* IPS should be fine as long as at least one plane is enabled. */
  5328. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5329. return false;
  5330. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5331. if (IS_BROADWELL(dev_priv) &&
  5332. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5333. return false;
  5334. return true;
  5335. }
  5336. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5337. {
  5338. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5339. /* GDG double wide on either pipe, otherwise pipe A only */
  5340. return INTEL_GEN(dev_priv) < 4 &&
  5341. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5342. }
  5343. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5344. {
  5345. uint32_t pixel_rate;
  5346. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5347. /*
  5348. * We only use IF-ID interlacing. If we ever use
  5349. * PF-ID we'll need to adjust the pixel_rate here.
  5350. */
  5351. if (pipe_config->pch_pfit.enabled) {
  5352. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5353. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5354. pipe_w = pipe_config->pipe_src_w;
  5355. pipe_h = pipe_config->pipe_src_h;
  5356. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5357. pfit_h = pfit_size & 0xFFFF;
  5358. if (pipe_w < pfit_w)
  5359. pipe_w = pfit_w;
  5360. if (pipe_h < pfit_h)
  5361. pipe_h = pfit_h;
  5362. if (WARN_ON(!pfit_w || !pfit_h))
  5363. return pixel_rate;
  5364. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5365. pfit_w * pfit_h);
  5366. }
  5367. return pixel_rate;
  5368. }
  5369. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5370. {
  5371. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5372. if (HAS_GMCH_DISPLAY(dev_priv))
  5373. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5374. crtc_state->pixel_rate =
  5375. crtc_state->base.adjusted_mode.crtc_clock;
  5376. else
  5377. crtc_state->pixel_rate =
  5378. ilk_pipe_pixel_rate(crtc_state);
  5379. }
  5380. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5381. struct intel_crtc_state *pipe_config)
  5382. {
  5383. struct drm_device *dev = crtc->base.dev;
  5384. struct drm_i915_private *dev_priv = to_i915(dev);
  5385. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5386. int clock_limit = dev_priv->max_dotclk_freq;
  5387. if (INTEL_GEN(dev_priv) < 4) {
  5388. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5389. /*
  5390. * Enable double wide mode when the dot clock
  5391. * is > 90% of the (display) core speed.
  5392. */
  5393. if (intel_crtc_supports_double_wide(crtc) &&
  5394. adjusted_mode->crtc_clock > clock_limit) {
  5395. clock_limit = dev_priv->max_dotclk_freq;
  5396. pipe_config->double_wide = true;
  5397. }
  5398. }
  5399. if (adjusted_mode->crtc_clock > clock_limit) {
  5400. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5401. adjusted_mode->crtc_clock, clock_limit,
  5402. yesno(pipe_config->double_wide));
  5403. return -EINVAL;
  5404. }
  5405. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5406. /*
  5407. * There is only one pipe CSC unit per pipe, and we need that
  5408. * for output conversion from RGB->YCBCR. So if CTM is already
  5409. * applied we can't support YCBCR420 output.
  5410. */
  5411. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5412. return -EINVAL;
  5413. }
  5414. /*
  5415. * Pipe horizontal size must be even in:
  5416. * - DVO ganged mode
  5417. * - LVDS dual channel mode
  5418. * - Double wide pipe
  5419. */
  5420. if (pipe_config->pipe_src_w & 1) {
  5421. if (pipe_config->double_wide) {
  5422. DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
  5423. return -EINVAL;
  5424. }
  5425. if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5426. intel_is_dual_link_lvds(dev)) {
  5427. DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
  5428. return -EINVAL;
  5429. }
  5430. }
  5431. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5432. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5433. */
  5434. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5435. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5436. return -EINVAL;
  5437. intel_crtc_compute_pixel_rate(pipe_config);
  5438. if (pipe_config->has_pch_encoder)
  5439. return ironlake_fdi_compute_config(crtc, pipe_config);
  5440. return 0;
  5441. }
  5442. static void
  5443. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5444. {
  5445. while (*num > DATA_LINK_M_N_MASK ||
  5446. *den > DATA_LINK_M_N_MASK) {
  5447. *num >>= 1;
  5448. *den >>= 1;
  5449. }
  5450. }
  5451. static void compute_m_n(unsigned int m, unsigned int n,
  5452. uint32_t *ret_m, uint32_t *ret_n,
  5453. bool reduce_m_n)
  5454. {
  5455. /*
  5456. * Reduce M/N as much as possible without loss in precision. Several DP
  5457. * dongles in particular seem to be fussy about too large *link* M/N
  5458. * values. The passed in values are more likely to have the least
  5459. * significant bits zero than M after rounding below, so do this first.
  5460. */
  5461. if (reduce_m_n) {
  5462. while ((m & 1) == 0 && (n & 1) == 0) {
  5463. m >>= 1;
  5464. n >>= 1;
  5465. }
  5466. }
  5467. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5468. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5469. intel_reduce_m_n_ratio(ret_m, ret_n);
  5470. }
  5471. void
  5472. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5473. int pixel_clock, int link_clock,
  5474. struct intel_link_m_n *m_n,
  5475. bool reduce_m_n)
  5476. {
  5477. m_n->tu = 64;
  5478. compute_m_n(bits_per_pixel * pixel_clock,
  5479. link_clock * nlanes * 8,
  5480. &m_n->gmch_m, &m_n->gmch_n,
  5481. reduce_m_n);
  5482. compute_m_n(pixel_clock, link_clock,
  5483. &m_n->link_m, &m_n->link_n,
  5484. reduce_m_n);
  5485. }
  5486. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5487. {
  5488. if (i915_modparams.panel_use_ssc >= 0)
  5489. return i915_modparams.panel_use_ssc != 0;
  5490. return dev_priv->vbt.lvds_use_ssc
  5491. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5492. }
  5493. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5494. {
  5495. return (1 << dpll->n) << 16 | dpll->m2;
  5496. }
  5497. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5498. {
  5499. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5500. }
  5501. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5502. struct intel_crtc_state *crtc_state,
  5503. struct dpll *reduced_clock)
  5504. {
  5505. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5506. u32 fp, fp2 = 0;
  5507. if (IS_PINEVIEW(dev_priv)) {
  5508. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5509. if (reduced_clock)
  5510. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5511. } else {
  5512. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5513. if (reduced_clock)
  5514. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5515. }
  5516. crtc_state->dpll_hw_state.fp0 = fp;
  5517. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5518. reduced_clock) {
  5519. crtc_state->dpll_hw_state.fp1 = fp2;
  5520. } else {
  5521. crtc_state->dpll_hw_state.fp1 = fp;
  5522. }
  5523. }
  5524. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5525. pipe)
  5526. {
  5527. u32 reg_val;
  5528. /*
  5529. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5530. * and set it to a reasonable value instead.
  5531. */
  5532. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5533. reg_val &= 0xffffff00;
  5534. reg_val |= 0x00000030;
  5535. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5536. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5537. reg_val &= 0x00ffffff;
  5538. reg_val |= 0x8c000000;
  5539. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5540. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5541. reg_val &= 0xffffff00;
  5542. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5543. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5544. reg_val &= 0x00ffffff;
  5545. reg_val |= 0xb0000000;
  5546. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5547. }
  5548. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5549. struct intel_link_m_n *m_n)
  5550. {
  5551. struct drm_device *dev = crtc->base.dev;
  5552. struct drm_i915_private *dev_priv = to_i915(dev);
  5553. int pipe = crtc->pipe;
  5554. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5555. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5556. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5557. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5558. }
  5559. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5560. struct intel_link_m_n *m_n,
  5561. struct intel_link_m_n *m2_n2)
  5562. {
  5563. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5564. int pipe = crtc->pipe;
  5565. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5566. if (INTEL_GEN(dev_priv) >= 5) {
  5567. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5568. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5569. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5570. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5571. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5572. * for gen < 8) and if DRRS is supported (to make sure the
  5573. * registers are not unnecessarily accessed).
  5574. */
  5575. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5576. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5577. I915_WRITE(PIPE_DATA_M2(transcoder),
  5578. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5579. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5580. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5581. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5582. }
  5583. } else {
  5584. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5585. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5586. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5587. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5588. }
  5589. }
  5590. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5591. {
  5592. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5593. if (m_n == M1_N1) {
  5594. dp_m_n = &crtc->config->dp_m_n;
  5595. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5596. } else if (m_n == M2_N2) {
  5597. /*
  5598. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5599. * needs to be programmed into M1_N1.
  5600. */
  5601. dp_m_n = &crtc->config->dp_m2_n2;
  5602. } else {
  5603. DRM_ERROR("Unsupported divider value\n");
  5604. return;
  5605. }
  5606. if (crtc->config->has_pch_encoder)
  5607. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5608. else
  5609. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5610. }
  5611. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5612. struct intel_crtc_state *pipe_config)
  5613. {
  5614. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5615. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5616. if (crtc->pipe != PIPE_A)
  5617. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5618. /* DPLL not used with DSI, but still need the rest set up */
  5619. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5620. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5621. DPLL_EXT_BUFFER_ENABLE_VLV;
  5622. pipe_config->dpll_hw_state.dpll_md =
  5623. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5624. }
  5625. static void chv_compute_dpll(struct intel_crtc *crtc,
  5626. struct intel_crtc_state *pipe_config)
  5627. {
  5628. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5629. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5630. if (crtc->pipe != PIPE_A)
  5631. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5632. /* DPLL not used with DSI, but still need the rest set up */
  5633. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5634. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5635. pipe_config->dpll_hw_state.dpll_md =
  5636. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5637. }
  5638. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5639. const struct intel_crtc_state *pipe_config)
  5640. {
  5641. struct drm_device *dev = crtc->base.dev;
  5642. struct drm_i915_private *dev_priv = to_i915(dev);
  5643. enum pipe pipe = crtc->pipe;
  5644. u32 mdiv;
  5645. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5646. u32 coreclk, reg_val;
  5647. /* Enable Refclk */
  5648. I915_WRITE(DPLL(pipe),
  5649. pipe_config->dpll_hw_state.dpll &
  5650. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5651. /* No need to actually set up the DPLL with DSI */
  5652. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5653. return;
  5654. mutex_lock(&dev_priv->sb_lock);
  5655. bestn = pipe_config->dpll.n;
  5656. bestm1 = pipe_config->dpll.m1;
  5657. bestm2 = pipe_config->dpll.m2;
  5658. bestp1 = pipe_config->dpll.p1;
  5659. bestp2 = pipe_config->dpll.p2;
  5660. /* See eDP HDMI DPIO driver vbios notes doc */
  5661. /* PLL B needs special handling */
  5662. if (pipe == PIPE_B)
  5663. vlv_pllb_recal_opamp(dev_priv, pipe);
  5664. /* Set up Tx target for periodic Rcomp update */
  5665. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5666. /* Disable target IRef on PLL */
  5667. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5668. reg_val &= 0x00ffffff;
  5669. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5670. /* Disable fast lock */
  5671. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5672. /* Set idtafcrecal before PLL is enabled */
  5673. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5674. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5675. mdiv |= ((bestn << DPIO_N_SHIFT));
  5676. mdiv |= (1 << DPIO_K_SHIFT);
  5677. /*
  5678. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5679. * but we don't support that).
  5680. * Note: don't use the DAC post divider as it seems unstable.
  5681. */
  5682. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5684. mdiv |= DPIO_ENABLE_CALIBRATION;
  5685. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5686. /* Set HBR and RBR LPF coefficients */
  5687. if (pipe_config->port_clock == 162000 ||
  5688. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5689. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5691. 0x009f0003);
  5692. else
  5693. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5694. 0x00d0000f);
  5695. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5696. /* Use SSC source */
  5697. if (pipe == PIPE_A)
  5698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5699. 0x0df40000);
  5700. else
  5701. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5702. 0x0df70000);
  5703. } else { /* HDMI or VGA */
  5704. /* Use bend source */
  5705. if (pipe == PIPE_A)
  5706. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5707. 0x0df70000);
  5708. else
  5709. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5710. 0x0df40000);
  5711. }
  5712. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5713. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5714. if (intel_crtc_has_dp_encoder(crtc->config))
  5715. coreclk |= 0x01000000;
  5716. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5717. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5718. mutex_unlock(&dev_priv->sb_lock);
  5719. }
  5720. static void chv_prepare_pll(struct intel_crtc *crtc,
  5721. const struct intel_crtc_state *pipe_config)
  5722. {
  5723. struct drm_device *dev = crtc->base.dev;
  5724. struct drm_i915_private *dev_priv = to_i915(dev);
  5725. enum pipe pipe = crtc->pipe;
  5726. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5727. u32 loopfilter, tribuf_calcntr;
  5728. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5729. u32 dpio_val;
  5730. int vco;
  5731. /* Enable Refclk and SSC */
  5732. I915_WRITE(DPLL(pipe),
  5733. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5734. /* No need to actually set up the DPLL with DSI */
  5735. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5736. return;
  5737. bestn = pipe_config->dpll.n;
  5738. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5739. bestm1 = pipe_config->dpll.m1;
  5740. bestm2 = pipe_config->dpll.m2 >> 22;
  5741. bestp1 = pipe_config->dpll.p1;
  5742. bestp2 = pipe_config->dpll.p2;
  5743. vco = pipe_config->dpll.vco;
  5744. dpio_val = 0;
  5745. loopfilter = 0;
  5746. mutex_lock(&dev_priv->sb_lock);
  5747. /* p1 and p2 divider */
  5748. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5749. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5750. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5751. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5752. 1 << DPIO_CHV_K_DIV_SHIFT);
  5753. /* Feedback post-divider - m2 */
  5754. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5755. /* Feedback refclk divider - n and m1 */
  5756. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5757. DPIO_CHV_M1_DIV_BY_2 |
  5758. 1 << DPIO_CHV_N_DIV_SHIFT);
  5759. /* M2 fraction division */
  5760. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5761. /* M2 fraction division enable */
  5762. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5763. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5764. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5765. if (bestm2_frac)
  5766. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5767. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5768. /* Program digital lock detect threshold */
  5769. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5770. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5771. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5772. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5773. if (!bestm2_frac)
  5774. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5775. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5776. /* Loop filter */
  5777. if (vco == 5400000) {
  5778. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5779. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5780. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5781. tribuf_calcntr = 0x9;
  5782. } else if (vco <= 6200000) {
  5783. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5784. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5785. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5786. tribuf_calcntr = 0x9;
  5787. } else if (vco <= 6480000) {
  5788. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5789. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5790. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5791. tribuf_calcntr = 0x8;
  5792. } else {
  5793. /* Not supported. Apply the same limits as in the max case */
  5794. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5795. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5796. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5797. tribuf_calcntr = 0;
  5798. }
  5799. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5800. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5801. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5802. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5803. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5804. /* AFC Recal */
  5805. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5806. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5807. DPIO_AFC_RECAL);
  5808. mutex_unlock(&dev_priv->sb_lock);
  5809. }
  5810. /**
  5811. * vlv_force_pll_on - forcibly enable just the PLL
  5812. * @dev_priv: i915 private structure
  5813. * @pipe: pipe PLL to enable
  5814. * @dpll: PLL configuration
  5815. *
  5816. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5817. * in cases where we need the PLL enabled even when @pipe is not going to
  5818. * be enabled.
  5819. */
  5820. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5821. const struct dpll *dpll)
  5822. {
  5823. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5824. struct intel_crtc_state *pipe_config;
  5825. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5826. if (!pipe_config)
  5827. return -ENOMEM;
  5828. pipe_config->base.crtc = &crtc->base;
  5829. pipe_config->pixel_multiplier = 1;
  5830. pipe_config->dpll = *dpll;
  5831. if (IS_CHERRYVIEW(dev_priv)) {
  5832. chv_compute_dpll(crtc, pipe_config);
  5833. chv_prepare_pll(crtc, pipe_config);
  5834. chv_enable_pll(crtc, pipe_config);
  5835. } else {
  5836. vlv_compute_dpll(crtc, pipe_config);
  5837. vlv_prepare_pll(crtc, pipe_config);
  5838. vlv_enable_pll(crtc, pipe_config);
  5839. }
  5840. kfree(pipe_config);
  5841. return 0;
  5842. }
  5843. /**
  5844. * vlv_force_pll_off - forcibly disable just the PLL
  5845. * @dev_priv: i915 private structure
  5846. * @pipe: pipe PLL to disable
  5847. *
  5848. * Disable the PLL for @pipe. To be used in cases where we need
  5849. * the PLL enabled even when @pipe is not going to be enabled.
  5850. */
  5851. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5852. {
  5853. if (IS_CHERRYVIEW(dev_priv))
  5854. chv_disable_pll(dev_priv, pipe);
  5855. else
  5856. vlv_disable_pll(dev_priv, pipe);
  5857. }
  5858. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5859. struct intel_crtc_state *crtc_state,
  5860. struct dpll *reduced_clock)
  5861. {
  5862. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5863. u32 dpll;
  5864. struct dpll *clock = &crtc_state->dpll;
  5865. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5866. dpll = DPLL_VGA_MODE_DIS;
  5867. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5868. dpll |= DPLLB_MODE_LVDS;
  5869. else
  5870. dpll |= DPLLB_MODE_DAC_SERIAL;
  5871. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5872. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5873. dpll |= (crtc_state->pixel_multiplier - 1)
  5874. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5875. }
  5876. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5877. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5878. dpll |= DPLL_SDVO_HIGH_SPEED;
  5879. if (intel_crtc_has_dp_encoder(crtc_state))
  5880. dpll |= DPLL_SDVO_HIGH_SPEED;
  5881. /* compute bitmask from p1 value */
  5882. if (IS_PINEVIEW(dev_priv))
  5883. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5884. else {
  5885. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5886. if (IS_G4X(dev_priv) && reduced_clock)
  5887. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5888. }
  5889. switch (clock->p2) {
  5890. case 5:
  5891. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5892. break;
  5893. case 7:
  5894. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5895. break;
  5896. case 10:
  5897. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5898. break;
  5899. case 14:
  5900. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5901. break;
  5902. }
  5903. if (INTEL_GEN(dev_priv) >= 4)
  5904. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5905. if (crtc_state->sdvo_tv_clock)
  5906. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5907. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5908. intel_panel_use_ssc(dev_priv))
  5909. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5910. else
  5911. dpll |= PLL_REF_INPUT_DREFCLK;
  5912. dpll |= DPLL_VCO_ENABLE;
  5913. crtc_state->dpll_hw_state.dpll = dpll;
  5914. if (INTEL_GEN(dev_priv) >= 4) {
  5915. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5916. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5917. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5918. }
  5919. }
  5920. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5921. struct intel_crtc_state *crtc_state,
  5922. struct dpll *reduced_clock)
  5923. {
  5924. struct drm_device *dev = crtc->base.dev;
  5925. struct drm_i915_private *dev_priv = to_i915(dev);
  5926. u32 dpll;
  5927. struct dpll *clock = &crtc_state->dpll;
  5928. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5929. dpll = DPLL_VGA_MODE_DIS;
  5930. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5931. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5932. } else {
  5933. if (clock->p1 == 2)
  5934. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5935. else
  5936. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5937. if (clock->p2 == 4)
  5938. dpll |= PLL_P2_DIVIDE_BY_4;
  5939. }
  5940. if (!IS_I830(dev_priv) &&
  5941. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5942. dpll |= DPLL_DVO_2X_MODE;
  5943. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5944. intel_panel_use_ssc(dev_priv))
  5945. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5946. else
  5947. dpll |= PLL_REF_INPUT_DREFCLK;
  5948. dpll |= DPLL_VCO_ENABLE;
  5949. crtc_state->dpll_hw_state.dpll = dpll;
  5950. }
  5951. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5952. {
  5953. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5954. enum pipe pipe = intel_crtc->pipe;
  5955. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5956. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5957. uint32_t crtc_vtotal, crtc_vblank_end;
  5958. int vsyncshift = 0;
  5959. /* We need to be careful not to changed the adjusted mode, for otherwise
  5960. * the hw state checker will get angry at the mismatch. */
  5961. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5962. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5963. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5964. /* the chip adds 2 halflines automatically */
  5965. crtc_vtotal -= 1;
  5966. crtc_vblank_end -= 1;
  5967. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5968. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5969. else
  5970. vsyncshift = adjusted_mode->crtc_hsync_start -
  5971. adjusted_mode->crtc_htotal / 2;
  5972. if (vsyncshift < 0)
  5973. vsyncshift += adjusted_mode->crtc_htotal;
  5974. }
  5975. if (INTEL_GEN(dev_priv) > 3)
  5976. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5977. I915_WRITE(HTOTAL(cpu_transcoder),
  5978. (adjusted_mode->crtc_hdisplay - 1) |
  5979. ((adjusted_mode->crtc_htotal - 1) << 16));
  5980. I915_WRITE(HBLANK(cpu_transcoder),
  5981. (adjusted_mode->crtc_hblank_start - 1) |
  5982. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5983. I915_WRITE(HSYNC(cpu_transcoder),
  5984. (adjusted_mode->crtc_hsync_start - 1) |
  5985. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5986. I915_WRITE(VTOTAL(cpu_transcoder),
  5987. (adjusted_mode->crtc_vdisplay - 1) |
  5988. ((crtc_vtotal - 1) << 16));
  5989. I915_WRITE(VBLANK(cpu_transcoder),
  5990. (adjusted_mode->crtc_vblank_start - 1) |
  5991. ((crtc_vblank_end - 1) << 16));
  5992. I915_WRITE(VSYNC(cpu_transcoder),
  5993. (adjusted_mode->crtc_vsync_start - 1) |
  5994. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5995. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5996. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5997. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5998. * bits. */
  5999. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6000. (pipe == PIPE_B || pipe == PIPE_C))
  6001. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6002. }
  6003. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6004. {
  6005. struct drm_device *dev = intel_crtc->base.dev;
  6006. struct drm_i915_private *dev_priv = to_i915(dev);
  6007. enum pipe pipe = intel_crtc->pipe;
  6008. /* pipesrc controls the size that is scaled from, which should
  6009. * always be the user's requested size.
  6010. */
  6011. I915_WRITE(PIPESRC(pipe),
  6012. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6013. (intel_crtc->config->pipe_src_h - 1));
  6014. }
  6015. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6016. struct intel_crtc_state *pipe_config)
  6017. {
  6018. struct drm_device *dev = crtc->base.dev;
  6019. struct drm_i915_private *dev_priv = to_i915(dev);
  6020. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6021. uint32_t tmp;
  6022. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6023. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6024. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6025. tmp = I915_READ(HBLANK(cpu_transcoder));
  6026. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6027. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6028. tmp = I915_READ(HSYNC(cpu_transcoder));
  6029. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6030. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6031. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6032. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6033. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6034. tmp = I915_READ(VBLANK(cpu_transcoder));
  6035. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6036. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6037. tmp = I915_READ(VSYNC(cpu_transcoder));
  6038. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6039. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6040. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6041. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6042. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6043. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6044. }
  6045. }
  6046. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6047. struct intel_crtc_state *pipe_config)
  6048. {
  6049. struct drm_device *dev = crtc->base.dev;
  6050. struct drm_i915_private *dev_priv = to_i915(dev);
  6051. u32 tmp;
  6052. tmp = I915_READ(PIPESRC(crtc->pipe));
  6053. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6054. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6055. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6056. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6057. }
  6058. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6059. struct intel_crtc_state *pipe_config)
  6060. {
  6061. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6062. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6063. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6064. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6065. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6066. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6067. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6068. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6069. mode->flags = pipe_config->base.adjusted_mode.flags;
  6070. mode->type = DRM_MODE_TYPE_DRIVER;
  6071. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6072. mode->hsync = drm_mode_hsync(mode);
  6073. mode->vrefresh = drm_mode_vrefresh(mode);
  6074. drm_mode_set_name(mode);
  6075. }
  6076. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6077. {
  6078. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6079. uint32_t pipeconf;
  6080. pipeconf = 0;
  6081. /* we keep both pipes enabled on 830 */
  6082. if (IS_I830(dev_priv))
  6083. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6084. if (intel_crtc->config->double_wide)
  6085. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6086. /* only g4x and later have fancy bpc/dither controls */
  6087. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6088. IS_CHERRYVIEW(dev_priv)) {
  6089. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6090. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6091. pipeconf |= PIPECONF_DITHER_EN |
  6092. PIPECONF_DITHER_TYPE_SP;
  6093. switch (intel_crtc->config->pipe_bpp) {
  6094. case 18:
  6095. pipeconf |= PIPECONF_6BPC;
  6096. break;
  6097. case 24:
  6098. pipeconf |= PIPECONF_8BPC;
  6099. break;
  6100. case 30:
  6101. pipeconf |= PIPECONF_10BPC;
  6102. break;
  6103. default:
  6104. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6105. BUG();
  6106. }
  6107. }
  6108. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6109. if (INTEL_GEN(dev_priv) < 4 ||
  6110. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6111. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6112. else
  6113. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6114. } else
  6115. pipeconf |= PIPECONF_PROGRESSIVE;
  6116. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6117. intel_crtc->config->limited_color_range)
  6118. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6119. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6120. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6121. }
  6122. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6123. struct intel_crtc_state *crtc_state)
  6124. {
  6125. struct drm_device *dev = crtc->base.dev;
  6126. struct drm_i915_private *dev_priv = to_i915(dev);
  6127. const struct intel_limit *limit;
  6128. int refclk = 48000;
  6129. memset(&crtc_state->dpll_hw_state, 0,
  6130. sizeof(crtc_state->dpll_hw_state));
  6131. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6132. if (intel_panel_use_ssc(dev_priv)) {
  6133. refclk = dev_priv->vbt.lvds_ssc_freq;
  6134. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6135. }
  6136. limit = &intel_limits_i8xx_lvds;
  6137. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6138. limit = &intel_limits_i8xx_dvo;
  6139. } else {
  6140. limit = &intel_limits_i8xx_dac;
  6141. }
  6142. if (!crtc_state->clock_set &&
  6143. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6144. refclk, NULL, &crtc_state->dpll)) {
  6145. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6146. return -EINVAL;
  6147. }
  6148. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6149. return 0;
  6150. }
  6151. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6152. struct intel_crtc_state *crtc_state)
  6153. {
  6154. struct drm_device *dev = crtc->base.dev;
  6155. struct drm_i915_private *dev_priv = to_i915(dev);
  6156. const struct intel_limit *limit;
  6157. int refclk = 96000;
  6158. memset(&crtc_state->dpll_hw_state, 0,
  6159. sizeof(crtc_state->dpll_hw_state));
  6160. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6161. if (intel_panel_use_ssc(dev_priv)) {
  6162. refclk = dev_priv->vbt.lvds_ssc_freq;
  6163. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6164. }
  6165. if (intel_is_dual_link_lvds(dev))
  6166. limit = &intel_limits_g4x_dual_channel_lvds;
  6167. else
  6168. limit = &intel_limits_g4x_single_channel_lvds;
  6169. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6170. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6171. limit = &intel_limits_g4x_hdmi;
  6172. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6173. limit = &intel_limits_g4x_sdvo;
  6174. } else {
  6175. /* The option is for other outputs */
  6176. limit = &intel_limits_i9xx_sdvo;
  6177. }
  6178. if (!crtc_state->clock_set &&
  6179. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6180. refclk, NULL, &crtc_state->dpll)) {
  6181. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6182. return -EINVAL;
  6183. }
  6184. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6185. return 0;
  6186. }
  6187. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6188. struct intel_crtc_state *crtc_state)
  6189. {
  6190. struct drm_device *dev = crtc->base.dev;
  6191. struct drm_i915_private *dev_priv = to_i915(dev);
  6192. const struct intel_limit *limit;
  6193. int refclk = 96000;
  6194. memset(&crtc_state->dpll_hw_state, 0,
  6195. sizeof(crtc_state->dpll_hw_state));
  6196. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6197. if (intel_panel_use_ssc(dev_priv)) {
  6198. refclk = dev_priv->vbt.lvds_ssc_freq;
  6199. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6200. }
  6201. limit = &intel_limits_pineview_lvds;
  6202. } else {
  6203. limit = &intel_limits_pineview_sdvo;
  6204. }
  6205. if (!crtc_state->clock_set &&
  6206. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6207. refclk, NULL, &crtc_state->dpll)) {
  6208. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6209. return -EINVAL;
  6210. }
  6211. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6212. return 0;
  6213. }
  6214. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6215. struct intel_crtc_state *crtc_state)
  6216. {
  6217. struct drm_device *dev = crtc->base.dev;
  6218. struct drm_i915_private *dev_priv = to_i915(dev);
  6219. const struct intel_limit *limit;
  6220. int refclk = 96000;
  6221. memset(&crtc_state->dpll_hw_state, 0,
  6222. sizeof(crtc_state->dpll_hw_state));
  6223. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6224. if (intel_panel_use_ssc(dev_priv)) {
  6225. refclk = dev_priv->vbt.lvds_ssc_freq;
  6226. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6227. }
  6228. limit = &intel_limits_i9xx_lvds;
  6229. } else {
  6230. limit = &intel_limits_i9xx_sdvo;
  6231. }
  6232. if (!crtc_state->clock_set &&
  6233. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6234. refclk, NULL, &crtc_state->dpll)) {
  6235. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6236. return -EINVAL;
  6237. }
  6238. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6239. return 0;
  6240. }
  6241. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6242. struct intel_crtc_state *crtc_state)
  6243. {
  6244. int refclk = 100000;
  6245. const struct intel_limit *limit = &intel_limits_chv;
  6246. memset(&crtc_state->dpll_hw_state, 0,
  6247. sizeof(crtc_state->dpll_hw_state));
  6248. if (!crtc_state->clock_set &&
  6249. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6250. refclk, NULL, &crtc_state->dpll)) {
  6251. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6252. return -EINVAL;
  6253. }
  6254. chv_compute_dpll(crtc, crtc_state);
  6255. return 0;
  6256. }
  6257. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6258. struct intel_crtc_state *crtc_state)
  6259. {
  6260. int refclk = 100000;
  6261. const struct intel_limit *limit = &intel_limits_vlv;
  6262. memset(&crtc_state->dpll_hw_state, 0,
  6263. sizeof(crtc_state->dpll_hw_state));
  6264. if (!crtc_state->clock_set &&
  6265. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6266. refclk, NULL, &crtc_state->dpll)) {
  6267. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6268. return -EINVAL;
  6269. }
  6270. vlv_compute_dpll(crtc, crtc_state);
  6271. return 0;
  6272. }
  6273. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6274. struct intel_crtc_state *pipe_config)
  6275. {
  6276. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6277. uint32_t tmp;
  6278. if (INTEL_GEN(dev_priv) <= 3 &&
  6279. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6280. return;
  6281. tmp = I915_READ(PFIT_CONTROL);
  6282. if (!(tmp & PFIT_ENABLE))
  6283. return;
  6284. /* Check whether the pfit is attached to our pipe. */
  6285. if (INTEL_GEN(dev_priv) < 4) {
  6286. if (crtc->pipe != PIPE_B)
  6287. return;
  6288. } else {
  6289. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6290. return;
  6291. }
  6292. pipe_config->gmch_pfit.control = tmp;
  6293. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6294. }
  6295. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6296. struct intel_crtc_state *pipe_config)
  6297. {
  6298. struct drm_device *dev = crtc->base.dev;
  6299. struct drm_i915_private *dev_priv = to_i915(dev);
  6300. int pipe = pipe_config->cpu_transcoder;
  6301. struct dpll clock;
  6302. u32 mdiv;
  6303. int refclk = 100000;
  6304. /* In case of DSI, DPLL will not be used */
  6305. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6306. return;
  6307. mutex_lock(&dev_priv->sb_lock);
  6308. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6309. mutex_unlock(&dev_priv->sb_lock);
  6310. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6311. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6312. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6313. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6314. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6315. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6316. }
  6317. static void
  6318. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6319. struct intel_initial_plane_config *plane_config)
  6320. {
  6321. struct drm_device *dev = crtc->base.dev;
  6322. struct drm_i915_private *dev_priv = to_i915(dev);
  6323. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6324. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6325. enum pipe pipe = crtc->pipe;
  6326. u32 val, base, offset;
  6327. int fourcc, pixel_format;
  6328. unsigned int aligned_height;
  6329. struct drm_framebuffer *fb;
  6330. struct intel_framebuffer *intel_fb;
  6331. if (!plane->get_hw_state(plane))
  6332. return;
  6333. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6334. if (!intel_fb) {
  6335. DRM_DEBUG_KMS("failed to alloc fb\n");
  6336. return;
  6337. }
  6338. fb = &intel_fb->base;
  6339. fb->dev = dev;
  6340. val = I915_READ(DSPCNTR(i9xx_plane));
  6341. if (INTEL_GEN(dev_priv) >= 4) {
  6342. if (val & DISPPLANE_TILED) {
  6343. plane_config->tiling = I915_TILING_X;
  6344. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6345. }
  6346. }
  6347. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6348. fourcc = i9xx_format_to_fourcc(pixel_format);
  6349. fb->format = drm_format_info(fourcc);
  6350. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6351. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6352. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6353. } else if (INTEL_GEN(dev_priv) >= 4) {
  6354. if (plane_config->tiling)
  6355. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6356. else
  6357. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6358. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6359. } else {
  6360. base = I915_READ(DSPADDR(i9xx_plane));
  6361. }
  6362. plane_config->base = base;
  6363. val = I915_READ(PIPESRC(pipe));
  6364. fb->width = ((val >> 16) & 0xfff) + 1;
  6365. fb->height = ((val >> 0) & 0xfff) + 1;
  6366. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6367. fb->pitches[0] = val & 0xffffffc0;
  6368. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6369. plane_config->size = fb->pitches[0] * aligned_height;
  6370. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6371. crtc->base.name, plane->base.name, fb->width, fb->height,
  6372. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6373. plane_config->size);
  6374. plane_config->fb = intel_fb;
  6375. }
  6376. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6377. struct intel_crtc_state *pipe_config)
  6378. {
  6379. struct drm_device *dev = crtc->base.dev;
  6380. struct drm_i915_private *dev_priv = to_i915(dev);
  6381. int pipe = pipe_config->cpu_transcoder;
  6382. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6383. struct dpll clock;
  6384. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6385. int refclk = 100000;
  6386. /* In case of DSI, DPLL will not be used */
  6387. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6388. return;
  6389. mutex_lock(&dev_priv->sb_lock);
  6390. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6391. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6392. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6393. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6394. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6395. mutex_unlock(&dev_priv->sb_lock);
  6396. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6397. clock.m2 = (pll_dw0 & 0xff) << 22;
  6398. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6399. clock.m2 |= pll_dw2 & 0x3fffff;
  6400. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6401. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6402. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6403. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6404. }
  6405. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6406. struct intel_crtc_state *pipe_config)
  6407. {
  6408. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6409. enum intel_display_power_domain power_domain;
  6410. uint32_t tmp;
  6411. bool ret;
  6412. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6413. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6414. return false;
  6415. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6416. pipe_config->shared_dpll = NULL;
  6417. ret = false;
  6418. tmp = I915_READ(PIPECONF(crtc->pipe));
  6419. if (!(tmp & PIPECONF_ENABLE))
  6420. goto out;
  6421. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6422. IS_CHERRYVIEW(dev_priv)) {
  6423. switch (tmp & PIPECONF_BPC_MASK) {
  6424. case PIPECONF_6BPC:
  6425. pipe_config->pipe_bpp = 18;
  6426. break;
  6427. case PIPECONF_8BPC:
  6428. pipe_config->pipe_bpp = 24;
  6429. break;
  6430. case PIPECONF_10BPC:
  6431. pipe_config->pipe_bpp = 30;
  6432. break;
  6433. default:
  6434. break;
  6435. }
  6436. }
  6437. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6438. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6439. pipe_config->limited_color_range = true;
  6440. if (INTEL_GEN(dev_priv) < 4)
  6441. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6442. intel_get_pipe_timings(crtc, pipe_config);
  6443. intel_get_pipe_src_size(crtc, pipe_config);
  6444. i9xx_get_pfit_config(crtc, pipe_config);
  6445. if (INTEL_GEN(dev_priv) >= 4) {
  6446. /* No way to read it out on pipes B and C */
  6447. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6448. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6449. else
  6450. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6451. pipe_config->pixel_multiplier =
  6452. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6453. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6454. pipe_config->dpll_hw_state.dpll_md = tmp;
  6455. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6456. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6457. tmp = I915_READ(DPLL(crtc->pipe));
  6458. pipe_config->pixel_multiplier =
  6459. ((tmp & SDVO_MULTIPLIER_MASK)
  6460. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6461. } else {
  6462. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6463. * port and will be fixed up in the encoder->get_config
  6464. * function. */
  6465. pipe_config->pixel_multiplier = 1;
  6466. }
  6467. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6468. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6469. /*
  6470. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6471. * on 830. Filter it out here so that we don't
  6472. * report errors due to that.
  6473. */
  6474. if (IS_I830(dev_priv))
  6475. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6476. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6477. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6478. } else {
  6479. /* Mask out read-only status bits. */
  6480. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6481. DPLL_PORTC_READY_MASK |
  6482. DPLL_PORTB_READY_MASK);
  6483. }
  6484. if (IS_CHERRYVIEW(dev_priv))
  6485. chv_crtc_clock_get(crtc, pipe_config);
  6486. else if (IS_VALLEYVIEW(dev_priv))
  6487. vlv_crtc_clock_get(crtc, pipe_config);
  6488. else
  6489. i9xx_crtc_clock_get(crtc, pipe_config);
  6490. /*
  6491. * Normally the dotclock is filled in by the encoder .get_config()
  6492. * but in case the pipe is enabled w/o any ports we need a sane
  6493. * default.
  6494. */
  6495. pipe_config->base.adjusted_mode.crtc_clock =
  6496. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6497. ret = true;
  6498. out:
  6499. intel_display_power_put(dev_priv, power_domain);
  6500. return ret;
  6501. }
  6502. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6503. {
  6504. struct intel_encoder *encoder;
  6505. int i;
  6506. u32 val, final;
  6507. bool has_lvds = false;
  6508. bool has_cpu_edp = false;
  6509. bool has_panel = false;
  6510. bool has_ck505 = false;
  6511. bool can_ssc = false;
  6512. bool using_ssc_source = false;
  6513. /* We need to take the global config into account */
  6514. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6515. switch (encoder->type) {
  6516. case INTEL_OUTPUT_LVDS:
  6517. has_panel = true;
  6518. has_lvds = true;
  6519. break;
  6520. case INTEL_OUTPUT_EDP:
  6521. has_panel = true;
  6522. if (encoder->port == PORT_A)
  6523. has_cpu_edp = true;
  6524. break;
  6525. default:
  6526. break;
  6527. }
  6528. }
  6529. if (HAS_PCH_IBX(dev_priv)) {
  6530. has_ck505 = dev_priv->vbt.display_clock_mode;
  6531. can_ssc = has_ck505;
  6532. } else {
  6533. has_ck505 = false;
  6534. can_ssc = true;
  6535. }
  6536. /* Check if any DPLLs are using the SSC source */
  6537. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6538. u32 temp = I915_READ(PCH_DPLL(i));
  6539. if (!(temp & DPLL_VCO_ENABLE))
  6540. continue;
  6541. if ((temp & PLL_REF_INPUT_MASK) ==
  6542. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6543. using_ssc_source = true;
  6544. break;
  6545. }
  6546. }
  6547. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6548. has_panel, has_lvds, has_ck505, using_ssc_source);
  6549. /* Ironlake: try to setup display ref clock before DPLL
  6550. * enabling. This is only under driver's control after
  6551. * PCH B stepping, previous chipset stepping should be
  6552. * ignoring this setting.
  6553. */
  6554. val = I915_READ(PCH_DREF_CONTROL);
  6555. /* As we must carefully and slowly disable/enable each source in turn,
  6556. * compute the final state we want first and check if we need to
  6557. * make any changes at all.
  6558. */
  6559. final = val;
  6560. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6561. if (has_ck505)
  6562. final |= DREF_NONSPREAD_CK505_ENABLE;
  6563. else
  6564. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6565. final &= ~DREF_SSC_SOURCE_MASK;
  6566. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6567. final &= ~DREF_SSC1_ENABLE;
  6568. if (has_panel) {
  6569. final |= DREF_SSC_SOURCE_ENABLE;
  6570. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6571. final |= DREF_SSC1_ENABLE;
  6572. if (has_cpu_edp) {
  6573. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6574. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6575. else
  6576. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6577. } else
  6578. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6579. } else if (using_ssc_source) {
  6580. final |= DREF_SSC_SOURCE_ENABLE;
  6581. final |= DREF_SSC1_ENABLE;
  6582. }
  6583. if (final == val)
  6584. return;
  6585. /* Always enable nonspread source */
  6586. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6587. if (has_ck505)
  6588. val |= DREF_NONSPREAD_CK505_ENABLE;
  6589. else
  6590. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6591. if (has_panel) {
  6592. val &= ~DREF_SSC_SOURCE_MASK;
  6593. val |= DREF_SSC_SOURCE_ENABLE;
  6594. /* SSC must be turned on before enabling the CPU output */
  6595. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6596. DRM_DEBUG_KMS("Using SSC on panel\n");
  6597. val |= DREF_SSC1_ENABLE;
  6598. } else
  6599. val &= ~DREF_SSC1_ENABLE;
  6600. /* Get SSC going before enabling the outputs */
  6601. I915_WRITE(PCH_DREF_CONTROL, val);
  6602. POSTING_READ(PCH_DREF_CONTROL);
  6603. udelay(200);
  6604. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6605. /* Enable CPU source on CPU attached eDP */
  6606. if (has_cpu_edp) {
  6607. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6608. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6609. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6610. } else
  6611. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6612. } else
  6613. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6614. I915_WRITE(PCH_DREF_CONTROL, val);
  6615. POSTING_READ(PCH_DREF_CONTROL);
  6616. udelay(200);
  6617. } else {
  6618. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6619. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6620. /* Turn off CPU output */
  6621. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6622. I915_WRITE(PCH_DREF_CONTROL, val);
  6623. POSTING_READ(PCH_DREF_CONTROL);
  6624. udelay(200);
  6625. if (!using_ssc_source) {
  6626. DRM_DEBUG_KMS("Disabling SSC source\n");
  6627. /* Turn off the SSC source */
  6628. val &= ~DREF_SSC_SOURCE_MASK;
  6629. val |= DREF_SSC_SOURCE_DISABLE;
  6630. /* Turn off SSC1 */
  6631. val &= ~DREF_SSC1_ENABLE;
  6632. I915_WRITE(PCH_DREF_CONTROL, val);
  6633. POSTING_READ(PCH_DREF_CONTROL);
  6634. udelay(200);
  6635. }
  6636. }
  6637. BUG_ON(val != final);
  6638. }
  6639. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6640. {
  6641. uint32_t tmp;
  6642. tmp = I915_READ(SOUTH_CHICKEN2);
  6643. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6644. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6645. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6646. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6647. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6648. tmp = I915_READ(SOUTH_CHICKEN2);
  6649. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6650. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6651. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6652. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6653. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6654. }
  6655. /* WaMPhyProgramming:hsw */
  6656. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6657. {
  6658. uint32_t tmp;
  6659. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6660. tmp &= ~(0xFF << 24);
  6661. tmp |= (0x12 << 24);
  6662. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6663. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6664. tmp |= (1 << 11);
  6665. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6666. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6667. tmp |= (1 << 11);
  6668. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6669. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6670. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6671. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6672. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6673. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6674. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6675. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6676. tmp &= ~(7 << 13);
  6677. tmp |= (5 << 13);
  6678. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6679. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6680. tmp &= ~(7 << 13);
  6681. tmp |= (5 << 13);
  6682. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6683. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6684. tmp &= ~0xFF;
  6685. tmp |= 0x1C;
  6686. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6687. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6688. tmp &= ~0xFF;
  6689. tmp |= 0x1C;
  6690. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6691. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6692. tmp &= ~(0xFF << 16);
  6693. tmp |= (0x1C << 16);
  6694. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6695. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6696. tmp &= ~(0xFF << 16);
  6697. tmp |= (0x1C << 16);
  6698. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6699. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6700. tmp |= (1 << 27);
  6701. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6702. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6703. tmp |= (1 << 27);
  6704. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6705. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6706. tmp &= ~(0xF << 28);
  6707. tmp |= (4 << 28);
  6708. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6709. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6710. tmp &= ~(0xF << 28);
  6711. tmp |= (4 << 28);
  6712. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6713. }
  6714. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6715. * Programming" based on the parameters passed:
  6716. * - Sequence to enable CLKOUT_DP
  6717. * - Sequence to enable CLKOUT_DP without spread
  6718. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6719. */
  6720. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6721. bool with_spread, bool with_fdi)
  6722. {
  6723. uint32_t reg, tmp;
  6724. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6725. with_spread = true;
  6726. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6727. with_fdi, "LP PCH doesn't have FDI\n"))
  6728. with_fdi = false;
  6729. mutex_lock(&dev_priv->sb_lock);
  6730. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6731. tmp &= ~SBI_SSCCTL_DISABLE;
  6732. tmp |= SBI_SSCCTL_PATHALT;
  6733. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6734. udelay(24);
  6735. if (with_spread) {
  6736. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6737. tmp &= ~SBI_SSCCTL_PATHALT;
  6738. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6739. if (with_fdi) {
  6740. lpt_reset_fdi_mphy(dev_priv);
  6741. lpt_program_fdi_mphy(dev_priv);
  6742. }
  6743. }
  6744. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6745. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6746. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6747. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6748. mutex_unlock(&dev_priv->sb_lock);
  6749. }
  6750. /* Sequence to disable CLKOUT_DP */
  6751. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6752. {
  6753. uint32_t reg, tmp;
  6754. mutex_lock(&dev_priv->sb_lock);
  6755. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6756. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6757. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6758. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6759. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6760. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6761. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6762. tmp |= SBI_SSCCTL_PATHALT;
  6763. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6764. udelay(32);
  6765. }
  6766. tmp |= SBI_SSCCTL_DISABLE;
  6767. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6768. }
  6769. mutex_unlock(&dev_priv->sb_lock);
  6770. }
  6771. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6772. static const uint16_t sscdivintphase[] = {
  6773. [BEND_IDX( 50)] = 0x3B23,
  6774. [BEND_IDX( 45)] = 0x3B23,
  6775. [BEND_IDX( 40)] = 0x3C23,
  6776. [BEND_IDX( 35)] = 0x3C23,
  6777. [BEND_IDX( 30)] = 0x3D23,
  6778. [BEND_IDX( 25)] = 0x3D23,
  6779. [BEND_IDX( 20)] = 0x3E23,
  6780. [BEND_IDX( 15)] = 0x3E23,
  6781. [BEND_IDX( 10)] = 0x3F23,
  6782. [BEND_IDX( 5)] = 0x3F23,
  6783. [BEND_IDX( 0)] = 0x0025,
  6784. [BEND_IDX( -5)] = 0x0025,
  6785. [BEND_IDX(-10)] = 0x0125,
  6786. [BEND_IDX(-15)] = 0x0125,
  6787. [BEND_IDX(-20)] = 0x0225,
  6788. [BEND_IDX(-25)] = 0x0225,
  6789. [BEND_IDX(-30)] = 0x0325,
  6790. [BEND_IDX(-35)] = 0x0325,
  6791. [BEND_IDX(-40)] = 0x0425,
  6792. [BEND_IDX(-45)] = 0x0425,
  6793. [BEND_IDX(-50)] = 0x0525,
  6794. };
  6795. /*
  6796. * Bend CLKOUT_DP
  6797. * steps -50 to 50 inclusive, in steps of 5
  6798. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6799. * change in clock period = -(steps / 10) * 5.787 ps
  6800. */
  6801. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6802. {
  6803. uint32_t tmp;
  6804. int idx = BEND_IDX(steps);
  6805. if (WARN_ON(steps % 5 != 0))
  6806. return;
  6807. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6808. return;
  6809. mutex_lock(&dev_priv->sb_lock);
  6810. if (steps % 10 != 0)
  6811. tmp = 0xAAAAAAAB;
  6812. else
  6813. tmp = 0x00000000;
  6814. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6815. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6816. tmp &= 0xffff0000;
  6817. tmp |= sscdivintphase[idx];
  6818. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6819. mutex_unlock(&dev_priv->sb_lock);
  6820. }
  6821. #undef BEND_IDX
  6822. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6823. {
  6824. struct intel_encoder *encoder;
  6825. bool has_vga = false;
  6826. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6827. switch (encoder->type) {
  6828. case INTEL_OUTPUT_ANALOG:
  6829. has_vga = true;
  6830. break;
  6831. default:
  6832. break;
  6833. }
  6834. }
  6835. if (has_vga) {
  6836. lpt_bend_clkout_dp(dev_priv, 0);
  6837. lpt_enable_clkout_dp(dev_priv, true, true);
  6838. } else {
  6839. lpt_disable_clkout_dp(dev_priv);
  6840. }
  6841. }
  6842. /*
  6843. * Initialize reference clocks when the driver loads
  6844. */
  6845. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6846. {
  6847. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6848. ironlake_init_pch_refclk(dev_priv);
  6849. else if (HAS_PCH_LPT(dev_priv))
  6850. lpt_init_pch_refclk(dev_priv);
  6851. }
  6852. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6853. {
  6854. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6856. int pipe = intel_crtc->pipe;
  6857. uint32_t val;
  6858. val = 0;
  6859. switch (intel_crtc->config->pipe_bpp) {
  6860. case 18:
  6861. val |= PIPECONF_6BPC;
  6862. break;
  6863. case 24:
  6864. val |= PIPECONF_8BPC;
  6865. break;
  6866. case 30:
  6867. val |= PIPECONF_10BPC;
  6868. break;
  6869. case 36:
  6870. val |= PIPECONF_12BPC;
  6871. break;
  6872. default:
  6873. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6874. BUG();
  6875. }
  6876. if (intel_crtc->config->dither)
  6877. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6878. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6879. val |= PIPECONF_INTERLACED_ILK;
  6880. else
  6881. val |= PIPECONF_PROGRESSIVE;
  6882. if (intel_crtc->config->limited_color_range)
  6883. val |= PIPECONF_COLOR_RANGE_SELECT;
  6884. I915_WRITE(PIPECONF(pipe), val);
  6885. POSTING_READ(PIPECONF(pipe));
  6886. }
  6887. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6888. {
  6889. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6891. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6892. u32 val = 0;
  6893. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6894. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6895. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6896. val |= PIPECONF_INTERLACED_ILK;
  6897. else
  6898. val |= PIPECONF_PROGRESSIVE;
  6899. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6900. POSTING_READ(PIPECONF(cpu_transcoder));
  6901. }
  6902. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6903. {
  6904. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6906. struct intel_crtc_state *config = intel_crtc->config;
  6907. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  6908. u32 val = 0;
  6909. switch (intel_crtc->config->pipe_bpp) {
  6910. case 18:
  6911. val |= PIPEMISC_DITHER_6_BPC;
  6912. break;
  6913. case 24:
  6914. val |= PIPEMISC_DITHER_8_BPC;
  6915. break;
  6916. case 30:
  6917. val |= PIPEMISC_DITHER_10_BPC;
  6918. break;
  6919. case 36:
  6920. val |= PIPEMISC_DITHER_12_BPC;
  6921. break;
  6922. default:
  6923. /* Case prevented by pipe_config_set_bpp. */
  6924. BUG();
  6925. }
  6926. if (intel_crtc->config->dither)
  6927. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6928. if (config->ycbcr420) {
  6929. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6930. PIPEMISC_YUV420_ENABLE |
  6931. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6932. }
  6933. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6934. }
  6935. }
  6936. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6937. {
  6938. /*
  6939. * Account for spread spectrum to avoid
  6940. * oversubscribing the link. Max center spread
  6941. * is 2.5%; use 5% for safety's sake.
  6942. */
  6943. u32 bps = target_clock * bpp * 21 / 20;
  6944. return DIV_ROUND_UP(bps, link_bw * 8);
  6945. }
  6946. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6947. {
  6948. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6949. }
  6950. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6951. struct intel_crtc_state *crtc_state,
  6952. struct dpll *reduced_clock)
  6953. {
  6954. struct drm_crtc *crtc = &intel_crtc->base;
  6955. struct drm_device *dev = crtc->dev;
  6956. struct drm_i915_private *dev_priv = to_i915(dev);
  6957. u32 dpll, fp, fp2;
  6958. int factor;
  6959. /* Enable autotuning of the PLL clock (if permissible) */
  6960. factor = 21;
  6961. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6962. if ((intel_panel_use_ssc(dev_priv) &&
  6963. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6964. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6965. factor = 25;
  6966. } else if (crtc_state->sdvo_tv_clock)
  6967. factor = 20;
  6968. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6969. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6970. fp |= FP_CB_TUNE;
  6971. if (reduced_clock) {
  6972. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6973. if (reduced_clock->m < factor * reduced_clock->n)
  6974. fp2 |= FP_CB_TUNE;
  6975. } else {
  6976. fp2 = fp;
  6977. }
  6978. dpll = 0;
  6979. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6980. dpll |= DPLLB_MODE_LVDS;
  6981. else
  6982. dpll |= DPLLB_MODE_DAC_SERIAL;
  6983. dpll |= (crtc_state->pixel_multiplier - 1)
  6984. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6985. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6986. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6987. dpll |= DPLL_SDVO_HIGH_SPEED;
  6988. if (intel_crtc_has_dp_encoder(crtc_state))
  6989. dpll |= DPLL_SDVO_HIGH_SPEED;
  6990. /*
  6991. * The high speed IO clock is only really required for
  6992. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6993. * possible to share the DPLL between CRT and HDMI. Enabling
  6994. * the clock needlessly does no real harm, except use up a
  6995. * bit of power potentially.
  6996. *
  6997. * We'll limit this to IVB with 3 pipes, since it has only two
  6998. * DPLLs and so DPLL sharing is the only way to get three pipes
  6999. * driving PCH ports at the same time. On SNB we could do this,
  7000. * and potentially avoid enabling the second DPLL, but it's not
  7001. * clear if it''s a win or loss power wise. No point in doing
  7002. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7003. */
  7004. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7005. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7006. dpll |= DPLL_SDVO_HIGH_SPEED;
  7007. /* compute bitmask from p1 value */
  7008. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7009. /* also FPA1 */
  7010. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7011. switch (crtc_state->dpll.p2) {
  7012. case 5:
  7013. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7014. break;
  7015. case 7:
  7016. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7017. break;
  7018. case 10:
  7019. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7020. break;
  7021. case 14:
  7022. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7023. break;
  7024. }
  7025. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7026. intel_panel_use_ssc(dev_priv))
  7027. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7028. else
  7029. dpll |= PLL_REF_INPUT_DREFCLK;
  7030. dpll |= DPLL_VCO_ENABLE;
  7031. crtc_state->dpll_hw_state.dpll = dpll;
  7032. crtc_state->dpll_hw_state.fp0 = fp;
  7033. crtc_state->dpll_hw_state.fp1 = fp2;
  7034. }
  7035. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7036. struct intel_crtc_state *crtc_state)
  7037. {
  7038. struct drm_device *dev = crtc->base.dev;
  7039. struct drm_i915_private *dev_priv = to_i915(dev);
  7040. const struct intel_limit *limit;
  7041. int refclk = 120000;
  7042. memset(&crtc_state->dpll_hw_state, 0,
  7043. sizeof(crtc_state->dpll_hw_state));
  7044. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7045. if (!crtc_state->has_pch_encoder)
  7046. return 0;
  7047. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7048. if (intel_panel_use_ssc(dev_priv)) {
  7049. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7050. dev_priv->vbt.lvds_ssc_freq);
  7051. refclk = dev_priv->vbt.lvds_ssc_freq;
  7052. }
  7053. if (intel_is_dual_link_lvds(dev)) {
  7054. if (refclk == 100000)
  7055. limit = &intel_limits_ironlake_dual_lvds_100m;
  7056. else
  7057. limit = &intel_limits_ironlake_dual_lvds;
  7058. } else {
  7059. if (refclk == 100000)
  7060. limit = &intel_limits_ironlake_single_lvds_100m;
  7061. else
  7062. limit = &intel_limits_ironlake_single_lvds;
  7063. }
  7064. } else {
  7065. limit = &intel_limits_ironlake_dac;
  7066. }
  7067. if (!crtc_state->clock_set &&
  7068. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7069. refclk, NULL, &crtc_state->dpll)) {
  7070. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7071. return -EINVAL;
  7072. }
  7073. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7074. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7075. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7076. pipe_name(crtc->pipe));
  7077. return -EINVAL;
  7078. }
  7079. return 0;
  7080. }
  7081. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7082. struct intel_link_m_n *m_n)
  7083. {
  7084. struct drm_device *dev = crtc->base.dev;
  7085. struct drm_i915_private *dev_priv = to_i915(dev);
  7086. enum pipe pipe = crtc->pipe;
  7087. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7088. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7089. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7090. & ~TU_SIZE_MASK;
  7091. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7092. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7093. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7094. }
  7095. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7096. enum transcoder transcoder,
  7097. struct intel_link_m_n *m_n,
  7098. struct intel_link_m_n *m2_n2)
  7099. {
  7100. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7101. enum pipe pipe = crtc->pipe;
  7102. if (INTEL_GEN(dev_priv) >= 5) {
  7103. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7104. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7105. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7106. & ~TU_SIZE_MASK;
  7107. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7108. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7109. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7110. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7111. * gen < 8) and if DRRS is supported (to make sure the
  7112. * registers are not unnecessarily read).
  7113. */
  7114. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7115. crtc->config->has_drrs) {
  7116. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7117. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7118. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7119. & ~TU_SIZE_MASK;
  7120. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7121. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7122. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7123. }
  7124. } else {
  7125. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7126. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7127. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7128. & ~TU_SIZE_MASK;
  7129. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7130. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7131. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7132. }
  7133. }
  7134. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7135. struct intel_crtc_state *pipe_config)
  7136. {
  7137. if (pipe_config->has_pch_encoder)
  7138. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7139. else
  7140. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7141. &pipe_config->dp_m_n,
  7142. &pipe_config->dp_m2_n2);
  7143. }
  7144. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7145. struct intel_crtc_state *pipe_config)
  7146. {
  7147. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7148. &pipe_config->fdi_m_n, NULL);
  7149. }
  7150. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7151. struct intel_crtc_state *pipe_config)
  7152. {
  7153. struct drm_device *dev = crtc->base.dev;
  7154. struct drm_i915_private *dev_priv = to_i915(dev);
  7155. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7156. uint32_t ps_ctrl = 0;
  7157. int id = -1;
  7158. int i;
  7159. /* find scaler attached to this pipe */
  7160. for (i = 0; i < crtc->num_scalers; i++) {
  7161. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7162. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7163. id = i;
  7164. pipe_config->pch_pfit.enabled = true;
  7165. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7166. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7167. break;
  7168. }
  7169. }
  7170. scaler_state->scaler_id = id;
  7171. if (id >= 0) {
  7172. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7173. } else {
  7174. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7175. }
  7176. }
  7177. static void
  7178. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7179. struct intel_initial_plane_config *plane_config)
  7180. {
  7181. struct drm_device *dev = crtc->base.dev;
  7182. struct drm_i915_private *dev_priv = to_i915(dev);
  7183. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7184. enum plane_id plane_id = plane->id;
  7185. enum pipe pipe = crtc->pipe;
  7186. u32 val, base, offset, stride_mult, tiling, alpha;
  7187. int fourcc, pixel_format;
  7188. unsigned int aligned_height;
  7189. struct drm_framebuffer *fb;
  7190. struct intel_framebuffer *intel_fb;
  7191. if (!plane->get_hw_state(plane))
  7192. return;
  7193. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7194. if (!intel_fb) {
  7195. DRM_DEBUG_KMS("failed to alloc fb\n");
  7196. return;
  7197. }
  7198. fb = &intel_fb->base;
  7199. fb->dev = dev;
  7200. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7201. if (INTEL_GEN(dev_priv) >= 11)
  7202. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7203. else
  7204. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7205. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7206. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7207. alpha &= PLANE_COLOR_ALPHA_MASK;
  7208. } else {
  7209. alpha = val & PLANE_CTL_ALPHA_MASK;
  7210. }
  7211. fourcc = skl_format_to_fourcc(pixel_format,
  7212. val & PLANE_CTL_ORDER_RGBX, alpha);
  7213. fb->format = drm_format_info(fourcc);
  7214. tiling = val & PLANE_CTL_TILED_MASK;
  7215. switch (tiling) {
  7216. case PLANE_CTL_TILED_LINEAR:
  7217. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7218. break;
  7219. case PLANE_CTL_TILED_X:
  7220. plane_config->tiling = I915_TILING_X;
  7221. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7222. break;
  7223. case PLANE_CTL_TILED_Y:
  7224. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7225. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7226. else
  7227. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7228. break;
  7229. case PLANE_CTL_TILED_YF:
  7230. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7231. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7232. else
  7233. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7234. break;
  7235. default:
  7236. MISSING_CASE(tiling);
  7237. goto error;
  7238. }
  7239. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7240. plane_config->base = base;
  7241. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7242. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7243. fb->height = ((val >> 16) & 0xfff) + 1;
  7244. fb->width = ((val >> 0) & 0x1fff) + 1;
  7245. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7246. stride_mult = intel_fb_stride_alignment(fb, 0);
  7247. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7248. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7249. plane_config->size = fb->pitches[0] * aligned_height;
  7250. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7251. crtc->base.name, plane->base.name, fb->width, fb->height,
  7252. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7253. plane_config->size);
  7254. plane_config->fb = intel_fb;
  7255. return;
  7256. error:
  7257. kfree(intel_fb);
  7258. }
  7259. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7260. struct intel_crtc_state *pipe_config)
  7261. {
  7262. struct drm_device *dev = crtc->base.dev;
  7263. struct drm_i915_private *dev_priv = to_i915(dev);
  7264. uint32_t tmp;
  7265. tmp = I915_READ(PF_CTL(crtc->pipe));
  7266. if (tmp & PF_ENABLE) {
  7267. pipe_config->pch_pfit.enabled = true;
  7268. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7269. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7270. /* We currently do not free assignements of panel fitters on
  7271. * ivb/hsw (since we don't use the higher upscaling modes which
  7272. * differentiates them) so just WARN about this case for now. */
  7273. if (IS_GEN7(dev_priv)) {
  7274. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7275. PF_PIPE_SEL_IVB(crtc->pipe));
  7276. }
  7277. }
  7278. }
  7279. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7280. struct intel_crtc_state *pipe_config)
  7281. {
  7282. struct drm_device *dev = crtc->base.dev;
  7283. struct drm_i915_private *dev_priv = to_i915(dev);
  7284. enum intel_display_power_domain power_domain;
  7285. uint32_t tmp;
  7286. bool ret;
  7287. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7288. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7289. return false;
  7290. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7291. pipe_config->shared_dpll = NULL;
  7292. ret = false;
  7293. tmp = I915_READ(PIPECONF(crtc->pipe));
  7294. if (!(tmp & PIPECONF_ENABLE))
  7295. goto out;
  7296. switch (tmp & PIPECONF_BPC_MASK) {
  7297. case PIPECONF_6BPC:
  7298. pipe_config->pipe_bpp = 18;
  7299. break;
  7300. case PIPECONF_8BPC:
  7301. pipe_config->pipe_bpp = 24;
  7302. break;
  7303. case PIPECONF_10BPC:
  7304. pipe_config->pipe_bpp = 30;
  7305. break;
  7306. case PIPECONF_12BPC:
  7307. pipe_config->pipe_bpp = 36;
  7308. break;
  7309. default:
  7310. break;
  7311. }
  7312. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7313. pipe_config->limited_color_range = true;
  7314. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7315. struct intel_shared_dpll *pll;
  7316. enum intel_dpll_id pll_id;
  7317. pipe_config->has_pch_encoder = true;
  7318. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7319. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7320. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7321. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7322. if (HAS_PCH_IBX(dev_priv)) {
  7323. /*
  7324. * The pipe->pch transcoder and pch transcoder->pll
  7325. * mapping is fixed.
  7326. */
  7327. pll_id = (enum intel_dpll_id) crtc->pipe;
  7328. } else {
  7329. tmp = I915_READ(PCH_DPLL_SEL);
  7330. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7331. pll_id = DPLL_ID_PCH_PLL_B;
  7332. else
  7333. pll_id= DPLL_ID_PCH_PLL_A;
  7334. }
  7335. pipe_config->shared_dpll =
  7336. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7337. pll = pipe_config->shared_dpll;
  7338. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7339. &pipe_config->dpll_hw_state));
  7340. tmp = pipe_config->dpll_hw_state.dpll;
  7341. pipe_config->pixel_multiplier =
  7342. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7343. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7344. ironlake_pch_clock_get(crtc, pipe_config);
  7345. } else {
  7346. pipe_config->pixel_multiplier = 1;
  7347. }
  7348. intel_get_pipe_timings(crtc, pipe_config);
  7349. intel_get_pipe_src_size(crtc, pipe_config);
  7350. ironlake_get_pfit_config(crtc, pipe_config);
  7351. ret = true;
  7352. out:
  7353. intel_display_power_put(dev_priv, power_domain);
  7354. return ret;
  7355. }
  7356. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7357. {
  7358. struct drm_device *dev = &dev_priv->drm;
  7359. struct intel_crtc *crtc;
  7360. for_each_intel_crtc(dev, crtc)
  7361. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7362. pipe_name(crtc->pipe));
  7363. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7364. "Display power well on\n");
  7365. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7366. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7367. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7368. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7369. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7370. "CPU PWM1 enabled\n");
  7371. if (IS_HASWELL(dev_priv))
  7372. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7373. "CPU PWM2 enabled\n");
  7374. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7375. "PCH PWM1 enabled\n");
  7376. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7377. "Utility pin enabled\n");
  7378. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7379. /*
  7380. * In theory we can still leave IRQs enabled, as long as only the HPD
  7381. * interrupts remain enabled. We used to check for that, but since it's
  7382. * gen-specific and since we only disable LCPLL after we fully disable
  7383. * the interrupts, the check below should be enough.
  7384. */
  7385. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7386. }
  7387. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7388. {
  7389. if (IS_HASWELL(dev_priv))
  7390. return I915_READ(D_COMP_HSW);
  7391. else
  7392. return I915_READ(D_COMP_BDW);
  7393. }
  7394. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7395. {
  7396. if (IS_HASWELL(dev_priv)) {
  7397. mutex_lock(&dev_priv->pcu_lock);
  7398. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7399. val))
  7400. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7401. mutex_unlock(&dev_priv->pcu_lock);
  7402. } else {
  7403. I915_WRITE(D_COMP_BDW, val);
  7404. POSTING_READ(D_COMP_BDW);
  7405. }
  7406. }
  7407. /*
  7408. * This function implements pieces of two sequences from BSpec:
  7409. * - Sequence for display software to disable LCPLL
  7410. * - Sequence for display software to allow package C8+
  7411. * The steps implemented here are just the steps that actually touch the LCPLL
  7412. * register. Callers should take care of disabling all the display engine
  7413. * functions, doing the mode unset, fixing interrupts, etc.
  7414. */
  7415. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7416. bool switch_to_fclk, bool allow_power_down)
  7417. {
  7418. uint32_t val;
  7419. assert_can_disable_lcpll(dev_priv);
  7420. val = I915_READ(LCPLL_CTL);
  7421. if (switch_to_fclk) {
  7422. val |= LCPLL_CD_SOURCE_FCLK;
  7423. I915_WRITE(LCPLL_CTL, val);
  7424. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7425. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7426. DRM_ERROR("Switching to FCLK failed\n");
  7427. val = I915_READ(LCPLL_CTL);
  7428. }
  7429. val |= LCPLL_PLL_DISABLE;
  7430. I915_WRITE(LCPLL_CTL, val);
  7431. POSTING_READ(LCPLL_CTL);
  7432. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7433. DRM_ERROR("LCPLL still locked\n");
  7434. val = hsw_read_dcomp(dev_priv);
  7435. val |= D_COMP_COMP_DISABLE;
  7436. hsw_write_dcomp(dev_priv, val);
  7437. ndelay(100);
  7438. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7439. 1))
  7440. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7441. if (allow_power_down) {
  7442. val = I915_READ(LCPLL_CTL);
  7443. val |= LCPLL_POWER_DOWN_ALLOW;
  7444. I915_WRITE(LCPLL_CTL, val);
  7445. POSTING_READ(LCPLL_CTL);
  7446. }
  7447. }
  7448. /*
  7449. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7450. * source.
  7451. */
  7452. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7453. {
  7454. uint32_t val;
  7455. val = I915_READ(LCPLL_CTL);
  7456. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7457. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7458. return;
  7459. /*
  7460. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7461. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7462. */
  7463. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7464. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7465. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7466. I915_WRITE(LCPLL_CTL, val);
  7467. POSTING_READ(LCPLL_CTL);
  7468. }
  7469. val = hsw_read_dcomp(dev_priv);
  7470. val |= D_COMP_COMP_FORCE;
  7471. val &= ~D_COMP_COMP_DISABLE;
  7472. hsw_write_dcomp(dev_priv, val);
  7473. val = I915_READ(LCPLL_CTL);
  7474. val &= ~LCPLL_PLL_DISABLE;
  7475. I915_WRITE(LCPLL_CTL, val);
  7476. if (intel_wait_for_register(dev_priv,
  7477. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7478. 5))
  7479. DRM_ERROR("LCPLL not locked yet\n");
  7480. if (val & LCPLL_CD_SOURCE_FCLK) {
  7481. val = I915_READ(LCPLL_CTL);
  7482. val &= ~LCPLL_CD_SOURCE_FCLK;
  7483. I915_WRITE(LCPLL_CTL, val);
  7484. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7485. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7486. DRM_ERROR("Switching back to LCPLL failed\n");
  7487. }
  7488. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7489. intel_update_cdclk(dev_priv);
  7490. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7491. }
  7492. /*
  7493. * Package states C8 and deeper are really deep PC states that can only be
  7494. * reached when all the devices on the system allow it, so even if the graphics
  7495. * device allows PC8+, it doesn't mean the system will actually get to these
  7496. * states. Our driver only allows PC8+ when going into runtime PM.
  7497. *
  7498. * The requirements for PC8+ are that all the outputs are disabled, the power
  7499. * well is disabled and most interrupts are disabled, and these are also
  7500. * requirements for runtime PM. When these conditions are met, we manually do
  7501. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7502. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7503. * hang the machine.
  7504. *
  7505. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7506. * the state of some registers, so when we come back from PC8+ we need to
  7507. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7508. * need to take care of the registers kept by RC6. Notice that this happens even
  7509. * if we don't put the device in PCI D3 state (which is what currently happens
  7510. * because of the runtime PM support).
  7511. *
  7512. * For more, read "Display Sequences for Package C8" on the hardware
  7513. * documentation.
  7514. */
  7515. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7516. {
  7517. uint32_t val;
  7518. DRM_DEBUG_KMS("Enabling package C8+\n");
  7519. if (HAS_PCH_LPT_LP(dev_priv)) {
  7520. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7521. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7522. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7523. }
  7524. lpt_disable_clkout_dp(dev_priv);
  7525. hsw_disable_lcpll(dev_priv, true, true);
  7526. }
  7527. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7528. {
  7529. uint32_t val;
  7530. DRM_DEBUG_KMS("Disabling package C8+\n");
  7531. hsw_restore_lcpll(dev_priv);
  7532. lpt_init_pch_refclk(dev_priv);
  7533. if (HAS_PCH_LPT_LP(dev_priv)) {
  7534. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7535. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7536. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7537. }
  7538. }
  7539. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7540. struct intel_crtc_state *crtc_state)
  7541. {
  7542. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7543. struct intel_encoder *encoder =
  7544. intel_ddi_get_crtc_new_encoder(crtc_state);
  7545. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7546. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7547. pipe_name(crtc->pipe));
  7548. return -EINVAL;
  7549. }
  7550. }
  7551. return 0;
  7552. }
  7553. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7554. enum port port,
  7555. struct intel_crtc_state *pipe_config)
  7556. {
  7557. enum intel_dpll_id id;
  7558. u32 temp;
  7559. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7560. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7561. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7562. return;
  7563. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7564. }
  7565. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7566. enum port port,
  7567. struct intel_crtc_state *pipe_config)
  7568. {
  7569. enum intel_dpll_id id;
  7570. switch (port) {
  7571. case PORT_A:
  7572. id = DPLL_ID_SKL_DPLL0;
  7573. break;
  7574. case PORT_B:
  7575. id = DPLL_ID_SKL_DPLL1;
  7576. break;
  7577. case PORT_C:
  7578. id = DPLL_ID_SKL_DPLL2;
  7579. break;
  7580. default:
  7581. DRM_ERROR("Incorrect port type\n");
  7582. return;
  7583. }
  7584. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7585. }
  7586. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7587. enum port port,
  7588. struct intel_crtc_state *pipe_config)
  7589. {
  7590. enum intel_dpll_id id;
  7591. u32 temp;
  7592. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7593. id = temp >> (port * 3 + 1);
  7594. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7595. return;
  7596. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7597. }
  7598. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7599. enum port port,
  7600. struct intel_crtc_state *pipe_config)
  7601. {
  7602. enum intel_dpll_id id;
  7603. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7604. switch (ddi_pll_sel) {
  7605. case PORT_CLK_SEL_WRPLL1:
  7606. id = DPLL_ID_WRPLL1;
  7607. break;
  7608. case PORT_CLK_SEL_WRPLL2:
  7609. id = DPLL_ID_WRPLL2;
  7610. break;
  7611. case PORT_CLK_SEL_SPLL:
  7612. id = DPLL_ID_SPLL;
  7613. break;
  7614. case PORT_CLK_SEL_LCPLL_810:
  7615. id = DPLL_ID_LCPLL_810;
  7616. break;
  7617. case PORT_CLK_SEL_LCPLL_1350:
  7618. id = DPLL_ID_LCPLL_1350;
  7619. break;
  7620. case PORT_CLK_SEL_LCPLL_2700:
  7621. id = DPLL_ID_LCPLL_2700;
  7622. break;
  7623. default:
  7624. MISSING_CASE(ddi_pll_sel);
  7625. /* fall through */
  7626. case PORT_CLK_SEL_NONE:
  7627. return;
  7628. }
  7629. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7630. }
  7631. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7632. struct intel_crtc_state *pipe_config,
  7633. u64 *power_domain_mask)
  7634. {
  7635. struct drm_device *dev = crtc->base.dev;
  7636. struct drm_i915_private *dev_priv = to_i915(dev);
  7637. enum intel_display_power_domain power_domain;
  7638. u32 tmp;
  7639. /*
  7640. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7641. * transcoder handled below.
  7642. */
  7643. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7644. /*
  7645. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7646. * consistency and less surprising code; it's in always on power).
  7647. */
  7648. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7649. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7650. enum pipe trans_edp_pipe;
  7651. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7652. default:
  7653. WARN(1, "unknown pipe linked to edp transcoder\n");
  7654. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7655. case TRANS_DDI_EDP_INPUT_A_ON:
  7656. trans_edp_pipe = PIPE_A;
  7657. break;
  7658. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7659. trans_edp_pipe = PIPE_B;
  7660. break;
  7661. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7662. trans_edp_pipe = PIPE_C;
  7663. break;
  7664. }
  7665. if (trans_edp_pipe == crtc->pipe)
  7666. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7667. }
  7668. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7669. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7670. return false;
  7671. *power_domain_mask |= BIT_ULL(power_domain);
  7672. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7673. return tmp & PIPECONF_ENABLE;
  7674. }
  7675. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7676. struct intel_crtc_state *pipe_config,
  7677. u64 *power_domain_mask)
  7678. {
  7679. struct drm_device *dev = crtc->base.dev;
  7680. struct drm_i915_private *dev_priv = to_i915(dev);
  7681. enum intel_display_power_domain power_domain;
  7682. enum port port;
  7683. enum transcoder cpu_transcoder;
  7684. u32 tmp;
  7685. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7686. if (port == PORT_A)
  7687. cpu_transcoder = TRANSCODER_DSI_A;
  7688. else
  7689. cpu_transcoder = TRANSCODER_DSI_C;
  7690. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7691. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7692. continue;
  7693. *power_domain_mask |= BIT_ULL(power_domain);
  7694. /*
  7695. * The PLL needs to be enabled with a valid divider
  7696. * configuration, otherwise accessing DSI registers will hang
  7697. * the machine. See BSpec North Display Engine
  7698. * registers/MIPI[BXT]. We can break out here early, since we
  7699. * need the same DSI PLL to be enabled for both DSI ports.
  7700. */
  7701. if (!intel_dsi_pll_is_enabled(dev_priv))
  7702. break;
  7703. /* XXX: this works for video mode only */
  7704. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7705. if (!(tmp & DPI_ENABLE))
  7706. continue;
  7707. tmp = I915_READ(MIPI_CTRL(port));
  7708. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7709. continue;
  7710. pipe_config->cpu_transcoder = cpu_transcoder;
  7711. break;
  7712. }
  7713. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7714. }
  7715. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7716. struct intel_crtc_state *pipe_config)
  7717. {
  7718. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7719. struct intel_shared_dpll *pll;
  7720. enum port port;
  7721. uint32_t tmp;
  7722. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7723. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7724. if (IS_CANNONLAKE(dev_priv))
  7725. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7726. else if (IS_GEN9_BC(dev_priv))
  7727. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7728. else if (IS_GEN9_LP(dev_priv))
  7729. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7730. else
  7731. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7732. pll = pipe_config->shared_dpll;
  7733. if (pll) {
  7734. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7735. &pipe_config->dpll_hw_state));
  7736. }
  7737. /*
  7738. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7739. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7740. * the PCH transcoder is on.
  7741. */
  7742. if (INTEL_GEN(dev_priv) < 9 &&
  7743. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7744. pipe_config->has_pch_encoder = true;
  7745. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7746. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7747. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7748. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7749. }
  7750. }
  7751. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7752. struct intel_crtc_state *pipe_config)
  7753. {
  7754. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7755. enum intel_display_power_domain power_domain;
  7756. u64 power_domain_mask;
  7757. bool active;
  7758. intel_crtc_init_scalers(crtc, pipe_config);
  7759. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7760. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7761. return false;
  7762. power_domain_mask = BIT_ULL(power_domain);
  7763. pipe_config->shared_dpll = NULL;
  7764. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7765. if (IS_GEN9_LP(dev_priv) &&
  7766. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7767. WARN_ON(active);
  7768. active = true;
  7769. }
  7770. if (!active)
  7771. goto out;
  7772. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7773. haswell_get_ddi_port_state(crtc, pipe_config);
  7774. intel_get_pipe_timings(crtc, pipe_config);
  7775. }
  7776. intel_get_pipe_src_size(crtc, pipe_config);
  7777. pipe_config->gamma_mode =
  7778. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7779. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7780. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7781. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7782. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7783. bool blend_mode_420 = tmp &
  7784. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7785. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7786. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7787. pipe_config->ycbcr420 != blend_mode_420)
  7788. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7789. } else if (clrspace_yuv) {
  7790. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7791. }
  7792. }
  7793. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7794. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7795. power_domain_mask |= BIT_ULL(power_domain);
  7796. if (INTEL_GEN(dev_priv) >= 9)
  7797. skylake_get_pfit_config(crtc, pipe_config);
  7798. else
  7799. ironlake_get_pfit_config(crtc, pipe_config);
  7800. }
  7801. if (hsw_crtc_supports_ips(crtc)) {
  7802. if (IS_HASWELL(dev_priv))
  7803. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7804. else {
  7805. /*
  7806. * We cannot readout IPS state on broadwell, set to
  7807. * true so we can set it to a defined state on first
  7808. * commit.
  7809. */
  7810. pipe_config->ips_enabled = true;
  7811. }
  7812. }
  7813. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7814. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7815. pipe_config->pixel_multiplier =
  7816. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7817. } else {
  7818. pipe_config->pixel_multiplier = 1;
  7819. }
  7820. out:
  7821. for_each_power_domain(power_domain, power_domain_mask)
  7822. intel_display_power_put(dev_priv, power_domain);
  7823. return active;
  7824. }
  7825. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7826. {
  7827. struct drm_i915_private *dev_priv =
  7828. to_i915(plane_state->base.plane->dev);
  7829. const struct drm_framebuffer *fb = plane_state->base.fb;
  7830. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7831. u32 base;
  7832. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7833. base = obj->phys_handle->busaddr;
  7834. else
  7835. base = intel_plane_ggtt_offset(plane_state);
  7836. base += plane_state->main.offset;
  7837. /* ILK+ do this automagically */
  7838. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7839. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7840. base += (plane_state->base.crtc_h *
  7841. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7842. return base;
  7843. }
  7844. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7845. {
  7846. int x = plane_state->base.crtc_x;
  7847. int y = plane_state->base.crtc_y;
  7848. u32 pos = 0;
  7849. if (x < 0) {
  7850. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7851. x = -x;
  7852. }
  7853. pos |= x << CURSOR_X_SHIFT;
  7854. if (y < 0) {
  7855. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7856. y = -y;
  7857. }
  7858. pos |= y << CURSOR_Y_SHIFT;
  7859. return pos;
  7860. }
  7861. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7862. {
  7863. const struct drm_mode_config *config =
  7864. &plane_state->base.plane->dev->mode_config;
  7865. int width = plane_state->base.crtc_w;
  7866. int height = plane_state->base.crtc_h;
  7867. return width > 0 && width <= config->cursor_width &&
  7868. height > 0 && height <= config->cursor_height;
  7869. }
  7870. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7871. struct intel_plane_state *plane_state)
  7872. {
  7873. const struct drm_framebuffer *fb = plane_state->base.fb;
  7874. int src_x, src_y;
  7875. u32 offset;
  7876. int ret;
  7877. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7878. &crtc_state->base,
  7879. DRM_PLANE_HELPER_NO_SCALING,
  7880. DRM_PLANE_HELPER_NO_SCALING,
  7881. true, true);
  7882. if (ret)
  7883. return ret;
  7884. if (!fb)
  7885. return 0;
  7886. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7887. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7888. return -EINVAL;
  7889. }
  7890. src_x = plane_state->base.src_x >> 16;
  7891. src_y = plane_state->base.src_y >> 16;
  7892. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7893. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7894. if (src_x != 0 || src_y != 0) {
  7895. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7896. return -EINVAL;
  7897. }
  7898. plane_state->main.offset = offset;
  7899. return 0;
  7900. }
  7901. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7902. const struct intel_plane_state *plane_state)
  7903. {
  7904. const struct drm_framebuffer *fb = plane_state->base.fb;
  7905. return CURSOR_ENABLE |
  7906. CURSOR_GAMMA_ENABLE |
  7907. CURSOR_FORMAT_ARGB |
  7908. CURSOR_STRIDE(fb->pitches[0]);
  7909. }
  7910. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7911. {
  7912. int width = plane_state->base.crtc_w;
  7913. /*
  7914. * 845g/865g are only limited by the width of their cursors,
  7915. * the height is arbitrary up to the precision of the register.
  7916. */
  7917. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7918. }
  7919. static int i845_check_cursor(struct intel_plane *plane,
  7920. struct intel_crtc_state *crtc_state,
  7921. struct intel_plane_state *plane_state)
  7922. {
  7923. const struct drm_framebuffer *fb = plane_state->base.fb;
  7924. int ret;
  7925. ret = intel_check_cursor(crtc_state, plane_state);
  7926. if (ret)
  7927. return ret;
  7928. /* if we want to turn off the cursor ignore width and height */
  7929. if (!fb)
  7930. return 0;
  7931. /* Check for which cursor types we support */
  7932. if (!i845_cursor_size_ok(plane_state)) {
  7933. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7934. plane_state->base.crtc_w,
  7935. plane_state->base.crtc_h);
  7936. return -EINVAL;
  7937. }
  7938. switch (fb->pitches[0]) {
  7939. case 256:
  7940. case 512:
  7941. case 1024:
  7942. case 2048:
  7943. break;
  7944. default:
  7945. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7946. fb->pitches[0]);
  7947. return -EINVAL;
  7948. }
  7949. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7950. return 0;
  7951. }
  7952. static void i845_update_cursor(struct intel_plane *plane,
  7953. const struct intel_crtc_state *crtc_state,
  7954. const struct intel_plane_state *plane_state)
  7955. {
  7956. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7957. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7958. unsigned long irqflags;
  7959. if (plane_state && plane_state->base.visible) {
  7960. unsigned int width = plane_state->base.crtc_w;
  7961. unsigned int height = plane_state->base.crtc_h;
  7962. cntl = plane_state->ctl;
  7963. size = (height << 12) | width;
  7964. base = intel_cursor_base(plane_state);
  7965. pos = intel_cursor_position(plane_state);
  7966. }
  7967. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7968. /* On these chipsets we can only modify the base/size/stride
  7969. * whilst the cursor is disabled.
  7970. */
  7971. if (plane->cursor.base != base ||
  7972. plane->cursor.size != size ||
  7973. plane->cursor.cntl != cntl) {
  7974. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7975. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7976. I915_WRITE_FW(CURSIZE, size);
  7977. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7978. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7979. plane->cursor.base = base;
  7980. plane->cursor.size = size;
  7981. plane->cursor.cntl = cntl;
  7982. } else {
  7983. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7984. }
  7985. POSTING_READ_FW(CURCNTR(PIPE_A));
  7986. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7987. }
  7988. static void i845_disable_cursor(struct intel_plane *plane,
  7989. struct intel_crtc *crtc)
  7990. {
  7991. i845_update_cursor(plane, NULL, NULL);
  7992. }
  7993. static bool i845_cursor_get_hw_state(struct intel_plane *plane)
  7994. {
  7995. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7996. enum intel_display_power_domain power_domain;
  7997. bool ret;
  7998. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  7999. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8000. return false;
  8001. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  8002. intel_display_power_put(dev_priv, power_domain);
  8003. return ret;
  8004. }
  8005. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8006. const struct intel_plane_state *plane_state)
  8007. {
  8008. struct drm_i915_private *dev_priv =
  8009. to_i915(plane_state->base.plane->dev);
  8010. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8011. u32 cntl;
  8012. cntl = MCURSOR_GAMMA_ENABLE;
  8013. if (HAS_DDI(dev_priv))
  8014. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8015. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8016. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8017. switch (plane_state->base.crtc_w) {
  8018. case 64:
  8019. cntl |= CURSOR_MODE_64_ARGB_AX;
  8020. break;
  8021. case 128:
  8022. cntl |= CURSOR_MODE_128_ARGB_AX;
  8023. break;
  8024. case 256:
  8025. cntl |= CURSOR_MODE_256_ARGB_AX;
  8026. break;
  8027. default:
  8028. MISSING_CASE(plane_state->base.crtc_w);
  8029. return 0;
  8030. }
  8031. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8032. cntl |= CURSOR_ROTATE_180;
  8033. return cntl;
  8034. }
  8035. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8036. {
  8037. struct drm_i915_private *dev_priv =
  8038. to_i915(plane_state->base.plane->dev);
  8039. int width = plane_state->base.crtc_w;
  8040. int height = plane_state->base.crtc_h;
  8041. if (!intel_cursor_size_ok(plane_state))
  8042. return false;
  8043. /* Cursor width is limited to a few power-of-two sizes */
  8044. switch (width) {
  8045. case 256:
  8046. case 128:
  8047. case 64:
  8048. break;
  8049. default:
  8050. return false;
  8051. }
  8052. /*
  8053. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8054. * height from 8 lines up to the cursor width, when the
  8055. * cursor is not rotated. Everything else requires square
  8056. * cursors.
  8057. */
  8058. if (HAS_CUR_FBC(dev_priv) &&
  8059. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8060. if (height < 8 || height > width)
  8061. return false;
  8062. } else {
  8063. if (height != width)
  8064. return false;
  8065. }
  8066. return true;
  8067. }
  8068. static int i9xx_check_cursor(struct intel_plane *plane,
  8069. struct intel_crtc_state *crtc_state,
  8070. struct intel_plane_state *plane_state)
  8071. {
  8072. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8073. const struct drm_framebuffer *fb = plane_state->base.fb;
  8074. enum pipe pipe = plane->pipe;
  8075. int ret;
  8076. ret = intel_check_cursor(crtc_state, plane_state);
  8077. if (ret)
  8078. return ret;
  8079. /* if we want to turn off the cursor ignore width and height */
  8080. if (!fb)
  8081. return 0;
  8082. /* Check for which cursor types we support */
  8083. if (!i9xx_cursor_size_ok(plane_state)) {
  8084. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8085. plane_state->base.crtc_w,
  8086. plane_state->base.crtc_h);
  8087. return -EINVAL;
  8088. }
  8089. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8090. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8091. fb->pitches[0], plane_state->base.crtc_w);
  8092. return -EINVAL;
  8093. }
  8094. /*
  8095. * There's something wrong with the cursor on CHV pipe C.
  8096. * If it straddles the left edge of the screen then
  8097. * moving it away from the edge or disabling it often
  8098. * results in a pipe underrun, and often that can lead to
  8099. * dead pipe (constant underrun reported, and it scans
  8100. * out just a solid color). To recover from that, the
  8101. * display power well must be turned off and on again.
  8102. * Refuse the put the cursor into that compromised position.
  8103. */
  8104. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8105. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8106. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8107. return -EINVAL;
  8108. }
  8109. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8110. return 0;
  8111. }
  8112. static void i9xx_update_cursor(struct intel_plane *plane,
  8113. const struct intel_crtc_state *crtc_state,
  8114. const struct intel_plane_state *plane_state)
  8115. {
  8116. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8117. enum pipe pipe = plane->pipe;
  8118. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8119. unsigned long irqflags;
  8120. if (plane_state && plane_state->base.visible) {
  8121. cntl = plane_state->ctl;
  8122. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8123. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8124. base = intel_cursor_base(plane_state);
  8125. pos = intel_cursor_position(plane_state);
  8126. }
  8127. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8128. /*
  8129. * On some platforms writing CURCNTR first will also
  8130. * cause CURPOS to be armed by the CURBASE write.
  8131. * Without the CURCNTR write the CURPOS write would
  8132. * arm itself. Thus we always start the full update
  8133. * with a CURCNTR write.
  8134. *
  8135. * On other platforms CURPOS always requires the
  8136. * CURBASE write to arm the update. Additonally
  8137. * a write to any of the cursor register will cancel
  8138. * an already armed cursor update. Thus leaving out
  8139. * the CURBASE write after CURPOS could lead to a
  8140. * cursor that doesn't appear to move, or even change
  8141. * shape. Thus we always write CURBASE.
  8142. *
  8143. * CURCNTR and CUR_FBC_CTL are always
  8144. * armed by the CURBASE write only.
  8145. */
  8146. if (plane->cursor.base != base ||
  8147. plane->cursor.size != fbc_ctl ||
  8148. plane->cursor.cntl != cntl) {
  8149. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8150. if (HAS_CUR_FBC(dev_priv))
  8151. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8152. I915_WRITE_FW(CURPOS(pipe), pos);
  8153. I915_WRITE_FW(CURBASE(pipe), base);
  8154. plane->cursor.base = base;
  8155. plane->cursor.size = fbc_ctl;
  8156. plane->cursor.cntl = cntl;
  8157. } else {
  8158. I915_WRITE_FW(CURPOS(pipe), pos);
  8159. I915_WRITE_FW(CURBASE(pipe), base);
  8160. }
  8161. POSTING_READ_FW(CURBASE(pipe));
  8162. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8163. }
  8164. static void i9xx_disable_cursor(struct intel_plane *plane,
  8165. struct intel_crtc *crtc)
  8166. {
  8167. i9xx_update_cursor(plane, NULL, NULL);
  8168. }
  8169. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
  8170. {
  8171. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8172. enum intel_display_power_domain power_domain;
  8173. enum pipe pipe = plane->pipe;
  8174. bool ret;
  8175. /*
  8176. * Not 100% correct for planes that can move between pipes,
  8177. * but that's only the case for gen2-3 which don't have any
  8178. * display power wells.
  8179. */
  8180. power_domain = POWER_DOMAIN_PIPE(pipe);
  8181. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8182. return false;
  8183. ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  8184. intel_display_power_put(dev_priv, power_domain);
  8185. return ret;
  8186. }
  8187. /* VESA 640x480x72Hz mode to set on the pipe */
  8188. static const struct drm_display_mode load_detect_mode = {
  8189. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8190. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8191. };
  8192. struct drm_framebuffer *
  8193. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8194. struct drm_mode_fb_cmd2 *mode_cmd)
  8195. {
  8196. struct intel_framebuffer *intel_fb;
  8197. int ret;
  8198. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8199. if (!intel_fb)
  8200. return ERR_PTR(-ENOMEM);
  8201. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8202. if (ret)
  8203. goto err;
  8204. return &intel_fb->base;
  8205. err:
  8206. kfree(intel_fb);
  8207. return ERR_PTR(ret);
  8208. }
  8209. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8210. struct drm_crtc *crtc)
  8211. {
  8212. struct drm_plane *plane;
  8213. struct drm_plane_state *plane_state;
  8214. int ret, i;
  8215. ret = drm_atomic_add_affected_planes(state, crtc);
  8216. if (ret)
  8217. return ret;
  8218. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8219. if (plane_state->crtc != crtc)
  8220. continue;
  8221. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8222. if (ret)
  8223. return ret;
  8224. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8225. }
  8226. return 0;
  8227. }
  8228. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8229. const struct drm_display_mode *mode,
  8230. struct intel_load_detect_pipe *old,
  8231. struct drm_modeset_acquire_ctx *ctx)
  8232. {
  8233. struct intel_crtc *intel_crtc;
  8234. struct intel_encoder *intel_encoder =
  8235. intel_attached_encoder(connector);
  8236. struct drm_crtc *possible_crtc;
  8237. struct drm_encoder *encoder = &intel_encoder->base;
  8238. struct drm_crtc *crtc = NULL;
  8239. struct drm_device *dev = encoder->dev;
  8240. struct drm_i915_private *dev_priv = to_i915(dev);
  8241. struct drm_mode_config *config = &dev->mode_config;
  8242. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8243. struct drm_connector_state *connector_state;
  8244. struct intel_crtc_state *crtc_state;
  8245. int ret, i = -1;
  8246. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8247. connector->base.id, connector->name,
  8248. encoder->base.id, encoder->name);
  8249. old->restore_state = NULL;
  8250. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8251. /*
  8252. * Algorithm gets a little messy:
  8253. *
  8254. * - if the connector already has an assigned crtc, use it (but make
  8255. * sure it's on first)
  8256. *
  8257. * - try to find the first unused crtc that can drive this connector,
  8258. * and use that if we find one
  8259. */
  8260. /* See if we already have a CRTC for this connector */
  8261. if (connector->state->crtc) {
  8262. crtc = connector->state->crtc;
  8263. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8264. if (ret)
  8265. goto fail;
  8266. /* Make sure the crtc and connector are running */
  8267. goto found;
  8268. }
  8269. /* Find an unused one (if possible) */
  8270. for_each_crtc(dev, possible_crtc) {
  8271. i++;
  8272. if (!(encoder->possible_crtcs & (1 << i)))
  8273. continue;
  8274. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8275. if (ret)
  8276. goto fail;
  8277. if (possible_crtc->state->enable) {
  8278. drm_modeset_unlock(&possible_crtc->mutex);
  8279. continue;
  8280. }
  8281. crtc = possible_crtc;
  8282. break;
  8283. }
  8284. /*
  8285. * If we didn't find an unused CRTC, don't use any.
  8286. */
  8287. if (!crtc) {
  8288. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8289. ret = -ENODEV;
  8290. goto fail;
  8291. }
  8292. found:
  8293. intel_crtc = to_intel_crtc(crtc);
  8294. state = drm_atomic_state_alloc(dev);
  8295. restore_state = drm_atomic_state_alloc(dev);
  8296. if (!state || !restore_state) {
  8297. ret = -ENOMEM;
  8298. goto fail;
  8299. }
  8300. state->acquire_ctx = ctx;
  8301. restore_state->acquire_ctx = ctx;
  8302. connector_state = drm_atomic_get_connector_state(state, connector);
  8303. if (IS_ERR(connector_state)) {
  8304. ret = PTR_ERR(connector_state);
  8305. goto fail;
  8306. }
  8307. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8308. if (ret)
  8309. goto fail;
  8310. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8311. if (IS_ERR(crtc_state)) {
  8312. ret = PTR_ERR(crtc_state);
  8313. goto fail;
  8314. }
  8315. crtc_state->base.active = crtc_state->base.enable = true;
  8316. if (!mode)
  8317. mode = &load_detect_mode;
  8318. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8319. if (ret)
  8320. goto fail;
  8321. ret = intel_modeset_disable_planes(state, crtc);
  8322. if (ret)
  8323. goto fail;
  8324. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8325. if (!ret)
  8326. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8327. if (ret) {
  8328. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8329. goto fail;
  8330. }
  8331. ret = drm_atomic_commit(state);
  8332. if (ret) {
  8333. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8334. goto fail;
  8335. }
  8336. old->restore_state = restore_state;
  8337. drm_atomic_state_put(state);
  8338. /* let the connector get through one full cycle before testing */
  8339. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8340. return true;
  8341. fail:
  8342. if (state) {
  8343. drm_atomic_state_put(state);
  8344. state = NULL;
  8345. }
  8346. if (restore_state) {
  8347. drm_atomic_state_put(restore_state);
  8348. restore_state = NULL;
  8349. }
  8350. if (ret == -EDEADLK)
  8351. return ret;
  8352. return false;
  8353. }
  8354. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8355. struct intel_load_detect_pipe *old,
  8356. struct drm_modeset_acquire_ctx *ctx)
  8357. {
  8358. struct intel_encoder *intel_encoder =
  8359. intel_attached_encoder(connector);
  8360. struct drm_encoder *encoder = &intel_encoder->base;
  8361. struct drm_atomic_state *state = old->restore_state;
  8362. int ret;
  8363. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8364. connector->base.id, connector->name,
  8365. encoder->base.id, encoder->name);
  8366. if (!state)
  8367. return;
  8368. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8369. if (ret)
  8370. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8371. drm_atomic_state_put(state);
  8372. }
  8373. static int i9xx_pll_refclk(struct drm_device *dev,
  8374. const struct intel_crtc_state *pipe_config)
  8375. {
  8376. struct drm_i915_private *dev_priv = to_i915(dev);
  8377. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8378. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8379. return dev_priv->vbt.lvds_ssc_freq;
  8380. else if (HAS_PCH_SPLIT(dev_priv))
  8381. return 120000;
  8382. else if (!IS_GEN2(dev_priv))
  8383. return 96000;
  8384. else
  8385. return 48000;
  8386. }
  8387. /* Returns the clock of the currently programmed mode of the given pipe. */
  8388. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8389. struct intel_crtc_state *pipe_config)
  8390. {
  8391. struct drm_device *dev = crtc->base.dev;
  8392. struct drm_i915_private *dev_priv = to_i915(dev);
  8393. int pipe = pipe_config->cpu_transcoder;
  8394. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8395. u32 fp;
  8396. struct dpll clock;
  8397. int port_clock;
  8398. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8399. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8400. fp = pipe_config->dpll_hw_state.fp0;
  8401. else
  8402. fp = pipe_config->dpll_hw_state.fp1;
  8403. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8404. if (IS_PINEVIEW(dev_priv)) {
  8405. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8406. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8407. } else {
  8408. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8409. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8410. }
  8411. if (!IS_GEN2(dev_priv)) {
  8412. if (IS_PINEVIEW(dev_priv))
  8413. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8414. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8415. else
  8416. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8417. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8418. switch (dpll & DPLL_MODE_MASK) {
  8419. case DPLLB_MODE_DAC_SERIAL:
  8420. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8421. 5 : 10;
  8422. break;
  8423. case DPLLB_MODE_LVDS:
  8424. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8425. 7 : 14;
  8426. break;
  8427. default:
  8428. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8429. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8430. return;
  8431. }
  8432. if (IS_PINEVIEW(dev_priv))
  8433. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8434. else
  8435. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8436. } else {
  8437. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8438. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8439. if (is_lvds) {
  8440. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8441. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8442. if (lvds & LVDS_CLKB_POWER_UP)
  8443. clock.p2 = 7;
  8444. else
  8445. clock.p2 = 14;
  8446. } else {
  8447. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8448. clock.p1 = 2;
  8449. else {
  8450. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8451. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8452. }
  8453. if (dpll & PLL_P2_DIVIDE_BY_4)
  8454. clock.p2 = 4;
  8455. else
  8456. clock.p2 = 2;
  8457. }
  8458. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8459. }
  8460. /*
  8461. * This value includes pixel_multiplier. We will use
  8462. * port_clock to compute adjusted_mode.crtc_clock in the
  8463. * encoder's get_config() function.
  8464. */
  8465. pipe_config->port_clock = port_clock;
  8466. }
  8467. int intel_dotclock_calculate(int link_freq,
  8468. const struct intel_link_m_n *m_n)
  8469. {
  8470. /*
  8471. * The calculation for the data clock is:
  8472. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8473. * But we want to avoid losing precison if possible, so:
  8474. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8475. *
  8476. * and the link clock is simpler:
  8477. * link_clock = (m * link_clock) / n
  8478. */
  8479. if (!m_n->link_n)
  8480. return 0;
  8481. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8482. }
  8483. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8484. struct intel_crtc_state *pipe_config)
  8485. {
  8486. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8487. /* read out port_clock from the DPLL */
  8488. i9xx_crtc_clock_get(crtc, pipe_config);
  8489. /*
  8490. * In case there is an active pipe without active ports,
  8491. * we may need some idea for the dotclock anyway.
  8492. * Calculate one based on the FDI configuration.
  8493. */
  8494. pipe_config->base.adjusted_mode.crtc_clock =
  8495. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8496. &pipe_config->fdi_m_n);
  8497. }
  8498. /* Returns the currently programmed mode of the given encoder. */
  8499. struct drm_display_mode *
  8500. intel_encoder_current_mode(struct intel_encoder *encoder)
  8501. {
  8502. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8503. struct intel_crtc_state *crtc_state;
  8504. struct drm_display_mode *mode;
  8505. struct intel_crtc *crtc;
  8506. enum pipe pipe;
  8507. if (!encoder->get_hw_state(encoder, &pipe))
  8508. return NULL;
  8509. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8510. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8511. if (!mode)
  8512. return NULL;
  8513. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8514. if (!crtc_state) {
  8515. kfree(mode);
  8516. return NULL;
  8517. }
  8518. crtc_state->base.crtc = &crtc->base;
  8519. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8520. kfree(crtc_state);
  8521. kfree(mode);
  8522. return NULL;
  8523. }
  8524. encoder->get_config(encoder, crtc_state);
  8525. intel_mode_from_pipe_config(mode, crtc_state);
  8526. kfree(crtc_state);
  8527. return mode;
  8528. }
  8529. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8530. {
  8531. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8532. drm_crtc_cleanup(crtc);
  8533. kfree(intel_crtc);
  8534. }
  8535. /**
  8536. * intel_wm_need_update - Check whether watermarks need updating
  8537. * @plane: drm plane
  8538. * @state: new plane state
  8539. *
  8540. * Check current plane state versus the new one to determine whether
  8541. * watermarks need to be recalculated.
  8542. *
  8543. * Returns true or false.
  8544. */
  8545. static bool intel_wm_need_update(struct drm_plane *plane,
  8546. struct drm_plane_state *state)
  8547. {
  8548. struct intel_plane_state *new = to_intel_plane_state(state);
  8549. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8550. /* Update watermarks on tiling or size changes. */
  8551. if (new->base.visible != cur->base.visible)
  8552. return true;
  8553. if (!cur->base.fb || !new->base.fb)
  8554. return false;
  8555. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8556. cur->base.rotation != new->base.rotation ||
  8557. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8558. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8559. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8560. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8561. return true;
  8562. return false;
  8563. }
  8564. static bool needs_scaling(const struct intel_plane_state *state)
  8565. {
  8566. int src_w = drm_rect_width(&state->base.src) >> 16;
  8567. int src_h = drm_rect_height(&state->base.src) >> 16;
  8568. int dst_w = drm_rect_width(&state->base.dst);
  8569. int dst_h = drm_rect_height(&state->base.dst);
  8570. return (src_w != dst_w || src_h != dst_h);
  8571. }
  8572. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8573. struct drm_crtc_state *crtc_state,
  8574. const struct intel_plane_state *old_plane_state,
  8575. struct drm_plane_state *plane_state)
  8576. {
  8577. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8578. struct drm_crtc *crtc = crtc_state->crtc;
  8579. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8580. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8581. struct drm_device *dev = crtc->dev;
  8582. struct drm_i915_private *dev_priv = to_i915(dev);
  8583. bool mode_changed = needs_modeset(crtc_state);
  8584. bool was_crtc_enabled = old_crtc_state->base.active;
  8585. bool is_crtc_enabled = crtc_state->active;
  8586. bool turn_off, turn_on, visible, was_visible;
  8587. struct drm_framebuffer *fb = plane_state->fb;
  8588. int ret;
  8589. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8590. ret = skl_update_scaler_plane(
  8591. to_intel_crtc_state(crtc_state),
  8592. to_intel_plane_state(plane_state));
  8593. if (ret)
  8594. return ret;
  8595. }
  8596. was_visible = old_plane_state->base.visible;
  8597. visible = plane_state->visible;
  8598. if (!was_crtc_enabled && WARN_ON(was_visible))
  8599. was_visible = false;
  8600. /*
  8601. * Visibility is calculated as if the crtc was on, but
  8602. * after scaler setup everything depends on it being off
  8603. * when the crtc isn't active.
  8604. *
  8605. * FIXME this is wrong for watermarks. Watermarks should also
  8606. * be computed as if the pipe would be active. Perhaps move
  8607. * per-plane wm computation to the .check_plane() hook, and
  8608. * only combine the results from all planes in the current place?
  8609. */
  8610. if (!is_crtc_enabled) {
  8611. plane_state->visible = visible = false;
  8612. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8613. }
  8614. if (!was_visible && !visible)
  8615. return 0;
  8616. if (fb != old_plane_state->base.fb)
  8617. pipe_config->fb_changed = true;
  8618. turn_off = was_visible && (!visible || mode_changed);
  8619. turn_on = visible && (!was_visible || mode_changed);
  8620. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8621. intel_crtc->base.base.id, intel_crtc->base.name,
  8622. plane->base.base.id, plane->base.name,
  8623. fb ? fb->base.id : -1);
  8624. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8625. plane->base.base.id, plane->base.name,
  8626. was_visible, visible,
  8627. turn_off, turn_on, mode_changed);
  8628. if (turn_on) {
  8629. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8630. pipe_config->update_wm_pre = true;
  8631. /* must disable cxsr around plane enable/disable */
  8632. if (plane->id != PLANE_CURSOR)
  8633. pipe_config->disable_cxsr = true;
  8634. } else if (turn_off) {
  8635. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8636. pipe_config->update_wm_post = true;
  8637. /* must disable cxsr around plane enable/disable */
  8638. if (plane->id != PLANE_CURSOR)
  8639. pipe_config->disable_cxsr = true;
  8640. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8641. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8642. /* FIXME bollocks */
  8643. pipe_config->update_wm_pre = true;
  8644. pipe_config->update_wm_post = true;
  8645. }
  8646. }
  8647. if (visible || was_visible)
  8648. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8649. /*
  8650. * WaCxSRDisabledForSpriteScaling:ivb
  8651. *
  8652. * cstate->update_wm was already set above, so this flag will
  8653. * take effect when we commit and program watermarks.
  8654. */
  8655. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8656. needs_scaling(to_intel_plane_state(plane_state)) &&
  8657. !needs_scaling(old_plane_state))
  8658. pipe_config->disable_lp_wm = true;
  8659. return 0;
  8660. }
  8661. static bool encoders_cloneable(const struct intel_encoder *a,
  8662. const struct intel_encoder *b)
  8663. {
  8664. /* masks could be asymmetric, so check both ways */
  8665. return a == b || (a->cloneable & (1 << b->type) &&
  8666. b->cloneable & (1 << a->type));
  8667. }
  8668. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8669. struct intel_crtc *crtc,
  8670. struct intel_encoder *encoder)
  8671. {
  8672. struct intel_encoder *source_encoder;
  8673. struct drm_connector *connector;
  8674. struct drm_connector_state *connector_state;
  8675. int i;
  8676. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8677. if (connector_state->crtc != &crtc->base)
  8678. continue;
  8679. source_encoder =
  8680. to_intel_encoder(connector_state->best_encoder);
  8681. if (!encoders_cloneable(encoder, source_encoder))
  8682. return false;
  8683. }
  8684. return true;
  8685. }
  8686. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8687. struct drm_crtc_state *crtc_state)
  8688. {
  8689. struct drm_device *dev = crtc->dev;
  8690. struct drm_i915_private *dev_priv = to_i915(dev);
  8691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8692. struct intel_crtc_state *pipe_config =
  8693. to_intel_crtc_state(crtc_state);
  8694. struct drm_atomic_state *state = crtc_state->state;
  8695. int ret;
  8696. bool mode_changed = needs_modeset(crtc_state);
  8697. if (mode_changed && !crtc_state->active)
  8698. pipe_config->update_wm_post = true;
  8699. if (mode_changed && crtc_state->enable &&
  8700. dev_priv->display.crtc_compute_clock &&
  8701. !WARN_ON(pipe_config->shared_dpll)) {
  8702. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8703. pipe_config);
  8704. if (ret)
  8705. return ret;
  8706. }
  8707. if (crtc_state->color_mgmt_changed) {
  8708. ret = intel_color_check(crtc, crtc_state);
  8709. if (ret)
  8710. return ret;
  8711. /*
  8712. * Changing color management on Intel hardware is
  8713. * handled as part of planes update.
  8714. */
  8715. crtc_state->planes_changed = true;
  8716. }
  8717. ret = 0;
  8718. if (dev_priv->display.compute_pipe_wm) {
  8719. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8720. if (ret) {
  8721. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8722. return ret;
  8723. }
  8724. }
  8725. if (dev_priv->display.compute_intermediate_wm &&
  8726. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8727. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8728. return 0;
  8729. /*
  8730. * Calculate 'intermediate' watermarks that satisfy both the
  8731. * old state and the new state. We can program these
  8732. * immediately.
  8733. */
  8734. ret = dev_priv->display.compute_intermediate_wm(dev,
  8735. intel_crtc,
  8736. pipe_config);
  8737. if (ret) {
  8738. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8739. return ret;
  8740. }
  8741. } else if (dev_priv->display.compute_intermediate_wm) {
  8742. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8743. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8744. }
  8745. if (INTEL_GEN(dev_priv) >= 9) {
  8746. if (mode_changed)
  8747. ret = skl_update_scaler_crtc(pipe_config);
  8748. if (!ret)
  8749. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8750. pipe_config);
  8751. if (!ret)
  8752. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8753. pipe_config);
  8754. }
  8755. if (HAS_IPS(dev_priv))
  8756. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8757. return ret;
  8758. }
  8759. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8760. .atomic_begin = intel_begin_crtc_commit,
  8761. .atomic_flush = intel_finish_crtc_commit,
  8762. .atomic_check = intel_crtc_atomic_check,
  8763. };
  8764. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8765. {
  8766. struct intel_connector *connector;
  8767. struct drm_connector_list_iter conn_iter;
  8768. drm_connector_list_iter_begin(dev, &conn_iter);
  8769. for_each_intel_connector_iter(connector, &conn_iter) {
  8770. if (connector->base.state->crtc)
  8771. drm_connector_unreference(&connector->base);
  8772. if (connector->base.encoder) {
  8773. connector->base.state->best_encoder =
  8774. connector->base.encoder;
  8775. connector->base.state->crtc =
  8776. connector->base.encoder->crtc;
  8777. drm_connector_reference(&connector->base);
  8778. } else {
  8779. connector->base.state->best_encoder = NULL;
  8780. connector->base.state->crtc = NULL;
  8781. }
  8782. }
  8783. drm_connector_list_iter_end(&conn_iter);
  8784. }
  8785. static void
  8786. connected_sink_compute_bpp(struct intel_connector *connector,
  8787. struct intel_crtc_state *pipe_config)
  8788. {
  8789. const struct drm_display_info *info = &connector->base.display_info;
  8790. int bpp = pipe_config->pipe_bpp;
  8791. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8792. connector->base.base.id,
  8793. connector->base.name);
  8794. /* Don't use an invalid EDID bpc value */
  8795. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8796. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8797. bpp, info->bpc * 3);
  8798. pipe_config->pipe_bpp = info->bpc * 3;
  8799. }
  8800. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8801. if (info->bpc == 0 && bpp > 24) {
  8802. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8803. bpp);
  8804. pipe_config->pipe_bpp = 24;
  8805. }
  8806. }
  8807. static int
  8808. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8809. struct intel_crtc_state *pipe_config)
  8810. {
  8811. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8812. struct drm_atomic_state *state;
  8813. struct drm_connector *connector;
  8814. struct drm_connector_state *connector_state;
  8815. int bpp, i;
  8816. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8817. IS_CHERRYVIEW(dev_priv)))
  8818. bpp = 10*3;
  8819. else if (INTEL_GEN(dev_priv) >= 5)
  8820. bpp = 12*3;
  8821. else
  8822. bpp = 8*3;
  8823. pipe_config->pipe_bpp = bpp;
  8824. state = pipe_config->base.state;
  8825. /* Clamp display bpp to EDID value */
  8826. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8827. if (connector_state->crtc != &crtc->base)
  8828. continue;
  8829. connected_sink_compute_bpp(to_intel_connector(connector),
  8830. pipe_config);
  8831. }
  8832. return bpp;
  8833. }
  8834. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8835. {
  8836. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8837. "type: 0x%x flags: 0x%x\n",
  8838. mode->crtc_clock,
  8839. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8840. mode->crtc_hsync_end, mode->crtc_htotal,
  8841. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8842. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8843. }
  8844. static inline void
  8845. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8846. unsigned int lane_count, struct intel_link_m_n *m_n)
  8847. {
  8848. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8849. id, lane_count,
  8850. m_n->gmch_m, m_n->gmch_n,
  8851. m_n->link_m, m_n->link_n, m_n->tu);
  8852. }
  8853. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8854. static const char * const output_type_str[] = {
  8855. OUTPUT_TYPE(UNUSED),
  8856. OUTPUT_TYPE(ANALOG),
  8857. OUTPUT_TYPE(DVO),
  8858. OUTPUT_TYPE(SDVO),
  8859. OUTPUT_TYPE(LVDS),
  8860. OUTPUT_TYPE(TVOUT),
  8861. OUTPUT_TYPE(HDMI),
  8862. OUTPUT_TYPE(DP),
  8863. OUTPUT_TYPE(EDP),
  8864. OUTPUT_TYPE(DSI),
  8865. OUTPUT_TYPE(DDI),
  8866. OUTPUT_TYPE(DP_MST),
  8867. };
  8868. #undef OUTPUT_TYPE
  8869. static void snprintf_output_types(char *buf, size_t len,
  8870. unsigned int output_types)
  8871. {
  8872. char *str = buf;
  8873. int i;
  8874. str[0] = '\0';
  8875. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8876. int r;
  8877. if ((output_types & BIT(i)) == 0)
  8878. continue;
  8879. r = snprintf(str, len, "%s%s",
  8880. str != buf ? "," : "", output_type_str[i]);
  8881. if (r >= len)
  8882. break;
  8883. str += r;
  8884. len -= r;
  8885. output_types &= ~BIT(i);
  8886. }
  8887. WARN_ON_ONCE(output_types != 0);
  8888. }
  8889. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8890. struct intel_crtc_state *pipe_config,
  8891. const char *context)
  8892. {
  8893. struct drm_device *dev = crtc->base.dev;
  8894. struct drm_i915_private *dev_priv = to_i915(dev);
  8895. struct drm_plane *plane;
  8896. struct intel_plane *intel_plane;
  8897. struct intel_plane_state *state;
  8898. struct drm_framebuffer *fb;
  8899. char buf[64];
  8900. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8901. crtc->base.base.id, crtc->base.name, context);
  8902. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  8903. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  8904. buf, pipe_config->output_types);
  8905. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8906. transcoder_name(pipe_config->cpu_transcoder),
  8907. pipe_config->pipe_bpp, pipe_config->dither);
  8908. if (pipe_config->has_pch_encoder)
  8909. intel_dump_m_n_config(pipe_config, "fdi",
  8910. pipe_config->fdi_lanes,
  8911. &pipe_config->fdi_m_n);
  8912. if (pipe_config->ycbcr420)
  8913. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8914. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8915. intel_dump_m_n_config(pipe_config, "dp m_n",
  8916. pipe_config->lane_count, &pipe_config->dp_m_n);
  8917. if (pipe_config->has_drrs)
  8918. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8919. pipe_config->lane_count,
  8920. &pipe_config->dp_m2_n2);
  8921. }
  8922. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8923. pipe_config->has_audio, pipe_config->has_infoframe);
  8924. DRM_DEBUG_KMS("requested mode:\n");
  8925. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8926. DRM_DEBUG_KMS("adjusted mode:\n");
  8927. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8928. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8929. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8930. pipe_config->port_clock,
  8931. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8932. pipe_config->pixel_rate);
  8933. if (INTEL_GEN(dev_priv) >= 9)
  8934. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8935. crtc->num_scalers,
  8936. pipe_config->scaler_state.scaler_users,
  8937. pipe_config->scaler_state.scaler_id);
  8938. if (HAS_GMCH_DISPLAY(dev_priv))
  8939. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8940. pipe_config->gmch_pfit.control,
  8941. pipe_config->gmch_pfit.pgm_ratios,
  8942. pipe_config->gmch_pfit.lvds_border_bits);
  8943. else
  8944. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8945. pipe_config->pch_pfit.pos,
  8946. pipe_config->pch_pfit.size,
  8947. enableddisabled(pipe_config->pch_pfit.enabled));
  8948. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8949. pipe_config->ips_enabled, pipe_config->double_wide);
  8950. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8951. DRM_DEBUG_KMS("planes on this crtc\n");
  8952. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8953. struct drm_format_name_buf format_name;
  8954. intel_plane = to_intel_plane(plane);
  8955. if (intel_plane->pipe != crtc->pipe)
  8956. continue;
  8957. state = to_intel_plane_state(plane->state);
  8958. fb = state->base.fb;
  8959. if (!fb) {
  8960. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  8961. plane->base.id, plane->name, state->scaler_id);
  8962. continue;
  8963. }
  8964. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  8965. plane->base.id, plane->name,
  8966. fb->base.id, fb->width, fb->height,
  8967. drm_get_format_name(fb->format->format, &format_name));
  8968. if (INTEL_GEN(dev_priv) >= 9)
  8969. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  8970. state->scaler_id,
  8971. state->base.src.x1 >> 16,
  8972. state->base.src.y1 >> 16,
  8973. drm_rect_width(&state->base.src) >> 16,
  8974. drm_rect_height(&state->base.src) >> 16,
  8975. state->base.dst.x1, state->base.dst.y1,
  8976. drm_rect_width(&state->base.dst),
  8977. drm_rect_height(&state->base.dst));
  8978. }
  8979. }
  8980. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  8981. {
  8982. struct drm_device *dev = state->dev;
  8983. struct drm_connector *connector;
  8984. struct drm_connector_list_iter conn_iter;
  8985. unsigned int used_ports = 0;
  8986. unsigned int used_mst_ports = 0;
  8987. bool ret = true;
  8988. /*
  8989. * Walk the connector list instead of the encoder
  8990. * list to detect the problem on ddi platforms
  8991. * where there's just one encoder per digital port.
  8992. */
  8993. drm_connector_list_iter_begin(dev, &conn_iter);
  8994. drm_for_each_connector_iter(connector, &conn_iter) {
  8995. struct drm_connector_state *connector_state;
  8996. struct intel_encoder *encoder;
  8997. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  8998. if (!connector_state)
  8999. connector_state = connector->state;
  9000. if (!connector_state->best_encoder)
  9001. continue;
  9002. encoder = to_intel_encoder(connector_state->best_encoder);
  9003. WARN_ON(!connector_state->crtc);
  9004. switch (encoder->type) {
  9005. unsigned int port_mask;
  9006. case INTEL_OUTPUT_DDI:
  9007. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9008. break;
  9009. case INTEL_OUTPUT_DP:
  9010. case INTEL_OUTPUT_HDMI:
  9011. case INTEL_OUTPUT_EDP:
  9012. port_mask = 1 << encoder->port;
  9013. /* the same port mustn't appear more than once */
  9014. if (used_ports & port_mask)
  9015. ret = false;
  9016. used_ports |= port_mask;
  9017. break;
  9018. case INTEL_OUTPUT_DP_MST:
  9019. used_mst_ports |=
  9020. 1 << encoder->port;
  9021. break;
  9022. default:
  9023. break;
  9024. }
  9025. }
  9026. drm_connector_list_iter_end(&conn_iter);
  9027. /* can't mix MST and SST/HDMI on the same port */
  9028. if (used_ports & used_mst_ports)
  9029. return false;
  9030. return ret;
  9031. }
  9032. static void
  9033. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9034. {
  9035. struct drm_i915_private *dev_priv =
  9036. to_i915(crtc_state->base.crtc->dev);
  9037. struct intel_crtc_scaler_state scaler_state;
  9038. struct intel_dpll_hw_state dpll_hw_state;
  9039. struct intel_shared_dpll *shared_dpll;
  9040. struct intel_crtc_wm_state wm_state;
  9041. bool force_thru, ips_force_disable;
  9042. /* FIXME: before the switch to atomic started, a new pipe_config was
  9043. * kzalloc'd. Code that depends on any field being zero should be
  9044. * fixed, so that the crtc_state can be safely duplicated. For now,
  9045. * only fields that are know to not cause problems are preserved. */
  9046. scaler_state = crtc_state->scaler_state;
  9047. shared_dpll = crtc_state->shared_dpll;
  9048. dpll_hw_state = crtc_state->dpll_hw_state;
  9049. force_thru = crtc_state->pch_pfit.force_thru;
  9050. ips_force_disable = crtc_state->ips_force_disable;
  9051. if (IS_G4X(dev_priv) ||
  9052. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9053. wm_state = crtc_state->wm;
  9054. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9055. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9056. memset(&crtc_state->base + 1, 0,
  9057. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9058. crtc_state->scaler_state = scaler_state;
  9059. crtc_state->shared_dpll = shared_dpll;
  9060. crtc_state->dpll_hw_state = dpll_hw_state;
  9061. crtc_state->pch_pfit.force_thru = force_thru;
  9062. crtc_state->ips_force_disable = ips_force_disable;
  9063. if (IS_G4X(dev_priv) ||
  9064. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9065. crtc_state->wm = wm_state;
  9066. }
  9067. static int
  9068. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9069. struct intel_crtc_state *pipe_config)
  9070. {
  9071. struct drm_atomic_state *state = pipe_config->base.state;
  9072. struct intel_encoder *encoder;
  9073. struct drm_connector *connector;
  9074. struct drm_connector_state *connector_state;
  9075. int base_bpp, ret = -EINVAL;
  9076. int i;
  9077. bool retry = true;
  9078. clear_intel_crtc_state(pipe_config);
  9079. pipe_config->cpu_transcoder =
  9080. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9081. /*
  9082. * Sanitize sync polarity flags based on requested ones. If neither
  9083. * positive or negative polarity is requested, treat this as meaning
  9084. * negative polarity.
  9085. */
  9086. if (!(pipe_config->base.adjusted_mode.flags &
  9087. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9088. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9089. if (!(pipe_config->base.adjusted_mode.flags &
  9090. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9091. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9092. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9093. pipe_config);
  9094. if (base_bpp < 0)
  9095. goto fail;
  9096. /*
  9097. * Determine the real pipe dimensions. Note that stereo modes can
  9098. * increase the actual pipe size due to the frame doubling and
  9099. * insertion of additional space for blanks between the frame. This
  9100. * is stored in the crtc timings. We use the requested mode to do this
  9101. * computation to clearly distinguish it from the adjusted mode, which
  9102. * can be changed by the connectors in the below retry loop.
  9103. */
  9104. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9105. &pipe_config->pipe_src_w,
  9106. &pipe_config->pipe_src_h);
  9107. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9108. if (connector_state->crtc != crtc)
  9109. continue;
  9110. encoder = to_intel_encoder(connector_state->best_encoder);
  9111. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9112. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9113. goto fail;
  9114. }
  9115. /*
  9116. * Determine output_types before calling the .compute_config()
  9117. * hooks so that the hooks can use this information safely.
  9118. */
  9119. if (encoder->compute_output_type)
  9120. pipe_config->output_types |=
  9121. BIT(encoder->compute_output_type(encoder, pipe_config,
  9122. connector_state));
  9123. else
  9124. pipe_config->output_types |= BIT(encoder->type);
  9125. }
  9126. encoder_retry:
  9127. /* Ensure the port clock defaults are reset when retrying. */
  9128. pipe_config->port_clock = 0;
  9129. pipe_config->pixel_multiplier = 1;
  9130. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9131. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9132. CRTC_STEREO_DOUBLE);
  9133. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9134. * adjust it according to limitations or connector properties, and also
  9135. * a chance to reject the mode entirely.
  9136. */
  9137. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9138. if (connector_state->crtc != crtc)
  9139. continue;
  9140. encoder = to_intel_encoder(connector_state->best_encoder);
  9141. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9142. DRM_DEBUG_KMS("Encoder config failure\n");
  9143. goto fail;
  9144. }
  9145. }
  9146. /* Set default port clock if not overwritten by the encoder. Needs to be
  9147. * done afterwards in case the encoder adjusts the mode. */
  9148. if (!pipe_config->port_clock)
  9149. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9150. * pipe_config->pixel_multiplier;
  9151. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9152. if (ret < 0) {
  9153. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9154. goto fail;
  9155. }
  9156. if (ret == RETRY) {
  9157. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9158. ret = -EINVAL;
  9159. goto fail;
  9160. }
  9161. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9162. retry = false;
  9163. goto encoder_retry;
  9164. }
  9165. /* Dithering seems to not pass-through bits correctly when it should, so
  9166. * only enable it on 6bpc panels and when its not a compliance
  9167. * test requesting 6bpc video pattern.
  9168. */
  9169. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9170. !pipe_config->dither_force_disable;
  9171. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9172. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9173. fail:
  9174. return ret;
  9175. }
  9176. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9177. {
  9178. int diff;
  9179. if (clock1 == clock2)
  9180. return true;
  9181. if (!clock1 || !clock2)
  9182. return false;
  9183. diff = abs(clock1 - clock2);
  9184. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9185. return true;
  9186. return false;
  9187. }
  9188. static bool
  9189. intel_compare_m_n(unsigned int m, unsigned int n,
  9190. unsigned int m2, unsigned int n2,
  9191. bool exact)
  9192. {
  9193. if (m == m2 && n == n2)
  9194. return true;
  9195. if (exact || !m || !n || !m2 || !n2)
  9196. return false;
  9197. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9198. if (n > n2) {
  9199. while (n > n2) {
  9200. m2 <<= 1;
  9201. n2 <<= 1;
  9202. }
  9203. } else if (n < n2) {
  9204. while (n < n2) {
  9205. m <<= 1;
  9206. n <<= 1;
  9207. }
  9208. }
  9209. if (n != n2)
  9210. return false;
  9211. return intel_fuzzy_clock_check(m, m2);
  9212. }
  9213. static bool
  9214. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9215. struct intel_link_m_n *m2_n2,
  9216. bool adjust)
  9217. {
  9218. if (m_n->tu == m2_n2->tu &&
  9219. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9220. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9221. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9222. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9223. if (adjust)
  9224. *m2_n2 = *m_n;
  9225. return true;
  9226. }
  9227. return false;
  9228. }
  9229. static void __printf(3, 4)
  9230. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9231. {
  9232. struct va_format vaf;
  9233. va_list args;
  9234. va_start(args, format);
  9235. vaf.fmt = format;
  9236. vaf.va = &args;
  9237. if (adjust)
  9238. drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
  9239. else
  9240. drm_err("mismatch in %s %pV", name, &vaf);
  9241. va_end(args);
  9242. }
  9243. static bool
  9244. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9245. struct intel_crtc_state *current_config,
  9246. struct intel_crtc_state *pipe_config,
  9247. bool adjust)
  9248. {
  9249. bool ret = true;
  9250. bool fixup_inherited = adjust &&
  9251. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9252. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9253. #define PIPE_CONF_CHECK_X(name) \
  9254. if (current_config->name != pipe_config->name) { \
  9255. pipe_config_err(adjust, __stringify(name), \
  9256. "(expected 0x%08x, found 0x%08x)\n", \
  9257. current_config->name, \
  9258. pipe_config->name); \
  9259. ret = false; \
  9260. }
  9261. #define PIPE_CONF_CHECK_I(name) \
  9262. if (current_config->name != pipe_config->name) { \
  9263. pipe_config_err(adjust, __stringify(name), \
  9264. "(expected %i, found %i)\n", \
  9265. current_config->name, \
  9266. pipe_config->name); \
  9267. ret = false; \
  9268. }
  9269. #define PIPE_CONF_CHECK_BOOL(name) \
  9270. if (current_config->name != pipe_config->name) { \
  9271. pipe_config_err(adjust, __stringify(name), \
  9272. "(expected %s, found %s)\n", \
  9273. yesno(current_config->name), \
  9274. yesno(pipe_config->name)); \
  9275. ret = false; \
  9276. }
  9277. /*
  9278. * Checks state where we only read out the enabling, but not the entire
  9279. * state itself (like full infoframes or ELD for audio). These states
  9280. * require a full modeset on bootup to fix up.
  9281. */
  9282. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) \
  9283. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9284. PIPE_CONF_CHECK_BOOL(name); \
  9285. } else { \
  9286. pipe_config_err(adjust, __stringify(name), \
  9287. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9288. yesno(current_config->name), \
  9289. yesno(pipe_config->name)); \
  9290. ret = false; \
  9291. }
  9292. #define PIPE_CONF_CHECK_P(name) \
  9293. if (current_config->name != pipe_config->name) { \
  9294. pipe_config_err(adjust, __stringify(name), \
  9295. "(expected %p, found %p)\n", \
  9296. current_config->name, \
  9297. pipe_config->name); \
  9298. ret = false; \
  9299. }
  9300. #define PIPE_CONF_CHECK_M_N(name) \
  9301. if (!intel_compare_link_m_n(&current_config->name, \
  9302. &pipe_config->name,\
  9303. adjust)) { \
  9304. pipe_config_err(adjust, __stringify(name), \
  9305. "(expected tu %i gmch %i/%i link %i/%i, " \
  9306. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9307. current_config->name.tu, \
  9308. current_config->name.gmch_m, \
  9309. current_config->name.gmch_n, \
  9310. current_config->name.link_m, \
  9311. current_config->name.link_n, \
  9312. pipe_config->name.tu, \
  9313. pipe_config->name.gmch_m, \
  9314. pipe_config->name.gmch_n, \
  9315. pipe_config->name.link_m, \
  9316. pipe_config->name.link_n); \
  9317. ret = false; \
  9318. }
  9319. /* This is required for BDW+ where there is only one set of registers for
  9320. * switching between high and low RR.
  9321. * This macro can be used whenever a comparison has to be made between one
  9322. * hw state and multiple sw state variables.
  9323. */
  9324. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9325. if (!intel_compare_link_m_n(&current_config->name, \
  9326. &pipe_config->name, adjust) && \
  9327. !intel_compare_link_m_n(&current_config->alt_name, \
  9328. &pipe_config->name, adjust)) { \
  9329. pipe_config_err(adjust, __stringify(name), \
  9330. "(expected tu %i gmch %i/%i link %i/%i, " \
  9331. "or tu %i gmch %i/%i link %i/%i, " \
  9332. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9333. current_config->name.tu, \
  9334. current_config->name.gmch_m, \
  9335. current_config->name.gmch_n, \
  9336. current_config->name.link_m, \
  9337. current_config->name.link_n, \
  9338. current_config->alt_name.tu, \
  9339. current_config->alt_name.gmch_m, \
  9340. current_config->alt_name.gmch_n, \
  9341. current_config->alt_name.link_m, \
  9342. current_config->alt_name.link_n, \
  9343. pipe_config->name.tu, \
  9344. pipe_config->name.gmch_m, \
  9345. pipe_config->name.gmch_n, \
  9346. pipe_config->name.link_m, \
  9347. pipe_config->name.link_n); \
  9348. ret = false; \
  9349. }
  9350. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9351. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9352. pipe_config_err(adjust, __stringify(name), \
  9353. "(%x) (expected %i, found %i)\n", \
  9354. (mask), \
  9355. current_config->name & (mask), \
  9356. pipe_config->name & (mask)); \
  9357. ret = false; \
  9358. }
  9359. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9360. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9361. pipe_config_err(adjust, __stringify(name), \
  9362. "(expected %i, found %i)\n", \
  9363. current_config->name, \
  9364. pipe_config->name); \
  9365. ret = false; \
  9366. }
  9367. #define PIPE_CONF_QUIRK(quirk) \
  9368. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9369. PIPE_CONF_CHECK_I(cpu_transcoder);
  9370. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9371. PIPE_CONF_CHECK_I(fdi_lanes);
  9372. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9373. PIPE_CONF_CHECK_I(lane_count);
  9374. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9375. if (INTEL_GEN(dev_priv) < 8) {
  9376. PIPE_CONF_CHECK_M_N(dp_m_n);
  9377. if (current_config->has_drrs)
  9378. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9379. } else
  9380. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9381. PIPE_CONF_CHECK_X(output_types);
  9382. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9383. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9384. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9385. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9386. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9387. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9388. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9389. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9390. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9391. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9392. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9393. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9394. PIPE_CONF_CHECK_I(pixel_multiplier);
  9395. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9396. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9397. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9398. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9399. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9400. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9401. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9402. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9403. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9404. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9405. DRM_MODE_FLAG_INTERLACE);
  9406. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9407. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9408. DRM_MODE_FLAG_PHSYNC);
  9409. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9410. DRM_MODE_FLAG_NHSYNC);
  9411. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9412. DRM_MODE_FLAG_PVSYNC);
  9413. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9414. DRM_MODE_FLAG_NVSYNC);
  9415. }
  9416. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9417. /* pfit ratios are autocomputed by the hw on gen4+ */
  9418. if (INTEL_GEN(dev_priv) < 4)
  9419. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9420. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9421. if (!adjust) {
  9422. PIPE_CONF_CHECK_I(pipe_src_w);
  9423. PIPE_CONF_CHECK_I(pipe_src_h);
  9424. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9425. if (current_config->pch_pfit.enabled) {
  9426. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9427. PIPE_CONF_CHECK_X(pch_pfit.size);
  9428. }
  9429. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9430. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9431. }
  9432. PIPE_CONF_CHECK_BOOL(double_wide);
  9433. PIPE_CONF_CHECK_P(shared_dpll);
  9434. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9435. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9436. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9437. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9438. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9439. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9440. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9441. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9442. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9443. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9444. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9445. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9446. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9447. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9448. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9449. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9450. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9451. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9452. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9453. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9454. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9455. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9456. PIPE_CONF_CHECK_X(dsi_pll.div);
  9457. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9458. PIPE_CONF_CHECK_I(pipe_bpp);
  9459. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9460. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9461. PIPE_CONF_CHECK_I(min_voltage_level);
  9462. #undef PIPE_CONF_CHECK_X
  9463. #undef PIPE_CONF_CHECK_I
  9464. #undef PIPE_CONF_CHECK_BOOL
  9465. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9466. #undef PIPE_CONF_CHECK_P
  9467. #undef PIPE_CONF_CHECK_FLAGS
  9468. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9469. #undef PIPE_CONF_QUIRK
  9470. return ret;
  9471. }
  9472. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9473. const struct intel_crtc_state *pipe_config)
  9474. {
  9475. if (pipe_config->has_pch_encoder) {
  9476. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9477. &pipe_config->fdi_m_n);
  9478. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9479. /*
  9480. * FDI already provided one idea for the dotclock.
  9481. * Yell if the encoder disagrees.
  9482. */
  9483. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9484. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9485. fdi_dotclock, dotclock);
  9486. }
  9487. }
  9488. static void verify_wm_state(struct drm_crtc *crtc,
  9489. struct drm_crtc_state *new_state)
  9490. {
  9491. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9492. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9493. struct skl_pipe_wm hw_wm, *sw_wm;
  9494. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9495. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9496. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9497. const enum pipe pipe = intel_crtc->pipe;
  9498. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9499. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9500. return;
  9501. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9502. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9503. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9504. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9505. /* planes */
  9506. for_each_universal_plane(dev_priv, pipe, plane) {
  9507. hw_plane_wm = &hw_wm.planes[plane];
  9508. sw_plane_wm = &sw_wm->planes[plane];
  9509. /* Watermarks */
  9510. for (level = 0; level <= max_level; level++) {
  9511. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9512. &sw_plane_wm->wm[level]))
  9513. continue;
  9514. 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",
  9515. pipe_name(pipe), plane + 1, level,
  9516. sw_plane_wm->wm[level].plane_en,
  9517. sw_plane_wm->wm[level].plane_res_b,
  9518. sw_plane_wm->wm[level].plane_res_l,
  9519. hw_plane_wm->wm[level].plane_en,
  9520. hw_plane_wm->wm[level].plane_res_b,
  9521. hw_plane_wm->wm[level].plane_res_l);
  9522. }
  9523. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9524. &sw_plane_wm->trans_wm)) {
  9525. 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",
  9526. pipe_name(pipe), plane + 1,
  9527. sw_plane_wm->trans_wm.plane_en,
  9528. sw_plane_wm->trans_wm.plane_res_b,
  9529. sw_plane_wm->trans_wm.plane_res_l,
  9530. hw_plane_wm->trans_wm.plane_en,
  9531. hw_plane_wm->trans_wm.plane_res_b,
  9532. hw_plane_wm->trans_wm.plane_res_l);
  9533. }
  9534. /* DDB */
  9535. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9536. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9537. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9538. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9539. pipe_name(pipe), plane + 1,
  9540. sw_ddb_entry->start, sw_ddb_entry->end,
  9541. hw_ddb_entry->start, hw_ddb_entry->end);
  9542. }
  9543. }
  9544. /*
  9545. * cursor
  9546. * If the cursor plane isn't active, we may not have updated it's ddb
  9547. * allocation. In that case since the ddb allocation will be updated
  9548. * once the plane becomes visible, we can skip this check
  9549. */
  9550. if (1) {
  9551. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9552. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9553. /* Watermarks */
  9554. for (level = 0; level <= max_level; level++) {
  9555. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9556. &sw_plane_wm->wm[level]))
  9557. continue;
  9558. 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",
  9559. pipe_name(pipe), level,
  9560. sw_plane_wm->wm[level].plane_en,
  9561. sw_plane_wm->wm[level].plane_res_b,
  9562. sw_plane_wm->wm[level].plane_res_l,
  9563. hw_plane_wm->wm[level].plane_en,
  9564. hw_plane_wm->wm[level].plane_res_b,
  9565. hw_plane_wm->wm[level].plane_res_l);
  9566. }
  9567. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9568. &sw_plane_wm->trans_wm)) {
  9569. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9570. pipe_name(pipe),
  9571. sw_plane_wm->trans_wm.plane_en,
  9572. sw_plane_wm->trans_wm.plane_res_b,
  9573. sw_plane_wm->trans_wm.plane_res_l,
  9574. hw_plane_wm->trans_wm.plane_en,
  9575. hw_plane_wm->trans_wm.plane_res_b,
  9576. hw_plane_wm->trans_wm.plane_res_l);
  9577. }
  9578. /* DDB */
  9579. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9580. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9581. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9582. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9583. pipe_name(pipe),
  9584. sw_ddb_entry->start, sw_ddb_entry->end,
  9585. hw_ddb_entry->start, hw_ddb_entry->end);
  9586. }
  9587. }
  9588. }
  9589. static void
  9590. verify_connector_state(struct drm_device *dev,
  9591. struct drm_atomic_state *state,
  9592. struct drm_crtc *crtc)
  9593. {
  9594. struct drm_connector *connector;
  9595. struct drm_connector_state *new_conn_state;
  9596. int i;
  9597. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9598. struct drm_encoder *encoder = connector->encoder;
  9599. struct drm_crtc_state *crtc_state = NULL;
  9600. if (new_conn_state->crtc != crtc)
  9601. continue;
  9602. if (crtc)
  9603. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9604. intel_connector_verify_state(crtc_state, new_conn_state);
  9605. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9606. "connector's atomic encoder doesn't match legacy encoder\n");
  9607. }
  9608. }
  9609. static void
  9610. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9611. {
  9612. struct intel_encoder *encoder;
  9613. struct drm_connector *connector;
  9614. struct drm_connector_state *old_conn_state, *new_conn_state;
  9615. int i;
  9616. for_each_intel_encoder(dev, encoder) {
  9617. bool enabled = false, found = false;
  9618. enum pipe pipe;
  9619. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9620. encoder->base.base.id,
  9621. encoder->base.name);
  9622. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9623. new_conn_state, i) {
  9624. if (old_conn_state->best_encoder == &encoder->base)
  9625. found = true;
  9626. if (new_conn_state->best_encoder != &encoder->base)
  9627. continue;
  9628. found = enabled = true;
  9629. I915_STATE_WARN(new_conn_state->crtc !=
  9630. encoder->base.crtc,
  9631. "connector's crtc doesn't match encoder crtc\n");
  9632. }
  9633. if (!found)
  9634. continue;
  9635. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9636. "encoder's enabled state mismatch "
  9637. "(expected %i, found %i)\n",
  9638. !!encoder->base.crtc, enabled);
  9639. if (!encoder->base.crtc) {
  9640. bool active;
  9641. active = encoder->get_hw_state(encoder, &pipe);
  9642. I915_STATE_WARN(active,
  9643. "encoder detached but still enabled on pipe %c.\n",
  9644. pipe_name(pipe));
  9645. }
  9646. }
  9647. }
  9648. static void
  9649. verify_crtc_state(struct drm_crtc *crtc,
  9650. struct drm_crtc_state *old_crtc_state,
  9651. struct drm_crtc_state *new_crtc_state)
  9652. {
  9653. struct drm_device *dev = crtc->dev;
  9654. struct drm_i915_private *dev_priv = to_i915(dev);
  9655. struct intel_encoder *encoder;
  9656. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9657. struct intel_crtc_state *pipe_config, *sw_config;
  9658. struct drm_atomic_state *old_state;
  9659. bool active;
  9660. old_state = old_crtc_state->state;
  9661. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9662. pipe_config = to_intel_crtc_state(old_crtc_state);
  9663. memset(pipe_config, 0, sizeof(*pipe_config));
  9664. pipe_config->base.crtc = crtc;
  9665. pipe_config->base.state = old_state;
  9666. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9667. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9668. /* we keep both pipes enabled on 830 */
  9669. if (IS_I830(dev_priv))
  9670. active = new_crtc_state->active;
  9671. I915_STATE_WARN(new_crtc_state->active != active,
  9672. "crtc active state doesn't match with hw state "
  9673. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9674. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9675. "transitional active state does not match atomic hw state "
  9676. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9677. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9678. enum pipe pipe;
  9679. active = encoder->get_hw_state(encoder, &pipe);
  9680. I915_STATE_WARN(active != new_crtc_state->active,
  9681. "[ENCODER:%i] active %i with crtc active %i\n",
  9682. encoder->base.base.id, active, new_crtc_state->active);
  9683. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9684. "Encoder connected to wrong pipe %c\n",
  9685. pipe_name(pipe));
  9686. if (active)
  9687. encoder->get_config(encoder, pipe_config);
  9688. }
  9689. intel_crtc_compute_pixel_rate(pipe_config);
  9690. if (!new_crtc_state->active)
  9691. return;
  9692. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9693. sw_config = to_intel_crtc_state(new_crtc_state);
  9694. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9695. pipe_config, false)) {
  9696. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9697. intel_dump_pipe_config(intel_crtc, pipe_config,
  9698. "[hw state]");
  9699. intel_dump_pipe_config(intel_crtc, sw_config,
  9700. "[sw state]");
  9701. }
  9702. }
  9703. static void
  9704. intel_verify_planes(struct intel_atomic_state *state)
  9705. {
  9706. struct intel_plane *plane;
  9707. const struct intel_plane_state *plane_state;
  9708. int i;
  9709. for_each_new_intel_plane_in_state(state, plane,
  9710. plane_state, i)
  9711. assert_plane(plane, plane_state->base.visible);
  9712. }
  9713. static void
  9714. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9715. struct intel_shared_dpll *pll,
  9716. struct drm_crtc *crtc,
  9717. struct drm_crtc_state *new_state)
  9718. {
  9719. struct intel_dpll_hw_state dpll_hw_state;
  9720. unsigned crtc_mask;
  9721. bool active;
  9722. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9723. DRM_DEBUG_KMS("%s\n", pll->name);
  9724. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9725. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9726. I915_STATE_WARN(!pll->on && pll->active_mask,
  9727. "pll in active use but not on in sw tracking\n");
  9728. I915_STATE_WARN(pll->on && !pll->active_mask,
  9729. "pll is on but not used by any active crtc\n");
  9730. I915_STATE_WARN(pll->on != active,
  9731. "pll on state mismatch (expected %i, found %i)\n",
  9732. pll->on, active);
  9733. }
  9734. if (!crtc) {
  9735. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9736. "more active pll users than references: %x vs %x\n",
  9737. pll->active_mask, pll->state.crtc_mask);
  9738. return;
  9739. }
  9740. crtc_mask = 1 << drm_crtc_index(crtc);
  9741. if (new_state->active)
  9742. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9743. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9744. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9745. else
  9746. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9747. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9748. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9749. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9750. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9751. crtc_mask, pll->state.crtc_mask);
  9752. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9753. &dpll_hw_state,
  9754. sizeof(dpll_hw_state)),
  9755. "pll hw state mismatch\n");
  9756. }
  9757. static void
  9758. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9759. struct drm_crtc_state *old_crtc_state,
  9760. struct drm_crtc_state *new_crtc_state)
  9761. {
  9762. struct drm_i915_private *dev_priv = to_i915(dev);
  9763. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9764. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9765. if (new_state->shared_dpll)
  9766. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9767. if (old_state->shared_dpll &&
  9768. old_state->shared_dpll != new_state->shared_dpll) {
  9769. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9770. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9771. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9772. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9773. pipe_name(drm_crtc_index(crtc)));
  9774. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9775. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9776. pipe_name(drm_crtc_index(crtc)));
  9777. }
  9778. }
  9779. static void
  9780. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9781. struct drm_atomic_state *state,
  9782. struct drm_crtc_state *old_state,
  9783. struct drm_crtc_state *new_state)
  9784. {
  9785. if (!needs_modeset(new_state) &&
  9786. !to_intel_crtc_state(new_state)->update_pipe)
  9787. return;
  9788. verify_wm_state(crtc, new_state);
  9789. verify_connector_state(crtc->dev, state, crtc);
  9790. verify_crtc_state(crtc, old_state, new_state);
  9791. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9792. }
  9793. static void
  9794. verify_disabled_dpll_state(struct drm_device *dev)
  9795. {
  9796. struct drm_i915_private *dev_priv = to_i915(dev);
  9797. int i;
  9798. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9799. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9800. }
  9801. static void
  9802. intel_modeset_verify_disabled(struct drm_device *dev,
  9803. struct drm_atomic_state *state)
  9804. {
  9805. verify_encoder_state(dev, state);
  9806. verify_connector_state(dev, state, NULL);
  9807. verify_disabled_dpll_state(dev);
  9808. }
  9809. static void update_scanline_offset(struct intel_crtc *crtc)
  9810. {
  9811. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9812. /*
  9813. * The scanline counter increments at the leading edge of hsync.
  9814. *
  9815. * On most platforms it starts counting from vtotal-1 on the
  9816. * first active line. That means the scanline counter value is
  9817. * always one less than what we would expect. Ie. just after
  9818. * start of vblank, which also occurs at start of hsync (on the
  9819. * last active line), the scanline counter will read vblank_start-1.
  9820. *
  9821. * On gen2 the scanline counter starts counting from 1 instead
  9822. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9823. * to keep the value positive), instead of adding one.
  9824. *
  9825. * On HSW+ the behaviour of the scanline counter depends on the output
  9826. * type. For DP ports it behaves like most other platforms, but on HDMI
  9827. * there's an extra 1 line difference. So we need to add two instead of
  9828. * one to the value.
  9829. *
  9830. * On VLV/CHV DSI the scanline counter would appear to increment
  9831. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9832. * that means we can't tell whether we're in vblank or not while
  9833. * we're on that particular line. We must still set scanline_offset
  9834. * to 1 so that the vblank timestamps come out correct when we query
  9835. * the scanline counter from within the vblank interrupt handler.
  9836. * However if queried just before the start of vblank we'll get an
  9837. * answer that's slightly in the future.
  9838. */
  9839. if (IS_GEN2(dev_priv)) {
  9840. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9841. int vtotal;
  9842. vtotal = adjusted_mode->crtc_vtotal;
  9843. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9844. vtotal /= 2;
  9845. crtc->scanline_offset = vtotal - 1;
  9846. } else if (HAS_DDI(dev_priv) &&
  9847. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9848. crtc->scanline_offset = 2;
  9849. } else
  9850. crtc->scanline_offset = 1;
  9851. }
  9852. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9853. {
  9854. struct drm_device *dev = state->dev;
  9855. struct drm_i915_private *dev_priv = to_i915(dev);
  9856. struct drm_crtc *crtc;
  9857. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9858. int i;
  9859. if (!dev_priv->display.crtc_compute_clock)
  9860. return;
  9861. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9863. struct intel_shared_dpll *old_dpll =
  9864. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9865. if (!needs_modeset(new_crtc_state))
  9866. continue;
  9867. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9868. if (!old_dpll)
  9869. continue;
  9870. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9871. }
  9872. }
  9873. /*
  9874. * This implements the workaround described in the "notes" section of the mode
  9875. * set sequence documentation. When going from no pipes or single pipe to
  9876. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9877. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9878. */
  9879. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9880. {
  9881. struct drm_crtc_state *crtc_state;
  9882. struct intel_crtc *intel_crtc;
  9883. struct drm_crtc *crtc;
  9884. struct intel_crtc_state *first_crtc_state = NULL;
  9885. struct intel_crtc_state *other_crtc_state = NULL;
  9886. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9887. int i;
  9888. /* look at all crtc's that are going to be enabled in during modeset */
  9889. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9890. intel_crtc = to_intel_crtc(crtc);
  9891. if (!crtc_state->active || !needs_modeset(crtc_state))
  9892. continue;
  9893. if (first_crtc_state) {
  9894. other_crtc_state = to_intel_crtc_state(crtc_state);
  9895. break;
  9896. } else {
  9897. first_crtc_state = to_intel_crtc_state(crtc_state);
  9898. first_pipe = intel_crtc->pipe;
  9899. }
  9900. }
  9901. /* No workaround needed? */
  9902. if (!first_crtc_state)
  9903. return 0;
  9904. /* w/a possibly needed, check how many crtc's are already enabled. */
  9905. for_each_intel_crtc(state->dev, intel_crtc) {
  9906. struct intel_crtc_state *pipe_config;
  9907. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9908. if (IS_ERR(pipe_config))
  9909. return PTR_ERR(pipe_config);
  9910. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9911. if (!pipe_config->base.active ||
  9912. needs_modeset(&pipe_config->base))
  9913. continue;
  9914. /* 2 or more enabled crtcs means no need for w/a */
  9915. if (enabled_pipe != INVALID_PIPE)
  9916. return 0;
  9917. enabled_pipe = intel_crtc->pipe;
  9918. }
  9919. if (enabled_pipe != INVALID_PIPE)
  9920. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9921. else if (other_crtc_state)
  9922. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9923. return 0;
  9924. }
  9925. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9926. {
  9927. struct drm_crtc *crtc;
  9928. /* Add all pipes to the state */
  9929. for_each_crtc(state->dev, crtc) {
  9930. struct drm_crtc_state *crtc_state;
  9931. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9932. if (IS_ERR(crtc_state))
  9933. return PTR_ERR(crtc_state);
  9934. }
  9935. return 0;
  9936. }
  9937. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9938. {
  9939. struct drm_crtc *crtc;
  9940. /*
  9941. * Add all pipes to the state, and force
  9942. * a modeset on all the active ones.
  9943. */
  9944. for_each_crtc(state->dev, crtc) {
  9945. struct drm_crtc_state *crtc_state;
  9946. int ret;
  9947. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9948. if (IS_ERR(crtc_state))
  9949. return PTR_ERR(crtc_state);
  9950. if (!crtc_state->active || needs_modeset(crtc_state))
  9951. continue;
  9952. crtc_state->mode_changed = true;
  9953. ret = drm_atomic_add_affected_connectors(state, crtc);
  9954. if (ret)
  9955. return ret;
  9956. ret = drm_atomic_add_affected_planes(state, crtc);
  9957. if (ret)
  9958. return ret;
  9959. }
  9960. return 0;
  9961. }
  9962. static int intel_modeset_checks(struct drm_atomic_state *state)
  9963. {
  9964. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9965. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9966. struct drm_crtc *crtc;
  9967. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9968. int ret = 0, i;
  9969. if (!check_digital_port_conflicts(state)) {
  9970. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9971. return -EINVAL;
  9972. }
  9973. intel_state->modeset = true;
  9974. intel_state->active_crtcs = dev_priv->active_crtcs;
  9975. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9976. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  9977. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9978. if (new_crtc_state->active)
  9979. intel_state->active_crtcs |= 1 << i;
  9980. else
  9981. intel_state->active_crtcs &= ~(1 << i);
  9982. if (old_crtc_state->active != new_crtc_state->active)
  9983. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  9984. }
  9985. /*
  9986. * See if the config requires any additional preparation, e.g.
  9987. * to adjust global state with pipes off. We need to do this
  9988. * here so we can get the modeset_pipe updated config for the new
  9989. * mode set on this crtc. For other crtcs we need to use the
  9990. * adjusted_mode bits in the crtc directly.
  9991. */
  9992. if (dev_priv->display.modeset_calc_cdclk) {
  9993. ret = dev_priv->display.modeset_calc_cdclk(state);
  9994. if (ret < 0)
  9995. return ret;
  9996. /*
  9997. * Writes to dev_priv->cdclk.logical must protected by
  9998. * holding all the crtc locks, even if we don't end up
  9999. * touching the hardware
  10000. */
  10001. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10002. &intel_state->cdclk.logical)) {
  10003. ret = intel_lock_all_pipes(state);
  10004. if (ret < 0)
  10005. return ret;
  10006. }
  10007. /* All pipes must be switched off while we change the cdclk. */
  10008. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10009. &intel_state->cdclk.actual)) {
  10010. ret = intel_modeset_all_pipes(state);
  10011. if (ret < 0)
  10012. return ret;
  10013. }
  10014. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10015. intel_state->cdclk.logical.cdclk,
  10016. intel_state->cdclk.actual.cdclk);
  10017. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10018. intel_state->cdclk.logical.voltage_level,
  10019. intel_state->cdclk.actual.voltage_level);
  10020. } else {
  10021. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10022. }
  10023. intel_modeset_clear_plls(state);
  10024. if (IS_HASWELL(dev_priv))
  10025. return haswell_mode_set_planes_workaround(state);
  10026. return 0;
  10027. }
  10028. /*
  10029. * Handle calculation of various watermark data at the end of the atomic check
  10030. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10031. * handlers to ensure that all derived state has been updated.
  10032. */
  10033. static int calc_watermark_data(struct drm_atomic_state *state)
  10034. {
  10035. struct drm_device *dev = state->dev;
  10036. struct drm_i915_private *dev_priv = to_i915(dev);
  10037. /* Is there platform-specific watermark information to calculate? */
  10038. if (dev_priv->display.compute_global_watermarks)
  10039. return dev_priv->display.compute_global_watermarks(state);
  10040. return 0;
  10041. }
  10042. /**
  10043. * intel_atomic_check - validate state object
  10044. * @dev: drm device
  10045. * @state: state to validate
  10046. */
  10047. static int intel_atomic_check(struct drm_device *dev,
  10048. struct drm_atomic_state *state)
  10049. {
  10050. struct drm_i915_private *dev_priv = to_i915(dev);
  10051. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10052. struct drm_crtc *crtc;
  10053. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10054. int ret, i;
  10055. bool any_ms = false;
  10056. /* Catch I915_MODE_FLAG_INHERITED */
  10057. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  10058. crtc_state, i) {
  10059. if (crtc_state->mode.private_flags !=
  10060. old_crtc_state->mode.private_flags)
  10061. crtc_state->mode_changed = true;
  10062. }
  10063. ret = drm_atomic_helper_check_modeset(dev, state);
  10064. if (ret)
  10065. return ret;
  10066. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10067. struct intel_crtc_state *pipe_config =
  10068. to_intel_crtc_state(crtc_state);
  10069. if (!needs_modeset(crtc_state))
  10070. continue;
  10071. if (!crtc_state->enable) {
  10072. any_ms = true;
  10073. continue;
  10074. }
  10075. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10076. if (ret) {
  10077. intel_dump_pipe_config(to_intel_crtc(crtc),
  10078. pipe_config, "[failed]");
  10079. return ret;
  10080. }
  10081. if (i915_modparams.fastboot &&
  10082. intel_pipe_config_compare(dev_priv,
  10083. to_intel_crtc_state(old_crtc_state),
  10084. pipe_config, true)) {
  10085. crtc_state->mode_changed = false;
  10086. pipe_config->update_pipe = true;
  10087. }
  10088. if (needs_modeset(crtc_state))
  10089. any_ms = true;
  10090. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10091. needs_modeset(crtc_state) ?
  10092. "[modeset]" : "[fastset]");
  10093. }
  10094. if (any_ms) {
  10095. ret = intel_modeset_checks(state);
  10096. if (ret)
  10097. return ret;
  10098. } else {
  10099. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10100. }
  10101. ret = drm_atomic_helper_check_planes(dev, state);
  10102. if (ret)
  10103. return ret;
  10104. intel_fbc_choose_crtc(dev_priv, intel_state);
  10105. return calc_watermark_data(state);
  10106. }
  10107. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10108. struct drm_atomic_state *state)
  10109. {
  10110. return drm_atomic_helper_prepare_planes(dev, state);
  10111. }
  10112. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10113. {
  10114. struct drm_device *dev = crtc->base.dev;
  10115. if (!dev->max_vblank_count)
  10116. return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
  10117. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10118. }
  10119. static void intel_update_crtc(struct drm_crtc *crtc,
  10120. struct drm_atomic_state *state,
  10121. struct drm_crtc_state *old_crtc_state,
  10122. struct drm_crtc_state *new_crtc_state)
  10123. {
  10124. struct drm_device *dev = crtc->dev;
  10125. struct drm_i915_private *dev_priv = to_i915(dev);
  10126. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10127. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10128. bool modeset = needs_modeset(new_crtc_state);
  10129. if (modeset) {
  10130. update_scanline_offset(intel_crtc);
  10131. dev_priv->display.crtc_enable(pipe_config, state);
  10132. } else {
  10133. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10134. pipe_config);
  10135. }
  10136. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10137. intel_fbc_enable(
  10138. intel_crtc, pipe_config,
  10139. to_intel_plane_state(crtc->primary->state));
  10140. }
  10141. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10142. }
  10143. static void intel_update_crtcs(struct drm_atomic_state *state)
  10144. {
  10145. struct drm_crtc *crtc;
  10146. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10147. int i;
  10148. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10149. if (!new_crtc_state->active)
  10150. continue;
  10151. intel_update_crtc(crtc, state, old_crtc_state,
  10152. new_crtc_state);
  10153. }
  10154. }
  10155. static void skl_update_crtcs(struct drm_atomic_state *state)
  10156. {
  10157. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10158. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10159. struct drm_crtc *crtc;
  10160. struct intel_crtc *intel_crtc;
  10161. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10162. struct intel_crtc_state *cstate;
  10163. unsigned int updated = 0;
  10164. bool progress;
  10165. enum pipe pipe;
  10166. int i;
  10167. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10168. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10169. /* ignore allocations for crtc's that have been turned off. */
  10170. if (new_crtc_state->active)
  10171. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10172. /*
  10173. * Whenever the number of active pipes changes, we need to make sure we
  10174. * update the pipes in the right order so that their ddb allocations
  10175. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10176. * cause pipe underruns and other bad stuff.
  10177. */
  10178. do {
  10179. progress = false;
  10180. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10181. bool vbl_wait = false;
  10182. unsigned int cmask = drm_crtc_mask(crtc);
  10183. intel_crtc = to_intel_crtc(crtc);
  10184. cstate = to_intel_crtc_state(new_crtc_state);
  10185. pipe = intel_crtc->pipe;
  10186. if (updated & cmask || !cstate->base.active)
  10187. continue;
  10188. if (skl_ddb_allocation_overlaps(dev_priv,
  10189. entries,
  10190. &cstate->wm.skl.ddb,
  10191. i))
  10192. continue;
  10193. updated |= cmask;
  10194. entries[i] = &cstate->wm.skl.ddb;
  10195. /*
  10196. * If this is an already active pipe, it's DDB changed,
  10197. * and this isn't the last pipe that needs updating
  10198. * then we need to wait for a vblank to pass for the
  10199. * new ddb allocation to take effect.
  10200. */
  10201. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10202. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10203. !new_crtc_state->active_changed &&
  10204. intel_state->wm_results.dirty_pipes != updated)
  10205. vbl_wait = true;
  10206. intel_update_crtc(crtc, state, old_crtc_state,
  10207. new_crtc_state);
  10208. if (vbl_wait)
  10209. intel_wait_for_vblank(dev_priv, pipe);
  10210. progress = true;
  10211. }
  10212. } while (progress);
  10213. }
  10214. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10215. {
  10216. struct intel_atomic_state *state, *next;
  10217. struct llist_node *freed;
  10218. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10219. llist_for_each_entry_safe(state, next, freed, freed)
  10220. drm_atomic_state_put(&state->base);
  10221. }
  10222. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10223. {
  10224. struct drm_i915_private *dev_priv =
  10225. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10226. intel_atomic_helper_free_state(dev_priv);
  10227. }
  10228. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10229. {
  10230. struct wait_queue_entry wait_fence, wait_reset;
  10231. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10232. init_wait_entry(&wait_fence, 0);
  10233. init_wait_entry(&wait_reset, 0);
  10234. for (;;) {
  10235. prepare_to_wait(&intel_state->commit_ready.wait,
  10236. &wait_fence, TASK_UNINTERRUPTIBLE);
  10237. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10238. &wait_reset, TASK_UNINTERRUPTIBLE);
  10239. if (i915_sw_fence_done(&intel_state->commit_ready)
  10240. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10241. break;
  10242. schedule();
  10243. }
  10244. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10245. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10246. }
  10247. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10248. {
  10249. struct drm_device *dev = state->dev;
  10250. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10251. struct drm_i915_private *dev_priv = to_i915(dev);
  10252. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10253. struct drm_crtc *crtc;
  10254. struct intel_crtc_state *intel_cstate;
  10255. u64 put_domains[I915_MAX_PIPES] = {};
  10256. int i;
  10257. intel_atomic_commit_fence_wait(intel_state);
  10258. drm_atomic_helper_wait_for_dependencies(state);
  10259. if (intel_state->modeset)
  10260. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10261. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10263. if (needs_modeset(new_crtc_state) ||
  10264. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10265. put_domains[to_intel_crtc(crtc)->pipe] =
  10266. modeset_get_crtc_power_domains(crtc,
  10267. to_intel_crtc_state(new_crtc_state));
  10268. }
  10269. if (!needs_modeset(new_crtc_state))
  10270. continue;
  10271. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10272. to_intel_crtc_state(new_crtc_state));
  10273. if (old_crtc_state->active) {
  10274. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10275. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10276. intel_crtc->active = false;
  10277. intel_fbc_disable(intel_crtc);
  10278. intel_disable_shared_dpll(intel_crtc);
  10279. /*
  10280. * Underruns don't always raise
  10281. * interrupts, so check manually.
  10282. */
  10283. intel_check_cpu_fifo_underruns(dev_priv);
  10284. intel_check_pch_fifo_underruns(dev_priv);
  10285. if (!new_crtc_state->active) {
  10286. /*
  10287. * Make sure we don't call initial_watermarks
  10288. * for ILK-style watermark updates.
  10289. *
  10290. * No clue what this is supposed to achieve.
  10291. */
  10292. if (INTEL_GEN(dev_priv) >= 9)
  10293. dev_priv->display.initial_watermarks(intel_state,
  10294. to_intel_crtc_state(new_crtc_state));
  10295. }
  10296. }
  10297. }
  10298. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10299. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10300. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10301. if (intel_state->modeset) {
  10302. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10303. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10304. /*
  10305. * SKL workaround: bspec recommends we disable the SAGV when we
  10306. * have more then one pipe enabled
  10307. */
  10308. if (!intel_can_enable_sagv(state))
  10309. intel_disable_sagv(dev_priv);
  10310. intel_modeset_verify_disabled(dev, state);
  10311. }
  10312. /* Complete the events for pipes that have now been disabled */
  10313. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10314. bool modeset = needs_modeset(new_crtc_state);
  10315. /* Complete events for now disable pipes here. */
  10316. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10317. spin_lock_irq(&dev->event_lock);
  10318. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10319. spin_unlock_irq(&dev->event_lock);
  10320. new_crtc_state->event = NULL;
  10321. }
  10322. }
  10323. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10324. dev_priv->display.update_crtcs(state);
  10325. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10326. * already, but still need the state for the delayed optimization. To
  10327. * fix this:
  10328. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10329. * - schedule that vblank worker _before_ calling hw_done
  10330. * - at the start of commit_tail, cancel it _synchrously
  10331. * - switch over to the vblank wait helper in the core after that since
  10332. * we don't need out special handling any more.
  10333. */
  10334. drm_atomic_helper_wait_for_flip_done(dev, state);
  10335. /*
  10336. * Now that the vblank has passed, we can go ahead and program the
  10337. * optimal watermarks on platforms that need two-step watermark
  10338. * programming.
  10339. *
  10340. * TODO: Move this (and other cleanup) to an async worker eventually.
  10341. */
  10342. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10343. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10344. if (dev_priv->display.optimize_watermarks)
  10345. dev_priv->display.optimize_watermarks(intel_state,
  10346. intel_cstate);
  10347. }
  10348. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10349. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10350. if (put_domains[i])
  10351. modeset_put_power_domains(dev_priv, put_domains[i]);
  10352. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10353. }
  10354. if (intel_state->modeset)
  10355. intel_verify_planes(intel_state);
  10356. if (intel_state->modeset && intel_can_enable_sagv(state))
  10357. intel_enable_sagv(dev_priv);
  10358. drm_atomic_helper_commit_hw_done(state);
  10359. if (intel_state->modeset) {
  10360. /* As one of the primary mmio accessors, KMS has a high
  10361. * likelihood of triggering bugs in unclaimed access. After we
  10362. * finish modesetting, see if an error has been flagged, and if
  10363. * so enable debugging for the next modeset - and hope we catch
  10364. * the culprit.
  10365. */
  10366. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10367. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10368. }
  10369. drm_atomic_helper_cleanup_planes(dev, state);
  10370. drm_atomic_helper_commit_cleanup_done(state);
  10371. drm_atomic_state_put(state);
  10372. intel_atomic_helper_free_state(dev_priv);
  10373. }
  10374. static void intel_atomic_commit_work(struct work_struct *work)
  10375. {
  10376. struct drm_atomic_state *state =
  10377. container_of(work, struct drm_atomic_state, commit_work);
  10378. intel_atomic_commit_tail(state);
  10379. }
  10380. static int __i915_sw_fence_call
  10381. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10382. enum i915_sw_fence_notify notify)
  10383. {
  10384. struct intel_atomic_state *state =
  10385. container_of(fence, struct intel_atomic_state, commit_ready);
  10386. switch (notify) {
  10387. case FENCE_COMPLETE:
  10388. /* we do blocking waits in the worker, nothing to do here */
  10389. break;
  10390. case FENCE_FREE:
  10391. {
  10392. struct intel_atomic_helper *helper =
  10393. &to_i915(state->base.dev)->atomic_helper;
  10394. if (llist_add(&state->freed, &helper->free_list))
  10395. schedule_work(&helper->free_work);
  10396. break;
  10397. }
  10398. }
  10399. return NOTIFY_DONE;
  10400. }
  10401. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10402. {
  10403. struct drm_plane_state *old_plane_state, *new_plane_state;
  10404. struct drm_plane *plane;
  10405. int i;
  10406. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10407. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10408. intel_fb_obj(new_plane_state->fb),
  10409. to_intel_plane(plane)->frontbuffer_bit);
  10410. }
  10411. /**
  10412. * intel_atomic_commit - commit validated state object
  10413. * @dev: DRM device
  10414. * @state: the top-level driver state object
  10415. * @nonblock: nonblocking commit
  10416. *
  10417. * This function commits a top-level state object that has been validated
  10418. * with drm_atomic_helper_check().
  10419. *
  10420. * RETURNS
  10421. * Zero for success or -errno.
  10422. */
  10423. static int intel_atomic_commit(struct drm_device *dev,
  10424. struct drm_atomic_state *state,
  10425. bool nonblock)
  10426. {
  10427. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10428. struct drm_i915_private *dev_priv = to_i915(dev);
  10429. int ret = 0;
  10430. drm_atomic_state_get(state);
  10431. i915_sw_fence_init(&intel_state->commit_ready,
  10432. intel_atomic_commit_ready);
  10433. /*
  10434. * The intel_legacy_cursor_update() fast path takes care
  10435. * of avoiding the vblank waits for simple cursor
  10436. * movement and flips. For cursor on/off and size changes,
  10437. * we want to perform the vblank waits so that watermark
  10438. * updates happen during the correct frames. Gen9+ have
  10439. * double buffered watermarks and so shouldn't need this.
  10440. *
  10441. * Unset state->legacy_cursor_update before the call to
  10442. * drm_atomic_helper_setup_commit() because otherwise
  10443. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10444. * we get FIFO underruns because we didn't wait
  10445. * for vblank.
  10446. *
  10447. * FIXME doing watermarks and fb cleanup from a vblank worker
  10448. * (assuming we had any) would solve these problems.
  10449. */
  10450. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10451. struct intel_crtc_state *new_crtc_state;
  10452. struct intel_crtc *crtc;
  10453. int i;
  10454. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10455. if (new_crtc_state->wm.need_postvbl_update ||
  10456. new_crtc_state->update_wm_post)
  10457. state->legacy_cursor_update = false;
  10458. }
  10459. ret = intel_atomic_prepare_commit(dev, state);
  10460. if (ret) {
  10461. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10462. i915_sw_fence_commit(&intel_state->commit_ready);
  10463. return ret;
  10464. }
  10465. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10466. if (!ret)
  10467. ret = drm_atomic_helper_swap_state(state, true);
  10468. if (ret) {
  10469. i915_sw_fence_commit(&intel_state->commit_ready);
  10470. drm_atomic_helper_cleanup_planes(dev, state);
  10471. return ret;
  10472. }
  10473. dev_priv->wm.distrust_bios_wm = false;
  10474. intel_shared_dpll_swap_state(state);
  10475. intel_atomic_track_fbs(state);
  10476. if (intel_state->modeset) {
  10477. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10478. sizeof(intel_state->min_cdclk));
  10479. memcpy(dev_priv->min_voltage_level,
  10480. intel_state->min_voltage_level,
  10481. sizeof(intel_state->min_voltage_level));
  10482. dev_priv->active_crtcs = intel_state->active_crtcs;
  10483. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10484. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10485. }
  10486. drm_atomic_state_get(state);
  10487. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10488. i915_sw_fence_commit(&intel_state->commit_ready);
  10489. if (nonblock && intel_state->modeset) {
  10490. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10491. } else if (nonblock) {
  10492. queue_work(system_unbound_wq, &state->commit_work);
  10493. } else {
  10494. if (intel_state->modeset)
  10495. flush_workqueue(dev_priv->modeset_wq);
  10496. intel_atomic_commit_tail(state);
  10497. }
  10498. return 0;
  10499. }
  10500. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10501. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10502. .set_config = drm_atomic_helper_set_config,
  10503. .destroy = intel_crtc_destroy,
  10504. .page_flip = drm_atomic_helper_page_flip,
  10505. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10506. .atomic_destroy_state = intel_crtc_destroy_state,
  10507. .set_crc_source = intel_crtc_set_crc_source,
  10508. };
  10509. struct wait_rps_boost {
  10510. struct wait_queue_entry wait;
  10511. struct drm_crtc *crtc;
  10512. struct i915_request *request;
  10513. };
  10514. static int do_rps_boost(struct wait_queue_entry *_wait,
  10515. unsigned mode, int sync, void *key)
  10516. {
  10517. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10518. struct i915_request *rq = wait->request;
  10519. /*
  10520. * If we missed the vblank, but the request is already running it
  10521. * is reasonable to assume that it will complete before the next
  10522. * vblank without our intervention, so leave RPS alone.
  10523. */
  10524. if (!i915_request_started(rq))
  10525. gen6_rps_boost(rq, NULL);
  10526. i915_request_put(rq);
  10527. drm_crtc_vblank_put(wait->crtc);
  10528. list_del(&wait->wait.entry);
  10529. kfree(wait);
  10530. return 1;
  10531. }
  10532. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10533. struct dma_fence *fence)
  10534. {
  10535. struct wait_rps_boost *wait;
  10536. if (!dma_fence_is_i915(fence))
  10537. return;
  10538. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10539. return;
  10540. if (drm_crtc_vblank_get(crtc))
  10541. return;
  10542. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10543. if (!wait) {
  10544. drm_crtc_vblank_put(crtc);
  10545. return;
  10546. }
  10547. wait->request = to_request(dma_fence_get(fence));
  10548. wait->crtc = crtc;
  10549. wait->wait.func = do_rps_boost;
  10550. wait->wait.flags = 0;
  10551. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10552. }
  10553. static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
  10554. {
  10555. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  10556. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10557. struct drm_framebuffer *fb = plane_state->base.fb;
  10558. struct i915_vma *vma;
  10559. if (plane->id == PLANE_CURSOR &&
  10560. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10561. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10562. const int align = intel_cursor_alignment(dev_priv);
  10563. return i915_gem_object_attach_phys(obj, align);
  10564. }
  10565. vma = intel_pin_and_fence_fb_obj(fb,
  10566. plane_state->base.rotation,
  10567. intel_plane_uses_fence(plane_state),
  10568. &plane_state->flags);
  10569. if (IS_ERR(vma))
  10570. return PTR_ERR(vma);
  10571. plane_state->vma = vma;
  10572. return 0;
  10573. }
  10574. static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
  10575. {
  10576. struct i915_vma *vma;
  10577. vma = fetch_and_zero(&old_plane_state->vma);
  10578. if (vma)
  10579. intel_unpin_fb_vma(vma, old_plane_state->flags);
  10580. }
  10581. /**
  10582. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10583. * @plane: drm plane to prepare for
  10584. * @new_state: the plane state being prepared
  10585. *
  10586. * Prepares a framebuffer for usage on a display plane. Generally this
  10587. * involves pinning the underlying object and updating the frontbuffer tracking
  10588. * bits. Some older platforms need special physical address handling for
  10589. * cursor planes.
  10590. *
  10591. * Must be called with struct_mutex held.
  10592. *
  10593. * Returns 0 on success, negative error code on failure.
  10594. */
  10595. int
  10596. intel_prepare_plane_fb(struct drm_plane *plane,
  10597. struct drm_plane_state *new_state)
  10598. {
  10599. struct intel_atomic_state *intel_state =
  10600. to_intel_atomic_state(new_state->state);
  10601. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10602. struct drm_framebuffer *fb = new_state->fb;
  10603. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10604. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10605. int ret;
  10606. if (old_obj) {
  10607. struct drm_crtc_state *crtc_state =
  10608. drm_atomic_get_existing_crtc_state(new_state->state,
  10609. plane->state->crtc);
  10610. /* Big Hammer, we also need to ensure that any pending
  10611. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10612. * current scanout is retired before unpinning the old
  10613. * framebuffer. Note that we rely on userspace rendering
  10614. * into the buffer attached to the pipe they are waiting
  10615. * on. If not, userspace generates a GPU hang with IPEHR
  10616. * point to the MI_WAIT_FOR_EVENT.
  10617. *
  10618. * This should only fail upon a hung GPU, in which case we
  10619. * can safely continue.
  10620. */
  10621. if (needs_modeset(crtc_state)) {
  10622. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10623. old_obj->resv, NULL,
  10624. false, 0,
  10625. GFP_KERNEL);
  10626. if (ret < 0)
  10627. return ret;
  10628. }
  10629. }
  10630. if (new_state->fence) { /* explicit fencing */
  10631. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10632. new_state->fence,
  10633. I915_FENCE_TIMEOUT,
  10634. GFP_KERNEL);
  10635. if (ret < 0)
  10636. return ret;
  10637. }
  10638. if (!obj)
  10639. return 0;
  10640. ret = i915_gem_object_pin_pages(obj);
  10641. if (ret)
  10642. return ret;
  10643. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10644. if (ret) {
  10645. i915_gem_object_unpin_pages(obj);
  10646. return ret;
  10647. }
  10648. ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
  10649. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10650. mutex_unlock(&dev_priv->drm.struct_mutex);
  10651. i915_gem_object_unpin_pages(obj);
  10652. if (ret)
  10653. return ret;
  10654. if (!new_state->fence) { /* implicit fencing */
  10655. struct dma_fence *fence;
  10656. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10657. obj->resv, NULL,
  10658. false, I915_FENCE_TIMEOUT,
  10659. GFP_KERNEL);
  10660. if (ret < 0)
  10661. return ret;
  10662. fence = reservation_object_get_excl_rcu(obj->resv);
  10663. if (fence) {
  10664. add_rps_boost_after_vblank(new_state->crtc, fence);
  10665. dma_fence_put(fence);
  10666. }
  10667. } else {
  10668. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10669. }
  10670. return 0;
  10671. }
  10672. /**
  10673. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10674. * @plane: drm plane to clean up for
  10675. * @old_state: the state from the previous modeset
  10676. *
  10677. * Cleans up a framebuffer that has just been removed from a plane.
  10678. *
  10679. * Must be called with struct_mutex held.
  10680. */
  10681. void
  10682. intel_cleanup_plane_fb(struct drm_plane *plane,
  10683. struct drm_plane_state *old_state)
  10684. {
  10685. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10686. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10687. mutex_lock(&dev_priv->drm.struct_mutex);
  10688. intel_plane_unpin_fb(to_intel_plane_state(old_state));
  10689. mutex_unlock(&dev_priv->drm.struct_mutex);
  10690. }
  10691. int
  10692. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10693. {
  10694. struct drm_i915_private *dev_priv;
  10695. int max_scale;
  10696. int crtc_clock, max_dotclk;
  10697. if (!intel_crtc || !crtc_state->base.enable)
  10698. return DRM_PLANE_HELPER_NO_SCALING;
  10699. dev_priv = to_i915(intel_crtc->base.dev);
  10700. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10701. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10702. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10703. max_dotclk *= 2;
  10704. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10705. return DRM_PLANE_HELPER_NO_SCALING;
  10706. /*
  10707. * skl max scale is lower of:
  10708. * close to 3 but not 3, -1 is for that purpose
  10709. * or
  10710. * cdclk/crtc_clock
  10711. */
  10712. max_scale = min((1 << 16) * 3 - 1,
  10713. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10714. return max_scale;
  10715. }
  10716. static int
  10717. intel_check_primary_plane(struct intel_plane *plane,
  10718. struct intel_crtc_state *crtc_state,
  10719. struct intel_plane_state *state)
  10720. {
  10721. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10722. struct drm_crtc *crtc = state->base.crtc;
  10723. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10724. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10725. bool can_position = false;
  10726. int ret;
  10727. if (INTEL_GEN(dev_priv) >= 9) {
  10728. /* use scaler when colorkey is not required */
  10729. if (!state->ckey.flags) {
  10730. min_scale = 1;
  10731. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10732. }
  10733. can_position = true;
  10734. }
  10735. ret = drm_atomic_helper_check_plane_state(&state->base,
  10736. &crtc_state->base,
  10737. min_scale, max_scale,
  10738. can_position, true);
  10739. if (ret)
  10740. return ret;
  10741. if (!state->base.fb)
  10742. return 0;
  10743. if (INTEL_GEN(dev_priv) >= 9) {
  10744. ret = skl_check_plane_surface(crtc_state, state);
  10745. if (ret)
  10746. return ret;
  10747. state->ctl = skl_plane_ctl(crtc_state, state);
  10748. } else {
  10749. ret = i9xx_check_plane_surface(state);
  10750. if (ret)
  10751. return ret;
  10752. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10753. }
  10754. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10755. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10756. return 0;
  10757. }
  10758. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10759. struct drm_crtc_state *old_crtc_state)
  10760. {
  10761. struct drm_device *dev = crtc->dev;
  10762. struct drm_i915_private *dev_priv = to_i915(dev);
  10763. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10764. struct intel_crtc_state *old_intel_cstate =
  10765. to_intel_crtc_state(old_crtc_state);
  10766. struct intel_atomic_state *old_intel_state =
  10767. to_intel_atomic_state(old_crtc_state->state);
  10768. struct intel_crtc_state *intel_cstate =
  10769. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10770. bool modeset = needs_modeset(&intel_cstate->base);
  10771. if (!modeset &&
  10772. (intel_cstate->base.color_mgmt_changed ||
  10773. intel_cstate->update_pipe)) {
  10774. intel_color_set_csc(&intel_cstate->base);
  10775. intel_color_load_luts(&intel_cstate->base);
  10776. }
  10777. /* Perform vblank evasion around commit operation */
  10778. intel_pipe_update_start(intel_cstate);
  10779. if (modeset)
  10780. goto out;
  10781. if (intel_cstate->update_pipe)
  10782. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10783. else if (INTEL_GEN(dev_priv) >= 9)
  10784. skl_detach_scalers(intel_crtc);
  10785. out:
  10786. if (dev_priv->display.atomic_update_watermarks)
  10787. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10788. intel_cstate);
  10789. }
  10790. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10791. struct drm_crtc_state *old_crtc_state)
  10792. {
  10793. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10794. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10795. struct intel_atomic_state *old_intel_state =
  10796. to_intel_atomic_state(old_crtc_state->state);
  10797. struct intel_crtc_state *new_crtc_state =
  10798. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10799. intel_pipe_update_end(new_crtc_state);
  10800. if (new_crtc_state->update_pipe &&
  10801. !needs_modeset(&new_crtc_state->base) &&
  10802. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED) {
  10803. if (!IS_GEN2(dev_priv))
  10804. intel_set_cpu_fifo_underrun_reporting(dev_priv, intel_crtc->pipe, true);
  10805. if (new_crtc_state->has_pch_encoder) {
  10806. enum pipe pch_transcoder =
  10807. intel_crtc_pch_transcoder(intel_crtc);
  10808. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10809. }
  10810. }
  10811. }
  10812. /**
  10813. * intel_plane_destroy - destroy a plane
  10814. * @plane: plane to destroy
  10815. *
  10816. * Common destruction function for all types of planes (primary, cursor,
  10817. * sprite).
  10818. */
  10819. void intel_plane_destroy(struct drm_plane *plane)
  10820. {
  10821. drm_plane_cleanup(plane);
  10822. kfree(to_intel_plane(plane));
  10823. }
  10824. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10825. {
  10826. switch (format) {
  10827. case DRM_FORMAT_C8:
  10828. case DRM_FORMAT_RGB565:
  10829. case DRM_FORMAT_XRGB1555:
  10830. case DRM_FORMAT_XRGB8888:
  10831. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10832. modifier == I915_FORMAT_MOD_X_TILED;
  10833. default:
  10834. return false;
  10835. }
  10836. }
  10837. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10838. {
  10839. switch (format) {
  10840. case DRM_FORMAT_C8:
  10841. case DRM_FORMAT_RGB565:
  10842. case DRM_FORMAT_XRGB8888:
  10843. case DRM_FORMAT_XBGR8888:
  10844. case DRM_FORMAT_XRGB2101010:
  10845. case DRM_FORMAT_XBGR2101010:
  10846. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10847. modifier == I915_FORMAT_MOD_X_TILED;
  10848. default:
  10849. return false;
  10850. }
  10851. }
  10852. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10853. {
  10854. switch (format) {
  10855. case DRM_FORMAT_XRGB8888:
  10856. case DRM_FORMAT_XBGR8888:
  10857. case DRM_FORMAT_ARGB8888:
  10858. case DRM_FORMAT_ABGR8888:
  10859. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10860. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10861. return true;
  10862. /* fall through */
  10863. case DRM_FORMAT_RGB565:
  10864. case DRM_FORMAT_XRGB2101010:
  10865. case DRM_FORMAT_XBGR2101010:
  10866. case DRM_FORMAT_YUYV:
  10867. case DRM_FORMAT_YVYU:
  10868. case DRM_FORMAT_UYVY:
  10869. case DRM_FORMAT_VYUY:
  10870. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10871. return true;
  10872. /* fall through */
  10873. case DRM_FORMAT_C8:
  10874. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10875. modifier == I915_FORMAT_MOD_X_TILED ||
  10876. modifier == I915_FORMAT_MOD_Y_TILED)
  10877. return true;
  10878. /* fall through */
  10879. default:
  10880. return false;
  10881. }
  10882. }
  10883. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10884. uint32_t format,
  10885. uint64_t modifier)
  10886. {
  10887. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10888. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10889. return false;
  10890. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10891. modifier != DRM_FORMAT_MOD_LINEAR)
  10892. return false;
  10893. if (INTEL_GEN(dev_priv) >= 9)
  10894. return skl_mod_supported(format, modifier);
  10895. else if (INTEL_GEN(dev_priv) >= 4)
  10896. return i965_mod_supported(format, modifier);
  10897. else
  10898. return i8xx_mod_supported(format, modifier);
  10899. }
  10900. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10901. uint32_t format,
  10902. uint64_t modifier)
  10903. {
  10904. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10905. return false;
  10906. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10907. }
  10908. static struct drm_plane_funcs intel_plane_funcs = {
  10909. .update_plane = drm_atomic_helper_update_plane,
  10910. .disable_plane = drm_atomic_helper_disable_plane,
  10911. .destroy = intel_plane_destroy,
  10912. .atomic_get_property = intel_plane_atomic_get_property,
  10913. .atomic_set_property = intel_plane_atomic_set_property,
  10914. .atomic_duplicate_state = intel_plane_duplicate_state,
  10915. .atomic_destroy_state = intel_plane_destroy_state,
  10916. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10917. };
  10918. static int
  10919. intel_legacy_cursor_update(struct drm_plane *plane,
  10920. struct drm_crtc *crtc,
  10921. struct drm_framebuffer *fb,
  10922. int crtc_x, int crtc_y,
  10923. unsigned int crtc_w, unsigned int crtc_h,
  10924. uint32_t src_x, uint32_t src_y,
  10925. uint32_t src_w, uint32_t src_h,
  10926. struct drm_modeset_acquire_ctx *ctx)
  10927. {
  10928. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10929. int ret;
  10930. struct drm_plane_state *old_plane_state, *new_plane_state;
  10931. struct intel_plane *intel_plane = to_intel_plane(plane);
  10932. struct drm_framebuffer *old_fb;
  10933. struct drm_crtc_state *crtc_state = crtc->state;
  10934. /*
  10935. * When crtc is inactive or there is a modeset pending,
  10936. * wait for it to complete in the slowpath
  10937. */
  10938. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10939. to_intel_crtc_state(crtc_state)->update_pipe)
  10940. goto slow;
  10941. old_plane_state = plane->state;
  10942. /*
  10943. * Don't do an async update if there is an outstanding commit modifying
  10944. * the plane. This prevents our async update's changes from getting
  10945. * overridden by a previous synchronous update's state.
  10946. */
  10947. if (old_plane_state->commit &&
  10948. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  10949. goto slow;
  10950. /*
  10951. * If any parameters change that may affect watermarks,
  10952. * take the slowpath. Only changing fb or position should be
  10953. * in the fastpath.
  10954. */
  10955. if (old_plane_state->crtc != crtc ||
  10956. old_plane_state->src_w != src_w ||
  10957. old_plane_state->src_h != src_h ||
  10958. old_plane_state->crtc_w != crtc_w ||
  10959. old_plane_state->crtc_h != crtc_h ||
  10960. !old_plane_state->fb != !fb)
  10961. goto slow;
  10962. new_plane_state = intel_plane_duplicate_state(plane);
  10963. if (!new_plane_state)
  10964. return -ENOMEM;
  10965. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10966. new_plane_state->src_x = src_x;
  10967. new_plane_state->src_y = src_y;
  10968. new_plane_state->src_w = src_w;
  10969. new_plane_state->src_h = src_h;
  10970. new_plane_state->crtc_x = crtc_x;
  10971. new_plane_state->crtc_y = crtc_y;
  10972. new_plane_state->crtc_w = crtc_w;
  10973. new_plane_state->crtc_h = crtc_h;
  10974. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10975. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  10976. to_intel_plane_state(plane->state),
  10977. to_intel_plane_state(new_plane_state));
  10978. if (ret)
  10979. goto out_free;
  10980. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10981. if (ret)
  10982. goto out_free;
  10983. ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
  10984. if (ret)
  10985. goto out_unlock;
  10986. old_fb = old_plane_state->fb;
  10987. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10988. intel_plane->frontbuffer_bit);
  10989. /* Swap plane state */
  10990. plane->state = new_plane_state;
  10991. if (plane->state->visible) {
  10992. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10993. intel_plane->update_plane(intel_plane,
  10994. to_intel_crtc_state(crtc->state),
  10995. to_intel_plane_state(plane->state));
  10996. } else {
  10997. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10998. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10999. }
  11000. intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
  11001. out_unlock:
  11002. mutex_unlock(&dev_priv->drm.struct_mutex);
  11003. out_free:
  11004. if (ret)
  11005. intel_plane_destroy_state(plane, new_plane_state);
  11006. else
  11007. intel_plane_destroy_state(plane, old_plane_state);
  11008. return ret;
  11009. slow:
  11010. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11011. crtc_x, crtc_y, crtc_w, crtc_h,
  11012. src_x, src_y, src_w, src_h, ctx);
  11013. }
  11014. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11015. .update_plane = intel_legacy_cursor_update,
  11016. .disable_plane = drm_atomic_helper_disable_plane,
  11017. .destroy = intel_plane_destroy,
  11018. .atomic_get_property = intel_plane_atomic_get_property,
  11019. .atomic_set_property = intel_plane_atomic_set_property,
  11020. .atomic_duplicate_state = intel_plane_duplicate_state,
  11021. .atomic_destroy_state = intel_plane_destroy_state,
  11022. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  11023. };
  11024. static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
  11025. enum i9xx_plane_id i9xx_plane)
  11026. {
  11027. if (!HAS_FBC(dev_priv))
  11028. return false;
  11029. if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  11030. return i9xx_plane == PLANE_A; /* tied to pipe A */
  11031. else if (IS_IVYBRIDGE(dev_priv))
  11032. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
  11033. i9xx_plane == PLANE_C;
  11034. else if (INTEL_GEN(dev_priv) >= 4)
  11035. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
  11036. else
  11037. return i9xx_plane == PLANE_A;
  11038. }
  11039. static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
  11040. enum pipe pipe, enum plane_id plane_id)
  11041. {
  11042. if (!HAS_FBC(dev_priv))
  11043. return false;
  11044. return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
  11045. }
  11046. static struct intel_plane *
  11047. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11048. {
  11049. struct intel_plane *primary = NULL;
  11050. struct intel_plane_state *state = NULL;
  11051. const uint32_t *intel_primary_formats;
  11052. unsigned int supported_rotations;
  11053. unsigned int num_formats;
  11054. const uint64_t *modifiers;
  11055. int ret;
  11056. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11057. if (!primary) {
  11058. ret = -ENOMEM;
  11059. goto fail;
  11060. }
  11061. state = intel_create_plane_state(&primary->base);
  11062. if (!state) {
  11063. ret = -ENOMEM;
  11064. goto fail;
  11065. }
  11066. primary->base.state = &state->base;
  11067. primary->can_scale = false;
  11068. primary->max_downscale = 1;
  11069. if (INTEL_GEN(dev_priv) >= 9) {
  11070. primary->can_scale = true;
  11071. state->scaler_id = -1;
  11072. }
  11073. primary->pipe = pipe;
  11074. /*
  11075. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11076. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11077. */
  11078. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11079. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11080. else
  11081. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11082. primary->id = PLANE_PRIMARY;
  11083. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11084. if (INTEL_GEN(dev_priv) >= 9)
  11085. primary->has_fbc = skl_plane_has_fbc(dev_priv,
  11086. primary->pipe,
  11087. primary->id);
  11088. else
  11089. primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
  11090. primary->i9xx_plane);
  11091. if (primary->has_fbc) {
  11092. struct intel_fbc *fbc = &dev_priv->fbc;
  11093. fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
  11094. }
  11095. primary->check_plane = intel_check_primary_plane;
  11096. if (INTEL_GEN(dev_priv) >= 9) {
  11097. intel_primary_formats = skl_primary_formats;
  11098. num_formats = ARRAY_SIZE(skl_primary_formats);
  11099. if (skl_plane_has_ccs(dev_priv, pipe, PLANE_PRIMARY))
  11100. modifiers = skl_format_modifiers_ccs;
  11101. else
  11102. modifiers = skl_format_modifiers_noccs;
  11103. primary->update_plane = skl_update_plane;
  11104. primary->disable_plane = skl_disable_plane;
  11105. primary->get_hw_state = skl_plane_get_hw_state;
  11106. } else if (INTEL_GEN(dev_priv) >= 4) {
  11107. intel_primary_formats = i965_primary_formats;
  11108. num_formats = ARRAY_SIZE(i965_primary_formats);
  11109. modifiers = i9xx_format_modifiers;
  11110. primary->update_plane = i9xx_update_plane;
  11111. primary->disable_plane = i9xx_disable_plane;
  11112. primary->get_hw_state = i9xx_plane_get_hw_state;
  11113. } else {
  11114. intel_primary_formats = i8xx_primary_formats;
  11115. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11116. modifiers = i9xx_format_modifiers;
  11117. primary->update_plane = i9xx_update_plane;
  11118. primary->disable_plane = i9xx_disable_plane;
  11119. primary->get_hw_state = i9xx_plane_get_hw_state;
  11120. }
  11121. if (INTEL_GEN(dev_priv) >= 9)
  11122. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11123. 0, &intel_plane_funcs,
  11124. intel_primary_formats, num_formats,
  11125. modifiers,
  11126. DRM_PLANE_TYPE_PRIMARY,
  11127. "plane 1%c", pipe_name(pipe));
  11128. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11129. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11130. 0, &intel_plane_funcs,
  11131. intel_primary_formats, num_formats,
  11132. modifiers,
  11133. DRM_PLANE_TYPE_PRIMARY,
  11134. "primary %c", pipe_name(pipe));
  11135. else
  11136. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11137. 0, &intel_plane_funcs,
  11138. intel_primary_formats, num_formats,
  11139. modifiers,
  11140. DRM_PLANE_TYPE_PRIMARY,
  11141. "plane %c",
  11142. plane_name(primary->i9xx_plane));
  11143. if (ret)
  11144. goto fail;
  11145. if (INTEL_GEN(dev_priv) >= 10) {
  11146. supported_rotations =
  11147. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11148. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11149. DRM_MODE_REFLECT_X;
  11150. } else if (INTEL_GEN(dev_priv) >= 9) {
  11151. supported_rotations =
  11152. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11153. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11154. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11155. supported_rotations =
  11156. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11157. DRM_MODE_REFLECT_X;
  11158. } else if (INTEL_GEN(dev_priv) >= 4) {
  11159. supported_rotations =
  11160. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11161. } else {
  11162. supported_rotations = DRM_MODE_ROTATE_0;
  11163. }
  11164. if (INTEL_GEN(dev_priv) >= 4)
  11165. drm_plane_create_rotation_property(&primary->base,
  11166. DRM_MODE_ROTATE_0,
  11167. supported_rotations);
  11168. if (INTEL_GEN(dev_priv) >= 9)
  11169. drm_plane_create_color_properties(&primary->base,
  11170. BIT(DRM_COLOR_YCBCR_BT601) |
  11171. BIT(DRM_COLOR_YCBCR_BT709),
  11172. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  11173. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  11174. DRM_COLOR_YCBCR_BT709,
  11175. DRM_COLOR_YCBCR_LIMITED_RANGE);
  11176. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11177. return primary;
  11178. fail:
  11179. kfree(state);
  11180. kfree(primary);
  11181. return ERR_PTR(ret);
  11182. }
  11183. static struct intel_plane *
  11184. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11185. enum pipe pipe)
  11186. {
  11187. struct intel_plane *cursor = NULL;
  11188. struct intel_plane_state *state = NULL;
  11189. int ret;
  11190. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11191. if (!cursor) {
  11192. ret = -ENOMEM;
  11193. goto fail;
  11194. }
  11195. state = intel_create_plane_state(&cursor->base);
  11196. if (!state) {
  11197. ret = -ENOMEM;
  11198. goto fail;
  11199. }
  11200. cursor->base.state = &state->base;
  11201. cursor->can_scale = false;
  11202. cursor->max_downscale = 1;
  11203. cursor->pipe = pipe;
  11204. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11205. cursor->id = PLANE_CURSOR;
  11206. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11207. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11208. cursor->update_plane = i845_update_cursor;
  11209. cursor->disable_plane = i845_disable_cursor;
  11210. cursor->get_hw_state = i845_cursor_get_hw_state;
  11211. cursor->check_plane = i845_check_cursor;
  11212. } else {
  11213. cursor->update_plane = i9xx_update_cursor;
  11214. cursor->disable_plane = i9xx_disable_cursor;
  11215. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11216. cursor->check_plane = i9xx_check_cursor;
  11217. }
  11218. cursor->cursor.base = ~0;
  11219. cursor->cursor.cntl = ~0;
  11220. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11221. cursor->cursor.size = ~0;
  11222. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11223. 0, &intel_cursor_plane_funcs,
  11224. intel_cursor_formats,
  11225. ARRAY_SIZE(intel_cursor_formats),
  11226. cursor_format_modifiers,
  11227. DRM_PLANE_TYPE_CURSOR,
  11228. "cursor %c", pipe_name(pipe));
  11229. if (ret)
  11230. goto fail;
  11231. if (INTEL_GEN(dev_priv) >= 4)
  11232. drm_plane_create_rotation_property(&cursor->base,
  11233. DRM_MODE_ROTATE_0,
  11234. DRM_MODE_ROTATE_0 |
  11235. DRM_MODE_ROTATE_180);
  11236. if (INTEL_GEN(dev_priv) >= 9)
  11237. state->scaler_id = -1;
  11238. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11239. return cursor;
  11240. fail:
  11241. kfree(state);
  11242. kfree(cursor);
  11243. return ERR_PTR(ret);
  11244. }
  11245. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11246. struct intel_crtc_state *crtc_state)
  11247. {
  11248. struct intel_crtc_scaler_state *scaler_state =
  11249. &crtc_state->scaler_state;
  11250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11251. int i;
  11252. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11253. if (!crtc->num_scalers)
  11254. return;
  11255. for (i = 0; i < crtc->num_scalers; i++) {
  11256. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11257. scaler->in_use = 0;
  11258. scaler->mode = PS_SCALER_MODE_DYN;
  11259. }
  11260. scaler_state->scaler_id = -1;
  11261. }
  11262. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11263. {
  11264. struct intel_crtc *intel_crtc;
  11265. struct intel_crtc_state *crtc_state = NULL;
  11266. struct intel_plane *primary = NULL;
  11267. struct intel_plane *cursor = NULL;
  11268. int sprite, ret;
  11269. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11270. if (!intel_crtc)
  11271. return -ENOMEM;
  11272. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11273. if (!crtc_state) {
  11274. ret = -ENOMEM;
  11275. goto fail;
  11276. }
  11277. intel_crtc->config = crtc_state;
  11278. intel_crtc->base.state = &crtc_state->base;
  11279. crtc_state->base.crtc = &intel_crtc->base;
  11280. primary = intel_primary_plane_create(dev_priv, pipe);
  11281. if (IS_ERR(primary)) {
  11282. ret = PTR_ERR(primary);
  11283. goto fail;
  11284. }
  11285. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11286. for_each_sprite(dev_priv, pipe, sprite) {
  11287. struct intel_plane *plane;
  11288. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11289. if (IS_ERR(plane)) {
  11290. ret = PTR_ERR(plane);
  11291. goto fail;
  11292. }
  11293. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11294. }
  11295. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11296. if (IS_ERR(cursor)) {
  11297. ret = PTR_ERR(cursor);
  11298. goto fail;
  11299. }
  11300. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11301. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11302. &primary->base, &cursor->base,
  11303. &intel_crtc_funcs,
  11304. "pipe %c", pipe_name(pipe));
  11305. if (ret)
  11306. goto fail;
  11307. intel_crtc->pipe = pipe;
  11308. /* initialize shared scalers */
  11309. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11310. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11311. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] != NULL);
  11312. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] = intel_crtc;
  11313. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11314. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11315. intel_color_init(&intel_crtc->base);
  11316. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11317. return 0;
  11318. fail:
  11319. /*
  11320. * drm_mode_config_cleanup() will free up any
  11321. * crtcs/planes already initialized.
  11322. */
  11323. kfree(crtc_state);
  11324. kfree(intel_crtc);
  11325. return ret;
  11326. }
  11327. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11328. {
  11329. struct drm_device *dev = connector->base.dev;
  11330. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11331. if (!connector->base.state->crtc)
  11332. return INVALID_PIPE;
  11333. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11334. }
  11335. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11336. struct drm_file *file)
  11337. {
  11338. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11339. struct drm_crtc *drmmode_crtc;
  11340. struct intel_crtc *crtc;
  11341. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11342. if (!drmmode_crtc)
  11343. return -ENOENT;
  11344. crtc = to_intel_crtc(drmmode_crtc);
  11345. pipe_from_crtc_id->pipe = crtc->pipe;
  11346. return 0;
  11347. }
  11348. static int intel_encoder_clones(struct intel_encoder *encoder)
  11349. {
  11350. struct drm_device *dev = encoder->base.dev;
  11351. struct intel_encoder *source_encoder;
  11352. int index_mask = 0;
  11353. int entry = 0;
  11354. for_each_intel_encoder(dev, source_encoder) {
  11355. if (encoders_cloneable(encoder, source_encoder))
  11356. index_mask |= (1 << entry);
  11357. entry++;
  11358. }
  11359. return index_mask;
  11360. }
  11361. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11362. {
  11363. if (!IS_MOBILE(dev_priv))
  11364. return false;
  11365. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11366. return false;
  11367. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11368. return false;
  11369. return true;
  11370. }
  11371. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11372. {
  11373. if (INTEL_GEN(dev_priv) >= 9)
  11374. return false;
  11375. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11376. return false;
  11377. if (IS_CHERRYVIEW(dev_priv))
  11378. return false;
  11379. if (HAS_PCH_LPT_H(dev_priv) &&
  11380. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11381. return false;
  11382. /* DDI E can't be used if DDI A requires 4 lanes */
  11383. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11384. return false;
  11385. if (!dev_priv->vbt.int_crt_support)
  11386. return false;
  11387. return true;
  11388. }
  11389. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11390. {
  11391. int pps_num;
  11392. int pps_idx;
  11393. if (HAS_DDI(dev_priv))
  11394. return;
  11395. /*
  11396. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11397. * everywhere where registers can be write protected.
  11398. */
  11399. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11400. pps_num = 2;
  11401. else
  11402. pps_num = 1;
  11403. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11404. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11405. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11406. I915_WRITE(PP_CONTROL(pps_idx), val);
  11407. }
  11408. }
  11409. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11410. {
  11411. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11412. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11413. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11414. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11415. else
  11416. dev_priv->pps_mmio_base = PPS_BASE;
  11417. intel_pps_unlock_regs_wa(dev_priv);
  11418. }
  11419. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11420. {
  11421. struct intel_encoder *encoder;
  11422. bool dpd_is_edp = false;
  11423. intel_pps_init(dev_priv);
  11424. /*
  11425. * intel_edp_init_connector() depends on this completing first, to
  11426. * prevent the registeration of both eDP and LVDS and the incorrect
  11427. * sharing of the PPS.
  11428. */
  11429. intel_lvds_init(dev_priv);
  11430. if (intel_crt_present(dev_priv))
  11431. intel_crt_init(dev_priv);
  11432. if (IS_GEN9_LP(dev_priv)) {
  11433. /*
  11434. * FIXME: Broxton doesn't support port detection via the
  11435. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11436. * detect the ports.
  11437. */
  11438. intel_ddi_init(dev_priv, PORT_A);
  11439. intel_ddi_init(dev_priv, PORT_B);
  11440. intel_ddi_init(dev_priv, PORT_C);
  11441. intel_dsi_init(dev_priv);
  11442. } else if (HAS_DDI(dev_priv)) {
  11443. int found;
  11444. /*
  11445. * Haswell uses DDI functions to detect digital outputs.
  11446. * On SKL pre-D0 the strap isn't connected, so we assume
  11447. * it's there.
  11448. */
  11449. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11450. /* WaIgnoreDDIAStrap: skl */
  11451. if (found || IS_GEN9_BC(dev_priv))
  11452. intel_ddi_init(dev_priv, PORT_A);
  11453. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11454. * register */
  11455. found = I915_READ(SFUSE_STRAP);
  11456. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11457. intel_ddi_init(dev_priv, PORT_B);
  11458. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11459. intel_ddi_init(dev_priv, PORT_C);
  11460. if (found & SFUSE_STRAP_DDID_DETECTED)
  11461. intel_ddi_init(dev_priv, PORT_D);
  11462. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11463. intel_ddi_init(dev_priv, PORT_F);
  11464. /*
  11465. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11466. */
  11467. if (IS_GEN9_BC(dev_priv) &&
  11468. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11469. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11470. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11471. intel_ddi_init(dev_priv, PORT_E);
  11472. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11473. int found;
  11474. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11475. if (has_edp_a(dev_priv))
  11476. intel_dp_init(dev_priv, DP_A, PORT_A);
  11477. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11478. /* PCH SDVOB multiplex with HDMIB */
  11479. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11480. if (!found)
  11481. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11482. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11483. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11484. }
  11485. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11486. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11487. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11488. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11489. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11490. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11491. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11492. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11493. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11494. bool has_edp, has_port;
  11495. /*
  11496. * The DP_DETECTED bit is the latched state of the DDC
  11497. * SDA pin at boot. However since eDP doesn't require DDC
  11498. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11499. * eDP ports may have been muxed to an alternate function.
  11500. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11501. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11502. * detect eDP ports.
  11503. *
  11504. * Sadly the straps seem to be missing sometimes even for HDMI
  11505. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11506. * and VBT for the presence of the port. Additionally we can't
  11507. * trust the port type the VBT declares as we've seen at least
  11508. * HDMI ports that the VBT claim are DP or eDP.
  11509. */
  11510. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11511. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11512. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11513. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11514. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11515. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11516. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11517. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11518. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11519. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11520. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11521. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11522. if (IS_CHERRYVIEW(dev_priv)) {
  11523. /*
  11524. * eDP not supported on port D,
  11525. * so no need to worry about it
  11526. */
  11527. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11528. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11529. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11530. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11531. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11532. }
  11533. intel_dsi_init(dev_priv);
  11534. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11535. bool found = false;
  11536. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11537. DRM_DEBUG_KMS("probing SDVOB\n");
  11538. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11539. if (!found && IS_G4X(dev_priv)) {
  11540. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11541. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11542. }
  11543. if (!found && IS_G4X(dev_priv))
  11544. intel_dp_init(dev_priv, DP_B, PORT_B);
  11545. }
  11546. /* Before G4X SDVOC doesn't have its own detect register */
  11547. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11548. DRM_DEBUG_KMS("probing SDVOC\n");
  11549. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11550. }
  11551. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11552. if (IS_G4X(dev_priv)) {
  11553. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11554. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11555. }
  11556. if (IS_G4X(dev_priv))
  11557. intel_dp_init(dev_priv, DP_C, PORT_C);
  11558. }
  11559. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11560. intel_dp_init(dev_priv, DP_D, PORT_D);
  11561. } else if (IS_GEN2(dev_priv))
  11562. intel_dvo_init(dev_priv);
  11563. if (SUPPORTS_TV(dev_priv))
  11564. intel_tv_init(dev_priv);
  11565. intel_psr_init(dev_priv);
  11566. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11567. encoder->base.possible_crtcs = encoder->crtc_mask;
  11568. encoder->base.possible_clones =
  11569. intel_encoder_clones(encoder);
  11570. }
  11571. intel_init_pch_refclk(dev_priv);
  11572. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11573. }
  11574. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11575. {
  11576. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11577. drm_framebuffer_cleanup(fb);
  11578. i915_gem_object_lock(intel_fb->obj);
  11579. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11580. i915_gem_object_unlock(intel_fb->obj);
  11581. i915_gem_object_put(intel_fb->obj);
  11582. kfree(intel_fb);
  11583. }
  11584. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11585. struct drm_file *file,
  11586. unsigned int *handle)
  11587. {
  11588. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11589. struct drm_i915_gem_object *obj = intel_fb->obj;
  11590. if (obj->userptr.mm) {
  11591. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11592. return -EINVAL;
  11593. }
  11594. return drm_gem_handle_create(file, &obj->base, handle);
  11595. }
  11596. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11597. struct drm_file *file,
  11598. unsigned flags, unsigned color,
  11599. struct drm_clip_rect *clips,
  11600. unsigned num_clips)
  11601. {
  11602. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11603. i915_gem_object_flush_if_display(obj);
  11604. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11605. return 0;
  11606. }
  11607. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11608. .destroy = intel_user_framebuffer_destroy,
  11609. .create_handle = intel_user_framebuffer_create_handle,
  11610. .dirty = intel_user_framebuffer_dirty,
  11611. };
  11612. static
  11613. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11614. uint64_t fb_modifier, uint32_t pixel_format)
  11615. {
  11616. u32 gen = INTEL_GEN(dev_priv);
  11617. if (gen >= 9) {
  11618. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11619. /* "The stride in bytes must not exceed the of the size of 8K
  11620. * pixels and 32K bytes."
  11621. */
  11622. return min(8192 * cpp, 32768);
  11623. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11624. return 32*1024;
  11625. } else if (gen >= 4) {
  11626. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11627. return 16*1024;
  11628. else
  11629. return 32*1024;
  11630. } else if (gen >= 3) {
  11631. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11632. return 8*1024;
  11633. else
  11634. return 16*1024;
  11635. } else {
  11636. /* XXX DSPC is limited to 4k tiled */
  11637. return 8*1024;
  11638. }
  11639. }
  11640. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11641. struct drm_i915_gem_object *obj,
  11642. struct drm_mode_fb_cmd2 *mode_cmd)
  11643. {
  11644. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11645. struct drm_framebuffer *fb = &intel_fb->base;
  11646. struct drm_format_name_buf format_name;
  11647. u32 pitch_limit;
  11648. unsigned int tiling, stride;
  11649. int ret = -EINVAL;
  11650. int i;
  11651. i915_gem_object_lock(obj);
  11652. obj->framebuffer_references++;
  11653. tiling = i915_gem_object_get_tiling(obj);
  11654. stride = i915_gem_object_get_stride(obj);
  11655. i915_gem_object_unlock(obj);
  11656. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11657. /*
  11658. * If there's a fence, enforce that
  11659. * the fb modifier and tiling mode match.
  11660. */
  11661. if (tiling != I915_TILING_NONE &&
  11662. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11663. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11664. goto err;
  11665. }
  11666. } else {
  11667. if (tiling == I915_TILING_X) {
  11668. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11669. } else if (tiling == I915_TILING_Y) {
  11670. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11671. goto err;
  11672. }
  11673. }
  11674. /* Passed in modifier sanity checking. */
  11675. switch (mode_cmd->modifier[0]) {
  11676. case I915_FORMAT_MOD_Y_TILED_CCS:
  11677. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11678. switch (mode_cmd->pixel_format) {
  11679. case DRM_FORMAT_XBGR8888:
  11680. case DRM_FORMAT_ABGR8888:
  11681. case DRM_FORMAT_XRGB8888:
  11682. case DRM_FORMAT_ARGB8888:
  11683. break;
  11684. default:
  11685. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11686. goto err;
  11687. }
  11688. /* fall through */
  11689. case I915_FORMAT_MOD_Y_TILED:
  11690. case I915_FORMAT_MOD_Yf_TILED:
  11691. if (INTEL_GEN(dev_priv) < 9) {
  11692. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11693. mode_cmd->modifier[0]);
  11694. goto err;
  11695. }
  11696. case DRM_FORMAT_MOD_LINEAR:
  11697. case I915_FORMAT_MOD_X_TILED:
  11698. break;
  11699. default:
  11700. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11701. mode_cmd->modifier[0]);
  11702. goto err;
  11703. }
  11704. /*
  11705. * gen2/3 display engine uses the fence if present,
  11706. * so the tiling mode must match the fb modifier exactly.
  11707. */
  11708. if (INTEL_GEN(dev_priv) < 4 &&
  11709. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11710. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11711. goto err;
  11712. }
  11713. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11714. mode_cmd->pixel_format);
  11715. if (mode_cmd->pitches[0] > pitch_limit) {
  11716. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11717. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11718. "tiled" : "linear",
  11719. mode_cmd->pitches[0], pitch_limit);
  11720. goto err;
  11721. }
  11722. /*
  11723. * If there's a fence, enforce that
  11724. * the fb pitch and fence stride match.
  11725. */
  11726. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11727. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11728. mode_cmd->pitches[0], stride);
  11729. goto err;
  11730. }
  11731. /* Reject formats not supported by any plane early. */
  11732. switch (mode_cmd->pixel_format) {
  11733. case DRM_FORMAT_C8:
  11734. case DRM_FORMAT_RGB565:
  11735. case DRM_FORMAT_XRGB8888:
  11736. case DRM_FORMAT_ARGB8888:
  11737. break;
  11738. case DRM_FORMAT_XRGB1555:
  11739. if (INTEL_GEN(dev_priv) > 3) {
  11740. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11741. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11742. goto err;
  11743. }
  11744. break;
  11745. case DRM_FORMAT_ABGR8888:
  11746. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11747. INTEL_GEN(dev_priv) < 9) {
  11748. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11749. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11750. goto err;
  11751. }
  11752. break;
  11753. case DRM_FORMAT_XBGR8888:
  11754. case DRM_FORMAT_XRGB2101010:
  11755. case DRM_FORMAT_XBGR2101010:
  11756. if (INTEL_GEN(dev_priv) < 4) {
  11757. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11758. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11759. goto err;
  11760. }
  11761. break;
  11762. case DRM_FORMAT_ABGR2101010:
  11763. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11764. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11765. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11766. goto err;
  11767. }
  11768. break;
  11769. case DRM_FORMAT_YUYV:
  11770. case DRM_FORMAT_UYVY:
  11771. case DRM_FORMAT_YVYU:
  11772. case DRM_FORMAT_VYUY:
  11773. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11774. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11775. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11776. goto err;
  11777. }
  11778. break;
  11779. default:
  11780. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11781. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11782. goto err;
  11783. }
  11784. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11785. if (mode_cmd->offsets[0] != 0)
  11786. goto err;
  11787. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11788. for (i = 0; i < fb->format->num_planes; i++) {
  11789. u32 stride_alignment;
  11790. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11791. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11792. goto err;
  11793. }
  11794. stride_alignment = intel_fb_stride_alignment(fb, i);
  11795. /*
  11796. * Display WA #0531: skl,bxt,kbl,glk
  11797. *
  11798. * Render decompression and plane width > 3840
  11799. * combined with horizontal panning requires the
  11800. * plane stride to be a multiple of 4. We'll just
  11801. * require the entire fb to accommodate that to avoid
  11802. * potential runtime errors at plane configuration time.
  11803. */
  11804. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11805. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11806. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11807. stride_alignment *= 4;
  11808. if (fb->pitches[i] & (stride_alignment - 1)) {
  11809. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11810. i, fb->pitches[i], stride_alignment);
  11811. goto err;
  11812. }
  11813. }
  11814. intel_fb->obj = obj;
  11815. ret = intel_fill_fb_info(dev_priv, fb);
  11816. if (ret)
  11817. goto err;
  11818. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11819. if (ret) {
  11820. DRM_ERROR("framebuffer init failed %d\n", ret);
  11821. goto err;
  11822. }
  11823. return 0;
  11824. err:
  11825. i915_gem_object_lock(obj);
  11826. obj->framebuffer_references--;
  11827. i915_gem_object_unlock(obj);
  11828. return ret;
  11829. }
  11830. static struct drm_framebuffer *
  11831. intel_user_framebuffer_create(struct drm_device *dev,
  11832. struct drm_file *filp,
  11833. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11834. {
  11835. struct drm_framebuffer *fb;
  11836. struct drm_i915_gem_object *obj;
  11837. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11838. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11839. if (!obj)
  11840. return ERR_PTR(-ENOENT);
  11841. fb = intel_framebuffer_create(obj, &mode_cmd);
  11842. if (IS_ERR(fb))
  11843. i915_gem_object_put(obj);
  11844. return fb;
  11845. }
  11846. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11847. {
  11848. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11849. drm_atomic_state_default_release(state);
  11850. i915_sw_fence_fini(&intel_state->commit_ready);
  11851. kfree(state);
  11852. }
  11853. static enum drm_mode_status
  11854. intel_mode_valid(struct drm_device *dev,
  11855. const struct drm_display_mode *mode)
  11856. {
  11857. if (mode->vscan > 1)
  11858. return MODE_NO_VSCAN;
  11859. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  11860. return MODE_NO_DBLESCAN;
  11861. if (mode->flags & DRM_MODE_FLAG_HSKEW)
  11862. return MODE_H_ILLEGAL;
  11863. if (mode->flags & (DRM_MODE_FLAG_CSYNC |
  11864. DRM_MODE_FLAG_NCSYNC |
  11865. DRM_MODE_FLAG_PCSYNC))
  11866. return MODE_HSYNC;
  11867. if (mode->flags & (DRM_MODE_FLAG_BCAST |
  11868. DRM_MODE_FLAG_PIXMUX |
  11869. DRM_MODE_FLAG_CLKDIV2))
  11870. return MODE_BAD;
  11871. return MODE_OK;
  11872. }
  11873. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11874. .fb_create = intel_user_framebuffer_create,
  11875. .get_format_info = intel_get_format_info,
  11876. .output_poll_changed = intel_fbdev_output_poll_changed,
  11877. .mode_valid = intel_mode_valid,
  11878. .atomic_check = intel_atomic_check,
  11879. .atomic_commit = intel_atomic_commit,
  11880. .atomic_state_alloc = intel_atomic_state_alloc,
  11881. .atomic_state_clear = intel_atomic_state_clear,
  11882. .atomic_state_free = intel_atomic_state_free,
  11883. };
  11884. /**
  11885. * intel_init_display_hooks - initialize the display modesetting hooks
  11886. * @dev_priv: device private
  11887. */
  11888. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11889. {
  11890. intel_init_cdclk_hooks(dev_priv);
  11891. if (INTEL_GEN(dev_priv) >= 9) {
  11892. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11893. dev_priv->display.get_initial_plane_config =
  11894. skylake_get_initial_plane_config;
  11895. dev_priv->display.crtc_compute_clock =
  11896. haswell_crtc_compute_clock;
  11897. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11898. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11899. } else if (HAS_DDI(dev_priv)) {
  11900. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11901. dev_priv->display.get_initial_plane_config =
  11902. i9xx_get_initial_plane_config;
  11903. dev_priv->display.crtc_compute_clock =
  11904. haswell_crtc_compute_clock;
  11905. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11906. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11907. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11908. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11909. dev_priv->display.get_initial_plane_config =
  11910. i9xx_get_initial_plane_config;
  11911. dev_priv->display.crtc_compute_clock =
  11912. ironlake_crtc_compute_clock;
  11913. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11914. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11915. } else if (IS_CHERRYVIEW(dev_priv)) {
  11916. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11917. dev_priv->display.get_initial_plane_config =
  11918. i9xx_get_initial_plane_config;
  11919. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11920. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11921. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11922. } else if (IS_VALLEYVIEW(dev_priv)) {
  11923. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11924. dev_priv->display.get_initial_plane_config =
  11925. i9xx_get_initial_plane_config;
  11926. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11927. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11928. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11929. } else if (IS_G4X(dev_priv)) {
  11930. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11931. dev_priv->display.get_initial_plane_config =
  11932. i9xx_get_initial_plane_config;
  11933. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11934. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11935. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11936. } else if (IS_PINEVIEW(dev_priv)) {
  11937. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11938. dev_priv->display.get_initial_plane_config =
  11939. i9xx_get_initial_plane_config;
  11940. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11941. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11942. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11943. } else if (!IS_GEN2(dev_priv)) {
  11944. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11945. dev_priv->display.get_initial_plane_config =
  11946. i9xx_get_initial_plane_config;
  11947. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11948. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11949. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11950. } else {
  11951. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11952. dev_priv->display.get_initial_plane_config =
  11953. i9xx_get_initial_plane_config;
  11954. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11955. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11956. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11957. }
  11958. if (IS_GEN5(dev_priv)) {
  11959. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11960. } else if (IS_GEN6(dev_priv)) {
  11961. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11962. } else if (IS_IVYBRIDGE(dev_priv)) {
  11963. /* FIXME: detect B0+ stepping and use auto training */
  11964. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11965. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11966. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11967. }
  11968. if (INTEL_GEN(dev_priv) >= 9)
  11969. dev_priv->display.update_crtcs = skl_update_crtcs;
  11970. else
  11971. dev_priv->display.update_crtcs = intel_update_crtcs;
  11972. }
  11973. /*
  11974. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11975. */
  11976. static void quirk_ssc_force_disable(struct drm_device *dev)
  11977. {
  11978. struct drm_i915_private *dev_priv = to_i915(dev);
  11979. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11980. DRM_INFO("applying lvds SSC disable quirk\n");
  11981. }
  11982. /*
  11983. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11984. * brightness value
  11985. */
  11986. static void quirk_invert_brightness(struct drm_device *dev)
  11987. {
  11988. struct drm_i915_private *dev_priv = to_i915(dev);
  11989. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11990. DRM_INFO("applying inverted panel brightness quirk\n");
  11991. }
  11992. /* Some VBT's incorrectly indicate no backlight is present */
  11993. static void quirk_backlight_present(struct drm_device *dev)
  11994. {
  11995. struct drm_i915_private *dev_priv = to_i915(dev);
  11996. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11997. DRM_INFO("applying backlight present quirk\n");
  11998. }
  11999. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  12000. * which is 300 ms greater than eDP spec T12 min.
  12001. */
  12002. static void quirk_increase_t12_delay(struct drm_device *dev)
  12003. {
  12004. struct drm_i915_private *dev_priv = to_i915(dev);
  12005. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  12006. DRM_INFO("Applying T12 delay quirk\n");
  12007. }
  12008. struct intel_quirk {
  12009. int device;
  12010. int subsystem_vendor;
  12011. int subsystem_device;
  12012. void (*hook)(struct drm_device *dev);
  12013. };
  12014. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12015. struct intel_dmi_quirk {
  12016. void (*hook)(struct drm_device *dev);
  12017. const struct dmi_system_id (*dmi_id_list)[];
  12018. };
  12019. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12020. {
  12021. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12022. return 1;
  12023. }
  12024. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12025. {
  12026. .dmi_id_list = &(const struct dmi_system_id[]) {
  12027. {
  12028. .callback = intel_dmi_reverse_brightness,
  12029. .ident = "NCR Corporation",
  12030. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12031. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12032. },
  12033. },
  12034. { } /* terminating entry */
  12035. },
  12036. .hook = quirk_invert_brightness,
  12037. },
  12038. };
  12039. static struct intel_quirk intel_quirks[] = {
  12040. /* Lenovo U160 cannot use SSC on LVDS */
  12041. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12042. /* Sony Vaio Y cannot use SSC on LVDS */
  12043. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12044. /* Acer Aspire 5734Z must invert backlight brightness */
  12045. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12046. /* Acer/eMachines G725 */
  12047. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12048. /* Acer/eMachines e725 */
  12049. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12050. /* Acer/Packard Bell NCL20 */
  12051. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12052. /* Acer Aspire 4736Z */
  12053. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12054. /* Acer Aspire 5336 */
  12055. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12056. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12057. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12058. /* Acer C720 Chromebook (Core i3 4005U) */
  12059. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12060. /* Apple Macbook 2,1 (Core 2 T7400) */
  12061. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12062. /* Apple Macbook 4,1 */
  12063. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12064. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12065. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12066. /* HP Chromebook 14 (Celeron 2955U) */
  12067. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12068. /* Dell Chromebook 11 */
  12069. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12070. /* Dell Chromebook 11 (2015 version) */
  12071. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12072. /* Toshiba Satellite P50-C-18C */
  12073. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12074. };
  12075. static void intel_init_quirks(struct drm_device *dev)
  12076. {
  12077. struct pci_dev *d = dev->pdev;
  12078. int i;
  12079. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12080. struct intel_quirk *q = &intel_quirks[i];
  12081. if (d->device == q->device &&
  12082. (d->subsystem_vendor == q->subsystem_vendor ||
  12083. q->subsystem_vendor == PCI_ANY_ID) &&
  12084. (d->subsystem_device == q->subsystem_device ||
  12085. q->subsystem_device == PCI_ANY_ID))
  12086. q->hook(dev);
  12087. }
  12088. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12089. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12090. intel_dmi_quirks[i].hook(dev);
  12091. }
  12092. }
  12093. /* Disable the VGA plane that we never use */
  12094. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12095. {
  12096. struct pci_dev *pdev = dev_priv->drm.pdev;
  12097. u8 sr1;
  12098. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12099. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12100. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12101. outb(SR01, VGA_SR_INDEX);
  12102. sr1 = inb(VGA_SR_DATA);
  12103. outb(sr1 | 1<<5, VGA_SR_DATA);
  12104. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12105. udelay(300);
  12106. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12107. POSTING_READ(vga_reg);
  12108. }
  12109. void intel_modeset_init_hw(struct drm_device *dev)
  12110. {
  12111. struct drm_i915_private *dev_priv = to_i915(dev);
  12112. intel_update_cdclk(dev_priv);
  12113. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12114. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12115. }
  12116. /*
  12117. * Calculate what we think the watermarks should be for the state we've read
  12118. * out of the hardware and then immediately program those watermarks so that
  12119. * we ensure the hardware settings match our internal state.
  12120. *
  12121. * We can calculate what we think WM's should be by creating a duplicate of the
  12122. * current state (which was constructed during hardware readout) and running it
  12123. * through the atomic check code to calculate new watermark values in the
  12124. * state object.
  12125. */
  12126. static void sanitize_watermarks(struct drm_device *dev)
  12127. {
  12128. struct drm_i915_private *dev_priv = to_i915(dev);
  12129. struct drm_atomic_state *state;
  12130. struct intel_atomic_state *intel_state;
  12131. struct drm_crtc *crtc;
  12132. struct drm_crtc_state *cstate;
  12133. struct drm_modeset_acquire_ctx ctx;
  12134. int ret;
  12135. int i;
  12136. /* Only supported on platforms that use atomic watermark design */
  12137. if (!dev_priv->display.optimize_watermarks)
  12138. return;
  12139. /*
  12140. * We need to hold connection_mutex before calling duplicate_state so
  12141. * that the connector loop is protected.
  12142. */
  12143. drm_modeset_acquire_init(&ctx, 0);
  12144. retry:
  12145. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12146. if (ret == -EDEADLK) {
  12147. drm_modeset_backoff(&ctx);
  12148. goto retry;
  12149. } else if (WARN_ON(ret)) {
  12150. goto fail;
  12151. }
  12152. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12153. if (WARN_ON(IS_ERR(state)))
  12154. goto fail;
  12155. intel_state = to_intel_atomic_state(state);
  12156. /*
  12157. * Hardware readout is the only time we don't want to calculate
  12158. * intermediate watermarks (since we don't trust the current
  12159. * watermarks).
  12160. */
  12161. if (!HAS_GMCH_DISPLAY(dev_priv))
  12162. intel_state->skip_intermediate_wm = true;
  12163. ret = intel_atomic_check(dev, state);
  12164. if (ret) {
  12165. /*
  12166. * If we fail here, it means that the hardware appears to be
  12167. * programmed in a way that shouldn't be possible, given our
  12168. * understanding of watermark requirements. This might mean a
  12169. * mistake in the hardware readout code or a mistake in the
  12170. * watermark calculations for a given platform. Raise a WARN
  12171. * so that this is noticeable.
  12172. *
  12173. * If this actually happens, we'll have to just leave the
  12174. * BIOS-programmed watermarks untouched and hope for the best.
  12175. */
  12176. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12177. goto put_state;
  12178. }
  12179. /* Write calculated watermark values back */
  12180. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12181. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12182. cs->wm.need_postvbl_update = true;
  12183. dev_priv->display.optimize_watermarks(intel_state, cs);
  12184. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12185. }
  12186. put_state:
  12187. drm_atomic_state_put(state);
  12188. fail:
  12189. drm_modeset_drop_locks(&ctx);
  12190. drm_modeset_acquire_fini(&ctx);
  12191. }
  12192. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12193. {
  12194. if (IS_GEN5(dev_priv)) {
  12195. u32 fdi_pll_clk =
  12196. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12197. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12198. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12199. dev_priv->fdi_pll_freq = 270000;
  12200. } else {
  12201. return;
  12202. }
  12203. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12204. }
  12205. int intel_modeset_init(struct drm_device *dev)
  12206. {
  12207. struct drm_i915_private *dev_priv = to_i915(dev);
  12208. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12209. enum pipe pipe;
  12210. struct intel_crtc *crtc;
  12211. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12212. drm_mode_config_init(dev);
  12213. dev->mode_config.min_width = 0;
  12214. dev->mode_config.min_height = 0;
  12215. dev->mode_config.preferred_depth = 24;
  12216. dev->mode_config.prefer_shadow = 1;
  12217. dev->mode_config.allow_fb_modifiers = true;
  12218. dev->mode_config.funcs = &intel_mode_funcs;
  12219. init_llist_head(&dev_priv->atomic_helper.free_list);
  12220. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12221. intel_atomic_helper_free_state_worker);
  12222. intel_init_quirks(dev);
  12223. intel_init_pm(dev_priv);
  12224. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12225. return 0;
  12226. /*
  12227. * There may be no VBT; and if the BIOS enabled SSC we can
  12228. * just keep using it to avoid unnecessary flicker. Whereas if the
  12229. * BIOS isn't using it, don't assume it will work even if the VBT
  12230. * indicates as much.
  12231. */
  12232. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12233. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12234. DREF_SSC1_ENABLE);
  12235. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12236. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12237. bios_lvds_use_ssc ? "en" : "dis",
  12238. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12239. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12240. }
  12241. }
  12242. if (IS_GEN2(dev_priv)) {
  12243. dev->mode_config.max_width = 2048;
  12244. dev->mode_config.max_height = 2048;
  12245. } else if (IS_GEN3(dev_priv)) {
  12246. dev->mode_config.max_width = 4096;
  12247. dev->mode_config.max_height = 4096;
  12248. } else {
  12249. dev->mode_config.max_width = 8192;
  12250. dev->mode_config.max_height = 8192;
  12251. }
  12252. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12253. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12254. dev->mode_config.cursor_height = 1023;
  12255. } else if (IS_GEN2(dev_priv)) {
  12256. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12257. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12258. } else {
  12259. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12260. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12261. }
  12262. dev->mode_config.fb_base = ggtt->gmadr.start;
  12263. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12264. INTEL_INFO(dev_priv)->num_pipes,
  12265. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12266. for_each_pipe(dev_priv, pipe) {
  12267. int ret;
  12268. ret = intel_crtc_init(dev_priv, pipe);
  12269. if (ret) {
  12270. drm_mode_config_cleanup(dev);
  12271. return ret;
  12272. }
  12273. }
  12274. intel_shared_dpll_init(dev);
  12275. intel_update_fdi_pll_freq(dev_priv);
  12276. intel_update_czclk(dev_priv);
  12277. intel_modeset_init_hw(dev);
  12278. if (dev_priv->max_cdclk_freq == 0)
  12279. intel_update_max_cdclk(dev_priv);
  12280. /* Just disable it once at startup */
  12281. i915_disable_vga(dev_priv);
  12282. intel_setup_outputs(dev_priv);
  12283. drm_modeset_lock_all(dev);
  12284. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12285. drm_modeset_unlock_all(dev);
  12286. for_each_intel_crtc(dev, crtc) {
  12287. struct intel_initial_plane_config plane_config = {};
  12288. if (!crtc->active)
  12289. continue;
  12290. /*
  12291. * Note that reserving the BIOS fb up front prevents us
  12292. * from stuffing other stolen allocations like the ring
  12293. * on top. This prevents some ugliness at boot time, and
  12294. * can even allow for smooth boot transitions if the BIOS
  12295. * fb is large enough for the active pipe configuration.
  12296. */
  12297. dev_priv->display.get_initial_plane_config(crtc,
  12298. &plane_config);
  12299. /*
  12300. * If the fb is shared between multiple heads, we'll
  12301. * just get the first one.
  12302. */
  12303. intel_find_initial_plane_obj(crtc, &plane_config);
  12304. }
  12305. /*
  12306. * Make sure hardware watermarks really match the state we read out.
  12307. * Note that we need to do this after reconstructing the BIOS fb's
  12308. * since the watermark calculation done here will use pstate->fb.
  12309. */
  12310. if (!HAS_GMCH_DISPLAY(dev_priv))
  12311. sanitize_watermarks(dev);
  12312. return 0;
  12313. }
  12314. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12315. {
  12316. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12317. /* 640x480@60Hz, ~25175 kHz */
  12318. struct dpll clock = {
  12319. .m1 = 18,
  12320. .m2 = 7,
  12321. .p1 = 13,
  12322. .p2 = 4,
  12323. .n = 2,
  12324. };
  12325. u32 dpll, fp;
  12326. int i;
  12327. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12328. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12329. pipe_name(pipe), clock.vco, clock.dot);
  12330. fp = i9xx_dpll_compute_fp(&clock);
  12331. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12332. DPLL_VGA_MODE_DIS |
  12333. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12334. PLL_P2_DIVIDE_BY_4 |
  12335. PLL_REF_INPUT_DREFCLK |
  12336. DPLL_VCO_ENABLE;
  12337. I915_WRITE(FP0(pipe), fp);
  12338. I915_WRITE(FP1(pipe), fp);
  12339. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12340. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12341. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12342. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12343. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12344. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12345. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12346. /*
  12347. * Apparently we need to have VGA mode enabled prior to changing
  12348. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12349. * dividers, even though the register value does change.
  12350. */
  12351. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12352. I915_WRITE(DPLL(pipe), dpll);
  12353. /* Wait for the clocks to stabilize. */
  12354. POSTING_READ(DPLL(pipe));
  12355. udelay(150);
  12356. /* The pixel multiplier can only be updated once the
  12357. * DPLL is enabled and the clocks are stable.
  12358. *
  12359. * So write it again.
  12360. */
  12361. I915_WRITE(DPLL(pipe), dpll);
  12362. /* We do this three times for luck */
  12363. for (i = 0; i < 3 ; i++) {
  12364. I915_WRITE(DPLL(pipe), dpll);
  12365. POSTING_READ(DPLL(pipe));
  12366. udelay(150); /* wait for warmup */
  12367. }
  12368. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12369. POSTING_READ(PIPECONF(pipe));
  12370. intel_wait_for_pipe_scanline_moving(crtc);
  12371. }
  12372. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12373. {
  12374. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12375. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12376. pipe_name(pipe));
  12377. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12378. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12379. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12380. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
  12381. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
  12382. I915_WRITE(PIPECONF(pipe), 0);
  12383. POSTING_READ(PIPECONF(pipe));
  12384. intel_wait_for_pipe_scanline_stopped(crtc);
  12385. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12386. POSTING_READ(DPLL(pipe));
  12387. }
  12388. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12389. struct intel_plane *plane)
  12390. {
  12391. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12392. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  12393. u32 val = I915_READ(DSPCNTR(i9xx_plane));
  12394. return (val & DISPLAY_PLANE_ENABLE) == 0 ||
  12395. (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
  12396. }
  12397. static void
  12398. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12399. {
  12400. struct intel_crtc *crtc;
  12401. if (INTEL_GEN(dev_priv) >= 4)
  12402. return;
  12403. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12404. struct intel_plane *plane =
  12405. to_intel_plane(crtc->base.primary);
  12406. if (intel_plane_mapping_ok(crtc, plane))
  12407. continue;
  12408. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12409. plane->base.name);
  12410. intel_plane_disable_noatomic(crtc, plane);
  12411. }
  12412. }
  12413. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12414. {
  12415. struct drm_device *dev = crtc->base.dev;
  12416. struct intel_encoder *encoder;
  12417. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12418. return true;
  12419. return false;
  12420. }
  12421. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12422. {
  12423. struct drm_device *dev = encoder->base.dev;
  12424. struct intel_connector *connector;
  12425. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12426. return connector;
  12427. return NULL;
  12428. }
  12429. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12430. enum pipe pch_transcoder)
  12431. {
  12432. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12433. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12434. }
  12435. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12436. struct drm_modeset_acquire_ctx *ctx)
  12437. {
  12438. struct drm_device *dev = crtc->base.dev;
  12439. struct drm_i915_private *dev_priv = to_i915(dev);
  12440. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12441. /* Clear any frame start delays used for debugging left by the BIOS */
  12442. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12443. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12444. I915_WRITE(reg,
  12445. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12446. }
  12447. /* restore vblank interrupts to correct state */
  12448. drm_crtc_vblank_reset(&crtc->base);
  12449. if (crtc->active) {
  12450. struct intel_plane *plane;
  12451. drm_crtc_vblank_on(&crtc->base);
  12452. /* Disable everything but the primary plane */
  12453. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12454. const struct intel_plane_state *plane_state =
  12455. to_intel_plane_state(plane->base.state);
  12456. if (plane_state->base.visible &&
  12457. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12458. intel_plane_disable_noatomic(crtc, plane);
  12459. }
  12460. }
  12461. /* Adjust the state of the output pipe according to whether we
  12462. * have active connectors/encoders. */
  12463. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12464. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12465. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12466. /*
  12467. * We start out with underrun reporting disabled to avoid races.
  12468. * For correct bookkeeping mark this on active crtcs.
  12469. *
  12470. * Also on gmch platforms we dont have any hardware bits to
  12471. * disable the underrun reporting. Which means we need to start
  12472. * out with underrun reporting disabled also on inactive pipes,
  12473. * since otherwise we'll complain about the garbage we read when
  12474. * e.g. coming up after runtime pm.
  12475. *
  12476. * No protection against concurrent access is required - at
  12477. * worst a fifo underrun happens which also sets this to false.
  12478. */
  12479. crtc->cpu_fifo_underrun_disabled = true;
  12480. /*
  12481. * We track the PCH trancoder underrun reporting state
  12482. * within the crtc. With crtc for pipe A housing the underrun
  12483. * reporting state for PCH transcoder A, crtc for pipe B housing
  12484. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12485. * and marking underrun reporting as disabled for the non-existing
  12486. * PCH transcoders B and C would prevent enabling the south
  12487. * error interrupt (see cpt_can_enable_serr_int()).
  12488. */
  12489. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12490. crtc->pch_fifo_underrun_disabled = true;
  12491. }
  12492. }
  12493. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12494. {
  12495. struct intel_connector *connector;
  12496. /* We need to check both for a crtc link (meaning that the
  12497. * encoder is active and trying to read from a pipe) and the
  12498. * pipe itself being active. */
  12499. bool has_active_crtc = encoder->base.crtc &&
  12500. to_intel_crtc(encoder->base.crtc)->active;
  12501. connector = intel_encoder_find_connector(encoder);
  12502. if (connector && !has_active_crtc) {
  12503. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12504. encoder->base.base.id,
  12505. encoder->base.name);
  12506. /* Connector is active, but has no active pipe. This is
  12507. * fallout from our resume register restoring. Disable
  12508. * the encoder manually again. */
  12509. if (encoder->base.crtc) {
  12510. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12511. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12512. encoder->base.base.id,
  12513. encoder->base.name);
  12514. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12515. if (encoder->post_disable)
  12516. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12517. }
  12518. encoder->base.crtc = NULL;
  12519. /* Inconsistent output/port/pipe state happens presumably due to
  12520. * a bug in one of the get_hw_state functions. Or someplace else
  12521. * in our code, like the register restore mess on resume. Clamp
  12522. * things to off as a safer default. */
  12523. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12524. connector->base.encoder = NULL;
  12525. }
  12526. }
  12527. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12528. {
  12529. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12530. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12531. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12532. i915_disable_vga(dev_priv);
  12533. }
  12534. }
  12535. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12536. {
  12537. /* This function can be called both from intel_modeset_setup_hw_state or
  12538. * at a very early point in our resume sequence, where the power well
  12539. * structures are not yet restored. Since this function is at a very
  12540. * paranoid "someone might have enabled VGA while we were not looking"
  12541. * level, just check if the power well is enabled instead of trying to
  12542. * follow the "don't touch the power well if we don't need it" policy
  12543. * the rest of the driver uses. */
  12544. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12545. return;
  12546. i915_redisable_vga_power_on(dev_priv);
  12547. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12548. }
  12549. /* FIXME read out full plane state for all planes */
  12550. static void readout_plane_state(struct intel_crtc *crtc)
  12551. {
  12552. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12553. struct intel_crtc_state *crtc_state =
  12554. to_intel_crtc_state(crtc->base.state);
  12555. struct intel_plane *plane;
  12556. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12557. struct intel_plane_state *plane_state =
  12558. to_intel_plane_state(plane->base.state);
  12559. bool visible = plane->get_hw_state(plane);
  12560. intel_set_plane_visible(crtc_state, plane_state, visible);
  12561. }
  12562. }
  12563. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12564. {
  12565. struct drm_i915_private *dev_priv = to_i915(dev);
  12566. enum pipe pipe;
  12567. struct intel_crtc *crtc;
  12568. struct intel_encoder *encoder;
  12569. struct intel_connector *connector;
  12570. struct drm_connector_list_iter conn_iter;
  12571. int i;
  12572. dev_priv->active_crtcs = 0;
  12573. for_each_intel_crtc(dev, crtc) {
  12574. struct intel_crtc_state *crtc_state =
  12575. to_intel_crtc_state(crtc->base.state);
  12576. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12577. memset(crtc_state, 0, sizeof(*crtc_state));
  12578. crtc_state->base.crtc = &crtc->base;
  12579. crtc_state->base.active = crtc_state->base.enable =
  12580. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12581. crtc->base.enabled = crtc_state->base.enable;
  12582. crtc->active = crtc_state->base.active;
  12583. if (crtc_state->base.active)
  12584. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12585. readout_plane_state(crtc);
  12586. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12587. crtc->base.base.id, crtc->base.name,
  12588. enableddisabled(crtc_state->base.active));
  12589. }
  12590. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12591. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12592. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12593. &pll->state.hw_state);
  12594. pll->state.crtc_mask = 0;
  12595. for_each_intel_crtc(dev, crtc) {
  12596. struct intel_crtc_state *crtc_state =
  12597. to_intel_crtc_state(crtc->base.state);
  12598. if (crtc_state->base.active &&
  12599. crtc_state->shared_dpll == pll)
  12600. pll->state.crtc_mask |= 1 << crtc->pipe;
  12601. }
  12602. pll->active_mask = pll->state.crtc_mask;
  12603. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12604. pll->name, pll->state.crtc_mask, pll->on);
  12605. }
  12606. for_each_intel_encoder(dev, encoder) {
  12607. pipe = 0;
  12608. if (encoder->get_hw_state(encoder, &pipe)) {
  12609. struct intel_crtc_state *crtc_state;
  12610. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12611. crtc_state = to_intel_crtc_state(crtc->base.state);
  12612. encoder->base.crtc = &crtc->base;
  12613. encoder->get_config(encoder, crtc_state);
  12614. } else {
  12615. encoder->base.crtc = NULL;
  12616. }
  12617. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12618. encoder->base.base.id, encoder->base.name,
  12619. enableddisabled(encoder->base.crtc),
  12620. pipe_name(pipe));
  12621. }
  12622. drm_connector_list_iter_begin(dev, &conn_iter);
  12623. for_each_intel_connector_iter(connector, &conn_iter) {
  12624. if (connector->get_hw_state(connector)) {
  12625. connector->base.dpms = DRM_MODE_DPMS_ON;
  12626. encoder = connector->encoder;
  12627. connector->base.encoder = &encoder->base;
  12628. if (encoder->base.crtc &&
  12629. encoder->base.crtc->state->active) {
  12630. /*
  12631. * This has to be done during hardware readout
  12632. * because anything calling .crtc_disable may
  12633. * rely on the connector_mask being accurate.
  12634. */
  12635. encoder->base.crtc->state->connector_mask |=
  12636. 1 << drm_connector_index(&connector->base);
  12637. encoder->base.crtc->state->encoder_mask |=
  12638. 1 << drm_encoder_index(&encoder->base);
  12639. }
  12640. } else {
  12641. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12642. connector->base.encoder = NULL;
  12643. }
  12644. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12645. connector->base.base.id, connector->base.name,
  12646. enableddisabled(connector->base.encoder));
  12647. }
  12648. drm_connector_list_iter_end(&conn_iter);
  12649. for_each_intel_crtc(dev, crtc) {
  12650. struct intel_crtc_state *crtc_state =
  12651. to_intel_crtc_state(crtc->base.state);
  12652. int min_cdclk = 0;
  12653. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12654. if (crtc_state->base.active) {
  12655. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12656. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12657. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12658. /*
  12659. * The initial mode needs to be set in order to keep
  12660. * the atomic core happy. It wants a valid mode if the
  12661. * crtc's enabled, so we do the above call.
  12662. *
  12663. * But we don't set all the derived state fully, hence
  12664. * set a flag to indicate that a full recalculation is
  12665. * needed on the next commit.
  12666. */
  12667. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12668. intel_crtc_compute_pixel_rate(crtc_state);
  12669. if (dev_priv->display.modeset_calc_cdclk) {
  12670. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12671. if (WARN_ON(min_cdclk < 0))
  12672. min_cdclk = 0;
  12673. }
  12674. drm_calc_timestamping_constants(&crtc->base,
  12675. &crtc_state->base.adjusted_mode);
  12676. update_scanline_offset(crtc);
  12677. }
  12678. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12679. dev_priv->min_voltage_level[crtc->pipe] =
  12680. crtc_state->min_voltage_level;
  12681. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12682. }
  12683. }
  12684. static void
  12685. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12686. {
  12687. struct intel_encoder *encoder;
  12688. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12689. u64 get_domains;
  12690. enum intel_display_power_domain domain;
  12691. if (!encoder->get_power_domains)
  12692. continue;
  12693. get_domains = encoder->get_power_domains(encoder);
  12694. for_each_power_domain(domain, get_domains)
  12695. intel_display_power_get(dev_priv, domain);
  12696. }
  12697. }
  12698. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12699. {
  12700. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12701. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12702. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12703. DARBF_GATING_DIS);
  12704. if (IS_HASWELL(dev_priv)) {
  12705. /*
  12706. * WaRsPkgCStateDisplayPMReq:hsw
  12707. * System hang if this isn't done before disabling all planes!
  12708. */
  12709. I915_WRITE(CHICKEN_PAR1_1,
  12710. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12711. }
  12712. }
  12713. /* Scan out the current hw modeset state,
  12714. * and sanitizes it to the current state
  12715. */
  12716. static void
  12717. intel_modeset_setup_hw_state(struct drm_device *dev,
  12718. struct drm_modeset_acquire_ctx *ctx)
  12719. {
  12720. struct drm_i915_private *dev_priv = to_i915(dev);
  12721. enum pipe pipe;
  12722. struct intel_crtc *crtc;
  12723. struct intel_encoder *encoder;
  12724. int i;
  12725. intel_early_display_was(dev_priv);
  12726. intel_modeset_readout_hw_state(dev);
  12727. /* HW state is read out, now we need to sanitize this mess. */
  12728. get_encoder_power_domains(dev_priv);
  12729. intel_sanitize_plane_mapping(dev_priv);
  12730. for_each_intel_encoder(dev, encoder) {
  12731. intel_sanitize_encoder(encoder);
  12732. }
  12733. for_each_pipe(dev_priv, pipe) {
  12734. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12735. intel_sanitize_crtc(crtc, ctx);
  12736. intel_dump_pipe_config(crtc, crtc->config,
  12737. "[setup_hw_state]");
  12738. }
  12739. intel_modeset_update_connector_atomic_state(dev);
  12740. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12741. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12742. if (!pll->on || pll->active_mask)
  12743. continue;
  12744. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12745. pll->funcs.disable(dev_priv, pll);
  12746. pll->on = false;
  12747. }
  12748. if (IS_G4X(dev_priv)) {
  12749. g4x_wm_get_hw_state(dev);
  12750. g4x_wm_sanitize(dev_priv);
  12751. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12752. vlv_wm_get_hw_state(dev);
  12753. vlv_wm_sanitize(dev_priv);
  12754. } else if (INTEL_GEN(dev_priv) >= 9) {
  12755. skl_wm_get_hw_state(dev);
  12756. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12757. ilk_wm_get_hw_state(dev);
  12758. }
  12759. for_each_intel_crtc(dev, crtc) {
  12760. u64 put_domains;
  12761. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12762. if (WARN_ON(put_domains))
  12763. modeset_put_power_domains(dev_priv, put_domains);
  12764. }
  12765. intel_display_set_init_power(dev_priv, false);
  12766. intel_power_domains_verify_state(dev_priv);
  12767. intel_fbc_init_pipe_state(dev_priv);
  12768. }
  12769. void intel_display_resume(struct drm_device *dev)
  12770. {
  12771. struct drm_i915_private *dev_priv = to_i915(dev);
  12772. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12773. struct drm_modeset_acquire_ctx ctx;
  12774. int ret;
  12775. dev_priv->modeset_restore_state = NULL;
  12776. if (state)
  12777. state->acquire_ctx = &ctx;
  12778. drm_modeset_acquire_init(&ctx, 0);
  12779. while (1) {
  12780. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12781. if (ret != -EDEADLK)
  12782. break;
  12783. drm_modeset_backoff(&ctx);
  12784. }
  12785. if (!ret)
  12786. ret = __intel_display_resume(dev, state, &ctx);
  12787. intel_enable_ipc(dev_priv);
  12788. drm_modeset_drop_locks(&ctx);
  12789. drm_modeset_acquire_fini(&ctx);
  12790. if (ret)
  12791. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12792. if (state)
  12793. drm_atomic_state_put(state);
  12794. }
  12795. int intel_connector_register(struct drm_connector *connector)
  12796. {
  12797. struct intel_connector *intel_connector = to_intel_connector(connector);
  12798. int ret;
  12799. ret = intel_backlight_device_register(intel_connector);
  12800. if (ret)
  12801. goto err;
  12802. return 0;
  12803. err:
  12804. return ret;
  12805. }
  12806. void intel_connector_unregister(struct drm_connector *connector)
  12807. {
  12808. struct intel_connector *intel_connector = to_intel_connector(connector);
  12809. intel_backlight_device_unregister(intel_connector);
  12810. intel_panel_destroy_backlight(connector);
  12811. }
  12812. static void intel_hpd_poll_fini(struct drm_device *dev)
  12813. {
  12814. struct intel_connector *connector;
  12815. struct drm_connector_list_iter conn_iter;
  12816. /* Kill all the work that may have been queued by hpd. */
  12817. drm_connector_list_iter_begin(dev, &conn_iter);
  12818. for_each_intel_connector_iter(connector, &conn_iter) {
  12819. if (connector->modeset_retry_work.func)
  12820. cancel_work_sync(&connector->modeset_retry_work);
  12821. if (connector->hdcp_shim) {
  12822. cancel_delayed_work_sync(&connector->hdcp_check_work);
  12823. cancel_work_sync(&connector->hdcp_prop_work);
  12824. }
  12825. }
  12826. drm_connector_list_iter_end(&conn_iter);
  12827. }
  12828. void intel_modeset_cleanup(struct drm_device *dev)
  12829. {
  12830. struct drm_i915_private *dev_priv = to_i915(dev);
  12831. flush_work(&dev_priv->atomic_helper.free_work);
  12832. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12833. intel_disable_gt_powersave(dev_priv);
  12834. /*
  12835. * Interrupts and polling as the first thing to avoid creating havoc.
  12836. * Too much stuff here (turning of connectors, ...) would
  12837. * experience fancy races otherwise.
  12838. */
  12839. intel_irq_uninstall(dev_priv);
  12840. /*
  12841. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12842. * poll handlers. Hence disable polling after hpd handling is shut down.
  12843. */
  12844. intel_hpd_poll_fini(dev);
  12845. /* poll work can call into fbdev, hence clean that up afterwards */
  12846. intel_fbdev_fini(dev_priv);
  12847. intel_unregister_dsm_handler();
  12848. intel_fbc_global_disable(dev_priv);
  12849. /* flush any delayed tasks or pending work */
  12850. flush_scheduled_work();
  12851. drm_mode_config_cleanup(dev);
  12852. intel_cleanup_overlay(dev_priv);
  12853. intel_cleanup_gt_powersave(dev_priv);
  12854. intel_teardown_gmbus(dev_priv);
  12855. destroy_workqueue(dev_priv->modeset_wq);
  12856. }
  12857. void intel_connector_attach_encoder(struct intel_connector *connector,
  12858. struct intel_encoder *encoder)
  12859. {
  12860. connector->encoder = encoder;
  12861. drm_mode_connector_attach_encoder(&connector->base,
  12862. &encoder->base);
  12863. }
  12864. /*
  12865. * set vga decode state - true == enable VGA decode
  12866. */
  12867. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12868. {
  12869. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12870. u16 gmch_ctrl;
  12871. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12872. DRM_ERROR("failed to read control word\n");
  12873. return -EIO;
  12874. }
  12875. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12876. return 0;
  12877. if (state)
  12878. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12879. else
  12880. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12881. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12882. DRM_ERROR("failed to write control word\n");
  12883. return -EIO;
  12884. }
  12885. return 0;
  12886. }
  12887. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12888. struct intel_display_error_state {
  12889. u32 power_well_driver;
  12890. int num_transcoders;
  12891. struct intel_cursor_error_state {
  12892. u32 control;
  12893. u32 position;
  12894. u32 base;
  12895. u32 size;
  12896. } cursor[I915_MAX_PIPES];
  12897. struct intel_pipe_error_state {
  12898. bool power_domain_on;
  12899. u32 source;
  12900. u32 stat;
  12901. } pipe[I915_MAX_PIPES];
  12902. struct intel_plane_error_state {
  12903. u32 control;
  12904. u32 stride;
  12905. u32 size;
  12906. u32 pos;
  12907. u32 addr;
  12908. u32 surface;
  12909. u32 tile_offset;
  12910. } plane[I915_MAX_PIPES];
  12911. struct intel_transcoder_error_state {
  12912. bool power_domain_on;
  12913. enum transcoder cpu_transcoder;
  12914. u32 conf;
  12915. u32 htotal;
  12916. u32 hblank;
  12917. u32 hsync;
  12918. u32 vtotal;
  12919. u32 vblank;
  12920. u32 vsync;
  12921. } transcoder[4];
  12922. };
  12923. struct intel_display_error_state *
  12924. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12925. {
  12926. struct intel_display_error_state *error;
  12927. int transcoders[] = {
  12928. TRANSCODER_A,
  12929. TRANSCODER_B,
  12930. TRANSCODER_C,
  12931. TRANSCODER_EDP,
  12932. };
  12933. int i;
  12934. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12935. return NULL;
  12936. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12937. if (error == NULL)
  12938. return NULL;
  12939. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12940. error->power_well_driver =
  12941. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12942. for_each_pipe(dev_priv, i) {
  12943. error->pipe[i].power_domain_on =
  12944. __intel_display_power_is_enabled(dev_priv,
  12945. POWER_DOMAIN_PIPE(i));
  12946. if (!error->pipe[i].power_domain_on)
  12947. continue;
  12948. error->cursor[i].control = I915_READ(CURCNTR(i));
  12949. error->cursor[i].position = I915_READ(CURPOS(i));
  12950. error->cursor[i].base = I915_READ(CURBASE(i));
  12951. error->plane[i].control = I915_READ(DSPCNTR(i));
  12952. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12953. if (INTEL_GEN(dev_priv) <= 3) {
  12954. error->plane[i].size = I915_READ(DSPSIZE(i));
  12955. error->plane[i].pos = I915_READ(DSPPOS(i));
  12956. }
  12957. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12958. error->plane[i].addr = I915_READ(DSPADDR(i));
  12959. if (INTEL_GEN(dev_priv) >= 4) {
  12960. error->plane[i].surface = I915_READ(DSPSURF(i));
  12961. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12962. }
  12963. error->pipe[i].source = I915_READ(PIPESRC(i));
  12964. if (HAS_GMCH_DISPLAY(dev_priv))
  12965. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12966. }
  12967. /* Note: this does not include DSI transcoders. */
  12968. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12969. if (HAS_DDI(dev_priv))
  12970. error->num_transcoders++; /* Account for eDP. */
  12971. for (i = 0; i < error->num_transcoders; i++) {
  12972. enum transcoder cpu_transcoder = transcoders[i];
  12973. error->transcoder[i].power_domain_on =
  12974. __intel_display_power_is_enabled(dev_priv,
  12975. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12976. if (!error->transcoder[i].power_domain_on)
  12977. continue;
  12978. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12979. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12980. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12981. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12982. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12983. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12984. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12985. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12986. }
  12987. return error;
  12988. }
  12989. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12990. void
  12991. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12992. struct intel_display_error_state *error)
  12993. {
  12994. struct drm_i915_private *dev_priv = m->i915;
  12995. int i;
  12996. if (!error)
  12997. return;
  12998. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12999. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13000. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13001. error->power_well_driver);
  13002. for_each_pipe(dev_priv, i) {
  13003. err_printf(m, "Pipe [%d]:\n", i);
  13004. err_printf(m, " Power: %s\n",
  13005. onoff(error->pipe[i].power_domain_on));
  13006. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13007. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13008. err_printf(m, "Plane [%d]:\n", i);
  13009. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13010. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13011. if (INTEL_GEN(dev_priv) <= 3) {
  13012. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13013. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13014. }
  13015. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13016. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13017. if (INTEL_GEN(dev_priv) >= 4) {
  13018. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13019. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13020. }
  13021. err_printf(m, "Cursor [%d]:\n", i);
  13022. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13023. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13024. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13025. }
  13026. for (i = 0; i < error->num_transcoders; i++) {
  13027. err_printf(m, "CPU transcoder: %s\n",
  13028. transcoder_name(error->transcoder[i].cpu_transcoder));
  13029. err_printf(m, " Power: %s\n",
  13030. onoff(error->transcoder[i].power_domain_on));
  13031. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13032. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13033. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13034. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13035. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13036. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13037. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13038. }
  13039. }
  13040. #endif