intel_display.c 437 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489
  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 if (IS_GEN5(dev_priv))
  196. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  197. else
  198. return 270000;
  199. }
  200. static const struct intel_limit intel_limits_i8xx_dac = {
  201. .dot = { .min = 25000, .max = 350000 },
  202. .vco = { .min = 908000, .max = 1512000 },
  203. .n = { .min = 2, .max = 16 },
  204. .m = { .min = 96, .max = 140 },
  205. .m1 = { .min = 18, .max = 26 },
  206. .m2 = { .min = 6, .max = 16 },
  207. .p = { .min = 4, .max = 128 },
  208. .p1 = { .min = 2, .max = 33 },
  209. .p2 = { .dot_limit = 165000,
  210. .p2_slow = 4, .p2_fast = 2 },
  211. };
  212. static const struct intel_limit intel_limits_i8xx_dvo = {
  213. .dot = { .min = 25000, .max = 350000 },
  214. .vco = { .min = 908000, .max = 1512000 },
  215. .n = { .min = 2, .max = 16 },
  216. .m = { .min = 96, .max = 140 },
  217. .m1 = { .min = 18, .max = 26 },
  218. .m2 = { .min = 6, .max = 16 },
  219. .p = { .min = 4, .max = 128 },
  220. .p1 = { .min = 2, .max = 33 },
  221. .p2 = { .dot_limit = 165000,
  222. .p2_slow = 4, .p2_fast = 4 },
  223. };
  224. static const struct intel_limit intel_limits_i8xx_lvds = {
  225. .dot = { .min = 25000, .max = 350000 },
  226. .vco = { .min = 908000, .max = 1512000 },
  227. .n = { .min = 2, .max = 16 },
  228. .m = { .min = 96, .max = 140 },
  229. .m1 = { .min = 18, .max = 26 },
  230. .m2 = { .min = 6, .max = 16 },
  231. .p = { .min = 4, .max = 128 },
  232. .p1 = { .min = 1, .max = 6 },
  233. .p2 = { .dot_limit = 165000,
  234. .p2_slow = 14, .p2_fast = 7 },
  235. };
  236. static const struct intel_limit intel_limits_i9xx_sdvo = {
  237. .dot = { .min = 20000, .max = 400000 },
  238. .vco = { .min = 1400000, .max = 2800000 },
  239. .n = { .min = 1, .max = 6 },
  240. .m = { .min = 70, .max = 120 },
  241. .m1 = { .min = 8, .max = 18 },
  242. .m2 = { .min = 3, .max = 7 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 200000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const struct intel_limit intel_limits_i9xx_lvds = {
  249. .dot = { .min = 20000, .max = 400000 },
  250. .vco = { .min = 1400000, .max = 2800000 },
  251. .n = { .min = 1, .max = 6 },
  252. .m = { .min = 70, .max = 120 },
  253. .m1 = { .min = 8, .max = 18 },
  254. .m2 = { .min = 3, .max = 7 },
  255. .p = { .min = 7, .max = 98 },
  256. .p1 = { .min = 1, .max = 8 },
  257. .p2 = { .dot_limit = 112000,
  258. .p2_slow = 14, .p2_fast = 7 },
  259. };
  260. static const struct intel_limit intel_limits_g4x_sdvo = {
  261. .dot = { .min = 25000, .max = 270000 },
  262. .vco = { .min = 1750000, .max = 3500000},
  263. .n = { .min = 1, .max = 4 },
  264. .m = { .min = 104, .max = 138 },
  265. .m1 = { .min = 17, .max = 23 },
  266. .m2 = { .min = 5, .max = 11 },
  267. .p = { .min = 10, .max = 30 },
  268. .p1 = { .min = 1, .max = 3},
  269. .p2 = { .dot_limit = 270000,
  270. .p2_slow = 10,
  271. .p2_fast = 10
  272. },
  273. };
  274. static const struct intel_limit intel_limits_g4x_hdmi = {
  275. .dot = { .min = 22000, .max = 400000 },
  276. .vco = { .min = 1750000, .max = 3500000},
  277. .n = { .min = 1, .max = 4 },
  278. .m = { .min = 104, .max = 138 },
  279. .m1 = { .min = 16, .max = 23 },
  280. .m2 = { .min = 5, .max = 11 },
  281. .p = { .min = 5, .max = 80 },
  282. .p1 = { .min = 1, .max = 8},
  283. .p2 = { .dot_limit = 165000,
  284. .p2_slow = 10, .p2_fast = 5 },
  285. };
  286. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  287. .dot = { .min = 20000, .max = 115000 },
  288. .vco = { .min = 1750000, .max = 3500000 },
  289. .n = { .min = 1, .max = 3 },
  290. .m = { .min = 104, .max = 138 },
  291. .m1 = { .min = 17, .max = 23 },
  292. .m2 = { .min = 5, .max = 11 },
  293. .p = { .min = 28, .max = 112 },
  294. .p1 = { .min = 2, .max = 8 },
  295. .p2 = { .dot_limit = 0,
  296. .p2_slow = 14, .p2_fast = 14
  297. },
  298. };
  299. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  300. .dot = { .min = 80000, .max = 224000 },
  301. .vco = { .min = 1750000, .max = 3500000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 104, .max = 138 },
  304. .m1 = { .min = 17, .max = 23 },
  305. .m2 = { .min = 5, .max = 11 },
  306. .p = { .min = 14, .max = 42 },
  307. .p1 = { .min = 2, .max = 6 },
  308. .p2 = { .dot_limit = 0,
  309. .p2_slow = 7, .p2_fast = 7
  310. },
  311. };
  312. static const struct intel_limit intel_limits_pineview_sdvo = {
  313. .dot = { .min = 20000, .max = 400000},
  314. .vco = { .min = 1700000, .max = 3500000 },
  315. /* Pineview's Ncounter is a ring counter */
  316. .n = { .min = 3, .max = 6 },
  317. .m = { .min = 2, .max = 256 },
  318. /* Pineview only has one combined m divider, which we treat as m2. */
  319. .m1 = { .min = 0, .max = 0 },
  320. .m2 = { .min = 0, .max = 254 },
  321. .p = { .min = 5, .max = 80 },
  322. .p1 = { .min = 1, .max = 8 },
  323. .p2 = { .dot_limit = 200000,
  324. .p2_slow = 10, .p2_fast = 5 },
  325. };
  326. static const struct intel_limit intel_limits_pineview_lvds = {
  327. .dot = { .min = 20000, .max = 400000 },
  328. .vco = { .min = 1700000, .max = 3500000 },
  329. .n = { .min = 3, .max = 6 },
  330. .m = { .min = 2, .max = 256 },
  331. .m1 = { .min = 0, .max = 0 },
  332. .m2 = { .min = 0, .max = 254 },
  333. .p = { .min = 7, .max = 112 },
  334. .p1 = { .min = 1, .max = 8 },
  335. .p2 = { .dot_limit = 112000,
  336. .p2_slow = 14, .p2_fast = 14 },
  337. };
  338. /* Ironlake / Sandybridge
  339. *
  340. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  341. * the range value for them is (actual_value - 2).
  342. */
  343. static const struct intel_limit intel_limits_ironlake_dac = {
  344. .dot = { .min = 25000, .max = 350000 },
  345. .vco = { .min = 1760000, .max = 3510000 },
  346. .n = { .min = 1, .max = 5 },
  347. .m = { .min = 79, .max = 127 },
  348. .m1 = { .min = 12, .max = 22 },
  349. .m2 = { .min = 5, .max = 9 },
  350. .p = { .min = 5, .max = 80 },
  351. .p1 = { .min = 1, .max = 8 },
  352. .p2 = { .dot_limit = 225000,
  353. .p2_slow = 10, .p2_fast = 5 },
  354. };
  355. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  356. .dot = { .min = 25000, .max = 350000 },
  357. .vco = { .min = 1760000, .max = 3510000 },
  358. .n = { .min = 1, .max = 3 },
  359. .m = { .min = 79, .max = 118 },
  360. .m1 = { .min = 12, .max = 22 },
  361. .m2 = { .min = 5, .max = 9 },
  362. .p = { .min = 28, .max = 112 },
  363. .p1 = { .min = 2, .max = 8 },
  364. .p2 = { .dot_limit = 225000,
  365. .p2_slow = 14, .p2_fast = 14 },
  366. };
  367. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  368. .dot = { .min = 25000, .max = 350000 },
  369. .vco = { .min = 1760000, .max = 3510000 },
  370. .n = { .min = 1, .max = 3 },
  371. .m = { .min = 79, .max = 127 },
  372. .m1 = { .min = 12, .max = 22 },
  373. .m2 = { .min = 5, .max = 9 },
  374. .p = { .min = 14, .max = 56 },
  375. .p1 = { .min = 2, .max = 8 },
  376. .p2 = { .dot_limit = 225000,
  377. .p2_slow = 7, .p2_fast = 7 },
  378. };
  379. /* LVDS 100mhz refclk limits. */
  380. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  381. .dot = { .min = 25000, .max = 350000 },
  382. .vco = { .min = 1760000, .max = 3510000 },
  383. .n = { .min = 1, .max = 2 },
  384. .m = { .min = 79, .max = 126 },
  385. .m1 = { .min = 12, .max = 22 },
  386. .m2 = { .min = 5, .max = 9 },
  387. .p = { .min = 28, .max = 112 },
  388. .p1 = { .min = 2, .max = 8 },
  389. .p2 = { .dot_limit = 225000,
  390. .p2_slow = 14, .p2_fast = 14 },
  391. };
  392. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  393. .dot = { .min = 25000, .max = 350000 },
  394. .vco = { .min = 1760000, .max = 3510000 },
  395. .n = { .min = 1, .max = 3 },
  396. .m = { .min = 79, .max = 126 },
  397. .m1 = { .min = 12, .max = 22 },
  398. .m2 = { .min = 5, .max = 9 },
  399. .p = { .min = 14, .max = 42 },
  400. .p1 = { .min = 2, .max = 6 },
  401. .p2 = { .dot_limit = 225000,
  402. .p2_slow = 7, .p2_fast = 7 },
  403. };
  404. static const struct intel_limit intel_limits_vlv = {
  405. /*
  406. * These are the data rate limits (measured in fast clocks)
  407. * since those are the strictest limits we have. The fast
  408. * clock and actual rate limits are more relaxed, so checking
  409. * them would make no difference.
  410. */
  411. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  412. .vco = { .min = 4000000, .max = 6000000 },
  413. .n = { .min = 1, .max = 7 },
  414. .m1 = { .min = 2, .max = 3 },
  415. .m2 = { .min = 11, .max = 156 },
  416. .p1 = { .min = 2, .max = 3 },
  417. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  418. };
  419. static const struct intel_limit intel_limits_chv = {
  420. /*
  421. * These are the data rate limits (measured in fast clocks)
  422. * since those are the strictest limits we have. The fast
  423. * clock and actual rate limits are more relaxed, so checking
  424. * them would make no difference.
  425. */
  426. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  427. .vco = { .min = 4800000, .max = 6480000 },
  428. .n = { .min = 1, .max = 1 },
  429. .m1 = { .min = 2, .max = 2 },
  430. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  431. .p1 = { .min = 2, .max = 4 },
  432. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  433. };
  434. static const struct intel_limit intel_limits_bxt = {
  435. /* FIXME: find real dot limits */
  436. .dot = { .min = 0, .max = INT_MAX },
  437. .vco = { .min = 4800000, .max = 6700000 },
  438. .n = { .min = 1, .max = 1 },
  439. .m1 = { .min = 2, .max = 2 },
  440. /* FIXME: find real m2 limits */
  441. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  442. .p1 = { .min = 2, .max = 4 },
  443. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  444. };
  445. static bool
  446. needs_modeset(struct drm_crtc_state *state)
  447. {
  448. return drm_atomic_crtc_needs_modeset(state);
  449. }
  450. /*
  451. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  452. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  453. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  454. * The helpers' return value is the rate of the clock that is fed to the
  455. * display engine's pipe which can be the above fast dot clock rate or a
  456. * divided-down version of it.
  457. */
  458. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  459. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  460. {
  461. clock->m = clock->m2 + 2;
  462. clock->p = clock->p1 * clock->p2;
  463. if (WARN_ON(clock->n == 0 || clock->p == 0))
  464. return 0;
  465. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  466. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  467. return clock->dot;
  468. }
  469. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  470. {
  471. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  472. }
  473. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  474. {
  475. clock->m = i9xx_dpll_compute_m(clock);
  476. clock->p = clock->p1 * clock->p2;
  477. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  478. return 0;
  479. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  480. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  481. return clock->dot;
  482. }
  483. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  484. {
  485. clock->m = clock->m1 * clock->m2;
  486. clock->p = clock->p1 * clock->p2;
  487. if (WARN_ON(clock->n == 0 || clock->p == 0))
  488. return 0;
  489. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  490. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  491. return clock->dot / 5;
  492. }
  493. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  494. {
  495. clock->m = clock->m1 * clock->m2;
  496. clock->p = clock->p1 * clock->p2;
  497. if (WARN_ON(clock->n == 0 || clock->p == 0))
  498. return 0;
  499. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  500. clock->n << 22);
  501. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  502. return clock->dot / 5;
  503. }
  504. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  505. /**
  506. * Returns whether the given set of divisors are valid for a given refclk with
  507. * the given connectors.
  508. */
  509. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  510. const struct intel_limit *limit,
  511. const struct dpll *clock)
  512. {
  513. if (clock->n < limit->n.min || limit->n.max < clock->n)
  514. INTELPllInvalid("n out of range\n");
  515. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  516. INTELPllInvalid("p1 out of range\n");
  517. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  518. INTELPllInvalid("m2 out of range\n");
  519. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  520. INTELPllInvalid("m1 out of range\n");
  521. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  522. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  523. if (clock->m1 <= clock->m2)
  524. INTELPllInvalid("m1 <= m2\n");
  525. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  526. !IS_GEN9_LP(dev_priv)) {
  527. if (clock->p < limit->p.min || limit->p.max < clock->p)
  528. INTELPllInvalid("p out of range\n");
  529. if (clock->m < limit->m.min || limit->m.max < clock->m)
  530. INTELPllInvalid("m out of range\n");
  531. }
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static int
  542. i9xx_select_p2_div(const struct intel_limit *limit,
  543. const struct intel_crtc_state *crtc_state,
  544. int target)
  545. {
  546. struct drm_device *dev = crtc_state->base.crtc->dev;
  547. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  548. /*
  549. * For LVDS just rely on its current settings for dual-channel.
  550. * We haven't figured out how to reliably set up different
  551. * single/dual channel state, if we even can.
  552. */
  553. if (intel_is_dual_link_lvds(dev))
  554. return limit->p2.p2_fast;
  555. else
  556. return limit->p2.p2_slow;
  557. } else {
  558. if (target < limit->p2.dot_limit)
  559. return limit->p2.p2_slow;
  560. else
  561. return limit->p2.p2_fast;
  562. }
  563. }
  564. /*
  565. * Returns a set of divisors for the desired target clock with the given
  566. * refclk, or FALSE. The returned values represent the clock equation:
  567. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  568. *
  569. * Target and reference clocks are specified in kHz.
  570. *
  571. * If match_clock is provided, then best_clock P divider must match the P
  572. * divider from @match_clock used for LVDS downclocking.
  573. */
  574. static bool
  575. i9xx_find_best_dpll(const struct intel_limit *limit,
  576. struct intel_crtc_state *crtc_state,
  577. int target, int refclk, struct dpll *match_clock,
  578. struct dpll *best_clock)
  579. {
  580. struct drm_device *dev = crtc_state->base.crtc->dev;
  581. struct dpll clock;
  582. int err = target;
  583. memset(best_clock, 0, sizeof(*best_clock));
  584. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  585. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  586. clock.m1++) {
  587. for (clock.m2 = limit->m2.min;
  588. clock.m2 <= limit->m2.max; clock.m2++) {
  589. if (clock.m2 >= clock.m1)
  590. break;
  591. for (clock.n = limit->n.min;
  592. clock.n <= limit->n.max; clock.n++) {
  593. for (clock.p1 = limit->p1.min;
  594. clock.p1 <= limit->p1.max; clock.p1++) {
  595. int this_err;
  596. i9xx_calc_dpll_params(refclk, &clock);
  597. if (!intel_PLL_is_valid(to_i915(dev),
  598. limit,
  599. &clock))
  600. continue;
  601. if (match_clock &&
  602. clock.p != match_clock->p)
  603. continue;
  604. this_err = abs(clock.dot - target);
  605. if (this_err < err) {
  606. *best_clock = clock;
  607. err = this_err;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. return (err != target);
  614. }
  615. /*
  616. * Returns a set of divisors for the desired target clock with the given
  617. * refclk, or FALSE. The returned values represent the clock equation:
  618. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  619. *
  620. * Target and reference clocks are specified in kHz.
  621. *
  622. * If match_clock is provided, then best_clock P divider must match the P
  623. * divider from @match_clock used for LVDS downclocking.
  624. */
  625. static bool
  626. pnv_find_best_dpll(const struct intel_limit *limit,
  627. struct intel_crtc_state *crtc_state,
  628. int target, int refclk, struct dpll *match_clock,
  629. struct dpll *best_clock)
  630. {
  631. struct drm_device *dev = crtc_state->base.crtc->dev;
  632. struct dpll clock;
  633. int err = target;
  634. memset(best_clock, 0, sizeof(*best_clock));
  635. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  636. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  637. clock.m1++) {
  638. for (clock.m2 = limit->m2.min;
  639. clock.m2 <= limit->m2.max; clock.m2++) {
  640. for (clock.n = limit->n.min;
  641. clock.n <= limit->n.max; clock.n++) {
  642. for (clock.p1 = limit->p1.min;
  643. clock.p1 <= limit->p1.max; clock.p1++) {
  644. int this_err;
  645. pnv_calc_dpll_params(refclk, &clock);
  646. if (!intel_PLL_is_valid(to_i915(dev),
  647. limit,
  648. &clock))
  649. continue;
  650. if (match_clock &&
  651. clock.p != match_clock->p)
  652. continue;
  653. this_err = abs(clock.dot - target);
  654. if (this_err < err) {
  655. *best_clock = clock;
  656. err = this_err;
  657. }
  658. }
  659. }
  660. }
  661. }
  662. return (err != target);
  663. }
  664. /*
  665. * Returns a set of divisors for the desired target clock with the given
  666. * refclk, or FALSE. The returned values represent the clock equation:
  667. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  668. *
  669. * Target and reference clocks are specified in kHz.
  670. *
  671. * If match_clock is provided, then best_clock P divider must match the P
  672. * divider from @match_clock used for LVDS downclocking.
  673. */
  674. static bool
  675. g4x_find_best_dpll(const struct intel_limit *limit,
  676. struct intel_crtc_state *crtc_state,
  677. int target, int refclk, struct dpll *match_clock,
  678. struct dpll *best_clock)
  679. {
  680. struct drm_device *dev = crtc_state->base.crtc->dev;
  681. struct dpll clock;
  682. int max_n;
  683. bool found = false;
  684. /* approximately equals target * 0.00585 */
  685. int err_most = (target >> 8) + (target >> 9);
  686. memset(best_clock, 0, sizeof(*best_clock));
  687. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  688. max_n = limit->n.max;
  689. /* based on hardware requirement, prefer smaller n to precision */
  690. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  691. /* based on hardware requirement, prefere larger m1,m2 */
  692. for (clock.m1 = limit->m1.max;
  693. clock.m1 >= limit->m1.min; clock.m1--) {
  694. for (clock.m2 = limit->m2.max;
  695. clock.m2 >= limit->m2.min; clock.m2--) {
  696. for (clock.p1 = limit->p1.max;
  697. clock.p1 >= limit->p1.min; clock.p1--) {
  698. int this_err;
  699. i9xx_calc_dpll_params(refclk, &clock);
  700. if (!intel_PLL_is_valid(to_i915(dev),
  701. limit,
  702. &clock))
  703. continue;
  704. this_err = abs(clock.dot - target);
  705. if (this_err < err_most) {
  706. *best_clock = clock;
  707. err_most = this_err;
  708. max_n = clock.n;
  709. found = true;
  710. }
  711. }
  712. }
  713. }
  714. }
  715. return found;
  716. }
  717. /*
  718. * Check if the calculated PLL configuration is more optimal compared to the
  719. * best configuration and error found so far. Return the calculated error.
  720. */
  721. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  722. const struct dpll *calculated_clock,
  723. const struct dpll *best_clock,
  724. unsigned int best_error_ppm,
  725. unsigned int *error_ppm)
  726. {
  727. /*
  728. * For CHV ignore the error and consider only the P value.
  729. * Prefer a bigger P value based on HW requirements.
  730. */
  731. if (IS_CHERRYVIEW(to_i915(dev))) {
  732. *error_ppm = 0;
  733. return calculated_clock->p > best_clock->p;
  734. }
  735. if (WARN_ON_ONCE(!target_freq))
  736. return false;
  737. *error_ppm = div_u64(1000000ULL *
  738. abs(target_freq - calculated_clock->dot),
  739. target_freq);
  740. /*
  741. * Prefer a better P value over a better (smaller) error if the error
  742. * is small. Ensure this preference for future configurations too by
  743. * setting the error to 0.
  744. */
  745. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  746. *error_ppm = 0;
  747. return true;
  748. }
  749. return *error_ppm + 10 < best_error_ppm;
  750. }
  751. /*
  752. * Returns a set of divisors for the desired target clock with the given
  753. * refclk, or FALSE. The returned values represent the clock equation:
  754. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  755. */
  756. static bool
  757. vlv_find_best_dpll(const struct intel_limit *limit,
  758. struct intel_crtc_state *crtc_state,
  759. int target, int refclk, struct dpll *match_clock,
  760. struct dpll *best_clock)
  761. {
  762. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  763. struct drm_device *dev = crtc->base.dev;
  764. struct dpll clock;
  765. unsigned int bestppm = 1000000;
  766. /* min update 19.2 MHz */
  767. int max_n = min(limit->n.max, refclk / 19200);
  768. bool found = false;
  769. target *= 5; /* fast clock */
  770. memset(best_clock, 0, sizeof(*best_clock));
  771. /* based on hardware requirement, prefer smaller n to precision */
  772. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  773. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  774. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  775. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  776. clock.p = clock.p1 * clock.p2;
  777. /* based on hardware requirement, prefer bigger m1,m2 values */
  778. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  779. unsigned int ppm;
  780. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  781. refclk * clock.m1);
  782. vlv_calc_dpll_params(refclk, &clock);
  783. if (!intel_PLL_is_valid(to_i915(dev),
  784. limit,
  785. &clock))
  786. continue;
  787. if (!vlv_PLL_is_optimal(dev, target,
  788. &clock,
  789. best_clock,
  790. bestppm, &ppm))
  791. continue;
  792. *best_clock = clock;
  793. bestppm = ppm;
  794. found = true;
  795. }
  796. }
  797. }
  798. }
  799. return found;
  800. }
  801. /*
  802. * Returns a set of divisors for the desired target clock with the given
  803. * refclk, or FALSE. The returned values represent the clock equation:
  804. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  805. */
  806. static bool
  807. chv_find_best_dpll(const struct intel_limit *limit,
  808. struct intel_crtc_state *crtc_state,
  809. int target, int refclk, struct dpll *match_clock,
  810. struct dpll *best_clock)
  811. {
  812. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  813. struct drm_device *dev = crtc->base.dev;
  814. unsigned int best_error_ppm;
  815. struct dpll clock;
  816. uint64_t m2;
  817. int found = false;
  818. memset(best_clock, 0, sizeof(*best_clock));
  819. best_error_ppm = 1000000;
  820. /*
  821. * Based on hardware doc, the n always set to 1, and m1 always
  822. * set to 2. If requires to support 200Mhz refclk, we need to
  823. * revisit this because n may not 1 anymore.
  824. */
  825. clock.n = 1, clock.m1 = 2;
  826. target *= 5; /* fast clock */
  827. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  828. for (clock.p2 = limit->p2.p2_fast;
  829. clock.p2 >= limit->p2.p2_slow;
  830. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  831. unsigned int error_ppm;
  832. clock.p = clock.p1 * clock.p2;
  833. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  834. clock.n) << 22, refclk * clock.m1);
  835. if (m2 > INT_MAX/clock.m1)
  836. continue;
  837. clock.m2 = m2;
  838. chv_calc_dpll_params(refclk, &clock);
  839. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  840. continue;
  841. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  842. best_error_ppm, &error_ppm))
  843. continue;
  844. *best_clock = clock;
  845. best_error_ppm = error_ppm;
  846. found = true;
  847. }
  848. }
  849. return found;
  850. }
  851. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  852. struct dpll *best_clock)
  853. {
  854. int refclk = 100000;
  855. const struct intel_limit *limit = &intel_limits_bxt;
  856. return chv_find_best_dpll(limit, crtc_state,
  857. target_clock, refclk, NULL, best_clock);
  858. }
  859. bool intel_crtc_active(struct intel_crtc *crtc)
  860. {
  861. /* Be paranoid as we can arrive here with only partial
  862. * state retrieved from the hardware during setup.
  863. *
  864. * We can ditch the adjusted_mode.crtc_clock check as soon
  865. * as Haswell has gained clock readout/fastboot support.
  866. *
  867. * We can ditch the crtc->primary->fb check as soon as we can
  868. * properly reconstruct framebuffers.
  869. *
  870. * FIXME: The intel_crtc->active here should be switched to
  871. * crtc->state->active once we have proper CRTC states wired up
  872. * for atomic.
  873. */
  874. return crtc->active && crtc->base.primary->state->fb &&
  875. crtc->config->base.adjusted_mode.crtc_clock;
  876. }
  877. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  878. enum pipe pipe)
  879. {
  880. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  881. return crtc->config->cpu_transcoder;
  882. }
  883. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  884. {
  885. i915_reg_t reg = PIPEDSL(pipe);
  886. u32 line1, line2;
  887. u32 line_mask;
  888. if (IS_GEN2(dev_priv))
  889. line_mask = DSL_LINEMASK_GEN2;
  890. else
  891. line_mask = DSL_LINEMASK_GEN3;
  892. line1 = I915_READ(reg) & line_mask;
  893. msleep(5);
  894. line2 = I915_READ(reg) & line_mask;
  895. return line1 == line2;
  896. }
  897. /*
  898. * intel_wait_for_pipe_off - wait for pipe to turn off
  899. * @crtc: crtc whose pipe to wait for
  900. *
  901. * After disabling a pipe, we can't wait for vblank in the usual way,
  902. * spinning on the vblank interrupt status bit, since we won't actually
  903. * see an interrupt when the pipe is disabled.
  904. *
  905. * On Gen4 and above:
  906. * wait for the pipe register state bit to turn off
  907. *
  908. * Otherwise:
  909. * wait for the display line value to settle (it usually
  910. * ends up stopping at the start of the next frame).
  911. *
  912. */
  913. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  914. {
  915. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  916. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  917. enum pipe pipe = crtc->pipe;
  918. if (INTEL_GEN(dev_priv) >= 4) {
  919. i915_reg_t reg = PIPECONF(cpu_transcoder);
  920. /* Wait for the Pipe State to go off */
  921. if (intel_wait_for_register(dev_priv,
  922. reg, I965_PIPECONF_ACTIVE, 0,
  923. 100))
  924. WARN(1, "pipe_off wait timed out\n");
  925. } else {
  926. /* Wait for the display line to settle */
  927. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  928. WARN(1, "pipe_off wait timed out\n");
  929. }
  930. }
  931. /* Only for pre-ILK configs */
  932. void assert_pll(struct drm_i915_private *dev_priv,
  933. enum pipe pipe, bool state)
  934. {
  935. u32 val;
  936. bool cur_state;
  937. val = I915_READ(DPLL(pipe));
  938. cur_state = !!(val & DPLL_VCO_ENABLE);
  939. I915_STATE_WARN(cur_state != state,
  940. "PLL state assertion failure (expected %s, current %s)\n",
  941. onoff(state), onoff(cur_state));
  942. }
  943. /* XXX: the dsi pll is shared between MIPI DSI ports */
  944. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  945. {
  946. u32 val;
  947. bool cur_state;
  948. mutex_lock(&dev_priv->sb_lock);
  949. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  950. mutex_unlock(&dev_priv->sb_lock);
  951. cur_state = val & DSI_PLL_VCO_EN;
  952. I915_STATE_WARN(cur_state != state,
  953. "DSI PLL state assertion failure (expected %s, current %s)\n",
  954. onoff(state), onoff(cur_state));
  955. }
  956. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  957. enum pipe pipe, bool state)
  958. {
  959. bool cur_state;
  960. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  961. pipe);
  962. if (HAS_DDI(dev_priv)) {
  963. /* DDI does not have a specific FDI_TX register */
  964. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  965. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  966. } else {
  967. u32 val = I915_READ(FDI_TX_CTL(pipe));
  968. cur_state = !!(val & FDI_TX_ENABLE);
  969. }
  970. I915_STATE_WARN(cur_state != state,
  971. "FDI TX state assertion failure (expected %s, current %s)\n",
  972. onoff(state), onoff(cur_state));
  973. }
  974. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  975. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  976. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  977. enum pipe pipe, bool state)
  978. {
  979. u32 val;
  980. bool cur_state;
  981. val = I915_READ(FDI_RX_CTL(pipe));
  982. cur_state = !!(val & FDI_RX_ENABLE);
  983. I915_STATE_WARN(cur_state != state,
  984. "FDI RX state assertion failure (expected %s, current %s)\n",
  985. onoff(state), onoff(cur_state));
  986. }
  987. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  988. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  989. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (IS_GEN5(dev_priv))
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv))
  998. return;
  999. val = I915_READ(FDI_TX_CTL(pipe));
  1000. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1001. }
  1002. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe, bool state)
  1004. {
  1005. u32 val;
  1006. bool cur_state;
  1007. val = I915_READ(FDI_RX_CTL(pipe));
  1008. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1009. I915_STATE_WARN(cur_state != state,
  1010. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1011. onoff(state), onoff(cur_state));
  1012. }
  1013. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1014. {
  1015. i915_reg_t pp_reg;
  1016. u32 val;
  1017. enum pipe panel_pipe = PIPE_A;
  1018. bool locked = true;
  1019. if (WARN_ON(HAS_DDI(dev_priv)))
  1020. return;
  1021. if (HAS_PCH_SPLIT(dev_priv)) {
  1022. u32 port_sel;
  1023. pp_reg = PP_CONTROL(0);
  1024. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1025. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1026. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1027. panel_pipe = PIPE_B;
  1028. /* XXX: else fix for eDP */
  1029. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1030. /* presumably write lock depends on pipe, not port select */
  1031. pp_reg = PP_CONTROL(pipe);
  1032. panel_pipe = pipe;
  1033. } else {
  1034. pp_reg = PP_CONTROL(0);
  1035. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1036. panel_pipe = PIPE_B;
  1037. }
  1038. val = I915_READ(pp_reg);
  1039. if (!(val & PANEL_POWER_ON) ||
  1040. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1041. locked = false;
  1042. I915_STATE_WARN(panel_pipe == pipe && locked,
  1043. "panel assertion failure, pipe %c regs locked\n",
  1044. pipe_name(pipe));
  1045. }
  1046. static void assert_cursor(struct drm_i915_private *dev_priv,
  1047. enum pipe pipe, bool state)
  1048. {
  1049. bool cur_state;
  1050. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1051. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1052. else
  1053. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1054. I915_STATE_WARN(cur_state != state,
  1055. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1056. pipe_name(pipe), onoff(state), onoff(cur_state));
  1057. }
  1058. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1059. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1060. void assert_pipe(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe, bool state)
  1062. {
  1063. bool cur_state;
  1064. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1065. pipe);
  1066. enum intel_display_power_domain power_domain;
  1067. /* we keep both pipes enabled on 830 */
  1068. if (IS_I830(dev_priv))
  1069. state = true;
  1070. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1071. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1072. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1073. cur_state = !!(val & PIPECONF_ENABLE);
  1074. intel_display_power_put(dev_priv, power_domain);
  1075. } else {
  1076. cur_state = false;
  1077. }
  1078. I915_STATE_WARN(cur_state != state,
  1079. "pipe %c assertion failure (expected %s, current %s)\n",
  1080. pipe_name(pipe), onoff(state), onoff(cur_state));
  1081. }
  1082. static void assert_plane(struct drm_i915_private *dev_priv,
  1083. enum plane plane, bool state)
  1084. {
  1085. u32 val;
  1086. bool cur_state;
  1087. val = I915_READ(DSPCNTR(plane));
  1088. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1089. I915_STATE_WARN(cur_state != state,
  1090. "plane %c assertion failure (expected %s, current %s)\n",
  1091. plane_name(plane), onoff(state), onoff(cur_state));
  1092. }
  1093. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1094. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1095. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1096. enum pipe pipe)
  1097. {
  1098. int i;
  1099. /* Primary planes are fixed to pipes on gen4+ */
  1100. if (INTEL_GEN(dev_priv) >= 4) {
  1101. u32 val = I915_READ(DSPCNTR(pipe));
  1102. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1103. "plane %c assertion failure, should be disabled but not\n",
  1104. plane_name(pipe));
  1105. return;
  1106. }
  1107. /* Need to check both planes against the pipe */
  1108. for_each_pipe(dev_priv, i) {
  1109. u32 val = I915_READ(DSPCNTR(i));
  1110. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1111. DISPPLANE_SEL_PIPE_SHIFT;
  1112. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1113. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1114. plane_name(i), pipe_name(pipe));
  1115. }
  1116. }
  1117. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe)
  1119. {
  1120. int sprite;
  1121. if (INTEL_GEN(dev_priv) >= 9) {
  1122. for_each_sprite(dev_priv, pipe, sprite) {
  1123. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1124. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1125. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1126. sprite, pipe_name(pipe));
  1127. }
  1128. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1129. for_each_sprite(dev_priv, pipe, sprite) {
  1130. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1131. I915_STATE_WARN(val & SP_ENABLE,
  1132. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1133. sprite_name(pipe, sprite), pipe_name(pipe));
  1134. }
  1135. } else if (INTEL_GEN(dev_priv) >= 7) {
  1136. u32 val = I915_READ(SPRCTL(pipe));
  1137. I915_STATE_WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  1141. u32 val = I915_READ(DVSCNTR(pipe));
  1142. I915_STATE_WARN(val & DVS_ENABLE,
  1143. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1144. plane_name(pipe), pipe_name(pipe));
  1145. }
  1146. }
  1147. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1148. {
  1149. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1150. drm_crtc_vblank_put(crtc);
  1151. }
  1152. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. u32 val;
  1156. bool enabled;
  1157. val = I915_READ(PCH_TRANSCONF(pipe));
  1158. enabled = !!(val & TRANS_ENABLE);
  1159. I915_STATE_WARN(enabled,
  1160. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1161. pipe_name(pipe));
  1162. }
  1163. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1164. enum pipe pipe, u32 port_sel, u32 val)
  1165. {
  1166. if ((val & DP_PORT_EN) == 0)
  1167. return false;
  1168. if (HAS_PCH_CPT(dev_priv)) {
  1169. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1170. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1171. return false;
  1172. } else if (IS_CHERRYVIEW(dev_priv)) {
  1173. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1174. return false;
  1175. } else {
  1176. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1177. return false;
  1178. }
  1179. return true;
  1180. }
  1181. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1182. enum pipe pipe, u32 val)
  1183. {
  1184. if ((val & SDVO_ENABLE) == 0)
  1185. return false;
  1186. if (HAS_PCH_CPT(dev_priv)) {
  1187. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1188. return false;
  1189. } else if (IS_CHERRYVIEW(dev_priv)) {
  1190. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1191. return false;
  1192. } else {
  1193. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1194. return false;
  1195. }
  1196. return true;
  1197. }
  1198. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe, u32 val)
  1200. {
  1201. if ((val & LVDS_PORT_EN) == 0)
  1202. return false;
  1203. if (HAS_PCH_CPT(dev_priv)) {
  1204. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1205. return false;
  1206. } else {
  1207. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1208. return false;
  1209. }
  1210. return true;
  1211. }
  1212. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1213. enum pipe pipe, u32 val)
  1214. {
  1215. if ((val & ADPA_DAC_ENABLE) == 0)
  1216. return false;
  1217. if (HAS_PCH_CPT(dev_priv)) {
  1218. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1219. return false;
  1220. } else {
  1221. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1222. return false;
  1223. }
  1224. return true;
  1225. }
  1226. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1227. enum pipe pipe, i915_reg_t reg,
  1228. u32 port_sel)
  1229. {
  1230. u32 val = I915_READ(reg);
  1231. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1232. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1233. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1234. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1235. && (val & DP_PIPEB_SELECT),
  1236. "IBX PCH dp port still using transcoder B\n");
  1237. }
  1238. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1239. enum pipe pipe, i915_reg_t reg)
  1240. {
  1241. u32 val = I915_READ(reg);
  1242. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1243. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1244. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1245. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1246. && (val & SDVO_PIPE_B_SELECT),
  1247. "IBX PCH hdmi port still using transcoder B\n");
  1248. }
  1249. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe)
  1251. {
  1252. u32 val;
  1253. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1254. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1255. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1256. val = I915_READ(PCH_ADPA);
  1257. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1258. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1259. pipe_name(pipe));
  1260. val = I915_READ(PCH_LVDS);
  1261. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1262. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1263. pipe_name(pipe));
  1264. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1265. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1266. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1267. }
  1268. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1269. const struct intel_crtc_state *pipe_config)
  1270. {
  1271. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1272. enum pipe pipe = crtc->pipe;
  1273. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1274. POSTING_READ(DPLL(pipe));
  1275. udelay(150);
  1276. if (intel_wait_for_register(dev_priv,
  1277. DPLL(pipe),
  1278. DPLL_LOCK_VLV,
  1279. DPLL_LOCK_VLV,
  1280. 1))
  1281. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1282. }
  1283. static void vlv_enable_pll(struct intel_crtc *crtc,
  1284. const struct intel_crtc_state *pipe_config)
  1285. {
  1286. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1287. enum pipe pipe = crtc->pipe;
  1288. assert_pipe_disabled(dev_priv, pipe);
  1289. /* PLL is protected by panel, make sure we can write it */
  1290. assert_panel_unlocked(dev_priv, pipe);
  1291. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1292. _vlv_enable_pll(crtc, pipe_config);
  1293. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1294. POSTING_READ(DPLL_MD(pipe));
  1295. }
  1296. static void _chv_enable_pll(struct intel_crtc *crtc,
  1297. const struct intel_crtc_state *pipe_config)
  1298. {
  1299. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1300. enum pipe pipe = crtc->pipe;
  1301. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1302. u32 tmp;
  1303. mutex_lock(&dev_priv->sb_lock);
  1304. /* Enable back the 10bit clock to display controller */
  1305. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1306. tmp |= DPIO_DCLKP_EN;
  1307. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1308. mutex_unlock(&dev_priv->sb_lock);
  1309. /*
  1310. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1311. */
  1312. udelay(1);
  1313. /* Enable PLL */
  1314. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1315. /* Check PLL is locked */
  1316. if (intel_wait_for_register(dev_priv,
  1317. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1318. 1))
  1319. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1320. }
  1321. static void chv_enable_pll(struct intel_crtc *crtc,
  1322. const struct intel_crtc_state *pipe_config)
  1323. {
  1324. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1325. enum pipe pipe = crtc->pipe;
  1326. assert_pipe_disabled(dev_priv, pipe);
  1327. /* PLL is protected by panel, make sure we can write it */
  1328. assert_panel_unlocked(dev_priv, pipe);
  1329. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1330. _chv_enable_pll(crtc, pipe_config);
  1331. if (pipe != PIPE_A) {
  1332. /*
  1333. * WaPixelRepeatModeFixForC0:chv
  1334. *
  1335. * DPLLCMD is AWOL. Use chicken bits to propagate
  1336. * the value from DPLLBMD to either pipe B or C.
  1337. */
  1338. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1339. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1340. I915_WRITE(CBR4_VLV, 0);
  1341. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1342. /*
  1343. * DPLLB VGA mode also seems to cause problems.
  1344. * We should always have it disabled.
  1345. */
  1346. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1347. } else {
  1348. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1349. POSTING_READ(DPLL_MD(pipe));
  1350. }
  1351. }
  1352. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1353. {
  1354. struct intel_crtc *crtc;
  1355. int count = 0;
  1356. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1357. count += crtc->base.state->active &&
  1358. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1359. }
  1360. return count;
  1361. }
  1362. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1363. {
  1364. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1365. i915_reg_t reg = DPLL(crtc->pipe);
  1366. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1367. int i;
  1368. assert_pipe_disabled(dev_priv, crtc->pipe);
  1369. /* PLL is protected by panel, make sure we can write it */
  1370. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1371. assert_panel_unlocked(dev_priv, crtc->pipe);
  1372. /* Enable DVO 2x clock on both PLLs if necessary */
  1373. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1374. /*
  1375. * It appears to be important that we don't enable this
  1376. * for the current pipe before otherwise configuring the
  1377. * PLL. No idea how this should be handled if multiple
  1378. * DVO outputs are enabled simultaneosly.
  1379. */
  1380. dpll |= DPLL_DVO_2X_MODE;
  1381. I915_WRITE(DPLL(!crtc->pipe),
  1382. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1383. }
  1384. /*
  1385. * Apparently we need to have VGA mode enabled prior to changing
  1386. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1387. * dividers, even though the register value does change.
  1388. */
  1389. I915_WRITE(reg, 0);
  1390. I915_WRITE(reg, dpll);
  1391. /* Wait for the clocks to stabilize. */
  1392. POSTING_READ(reg);
  1393. udelay(150);
  1394. if (INTEL_GEN(dev_priv) >= 4) {
  1395. I915_WRITE(DPLL_MD(crtc->pipe),
  1396. crtc->config->dpll_hw_state.dpll_md);
  1397. } else {
  1398. /* The pixel multiplier can only be updated once the
  1399. * DPLL is enabled and the clocks are stable.
  1400. *
  1401. * So write it again.
  1402. */
  1403. I915_WRITE(reg, dpll);
  1404. }
  1405. /* We do this three times for luck */
  1406. for (i = 0; i < 3; i++) {
  1407. I915_WRITE(reg, dpll);
  1408. POSTING_READ(reg);
  1409. udelay(150); /* wait for warmup */
  1410. }
  1411. }
  1412. /**
  1413. * i9xx_disable_pll - disable a PLL
  1414. * @dev_priv: i915 private structure
  1415. * @pipe: pipe PLL to disable
  1416. *
  1417. * Disable the PLL for @pipe, making sure the pipe is off first.
  1418. *
  1419. * Note! This is for pre-ILK only.
  1420. */
  1421. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1422. {
  1423. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1424. enum pipe pipe = crtc->pipe;
  1425. /* Disable DVO 2x clock on both PLLs if necessary */
  1426. if (IS_I830(dev_priv) &&
  1427. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1428. !intel_num_dvo_pipes(dev_priv)) {
  1429. I915_WRITE(DPLL(PIPE_B),
  1430. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1431. I915_WRITE(DPLL(PIPE_A),
  1432. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1433. }
  1434. /* Don't disable pipe or pipe PLLs if needed */
  1435. if (IS_I830(dev_priv))
  1436. return;
  1437. /* Make sure the pipe isn't still relying on us */
  1438. assert_pipe_disabled(dev_priv, pipe);
  1439. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1440. POSTING_READ(DPLL(pipe));
  1441. }
  1442. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1443. {
  1444. u32 val;
  1445. /* Make sure the pipe isn't still relying on us */
  1446. assert_pipe_disabled(dev_priv, pipe);
  1447. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1448. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1449. if (pipe != PIPE_A)
  1450. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1451. I915_WRITE(DPLL(pipe), val);
  1452. POSTING_READ(DPLL(pipe));
  1453. }
  1454. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1455. {
  1456. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1457. u32 val;
  1458. /* Make sure the pipe isn't still relying on us */
  1459. assert_pipe_disabled(dev_priv, pipe);
  1460. val = DPLL_SSC_REF_CLK_CHV |
  1461. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1462. if (pipe != PIPE_A)
  1463. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1464. I915_WRITE(DPLL(pipe), val);
  1465. POSTING_READ(DPLL(pipe));
  1466. mutex_lock(&dev_priv->sb_lock);
  1467. /* Disable 10bit clock to display controller */
  1468. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1469. val &= ~DPIO_DCLKP_EN;
  1470. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1471. mutex_unlock(&dev_priv->sb_lock);
  1472. }
  1473. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1474. struct intel_digital_port *dport,
  1475. unsigned int expected_mask)
  1476. {
  1477. u32 port_mask;
  1478. i915_reg_t dpll_reg;
  1479. switch (dport->port) {
  1480. case PORT_B:
  1481. port_mask = DPLL_PORTB_READY_MASK;
  1482. dpll_reg = DPLL(0);
  1483. break;
  1484. case PORT_C:
  1485. port_mask = DPLL_PORTC_READY_MASK;
  1486. dpll_reg = DPLL(0);
  1487. expected_mask <<= 4;
  1488. break;
  1489. case PORT_D:
  1490. port_mask = DPLL_PORTD_READY_MASK;
  1491. dpll_reg = DPIO_PHY_STATUS;
  1492. break;
  1493. default:
  1494. BUG();
  1495. }
  1496. if (intel_wait_for_register(dev_priv,
  1497. dpll_reg, port_mask, expected_mask,
  1498. 1000))
  1499. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1500. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1501. }
  1502. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1503. enum pipe pipe)
  1504. {
  1505. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1506. pipe);
  1507. i915_reg_t reg;
  1508. uint32_t val, pipeconf_val;
  1509. /* Make sure PCH DPLL is enabled */
  1510. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1511. /* FDI must be feeding us bits for PCH ports */
  1512. assert_fdi_tx_enabled(dev_priv, pipe);
  1513. assert_fdi_rx_enabled(dev_priv, pipe);
  1514. if (HAS_PCH_CPT(dev_priv)) {
  1515. /* Workaround: Set the timing override bit before enabling the
  1516. * pch transcoder. */
  1517. reg = TRANS_CHICKEN2(pipe);
  1518. val = I915_READ(reg);
  1519. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1520. I915_WRITE(reg, val);
  1521. }
  1522. reg = PCH_TRANSCONF(pipe);
  1523. val = I915_READ(reg);
  1524. pipeconf_val = I915_READ(PIPECONF(pipe));
  1525. if (HAS_PCH_IBX(dev_priv)) {
  1526. /*
  1527. * Make the BPC in transcoder be consistent with
  1528. * that in pipeconf reg. For HDMI we must use 8bpc
  1529. * here for both 8bpc and 12bpc.
  1530. */
  1531. val &= ~PIPECONF_BPC_MASK;
  1532. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1533. val |= PIPECONF_8BPC;
  1534. else
  1535. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1536. }
  1537. val &= ~TRANS_INTERLACE_MASK;
  1538. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1539. if (HAS_PCH_IBX(dev_priv) &&
  1540. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1541. val |= TRANS_LEGACY_INTERLACED_ILK;
  1542. else
  1543. val |= TRANS_INTERLACED;
  1544. else
  1545. val |= TRANS_PROGRESSIVE;
  1546. I915_WRITE(reg, val | TRANS_ENABLE);
  1547. if (intel_wait_for_register(dev_priv,
  1548. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1549. 100))
  1550. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1551. }
  1552. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1553. enum transcoder cpu_transcoder)
  1554. {
  1555. u32 val, pipeconf_val;
  1556. /* FDI must be feeding us bits for PCH ports */
  1557. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1558. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1559. /* Workaround: set timing override bit. */
  1560. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1561. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1562. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1563. val = TRANS_ENABLE;
  1564. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1565. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1566. PIPECONF_INTERLACED_ILK)
  1567. val |= TRANS_INTERLACED;
  1568. else
  1569. val |= TRANS_PROGRESSIVE;
  1570. I915_WRITE(LPT_TRANSCONF, val);
  1571. if (intel_wait_for_register(dev_priv,
  1572. LPT_TRANSCONF,
  1573. TRANS_STATE_ENABLE,
  1574. TRANS_STATE_ENABLE,
  1575. 100))
  1576. DRM_ERROR("Failed to enable PCH transcoder\n");
  1577. }
  1578. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1579. enum pipe pipe)
  1580. {
  1581. i915_reg_t reg;
  1582. uint32_t val;
  1583. /* FDI relies on the transcoder */
  1584. assert_fdi_tx_disabled(dev_priv, pipe);
  1585. assert_fdi_rx_disabled(dev_priv, pipe);
  1586. /* Ports must be off as well */
  1587. assert_pch_ports_disabled(dev_priv, pipe);
  1588. reg = PCH_TRANSCONF(pipe);
  1589. val = I915_READ(reg);
  1590. val &= ~TRANS_ENABLE;
  1591. I915_WRITE(reg, val);
  1592. /* wait for PCH transcoder off, transcoder state */
  1593. if (intel_wait_for_register(dev_priv,
  1594. reg, TRANS_STATE_ENABLE, 0,
  1595. 50))
  1596. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1597. if (HAS_PCH_CPT(dev_priv)) {
  1598. /* Workaround: Clear the timing override chicken bit again. */
  1599. reg = TRANS_CHICKEN2(pipe);
  1600. val = I915_READ(reg);
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(reg, val);
  1603. }
  1604. }
  1605. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1606. {
  1607. u32 val;
  1608. val = I915_READ(LPT_TRANSCONF);
  1609. val &= ~TRANS_ENABLE;
  1610. I915_WRITE(LPT_TRANSCONF, val);
  1611. /* wait for PCH transcoder off, transcoder state */
  1612. if (intel_wait_for_register(dev_priv,
  1613. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1614. 50))
  1615. DRM_ERROR("Failed to disable PCH transcoder\n");
  1616. /* Workaround: clear timing override bit. */
  1617. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1618. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1619. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1620. }
  1621. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1622. {
  1623. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1624. WARN_ON(!crtc->config->has_pch_encoder);
  1625. if (HAS_PCH_LPT(dev_priv))
  1626. return PIPE_A;
  1627. else
  1628. return crtc->pipe;
  1629. }
  1630. /**
  1631. * intel_enable_pipe - enable a pipe, asserting requirements
  1632. * @crtc: crtc responsible for the pipe
  1633. *
  1634. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1635. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1636. */
  1637. static void intel_enable_pipe(struct intel_crtc *crtc)
  1638. {
  1639. struct drm_device *dev = crtc->base.dev;
  1640. struct drm_i915_private *dev_priv = to_i915(dev);
  1641. enum pipe pipe = crtc->pipe;
  1642. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1643. i915_reg_t reg;
  1644. u32 val;
  1645. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1646. assert_planes_disabled(dev_priv, pipe);
  1647. assert_cursor_disabled(dev_priv, pipe);
  1648. assert_sprites_disabled(dev_priv, pipe);
  1649. /*
  1650. * A pipe without a PLL won't actually be able to drive bits from
  1651. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1652. * need the check.
  1653. */
  1654. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1655. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1656. assert_dsi_pll_enabled(dev_priv);
  1657. else
  1658. assert_pll_enabled(dev_priv, pipe);
  1659. } else {
  1660. if (crtc->config->has_pch_encoder) {
  1661. /* if driving the PCH, we need FDI enabled */
  1662. assert_fdi_rx_pll_enabled(dev_priv,
  1663. intel_crtc_pch_transcoder(crtc));
  1664. assert_fdi_tx_pll_enabled(dev_priv,
  1665. (enum pipe) cpu_transcoder);
  1666. }
  1667. /* FIXME: assert CPU port conditions for SNB+ */
  1668. }
  1669. reg = PIPECONF(cpu_transcoder);
  1670. val = I915_READ(reg);
  1671. if (val & PIPECONF_ENABLE) {
  1672. /* we keep both pipes enabled on 830 */
  1673. WARN_ON(!IS_I830(dev_priv));
  1674. return;
  1675. }
  1676. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1677. POSTING_READ(reg);
  1678. /*
  1679. * Until the pipe starts DSL will read as 0, which would cause
  1680. * an apparent vblank timestamp jump, which messes up also the
  1681. * frame count when it's derived from the timestamps. So let's
  1682. * wait for the pipe to start properly before we call
  1683. * drm_crtc_vblank_on()
  1684. */
  1685. if (dev->max_vblank_count == 0 &&
  1686. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1687. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1688. }
  1689. /**
  1690. * intel_disable_pipe - disable a pipe, asserting requirements
  1691. * @crtc: crtc whose pipes is to be disabled
  1692. *
  1693. * Disable the pipe of @crtc, making sure that various hardware
  1694. * specific requirements are met, if applicable, e.g. plane
  1695. * disabled, panel fitter off, etc.
  1696. *
  1697. * Will wait until the pipe has shut down before returning.
  1698. */
  1699. static void intel_disable_pipe(struct intel_crtc *crtc)
  1700. {
  1701. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1702. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1703. enum pipe pipe = crtc->pipe;
  1704. i915_reg_t reg;
  1705. u32 val;
  1706. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1707. /*
  1708. * Make sure planes won't keep trying to pump pixels to us,
  1709. * or we might hang the display.
  1710. */
  1711. assert_planes_disabled(dev_priv, pipe);
  1712. assert_cursor_disabled(dev_priv, pipe);
  1713. assert_sprites_disabled(dev_priv, pipe);
  1714. reg = PIPECONF(cpu_transcoder);
  1715. val = I915_READ(reg);
  1716. if ((val & PIPECONF_ENABLE) == 0)
  1717. return;
  1718. /*
  1719. * Double wide has implications for planes
  1720. * so best keep it disabled when not needed.
  1721. */
  1722. if (crtc->config->double_wide)
  1723. val &= ~PIPECONF_DOUBLE_WIDE;
  1724. /* Don't disable pipe or pipe PLLs if needed */
  1725. if (!IS_I830(dev_priv))
  1726. val &= ~PIPECONF_ENABLE;
  1727. I915_WRITE(reg, val);
  1728. if ((val & PIPECONF_ENABLE) == 0)
  1729. intel_wait_for_pipe_off(crtc);
  1730. }
  1731. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1732. {
  1733. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1734. }
  1735. static unsigned int
  1736. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1739. unsigned int cpp = fb->format->cpp[plane];
  1740. switch (fb->modifier) {
  1741. case DRM_FORMAT_MOD_LINEAR:
  1742. return cpp;
  1743. case I915_FORMAT_MOD_X_TILED:
  1744. if (IS_GEN2(dev_priv))
  1745. return 128;
  1746. else
  1747. return 512;
  1748. case I915_FORMAT_MOD_Y_TILED_CCS:
  1749. if (plane == 1)
  1750. return 128;
  1751. /* fall through */
  1752. case I915_FORMAT_MOD_Y_TILED:
  1753. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1754. return 128;
  1755. else
  1756. return 512;
  1757. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1758. if (plane == 1)
  1759. return 128;
  1760. /* fall through */
  1761. case I915_FORMAT_MOD_Yf_TILED:
  1762. switch (cpp) {
  1763. case 1:
  1764. return 64;
  1765. case 2:
  1766. case 4:
  1767. return 128;
  1768. case 8:
  1769. case 16:
  1770. return 256;
  1771. default:
  1772. MISSING_CASE(cpp);
  1773. return cpp;
  1774. }
  1775. break;
  1776. default:
  1777. MISSING_CASE(fb->modifier);
  1778. return cpp;
  1779. }
  1780. }
  1781. static unsigned int
  1782. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1783. {
  1784. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1785. return 1;
  1786. else
  1787. return intel_tile_size(to_i915(fb->dev)) /
  1788. intel_tile_width_bytes(fb, plane);
  1789. }
  1790. /* Return the tile dimensions in pixel units */
  1791. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1792. unsigned int *tile_width,
  1793. unsigned int *tile_height)
  1794. {
  1795. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1796. unsigned int cpp = fb->format->cpp[plane];
  1797. *tile_width = tile_width_bytes / cpp;
  1798. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1799. }
  1800. unsigned int
  1801. intel_fb_align_height(const struct drm_framebuffer *fb,
  1802. int plane, unsigned int height)
  1803. {
  1804. unsigned int tile_height = intel_tile_height(fb, plane);
  1805. return ALIGN(height, tile_height);
  1806. }
  1807. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1808. {
  1809. unsigned int size = 0;
  1810. int i;
  1811. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1812. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1813. return size;
  1814. }
  1815. static void
  1816. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1817. const struct drm_framebuffer *fb,
  1818. unsigned int rotation)
  1819. {
  1820. view->type = I915_GGTT_VIEW_NORMAL;
  1821. if (drm_rotation_90_or_270(rotation)) {
  1822. view->type = I915_GGTT_VIEW_ROTATED;
  1823. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1824. }
  1825. }
  1826. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1827. {
  1828. if (IS_I830(dev_priv))
  1829. return 16 * 1024;
  1830. else if (IS_I85X(dev_priv))
  1831. return 256;
  1832. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1833. return 32;
  1834. else
  1835. return 4 * 1024;
  1836. }
  1837. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1838. {
  1839. if (INTEL_INFO(dev_priv)->gen >= 9)
  1840. return 256 * 1024;
  1841. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1842. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1843. return 128 * 1024;
  1844. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1845. return 4 * 1024;
  1846. else
  1847. return 0;
  1848. }
  1849. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1850. int plane)
  1851. {
  1852. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1853. /* AUX_DIST needs only 4K alignment */
  1854. if (plane == 1)
  1855. return 4096;
  1856. switch (fb->modifier) {
  1857. case DRM_FORMAT_MOD_LINEAR:
  1858. return intel_linear_alignment(dev_priv);
  1859. case I915_FORMAT_MOD_X_TILED:
  1860. if (INTEL_GEN(dev_priv) >= 9)
  1861. return 256 * 1024;
  1862. return 0;
  1863. case I915_FORMAT_MOD_Y_TILED_CCS:
  1864. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1865. case I915_FORMAT_MOD_Y_TILED:
  1866. case I915_FORMAT_MOD_Yf_TILED:
  1867. return 1 * 1024 * 1024;
  1868. default:
  1869. MISSING_CASE(fb->modifier);
  1870. return 0;
  1871. }
  1872. }
  1873. struct i915_vma *
  1874. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1875. {
  1876. struct drm_device *dev = fb->dev;
  1877. struct drm_i915_private *dev_priv = to_i915(dev);
  1878. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1879. struct i915_ggtt_view view;
  1880. struct i915_vma *vma;
  1881. u32 alignment;
  1882. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1883. alignment = intel_surf_alignment(fb, 0);
  1884. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1885. /* Note that the w/a also requires 64 PTE of padding following the
  1886. * bo. We currently fill all unused PTE with the shadow page and so
  1887. * we should always have valid PTE following the scanout preventing
  1888. * the VT-d warning.
  1889. */
  1890. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1891. alignment = 256 * 1024;
  1892. /*
  1893. * Global gtt pte registers are special registers which actually forward
  1894. * writes to a chunk of system memory. Which means that there is no risk
  1895. * that the register values disappear as soon as we call
  1896. * intel_runtime_pm_put(), so it is correct to wrap only the
  1897. * pin/unpin/fence and not more.
  1898. */
  1899. intel_runtime_pm_get(dev_priv);
  1900. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1901. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1902. if (IS_ERR(vma))
  1903. goto err;
  1904. if (i915_vma_is_map_and_fenceable(vma)) {
  1905. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1906. * fence, whereas 965+ only requires a fence if using
  1907. * framebuffer compression. For simplicity, we always, when
  1908. * possible, install a fence as the cost is not that onerous.
  1909. *
  1910. * If we fail to fence the tiled scanout, then either the
  1911. * modeset will reject the change (which is highly unlikely as
  1912. * the affected systems, all but one, do not have unmappable
  1913. * space) or we will not be able to enable full powersaving
  1914. * techniques (also likely not to apply due to various limits
  1915. * FBC and the like impose on the size of the buffer, which
  1916. * presumably we violated anyway with this unmappable buffer).
  1917. * Anyway, it is presumably better to stumble onwards with
  1918. * something and try to run the system in a "less than optimal"
  1919. * mode that matches the user configuration.
  1920. */
  1921. if (i915_vma_get_fence(vma) == 0)
  1922. i915_vma_pin_fence(vma);
  1923. }
  1924. i915_vma_get(vma);
  1925. err:
  1926. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1927. intel_runtime_pm_put(dev_priv);
  1928. return vma;
  1929. }
  1930. void intel_unpin_fb_vma(struct i915_vma *vma)
  1931. {
  1932. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1933. i915_vma_unpin_fence(vma);
  1934. i915_gem_object_unpin_from_display_plane(vma);
  1935. i915_vma_put(vma);
  1936. }
  1937. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1938. unsigned int rotation)
  1939. {
  1940. if (drm_rotation_90_or_270(rotation))
  1941. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1942. else
  1943. return fb->pitches[plane];
  1944. }
  1945. /*
  1946. * Convert the x/y offsets into a linear offset.
  1947. * Only valid with 0/180 degree rotation, which is fine since linear
  1948. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1949. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1950. */
  1951. u32 intel_fb_xy_to_linear(int x, int y,
  1952. const struct intel_plane_state *state,
  1953. int plane)
  1954. {
  1955. const struct drm_framebuffer *fb = state->base.fb;
  1956. unsigned int cpp = fb->format->cpp[plane];
  1957. unsigned int pitch = fb->pitches[plane];
  1958. return y * pitch + x * cpp;
  1959. }
  1960. /*
  1961. * Add the x/y offsets derived from fb->offsets[] to the user
  1962. * specified plane src x/y offsets. The resulting x/y offsets
  1963. * specify the start of scanout from the beginning of the gtt mapping.
  1964. */
  1965. void intel_add_fb_offsets(int *x, int *y,
  1966. const struct intel_plane_state *state,
  1967. int plane)
  1968. {
  1969. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1970. unsigned int rotation = state->base.rotation;
  1971. if (drm_rotation_90_or_270(rotation)) {
  1972. *x += intel_fb->rotated[plane].x;
  1973. *y += intel_fb->rotated[plane].y;
  1974. } else {
  1975. *x += intel_fb->normal[plane].x;
  1976. *y += intel_fb->normal[plane].y;
  1977. }
  1978. }
  1979. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1980. unsigned int tile_width,
  1981. unsigned int tile_height,
  1982. unsigned int tile_size,
  1983. unsigned int pitch_tiles,
  1984. u32 old_offset,
  1985. u32 new_offset)
  1986. {
  1987. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1988. unsigned int tiles;
  1989. WARN_ON(old_offset & (tile_size - 1));
  1990. WARN_ON(new_offset & (tile_size - 1));
  1991. WARN_ON(new_offset > old_offset);
  1992. tiles = (old_offset - new_offset) / tile_size;
  1993. *y += tiles / pitch_tiles * tile_height;
  1994. *x += tiles % pitch_tiles * tile_width;
  1995. /* minimize x in case it got needlessly big */
  1996. *y += *x / pitch_pixels * tile_height;
  1997. *x %= pitch_pixels;
  1998. return new_offset;
  1999. }
  2000. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2001. const struct drm_framebuffer *fb, int plane,
  2002. unsigned int rotation,
  2003. u32 old_offset, u32 new_offset)
  2004. {
  2005. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2006. unsigned int cpp = fb->format->cpp[plane];
  2007. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2008. WARN_ON(new_offset > old_offset);
  2009. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2010. unsigned int tile_size, tile_width, tile_height;
  2011. unsigned int pitch_tiles;
  2012. tile_size = intel_tile_size(dev_priv);
  2013. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2014. if (drm_rotation_90_or_270(rotation)) {
  2015. pitch_tiles = pitch / tile_height;
  2016. swap(tile_width, tile_height);
  2017. } else {
  2018. pitch_tiles = pitch / (tile_width * cpp);
  2019. }
  2020. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2021. tile_size, pitch_tiles,
  2022. old_offset, new_offset);
  2023. } else {
  2024. old_offset += *y * pitch + *x * cpp;
  2025. *y = (old_offset - new_offset) / pitch;
  2026. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2027. }
  2028. return new_offset;
  2029. }
  2030. /*
  2031. * Adjust the tile offset by moving the difference into
  2032. * the x/y offsets.
  2033. */
  2034. static u32 intel_adjust_tile_offset(int *x, int *y,
  2035. const struct intel_plane_state *state, int plane,
  2036. u32 old_offset, u32 new_offset)
  2037. {
  2038. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  2039. state->base.rotation,
  2040. old_offset, new_offset);
  2041. }
  2042. /*
  2043. * Computes the linear offset to the base tile and adjusts
  2044. * x, y. bytes per pixel is assumed to be a power-of-two.
  2045. *
  2046. * In the 90/270 rotated case, x and y are assumed
  2047. * to be already rotated to match the rotated GTT view, and
  2048. * pitch is the tile_height aligned framebuffer height.
  2049. *
  2050. * This function is used when computing the derived information
  2051. * under intel_framebuffer, so using any of that information
  2052. * here is not allowed. Anything under drm_framebuffer can be
  2053. * used. This is why the user has to pass in the pitch since it
  2054. * is specified in the rotated orientation.
  2055. */
  2056. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2057. int *x, int *y,
  2058. const struct drm_framebuffer *fb, int plane,
  2059. unsigned int pitch,
  2060. unsigned int rotation,
  2061. u32 alignment)
  2062. {
  2063. uint64_t fb_modifier = fb->modifier;
  2064. unsigned int cpp = fb->format->cpp[plane];
  2065. u32 offset, offset_aligned;
  2066. if (alignment)
  2067. alignment--;
  2068. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2069. unsigned int tile_size, tile_width, tile_height;
  2070. unsigned int tile_rows, tiles, pitch_tiles;
  2071. tile_size = intel_tile_size(dev_priv);
  2072. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2073. if (drm_rotation_90_or_270(rotation)) {
  2074. pitch_tiles = pitch / tile_height;
  2075. swap(tile_width, tile_height);
  2076. } else {
  2077. pitch_tiles = pitch / (tile_width * cpp);
  2078. }
  2079. tile_rows = *y / tile_height;
  2080. *y %= tile_height;
  2081. tiles = *x / tile_width;
  2082. *x %= tile_width;
  2083. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2084. offset_aligned = offset & ~alignment;
  2085. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2086. tile_size, pitch_tiles,
  2087. offset, offset_aligned);
  2088. } else {
  2089. offset = *y * pitch + *x * cpp;
  2090. offset_aligned = offset & ~alignment;
  2091. *y = (offset & alignment) / pitch;
  2092. *x = ((offset & alignment) - *y * pitch) / cpp;
  2093. }
  2094. return offset_aligned;
  2095. }
  2096. u32 intel_compute_tile_offset(int *x, int *y,
  2097. const struct intel_plane_state *state,
  2098. int plane)
  2099. {
  2100. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2101. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2102. const struct drm_framebuffer *fb = state->base.fb;
  2103. unsigned int rotation = state->base.rotation;
  2104. int pitch = intel_fb_pitch(fb, plane, rotation);
  2105. u32 alignment;
  2106. if (intel_plane->id == PLANE_CURSOR)
  2107. alignment = intel_cursor_alignment(dev_priv);
  2108. else
  2109. alignment = intel_surf_alignment(fb, plane);
  2110. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2111. rotation, alignment);
  2112. }
  2113. /* Convert the fb->offset[] into x/y offsets */
  2114. static int intel_fb_offset_to_xy(int *x, int *y,
  2115. const struct drm_framebuffer *fb, int plane)
  2116. {
  2117. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2118. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2119. fb->offsets[plane] % intel_tile_size(dev_priv))
  2120. return -EINVAL;
  2121. *x = 0;
  2122. *y = 0;
  2123. _intel_adjust_tile_offset(x, y,
  2124. fb, plane, DRM_MODE_ROTATE_0,
  2125. fb->offsets[plane], 0);
  2126. return 0;
  2127. }
  2128. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2129. {
  2130. switch (fb_modifier) {
  2131. case I915_FORMAT_MOD_X_TILED:
  2132. return I915_TILING_X;
  2133. case I915_FORMAT_MOD_Y_TILED:
  2134. case I915_FORMAT_MOD_Y_TILED_CCS:
  2135. return I915_TILING_Y;
  2136. default:
  2137. return I915_TILING_NONE;
  2138. }
  2139. }
  2140. static const struct drm_format_info ccs_formats[] = {
  2141. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2142. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2143. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2144. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2145. };
  2146. static const struct drm_format_info *
  2147. lookup_format_info(const struct drm_format_info formats[],
  2148. int num_formats, u32 format)
  2149. {
  2150. int i;
  2151. for (i = 0; i < num_formats; i++) {
  2152. if (formats[i].format == format)
  2153. return &formats[i];
  2154. }
  2155. return NULL;
  2156. }
  2157. static const struct drm_format_info *
  2158. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2159. {
  2160. switch (cmd->modifier[0]) {
  2161. case I915_FORMAT_MOD_Y_TILED_CCS:
  2162. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2163. return lookup_format_info(ccs_formats,
  2164. ARRAY_SIZE(ccs_formats),
  2165. cmd->pixel_format);
  2166. default:
  2167. return NULL;
  2168. }
  2169. }
  2170. static int
  2171. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2172. struct drm_framebuffer *fb)
  2173. {
  2174. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2175. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2176. u32 gtt_offset_rotated = 0;
  2177. unsigned int max_size = 0;
  2178. int i, num_planes = fb->format->num_planes;
  2179. unsigned int tile_size = intel_tile_size(dev_priv);
  2180. for (i = 0; i < num_planes; i++) {
  2181. unsigned int width, height;
  2182. unsigned int cpp, size;
  2183. u32 offset;
  2184. int x, y;
  2185. int ret;
  2186. cpp = fb->format->cpp[i];
  2187. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2188. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2189. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2190. if (ret) {
  2191. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2192. i, fb->offsets[i]);
  2193. return ret;
  2194. }
  2195. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2196. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2197. int hsub = fb->format->hsub;
  2198. int vsub = fb->format->vsub;
  2199. int tile_width, tile_height;
  2200. int main_x, main_y;
  2201. int ccs_x, ccs_y;
  2202. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2203. tile_width *= hsub;
  2204. tile_height *= vsub;
  2205. ccs_x = (x * hsub) % tile_width;
  2206. ccs_y = (y * vsub) % tile_height;
  2207. main_x = intel_fb->normal[0].x % tile_width;
  2208. main_y = intel_fb->normal[0].y % tile_height;
  2209. /*
  2210. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2211. * x/y offsets must match between CCS and the main surface.
  2212. */
  2213. if (main_x != ccs_x || main_y != ccs_y) {
  2214. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2215. main_x, main_y,
  2216. ccs_x, ccs_y,
  2217. intel_fb->normal[0].x,
  2218. intel_fb->normal[0].y,
  2219. x, y);
  2220. return -EINVAL;
  2221. }
  2222. }
  2223. /*
  2224. * The fence (if used) is aligned to the start of the object
  2225. * so having the framebuffer wrap around across the edge of the
  2226. * fenced region doesn't really work. We have no API to configure
  2227. * the fence start offset within the object (nor could we probably
  2228. * on gen2/3). So it's just easier if we just require that the
  2229. * fb layout agrees with the fence layout. We already check that the
  2230. * fb stride matches the fence stride elsewhere.
  2231. */
  2232. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2233. (x + width) * cpp > fb->pitches[i]) {
  2234. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2235. i, fb->offsets[i]);
  2236. return -EINVAL;
  2237. }
  2238. /*
  2239. * First pixel of the framebuffer from
  2240. * the start of the normal gtt mapping.
  2241. */
  2242. intel_fb->normal[i].x = x;
  2243. intel_fb->normal[i].y = y;
  2244. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2245. fb, i, fb->pitches[i],
  2246. DRM_MODE_ROTATE_0, tile_size);
  2247. offset /= tile_size;
  2248. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2249. unsigned int tile_width, tile_height;
  2250. unsigned int pitch_tiles;
  2251. struct drm_rect r;
  2252. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2253. rot_info->plane[i].offset = offset;
  2254. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2255. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2256. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2257. intel_fb->rotated[i].pitch =
  2258. rot_info->plane[i].height * tile_height;
  2259. /* how many tiles does this plane need */
  2260. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2261. /*
  2262. * If the plane isn't horizontally tile aligned,
  2263. * we need one more tile.
  2264. */
  2265. if (x != 0)
  2266. size++;
  2267. /* rotate the x/y offsets to match the GTT view */
  2268. r.x1 = x;
  2269. r.y1 = y;
  2270. r.x2 = x + width;
  2271. r.y2 = y + height;
  2272. drm_rect_rotate(&r,
  2273. rot_info->plane[i].width * tile_width,
  2274. rot_info->plane[i].height * tile_height,
  2275. DRM_MODE_ROTATE_270);
  2276. x = r.x1;
  2277. y = r.y1;
  2278. /* rotate the tile dimensions to match the GTT view */
  2279. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2280. swap(tile_width, tile_height);
  2281. /*
  2282. * We only keep the x/y offsets, so push all of the
  2283. * gtt offset into the x/y offsets.
  2284. */
  2285. __intel_adjust_tile_offset(&x, &y,
  2286. tile_width, tile_height,
  2287. tile_size, pitch_tiles,
  2288. gtt_offset_rotated * tile_size, 0);
  2289. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2290. /*
  2291. * First pixel of the framebuffer from
  2292. * the start of the rotated gtt mapping.
  2293. */
  2294. intel_fb->rotated[i].x = x;
  2295. intel_fb->rotated[i].y = y;
  2296. } else {
  2297. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2298. x * cpp, tile_size);
  2299. }
  2300. /* how many tiles in total needed in the bo */
  2301. max_size = max(max_size, offset + size);
  2302. }
  2303. if (max_size * tile_size > intel_fb->obj->base.size) {
  2304. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2305. max_size * tile_size, intel_fb->obj->base.size);
  2306. return -EINVAL;
  2307. }
  2308. return 0;
  2309. }
  2310. static int i9xx_format_to_fourcc(int format)
  2311. {
  2312. switch (format) {
  2313. case DISPPLANE_8BPP:
  2314. return DRM_FORMAT_C8;
  2315. case DISPPLANE_BGRX555:
  2316. return DRM_FORMAT_XRGB1555;
  2317. case DISPPLANE_BGRX565:
  2318. return DRM_FORMAT_RGB565;
  2319. default:
  2320. case DISPPLANE_BGRX888:
  2321. return DRM_FORMAT_XRGB8888;
  2322. case DISPPLANE_RGBX888:
  2323. return DRM_FORMAT_XBGR8888;
  2324. case DISPPLANE_BGRX101010:
  2325. return DRM_FORMAT_XRGB2101010;
  2326. case DISPPLANE_RGBX101010:
  2327. return DRM_FORMAT_XBGR2101010;
  2328. }
  2329. }
  2330. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2331. {
  2332. switch (format) {
  2333. case PLANE_CTL_FORMAT_RGB_565:
  2334. return DRM_FORMAT_RGB565;
  2335. default:
  2336. case PLANE_CTL_FORMAT_XRGB_8888:
  2337. if (rgb_order) {
  2338. if (alpha)
  2339. return DRM_FORMAT_ABGR8888;
  2340. else
  2341. return DRM_FORMAT_XBGR8888;
  2342. } else {
  2343. if (alpha)
  2344. return DRM_FORMAT_ARGB8888;
  2345. else
  2346. return DRM_FORMAT_XRGB8888;
  2347. }
  2348. case PLANE_CTL_FORMAT_XRGB_2101010:
  2349. if (rgb_order)
  2350. return DRM_FORMAT_XBGR2101010;
  2351. else
  2352. return DRM_FORMAT_XRGB2101010;
  2353. }
  2354. }
  2355. static bool
  2356. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2357. struct intel_initial_plane_config *plane_config)
  2358. {
  2359. struct drm_device *dev = crtc->base.dev;
  2360. struct drm_i915_private *dev_priv = to_i915(dev);
  2361. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2362. struct drm_i915_gem_object *obj = NULL;
  2363. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2364. struct drm_framebuffer *fb = &plane_config->fb->base;
  2365. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2366. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2367. PAGE_SIZE);
  2368. size_aligned -= base_aligned;
  2369. if (plane_config->size == 0)
  2370. return false;
  2371. /* If the FB is too big, just don't use it since fbdev is not very
  2372. * important and we should probably use that space with FBC or other
  2373. * features. */
  2374. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2375. return false;
  2376. mutex_lock(&dev->struct_mutex);
  2377. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2378. base_aligned,
  2379. base_aligned,
  2380. size_aligned);
  2381. mutex_unlock(&dev->struct_mutex);
  2382. if (!obj)
  2383. return false;
  2384. if (plane_config->tiling == I915_TILING_X)
  2385. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2386. mode_cmd.pixel_format = fb->format->format;
  2387. mode_cmd.width = fb->width;
  2388. mode_cmd.height = fb->height;
  2389. mode_cmd.pitches[0] = fb->pitches[0];
  2390. mode_cmd.modifier[0] = fb->modifier;
  2391. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2392. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2393. DRM_DEBUG_KMS("intel fb init failed\n");
  2394. goto out_unref_obj;
  2395. }
  2396. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2397. return true;
  2398. out_unref_obj:
  2399. i915_gem_object_put(obj);
  2400. return false;
  2401. }
  2402. static void
  2403. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2404. struct intel_plane_state *plane_state,
  2405. bool visible)
  2406. {
  2407. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2408. plane_state->base.visible = visible;
  2409. /* FIXME pre-g4x don't work like this */
  2410. if (visible) {
  2411. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2412. crtc_state->active_planes |= BIT(plane->id);
  2413. } else {
  2414. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2415. crtc_state->active_planes &= ~BIT(plane->id);
  2416. }
  2417. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2418. crtc_state->base.crtc->name,
  2419. crtc_state->active_planes);
  2420. }
  2421. static void
  2422. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2423. struct intel_initial_plane_config *plane_config)
  2424. {
  2425. struct drm_device *dev = intel_crtc->base.dev;
  2426. struct drm_i915_private *dev_priv = to_i915(dev);
  2427. struct drm_crtc *c;
  2428. struct drm_i915_gem_object *obj;
  2429. struct drm_plane *primary = intel_crtc->base.primary;
  2430. struct drm_plane_state *plane_state = primary->state;
  2431. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2432. struct intel_plane *intel_plane = to_intel_plane(primary);
  2433. struct intel_plane_state *intel_state =
  2434. to_intel_plane_state(plane_state);
  2435. struct drm_framebuffer *fb;
  2436. if (!plane_config->fb)
  2437. return;
  2438. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2439. fb = &plane_config->fb->base;
  2440. goto valid_fb;
  2441. }
  2442. kfree(plane_config->fb);
  2443. /*
  2444. * Failed to alloc the obj, check to see if we should share
  2445. * an fb with another CRTC instead
  2446. */
  2447. for_each_crtc(dev, c) {
  2448. struct intel_plane_state *state;
  2449. if (c == &intel_crtc->base)
  2450. continue;
  2451. if (!to_intel_crtc(c)->active)
  2452. continue;
  2453. state = to_intel_plane_state(c->primary->state);
  2454. if (!state->vma)
  2455. continue;
  2456. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2457. fb = c->primary->fb;
  2458. drm_framebuffer_reference(fb);
  2459. goto valid_fb;
  2460. }
  2461. }
  2462. /*
  2463. * We've failed to reconstruct the BIOS FB. Current display state
  2464. * indicates that the primary plane is visible, but has a NULL FB,
  2465. * which will lead to problems later if we don't fix it up. The
  2466. * simplest solution is to just disable the primary plane now and
  2467. * pretend the BIOS never had it enabled.
  2468. */
  2469. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2470. to_intel_plane_state(plane_state),
  2471. false);
  2472. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2473. trace_intel_disable_plane(primary, intel_crtc);
  2474. intel_plane->disable_plane(intel_plane, intel_crtc);
  2475. return;
  2476. valid_fb:
  2477. mutex_lock(&dev->struct_mutex);
  2478. intel_state->vma =
  2479. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2480. mutex_unlock(&dev->struct_mutex);
  2481. if (IS_ERR(intel_state->vma)) {
  2482. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2483. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2484. intel_state->vma = NULL;
  2485. drm_framebuffer_unreference(fb);
  2486. return;
  2487. }
  2488. plane_state->src_x = 0;
  2489. plane_state->src_y = 0;
  2490. plane_state->src_w = fb->width << 16;
  2491. plane_state->src_h = fb->height << 16;
  2492. plane_state->crtc_x = 0;
  2493. plane_state->crtc_y = 0;
  2494. plane_state->crtc_w = fb->width;
  2495. plane_state->crtc_h = fb->height;
  2496. intel_state->base.src = drm_plane_state_src(plane_state);
  2497. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2498. obj = intel_fb_obj(fb);
  2499. if (i915_gem_object_is_tiled(obj))
  2500. dev_priv->preserve_bios_swizzle = true;
  2501. drm_framebuffer_reference(fb);
  2502. primary->fb = primary->state->fb = fb;
  2503. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2504. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2505. to_intel_plane_state(plane_state),
  2506. true);
  2507. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2508. &obj->frontbuffer_bits);
  2509. }
  2510. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2511. unsigned int rotation)
  2512. {
  2513. int cpp = fb->format->cpp[plane];
  2514. switch (fb->modifier) {
  2515. case DRM_FORMAT_MOD_LINEAR:
  2516. case I915_FORMAT_MOD_X_TILED:
  2517. switch (cpp) {
  2518. case 8:
  2519. return 4096;
  2520. case 4:
  2521. case 2:
  2522. case 1:
  2523. return 8192;
  2524. default:
  2525. MISSING_CASE(cpp);
  2526. break;
  2527. }
  2528. break;
  2529. case I915_FORMAT_MOD_Y_TILED_CCS:
  2530. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2531. /* FIXME AUX plane? */
  2532. case I915_FORMAT_MOD_Y_TILED:
  2533. case I915_FORMAT_MOD_Yf_TILED:
  2534. switch (cpp) {
  2535. case 8:
  2536. return 2048;
  2537. case 4:
  2538. return 4096;
  2539. case 2:
  2540. case 1:
  2541. return 8192;
  2542. default:
  2543. MISSING_CASE(cpp);
  2544. break;
  2545. }
  2546. break;
  2547. default:
  2548. MISSING_CASE(fb->modifier);
  2549. }
  2550. return 2048;
  2551. }
  2552. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2553. int main_x, int main_y, u32 main_offset)
  2554. {
  2555. const struct drm_framebuffer *fb = plane_state->base.fb;
  2556. int hsub = fb->format->hsub;
  2557. int vsub = fb->format->vsub;
  2558. int aux_x = plane_state->aux.x;
  2559. int aux_y = plane_state->aux.y;
  2560. u32 aux_offset = plane_state->aux.offset;
  2561. u32 alignment = intel_surf_alignment(fb, 1);
  2562. while (aux_offset >= main_offset && aux_y <= main_y) {
  2563. int x, y;
  2564. if (aux_x == main_x && aux_y == main_y)
  2565. break;
  2566. if (aux_offset == 0)
  2567. break;
  2568. x = aux_x / hsub;
  2569. y = aux_y / vsub;
  2570. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2571. aux_offset, aux_offset - alignment);
  2572. aux_x = x * hsub + aux_x % hsub;
  2573. aux_y = y * vsub + aux_y % vsub;
  2574. }
  2575. if (aux_x != main_x || aux_y != main_y)
  2576. return false;
  2577. plane_state->aux.offset = aux_offset;
  2578. plane_state->aux.x = aux_x;
  2579. plane_state->aux.y = aux_y;
  2580. return true;
  2581. }
  2582. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2583. {
  2584. const struct drm_framebuffer *fb = plane_state->base.fb;
  2585. unsigned int rotation = plane_state->base.rotation;
  2586. int x = plane_state->base.src.x1 >> 16;
  2587. int y = plane_state->base.src.y1 >> 16;
  2588. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2589. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2590. int max_width = skl_max_plane_width(fb, 0, rotation);
  2591. int max_height = 4096;
  2592. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2593. if (w > max_width || h > max_height) {
  2594. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2595. w, h, max_width, max_height);
  2596. return -EINVAL;
  2597. }
  2598. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2599. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2600. alignment = intel_surf_alignment(fb, 0);
  2601. /*
  2602. * AUX surface offset is specified as the distance from the
  2603. * main surface offset, and it must be non-negative. Make
  2604. * sure that is what we will get.
  2605. */
  2606. if (offset > aux_offset)
  2607. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2608. offset, aux_offset & ~(alignment - 1));
  2609. /*
  2610. * When using an X-tiled surface, the plane blows up
  2611. * if the x offset + width exceed the stride.
  2612. *
  2613. * TODO: linear and Y-tiled seem fine, Yf untested,
  2614. */
  2615. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2616. int cpp = fb->format->cpp[0];
  2617. while ((x + w) * cpp > fb->pitches[0]) {
  2618. if (offset == 0) {
  2619. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2620. return -EINVAL;
  2621. }
  2622. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2623. offset, offset - alignment);
  2624. }
  2625. }
  2626. /*
  2627. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2628. * they match with the main surface x/y offsets.
  2629. */
  2630. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2631. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2632. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2633. if (offset == 0)
  2634. break;
  2635. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2636. offset, offset - alignment);
  2637. }
  2638. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2639. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2640. return -EINVAL;
  2641. }
  2642. }
  2643. plane_state->main.offset = offset;
  2644. plane_state->main.x = x;
  2645. plane_state->main.y = y;
  2646. return 0;
  2647. }
  2648. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2649. {
  2650. const struct drm_framebuffer *fb = plane_state->base.fb;
  2651. unsigned int rotation = plane_state->base.rotation;
  2652. int max_width = skl_max_plane_width(fb, 1, rotation);
  2653. int max_height = 4096;
  2654. int x = plane_state->base.src.x1 >> 17;
  2655. int y = plane_state->base.src.y1 >> 17;
  2656. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2657. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2658. u32 offset;
  2659. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2660. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2661. /* FIXME not quite sure how/if these apply to the chroma plane */
  2662. if (w > max_width || h > max_height) {
  2663. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2664. w, h, max_width, max_height);
  2665. return -EINVAL;
  2666. }
  2667. plane_state->aux.offset = offset;
  2668. plane_state->aux.x = x;
  2669. plane_state->aux.y = y;
  2670. return 0;
  2671. }
  2672. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2673. {
  2674. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2675. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2676. const struct drm_framebuffer *fb = plane_state->base.fb;
  2677. int src_x = plane_state->base.src.x1 >> 16;
  2678. int src_y = plane_state->base.src.y1 >> 16;
  2679. int hsub = fb->format->hsub;
  2680. int vsub = fb->format->vsub;
  2681. int x = src_x / hsub;
  2682. int y = src_y / vsub;
  2683. u32 offset;
  2684. switch (plane->id) {
  2685. case PLANE_PRIMARY:
  2686. case PLANE_SPRITE0:
  2687. break;
  2688. default:
  2689. DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
  2690. return -EINVAL;
  2691. }
  2692. if (crtc->pipe == PIPE_C) {
  2693. DRM_DEBUG_KMS("No RC support on pipe C\n");
  2694. return -EINVAL;
  2695. }
  2696. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2697. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2698. plane_state->base.rotation);
  2699. return -EINVAL;
  2700. }
  2701. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2702. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2703. plane_state->aux.offset = offset;
  2704. plane_state->aux.x = x * hsub + src_x % hsub;
  2705. plane_state->aux.y = y * vsub + src_y % vsub;
  2706. return 0;
  2707. }
  2708. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2709. {
  2710. const struct drm_framebuffer *fb = plane_state->base.fb;
  2711. unsigned int rotation = plane_state->base.rotation;
  2712. int ret;
  2713. if (!plane_state->base.visible)
  2714. return 0;
  2715. /* Rotate src coordinates to match rotated GTT view */
  2716. if (drm_rotation_90_or_270(rotation))
  2717. drm_rect_rotate(&plane_state->base.src,
  2718. fb->width << 16, fb->height << 16,
  2719. DRM_MODE_ROTATE_270);
  2720. /*
  2721. * Handle the AUX surface first since
  2722. * the main surface setup depends on it.
  2723. */
  2724. if (fb->format->format == DRM_FORMAT_NV12) {
  2725. ret = skl_check_nv12_aux_surface(plane_state);
  2726. if (ret)
  2727. return ret;
  2728. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2729. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2730. ret = skl_check_ccs_aux_surface(plane_state);
  2731. if (ret)
  2732. return ret;
  2733. } else {
  2734. plane_state->aux.offset = ~0xfff;
  2735. plane_state->aux.x = 0;
  2736. plane_state->aux.y = 0;
  2737. }
  2738. ret = skl_check_main_surface(plane_state);
  2739. if (ret)
  2740. return ret;
  2741. return 0;
  2742. }
  2743. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2744. const struct intel_plane_state *plane_state)
  2745. {
  2746. struct drm_i915_private *dev_priv =
  2747. to_i915(plane_state->base.plane->dev);
  2748. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2749. const struct drm_framebuffer *fb = plane_state->base.fb;
  2750. unsigned int rotation = plane_state->base.rotation;
  2751. u32 dspcntr;
  2752. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2753. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2754. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2755. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2756. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2757. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2758. if (INTEL_GEN(dev_priv) < 4)
  2759. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2760. switch (fb->format->format) {
  2761. case DRM_FORMAT_C8:
  2762. dspcntr |= DISPPLANE_8BPP;
  2763. break;
  2764. case DRM_FORMAT_XRGB1555:
  2765. dspcntr |= DISPPLANE_BGRX555;
  2766. break;
  2767. case DRM_FORMAT_RGB565:
  2768. dspcntr |= DISPPLANE_BGRX565;
  2769. break;
  2770. case DRM_FORMAT_XRGB8888:
  2771. dspcntr |= DISPPLANE_BGRX888;
  2772. break;
  2773. case DRM_FORMAT_XBGR8888:
  2774. dspcntr |= DISPPLANE_RGBX888;
  2775. break;
  2776. case DRM_FORMAT_XRGB2101010:
  2777. dspcntr |= DISPPLANE_BGRX101010;
  2778. break;
  2779. case DRM_FORMAT_XBGR2101010:
  2780. dspcntr |= DISPPLANE_RGBX101010;
  2781. break;
  2782. default:
  2783. MISSING_CASE(fb->format->format);
  2784. return 0;
  2785. }
  2786. if (INTEL_GEN(dev_priv) >= 4 &&
  2787. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2788. dspcntr |= DISPPLANE_TILED;
  2789. if (rotation & DRM_MODE_ROTATE_180)
  2790. dspcntr |= DISPPLANE_ROTATE_180;
  2791. if (rotation & DRM_MODE_REFLECT_X)
  2792. dspcntr |= DISPPLANE_MIRROR;
  2793. return dspcntr;
  2794. }
  2795. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2796. {
  2797. struct drm_i915_private *dev_priv =
  2798. to_i915(plane_state->base.plane->dev);
  2799. int src_x = plane_state->base.src.x1 >> 16;
  2800. int src_y = plane_state->base.src.y1 >> 16;
  2801. u32 offset;
  2802. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2803. if (INTEL_GEN(dev_priv) >= 4)
  2804. offset = intel_compute_tile_offset(&src_x, &src_y,
  2805. plane_state, 0);
  2806. else
  2807. offset = 0;
  2808. /* HSW/BDW do this automagically in hardware */
  2809. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2810. unsigned int rotation = plane_state->base.rotation;
  2811. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2812. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2813. if (rotation & DRM_MODE_ROTATE_180) {
  2814. src_x += src_w - 1;
  2815. src_y += src_h - 1;
  2816. } else if (rotation & DRM_MODE_REFLECT_X) {
  2817. src_x += src_w - 1;
  2818. }
  2819. }
  2820. plane_state->main.offset = offset;
  2821. plane_state->main.x = src_x;
  2822. plane_state->main.y = src_y;
  2823. return 0;
  2824. }
  2825. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2826. const struct intel_crtc_state *crtc_state,
  2827. const struct intel_plane_state *plane_state)
  2828. {
  2829. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2830. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2831. const struct drm_framebuffer *fb = plane_state->base.fb;
  2832. enum plane plane = primary->plane;
  2833. u32 linear_offset;
  2834. u32 dspcntr = plane_state->ctl;
  2835. i915_reg_t reg = DSPCNTR(plane);
  2836. int x = plane_state->main.x;
  2837. int y = plane_state->main.y;
  2838. unsigned long irqflags;
  2839. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2840. if (INTEL_GEN(dev_priv) >= 4)
  2841. crtc->dspaddr_offset = plane_state->main.offset;
  2842. else
  2843. crtc->dspaddr_offset = linear_offset;
  2844. crtc->adjusted_x = x;
  2845. crtc->adjusted_y = y;
  2846. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2847. if (INTEL_GEN(dev_priv) < 4) {
  2848. /* pipesrc and dspsize control the size that is scaled from,
  2849. * which should always be the user's requested size.
  2850. */
  2851. I915_WRITE_FW(DSPSIZE(plane),
  2852. ((crtc_state->pipe_src_h - 1) << 16) |
  2853. (crtc_state->pipe_src_w - 1));
  2854. I915_WRITE_FW(DSPPOS(plane), 0);
  2855. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2856. I915_WRITE_FW(PRIMSIZE(plane),
  2857. ((crtc_state->pipe_src_h - 1) << 16) |
  2858. (crtc_state->pipe_src_w - 1));
  2859. I915_WRITE_FW(PRIMPOS(plane), 0);
  2860. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2861. }
  2862. I915_WRITE_FW(reg, dspcntr);
  2863. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2864. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2865. I915_WRITE_FW(DSPSURF(plane),
  2866. intel_plane_ggtt_offset(plane_state) +
  2867. crtc->dspaddr_offset);
  2868. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2869. } else if (INTEL_GEN(dev_priv) >= 4) {
  2870. I915_WRITE_FW(DSPSURF(plane),
  2871. intel_plane_ggtt_offset(plane_state) +
  2872. crtc->dspaddr_offset);
  2873. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2874. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2875. } else {
  2876. I915_WRITE_FW(DSPADDR(plane),
  2877. intel_plane_ggtt_offset(plane_state) +
  2878. crtc->dspaddr_offset);
  2879. }
  2880. POSTING_READ_FW(reg);
  2881. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2882. }
  2883. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2884. struct intel_crtc *crtc)
  2885. {
  2886. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2887. enum plane plane = primary->plane;
  2888. unsigned long irqflags;
  2889. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2890. I915_WRITE_FW(DSPCNTR(plane), 0);
  2891. if (INTEL_INFO(dev_priv)->gen >= 4)
  2892. I915_WRITE_FW(DSPSURF(plane), 0);
  2893. else
  2894. I915_WRITE_FW(DSPADDR(plane), 0);
  2895. POSTING_READ_FW(DSPCNTR(plane));
  2896. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2897. }
  2898. static u32
  2899. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2900. {
  2901. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2902. return 64;
  2903. else
  2904. return intel_tile_width_bytes(fb, plane);
  2905. }
  2906. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2907. {
  2908. struct drm_device *dev = intel_crtc->base.dev;
  2909. struct drm_i915_private *dev_priv = to_i915(dev);
  2910. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2911. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2912. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2913. }
  2914. /*
  2915. * This function detaches (aka. unbinds) unused scalers in hardware
  2916. */
  2917. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2918. {
  2919. struct intel_crtc_scaler_state *scaler_state;
  2920. int i;
  2921. scaler_state = &intel_crtc->config->scaler_state;
  2922. /* loop through and disable scalers that aren't in use */
  2923. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2924. if (!scaler_state->scalers[i].in_use)
  2925. skl_detach_scaler(intel_crtc, i);
  2926. }
  2927. }
  2928. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2929. unsigned int rotation)
  2930. {
  2931. u32 stride;
  2932. if (plane >= fb->format->num_planes)
  2933. return 0;
  2934. stride = intel_fb_pitch(fb, plane, rotation);
  2935. /*
  2936. * The stride is either expressed as a multiple of 64 bytes chunks for
  2937. * linear buffers or in number of tiles for tiled buffers.
  2938. */
  2939. if (drm_rotation_90_or_270(rotation))
  2940. stride /= intel_tile_height(fb, plane);
  2941. else
  2942. stride /= intel_fb_stride_alignment(fb, plane);
  2943. return stride;
  2944. }
  2945. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2946. {
  2947. switch (pixel_format) {
  2948. case DRM_FORMAT_C8:
  2949. return PLANE_CTL_FORMAT_INDEXED;
  2950. case DRM_FORMAT_RGB565:
  2951. return PLANE_CTL_FORMAT_RGB_565;
  2952. case DRM_FORMAT_XBGR8888:
  2953. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2954. case DRM_FORMAT_XRGB8888:
  2955. return PLANE_CTL_FORMAT_XRGB_8888;
  2956. /*
  2957. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2958. * to be already pre-multiplied. We need to add a knob (or a different
  2959. * DRM_FORMAT) for user-space to configure that.
  2960. */
  2961. case DRM_FORMAT_ABGR8888:
  2962. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2963. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2964. case DRM_FORMAT_ARGB8888:
  2965. return PLANE_CTL_FORMAT_XRGB_8888 |
  2966. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2967. case DRM_FORMAT_XRGB2101010:
  2968. return PLANE_CTL_FORMAT_XRGB_2101010;
  2969. case DRM_FORMAT_XBGR2101010:
  2970. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2971. case DRM_FORMAT_YUYV:
  2972. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2973. case DRM_FORMAT_YVYU:
  2974. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2975. case DRM_FORMAT_UYVY:
  2976. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2977. case DRM_FORMAT_VYUY:
  2978. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2979. default:
  2980. MISSING_CASE(pixel_format);
  2981. }
  2982. return 0;
  2983. }
  2984. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2985. {
  2986. switch (fb_modifier) {
  2987. case DRM_FORMAT_MOD_LINEAR:
  2988. break;
  2989. case I915_FORMAT_MOD_X_TILED:
  2990. return PLANE_CTL_TILED_X;
  2991. case I915_FORMAT_MOD_Y_TILED:
  2992. return PLANE_CTL_TILED_Y;
  2993. case I915_FORMAT_MOD_Y_TILED_CCS:
  2994. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2995. case I915_FORMAT_MOD_Yf_TILED:
  2996. return PLANE_CTL_TILED_YF;
  2997. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2998. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  2999. default:
  3000. MISSING_CASE(fb_modifier);
  3001. }
  3002. return 0;
  3003. }
  3004. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  3005. {
  3006. switch (rotation) {
  3007. case DRM_MODE_ROTATE_0:
  3008. break;
  3009. /*
  3010. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3011. * while i915 HW rotation is clockwise, thats why this swapping.
  3012. */
  3013. case DRM_MODE_ROTATE_90:
  3014. return PLANE_CTL_ROTATE_270;
  3015. case DRM_MODE_ROTATE_180:
  3016. return PLANE_CTL_ROTATE_180;
  3017. case DRM_MODE_ROTATE_270:
  3018. return PLANE_CTL_ROTATE_90;
  3019. default:
  3020. MISSING_CASE(rotation);
  3021. }
  3022. return 0;
  3023. }
  3024. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3025. const struct intel_plane_state *plane_state)
  3026. {
  3027. struct drm_i915_private *dev_priv =
  3028. to_i915(plane_state->base.plane->dev);
  3029. const struct drm_framebuffer *fb = plane_state->base.fb;
  3030. unsigned int rotation = plane_state->base.rotation;
  3031. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3032. u32 plane_ctl;
  3033. plane_ctl = PLANE_CTL_ENABLE;
  3034. if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
  3035. plane_ctl |=
  3036. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3037. PLANE_CTL_PIPE_CSC_ENABLE |
  3038. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3039. }
  3040. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3041. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3042. plane_ctl |= skl_plane_ctl_rotation(rotation);
  3043. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3044. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3045. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3046. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3047. return plane_ctl;
  3048. }
  3049. static void skylake_update_primary_plane(struct intel_plane *plane,
  3050. const struct intel_crtc_state *crtc_state,
  3051. const struct intel_plane_state *plane_state)
  3052. {
  3053. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  3054. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3055. const struct drm_framebuffer *fb = plane_state->base.fb;
  3056. enum plane_id plane_id = plane->id;
  3057. enum pipe pipe = plane->pipe;
  3058. u32 plane_ctl = plane_state->ctl;
  3059. unsigned int rotation = plane_state->base.rotation;
  3060. u32 stride = skl_plane_stride(fb, 0, rotation);
  3061. u32 aux_stride = skl_plane_stride(fb, 1, rotation);
  3062. u32 surf_addr = plane_state->main.offset;
  3063. int scaler_id = plane_state->scaler_id;
  3064. int src_x = plane_state->main.x;
  3065. int src_y = plane_state->main.y;
  3066. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  3067. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  3068. int dst_x = plane_state->base.dst.x1;
  3069. int dst_y = plane_state->base.dst.y1;
  3070. int dst_w = drm_rect_width(&plane_state->base.dst);
  3071. int dst_h = drm_rect_height(&plane_state->base.dst);
  3072. unsigned long irqflags;
  3073. /* Sizes are 0 based */
  3074. src_w--;
  3075. src_h--;
  3076. dst_w--;
  3077. dst_h--;
  3078. crtc->dspaddr_offset = surf_addr;
  3079. crtc->adjusted_x = src_x;
  3080. crtc->adjusted_y = src_y;
  3081. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3082. if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
  3083. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  3084. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  3085. PLANE_COLOR_PIPE_CSC_ENABLE |
  3086. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  3087. }
  3088. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  3089. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  3090. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  3091. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  3092. I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
  3093. (plane_state->aux.offset - surf_addr) | aux_stride);
  3094. I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
  3095. (plane_state->aux.y << 16) | plane_state->aux.x);
  3096. if (scaler_id >= 0) {
  3097. uint32_t ps_ctrl = 0;
  3098. WARN_ON(!dst_w || !dst_h);
  3099. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  3100. crtc_state->scaler_state.scalers[scaler_id].mode;
  3101. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  3102. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  3103. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  3104. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  3105. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  3106. } else {
  3107. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  3108. }
  3109. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  3110. intel_plane_ggtt_offset(plane_state) + surf_addr);
  3111. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  3112. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3113. }
  3114. static void skylake_disable_primary_plane(struct intel_plane *primary,
  3115. struct intel_crtc *crtc)
  3116. {
  3117. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  3118. enum plane_id plane_id = primary->id;
  3119. enum pipe pipe = primary->pipe;
  3120. unsigned long irqflags;
  3121. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3122. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  3123. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  3124. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  3125. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3126. }
  3127. static int
  3128. __intel_display_resume(struct drm_device *dev,
  3129. struct drm_atomic_state *state,
  3130. struct drm_modeset_acquire_ctx *ctx)
  3131. {
  3132. struct drm_crtc_state *crtc_state;
  3133. struct drm_crtc *crtc;
  3134. int i, ret;
  3135. intel_modeset_setup_hw_state(dev, ctx);
  3136. i915_redisable_vga(to_i915(dev));
  3137. if (!state)
  3138. return 0;
  3139. /*
  3140. * We've duplicated the state, pointers to the old state are invalid.
  3141. *
  3142. * Don't attempt to use the old state until we commit the duplicated state.
  3143. */
  3144. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3145. /*
  3146. * Force recalculation even if we restore
  3147. * current state. With fast modeset this may not result
  3148. * in a modeset when the state is compatible.
  3149. */
  3150. crtc_state->mode_changed = true;
  3151. }
  3152. /* ignore any reset values/BIOS leftovers in the WM registers */
  3153. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3154. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3155. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3156. WARN_ON(ret == -EDEADLK);
  3157. return ret;
  3158. }
  3159. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3160. {
  3161. return intel_has_gpu_reset(dev_priv) &&
  3162. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3163. }
  3164. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3165. {
  3166. struct drm_device *dev = &dev_priv->drm;
  3167. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3168. struct drm_atomic_state *state;
  3169. int ret;
  3170. /* reset doesn't touch the display */
  3171. if (!i915.force_reset_modeset_test &&
  3172. !gpu_reset_clobbers_display(dev_priv))
  3173. return;
  3174. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3175. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3176. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3177. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3178. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3179. i915_gem_set_wedged(dev_priv);
  3180. }
  3181. /*
  3182. * Need mode_config.mutex so that we don't
  3183. * trample ongoing ->detect() and whatnot.
  3184. */
  3185. mutex_lock(&dev->mode_config.mutex);
  3186. drm_modeset_acquire_init(ctx, 0);
  3187. while (1) {
  3188. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3189. if (ret != -EDEADLK)
  3190. break;
  3191. drm_modeset_backoff(ctx);
  3192. }
  3193. /*
  3194. * Disabling the crtcs gracefully seems nicer. Also the
  3195. * g33 docs say we should at least disable all the planes.
  3196. */
  3197. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3198. if (IS_ERR(state)) {
  3199. ret = PTR_ERR(state);
  3200. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3201. return;
  3202. }
  3203. ret = drm_atomic_helper_disable_all(dev, ctx);
  3204. if (ret) {
  3205. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3206. drm_atomic_state_put(state);
  3207. return;
  3208. }
  3209. dev_priv->modeset_restore_state = state;
  3210. state->acquire_ctx = ctx;
  3211. }
  3212. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3213. {
  3214. struct drm_device *dev = &dev_priv->drm;
  3215. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3216. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3217. int ret;
  3218. /* reset doesn't touch the display */
  3219. if (!i915.force_reset_modeset_test &&
  3220. !gpu_reset_clobbers_display(dev_priv))
  3221. return;
  3222. if (!state)
  3223. goto unlock;
  3224. dev_priv->modeset_restore_state = NULL;
  3225. /* reset doesn't touch the display */
  3226. if (!gpu_reset_clobbers_display(dev_priv)) {
  3227. /* for testing only restore the display */
  3228. ret = __intel_display_resume(dev, state, ctx);
  3229. if (ret)
  3230. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3231. } else {
  3232. /*
  3233. * The display has been reset as well,
  3234. * so need a full re-initialization.
  3235. */
  3236. intel_runtime_pm_disable_interrupts(dev_priv);
  3237. intel_runtime_pm_enable_interrupts(dev_priv);
  3238. intel_pps_unlock_regs_wa(dev_priv);
  3239. intel_modeset_init_hw(dev);
  3240. spin_lock_irq(&dev_priv->irq_lock);
  3241. if (dev_priv->display.hpd_irq_setup)
  3242. dev_priv->display.hpd_irq_setup(dev_priv);
  3243. spin_unlock_irq(&dev_priv->irq_lock);
  3244. ret = __intel_display_resume(dev, state, ctx);
  3245. if (ret)
  3246. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3247. intel_hpd_init(dev_priv);
  3248. }
  3249. drm_atomic_state_put(state);
  3250. unlock:
  3251. drm_modeset_drop_locks(ctx);
  3252. drm_modeset_acquire_fini(ctx);
  3253. mutex_unlock(&dev->mode_config.mutex);
  3254. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3255. }
  3256. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3257. struct intel_crtc_state *old_crtc_state)
  3258. {
  3259. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3260. struct intel_crtc_state *pipe_config =
  3261. to_intel_crtc_state(crtc->base.state);
  3262. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3263. crtc->base.mode = crtc->base.state->mode;
  3264. /*
  3265. * Update pipe size and adjust fitter if needed: the reason for this is
  3266. * that in compute_mode_changes we check the native mode (not the pfit
  3267. * mode) to see if we can flip rather than do a full mode set. In the
  3268. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3269. * pfit state, we'll end up with a big fb scanned out into the wrong
  3270. * sized surface.
  3271. */
  3272. I915_WRITE(PIPESRC(crtc->pipe),
  3273. ((pipe_config->pipe_src_w - 1) << 16) |
  3274. (pipe_config->pipe_src_h - 1));
  3275. /* on skylake this is done by detaching scalers */
  3276. if (INTEL_GEN(dev_priv) >= 9) {
  3277. skl_detach_scalers(crtc);
  3278. if (pipe_config->pch_pfit.enabled)
  3279. skylake_pfit_enable(crtc);
  3280. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3281. if (pipe_config->pch_pfit.enabled)
  3282. ironlake_pfit_enable(crtc);
  3283. else if (old_crtc_state->pch_pfit.enabled)
  3284. ironlake_pfit_disable(crtc, true);
  3285. }
  3286. }
  3287. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3288. {
  3289. struct drm_device *dev = crtc->base.dev;
  3290. struct drm_i915_private *dev_priv = to_i915(dev);
  3291. int pipe = crtc->pipe;
  3292. i915_reg_t reg;
  3293. u32 temp;
  3294. /* enable normal train */
  3295. reg = FDI_TX_CTL(pipe);
  3296. temp = I915_READ(reg);
  3297. if (IS_IVYBRIDGE(dev_priv)) {
  3298. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3299. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3300. } else {
  3301. temp &= ~FDI_LINK_TRAIN_NONE;
  3302. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3303. }
  3304. I915_WRITE(reg, temp);
  3305. reg = FDI_RX_CTL(pipe);
  3306. temp = I915_READ(reg);
  3307. if (HAS_PCH_CPT(dev_priv)) {
  3308. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3309. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3310. } else {
  3311. temp &= ~FDI_LINK_TRAIN_NONE;
  3312. temp |= FDI_LINK_TRAIN_NONE;
  3313. }
  3314. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3315. /* wait one idle pattern time */
  3316. POSTING_READ(reg);
  3317. udelay(1000);
  3318. /* IVB wants error correction enabled */
  3319. if (IS_IVYBRIDGE(dev_priv))
  3320. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3321. FDI_FE_ERRC_ENABLE);
  3322. }
  3323. /* The FDI link training functions for ILK/Ibexpeak. */
  3324. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3325. const struct intel_crtc_state *crtc_state)
  3326. {
  3327. struct drm_device *dev = crtc->base.dev;
  3328. struct drm_i915_private *dev_priv = to_i915(dev);
  3329. int pipe = crtc->pipe;
  3330. i915_reg_t reg;
  3331. u32 temp, tries;
  3332. /* FDI needs bits from pipe first */
  3333. assert_pipe_enabled(dev_priv, pipe);
  3334. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3335. for train result */
  3336. reg = FDI_RX_IMR(pipe);
  3337. temp = I915_READ(reg);
  3338. temp &= ~FDI_RX_SYMBOL_LOCK;
  3339. temp &= ~FDI_RX_BIT_LOCK;
  3340. I915_WRITE(reg, temp);
  3341. I915_READ(reg);
  3342. udelay(150);
  3343. /* enable CPU FDI TX and PCH FDI RX */
  3344. reg = FDI_TX_CTL(pipe);
  3345. temp = I915_READ(reg);
  3346. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3347. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3348. temp &= ~FDI_LINK_TRAIN_NONE;
  3349. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3350. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3351. reg = FDI_RX_CTL(pipe);
  3352. temp = I915_READ(reg);
  3353. temp &= ~FDI_LINK_TRAIN_NONE;
  3354. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3355. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3356. POSTING_READ(reg);
  3357. udelay(150);
  3358. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3359. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3360. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3361. FDI_RX_PHASE_SYNC_POINTER_EN);
  3362. reg = FDI_RX_IIR(pipe);
  3363. for (tries = 0; tries < 5; tries++) {
  3364. temp = I915_READ(reg);
  3365. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3366. if ((temp & FDI_RX_BIT_LOCK)) {
  3367. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3368. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3369. break;
  3370. }
  3371. }
  3372. if (tries == 5)
  3373. DRM_ERROR("FDI train 1 fail!\n");
  3374. /* Train 2 */
  3375. reg = FDI_TX_CTL(pipe);
  3376. temp = I915_READ(reg);
  3377. temp &= ~FDI_LINK_TRAIN_NONE;
  3378. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3379. I915_WRITE(reg, temp);
  3380. reg = FDI_RX_CTL(pipe);
  3381. temp = I915_READ(reg);
  3382. temp &= ~FDI_LINK_TRAIN_NONE;
  3383. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3384. I915_WRITE(reg, temp);
  3385. POSTING_READ(reg);
  3386. udelay(150);
  3387. reg = FDI_RX_IIR(pipe);
  3388. for (tries = 0; tries < 5; tries++) {
  3389. temp = I915_READ(reg);
  3390. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3391. if (temp & FDI_RX_SYMBOL_LOCK) {
  3392. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3393. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3394. break;
  3395. }
  3396. }
  3397. if (tries == 5)
  3398. DRM_ERROR("FDI train 2 fail!\n");
  3399. DRM_DEBUG_KMS("FDI train done\n");
  3400. }
  3401. static const int snb_b_fdi_train_param[] = {
  3402. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3403. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3404. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3405. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3406. };
  3407. /* The FDI link training functions for SNB/Cougarpoint. */
  3408. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3409. const struct intel_crtc_state *crtc_state)
  3410. {
  3411. struct drm_device *dev = crtc->base.dev;
  3412. struct drm_i915_private *dev_priv = to_i915(dev);
  3413. int pipe = crtc->pipe;
  3414. i915_reg_t reg;
  3415. u32 temp, i, retry;
  3416. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3417. for train result */
  3418. reg = FDI_RX_IMR(pipe);
  3419. temp = I915_READ(reg);
  3420. temp &= ~FDI_RX_SYMBOL_LOCK;
  3421. temp &= ~FDI_RX_BIT_LOCK;
  3422. I915_WRITE(reg, temp);
  3423. POSTING_READ(reg);
  3424. udelay(150);
  3425. /* enable CPU FDI TX and PCH FDI RX */
  3426. reg = FDI_TX_CTL(pipe);
  3427. temp = I915_READ(reg);
  3428. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3429. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3430. temp &= ~FDI_LINK_TRAIN_NONE;
  3431. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3432. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3433. /* SNB-B */
  3434. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3435. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3436. I915_WRITE(FDI_RX_MISC(pipe),
  3437. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3438. reg = FDI_RX_CTL(pipe);
  3439. temp = I915_READ(reg);
  3440. if (HAS_PCH_CPT(dev_priv)) {
  3441. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3442. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3443. } else {
  3444. temp &= ~FDI_LINK_TRAIN_NONE;
  3445. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3446. }
  3447. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3448. POSTING_READ(reg);
  3449. udelay(150);
  3450. for (i = 0; i < 4; i++) {
  3451. reg = FDI_TX_CTL(pipe);
  3452. temp = I915_READ(reg);
  3453. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3454. temp |= snb_b_fdi_train_param[i];
  3455. I915_WRITE(reg, temp);
  3456. POSTING_READ(reg);
  3457. udelay(500);
  3458. for (retry = 0; retry < 5; retry++) {
  3459. reg = FDI_RX_IIR(pipe);
  3460. temp = I915_READ(reg);
  3461. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3462. if (temp & FDI_RX_BIT_LOCK) {
  3463. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3464. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3465. break;
  3466. }
  3467. udelay(50);
  3468. }
  3469. if (retry < 5)
  3470. break;
  3471. }
  3472. if (i == 4)
  3473. DRM_ERROR("FDI train 1 fail!\n");
  3474. /* Train 2 */
  3475. reg = FDI_TX_CTL(pipe);
  3476. temp = I915_READ(reg);
  3477. temp &= ~FDI_LINK_TRAIN_NONE;
  3478. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3479. if (IS_GEN6(dev_priv)) {
  3480. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3481. /* SNB-B */
  3482. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3483. }
  3484. I915_WRITE(reg, temp);
  3485. reg = FDI_RX_CTL(pipe);
  3486. temp = I915_READ(reg);
  3487. if (HAS_PCH_CPT(dev_priv)) {
  3488. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3489. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3490. } else {
  3491. temp &= ~FDI_LINK_TRAIN_NONE;
  3492. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3493. }
  3494. I915_WRITE(reg, temp);
  3495. POSTING_READ(reg);
  3496. udelay(150);
  3497. for (i = 0; i < 4; i++) {
  3498. reg = FDI_TX_CTL(pipe);
  3499. temp = I915_READ(reg);
  3500. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3501. temp |= snb_b_fdi_train_param[i];
  3502. I915_WRITE(reg, temp);
  3503. POSTING_READ(reg);
  3504. udelay(500);
  3505. for (retry = 0; retry < 5; retry++) {
  3506. reg = FDI_RX_IIR(pipe);
  3507. temp = I915_READ(reg);
  3508. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3509. if (temp & FDI_RX_SYMBOL_LOCK) {
  3510. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3511. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3512. break;
  3513. }
  3514. udelay(50);
  3515. }
  3516. if (retry < 5)
  3517. break;
  3518. }
  3519. if (i == 4)
  3520. DRM_ERROR("FDI train 2 fail!\n");
  3521. DRM_DEBUG_KMS("FDI train done.\n");
  3522. }
  3523. /* Manual link training for Ivy Bridge A0 parts */
  3524. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3525. const struct intel_crtc_state *crtc_state)
  3526. {
  3527. struct drm_device *dev = crtc->base.dev;
  3528. struct drm_i915_private *dev_priv = to_i915(dev);
  3529. int pipe = crtc->pipe;
  3530. i915_reg_t reg;
  3531. u32 temp, i, j;
  3532. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3533. for train result */
  3534. reg = FDI_RX_IMR(pipe);
  3535. temp = I915_READ(reg);
  3536. temp &= ~FDI_RX_SYMBOL_LOCK;
  3537. temp &= ~FDI_RX_BIT_LOCK;
  3538. I915_WRITE(reg, temp);
  3539. POSTING_READ(reg);
  3540. udelay(150);
  3541. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3542. I915_READ(FDI_RX_IIR(pipe)));
  3543. /* Try each vswing and preemphasis setting twice before moving on */
  3544. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3545. /* disable first in case we need to retry */
  3546. reg = FDI_TX_CTL(pipe);
  3547. temp = I915_READ(reg);
  3548. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3549. temp &= ~FDI_TX_ENABLE;
  3550. I915_WRITE(reg, temp);
  3551. reg = FDI_RX_CTL(pipe);
  3552. temp = I915_READ(reg);
  3553. temp &= ~FDI_LINK_TRAIN_AUTO;
  3554. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3555. temp &= ~FDI_RX_ENABLE;
  3556. I915_WRITE(reg, temp);
  3557. /* enable CPU FDI TX and PCH FDI RX */
  3558. reg = FDI_TX_CTL(pipe);
  3559. temp = I915_READ(reg);
  3560. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3561. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3562. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3563. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3564. temp |= snb_b_fdi_train_param[j/2];
  3565. temp |= FDI_COMPOSITE_SYNC;
  3566. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3567. I915_WRITE(FDI_RX_MISC(pipe),
  3568. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3569. reg = FDI_RX_CTL(pipe);
  3570. temp = I915_READ(reg);
  3571. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3572. temp |= FDI_COMPOSITE_SYNC;
  3573. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3574. POSTING_READ(reg);
  3575. udelay(1); /* should be 0.5us */
  3576. for (i = 0; i < 4; i++) {
  3577. reg = FDI_RX_IIR(pipe);
  3578. temp = I915_READ(reg);
  3579. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3580. if (temp & FDI_RX_BIT_LOCK ||
  3581. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3582. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3583. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3584. i);
  3585. break;
  3586. }
  3587. udelay(1); /* should be 0.5us */
  3588. }
  3589. if (i == 4) {
  3590. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3591. continue;
  3592. }
  3593. /* Train 2 */
  3594. reg = FDI_TX_CTL(pipe);
  3595. temp = I915_READ(reg);
  3596. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3597. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3598. I915_WRITE(reg, temp);
  3599. reg = FDI_RX_CTL(pipe);
  3600. temp = I915_READ(reg);
  3601. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3602. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3603. I915_WRITE(reg, temp);
  3604. POSTING_READ(reg);
  3605. udelay(2); /* should be 1.5us */
  3606. for (i = 0; i < 4; i++) {
  3607. reg = FDI_RX_IIR(pipe);
  3608. temp = I915_READ(reg);
  3609. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3610. if (temp & FDI_RX_SYMBOL_LOCK ||
  3611. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3612. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3613. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3614. i);
  3615. goto train_done;
  3616. }
  3617. udelay(2); /* should be 1.5us */
  3618. }
  3619. if (i == 4)
  3620. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3621. }
  3622. train_done:
  3623. DRM_DEBUG_KMS("FDI train done.\n");
  3624. }
  3625. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3626. {
  3627. struct drm_device *dev = intel_crtc->base.dev;
  3628. struct drm_i915_private *dev_priv = to_i915(dev);
  3629. int pipe = intel_crtc->pipe;
  3630. i915_reg_t reg;
  3631. u32 temp;
  3632. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3633. reg = FDI_RX_CTL(pipe);
  3634. temp = I915_READ(reg);
  3635. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3636. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3637. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3638. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3639. POSTING_READ(reg);
  3640. udelay(200);
  3641. /* Switch from Rawclk to PCDclk */
  3642. temp = I915_READ(reg);
  3643. I915_WRITE(reg, temp | FDI_PCDCLK);
  3644. POSTING_READ(reg);
  3645. udelay(200);
  3646. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3647. reg = FDI_TX_CTL(pipe);
  3648. temp = I915_READ(reg);
  3649. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3650. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3651. POSTING_READ(reg);
  3652. udelay(100);
  3653. }
  3654. }
  3655. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3656. {
  3657. struct drm_device *dev = intel_crtc->base.dev;
  3658. struct drm_i915_private *dev_priv = to_i915(dev);
  3659. int pipe = intel_crtc->pipe;
  3660. i915_reg_t reg;
  3661. u32 temp;
  3662. /* Switch from PCDclk to Rawclk */
  3663. reg = FDI_RX_CTL(pipe);
  3664. temp = I915_READ(reg);
  3665. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3666. /* Disable CPU FDI TX PLL */
  3667. reg = FDI_TX_CTL(pipe);
  3668. temp = I915_READ(reg);
  3669. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3670. POSTING_READ(reg);
  3671. udelay(100);
  3672. reg = FDI_RX_CTL(pipe);
  3673. temp = I915_READ(reg);
  3674. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3675. /* Wait for the clocks to turn off. */
  3676. POSTING_READ(reg);
  3677. udelay(100);
  3678. }
  3679. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3680. {
  3681. struct drm_device *dev = crtc->dev;
  3682. struct drm_i915_private *dev_priv = to_i915(dev);
  3683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3684. int pipe = intel_crtc->pipe;
  3685. i915_reg_t reg;
  3686. u32 temp;
  3687. /* disable CPU FDI tx and PCH FDI rx */
  3688. reg = FDI_TX_CTL(pipe);
  3689. temp = I915_READ(reg);
  3690. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3691. POSTING_READ(reg);
  3692. reg = FDI_RX_CTL(pipe);
  3693. temp = I915_READ(reg);
  3694. temp &= ~(0x7 << 16);
  3695. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3696. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3697. POSTING_READ(reg);
  3698. udelay(100);
  3699. /* Ironlake workaround, disable clock pointer after downing FDI */
  3700. if (HAS_PCH_IBX(dev_priv))
  3701. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3702. /* still set train pattern 1 */
  3703. reg = FDI_TX_CTL(pipe);
  3704. temp = I915_READ(reg);
  3705. temp &= ~FDI_LINK_TRAIN_NONE;
  3706. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3707. I915_WRITE(reg, temp);
  3708. reg = FDI_RX_CTL(pipe);
  3709. temp = I915_READ(reg);
  3710. if (HAS_PCH_CPT(dev_priv)) {
  3711. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3712. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3713. } else {
  3714. temp &= ~FDI_LINK_TRAIN_NONE;
  3715. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3716. }
  3717. /* BPC in FDI rx is consistent with that in PIPECONF */
  3718. temp &= ~(0x07 << 16);
  3719. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3720. I915_WRITE(reg, temp);
  3721. POSTING_READ(reg);
  3722. udelay(100);
  3723. }
  3724. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3725. {
  3726. struct drm_crtc *crtc;
  3727. bool cleanup_done;
  3728. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3729. struct drm_crtc_commit *commit;
  3730. spin_lock(&crtc->commit_lock);
  3731. commit = list_first_entry_or_null(&crtc->commit_list,
  3732. struct drm_crtc_commit, commit_entry);
  3733. cleanup_done = commit ?
  3734. try_wait_for_completion(&commit->cleanup_done) : true;
  3735. spin_unlock(&crtc->commit_lock);
  3736. if (cleanup_done)
  3737. continue;
  3738. drm_crtc_wait_one_vblank(crtc);
  3739. return true;
  3740. }
  3741. return false;
  3742. }
  3743. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3744. {
  3745. u32 temp;
  3746. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3747. mutex_lock(&dev_priv->sb_lock);
  3748. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3749. temp |= SBI_SSCCTL_DISABLE;
  3750. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3751. mutex_unlock(&dev_priv->sb_lock);
  3752. }
  3753. /* Program iCLKIP clock to the desired frequency */
  3754. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3755. {
  3756. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3757. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3758. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3759. u32 temp;
  3760. lpt_disable_iclkip(dev_priv);
  3761. /* The iCLK virtual clock root frequency is in MHz,
  3762. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3763. * divisors, it is necessary to divide one by another, so we
  3764. * convert the virtual clock precision to KHz here for higher
  3765. * precision.
  3766. */
  3767. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3768. u32 iclk_virtual_root_freq = 172800 * 1000;
  3769. u32 iclk_pi_range = 64;
  3770. u32 desired_divisor;
  3771. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3772. clock << auxdiv);
  3773. divsel = (desired_divisor / iclk_pi_range) - 2;
  3774. phaseinc = desired_divisor % iclk_pi_range;
  3775. /*
  3776. * Near 20MHz is a corner case which is
  3777. * out of range for the 7-bit divisor
  3778. */
  3779. if (divsel <= 0x7f)
  3780. break;
  3781. }
  3782. /* This should not happen with any sane values */
  3783. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3784. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3785. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3786. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3787. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3788. clock,
  3789. auxdiv,
  3790. divsel,
  3791. phasedir,
  3792. phaseinc);
  3793. mutex_lock(&dev_priv->sb_lock);
  3794. /* Program SSCDIVINTPHASE6 */
  3795. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3796. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3797. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3798. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3799. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3800. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3801. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3802. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3803. /* Program SSCAUXDIV */
  3804. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3805. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3806. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3807. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3808. /* Enable modulator and associated divider */
  3809. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3810. temp &= ~SBI_SSCCTL_DISABLE;
  3811. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3812. mutex_unlock(&dev_priv->sb_lock);
  3813. /* Wait for initialization time */
  3814. udelay(24);
  3815. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3816. }
  3817. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3818. {
  3819. u32 divsel, phaseinc, auxdiv;
  3820. u32 iclk_virtual_root_freq = 172800 * 1000;
  3821. u32 iclk_pi_range = 64;
  3822. u32 desired_divisor;
  3823. u32 temp;
  3824. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3825. return 0;
  3826. mutex_lock(&dev_priv->sb_lock);
  3827. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3828. if (temp & SBI_SSCCTL_DISABLE) {
  3829. mutex_unlock(&dev_priv->sb_lock);
  3830. return 0;
  3831. }
  3832. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3833. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3834. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3835. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3836. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3837. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3838. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3839. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3840. mutex_unlock(&dev_priv->sb_lock);
  3841. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3842. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3843. desired_divisor << auxdiv);
  3844. }
  3845. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3846. enum pipe pch_transcoder)
  3847. {
  3848. struct drm_device *dev = crtc->base.dev;
  3849. struct drm_i915_private *dev_priv = to_i915(dev);
  3850. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3851. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3852. I915_READ(HTOTAL(cpu_transcoder)));
  3853. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3854. I915_READ(HBLANK(cpu_transcoder)));
  3855. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3856. I915_READ(HSYNC(cpu_transcoder)));
  3857. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3858. I915_READ(VTOTAL(cpu_transcoder)));
  3859. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3860. I915_READ(VBLANK(cpu_transcoder)));
  3861. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3862. I915_READ(VSYNC(cpu_transcoder)));
  3863. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3864. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3865. }
  3866. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3867. {
  3868. struct drm_i915_private *dev_priv = to_i915(dev);
  3869. uint32_t temp;
  3870. temp = I915_READ(SOUTH_CHICKEN1);
  3871. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3872. return;
  3873. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3874. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3875. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3876. if (enable)
  3877. temp |= FDI_BC_BIFURCATION_SELECT;
  3878. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3879. I915_WRITE(SOUTH_CHICKEN1, temp);
  3880. POSTING_READ(SOUTH_CHICKEN1);
  3881. }
  3882. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3883. {
  3884. struct drm_device *dev = intel_crtc->base.dev;
  3885. switch (intel_crtc->pipe) {
  3886. case PIPE_A:
  3887. break;
  3888. case PIPE_B:
  3889. if (intel_crtc->config->fdi_lanes > 2)
  3890. cpt_set_fdi_bc_bifurcation(dev, false);
  3891. else
  3892. cpt_set_fdi_bc_bifurcation(dev, true);
  3893. break;
  3894. case PIPE_C:
  3895. cpt_set_fdi_bc_bifurcation(dev, true);
  3896. break;
  3897. default:
  3898. BUG();
  3899. }
  3900. }
  3901. /* Return which DP Port should be selected for Transcoder DP control */
  3902. static enum port
  3903. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3904. {
  3905. struct drm_device *dev = crtc->base.dev;
  3906. struct intel_encoder *encoder;
  3907. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3908. if (encoder->type == INTEL_OUTPUT_DP ||
  3909. encoder->type == INTEL_OUTPUT_EDP)
  3910. return enc_to_dig_port(&encoder->base)->port;
  3911. }
  3912. return -1;
  3913. }
  3914. /*
  3915. * Enable PCH resources required for PCH ports:
  3916. * - PCH PLLs
  3917. * - FDI training & RX/TX
  3918. * - update transcoder timings
  3919. * - DP transcoding bits
  3920. * - transcoder
  3921. */
  3922. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3923. {
  3924. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3925. struct drm_device *dev = crtc->base.dev;
  3926. struct drm_i915_private *dev_priv = to_i915(dev);
  3927. int pipe = crtc->pipe;
  3928. u32 temp;
  3929. assert_pch_transcoder_disabled(dev_priv, pipe);
  3930. if (IS_IVYBRIDGE(dev_priv))
  3931. ivybridge_update_fdi_bc_bifurcation(crtc);
  3932. /* Write the TU size bits before fdi link training, so that error
  3933. * detection works. */
  3934. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3935. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3936. /* For PCH output, training FDI link */
  3937. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3938. /* We need to program the right clock selection before writing the pixel
  3939. * mutliplier into the DPLL. */
  3940. if (HAS_PCH_CPT(dev_priv)) {
  3941. u32 sel;
  3942. temp = I915_READ(PCH_DPLL_SEL);
  3943. temp |= TRANS_DPLL_ENABLE(pipe);
  3944. sel = TRANS_DPLLB_SEL(pipe);
  3945. if (crtc_state->shared_dpll ==
  3946. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3947. temp |= sel;
  3948. else
  3949. temp &= ~sel;
  3950. I915_WRITE(PCH_DPLL_SEL, temp);
  3951. }
  3952. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3953. * transcoder, and we actually should do this to not upset any PCH
  3954. * transcoder that already use the clock when we share it.
  3955. *
  3956. * Note that enable_shared_dpll tries to do the right thing, but
  3957. * get_shared_dpll unconditionally resets the pll - we need that to have
  3958. * the right LVDS enable sequence. */
  3959. intel_enable_shared_dpll(crtc);
  3960. /* set transcoder timing, panel must allow it */
  3961. assert_panel_unlocked(dev_priv, pipe);
  3962. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3963. intel_fdi_normal_train(crtc);
  3964. /* For PCH DP, enable TRANS_DP_CTL */
  3965. if (HAS_PCH_CPT(dev_priv) &&
  3966. intel_crtc_has_dp_encoder(crtc_state)) {
  3967. const struct drm_display_mode *adjusted_mode =
  3968. &crtc_state->base.adjusted_mode;
  3969. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3970. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3971. temp = I915_READ(reg);
  3972. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3973. TRANS_DP_SYNC_MASK |
  3974. TRANS_DP_BPC_MASK);
  3975. temp |= TRANS_DP_OUTPUT_ENABLE;
  3976. temp |= bpc << 9; /* same format but at 11:9 */
  3977. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3978. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3979. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3980. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3981. switch (intel_trans_dp_port_sel(crtc)) {
  3982. case PORT_B:
  3983. temp |= TRANS_DP_PORT_SEL_B;
  3984. break;
  3985. case PORT_C:
  3986. temp |= TRANS_DP_PORT_SEL_C;
  3987. break;
  3988. case PORT_D:
  3989. temp |= TRANS_DP_PORT_SEL_D;
  3990. break;
  3991. default:
  3992. BUG();
  3993. }
  3994. I915_WRITE(reg, temp);
  3995. }
  3996. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3997. }
  3998. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3999. {
  4000. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4001. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4002. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  4003. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4004. lpt_program_iclkip(crtc);
  4005. /* Set transcoder timing. */
  4006. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4007. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4008. }
  4009. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4010. {
  4011. struct drm_i915_private *dev_priv = to_i915(dev);
  4012. i915_reg_t dslreg = PIPEDSL(pipe);
  4013. u32 temp;
  4014. temp = I915_READ(dslreg);
  4015. udelay(500);
  4016. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4017. if (wait_for(I915_READ(dslreg) != temp, 5))
  4018. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4019. }
  4020. }
  4021. static int
  4022. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4023. unsigned int scaler_user, int *scaler_id,
  4024. int src_w, int src_h, int dst_w, int dst_h)
  4025. {
  4026. struct intel_crtc_scaler_state *scaler_state =
  4027. &crtc_state->scaler_state;
  4028. struct intel_crtc *intel_crtc =
  4029. to_intel_crtc(crtc_state->base.crtc);
  4030. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4031. const struct drm_display_mode *adjusted_mode =
  4032. &crtc_state->base.adjusted_mode;
  4033. int need_scaling;
  4034. /*
  4035. * Src coordinates are already rotated by 270 degrees for
  4036. * the 90/270 degree plane rotation cases (to match the
  4037. * GTT mapping), hence no need to account for rotation here.
  4038. */
  4039. need_scaling = src_w != dst_w || src_h != dst_h;
  4040. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4041. need_scaling = true;
  4042. /*
  4043. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4044. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4045. * Once NV12 is enabled, handle it here while allocating scaler
  4046. * for NV12.
  4047. */
  4048. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4049. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4050. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4051. return -EINVAL;
  4052. }
  4053. /*
  4054. * if plane is being disabled or scaler is no more required or force detach
  4055. * - free scaler binded to this plane/crtc
  4056. * - in order to do this, update crtc->scaler_usage
  4057. *
  4058. * Here scaler state in crtc_state is set free so that
  4059. * scaler can be assigned to other user. Actual register
  4060. * update to free the scaler is done in plane/panel-fit programming.
  4061. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4062. */
  4063. if (force_detach || !need_scaling) {
  4064. if (*scaler_id >= 0) {
  4065. scaler_state->scaler_users &= ~(1 << scaler_user);
  4066. scaler_state->scalers[*scaler_id].in_use = 0;
  4067. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4068. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4069. intel_crtc->pipe, scaler_user, *scaler_id,
  4070. scaler_state->scaler_users);
  4071. *scaler_id = -1;
  4072. }
  4073. return 0;
  4074. }
  4075. /* range checks */
  4076. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4077. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4078. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4079. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4080. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4081. "size is out of scaler range\n",
  4082. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4083. return -EINVAL;
  4084. }
  4085. /* mark this plane as a scaler user in crtc_state */
  4086. scaler_state->scaler_users |= (1 << scaler_user);
  4087. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4088. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4089. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4090. scaler_state->scaler_users);
  4091. return 0;
  4092. }
  4093. /**
  4094. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4095. *
  4096. * @state: crtc's scaler state
  4097. *
  4098. * Return
  4099. * 0 - scaler_usage updated successfully
  4100. * error - requested scaling cannot be supported or other error condition
  4101. */
  4102. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4103. {
  4104. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4105. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4106. &state->scaler_state.scaler_id,
  4107. state->pipe_src_w, state->pipe_src_h,
  4108. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4109. }
  4110. /**
  4111. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4112. *
  4113. * @state: crtc's scaler state
  4114. * @plane_state: atomic plane state to update
  4115. *
  4116. * Return
  4117. * 0 - scaler_usage updated successfully
  4118. * error - requested scaling cannot be supported or other error condition
  4119. */
  4120. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4121. struct intel_plane_state *plane_state)
  4122. {
  4123. struct intel_plane *intel_plane =
  4124. to_intel_plane(plane_state->base.plane);
  4125. struct drm_framebuffer *fb = plane_state->base.fb;
  4126. int ret;
  4127. bool force_detach = !fb || !plane_state->base.visible;
  4128. ret = skl_update_scaler(crtc_state, force_detach,
  4129. drm_plane_index(&intel_plane->base),
  4130. &plane_state->scaler_id,
  4131. drm_rect_width(&plane_state->base.src) >> 16,
  4132. drm_rect_height(&plane_state->base.src) >> 16,
  4133. drm_rect_width(&plane_state->base.dst),
  4134. drm_rect_height(&plane_state->base.dst));
  4135. if (ret || plane_state->scaler_id < 0)
  4136. return ret;
  4137. /* check colorkey */
  4138. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4139. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4140. intel_plane->base.base.id,
  4141. intel_plane->base.name);
  4142. return -EINVAL;
  4143. }
  4144. /* Check src format */
  4145. switch (fb->format->format) {
  4146. case DRM_FORMAT_RGB565:
  4147. case DRM_FORMAT_XBGR8888:
  4148. case DRM_FORMAT_XRGB8888:
  4149. case DRM_FORMAT_ABGR8888:
  4150. case DRM_FORMAT_ARGB8888:
  4151. case DRM_FORMAT_XRGB2101010:
  4152. case DRM_FORMAT_XBGR2101010:
  4153. case DRM_FORMAT_YUYV:
  4154. case DRM_FORMAT_YVYU:
  4155. case DRM_FORMAT_UYVY:
  4156. case DRM_FORMAT_VYUY:
  4157. break;
  4158. default:
  4159. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4160. intel_plane->base.base.id, intel_plane->base.name,
  4161. fb->base.id, fb->format->format);
  4162. return -EINVAL;
  4163. }
  4164. return 0;
  4165. }
  4166. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4167. {
  4168. int i;
  4169. for (i = 0; i < crtc->num_scalers; i++)
  4170. skl_detach_scaler(crtc, i);
  4171. }
  4172. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4173. {
  4174. struct drm_device *dev = crtc->base.dev;
  4175. struct drm_i915_private *dev_priv = to_i915(dev);
  4176. int pipe = crtc->pipe;
  4177. struct intel_crtc_scaler_state *scaler_state =
  4178. &crtc->config->scaler_state;
  4179. if (crtc->config->pch_pfit.enabled) {
  4180. int id;
  4181. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4182. return;
  4183. id = scaler_state->scaler_id;
  4184. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4185. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4186. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4187. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4188. }
  4189. }
  4190. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4191. {
  4192. struct drm_device *dev = crtc->base.dev;
  4193. struct drm_i915_private *dev_priv = to_i915(dev);
  4194. int pipe = crtc->pipe;
  4195. if (crtc->config->pch_pfit.enabled) {
  4196. /* Force use of hard-coded filter coefficients
  4197. * as some pre-programmed values are broken,
  4198. * e.g. x201.
  4199. */
  4200. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4201. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4202. PF_PIPE_SEL_IVB(pipe));
  4203. else
  4204. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4205. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4206. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4207. }
  4208. }
  4209. void hsw_enable_ips(struct intel_crtc *crtc)
  4210. {
  4211. struct drm_device *dev = crtc->base.dev;
  4212. struct drm_i915_private *dev_priv = to_i915(dev);
  4213. if (!crtc->config->ips_enabled)
  4214. return;
  4215. /*
  4216. * We can only enable IPS after we enable a plane and wait for a vblank
  4217. * This function is called from post_plane_update, which is run after
  4218. * a vblank wait.
  4219. */
  4220. assert_plane_enabled(dev_priv, crtc->plane);
  4221. if (IS_BROADWELL(dev_priv)) {
  4222. mutex_lock(&dev_priv->rps.hw_lock);
  4223. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4224. mutex_unlock(&dev_priv->rps.hw_lock);
  4225. /* Quoting Art Runyan: "its not safe to expect any particular
  4226. * value in IPS_CTL bit 31 after enabling IPS through the
  4227. * mailbox." Moreover, the mailbox may return a bogus state,
  4228. * so we need to just enable it and continue on.
  4229. */
  4230. } else {
  4231. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4232. /* The bit only becomes 1 in the next vblank, so this wait here
  4233. * is essentially intel_wait_for_vblank. If we don't have this
  4234. * and don't wait for vblanks until the end of crtc_enable, then
  4235. * the HW state readout code will complain that the expected
  4236. * IPS_CTL value is not the one we read. */
  4237. if (intel_wait_for_register(dev_priv,
  4238. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4239. 50))
  4240. DRM_ERROR("Timed out waiting for IPS enable\n");
  4241. }
  4242. }
  4243. void hsw_disable_ips(struct intel_crtc *crtc)
  4244. {
  4245. struct drm_device *dev = crtc->base.dev;
  4246. struct drm_i915_private *dev_priv = to_i915(dev);
  4247. if (!crtc->config->ips_enabled)
  4248. return;
  4249. assert_plane_enabled(dev_priv, crtc->plane);
  4250. if (IS_BROADWELL(dev_priv)) {
  4251. mutex_lock(&dev_priv->rps.hw_lock);
  4252. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4253. mutex_unlock(&dev_priv->rps.hw_lock);
  4254. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4255. if (intel_wait_for_register(dev_priv,
  4256. IPS_CTL, IPS_ENABLE, 0,
  4257. 42))
  4258. DRM_ERROR("Timed out waiting for IPS disable\n");
  4259. } else {
  4260. I915_WRITE(IPS_CTL, 0);
  4261. POSTING_READ(IPS_CTL);
  4262. }
  4263. /* We need to wait for a vblank before we can disable the plane. */
  4264. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4265. }
  4266. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4267. {
  4268. if (intel_crtc->overlay) {
  4269. struct drm_device *dev = intel_crtc->base.dev;
  4270. mutex_lock(&dev->struct_mutex);
  4271. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4272. mutex_unlock(&dev->struct_mutex);
  4273. }
  4274. /* Let userspace switch the overlay on again. In most cases userspace
  4275. * has to recompute where to put it anyway.
  4276. */
  4277. }
  4278. /**
  4279. * intel_post_enable_primary - Perform operations after enabling primary plane
  4280. * @crtc: the CRTC whose primary plane was just enabled
  4281. *
  4282. * Performs potentially sleeping operations that must be done after the primary
  4283. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4284. * called due to an explicit primary plane update, or due to an implicit
  4285. * re-enable that is caused when a sprite plane is updated to no longer
  4286. * completely hide the primary plane.
  4287. */
  4288. static void
  4289. intel_post_enable_primary(struct drm_crtc *crtc)
  4290. {
  4291. struct drm_device *dev = crtc->dev;
  4292. struct drm_i915_private *dev_priv = to_i915(dev);
  4293. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4294. int pipe = intel_crtc->pipe;
  4295. /*
  4296. * FIXME IPS should be fine as long as one plane is
  4297. * enabled, but in practice it seems to have problems
  4298. * when going from primary only to sprite only and vice
  4299. * versa.
  4300. */
  4301. hsw_enable_ips(intel_crtc);
  4302. /*
  4303. * Gen2 reports pipe underruns whenever all planes are disabled.
  4304. * So don't enable underrun reporting before at least some planes
  4305. * are enabled.
  4306. * FIXME: Need to fix the logic to work when we turn off all planes
  4307. * but leave the pipe running.
  4308. */
  4309. if (IS_GEN2(dev_priv))
  4310. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4311. /* Underruns don't always raise interrupts, so check manually. */
  4312. intel_check_cpu_fifo_underruns(dev_priv);
  4313. intel_check_pch_fifo_underruns(dev_priv);
  4314. }
  4315. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4316. static void
  4317. intel_pre_disable_primary(struct drm_crtc *crtc)
  4318. {
  4319. struct drm_device *dev = crtc->dev;
  4320. struct drm_i915_private *dev_priv = to_i915(dev);
  4321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4322. int pipe = intel_crtc->pipe;
  4323. /*
  4324. * Gen2 reports pipe underruns whenever all planes are disabled.
  4325. * So diasble underrun reporting before all the planes get disabled.
  4326. * FIXME: Need to fix the logic to work when we turn off all planes
  4327. * but leave the pipe running.
  4328. */
  4329. if (IS_GEN2(dev_priv))
  4330. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4331. /*
  4332. * FIXME IPS should be fine as long as one plane is
  4333. * enabled, but in practice it seems to have problems
  4334. * when going from primary only to sprite only and vice
  4335. * versa.
  4336. */
  4337. hsw_disable_ips(intel_crtc);
  4338. }
  4339. /* FIXME get rid of this and use pre_plane_update */
  4340. static void
  4341. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4342. {
  4343. struct drm_device *dev = crtc->dev;
  4344. struct drm_i915_private *dev_priv = to_i915(dev);
  4345. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4346. int pipe = intel_crtc->pipe;
  4347. intel_pre_disable_primary(crtc);
  4348. /*
  4349. * Vblank time updates from the shadow to live plane control register
  4350. * are blocked if the memory self-refresh mode is active at that
  4351. * moment. So to make sure the plane gets truly disabled, disable
  4352. * first the self-refresh mode. The self-refresh enable bit in turn
  4353. * will be checked/applied by the HW only at the next frame start
  4354. * event which is after the vblank start event, so we need to have a
  4355. * wait-for-vblank between disabling the plane and the pipe.
  4356. */
  4357. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4358. intel_set_memory_cxsr(dev_priv, false))
  4359. intel_wait_for_vblank(dev_priv, pipe);
  4360. }
  4361. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4362. {
  4363. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4364. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4365. struct intel_crtc_state *pipe_config =
  4366. to_intel_crtc_state(crtc->base.state);
  4367. struct drm_plane *primary = crtc->base.primary;
  4368. struct drm_plane_state *old_pri_state =
  4369. drm_atomic_get_existing_plane_state(old_state, primary);
  4370. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4371. if (pipe_config->update_wm_post && pipe_config->base.active)
  4372. intel_update_watermarks(crtc);
  4373. if (old_pri_state) {
  4374. struct intel_plane_state *primary_state =
  4375. to_intel_plane_state(primary->state);
  4376. struct intel_plane_state *old_primary_state =
  4377. to_intel_plane_state(old_pri_state);
  4378. intel_fbc_post_update(crtc);
  4379. if (primary_state->base.visible &&
  4380. (needs_modeset(&pipe_config->base) ||
  4381. !old_primary_state->base.visible))
  4382. intel_post_enable_primary(&crtc->base);
  4383. }
  4384. }
  4385. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4386. struct intel_crtc_state *pipe_config)
  4387. {
  4388. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4389. struct drm_device *dev = crtc->base.dev;
  4390. struct drm_i915_private *dev_priv = to_i915(dev);
  4391. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4392. struct drm_plane *primary = crtc->base.primary;
  4393. struct drm_plane_state *old_pri_state =
  4394. drm_atomic_get_existing_plane_state(old_state, primary);
  4395. bool modeset = needs_modeset(&pipe_config->base);
  4396. struct intel_atomic_state *old_intel_state =
  4397. to_intel_atomic_state(old_state);
  4398. if (old_pri_state) {
  4399. struct intel_plane_state *primary_state =
  4400. to_intel_plane_state(primary->state);
  4401. struct intel_plane_state *old_primary_state =
  4402. to_intel_plane_state(old_pri_state);
  4403. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4404. if (old_primary_state->base.visible &&
  4405. (modeset || !primary_state->base.visible))
  4406. intel_pre_disable_primary(&crtc->base);
  4407. }
  4408. /*
  4409. * Vblank time updates from the shadow to live plane control register
  4410. * are blocked if the memory self-refresh mode is active at that
  4411. * moment. So to make sure the plane gets truly disabled, disable
  4412. * first the self-refresh mode. The self-refresh enable bit in turn
  4413. * will be checked/applied by the HW only at the next frame start
  4414. * event which is after the vblank start event, so we need to have a
  4415. * wait-for-vblank between disabling the plane and the pipe.
  4416. */
  4417. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4418. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4419. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4420. /*
  4421. * IVB workaround: must disable low power watermarks for at least
  4422. * one frame before enabling scaling. LP watermarks can be re-enabled
  4423. * when scaling is disabled.
  4424. *
  4425. * WaCxSRDisabledForSpriteScaling:ivb
  4426. */
  4427. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4428. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4429. /*
  4430. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4431. * watermark programming here.
  4432. */
  4433. if (needs_modeset(&pipe_config->base))
  4434. return;
  4435. /*
  4436. * For platforms that support atomic watermarks, program the
  4437. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4438. * will be the intermediate values that are safe for both pre- and
  4439. * post- vblank; when vblank happens, the 'active' values will be set
  4440. * to the final 'target' values and we'll do this again to get the
  4441. * optimal watermarks. For gen9+ platforms, the values we program here
  4442. * will be the final target values which will get automatically latched
  4443. * at vblank time; no further programming will be necessary.
  4444. *
  4445. * If a platform hasn't been transitioned to atomic watermarks yet,
  4446. * we'll continue to update watermarks the old way, if flags tell
  4447. * us to.
  4448. */
  4449. if (dev_priv->display.initial_watermarks != NULL)
  4450. dev_priv->display.initial_watermarks(old_intel_state,
  4451. pipe_config);
  4452. else if (pipe_config->update_wm_pre)
  4453. intel_update_watermarks(crtc);
  4454. }
  4455. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4456. {
  4457. struct drm_device *dev = crtc->dev;
  4458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4459. struct drm_plane *p;
  4460. int pipe = intel_crtc->pipe;
  4461. intel_crtc_dpms_overlay_disable(intel_crtc);
  4462. drm_for_each_plane_mask(p, dev, plane_mask)
  4463. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4464. /*
  4465. * FIXME: Once we grow proper nuclear flip support out of this we need
  4466. * to compute the mask of flip planes precisely. For the time being
  4467. * consider this a flip to a NULL plane.
  4468. */
  4469. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4470. }
  4471. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4472. struct intel_crtc_state *crtc_state,
  4473. struct drm_atomic_state *old_state)
  4474. {
  4475. struct drm_connector_state *conn_state;
  4476. struct drm_connector *conn;
  4477. int i;
  4478. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4479. struct intel_encoder *encoder =
  4480. to_intel_encoder(conn_state->best_encoder);
  4481. if (conn_state->crtc != crtc)
  4482. continue;
  4483. if (encoder->pre_pll_enable)
  4484. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4485. }
  4486. }
  4487. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4488. struct intel_crtc_state *crtc_state,
  4489. struct drm_atomic_state *old_state)
  4490. {
  4491. struct drm_connector_state *conn_state;
  4492. struct drm_connector *conn;
  4493. int i;
  4494. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4495. struct intel_encoder *encoder =
  4496. to_intel_encoder(conn_state->best_encoder);
  4497. if (conn_state->crtc != crtc)
  4498. continue;
  4499. if (encoder->pre_enable)
  4500. encoder->pre_enable(encoder, crtc_state, conn_state);
  4501. }
  4502. }
  4503. static void intel_encoders_enable(struct drm_crtc *crtc,
  4504. struct intel_crtc_state *crtc_state,
  4505. struct drm_atomic_state *old_state)
  4506. {
  4507. struct drm_connector_state *conn_state;
  4508. struct drm_connector *conn;
  4509. int i;
  4510. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4511. struct intel_encoder *encoder =
  4512. to_intel_encoder(conn_state->best_encoder);
  4513. if (conn_state->crtc != crtc)
  4514. continue;
  4515. encoder->enable(encoder, crtc_state, conn_state);
  4516. intel_opregion_notify_encoder(encoder, true);
  4517. }
  4518. }
  4519. static void intel_encoders_disable(struct drm_crtc *crtc,
  4520. struct intel_crtc_state *old_crtc_state,
  4521. struct drm_atomic_state *old_state)
  4522. {
  4523. struct drm_connector_state *old_conn_state;
  4524. struct drm_connector *conn;
  4525. int i;
  4526. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4527. struct intel_encoder *encoder =
  4528. to_intel_encoder(old_conn_state->best_encoder);
  4529. if (old_conn_state->crtc != crtc)
  4530. continue;
  4531. intel_opregion_notify_encoder(encoder, false);
  4532. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4533. }
  4534. }
  4535. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4536. struct intel_crtc_state *old_crtc_state,
  4537. struct drm_atomic_state *old_state)
  4538. {
  4539. struct drm_connector_state *old_conn_state;
  4540. struct drm_connector *conn;
  4541. int i;
  4542. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4543. struct intel_encoder *encoder =
  4544. to_intel_encoder(old_conn_state->best_encoder);
  4545. if (old_conn_state->crtc != crtc)
  4546. continue;
  4547. if (encoder->post_disable)
  4548. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4549. }
  4550. }
  4551. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4552. struct intel_crtc_state *old_crtc_state,
  4553. struct drm_atomic_state *old_state)
  4554. {
  4555. struct drm_connector_state *old_conn_state;
  4556. struct drm_connector *conn;
  4557. int i;
  4558. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4559. struct intel_encoder *encoder =
  4560. to_intel_encoder(old_conn_state->best_encoder);
  4561. if (old_conn_state->crtc != crtc)
  4562. continue;
  4563. if (encoder->post_pll_disable)
  4564. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4565. }
  4566. }
  4567. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4568. struct drm_atomic_state *old_state)
  4569. {
  4570. struct drm_crtc *crtc = pipe_config->base.crtc;
  4571. struct drm_device *dev = crtc->dev;
  4572. struct drm_i915_private *dev_priv = to_i915(dev);
  4573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4574. int pipe = intel_crtc->pipe;
  4575. struct intel_atomic_state *old_intel_state =
  4576. to_intel_atomic_state(old_state);
  4577. if (WARN_ON(intel_crtc->active))
  4578. return;
  4579. /*
  4580. * Sometimes spurious CPU pipe underruns happen during FDI
  4581. * training, at least with VGA+HDMI cloning. Suppress them.
  4582. *
  4583. * On ILK we get an occasional spurious CPU pipe underruns
  4584. * between eDP port A enable and vdd enable. Also PCH port
  4585. * enable seems to result in the occasional CPU pipe underrun.
  4586. *
  4587. * Spurious PCH underruns also occur during PCH enabling.
  4588. */
  4589. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4590. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4591. if (intel_crtc->config->has_pch_encoder)
  4592. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4593. if (intel_crtc->config->has_pch_encoder)
  4594. intel_prepare_shared_dpll(intel_crtc);
  4595. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4596. intel_dp_set_m_n(intel_crtc, M1_N1);
  4597. intel_set_pipe_timings(intel_crtc);
  4598. intel_set_pipe_src_size(intel_crtc);
  4599. if (intel_crtc->config->has_pch_encoder) {
  4600. intel_cpu_transcoder_set_m_n(intel_crtc,
  4601. &intel_crtc->config->fdi_m_n, NULL);
  4602. }
  4603. ironlake_set_pipeconf(crtc);
  4604. intel_crtc->active = true;
  4605. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4606. if (intel_crtc->config->has_pch_encoder) {
  4607. /* Note: FDI PLL enabling _must_ be done before we enable the
  4608. * cpu pipes, hence this is separate from all the other fdi/pch
  4609. * enabling. */
  4610. ironlake_fdi_pll_enable(intel_crtc);
  4611. } else {
  4612. assert_fdi_tx_disabled(dev_priv, pipe);
  4613. assert_fdi_rx_disabled(dev_priv, pipe);
  4614. }
  4615. ironlake_pfit_enable(intel_crtc);
  4616. /*
  4617. * On ILK+ LUT must be loaded before the pipe is running but with
  4618. * clocks enabled
  4619. */
  4620. intel_color_load_luts(&pipe_config->base);
  4621. if (dev_priv->display.initial_watermarks != NULL)
  4622. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4623. intel_enable_pipe(intel_crtc);
  4624. if (intel_crtc->config->has_pch_encoder)
  4625. ironlake_pch_enable(pipe_config);
  4626. assert_vblank_disabled(crtc);
  4627. drm_crtc_vblank_on(crtc);
  4628. intel_encoders_enable(crtc, pipe_config, old_state);
  4629. if (HAS_PCH_CPT(dev_priv))
  4630. cpt_verify_modeset(dev, intel_crtc->pipe);
  4631. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4632. if (intel_crtc->config->has_pch_encoder)
  4633. intel_wait_for_vblank(dev_priv, pipe);
  4634. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4635. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4636. }
  4637. /* IPS only exists on ULT machines and is tied to pipe A. */
  4638. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4639. {
  4640. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4641. }
  4642. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4643. struct drm_atomic_state *old_state)
  4644. {
  4645. struct drm_crtc *crtc = pipe_config->base.crtc;
  4646. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4647. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4648. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4649. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4650. struct intel_atomic_state *old_intel_state =
  4651. to_intel_atomic_state(old_state);
  4652. if (WARN_ON(intel_crtc->active))
  4653. return;
  4654. if (intel_crtc->config->has_pch_encoder)
  4655. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4656. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4657. if (intel_crtc->config->shared_dpll)
  4658. intel_enable_shared_dpll(intel_crtc);
  4659. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4660. intel_dp_set_m_n(intel_crtc, M1_N1);
  4661. if (!transcoder_is_dsi(cpu_transcoder))
  4662. intel_set_pipe_timings(intel_crtc);
  4663. intel_set_pipe_src_size(intel_crtc);
  4664. if (cpu_transcoder != TRANSCODER_EDP &&
  4665. !transcoder_is_dsi(cpu_transcoder)) {
  4666. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4667. intel_crtc->config->pixel_multiplier - 1);
  4668. }
  4669. if (intel_crtc->config->has_pch_encoder) {
  4670. intel_cpu_transcoder_set_m_n(intel_crtc,
  4671. &intel_crtc->config->fdi_m_n, NULL);
  4672. }
  4673. if (!transcoder_is_dsi(cpu_transcoder))
  4674. haswell_set_pipeconf(crtc);
  4675. haswell_set_pipemisc(crtc);
  4676. intel_color_set_csc(&pipe_config->base);
  4677. intel_crtc->active = true;
  4678. if (intel_crtc->config->has_pch_encoder)
  4679. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4680. else
  4681. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4682. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4683. if (intel_crtc->config->has_pch_encoder)
  4684. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4685. if (!transcoder_is_dsi(cpu_transcoder))
  4686. intel_ddi_enable_pipe_clock(pipe_config);
  4687. if (INTEL_GEN(dev_priv) >= 9)
  4688. skylake_pfit_enable(intel_crtc);
  4689. else
  4690. ironlake_pfit_enable(intel_crtc);
  4691. /*
  4692. * On ILK+ LUT must be loaded before the pipe is running but with
  4693. * clocks enabled
  4694. */
  4695. intel_color_load_luts(&pipe_config->base);
  4696. intel_ddi_set_pipe_settings(pipe_config);
  4697. if (!transcoder_is_dsi(cpu_transcoder))
  4698. intel_ddi_enable_transcoder_func(pipe_config);
  4699. if (dev_priv->display.initial_watermarks != NULL)
  4700. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4701. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4702. if (!transcoder_is_dsi(cpu_transcoder))
  4703. intel_enable_pipe(intel_crtc);
  4704. if (intel_crtc->config->has_pch_encoder)
  4705. lpt_pch_enable(pipe_config);
  4706. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4707. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4708. assert_vblank_disabled(crtc);
  4709. drm_crtc_vblank_on(crtc);
  4710. intel_encoders_enable(crtc, pipe_config, old_state);
  4711. if (intel_crtc->config->has_pch_encoder) {
  4712. intel_wait_for_vblank(dev_priv, pipe);
  4713. intel_wait_for_vblank(dev_priv, pipe);
  4714. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4715. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4716. }
  4717. /* If we change the relative order between pipe/planes enabling, we need
  4718. * to change the workaround. */
  4719. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4720. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4721. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4722. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4723. }
  4724. }
  4725. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4726. {
  4727. struct drm_device *dev = crtc->base.dev;
  4728. struct drm_i915_private *dev_priv = to_i915(dev);
  4729. int pipe = crtc->pipe;
  4730. /* To avoid upsetting the power well on haswell only disable the pfit if
  4731. * it's in use. The hw state code will make sure we get this right. */
  4732. if (force || crtc->config->pch_pfit.enabled) {
  4733. I915_WRITE(PF_CTL(pipe), 0);
  4734. I915_WRITE(PF_WIN_POS(pipe), 0);
  4735. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4736. }
  4737. }
  4738. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4739. struct drm_atomic_state *old_state)
  4740. {
  4741. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4742. struct drm_device *dev = crtc->dev;
  4743. struct drm_i915_private *dev_priv = to_i915(dev);
  4744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4745. int pipe = intel_crtc->pipe;
  4746. /*
  4747. * Sometimes spurious CPU pipe underruns happen when the
  4748. * pipe is already disabled, but FDI RX/TX is still enabled.
  4749. * Happens at least with VGA+HDMI cloning. Suppress them.
  4750. */
  4751. if (intel_crtc->config->has_pch_encoder) {
  4752. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4753. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4754. }
  4755. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4756. drm_crtc_vblank_off(crtc);
  4757. assert_vblank_disabled(crtc);
  4758. intel_disable_pipe(intel_crtc);
  4759. ironlake_pfit_disable(intel_crtc, false);
  4760. if (intel_crtc->config->has_pch_encoder)
  4761. ironlake_fdi_disable(crtc);
  4762. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4763. if (intel_crtc->config->has_pch_encoder) {
  4764. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4765. if (HAS_PCH_CPT(dev_priv)) {
  4766. i915_reg_t reg;
  4767. u32 temp;
  4768. /* disable TRANS_DP_CTL */
  4769. reg = TRANS_DP_CTL(pipe);
  4770. temp = I915_READ(reg);
  4771. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4772. TRANS_DP_PORT_SEL_MASK);
  4773. temp |= TRANS_DP_PORT_SEL_NONE;
  4774. I915_WRITE(reg, temp);
  4775. /* disable DPLL_SEL */
  4776. temp = I915_READ(PCH_DPLL_SEL);
  4777. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4778. I915_WRITE(PCH_DPLL_SEL, temp);
  4779. }
  4780. ironlake_fdi_pll_disable(intel_crtc);
  4781. }
  4782. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4783. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4784. }
  4785. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4786. struct drm_atomic_state *old_state)
  4787. {
  4788. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4789. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4790. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4791. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4792. if (intel_crtc->config->has_pch_encoder)
  4793. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  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(intel_crtc);
  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. if (old_crtc_state->has_pch_encoder)
  4812. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4813. }
  4814. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4815. {
  4816. struct drm_device *dev = crtc->base.dev;
  4817. struct drm_i915_private *dev_priv = to_i915(dev);
  4818. struct intel_crtc_state *pipe_config = crtc->config;
  4819. if (!pipe_config->gmch_pfit.control)
  4820. return;
  4821. /*
  4822. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4823. * according to register description and PRM.
  4824. */
  4825. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4826. assert_pipe_disabled(dev_priv, crtc->pipe);
  4827. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4828. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4829. /* Border color in case we don't scale up to the full screen. Black by
  4830. * default, change to something else for debugging. */
  4831. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4832. }
  4833. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4834. {
  4835. switch (port) {
  4836. case PORT_A:
  4837. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4838. case PORT_B:
  4839. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4840. case PORT_C:
  4841. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4842. case PORT_D:
  4843. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4844. case PORT_E:
  4845. return POWER_DOMAIN_PORT_DDI_E_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(POWER_DOMAIN_PIPE(pipe));
  4864. mask |= BIT(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(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(intel_crtc);
  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);
  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(intel_crtc);
  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(intel_crtc);
  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. u64 domains;
  5041. struct drm_atomic_state *state;
  5042. struct intel_crtc_state *crtc_state;
  5043. int ret;
  5044. if (!intel_crtc->active)
  5045. return;
  5046. if (crtc->primary->state->visible) {
  5047. intel_pre_disable_primary_noatomic(crtc);
  5048. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5049. crtc->primary->state->visible = false;
  5050. }
  5051. state = drm_atomic_state_alloc(crtc->dev);
  5052. if (!state) {
  5053. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5054. crtc->base.id, crtc->name);
  5055. return;
  5056. }
  5057. state->acquire_ctx = ctx;
  5058. /* Everything's already locked, -EDEADLK can't happen. */
  5059. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5060. ret = drm_atomic_add_affected_connectors(state, crtc);
  5061. WARN_ON(IS_ERR(crtc_state) || ret);
  5062. dev_priv->display.crtc_disable(crtc_state, state);
  5063. drm_atomic_state_put(state);
  5064. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5065. crtc->base.id, crtc->name);
  5066. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5067. crtc->state->active = false;
  5068. intel_crtc->active = false;
  5069. crtc->enabled = false;
  5070. crtc->state->connector_mask = 0;
  5071. crtc->state->encoder_mask = 0;
  5072. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5073. encoder->base.crtc = NULL;
  5074. intel_fbc_disable(intel_crtc);
  5075. intel_update_watermarks(intel_crtc);
  5076. intel_disable_shared_dpll(intel_crtc);
  5077. domains = intel_crtc->enabled_power_domains;
  5078. for_each_power_domain(domain, domains)
  5079. intel_display_power_put(dev_priv, domain);
  5080. intel_crtc->enabled_power_domains = 0;
  5081. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5082. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5083. }
  5084. /*
  5085. * turn all crtc's off, but do not adjust state
  5086. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5087. */
  5088. int intel_display_suspend(struct drm_device *dev)
  5089. {
  5090. struct drm_i915_private *dev_priv = to_i915(dev);
  5091. struct drm_atomic_state *state;
  5092. int ret;
  5093. state = drm_atomic_helper_suspend(dev);
  5094. ret = PTR_ERR_OR_ZERO(state);
  5095. if (ret)
  5096. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5097. else
  5098. dev_priv->modeset_restore_state = state;
  5099. return ret;
  5100. }
  5101. void intel_encoder_destroy(struct drm_encoder *encoder)
  5102. {
  5103. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5104. drm_encoder_cleanup(encoder);
  5105. kfree(intel_encoder);
  5106. }
  5107. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5108. * internal consistency). */
  5109. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5110. struct drm_connector_state *conn_state)
  5111. {
  5112. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5113. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5114. connector->base.base.id,
  5115. connector->base.name);
  5116. if (connector->get_hw_state(connector)) {
  5117. struct intel_encoder *encoder = connector->encoder;
  5118. I915_STATE_WARN(!crtc_state,
  5119. "connector enabled without attached crtc\n");
  5120. if (!crtc_state)
  5121. return;
  5122. I915_STATE_WARN(!crtc_state->active,
  5123. "connector is active, but attached crtc isn't\n");
  5124. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5125. return;
  5126. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5127. "atomic encoder doesn't match attached encoder\n");
  5128. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5129. "attached encoder crtc differs from connector crtc\n");
  5130. } else {
  5131. I915_STATE_WARN(crtc_state && crtc_state->active,
  5132. "attached crtc is active, but connector isn't\n");
  5133. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5134. "best encoder set without crtc!\n");
  5135. }
  5136. }
  5137. int intel_connector_init(struct intel_connector *connector)
  5138. {
  5139. struct intel_digital_connector_state *conn_state;
  5140. /*
  5141. * Allocate enough memory to hold intel_digital_connector_state,
  5142. * This might be a few bytes too many, but for connectors that don't
  5143. * need it we'll free the state and allocate a smaller one on the first
  5144. * succesful commit anyway.
  5145. */
  5146. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5147. if (!conn_state)
  5148. return -ENOMEM;
  5149. __drm_atomic_helper_connector_reset(&connector->base,
  5150. &conn_state->base);
  5151. return 0;
  5152. }
  5153. struct intel_connector *intel_connector_alloc(void)
  5154. {
  5155. struct intel_connector *connector;
  5156. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5157. if (!connector)
  5158. return NULL;
  5159. if (intel_connector_init(connector) < 0) {
  5160. kfree(connector);
  5161. return NULL;
  5162. }
  5163. return connector;
  5164. }
  5165. /* Simple connector->get_hw_state implementation for encoders that support only
  5166. * one connector and no cloning and hence the encoder state determines the state
  5167. * of the connector. */
  5168. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5169. {
  5170. enum pipe pipe = 0;
  5171. struct intel_encoder *encoder = connector->encoder;
  5172. return encoder->get_hw_state(encoder, &pipe);
  5173. }
  5174. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5175. {
  5176. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5177. return crtc_state->fdi_lanes;
  5178. return 0;
  5179. }
  5180. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5181. struct intel_crtc_state *pipe_config)
  5182. {
  5183. struct drm_i915_private *dev_priv = to_i915(dev);
  5184. struct drm_atomic_state *state = pipe_config->base.state;
  5185. struct intel_crtc *other_crtc;
  5186. struct intel_crtc_state *other_crtc_state;
  5187. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5188. pipe_name(pipe), pipe_config->fdi_lanes);
  5189. if (pipe_config->fdi_lanes > 4) {
  5190. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5191. pipe_name(pipe), pipe_config->fdi_lanes);
  5192. return -EINVAL;
  5193. }
  5194. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5195. if (pipe_config->fdi_lanes > 2) {
  5196. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5197. pipe_config->fdi_lanes);
  5198. return -EINVAL;
  5199. } else {
  5200. return 0;
  5201. }
  5202. }
  5203. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5204. return 0;
  5205. /* Ivybridge 3 pipe is really complicated */
  5206. switch (pipe) {
  5207. case PIPE_A:
  5208. return 0;
  5209. case PIPE_B:
  5210. if (pipe_config->fdi_lanes <= 2)
  5211. return 0;
  5212. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5213. other_crtc_state =
  5214. intel_atomic_get_crtc_state(state, other_crtc);
  5215. if (IS_ERR(other_crtc_state))
  5216. return PTR_ERR(other_crtc_state);
  5217. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5218. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5219. pipe_name(pipe), pipe_config->fdi_lanes);
  5220. return -EINVAL;
  5221. }
  5222. return 0;
  5223. case PIPE_C:
  5224. if (pipe_config->fdi_lanes > 2) {
  5225. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5226. pipe_name(pipe), pipe_config->fdi_lanes);
  5227. return -EINVAL;
  5228. }
  5229. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5230. other_crtc_state =
  5231. intel_atomic_get_crtc_state(state, other_crtc);
  5232. if (IS_ERR(other_crtc_state))
  5233. return PTR_ERR(other_crtc_state);
  5234. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5235. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5236. return -EINVAL;
  5237. }
  5238. return 0;
  5239. default:
  5240. BUG();
  5241. }
  5242. }
  5243. #define RETRY 1
  5244. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5245. struct intel_crtc_state *pipe_config)
  5246. {
  5247. struct drm_device *dev = intel_crtc->base.dev;
  5248. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5249. int lane, link_bw, fdi_dotclock, ret;
  5250. bool needs_recompute = false;
  5251. retry:
  5252. /* FDI is a binary signal running at ~2.7GHz, encoding
  5253. * each output octet as 10 bits. The actual frequency
  5254. * is stored as a divider into a 100MHz clock, and the
  5255. * mode pixel clock is stored in units of 1KHz.
  5256. * Hence the bw of each lane in terms of the mode signal
  5257. * is:
  5258. */
  5259. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5260. fdi_dotclock = adjusted_mode->crtc_clock;
  5261. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5262. pipe_config->pipe_bpp);
  5263. pipe_config->fdi_lanes = lane;
  5264. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5265. link_bw, &pipe_config->fdi_m_n, false);
  5266. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5267. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5268. pipe_config->pipe_bpp -= 2*3;
  5269. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5270. pipe_config->pipe_bpp);
  5271. needs_recompute = true;
  5272. pipe_config->bw_constrained = true;
  5273. goto retry;
  5274. }
  5275. if (needs_recompute)
  5276. return RETRY;
  5277. return ret;
  5278. }
  5279. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5280. struct intel_crtc_state *pipe_config)
  5281. {
  5282. if (pipe_config->pipe_bpp > 24)
  5283. return false;
  5284. /* HSW can handle pixel rate up to cdclk? */
  5285. if (IS_HASWELL(dev_priv))
  5286. return true;
  5287. /*
  5288. * We compare against max which means we must take
  5289. * the increased cdclk requirement into account when
  5290. * calculating the new cdclk.
  5291. *
  5292. * Should measure whether using a lower cdclk w/o IPS
  5293. */
  5294. return pipe_config->pixel_rate <=
  5295. dev_priv->max_cdclk_freq * 95 / 100;
  5296. }
  5297. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5298. struct intel_crtc_state *pipe_config)
  5299. {
  5300. struct drm_device *dev = crtc->base.dev;
  5301. struct drm_i915_private *dev_priv = to_i915(dev);
  5302. pipe_config->ips_enabled = i915.enable_ips &&
  5303. hsw_crtc_supports_ips(crtc) &&
  5304. pipe_config_supports_ips(dev_priv, pipe_config);
  5305. }
  5306. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5307. {
  5308. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5309. /* GDG double wide on either pipe, otherwise pipe A only */
  5310. return INTEL_INFO(dev_priv)->gen < 4 &&
  5311. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5312. }
  5313. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5314. {
  5315. uint32_t pixel_rate;
  5316. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5317. /*
  5318. * We only use IF-ID interlacing. If we ever use
  5319. * PF-ID we'll need to adjust the pixel_rate here.
  5320. */
  5321. if (pipe_config->pch_pfit.enabled) {
  5322. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5323. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5324. pipe_w = pipe_config->pipe_src_w;
  5325. pipe_h = pipe_config->pipe_src_h;
  5326. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5327. pfit_h = pfit_size & 0xFFFF;
  5328. if (pipe_w < pfit_w)
  5329. pipe_w = pfit_w;
  5330. if (pipe_h < pfit_h)
  5331. pipe_h = pfit_h;
  5332. if (WARN_ON(!pfit_w || !pfit_h))
  5333. return pixel_rate;
  5334. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5335. pfit_w * pfit_h);
  5336. }
  5337. return pixel_rate;
  5338. }
  5339. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5340. {
  5341. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5342. if (HAS_GMCH_DISPLAY(dev_priv))
  5343. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5344. crtc_state->pixel_rate =
  5345. crtc_state->base.adjusted_mode.crtc_clock;
  5346. else
  5347. crtc_state->pixel_rate =
  5348. ilk_pipe_pixel_rate(crtc_state);
  5349. }
  5350. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5351. struct intel_crtc_state *pipe_config)
  5352. {
  5353. struct drm_device *dev = crtc->base.dev;
  5354. struct drm_i915_private *dev_priv = to_i915(dev);
  5355. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5356. int clock_limit = dev_priv->max_dotclk_freq;
  5357. if (INTEL_GEN(dev_priv) < 4) {
  5358. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5359. /*
  5360. * Enable double wide mode when the dot clock
  5361. * is > 90% of the (display) core speed.
  5362. */
  5363. if (intel_crtc_supports_double_wide(crtc) &&
  5364. adjusted_mode->crtc_clock > clock_limit) {
  5365. clock_limit = dev_priv->max_dotclk_freq;
  5366. pipe_config->double_wide = true;
  5367. }
  5368. }
  5369. if (adjusted_mode->crtc_clock > clock_limit) {
  5370. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5371. adjusted_mode->crtc_clock, clock_limit,
  5372. yesno(pipe_config->double_wide));
  5373. return -EINVAL;
  5374. }
  5375. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5376. /*
  5377. * There is only one pipe CSC unit per pipe, and we need that
  5378. * for output conversion from RGB->YCBCR. So if CTM is already
  5379. * applied we can't support YCBCR420 output.
  5380. */
  5381. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5382. return -EINVAL;
  5383. }
  5384. /*
  5385. * Pipe horizontal size must be even in:
  5386. * - DVO ganged mode
  5387. * - LVDS dual channel mode
  5388. * - Double wide pipe
  5389. */
  5390. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5391. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5392. pipe_config->pipe_src_w &= ~1;
  5393. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5394. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5395. */
  5396. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5397. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5398. return -EINVAL;
  5399. intel_crtc_compute_pixel_rate(pipe_config);
  5400. if (HAS_IPS(dev_priv))
  5401. hsw_compute_ips_config(crtc, pipe_config);
  5402. if (pipe_config->has_pch_encoder)
  5403. return ironlake_fdi_compute_config(crtc, pipe_config);
  5404. return 0;
  5405. }
  5406. static void
  5407. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5408. {
  5409. while (*num > DATA_LINK_M_N_MASK ||
  5410. *den > DATA_LINK_M_N_MASK) {
  5411. *num >>= 1;
  5412. *den >>= 1;
  5413. }
  5414. }
  5415. static void compute_m_n(unsigned int m, unsigned int n,
  5416. uint32_t *ret_m, uint32_t *ret_n,
  5417. bool reduce_m_n)
  5418. {
  5419. /*
  5420. * Reduce M/N as much as possible without loss in precision. Several DP
  5421. * dongles in particular seem to be fussy about too large *link* M/N
  5422. * values. The passed in values are more likely to have the least
  5423. * significant bits zero than M after rounding below, so do this first.
  5424. */
  5425. if (reduce_m_n) {
  5426. while ((m & 1) == 0 && (n & 1) == 0) {
  5427. m >>= 1;
  5428. n >>= 1;
  5429. }
  5430. }
  5431. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5432. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5433. intel_reduce_m_n_ratio(ret_m, ret_n);
  5434. }
  5435. void
  5436. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5437. int pixel_clock, int link_clock,
  5438. struct intel_link_m_n *m_n,
  5439. bool reduce_m_n)
  5440. {
  5441. m_n->tu = 64;
  5442. compute_m_n(bits_per_pixel * pixel_clock,
  5443. link_clock * nlanes * 8,
  5444. &m_n->gmch_m, &m_n->gmch_n,
  5445. reduce_m_n);
  5446. compute_m_n(pixel_clock, link_clock,
  5447. &m_n->link_m, &m_n->link_n,
  5448. reduce_m_n);
  5449. }
  5450. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5451. {
  5452. if (i915.panel_use_ssc >= 0)
  5453. return i915.panel_use_ssc != 0;
  5454. return dev_priv->vbt.lvds_use_ssc
  5455. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5456. }
  5457. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5458. {
  5459. return (1 << dpll->n) << 16 | dpll->m2;
  5460. }
  5461. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5462. {
  5463. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5464. }
  5465. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5466. struct intel_crtc_state *crtc_state,
  5467. struct dpll *reduced_clock)
  5468. {
  5469. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5470. u32 fp, fp2 = 0;
  5471. if (IS_PINEVIEW(dev_priv)) {
  5472. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5473. if (reduced_clock)
  5474. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5475. } else {
  5476. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5477. if (reduced_clock)
  5478. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5479. }
  5480. crtc_state->dpll_hw_state.fp0 = fp;
  5481. crtc->lowfreq_avail = false;
  5482. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5483. reduced_clock) {
  5484. crtc_state->dpll_hw_state.fp1 = fp2;
  5485. crtc->lowfreq_avail = true;
  5486. } else {
  5487. crtc_state->dpll_hw_state.fp1 = fp;
  5488. }
  5489. }
  5490. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5491. pipe)
  5492. {
  5493. u32 reg_val;
  5494. /*
  5495. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5496. * and set it to a reasonable value instead.
  5497. */
  5498. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5499. reg_val &= 0xffffff00;
  5500. reg_val |= 0x00000030;
  5501. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5502. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5503. reg_val &= 0x00ffffff;
  5504. reg_val |= 0x8c000000;
  5505. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5506. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5507. reg_val &= 0xffffff00;
  5508. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5509. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5510. reg_val &= 0x00ffffff;
  5511. reg_val |= 0xb0000000;
  5512. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5513. }
  5514. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5515. struct intel_link_m_n *m_n)
  5516. {
  5517. struct drm_device *dev = crtc->base.dev;
  5518. struct drm_i915_private *dev_priv = to_i915(dev);
  5519. int pipe = crtc->pipe;
  5520. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5521. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5522. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5523. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5524. }
  5525. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5526. struct intel_link_m_n *m_n,
  5527. struct intel_link_m_n *m2_n2)
  5528. {
  5529. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5530. int pipe = crtc->pipe;
  5531. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5532. if (INTEL_GEN(dev_priv) >= 5) {
  5533. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5534. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5535. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5536. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5537. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5538. * for gen < 8) and if DRRS is supported (to make sure the
  5539. * registers are not unnecessarily accessed).
  5540. */
  5541. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5542. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5543. I915_WRITE(PIPE_DATA_M2(transcoder),
  5544. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5545. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5546. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5547. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5548. }
  5549. } else {
  5550. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5551. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5552. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5553. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5554. }
  5555. }
  5556. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5557. {
  5558. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5559. if (m_n == M1_N1) {
  5560. dp_m_n = &crtc->config->dp_m_n;
  5561. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5562. } else if (m_n == M2_N2) {
  5563. /*
  5564. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5565. * needs to be programmed into M1_N1.
  5566. */
  5567. dp_m_n = &crtc->config->dp_m2_n2;
  5568. } else {
  5569. DRM_ERROR("Unsupported divider value\n");
  5570. return;
  5571. }
  5572. if (crtc->config->has_pch_encoder)
  5573. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5574. else
  5575. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5576. }
  5577. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5578. struct intel_crtc_state *pipe_config)
  5579. {
  5580. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5581. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5582. if (crtc->pipe != PIPE_A)
  5583. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5584. /* DPLL not used with DSI, but still need the rest set up */
  5585. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5586. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5587. DPLL_EXT_BUFFER_ENABLE_VLV;
  5588. pipe_config->dpll_hw_state.dpll_md =
  5589. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5590. }
  5591. static void chv_compute_dpll(struct intel_crtc *crtc,
  5592. struct intel_crtc_state *pipe_config)
  5593. {
  5594. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5595. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5596. if (crtc->pipe != PIPE_A)
  5597. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5598. /* DPLL not used with DSI, but still need the rest set up */
  5599. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5600. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5601. pipe_config->dpll_hw_state.dpll_md =
  5602. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5603. }
  5604. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5605. const struct intel_crtc_state *pipe_config)
  5606. {
  5607. struct drm_device *dev = crtc->base.dev;
  5608. struct drm_i915_private *dev_priv = to_i915(dev);
  5609. enum pipe pipe = crtc->pipe;
  5610. u32 mdiv;
  5611. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5612. u32 coreclk, reg_val;
  5613. /* Enable Refclk */
  5614. I915_WRITE(DPLL(pipe),
  5615. pipe_config->dpll_hw_state.dpll &
  5616. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5617. /* No need to actually set up the DPLL with DSI */
  5618. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5619. return;
  5620. mutex_lock(&dev_priv->sb_lock);
  5621. bestn = pipe_config->dpll.n;
  5622. bestm1 = pipe_config->dpll.m1;
  5623. bestm2 = pipe_config->dpll.m2;
  5624. bestp1 = pipe_config->dpll.p1;
  5625. bestp2 = pipe_config->dpll.p2;
  5626. /* See eDP HDMI DPIO driver vbios notes doc */
  5627. /* PLL B needs special handling */
  5628. if (pipe == PIPE_B)
  5629. vlv_pllb_recal_opamp(dev_priv, pipe);
  5630. /* Set up Tx target for periodic Rcomp update */
  5631. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5632. /* Disable target IRef on PLL */
  5633. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5634. reg_val &= 0x00ffffff;
  5635. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5636. /* Disable fast lock */
  5637. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5638. /* Set idtafcrecal before PLL is enabled */
  5639. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5640. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5641. mdiv |= ((bestn << DPIO_N_SHIFT));
  5642. mdiv |= (1 << DPIO_K_SHIFT);
  5643. /*
  5644. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5645. * but we don't support that).
  5646. * Note: don't use the DAC post divider as it seems unstable.
  5647. */
  5648. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5649. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5650. mdiv |= DPIO_ENABLE_CALIBRATION;
  5651. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5652. /* Set HBR and RBR LPF coefficients */
  5653. if (pipe_config->port_clock == 162000 ||
  5654. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5655. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5656. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5657. 0x009f0003);
  5658. else
  5659. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5660. 0x00d0000f);
  5661. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5662. /* Use SSC source */
  5663. if (pipe == PIPE_A)
  5664. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5665. 0x0df40000);
  5666. else
  5667. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5668. 0x0df70000);
  5669. } else { /* HDMI or VGA */
  5670. /* Use bend source */
  5671. if (pipe == PIPE_A)
  5672. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5673. 0x0df70000);
  5674. else
  5675. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5676. 0x0df40000);
  5677. }
  5678. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5679. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5680. if (intel_crtc_has_dp_encoder(crtc->config))
  5681. coreclk |= 0x01000000;
  5682. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5684. mutex_unlock(&dev_priv->sb_lock);
  5685. }
  5686. static void chv_prepare_pll(struct intel_crtc *crtc,
  5687. const struct intel_crtc_state *pipe_config)
  5688. {
  5689. struct drm_device *dev = crtc->base.dev;
  5690. struct drm_i915_private *dev_priv = to_i915(dev);
  5691. enum pipe pipe = crtc->pipe;
  5692. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5693. u32 loopfilter, tribuf_calcntr;
  5694. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5695. u32 dpio_val;
  5696. int vco;
  5697. /* Enable Refclk and SSC */
  5698. I915_WRITE(DPLL(pipe),
  5699. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5700. /* No need to actually set up the DPLL with DSI */
  5701. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5702. return;
  5703. bestn = pipe_config->dpll.n;
  5704. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5705. bestm1 = pipe_config->dpll.m1;
  5706. bestm2 = pipe_config->dpll.m2 >> 22;
  5707. bestp1 = pipe_config->dpll.p1;
  5708. bestp2 = pipe_config->dpll.p2;
  5709. vco = pipe_config->dpll.vco;
  5710. dpio_val = 0;
  5711. loopfilter = 0;
  5712. mutex_lock(&dev_priv->sb_lock);
  5713. /* p1 and p2 divider */
  5714. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5715. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5716. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5717. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5718. 1 << DPIO_CHV_K_DIV_SHIFT);
  5719. /* Feedback post-divider - m2 */
  5720. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5721. /* Feedback refclk divider - n and m1 */
  5722. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5723. DPIO_CHV_M1_DIV_BY_2 |
  5724. 1 << DPIO_CHV_N_DIV_SHIFT);
  5725. /* M2 fraction division */
  5726. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5727. /* M2 fraction division enable */
  5728. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5729. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5730. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5731. if (bestm2_frac)
  5732. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5733. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5734. /* Program digital lock detect threshold */
  5735. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5736. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5737. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5738. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5739. if (!bestm2_frac)
  5740. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5741. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5742. /* Loop filter */
  5743. if (vco == 5400000) {
  5744. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5745. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5746. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5747. tribuf_calcntr = 0x9;
  5748. } else if (vco <= 6200000) {
  5749. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5750. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5751. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5752. tribuf_calcntr = 0x9;
  5753. } else if (vco <= 6480000) {
  5754. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5755. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5756. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5757. tribuf_calcntr = 0x8;
  5758. } else {
  5759. /* Not supported. Apply the same limits as in the max case */
  5760. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5761. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5762. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5763. tribuf_calcntr = 0;
  5764. }
  5765. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5766. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5767. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5768. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5769. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5770. /* AFC Recal */
  5771. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5772. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5773. DPIO_AFC_RECAL);
  5774. mutex_unlock(&dev_priv->sb_lock);
  5775. }
  5776. /**
  5777. * vlv_force_pll_on - forcibly enable just the PLL
  5778. * @dev_priv: i915 private structure
  5779. * @pipe: pipe PLL to enable
  5780. * @dpll: PLL configuration
  5781. *
  5782. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5783. * in cases where we need the PLL enabled even when @pipe is not going to
  5784. * be enabled.
  5785. */
  5786. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5787. const struct dpll *dpll)
  5788. {
  5789. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5790. struct intel_crtc_state *pipe_config;
  5791. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5792. if (!pipe_config)
  5793. return -ENOMEM;
  5794. pipe_config->base.crtc = &crtc->base;
  5795. pipe_config->pixel_multiplier = 1;
  5796. pipe_config->dpll = *dpll;
  5797. if (IS_CHERRYVIEW(dev_priv)) {
  5798. chv_compute_dpll(crtc, pipe_config);
  5799. chv_prepare_pll(crtc, pipe_config);
  5800. chv_enable_pll(crtc, pipe_config);
  5801. } else {
  5802. vlv_compute_dpll(crtc, pipe_config);
  5803. vlv_prepare_pll(crtc, pipe_config);
  5804. vlv_enable_pll(crtc, pipe_config);
  5805. }
  5806. kfree(pipe_config);
  5807. return 0;
  5808. }
  5809. /**
  5810. * vlv_force_pll_off - forcibly disable just the PLL
  5811. * @dev_priv: i915 private structure
  5812. * @pipe: pipe PLL to disable
  5813. *
  5814. * Disable the PLL for @pipe. To be used in cases where we need
  5815. * the PLL enabled even when @pipe is not going to be enabled.
  5816. */
  5817. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5818. {
  5819. if (IS_CHERRYVIEW(dev_priv))
  5820. chv_disable_pll(dev_priv, pipe);
  5821. else
  5822. vlv_disable_pll(dev_priv, pipe);
  5823. }
  5824. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5825. struct intel_crtc_state *crtc_state,
  5826. struct dpll *reduced_clock)
  5827. {
  5828. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5829. u32 dpll;
  5830. struct dpll *clock = &crtc_state->dpll;
  5831. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5832. dpll = DPLL_VGA_MODE_DIS;
  5833. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5834. dpll |= DPLLB_MODE_LVDS;
  5835. else
  5836. dpll |= DPLLB_MODE_DAC_SERIAL;
  5837. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5838. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5839. dpll |= (crtc_state->pixel_multiplier - 1)
  5840. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5841. }
  5842. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5843. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5844. dpll |= DPLL_SDVO_HIGH_SPEED;
  5845. if (intel_crtc_has_dp_encoder(crtc_state))
  5846. dpll |= DPLL_SDVO_HIGH_SPEED;
  5847. /* compute bitmask from p1 value */
  5848. if (IS_PINEVIEW(dev_priv))
  5849. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5850. else {
  5851. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5852. if (IS_G4X(dev_priv) && reduced_clock)
  5853. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5854. }
  5855. switch (clock->p2) {
  5856. case 5:
  5857. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5858. break;
  5859. case 7:
  5860. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5861. break;
  5862. case 10:
  5863. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5864. break;
  5865. case 14:
  5866. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5867. break;
  5868. }
  5869. if (INTEL_GEN(dev_priv) >= 4)
  5870. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5871. if (crtc_state->sdvo_tv_clock)
  5872. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5873. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5874. intel_panel_use_ssc(dev_priv))
  5875. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5876. else
  5877. dpll |= PLL_REF_INPUT_DREFCLK;
  5878. dpll |= DPLL_VCO_ENABLE;
  5879. crtc_state->dpll_hw_state.dpll = dpll;
  5880. if (INTEL_GEN(dev_priv) >= 4) {
  5881. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5882. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5883. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5884. }
  5885. }
  5886. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5887. struct intel_crtc_state *crtc_state,
  5888. struct dpll *reduced_clock)
  5889. {
  5890. struct drm_device *dev = crtc->base.dev;
  5891. struct drm_i915_private *dev_priv = to_i915(dev);
  5892. u32 dpll;
  5893. struct dpll *clock = &crtc_state->dpll;
  5894. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5895. dpll = DPLL_VGA_MODE_DIS;
  5896. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5897. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5898. } else {
  5899. if (clock->p1 == 2)
  5900. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5901. else
  5902. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5903. if (clock->p2 == 4)
  5904. dpll |= PLL_P2_DIVIDE_BY_4;
  5905. }
  5906. if (!IS_I830(dev_priv) &&
  5907. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5908. dpll |= DPLL_DVO_2X_MODE;
  5909. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5910. intel_panel_use_ssc(dev_priv))
  5911. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5912. else
  5913. dpll |= PLL_REF_INPUT_DREFCLK;
  5914. dpll |= DPLL_VCO_ENABLE;
  5915. crtc_state->dpll_hw_state.dpll = dpll;
  5916. }
  5917. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5918. {
  5919. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5920. enum pipe pipe = intel_crtc->pipe;
  5921. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5922. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5923. uint32_t crtc_vtotal, crtc_vblank_end;
  5924. int vsyncshift = 0;
  5925. /* We need to be careful not to changed the adjusted mode, for otherwise
  5926. * the hw state checker will get angry at the mismatch. */
  5927. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5928. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5929. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5930. /* the chip adds 2 halflines automatically */
  5931. crtc_vtotal -= 1;
  5932. crtc_vblank_end -= 1;
  5933. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5934. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5935. else
  5936. vsyncshift = adjusted_mode->crtc_hsync_start -
  5937. adjusted_mode->crtc_htotal / 2;
  5938. if (vsyncshift < 0)
  5939. vsyncshift += adjusted_mode->crtc_htotal;
  5940. }
  5941. if (INTEL_GEN(dev_priv) > 3)
  5942. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5943. I915_WRITE(HTOTAL(cpu_transcoder),
  5944. (adjusted_mode->crtc_hdisplay - 1) |
  5945. ((adjusted_mode->crtc_htotal - 1) << 16));
  5946. I915_WRITE(HBLANK(cpu_transcoder),
  5947. (adjusted_mode->crtc_hblank_start - 1) |
  5948. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5949. I915_WRITE(HSYNC(cpu_transcoder),
  5950. (adjusted_mode->crtc_hsync_start - 1) |
  5951. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5952. I915_WRITE(VTOTAL(cpu_transcoder),
  5953. (adjusted_mode->crtc_vdisplay - 1) |
  5954. ((crtc_vtotal - 1) << 16));
  5955. I915_WRITE(VBLANK(cpu_transcoder),
  5956. (adjusted_mode->crtc_vblank_start - 1) |
  5957. ((crtc_vblank_end - 1) << 16));
  5958. I915_WRITE(VSYNC(cpu_transcoder),
  5959. (adjusted_mode->crtc_vsync_start - 1) |
  5960. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5961. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5962. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5963. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5964. * bits. */
  5965. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5966. (pipe == PIPE_B || pipe == PIPE_C))
  5967. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5968. }
  5969. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5970. {
  5971. struct drm_device *dev = intel_crtc->base.dev;
  5972. struct drm_i915_private *dev_priv = to_i915(dev);
  5973. enum pipe pipe = intel_crtc->pipe;
  5974. /* pipesrc controls the size that is scaled from, which should
  5975. * always be the user's requested size.
  5976. */
  5977. I915_WRITE(PIPESRC(pipe),
  5978. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5979. (intel_crtc->config->pipe_src_h - 1));
  5980. }
  5981. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5982. struct intel_crtc_state *pipe_config)
  5983. {
  5984. struct drm_device *dev = crtc->base.dev;
  5985. struct drm_i915_private *dev_priv = to_i915(dev);
  5986. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5987. uint32_t tmp;
  5988. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5989. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5990. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5991. tmp = I915_READ(HBLANK(cpu_transcoder));
  5992. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5993. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5994. tmp = I915_READ(HSYNC(cpu_transcoder));
  5995. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5996. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5997. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5998. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5999. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6000. tmp = I915_READ(VBLANK(cpu_transcoder));
  6001. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6002. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6003. tmp = I915_READ(VSYNC(cpu_transcoder));
  6004. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6005. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6006. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6007. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6008. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6009. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6010. }
  6011. }
  6012. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6013. struct intel_crtc_state *pipe_config)
  6014. {
  6015. struct drm_device *dev = crtc->base.dev;
  6016. struct drm_i915_private *dev_priv = to_i915(dev);
  6017. u32 tmp;
  6018. tmp = I915_READ(PIPESRC(crtc->pipe));
  6019. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6020. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6021. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6022. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6023. }
  6024. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6025. struct intel_crtc_state *pipe_config)
  6026. {
  6027. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6028. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6029. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6030. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6031. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6032. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6033. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6034. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6035. mode->flags = pipe_config->base.adjusted_mode.flags;
  6036. mode->type = DRM_MODE_TYPE_DRIVER;
  6037. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6038. mode->hsync = drm_mode_hsync(mode);
  6039. mode->vrefresh = drm_mode_vrefresh(mode);
  6040. drm_mode_set_name(mode);
  6041. }
  6042. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6043. {
  6044. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6045. uint32_t pipeconf;
  6046. pipeconf = 0;
  6047. /* we keep both pipes enabled on 830 */
  6048. if (IS_I830(dev_priv))
  6049. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6050. if (intel_crtc->config->double_wide)
  6051. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6052. /* only g4x and later have fancy bpc/dither controls */
  6053. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6054. IS_CHERRYVIEW(dev_priv)) {
  6055. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6056. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6057. pipeconf |= PIPECONF_DITHER_EN |
  6058. PIPECONF_DITHER_TYPE_SP;
  6059. switch (intel_crtc->config->pipe_bpp) {
  6060. case 18:
  6061. pipeconf |= PIPECONF_6BPC;
  6062. break;
  6063. case 24:
  6064. pipeconf |= PIPECONF_8BPC;
  6065. break;
  6066. case 30:
  6067. pipeconf |= PIPECONF_10BPC;
  6068. break;
  6069. default:
  6070. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6071. BUG();
  6072. }
  6073. }
  6074. if (HAS_PIPE_CXSR(dev_priv)) {
  6075. if (intel_crtc->lowfreq_avail) {
  6076. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6077. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6078. } else {
  6079. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6080. }
  6081. }
  6082. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6083. if (INTEL_GEN(dev_priv) < 4 ||
  6084. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6085. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6086. else
  6087. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6088. } else
  6089. pipeconf |= PIPECONF_PROGRESSIVE;
  6090. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6091. intel_crtc->config->limited_color_range)
  6092. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6093. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6094. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6095. }
  6096. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6097. struct intel_crtc_state *crtc_state)
  6098. {
  6099. struct drm_device *dev = crtc->base.dev;
  6100. struct drm_i915_private *dev_priv = to_i915(dev);
  6101. const struct intel_limit *limit;
  6102. int refclk = 48000;
  6103. memset(&crtc_state->dpll_hw_state, 0,
  6104. sizeof(crtc_state->dpll_hw_state));
  6105. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6106. if (intel_panel_use_ssc(dev_priv)) {
  6107. refclk = dev_priv->vbt.lvds_ssc_freq;
  6108. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6109. }
  6110. limit = &intel_limits_i8xx_lvds;
  6111. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6112. limit = &intel_limits_i8xx_dvo;
  6113. } else {
  6114. limit = &intel_limits_i8xx_dac;
  6115. }
  6116. if (!crtc_state->clock_set &&
  6117. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6118. refclk, NULL, &crtc_state->dpll)) {
  6119. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6120. return -EINVAL;
  6121. }
  6122. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6123. return 0;
  6124. }
  6125. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6126. struct intel_crtc_state *crtc_state)
  6127. {
  6128. struct drm_device *dev = crtc->base.dev;
  6129. struct drm_i915_private *dev_priv = to_i915(dev);
  6130. const struct intel_limit *limit;
  6131. int refclk = 96000;
  6132. memset(&crtc_state->dpll_hw_state, 0,
  6133. sizeof(crtc_state->dpll_hw_state));
  6134. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6135. if (intel_panel_use_ssc(dev_priv)) {
  6136. refclk = dev_priv->vbt.lvds_ssc_freq;
  6137. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6138. }
  6139. if (intel_is_dual_link_lvds(dev))
  6140. limit = &intel_limits_g4x_dual_channel_lvds;
  6141. else
  6142. limit = &intel_limits_g4x_single_channel_lvds;
  6143. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6144. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6145. limit = &intel_limits_g4x_hdmi;
  6146. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6147. limit = &intel_limits_g4x_sdvo;
  6148. } else {
  6149. /* The option is for other outputs */
  6150. limit = &intel_limits_i9xx_sdvo;
  6151. }
  6152. if (!crtc_state->clock_set &&
  6153. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6154. refclk, NULL, &crtc_state->dpll)) {
  6155. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6156. return -EINVAL;
  6157. }
  6158. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6159. return 0;
  6160. }
  6161. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6162. struct intel_crtc_state *crtc_state)
  6163. {
  6164. struct drm_device *dev = crtc->base.dev;
  6165. struct drm_i915_private *dev_priv = to_i915(dev);
  6166. const struct intel_limit *limit;
  6167. int refclk = 96000;
  6168. memset(&crtc_state->dpll_hw_state, 0,
  6169. sizeof(crtc_state->dpll_hw_state));
  6170. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6171. if (intel_panel_use_ssc(dev_priv)) {
  6172. refclk = dev_priv->vbt.lvds_ssc_freq;
  6173. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6174. }
  6175. limit = &intel_limits_pineview_lvds;
  6176. } else {
  6177. limit = &intel_limits_pineview_sdvo;
  6178. }
  6179. if (!crtc_state->clock_set &&
  6180. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6181. refclk, NULL, &crtc_state->dpll)) {
  6182. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6183. return -EINVAL;
  6184. }
  6185. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6186. return 0;
  6187. }
  6188. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6189. struct intel_crtc_state *crtc_state)
  6190. {
  6191. struct drm_device *dev = crtc->base.dev;
  6192. struct drm_i915_private *dev_priv = to_i915(dev);
  6193. const struct intel_limit *limit;
  6194. int refclk = 96000;
  6195. memset(&crtc_state->dpll_hw_state, 0,
  6196. sizeof(crtc_state->dpll_hw_state));
  6197. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6198. if (intel_panel_use_ssc(dev_priv)) {
  6199. refclk = dev_priv->vbt.lvds_ssc_freq;
  6200. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6201. }
  6202. limit = &intel_limits_i9xx_lvds;
  6203. } else {
  6204. limit = &intel_limits_i9xx_sdvo;
  6205. }
  6206. if (!crtc_state->clock_set &&
  6207. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6208. refclk, NULL, &crtc_state->dpll)) {
  6209. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6210. return -EINVAL;
  6211. }
  6212. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6213. return 0;
  6214. }
  6215. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6216. struct intel_crtc_state *crtc_state)
  6217. {
  6218. int refclk = 100000;
  6219. const struct intel_limit *limit = &intel_limits_chv;
  6220. memset(&crtc_state->dpll_hw_state, 0,
  6221. sizeof(crtc_state->dpll_hw_state));
  6222. if (!crtc_state->clock_set &&
  6223. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6224. refclk, NULL, &crtc_state->dpll)) {
  6225. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6226. return -EINVAL;
  6227. }
  6228. chv_compute_dpll(crtc, crtc_state);
  6229. return 0;
  6230. }
  6231. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6232. struct intel_crtc_state *crtc_state)
  6233. {
  6234. int refclk = 100000;
  6235. const struct intel_limit *limit = &intel_limits_vlv;
  6236. memset(&crtc_state->dpll_hw_state, 0,
  6237. sizeof(crtc_state->dpll_hw_state));
  6238. if (!crtc_state->clock_set &&
  6239. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6240. refclk, NULL, &crtc_state->dpll)) {
  6241. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6242. return -EINVAL;
  6243. }
  6244. vlv_compute_dpll(crtc, crtc_state);
  6245. return 0;
  6246. }
  6247. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6248. struct intel_crtc_state *pipe_config)
  6249. {
  6250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6251. uint32_t tmp;
  6252. if (INTEL_GEN(dev_priv) <= 3 &&
  6253. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6254. return;
  6255. tmp = I915_READ(PFIT_CONTROL);
  6256. if (!(tmp & PFIT_ENABLE))
  6257. return;
  6258. /* Check whether the pfit is attached to our pipe. */
  6259. if (INTEL_GEN(dev_priv) < 4) {
  6260. if (crtc->pipe != PIPE_B)
  6261. return;
  6262. } else {
  6263. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6264. return;
  6265. }
  6266. pipe_config->gmch_pfit.control = tmp;
  6267. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6268. }
  6269. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6270. struct intel_crtc_state *pipe_config)
  6271. {
  6272. struct drm_device *dev = crtc->base.dev;
  6273. struct drm_i915_private *dev_priv = to_i915(dev);
  6274. int pipe = pipe_config->cpu_transcoder;
  6275. struct dpll clock;
  6276. u32 mdiv;
  6277. int refclk = 100000;
  6278. /* In case of DSI, DPLL will not be used */
  6279. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6280. return;
  6281. mutex_lock(&dev_priv->sb_lock);
  6282. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6283. mutex_unlock(&dev_priv->sb_lock);
  6284. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6285. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6286. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6287. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6288. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6289. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6290. }
  6291. static void
  6292. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6293. struct intel_initial_plane_config *plane_config)
  6294. {
  6295. struct drm_device *dev = crtc->base.dev;
  6296. struct drm_i915_private *dev_priv = to_i915(dev);
  6297. u32 val, base, offset;
  6298. int pipe = crtc->pipe, plane = crtc->plane;
  6299. int fourcc, pixel_format;
  6300. unsigned int aligned_height;
  6301. struct drm_framebuffer *fb;
  6302. struct intel_framebuffer *intel_fb;
  6303. val = I915_READ(DSPCNTR(plane));
  6304. if (!(val & DISPLAY_PLANE_ENABLE))
  6305. return;
  6306. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6307. if (!intel_fb) {
  6308. DRM_DEBUG_KMS("failed to alloc fb\n");
  6309. return;
  6310. }
  6311. fb = &intel_fb->base;
  6312. fb->dev = dev;
  6313. if (INTEL_GEN(dev_priv) >= 4) {
  6314. if (val & DISPPLANE_TILED) {
  6315. plane_config->tiling = I915_TILING_X;
  6316. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6317. }
  6318. }
  6319. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6320. fourcc = i9xx_format_to_fourcc(pixel_format);
  6321. fb->format = drm_format_info(fourcc);
  6322. if (INTEL_GEN(dev_priv) >= 4) {
  6323. if (plane_config->tiling)
  6324. offset = I915_READ(DSPTILEOFF(plane));
  6325. else
  6326. offset = I915_READ(DSPLINOFF(plane));
  6327. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6328. } else {
  6329. base = I915_READ(DSPADDR(plane));
  6330. }
  6331. plane_config->base = base;
  6332. val = I915_READ(PIPESRC(pipe));
  6333. fb->width = ((val >> 16) & 0xfff) + 1;
  6334. fb->height = ((val >> 0) & 0xfff) + 1;
  6335. val = I915_READ(DSPSTRIDE(pipe));
  6336. fb->pitches[0] = val & 0xffffffc0;
  6337. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6338. plane_config->size = fb->pitches[0] * aligned_height;
  6339. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6340. pipe_name(pipe), plane, fb->width, fb->height,
  6341. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6342. plane_config->size);
  6343. plane_config->fb = intel_fb;
  6344. }
  6345. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6346. struct intel_crtc_state *pipe_config)
  6347. {
  6348. struct drm_device *dev = crtc->base.dev;
  6349. struct drm_i915_private *dev_priv = to_i915(dev);
  6350. int pipe = pipe_config->cpu_transcoder;
  6351. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6352. struct dpll clock;
  6353. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6354. int refclk = 100000;
  6355. /* In case of DSI, DPLL will not be used */
  6356. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6357. return;
  6358. mutex_lock(&dev_priv->sb_lock);
  6359. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6360. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6361. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6362. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6363. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6364. mutex_unlock(&dev_priv->sb_lock);
  6365. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6366. clock.m2 = (pll_dw0 & 0xff) << 22;
  6367. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6368. clock.m2 |= pll_dw2 & 0x3fffff;
  6369. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6370. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6371. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6372. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6373. }
  6374. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6375. struct intel_crtc_state *pipe_config)
  6376. {
  6377. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6378. enum intel_display_power_domain power_domain;
  6379. uint32_t tmp;
  6380. bool ret;
  6381. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6382. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6383. return false;
  6384. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6385. pipe_config->shared_dpll = NULL;
  6386. ret = false;
  6387. tmp = I915_READ(PIPECONF(crtc->pipe));
  6388. if (!(tmp & PIPECONF_ENABLE))
  6389. goto out;
  6390. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6391. IS_CHERRYVIEW(dev_priv)) {
  6392. switch (tmp & PIPECONF_BPC_MASK) {
  6393. case PIPECONF_6BPC:
  6394. pipe_config->pipe_bpp = 18;
  6395. break;
  6396. case PIPECONF_8BPC:
  6397. pipe_config->pipe_bpp = 24;
  6398. break;
  6399. case PIPECONF_10BPC:
  6400. pipe_config->pipe_bpp = 30;
  6401. break;
  6402. default:
  6403. break;
  6404. }
  6405. }
  6406. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6407. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6408. pipe_config->limited_color_range = true;
  6409. if (INTEL_GEN(dev_priv) < 4)
  6410. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6411. intel_get_pipe_timings(crtc, pipe_config);
  6412. intel_get_pipe_src_size(crtc, pipe_config);
  6413. i9xx_get_pfit_config(crtc, pipe_config);
  6414. if (INTEL_GEN(dev_priv) >= 4) {
  6415. /* No way to read it out on pipes B and C */
  6416. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6417. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6418. else
  6419. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6420. pipe_config->pixel_multiplier =
  6421. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6422. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6423. pipe_config->dpll_hw_state.dpll_md = tmp;
  6424. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6425. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6426. tmp = I915_READ(DPLL(crtc->pipe));
  6427. pipe_config->pixel_multiplier =
  6428. ((tmp & SDVO_MULTIPLIER_MASK)
  6429. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6430. } else {
  6431. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6432. * port and will be fixed up in the encoder->get_config
  6433. * function. */
  6434. pipe_config->pixel_multiplier = 1;
  6435. }
  6436. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6437. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6438. /*
  6439. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6440. * on 830. Filter it out here so that we don't
  6441. * report errors due to that.
  6442. */
  6443. if (IS_I830(dev_priv))
  6444. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6445. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6446. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6447. } else {
  6448. /* Mask out read-only status bits. */
  6449. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6450. DPLL_PORTC_READY_MASK |
  6451. DPLL_PORTB_READY_MASK);
  6452. }
  6453. if (IS_CHERRYVIEW(dev_priv))
  6454. chv_crtc_clock_get(crtc, pipe_config);
  6455. else if (IS_VALLEYVIEW(dev_priv))
  6456. vlv_crtc_clock_get(crtc, pipe_config);
  6457. else
  6458. i9xx_crtc_clock_get(crtc, pipe_config);
  6459. /*
  6460. * Normally the dotclock is filled in by the encoder .get_config()
  6461. * but in case the pipe is enabled w/o any ports we need a sane
  6462. * default.
  6463. */
  6464. pipe_config->base.adjusted_mode.crtc_clock =
  6465. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6466. ret = true;
  6467. out:
  6468. intel_display_power_put(dev_priv, power_domain);
  6469. return ret;
  6470. }
  6471. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6472. {
  6473. struct intel_encoder *encoder;
  6474. int i;
  6475. u32 val, final;
  6476. bool has_lvds = false;
  6477. bool has_cpu_edp = false;
  6478. bool has_panel = false;
  6479. bool has_ck505 = false;
  6480. bool can_ssc = false;
  6481. bool using_ssc_source = false;
  6482. /* We need to take the global config into account */
  6483. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6484. switch (encoder->type) {
  6485. case INTEL_OUTPUT_LVDS:
  6486. has_panel = true;
  6487. has_lvds = true;
  6488. break;
  6489. case INTEL_OUTPUT_EDP:
  6490. has_panel = true;
  6491. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6492. has_cpu_edp = true;
  6493. break;
  6494. default:
  6495. break;
  6496. }
  6497. }
  6498. if (HAS_PCH_IBX(dev_priv)) {
  6499. has_ck505 = dev_priv->vbt.display_clock_mode;
  6500. can_ssc = has_ck505;
  6501. } else {
  6502. has_ck505 = false;
  6503. can_ssc = true;
  6504. }
  6505. /* Check if any DPLLs are using the SSC source */
  6506. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6507. u32 temp = I915_READ(PCH_DPLL(i));
  6508. if (!(temp & DPLL_VCO_ENABLE))
  6509. continue;
  6510. if ((temp & PLL_REF_INPUT_MASK) ==
  6511. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6512. using_ssc_source = true;
  6513. break;
  6514. }
  6515. }
  6516. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6517. has_panel, has_lvds, has_ck505, using_ssc_source);
  6518. /* Ironlake: try to setup display ref clock before DPLL
  6519. * enabling. This is only under driver's control after
  6520. * PCH B stepping, previous chipset stepping should be
  6521. * ignoring this setting.
  6522. */
  6523. val = I915_READ(PCH_DREF_CONTROL);
  6524. /* As we must carefully and slowly disable/enable each source in turn,
  6525. * compute the final state we want first and check if we need to
  6526. * make any changes at all.
  6527. */
  6528. final = val;
  6529. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6530. if (has_ck505)
  6531. final |= DREF_NONSPREAD_CK505_ENABLE;
  6532. else
  6533. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6534. final &= ~DREF_SSC_SOURCE_MASK;
  6535. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6536. final &= ~DREF_SSC1_ENABLE;
  6537. if (has_panel) {
  6538. final |= DREF_SSC_SOURCE_ENABLE;
  6539. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6540. final |= DREF_SSC1_ENABLE;
  6541. if (has_cpu_edp) {
  6542. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6543. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6544. else
  6545. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6546. } else
  6547. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6548. } else if (using_ssc_source) {
  6549. final |= DREF_SSC_SOURCE_ENABLE;
  6550. final |= DREF_SSC1_ENABLE;
  6551. }
  6552. if (final == val)
  6553. return;
  6554. /* Always enable nonspread source */
  6555. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6556. if (has_ck505)
  6557. val |= DREF_NONSPREAD_CK505_ENABLE;
  6558. else
  6559. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6560. if (has_panel) {
  6561. val &= ~DREF_SSC_SOURCE_MASK;
  6562. val |= DREF_SSC_SOURCE_ENABLE;
  6563. /* SSC must be turned on before enabling the CPU output */
  6564. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6565. DRM_DEBUG_KMS("Using SSC on panel\n");
  6566. val |= DREF_SSC1_ENABLE;
  6567. } else
  6568. val &= ~DREF_SSC1_ENABLE;
  6569. /* Get SSC going before enabling the outputs */
  6570. I915_WRITE(PCH_DREF_CONTROL, val);
  6571. POSTING_READ(PCH_DREF_CONTROL);
  6572. udelay(200);
  6573. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6574. /* Enable CPU source on CPU attached eDP */
  6575. if (has_cpu_edp) {
  6576. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6577. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6578. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6579. } else
  6580. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6581. } else
  6582. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6583. I915_WRITE(PCH_DREF_CONTROL, val);
  6584. POSTING_READ(PCH_DREF_CONTROL);
  6585. udelay(200);
  6586. } else {
  6587. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6588. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6589. /* Turn off CPU output */
  6590. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6591. I915_WRITE(PCH_DREF_CONTROL, val);
  6592. POSTING_READ(PCH_DREF_CONTROL);
  6593. udelay(200);
  6594. if (!using_ssc_source) {
  6595. DRM_DEBUG_KMS("Disabling SSC source\n");
  6596. /* Turn off the SSC source */
  6597. val &= ~DREF_SSC_SOURCE_MASK;
  6598. val |= DREF_SSC_SOURCE_DISABLE;
  6599. /* Turn off SSC1 */
  6600. val &= ~DREF_SSC1_ENABLE;
  6601. I915_WRITE(PCH_DREF_CONTROL, val);
  6602. POSTING_READ(PCH_DREF_CONTROL);
  6603. udelay(200);
  6604. }
  6605. }
  6606. BUG_ON(val != final);
  6607. }
  6608. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6609. {
  6610. uint32_t tmp;
  6611. tmp = I915_READ(SOUTH_CHICKEN2);
  6612. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6613. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6614. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6615. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6616. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6617. tmp = I915_READ(SOUTH_CHICKEN2);
  6618. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6619. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6620. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6621. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6622. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6623. }
  6624. /* WaMPhyProgramming:hsw */
  6625. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6626. {
  6627. uint32_t tmp;
  6628. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6629. tmp &= ~(0xFF << 24);
  6630. tmp |= (0x12 << 24);
  6631. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6632. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6633. tmp |= (1 << 11);
  6634. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6635. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6636. tmp |= (1 << 11);
  6637. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6638. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6639. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6640. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6641. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6642. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6643. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6644. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6645. tmp &= ~(7 << 13);
  6646. tmp |= (5 << 13);
  6647. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6648. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6649. tmp &= ~(7 << 13);
  6650. tmp |= (5 << 13);
  6651. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6652. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6653. tmp &= ~0xFF;
  6654. tmp |= 0x1C;
  6655. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6656. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6657. tmp &= ~0xFF;
  6658. tmp |= 0x1C;
  6659. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6660. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6661. tmp &= ~(0xFF << 16);
  6662. tmp |= (0x1C << 16);
  6663. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6664. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6665. tmp &= ~(0xFF << 16);
  6666. tmp |= (0x1C << 16);
  6667. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6668. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6669. tmp |= (1 << 27);
  6670. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6671. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6672. tmp |= (1 << 27);
  6673. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6674. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6675. tmp &= ~(0xF << 28);
  6676. tmp |= (4 << 28);
  6677. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6678. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6679. tmp &= ~(0xF << 28);
  6680. tmp |= (4 << 28);
  6681. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6682. }
  6683. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6684. * Programming" based on the parameters passed:
  6685. * - Sequence to enable CLKOUT_DP
  6686. * - Sequence to enable CLKOUT_DP without spread
  6687. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6688. */
  6689. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6690. bool with_spread, bool with_fdi)
  6691. {
  6692. uint32_t reg, tmp;
  6693. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6694. with_spread = true;
  6695. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6696. with_fdi, "LP PCH doesn't have FDI\n"))
  6697. with_fdi = false;
  6698. mutex_lock(&dev_priv->sb_lock);
  6699. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6700. tmp &= ~SBI_SSCCTL_DISABLE;
  6701. tmp |= SBI_SSCCTL_PATHALT;
  6702. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6703. udelay(24);
  6704. if (with_spread) {
  6705. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6706. tmp &= ~SBI_SSCCTL_PATHALT;
  6707. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6708. if (with_fdi) {
  6709. lpt_reset_fdi_mphy(dev_priv);
  6710. lpt_program_fdi_mphy(dev_priv);
  6711. }
  6712. }
  6713. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6714. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6715. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6716. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6717. mutex_unlock(&dev_priv->sb_lock);
  6718. }
  6719. /* Sequence to disable CLKOUT_DP */
  6720. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6721. {
  6722. uint32_t reg, tmp;
  6723. mutex_lock(&dev_priv->sb_lock);
  6724. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6725. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6726. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6727. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6728. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6729. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6730. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6731. tmp |= SBI_SSCCTL_PATHALT;
  6732. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6733. udelay(32);
  6734. }
  6735. tmp |= SBI_SSCCTL_DISABLE;
  6736. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6737. }
  6738. mutex_unlock(&dev_priv->sb_lock);
  6739. }
  6740. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6741. static const uint16_t sscdivintphase[] = {
  6742. [BEND_IDX( 50)] = 0x3B23,
  6743. [BEND_IDX( 45)] = 0x3B23,
  6744. [BEND_IDX( 40)] = 0x3C23,
  6745. [BEND_IDX( 35)] = 0x3C23,
  6746. [BEND_IDX( 30)] = 0x3D23,
  6747. [BEND_IDX( 25)] = 0x3D23,
  6748. [BEND_IDX( 20)] = 0x3E23,
  6749. [BEND_IDX( 15)] = 0x3E23,
  6750. [BEND_IDX( 10)] = 0x3F23,
  6751. [BEND_IDX( 5)] = 0x3F23,
  6752. [BEND_IDX( 0)] = 0x0025,
  6753. [BEND_IDX( -5)] = 0x0025,
  6754. [BEND_IDX(-10)] = 0x0125,
  6755. [BEND_IDX(-15)] = 0x0125,
  6756. [BEND_IDX(-20)] = 0x0225,
  6757. [BEND_IDX(-25)] = 0x0225,
  6758. [BEND_IDX(-30)] = 0x0325,
  6759. [BEND_IDX(-35)] = 0x0325,
  6760. [BEND_IDX(-40)] = 0x0425,
  6761. [BEND_IDX(-45)] = 0x0425,
  6762. [BEND_IDX(-50)] = 0x0525,
  6763. };
  6764. /*
  6765. * Bend CLKOUT_DP
  6766. * steps -50 to 50 inclusive, in steps of 5
  6767. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6768. * change in clock period = -(steps / 10) * 5.787 ps
  6769. */
  6770. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6771. {
  6772. uint32_t tmp;
  6773. int idx = BEND_IDX(steps);
  6774. if (WARN_ON(steps % 5 != 0))
  6775. return;
  6776. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6777. return;
  6778. mutex_lock(&dev_priv->sb_lock);
  6779. if (steps % 10 != 0)
  6780. tmp = 0xAAAAAAAB;
  6781. else
  6782. tmp = 0x00000000;
  6783. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6784. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6785. tmp &= 0xffff0000;
  6786. tmp |= sscdivintphase[idx];
  6787. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6788. mutex_unlock(&dev_priv->sb_lock);
  6789. }
  6790. #undef BEND_IDX
  6791. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6792. {
  6793. struct intel_encoder *encoder;
  6794. bool has_vga = false;
  6795. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6796. switch (encoder->type) {
  6797. case INTEL_OUTPUT_ANALOG:
  6798. has_vga = true;
  6799. break;
  6800. default:
  6801. break;
  6802. }
  6803. }
  6804. if (has_vga) {
  6805. lpt_bend_clkout_dp(dev_priv, 0);
  6806. lpt_enable_clkout_dp(dev_priv, true, true);
  6807. } else {
  6808. lpt_disable_clkout_dp(dev_priv);
  6809. }
  6810. }
  6811. /*
  6812. * Initialize reference clocks when the driver loads
  6813. */
  6814. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6815. {
  6816. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6817. ironlake_init_pch_refclk(dev_priv);
  6818. else if (HAS_PCH_LPT(dev_priv))
  6819. lpt_init_pch_refclk(dev_priv);
  6820. }
  6821. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6822. {
  6823. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6825. int pipe = intel_crtc->pipe;
  6826. uint32_t val;
  6827. val = 0;
  6828. switch (intel_crtc->config->pipe_bpp) {
  6829. case 18:
  6830. val |= PIPECONF_6BPC;
  6831. break;
  6832. case 24:
  6833. val |= PIPECONF_8BPC;
  6834. break;
  6835. case 30:
  6836. val |= PIPECONF_10BPC;
  6837. break;
  6838. case 36:
  6839. val |= PIPECONF_12BPC;
  6840. break;
  6841. default:
  6842. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6843. BUG();
  6844. }
  6845. if (intel_crtc->config->dither)
  6846. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6847. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6848. val |= PIPECONF_INTERLACED_ILK;
  6849. else
  6850. val |= PIPECONF_PROGRESSIVE;
  6851. if (intel_crtc->config->limited_color_range)
  6852. val |= PIPECONF_COLOR_RANGE_SELECT;
  6853. I915_WRITE(PIPECONF(pipe), val);
  6854. POSTING_READ(PIPECONF(pipe));
  6855. }
  6856. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6857. {
  6858. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6859. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6860. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6861. u32 val = 0;
  6862. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6863. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6864. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6865. val |= PIPECONF_INTERLACED_ILK;
  6866. else
  6867. val |= PIPECONF_PROGRESSIVE;
  6868. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6869. POSTING_READ(PIPECONF(cpu_transcoder));
  6870. }
  6871. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6872. {
  6873. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6875. struct intel_crtc_state *config = intel_crtc->config;
  6876. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6877. u32 val = 0;
  6878. switch (intel_crtc->config->pipe_bpp) {
  6879. case 18:
  6880. val |= PIPEMISC_DITHER_6_BPC;
  6881. break;
  6882. case 24:
  6883. val |= PIPEMISC_DITHER_8_BPC;
  6884. break;
  6885. case 30:
  6886. val |= PIPEMISC_DITHER_10_BPC;
  6887. break;
  6888. case 36:
  6889. val |= PIPEMISC_DITHER_12_BPC;
  6890. break;
  6891. default:
  6892. /* Case prevented by pipe_config_set_bpp. */
  6893. BUG();
  6894. }
  6895. if (intel_crtc->config->dither)
  6896. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6897. if (config->ycbcr420) {
  6898. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6899. PIPEMISC_YUV420_ENABLE |
  6900. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6901. }
  6902. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6903. }
  6904. }
  6905. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6906. {
  6907. /*
  6908. * Account for spread spectrum to avoid
  6909. * oversubscribing the link. Max center spread
  6910. * is 2.5%; use 5% for safety's sake.
  6911. */
  6912. u32 bps = target_clock * bpp * 21 / 20;
  6913. return DIV_ROUND_UP(bps, link_bw * 8);
  6914. }
  6915. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6916. {
  6917. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6918. }
  6919. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6920. struct intel_crtc_state *crtc_state,
  6921. struct dpll *reduced_clock)
  6922. {
  6923. struct drm_crtc *crtc = &intel_crtc->base;
  6924. struct drm_device *dev = crtc->dev;
  6925. struct drm_i915_private *dev_priv = to_i915(dev);
  6926. u32 dpll, fp, fp2;
  6927. int factor;
  6928. /* Enable autotuning of the PLL clock (if permissible) */
  6929. factor = 21;
  6930. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6931. if ((intel_panel_use_ssc(dev_priv) &&
  6932. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6933. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6934. factor = 25;
  6935. } else if (crtc_state->sdvo_tv_clock)
  6936. factor = 20;
  6937. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6938. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6939. fp |= FP_CB_TUNE;
  6940. if (reduced_clock) {
  6941. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6942. if (reduced_clock->m < factor * reduced_clock->n)
  6943. fp2 |= FP_CB_TUNE;
  6944. } else {
  6945. fp2 = fp;
  6946. }
  6947. dpll = 0;
  6948. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6949. dpll |= DPLLB_MODE_LVDS;
  6950. else
  6951. dpll |= DPLLB_MODE_DAC_SERIAL;
  6952. dpll |= (crtc_state->pixel_multiplier - 1)
  6953. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6954. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6955. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6956. dpll |= DPLL_SDVO_HIGH_SPEED;
  6957. if (intel_crtc_has_dp_encoder(crtc_state))
  6958. dpll |= DPLL_SDVO_HIGH_SPEED;
  6959. /*
  6960. * The high speed IO clock is only really required for
  6961. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6962. * possible to share the DPLL between CRT and HDMI. Enabling
  6963. * the clock needlessly does no real harm, except use up a
  6964. * bit of power potentially.
  6965. *
  6966. * We'll limit this to IVB with 3 pipes, since it has only two
  6967. * DPLLs and so DPLL sharing is the only way to get three pipes
  6968. * driving PCH ports at the same time. On SNB we could do this,
  6969. * and potentially avoid enabling the second DPLL, but it's not
  6970. * clear if it''s a win or loss power wise. No point in doing
  6971. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6972. */
  6973. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6974. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6975. dpll |= DPLL_SDVO_HIGH_SPEED;
  6976. /* compute bitmask from p1 value */
  6977. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6978. /* also FPA1 */
  6979. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6980. switch (crtc_state->dpll.p2) {
  6981. case 5:
  6982. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6983. break;
  6984. case 7:
  6985. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6986. break;
  6987. case 10:
  6988. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6989. break;
  6990. case 14:
  6991. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6992. break;
  6993. }
  6994. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6995. intel_panel_use_ssc(dev_priv))
  6996. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6997. else
  6998. dpll |= PLL_REF_INPUT_DREFCLK;
  6999. dpll |= DPLL_VCO_ENABLE;
  7000. crtc_state->dpll_hw_state.dpll = dpll;
  7001. crtc_state->dpll_hw_state.fp0 = fp;
  7002. crtc_state->dpll_hw_state.fp1 = fp2;
  7003. }
  7004. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7005. struct intel_crtc_state *crtc_state)
  7006. {
  7007. struct drm_device *dev = crtc->base.dev;
  7008. struct drm_i915_private *dev_priv = to_i915(dev);
  7009. const struct intel_limit *limit;
  7010. int refclk = 120000;
  7011. memset(&crtc_state->dpll_hw_state, 0,
  7012. sizeof(crtc_state->dpll_hw_state));
  7013. crtc->lowfreq_avail = false;
  7014. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7015. if (!crtc_state->has_pch_encoder)
  7016. return 0;
  7017. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7018. if (intel_panel_use_ssc(dev_priv)) {
  7019. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7020. dev_priv->vbt.lvds_ssc_freq);
  7021. refclk = dev_priv->vbt.lvds_ssc_freq;
  7022. }
  7023. if (intel_is_dual_link_lvds(dev)) {
  7024. if (refclk == 100000)
  7025. limit = &intel_limits_ironlake_dual_lvds_100m;
  7026. else
  7027. limit = &intel_limits_ironlake_dual_lvds;
  7028. } else {
  7029. if (refclk == 100000)
  7030. limit = &intel_limits_ironlake_single_lvds_100m;
  7031. else
  7032. limit = &intel_limits_ironlake_single_lvds;
  7033. }
  7034. } else {
  7035. limit = &intel_limits_ironlake_dac;
  7036. }
  7037. if (!crtc_state->clock_set &&
  7038. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7039. refclk, NULL, &crtc_state->dpll)) {
  7040. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7041. return -EINVAL;
  7042. }
  7043. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7044. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7045. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7046. pipe_name(crtc->pipe));
  7047. return -EINVAL;
  7048. }
  7049. return 0;
  7050. }
  7051. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7052. struct intel_link_m_n *m_n)
  7053. {
  7054. struct drm_device *dev = crtc->base.dev;
  7055. struct drm_i915_private *dev_priv = to_i915(dev);
  7056. enum pipe pipe = crtc->pipe;
  7057. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7058. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7059. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7060. & ~TU_SIZE_MASK;
  7061. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7062. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7063. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7064. }
  7065. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7066. enum transcoder transcoder,
  7067. struct intel_link_m_n *m_n,
  7068. struct intel_link_m_n *m2_n2)
  7069. {
  7070. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7071. enum pipe pipe = crtc->pipe;
  7072. if (INTEL_GEN(dev_priv) >= 5) {
  7073. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7074. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7075. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7076. & ~TU_SIZE_MASK;
  7077. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7078. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7079. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7080. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7081. * gen < 8) and if DRRS is supported (to make sure the
  7082. * registers are not unnecessarily read).
  7083. */
  7084. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7085. crtc->config->has_drrs) {
  7086. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7087. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7088. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7089. & ~TU_SIZE_MASK;
  7090. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7091. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7092. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7093. }
  7094. } else {
  7095. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7096. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7097. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7098. & ~TU_SIZE_MASK;
  7099. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7100. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7101. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7102. }
  7103. }
  7104. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7105. struct intel_crtc_state *pipe_config)
  7106. {
  7107. if (pipe_config->has_pch_encoder)
  7108. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7109. else
  7110. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7111. &pipe_config->dp_m_n,
  7112. &pipe_config->dp_m2_n2);
  7113. }
  7114. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7115. struct intel_crtc_state *pipe_config)
  7116. {
  7117. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7118. &pipe_config->fdi_m_n, NULL);
  7119. }
  7120. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7121. struct intel_crtc_state *pipe_config)
  7122. {
  7123. struct drm_device *dev = crtc->base.dev;
  7124. struct drm_i915_private *dev_priv = to_i915(dev);
  7125. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7126. uint32_t ps_ctrl = 0;
  7127. int id = -1;
  7128. int i;
  7129. /* find scaler attached to this pipe */
  7130. for (i = 0; i < crtc->num_scalers; i++) {
  7131. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7132. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7133. id = i;
  7134. pipe_config->pch_pfit.enabled = true;
  7135. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7136. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7137. break;
  7138. }
  7139. }
  7140. scaler_state->scaler_id = id;
  7141. if (id >= 0) {
  7142. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7143. } else {
  7144. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7145. }
  7146. }
  7147. static void
  7148. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7149. struct intel_initial_plane_config *plane_config)
  7150. {
  7151. struct drm_device *dev = crtc->base.dev;
  7152. struct drm_i915_private *dev_priv = to_i915(dev);
  7153. u32 val, base, offset, stride_mult, tiling;
  7154. int pipe = crtc->pipe;
  7155. int fourcc, pixel_format;
  7156. unsigned int aligned_height;
  7157. struct drm_framebuffer *fb;
  7158. struct intel_framebuffer *intel_fb;
  7159. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7160. if (!intel_fb) {
  7161. DRM_DEBUG_KMS("failed to alloc fb\n");
  7162. return;
  7163. }
  7164. fb = &intel_fb->base;
  7165. fb->dev = dev;
  7166. val = I915_READ(PLANE_CTL(pipe, 0));
  7167. if (!(val & PLANE_CTL_ENABLE))
  7168. goto error;
  7169. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7170. fourcc = skl_format_to_fourcc(pixel_format,
  7171. val & PLANE_CTL_ORDER_RGBX,
  7172. val & PLANE_CTL_ALPHA_MASK);
  7173. fb->format = drm_format_info(fourcc);
  7174. tiling = val & PLANE_CTL_TILED_MASK;
  7175. switch (tiling) {
  7176. case PLANE_CTL_TILED_LINEAR:
  7177. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7178. break;
  7179. case PLANE_CTL_TILED_X:
  7180. plane_config->tiling = I915_TILING_X;
  7181. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7182. break;
  7183. case PLANE_CTL_TILED_Y:
  7184. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7185. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7186. else
  7187. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7188. break;
  7189. case PLANE_CTL_TILED_YF:
  7190. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7191. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7192. else
  7193. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7194. break;
  7195. default:
  7196. MISSING_CASE(tiling);
  7197. goto error;
  7198. }
  7199. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7200. plane_config->base = base;
  7201. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7202. val = I915_READ(PLANE_SIZE(pipe, 0));
  7203. fb->height = ((val >> 16) & 0xfff) + 1;
  7204. fb->width = ((val >> 0) & 0x1fff) + 1;
  7205. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7206. stride_mult = intel_fb_stride_alignment(fb, 0);
  7207. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7208. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7209. plane_config->size = fb->pitches[0] * aligned_height;
  7210. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7211. pipe_name(pipe), fb->width, fb->height,
  7212. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7213. plane_config->size);
  7214. plane_config->fb = intel_fb;
  7215. return;
  7216. error:
  7217. kfree(intel_fb);
  7218. }
  7219. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7220. struct intel_crtc_state *pipe_config)
  7221. {
  7222. struct drm_device *dev = crtc->base.dev;
  7223. struct drm_i915_private *dev_priv = to_i915(dev);
  7224. uint32_t tmp;
  7225. tmp = I915_READ(PF_CTL(crtc->pipe));
  7226. if (tmp & PF_ENABLE) {
  7227. pipe_config->pch_pfit.enabled = true;
  7228. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7229. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7230. /* We currently do not free assignements of panel fitters on
  7231. * ivb/hsw (since we don't use the higher upscaling modes which
  7232. * differentiates them) so just WARN about this case for now. */
  7233. if (IS_GEN7(dev_priv)) {
  7234. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7235. PF_PIPE_SEL_IVB(crtc->pipe));
  7236. }
  7237. }
  7238. }
  7239. static void
  7240. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7241. struct intel_initial_plane_config *plane_config)
  7242. {
  7243. struct drm_device *dev = crtc->base.dev;
  7244. struct drm_i915_private *dev_priv = to_i915(dev);
  7245. u32 val, base, offset;
  7246. int pipe = crtc->pipe;
  7247. int fourcc, pixel_format;
  7248. unsigned int aligned_height;
  7249. struct drm_framebuffer *fb;
  7250. struct intel_framebuffer *intel_fb;
  7251. val = I915_READ(DSPCNTR(pipe));
  7252. if (!(val & DISPLAY_PLANE_ENABLE))
  7253. return;
  7254. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7255. if (!intel_fb) {
  7256. DRM_DEBUG_KMS("failed to alloc fb\n");
  7257. return;
  7258. }
  7259. fb = &intel_fb->base;
  7260. fb->dev = dev;
  7261. if (INTEL_GEN(dev_priv) >= 4) {
  7262. if (val & DISPPLANE_TILED) {
  7263. plane_config->tiling = I915_TILING_X;
  7264. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7265. }
  7266. }
  7267. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7268. fourcc = i9xx_format_to_fourcc(pixel_format);
  7269. fb->format = drm_format_info(fourcc);
  7270. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7271. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7272. offset = I915_READ(DSPOFFSET(pipe));
  7273. } else {
  7274. if (plane_config->tiling)
  7275. offset = I915_READ(DSPTILEOFF(pipe));
  7276. else
  7277. offset = I915_READ(DSPLINOFF(pipe));
  7278. }
  7279. plane_config->base = base;
  7280. val = I915_READ(PIPESRC(pipe));
  7281. fb->width = ((val >> 16) & 0xfff) + 1;
  7282. fb->height = ((val >> 0) & 0xfff) + 1;
  7283. val = I915_READ(DSPSTRIDE(pipe));
  7284. fb->pitches[0] = val & 0xffffffc0;
  7285. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7286. plane_config->size = fb->pitches[0] * aligned_height;
  7287. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7288. pipe_name(pipe), fb->width, fb->height,
  7289. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7290. plane_config->size);
  7291. plane_config->fb = intel_fb;
  7292. }
  7293. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7294. struct intel_crtc_state *pipe_config)
  7295. {
  7296. struct drm_device *dev = crtc->base.dev;
  7297. struct drm_i915_private *dev_priv = to_i915(dev);
  7298. enum intel_display_power_domain power_domain;
  7299. uint32_t tmp;
  7300. bool ret;
  7301. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7302. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7303. return false;
  7304. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7305. pipe_config->shared_dpll = NULL;
  7306. ret = false;
  7307. tmp = I915_READ(PIPECONF(crtc->pipe));
  7308. if (!(tmp & PIPECONF_ENABLE))
  7309. goto out;
  7310. switch (tmp & PIPECONF_BPC_MASK) {
  7311. case PIPECONF_6BPC:
  7312. pipe_config->pipe_bpp = 18;
  7313. break;
  7314. case PIPECONF_8BPC:
  7315. pipe_config->pipe_bpp = 24;
  7316. break;
  7317. case PIPECONF_10BPC:
  7318. pipe_config->pipe_bpp = 30;
  7319. break;
  7320. case PIPECONF_12BPC:
  7321. pipe_config->pipe_bpp = 36;
  7322. break;
  7323. default:
  7324. break;
  7325. }
  7326. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7327. pipe_config->limited_color_range = true;
  7328. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7329. struct intel_shared_dpll *pll;
  7330. enum intel_dpll_id pll_id;
  7331. pipe_config->has_pch_encoder = true;
  7332. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7333. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7334. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7335. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7336. if (HAS_PCH_IBX(dev_priv)) {
  7337. /*
  7338. * The pipe->pch transcoder and pch transcoder->pll
  7339. * mapping is fixed.
  7340. */
  7341. pll_id = (enum intel_dpll_id) crtc->pipe;
  7342. } else {
  7343. tmp = I915_READ(PCH_DPLL_SEL);
  7344. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7345. pll_id = DPLL_ID_PCH_PLL_B;
  7346. else
  7347. pll_id= DPLL_ID_PCH_PLL_A;
  7348. }
  7349. pipe_config->shared_dpll =
  7350. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7351. pll = pipe_config->shared_dpll;
  7352. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7353. &pipe_config->dpll_hw_state));
  7354. tmp = pipe_config->dpll_hw_state.dpll;
  7355. pipe_config->pixel_multiplier =
  7356. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7357. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7358. ironlake_pch_clock_get(crtc, pipe_config);
  7359. } else {
  7360. pipe_config->pixel_multiplier = 1;
  7361. }
  7362. intel_get_pipe_timings(crtc, pipe_config);
  7363. intel_get_pipe_src_size(crtc, pipe_config);
  7364. ironlake_get_pfit_config(crtc, pipe_config);
  7365. ret = true;
  7366. out:
  7367. intel_display_power_put(dev_priv, power_domain);
  7368. return ret;
  7369. }
  7370. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7371. {
  7372. struct drm_device *dev = &dev_priv->drm;
  7373. struct intel_crtc *crtc;
  7374. for_each_intel_crtc(dev, crtc)
  7375. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7376. pipe_name(crtc->pipe));
  7377. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7378. "Display power well on\n");
  7379. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7380. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7381. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7382. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7383. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7384. "CPU PWM1 enabled\n");
  7385. if (IS_HASWELL(dev_priv))
  7386. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7387. "CPU PWM2 enabled\n");
  7388. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7389. "PCH PWM1 enabled\n");
  7390. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7391. "Utility pin enabled\n");
  7392. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7393. /*
  7394. * In theory we can still leave IRQs enabled, as long as only the HPD
  7395. * interrupts remain enabled. We used to check for that, but since it's
  7396. * gen-specific and since we only disable LCPLL after we fully disable
  7397. * the interrupts, the check below should be enough.
  7398. */
  7399. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7400. }
  7401. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7402. {
  7403. if (IS_HASWELL(dev_priv))
  7404. return I915_READ(D_COMP_HSW);
  7405. else
  7406. return I915_READ(D_COMP_BDW);
  7407. }
  7408. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7409. {
  7410. if (IS_HASWELL(dev_priv)) {
  7411. mutex_lock(&dev_priv->rps.hw_lock);
  7412. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7413. val))
  7414. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7415. mutex_unlock(&dev_priv->rps.hw_lock);
  7416. } else {
  7417. I915_WRITE(D_COMP_BDW, val);
  7418. POSTING_READ(D_COMP_BDW);
  7419. }
  7420. }
  7421. /*
  7422. * This function implements pieces of two sequences from BSpec:
  7423. * - Sequence for display software to disable LCPLL
  7424. * - Sequence for display software to allow package C8+
  7425. * The steps implemented here are just the steps that actually touch the LCPLL
  7426. * register. Callers should take care of disabling all the display engine
  7427. * functions, doing the mode unset, fixing interrupts, etc.
  7428. */
  7429. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7430. bool switch_to_fclk, bool allow_power_down)
  7431. {
  7432. uint32_t val;
  7433. assert_can_disable_lcpll(dev_priv);
  7434. val = I915_READ(LCPLL_CTL);
  7435. if (switch_to_fclk) {
  7436. val |= LCPLL_CD_SOURCE_FCLK;
  7437. I915_WRITE(LCPLL_CTL, val);
  7438. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7439. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7440. DRM_ERROR("Switching to FCLK failed\n");
  7441. val = I915_READ(LCPLL_CTL);
  7442. }
  7443. val |= LCPLL_PLL_DISABLE;
  7444. I915_WRITE(LCPLL_CTL, val);
  7445. POSTING_READ(LCPLL_CTL);
  7446. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7447. DRM_ERROR("LCPLL still locked\n");
  7448. val = hsw_read_dcomp(dev_priv);
  7449. val |= D_COMP_COMP_DISABLE;
  7450. hsw_write_dcomp(dev_priv, val);
  7451. ndelay(100);
  7452. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7453. 1))
  7454. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7455. if (allow_power_down) {
  7456. val = I915_READ(LCPLL_CTL);
  7457. val |= LCPLL_POWER_DOWN_ALLOW;
  7458. I915_WRITE(LCPLL_CTL, val);
  7459. POSTING_READ(LCPLL_CTL);
  7460. }
  7461. }
  7462. /*
  7463. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7464. * source.
  7465. */
  7466. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7467. {
  7468. uint32_t val;
  7469. val = I915_READ(LCPLL_CTL);
  7470. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7471. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7472. return;
  7473. /*
  7474. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7475. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7476. */
  7477. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7478. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7479. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7480. I915_WRITE(LCPLL_CTL, val);
  7481. POSTING_READ(LCPLL_CTL);
  7482. }
  7483. val = hsw_read_dcomp(dev_priv);
  7484. val |= D_COMP_COMP_FORCE;
  7485. val &= ~D_COMP_COMP_DISABLE;
  7486. hsw_write_dcomp(dev_priv, val);
  7487. val = I915_READ(LCPLL_CTL);
  7488. val &= ~LCPLL_PLL_DISABLE;
  7489. I915_WRITE(LCPLL_CTL, val);
  7490. if (intel_wait_for_register(dev_priv,
  7491. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7492. 5))
  7493. DRM_ERROR("LCPLL not locked yet\n");
  7494. if (val & LCPLL_CD_SOURCE_FCLK) {
  7495. val = I915_READ(LCPLL_CTL);
  7496. val &= ~LCPLL_CD_SOURCE_FCLK;
  7497. I915_WRITE(LCPLL_CTL, val);
  7498. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7499. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7500. DRM_ERROR("Switching back to LCPLL failed\n");
  7501. }
  7502. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7503. intel_update_cdclk(dev_priv);
  7504. }
  7505. /*
  7506. * Package states C8 and deeper are really deep PC states that can only be
  7507. * reached when all the devices on the system allow it, so even if the graphics
  7508. * device allows PC8+, it doesn't mean the system will actually get to these
  7509. * states. Our driver only allows PC8+ when going into runtime PM.
  7510. *
  7511. * The requirements for PC8+ are that all the outputs are disabled, the power
  7512. * well is disabled and most interrupts are disabled, and these are also
  7513. * requirements for runtime PM. When these conditions are met, we manually do
  7514. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7515. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7516. * hang the machine.
  7517. *
  7518. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7519. * the state of some registers, so when we come back from PC8+ we need to
  7520. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7521. * need to take care of the registers kept by RC6. Notice that this happens even
  7522. * if we don't put the device in PCI D3 state (which is what currently happens
  7523. * because of the runtime PM support).
  7524. *
  7525. * For more, read "Display Sequences for Package C8" on the hardware
  7526. * documentation.
  7527. */
  7528. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7529. {
  7530. uint32_t val;
  7531. DRM_DEBUG_KMS("Enabling package C8+\n");
  7532. if (HAS_PCH_LPT_LP(dev_priv)) {
  7533. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7534. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7535. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7536. }
  7537. lpt_disable_clkout_dp(dev_priv);
  7538. hsw_disable_lcpll(dev_priv, true, true);
  7539. }
  7540. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7541. {
  7542. uint32_t val;
  7543. DRM_DEBUG_KMS("Disabling package C8+\n");
  7544. hsw_restore_lcpll(dev_priv);
  7545. lpt_init_pch_refclk(dev_priv);
  7546. if (HAS_PCH_LPT_LP(dev_priv)) {
  7547. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7548. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7549. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7550. }
  7551. }
  7552. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7553. struct intel_crtc_state *crtc_state)
  7554. {
  7555. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7556. struct intel_encoder *encoder =
  7557. intel_ddi_get_crtc_new_encoder(crtc_state);
  7558. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7559. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7560. pipe_name(crtc->pipe));
  7561. return -EINVAL;
  7562. }
  7563. }
  7564. crtc->lowfreq_avail = false;
  7565. return 0;
  7566. }
  7567. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7568. enum port port,
  7569. struct intel_crtc_state *pipe_config)
  7570. {
  7571. enum intel_dpll_id id;
  7572. u32 temp;
  7573. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7574. id = temp >> (port * 2);
  7575. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7576. return;
  7577. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7578. }
  7579. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7580. enum port port,
  7581. struct intel_crtc_state *pipe_config)
  7582. {
  7583. enum intel_dpll_id id;
  7584. switch (port) {
  7585. case PORT_A:
  7586. id = DPLL_ID_SKL_DPLL0;
  7587. break;
  7588. case PORT_B:
  7589. id = DPLL_ID_SKL_DPLL1;
  7590. break;
  7591. case PORT_C:
  7592. id = DPLL_ID_SKL_DPLL2;
  7593. break;
  7594. default:
  7595. DRM_ERROR("Incorrect port type\n");
  7596. return;
  7597. }
  7598. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7599. }
  7600. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7601. enum port port,
  7602. struct intel_crtc_state *pipe_config)
  7603. {
  7604. enum intel_dpll_id id;
  7605. u32 temp;
  7606. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7607. id = temp >> (port * 3 + 1);
  7608. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7609. return;
  7610. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7611. }
  7612. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7613. enum port port,
  7614. struct intel_crtc_state *pipe_config)
  7615. {
  7616. enum intel_dpll_id id;
  7617. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7618. switch (ddi_pll_sel) {
  7619. case PORT_CLK_SEL_WRPLL1:
  7620. id = DPLL_ID_WRPLL1;
  7621. break;
  7622. case PORT_CLK_SEL_WRPLL2:
  7623. id = DPLL_ID_WRPLL2;
  7624. break;
  7625. case PORT_CLK_SEL_SPLL:
  7626. id = DPLL_ID_SPLL;
  7627. break;
  7628. case PORT_CLK_SEL_LCPLL_810:
  7629. id = DPLL_ID_LCPLL_810;
  7630. break;
  7631. case PORT_CLK_SEL_LCPLL_1350:
  7632. id = DPLL_ID_LCPLL_1350;
  7633. break;
  7634. case PORT_CLK_SEL_LCPLL_2700:
  7635. id = DPLL_ID_LCPLL_2700;
  7636. break;
  7637. default:
  7638. MISSING_CASE(ddi_pll_sel);
  7639. /* fall through */
  7640. case PORT_CLK_SEL_NONE:
  7641. return;
  7642. }
  7643. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7644. }
  7645. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7646. struct intel_crtc_state *pipe_config,
  7647. u64 *power_domain_mask)
  7648. {
  7649. struct drm_device *dev = crtc->base.dev;
  7650. struct drm_i915_private *dev_priv = to_i915(dev);
  7651. enum intel_display_power_domain power_domain;
  7652. u32 tmp;
  7653. /*
  7654. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7655. * transcoder handled below.
  7656. */
  7657. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7658. /*
  7659. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7660. * consistency and less surprising code; it's in always on power).
  7661. */
  7662. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7663. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7664. enum pipe trans_edp_pipe;
  7665. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7666. default:
  7667. WARN(1, "unknown pipe linked to edp transcoder\n");
  7668. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7669. case TRANS_DDI_EDP_INPUT_A_ON:
  7670. trans_edp_pipe = PIPE_A;
  7671. break;
  7672. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7673. trans_edp_pipe = PIPE_B;
  7674. break;
  7675. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7676. trans_edp_pipe = PIPE_C;
  7677. break;
  7678. }
  7679. if (trans_edp_pipe == crtc->pipe)
  7680. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7681. }
  7682. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7683. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7684. return false;
  7685. *power_domain_mask |= BIT_ULL(power_domain);
  7686. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7687. return tmp & PIPECONF_ENABLE;
  7688. }
  7689. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7690. struct intel_crtc_state *pipe_config,
  7691. u64 *power_domain_mask)
  7692. {
  7693. struct drm_device *dev = crtc->base.dev;
  7694. struct drm_i915_private *dev_priv = to_i915(dev);
  7695. enum intel_display_power_domain power_domain;
  7696. enum port port;
  7697. enum transcoder cpu_transcoder;
  7698. u32 tmp;
  7699. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7700. if (port == PORT_A)
  7701. cpu_transcoder = TRANSCODER_DSI_A;
  7702. else
  7703. cpu_transcoder = TRANSCODER_DSI_C;
  7704. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7705. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7706. continue;
  7707. *power_domain_mask |= BIT_ULL(power_domain);
  7708. /*
  7709. * The PLL needs to be enabled with a valid divider
  7710. * configuration, otherwise accessing DSI registers will hang
  7711. * the machine. See BSpec North Display Engine
  7712. * registers/MIPI[BXT]. We can break out here early, since we
  7713. * need the same DSI PLL to be enabled for both DSI ports.
  7714. */
  7715. if (!intel_dsi_pll_is_enabled(dev_priv))
  7716. break;
  7717. /* XXX: this works for video mode only */
  7718. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7719. if (!(tmp & DPI_ENABLE))
  7720. continue;
  7721. tmp = I915_READ(MIPI_CTRL(port));
  7722. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7723. continue;
  7724. pipe_config->cpu_transcoder = cpu_transcoder;
  7725. break;
  7726. }
  7727. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7728. }
  7729. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7730. struct intel_crtc_state *pipe_config)
  7731. {
  7732. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7733. struct intel_shared_dpll *pll;
  7734. enum port port;
  7735. uint32_t tmp;
  7736. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7737. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7738. if (IS_CANNONLAKE(dev_priv))
  7739. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7740. else if (IS_GEN9_BC(dev_priv))
  7741. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7742. else if (IS_GEN9_LP(dev_priv))
  7743. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7744. else
  7745. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7746. pll = pipe_config->shared_dpll;
  7747. if (pll) {
  7748. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7749. &pipe_config->dpll_hw_state));
  7750. }
  7751. /*
  7752. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7753. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7754. * the PCH transcoder is on.
  7755. */
  7756. if (INTEL_GEN(dev_priv) < 9 &&
  7757. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7758. pipe_config->has_pch_encoder = true;
  7759. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7760. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7761. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7762. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7763. }
  7764. }
  7765. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7766. struct intel_crtc_state *pipe_config)
  7767. {
  7768. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7769. enum intel_display_power_domain power_domain;
  7770. u64 power_domain_mask;
  7771. bool active;
  7772. intel_crtc_init_scalers(crtc, pipe_config);
  7773. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7774. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7775. return false;
  7776. power_domain_mask = BIT_ULL(power_domain);
  7777. pipe_config->shared_dpll = NULL;
  7778. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7779. if (IS_GEN9_LP(dev_priv) &&
  7780. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7781. WARN_ON(active);
  7782. active = true;
  7783. }
  7784. if (!active)
  7785. goto out;
  7786. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7787. haswell_get_ddi_port_state(crtc, pipe_config);
  7788. intel_get_pipe_timings(crtc, pipe_config);
  7789. }
  7790. intel_get_pipe_src_size(crtc, pipe_config);
  7791. pipe_config->gamma_mode =
  7792. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7793. if (IS_BROADWELL(dev_priv) || dev_priv->info.gen >= 9) {
  7794. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7795. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7796. if (IS_GEMINILAKE(dev_priv) || dev_priv->info.gen >= 10) {
  7797. bool blend_mode_420 = tmp &
  7798. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7799. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7800. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7801. pipe_config->ycbcr420 != blend_mode_420)
  7802. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7803. } else if (clrspace_yuv) {
  7804. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7805. }
  7806. }
  7807. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7808. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7809. power_domain_mask |= BIT_ULL(power_domain);
  7810. if (INTEL_GEN(dev_priv) >= 9)
  7811. skylake_get_pfit_config(crtc, pipe_config);
  7812. else
  7813. ironlake_get_pfit_config(crtc, pipe_config);
  7814. }
  7815. if (IS_HASWELL(dev_priv))
  7816. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7817. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7818. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7819. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7820. pipe_config->pixel_multiplier =
  7821. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7822. } else {
  7823. pipe_config->pixel_multiplier = 1;
  7824. }
  7825. out:
  7826. for_each_power_domain(power_domain, power_domain_mask)
  7827. intel_display_power_put(dev_priv, power_domain);
  7828. return active;
  7829. }
  7830. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7831. {
  7832. struct drm_i915_private *dev_priv =
  7833. to_i915(plane_state->base.plane->dev);
  7834. const struct drm_framebuffer *fb = plane_state->base.fb;
  7835. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7836. u32 base;
  7837. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7838. base = obj->phys_handle->busaddr;
  7839. else
  7840. base = intel_plane_ggtt_offset(plane_state);
  7841. base += plane_state->main.offset;
  7842. /* ILK+ do this automagically */
  7843. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7844. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7845. base += (plane_state->base.crtc_h *
  7846. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7847. return base;
  7848. }
  7849. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7850. {
  7851. int x = plane_state->base.crtc_x;
  7852. int y = plane_state->base.crtc_y;
  7853. u32 pos = 0;
  7854. if (x < 0) {
  7855. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7856. x = -x;
  7857. }
  7858. pos |= x << CURSOR_X_SHIFT;
  7859. if (y < 0) {
  7860. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7861. y = -y;
  7862. }
  7863. pos |= y << CURSOR_Y_SHIFT;
  7864. return pos;
  7865. }
  7866. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7867. {
  7868. const struct drm_mode_config *config =
  7869. &plane_state->base.plane->dev->mode_config;
  7870. int width = plane_state->base.crtc_w;
  7871. int height = plane_state->base.crtc_h;
  7872. return width > 0 && width <= config->cursor_width &&
  7873. height > 0 && height <= config->cursor_height;
  7874. }
  7875. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7876. struct intel_plane_state *plane_state)
  7877. {
  7878. const struct drm_framebuffer *fb = plane_state->base.fb;
  7879. int src_x, src_y;
  7880. u32 offset;
  7881. int ret;
  7882. ret = drm_plane_helper_check_state(&plane_state->base,
  7883. &plane_state->clip,
  7884. DRM_PLANE_HELPER_NO_SCALING,
  7885. DRM_PLANE_HELPER_NO_SCALING,
  7886. true, true);
  7887. if (ret)
  7888. return ret;
  7889. if (!fb)
  7890. return 0;
  7891. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7892. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7893. return -EINVAL;
  7894. }
  7895. src_x = plane_state->base.src_x >> 16;
  7896. src_y = plane_state->base.src_y >> 16;
  7897. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7898. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7899. if (src_x != 0 || src_y != 0) {
  7900. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7901. return -EINVAL;
  7902. }
  7903. plane_state->main.offset = offset;
  7904. return 0;
  7905. }
  7906. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7907. const struct intel_plane_state *plane_state)
  7908. {
  7909. const struct drm_framebuffer *fb = plane_state->base.fb;
  7910. return CURSOR_ENABLE |
  7911. CURSOR_GAMMA_ENABLE |
  7912. CURSOR_FORMAT_ARGB |
  7913. CURSOR_STRIDE(fb->pitches[0]);
  7914. }
  7915. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7916. {
  7917. int width = plane_state->base.crtc_w;
  7918. /*
  7919. * 845g/865g are only limited by the width of their cursors,
  7920. * the height is arbitrary up to the precision of the register.
  7921. */
  7922. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7923. }
  7924. static int i845_check_cursor(struct intel_plane *plane,
  7925. struct intel_crtc_state *crtc_state,
  7926. struct intel_plane_state *plane_state)
  7927. {
  7928. const struct drm_framebuffer *fb = plane_state->base.fb;
  7929. int ret;
  7930. ret = intel_check_cursor(crtc_state, plane_state);
  7931. if (ret)
  7932. return ret;
  7933. /* if we want to turn off the cursor ignore width and height */
  7934. if (!fb)
  7935. return 0;
  7936. /* Check for which cursor types we support */
  7937. if (!i845_cursor_size_ok(plane_state)) {
  7938. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7939. plane_state->base.crtc_w,
  7940. plane_state->base.crtc_h);
  7941. return -EINVAL;
  7942. }
  7943. switch (fb->pitches[0]) {
  7944. case 256:
  7945. case 512:
  7946. case 1024:
  7947. case 2048:
  7948. break;
  7949. default:
  7950. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7951. fb->pitches[0]);
  7952. return -EINVAL;
  7953. }
  7954. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7955. return 0;
  7956. }
  7957. static void i845_update_cursor(struct intel_plane *plane,
  7958. const struct intel_crtc_state *crtc_state,
  7959. const struct intel_plane_state *plane_state)
  7960. {
  7961. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7962. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7963. unsigned long irqflags;
  7964. if (plane_state && plane_state->base.visible) {
  7965. unsigned int width = plane_state->base.crtc_w;
  7966. unsigned int height = plane_state->base.crtc_h;
  7967. cntl = plane_state->ctl;
  7968. size = (height << 12) | width;
  7969. base = intel_cursor_base(plane_state);
  7970. pos = intel_cursor_position(plane_state);
  7971. }
  7972. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7973. /* On these chipsets we can only modify the base/size/stride
  7974. * whilst the cursor is disabled.
  7975. */
  7976. if (plane->cursor.base != base ||
  7977. plane->cursor.size != size ||
  7978. plane->cursor.cntl != cntl) {
  7979. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7980. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7981. I915_WRITE_FW(CURSIZE, size);
  7982. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7983. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7984. plane->cursor.base = base;
  7985. plane->cursor.size = size;
  7986. plane->cursor.cntl = cntl;
  7987. } else {
  7988. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7989. }
  7990. POSTING_READ_FW(CURCNTR(PIPE_A));
  7991. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7992. }
  7993. static void i845_disable_cursor(struct intel_plane *plane,
  7994. struct intel_crtc *crtc)
  7995. {
  7996. i845_update_cursor(plane, NULL, NULL);
  7997. }
  7998. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7999. const struct intel_plane_state *plane_state)
  8000. {
  8001. struct drm_i915_private *dev_priv =
  8002. to_i915(plane_state->base.plane->dev);
  8003. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8004. u32 cntl;
  8005. cntl = MCURSOR_GAMMA_ENABLE;
  8006. if (HAS_DDI(dev_priv))
  8007. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8008. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8009. switch (plane_state->base.crtc_w) {
  8010. case 64:
  8011. cntl |= CURSOR_MODE_64_ARGB_AX;
  8012. break;
  8013. case 128:
  8014. cntl |= CURSOR_MODE_128_ARGB_AX;
  8015. break;
  8016. case 256:
  8017. cntl |= CURSOR_MODE_256_ARGB_AX;
  8018. break;
  8019. default:
  8020. MISSING_CASE(plane_state->base.crtc_w);
  8021. return 0;
  8022. }
  8023. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8024. cntl |= CURSOR_ROTATE_180;
  8025. return cntl;
  8026. }
  8027. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8028. {
  8029. struct drm_i915_private *dev_priv =
  8030. to_i915(plane_state->base.plane->dev);
  8031. int width = plane_state->base.crtc_w;
  8032. int height = plane_state->base.crtc_h;
  8033. if (!intel_cursor_size_ok(plane_state))
  8034. return false;
  8035. /* Cursor width is limited to a few power-of-two sizes */
  8036. switch (width) {
  8037. case 256:
  8038. case 128:
  8039. case 64:
  8040. break;
  8041. default:
  8042. return false;
  8043. }
  8044. /*
  8045. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8046. * height from 8 lines up to the cursor width, when the
  8047. * cursor is not rotated. Everything else requires square
  8048. * cursors.
  8049. */
  8050. if (HAS_CUR_FBC(dev_priv) &&
  8051. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8052. if (height < 8 || height > width)
  8053. return false;
  8054. } else {
  8055. if (height != width)
  8056. return false;
  8057. }
  8058. return true;
  8059. }
  8060. static int i9xx_check_cursor(struct intel_plane *plane,
  8061. struct intel_crtc_state *crtc_state,
  8062. struct intel_plane_state *plane_state)
  8063. {
  8064. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8065. const struct drm_framebuffer *fb = plane_state->base.fb;
  8066. enum pipe pipe = plane->pipe;
  8067. int ret;
  8068. ret = intel_check_cursor(crtc_state, plane_state);
  8069. if (ret)
  8070. return ret;
  8071. /* if we want to turn off the cursor ignore width and height */
  8072. if (!fb)
  8073. return 0;
  8074. /* Check for which cursor types we support */
  8075. if (!i9xx_cursor_size_ok(plane_state)) {
  8076. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8077. plane_state->base.crtc_w,
  8078. plane_state->base.crtc_h);
  8079. return -EINVAL;
  8080. }
  8081. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8082. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8083. fb->pitches[0], plane_state->base.crtc_w);
  8084. return -EINVAL;
  8085. }
  8086. /*
  8087. * There's something wrong with the cursor on CHV pipe C.
  8088. * If it straddles the left edge of the screen then
  8089. * moving it away from the edge or disabling it often
  8090. * results in a pipe underrun, and often that can lead to
  8091. * dead pipe (constant underrun reported, and it scans
  8092. * out just a solid color). To recover from that, the
  8093. * display power well must be turned off and on again.
  8094. * Refuse the put the cursor into that compromised position.
  8095. */
  8096. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8097. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8098. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8099. return -EINVAL;
  8100. }
  8101. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8102. return 0;
  8103. }
  8104. static void i9xx_update_cursor(struct intel_plane *plane,
  8105. const struct intel_crtc_state *crtc_state,
  8106. const struct intel_plane_state *plane_state)
  8107. {
  8108. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8109. enum pipe pipe = plane->pipe;
  8110. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8111. unsigned long irqflags;
  8112. if (plane_state && plane_state->base.visible) {
  8113. cntl = plane_state->ctl;
  8114. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8115. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8116. base = intel_cursor_base(plane_state);
  8117. pos = intel_cursor_position(plane_state);
  8118. }
  8119. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8120. /*
  8121. * On some platforms writing CURCNTR first will also
  8122. * cause CURPOS to be armed by the CURBASE write.
  8123. * Without the CURCNTR write the CURPOS write would
  8124. * arm itself. Thus we always start the full update
  8125. * with a CURCNTR write.
  8126. *
  8127. * On other platforms CURPOS always requires the
  8128. * CURBASE write to arm the update. Additonally
  8129. * a write to any of the cursor register will cancel
  8130. * an already armed cursor update. Thus leaving out
  8131. * the CURBASE write after CURPOS could lead to a
  8132. * cursor that doesn't appear to move, or even change
  8133. * shape. Thus we always write CURBASE.
  8134. *
  8135. * CURCNTR and CUR_FBC_CTL are always
  8136. * armed by the CURBASE write only.
  8137. */
  8138. if (plane->cursor.base != base ||
  8139. plane->cursor.size != fbc_ctl ||
  8140. plane->cursor.cntl != cntl) {
  8141. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8142. if (HAS_CUR_FBC(dev_priv))
  8143. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8144. I915_WRITE_FW(CURPOS(pipe), pos);
  8145. I915_WRITE_FW(CURBASE(pipe), base);
  8146. plane->cursor.base = base;
  8147. plane->cursor.size = fbc_ctl;
  8148. plane->cursor.cntl = cntl;
  8149. } else {
  8150. I915_WRITE_FW(CURPOS(pipe), pos);
  8151. I915_WRITE_FW(CURBASE(pipe), base);
  8152. }
  8153. POSTING_READ_FW(CURBASE(pipe));
  8154. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8155. }
  8156. static void i9xx_disable_cursor(struct intel_plane *plane,
  8157. struct intel_crtc *crtc)
  8158. {
  8159. i9xx_update_cursor(plane, NULL, NULL);
  8160. }
  8161. /* VESA 640x480x72Hz mode to set on the pipe */
  8162. static struct drm_display_mode load_detect_mode = {
  8163. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8164. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8165. };
  8166. struct drm_framebuffer *
  8167. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8168. struct drm_mode_fb_cmd2 *mode_cmd)
  8169. {
  8170. struct intel_framebuffer *intel_fb;
  8171. int ret;
  8172. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8173. if (!intel_fb)
  8174. return ERR_PTR(-ENOMEM);
  8175. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8176. if (ret)
  8177. goto err;
  8178. return &intel_fb->base;
  8179. err:
  8180. kfree(intel_fb);
  8181. return ERR_PTR(ret);
  8182. }
  8183. static u32
  8184. intel_framebuffer_pitch_for_width(int width, int bpp)
  8185. {
  8186. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8187. return ALIGN(pitch, 64);
  8188. }
  8189. static u32
  8190. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8191. {
  8192. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8193. return PAGE_ALIGN(pitch * mode->vdisplay);
  8194. }
  8195. static struct drm_framebuffer *
  8196. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8197. struct drm_display_mode *mode,
  8198. int depth, int bpp)
  8199. {
  8200. struct drm_framebuffer *fb;
  8201. struct drm_i915_gem_object *obj;
  8202. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8203. obj = i915_gem_object_create(to_i915(dev),
  8204. intel_framebuffer_size_for_mode(mode, bpp));
  8205. if (IS_ERR(obj))
  8206. return ERR_CAST(obj);
  8207. mode_cmd.width = mode->hdisplay;
  8208. mode_cmd.height = mode->vdisplay;
  8209. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8210. bpp);
  8211. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8212. fb = intel_framebuffer_create(obj, &mode_cmd);
  8213. if (IS_ERR(fb))
  8214. i915_gem_object_put(obj);
  8215. return fb;
  8216. }
  8217. static struct drm_framebuffer *
  8218. mode_fits_in_fbdev(struct drm_device *dev,
  8219. struct drm_display_mode *mode)
  8220. {
  8221. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8222. struct drm_i915_private *dev_priv = to_i915(dev);
  8223. struct drm_i915_gem_object *obj;
  8224. struct drm_framebuffer *fb;
  8225. if (!dev_priv->fbdev)
  8226. return NULL;
  8227. if (!dev_priv->fbdev->fb)
  8228. return NULL;
  8229. obj = dev_priv->fbdev->fb->obj;
  8230. BUG_ON(!obj);
  8231. fb = &dev_priv->fbdev->fb->base;
  8232. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8233. fb->format->cpp[0] * 8))
  8234. return NULL;
  8235. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8236. return NULL;
  8237. drm_framebuffer_reference(fb);
  8238. return fb;
  8239. #else
  8240. return NULL;
  8241. #endif
  8242. }
  8243. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8244. struct drm_crtc *crtc,
  8245. struct drm_display_mode *mode,
  8246. struct drm_framebuffer *fb,
  8247. int x, int y)
  8248. {
  8249. struct drm_plane_state *plane_state;
  8250. int hdisplay, vdisplay;
  8251. int ret;
  8252. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8253. if (IS_ERR(plane_state))
  8254. return PTR_ERR(plane_state);
  8255. if (mode)
  8256. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  8257. else
  8258. hdisplay = vdisplay = 0;
  8259. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8260. if (ret)
  8261. return ret;
  8262. drm_atomic_set_fb_for_plane(plane_state, fb);
  8263. plane_state->crtc_x = 0;
  8264. plane_state->crtc_y = 0;
  8265. plane_state->crtc_w = hdisplay;
  8266. plane_state->crtc_h = vdisplay;
  8267. plane_state->src_x = x << 16;
  8268. plane_state->src_y = y << 16;
  8269. plane_state->src_w = hdisplay << 16;
  8270. plane_state->src_h = vdisplay << 16;
  8271. return 0;
  8272. }
  8273. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8274. struct drm_display_mode *mode,
  8275. struct intel_load_detect_pipe *old,
  8276. struct drm_modeset_acquire_ctx *ctx)
  8277. {
  8278. struct intel_crtc *intel_crtc;
  8279. struct intel_encoder *intel_encoder =
  8280. intel_attached_encoder(connector);
  8281. struct drm_crtc *possible_crtc;
  8282. struct drm_encoder *encoder = &intel_encoder->base;
  8283. struct drm_crtc *crtc = NULL;
  8284. struct drm_device *dev = encoder->dev;
  8285. struct drm_i915_private *dev_priv = to_i915(dev);
  8286. struct drm_framebuffer *fb;
  8287. struct drm_mode_config *config = &dev->mode_config;
  8288. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8289. struct drm_connector_state *connector_state;
  8290. struct intel_crtc_state *crtc_state;
  8291. int ret, i = -1;
  8292. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8293. connector->base.id, connector->name,
  8294. encoder->base.id, encoder->name);
  8295. old->restore_state = NULL;
  8296. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8297. /*
  8298. * Algorithm gets a little messy:
  8299. *
  8300. * - if the connector already has an assigned crtc, use it (but make
  8301. * sure it's on first)
  8302. *
  8303. * - try to find the first unused crtc that can drive this connector,
  8304. * and use that if we find one
  8305. */
  8306. /* See if we already have a CRTC for this connector */
  8307. if (connector->state->crtc) {
  8308. crtc = connector->state->crtc;
  8309. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8310. if (ret)
  8311. goto fail;
  8312. /* Make sure the crtc and connector are running */
  8313. goto found;
  8314. }
  8315. /* Find an unused one (if possible) */
  8316. for_each_crtc(dev, possible_crtc) {
  8317. i++;
  8318. if (!(encoder->possible_crtcs & (1 << i)))
  8319. continue;
  8320. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8321. if (ret)
  8322. goto fail;
  8323. if (possible_crtc->state->enable) {
  8324. drm_modeset_unlock(&possible_crtc->mutex);
  8325. continue;
  8326. }
  8327. crtc = possible_crtc;
  8328. break;
  8329. }
  8330. /*
  8331. * If we didn't find an unused CRTC, don't use any.
  8332. */
  8333. if (!crtc) {
  8334. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8335. ret = -ENODEV;
  8336. goto fail;
  8337. }
  8338. found:
  8339. intel_crtc = to_intel_crtc(crtc);
  8340. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8341. if (ret)
  8342. goto fail;
  8343. state = drm_atomic_state_alloc(dev);
  8344. restore_state = drm_atomic_state_alloc(dev);
  8345. if (!state || !restore_state) {
  8346. ret = -ENOMEM;
  8347. goto fail;
  8348. }
  8349. state->acquire_ctx = ctx;
  8350. restore_state->acquire_ctx = ctx;
  8351. connector_state = drm_atomic_get_connector_state(state, connector);
  8352. if (IS_ERR(connector_state)) {
  8353. ret = PTR_ERR(connector_state);
  8354. goto fail;
  8355. }
  8356. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8357. if (ret)
  8358. goto fail;
  8359. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8360. if (IS_ERR(crtc_state)) {
  8361. ret = PTR_ERR(crtc_state);
  8362. goto fail;
  8363. }
  8364. crtc_state->base.active = crtc_state->base.enable = true;
  8365. if (!mode)
  8366. mode = &load_detect_mode;
  8367. /* We need a framebuffer large enough to accommodate all accesses
  8368. * that the plane may generate whilst we perform load detection.
  8369. * We can not rely on the fbcon either being present (we get called
  8370. * during its initialisation to detect all boot displays, or it may
  8371. * not even exist) or that it is large enough to satisfy the
  8372. * requested mode.
  8373. */
  8374. fb = mode_fits_in_fbdev(dev, mode);
  8375. if (fb == NULL) {
  8376. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8377. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8378. } else
  8379. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8380. if (IS_ERR(fb)) {
  8381. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8382. ret = PTR_ERR(fb);
  8383. goto fail;
  8384. }
  8385. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8386. if (ret)
  8387. goto fail;
  8388. drm_framebuffer_unreference(fb);
  8389. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8390. if (ret)
  8391. goto fail;
  8392. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8393. if (!ret)
  8394. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8395. if (!ret)
  8396. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8397. if (ret) {
  8398. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8399. goto fail;
  8400. }
  8401. ret = drm_atomic_commit(state);
  8402. if (ret) {
  8403. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8404. goto fail;
  8405. }
  8406. old->restore_state = restore_state;
  8407. drm_atomic_state_put(state);
  8408. /* let the connector get through one full cycle before testing */
  8409. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8410. return true;
  8411. fail:
  8412. if (state) {
  8413. drm_atomic_state_put(state);
  8414. state = NULL;
  8415. }
  8416. if (restore_state) {
  8417. drm_atomic_state_put(restore_state);
  8418. restore_state = NULL;
  8419. }
  8420. if (ret == -EDEADLK)
  8421. return ret;
  8422. return false;
  8423. }
  8424. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8425. struct intel_load_detect_pipe *old,
  8426. struct drm_modeset_acquire_ctx *ctx)
  8427. {
  8428. struct intel_encoder *intel_encoder =
  8429. intel_attached_encoder(connector);
  8430. struct drm_encoder *encoder = &intel_encoder->base;
  8431. struct drm_atomic_state *state = old->restore_state;
  8432. int ret;
  8433. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8434. connector->base.id, connector->name,
  8435. encoder->base.id, encoder->name);
  8436. if (!state)
  8437. return;
  8438. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8439. if (ret)
  8440. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8441. drm_atomic_state_put(state);
  8442. }
  8443. static int i9xx_pll_refclk(struct drm_device *dev,
  8444. const struct intel_crtc_state *pipe_config)
  8445. {
  8446. struct drm_i915_private *dev_priv = to_i915(dev);
  8447. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8448. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8449. return dev_priv->vbt.lvds_ssc_freq;
  8450. else if (HAS_PCH_SPLIT(dev_priv))
  8451. return 120000;
  8452. else if (!IS_GEN2(dev_priv))
  8453. return 96000;
  8454. else
  8455. return 48000;
  8456. }
  8457. /* Returns the clock of the currently programmed mode of the given pipe. */
  8458. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8459. struct intel_crtc_state *pipe_config)
  8460. {
  8461. struct drm_device *dev = crtc->base.dev;
  8462. struct drm_i915_private *dev_priv = to_i915(dev);
  8463. int pipe = pipe_config->cpu_transcoder;
  8464. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8465. u32 fp;
  8466. struct dpll clock;
  8467. int port_clock;
  8468. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8469. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8470. fp = pipe_config->dpll_hw_state.fp0;
  8471. else
  8472. fp = pipe_config->dpll_hw_state.fp1;
  8473. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8474. if (IS_PINEVIEW(dev_priv)) {
  8475. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8476. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8477. } else {
  8478. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8479. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8480. }
  8481. if (!IS_GEN2(dev_priv)) {
  8482. if (IS_PINEVIEW(dev_priv))
  8483. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8484. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8485. else
  8486. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8487. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8488. switch (dpll & DPLL_MODE_MASK) {
  8489. case DPLLB_MODE_DAC_SERIAL:
  8490. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8491. 5 : 10;
  8492. break;
  8493. case DPLLB_MODE_LVDS:
  8494. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8495. 7 : 14;
  8496. break;
  8497. default:
  8498. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8499. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8500. return;
  8501. }
  8502. if (IS_PINEVIEW(dev_priv))
  8503. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8504. else
  8505. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8506. } else {
  8507. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8508. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8509. if (is_lvds) {
  8510. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8511. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8512. if (lvds & LVDS_CLKB_POWER_UP)
  8513. clock.p2 = 7;
  8514. else
  8515. clock.p2 = 14;
  8516. } else {
  8517. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8518. clock.p1 = 2;
  8519. else {
  8520. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8521. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8522. }
  8523. if (dpll & PLL_P2_DIVIDE_BY_4)
  8524. clock.p2 = 4;
  8525. else
  8526. clock.p2 = 2;
  8527. }
  8528. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8529. }
  8530. /*
  8531. * This value includes pixel_multiplier. We will use
  8532. * port_clock to compute adjusted_mode.crtc_clock in the
  8533. * encoder's get_config() function.
  8534. */
  8535. pipe_config->port_clock = port_clock;
  8536. }
  8537. int intel_dotclock_calculate(int link_freq,
  8538. const struct intel_link_m_n *m_n)
  8539. {
  8540. /*
  8541. * The calculation for the data clock is:
  8542. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8543. * But we want to avoid losing precison if possible, so:
  8544. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8545. *
  8546. * and the link clock is simpler:
  8547. * link_clock = (m * link_clock) / n
  8548. */
  8549. if (!m_n->link_n)
  8550. return 0;
  8551. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8552. }
  8553. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8554. struct intel_crtc_state *pipe_config)
  8555. {
  8556. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8557. /* read out port_clock from the DPLL */
  8558. i9xx_crtc_clock_get(crtc, pipe_config);
  8559. /*
  8560. * In case there is an active pipe without active ports,
  8561. * we may need some idea for the dotclock anyway.
  8562. * Calculate one based on the FDI configuration.
  8563. */
  8564. pipe_config->base.adjusted_mode.crtc_clock =
  8565. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8566. &pipe_config->fdi_m_n);
  8567. }
  8568. /** Returns the currently programmed mode of the given pipe. */
  8569. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8570. struct drm_crtc *crtc)
  8571. {
  8572. struct drm_i915_private *dev_priv = to_i915(dev);
  8573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8574. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8575. struct drm_display_mode *mode;
  8576. struct intel_crtc_state *pipe_config;
  8577. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8578. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8579. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8580. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8581. enum pipe pipe = intel_crtc->pipe;
  8582. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8583. if (!mode)
  8584. return NULL;
  8585. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8586. if (!pipe_config) {
  8587. kfree(mode);
  8588. return NULL;
  8589. }
  8590. /*
  8591. * Construct a pipe_config sufficient for getting the clock info
  8592. * back out of crtc_clock_get.
  8593. *
  8594. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8595. * to use a real value here instead.
  8596. */
  8597. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8598. pipe_config->pixel_multiplier = 1;
  8599. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8600. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8601. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8602. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8603. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8604. mode->hdisplay = (htot & 0xffff) + 1;
  8605. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8606. mode->hsync_start = (hsync & 0xffff) + 1;
  8607. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8608. mode->vdisplay = (vtot & 0xffff) + 1;
  8609. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8610. mode->vsync_start = (vsync & 0xffff) + 1;
  8611. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8612. drm_mode_set_name(mode);
  8613. kfree(pipe_config);
  8614. return mode;
  8615. }
  8616. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8617. {
  8618. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8619. drm_crtc_cleanup(crtc);
  8620. kfree(intel_crtc);
  8621. }
  8622. /**
  8623. * intel_wm_need_update - Check whether watermarks need updating
  8624. * @plane: drm plane
  8625. * @state: new plane state
  8626. *
  8627. * Check current plane state versus the new one to determine whether
  8628. * watermarks need to be recalculated.
  8629. *
  8630. * Returns true or false.
  8631. */
  8632. static bool intel_wm_need_update(struct drm_plane *plane,
  8633. struct drm_plane_state *state)
  8634. {
  8635. struct intel_plane_state *new = to_intel_plane_state(state);
  8636. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8637. /* Update watermarks on tiling or size changes. */
  8638. if (new->base.visible != cur->base.visible)
  8639. return true;
  8640. if (!cur->base.fb || !new->base.fb)
  8641. return false;
  8642. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8643. cur->base.rotation != new->base.rotation ||
  8644. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8645. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8646. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8647. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8648. return true;
  8649. return false;
  8650. }
  8651. static bool needs_scaling(struct intel_plane_state *state)
  8652. {
  8653. int src_w = drm_rect_width(&state->base.src) >> 16;
  8654. int src_h = drm_rect_height(&state->base.src) >> 16;
  8655. int dst_w = drm_rect_width(&state->base.dst);
  8656. int dst_h = drm_rect_height(&state->base.dst);
  8657. return (src_w != dst_w || src_h != dst_h);
  8658. }
  8659. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  8660. struct drm_plane_state *plane_state)
  8661. {
  8662. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8663. struct drm_crtc *crtc = crtc_state->crtc;
  8664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8665. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8666. struct drm_device *dev = crtc->dev;
  8667. struct drm_i915_private *dev_priv = to_i915(dev);
  8668. struct intel_plane_state *old_plane_state =
  8669. to_intel_plane_state(plane->base.state);
  8670. bool mode_changed = needs_modeset(crtc_state);
  8671. bool was_crtc_enabled = crtc->state->active;
  8672. bool is_crtc_enabled = crtc_state->active;
  8673. bool turn_off, turn_on, visible, was_visible;
  8674. struct drm_framebuffer *fb = plane_state->fb;
  8675. int ret;
  8676. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8677. ret = skl_update_scaler_plane(
  8678. to_intel_crtc_state(crtc_state),
  8679. to_intel_plane_state(plane_state));
  8680. if (ret)
  8681. return ret;
  8682. }
  8683. was_visible = old_plane_state->base.visible;
  8684. visible = plane_state->visible;
  8685. if (!was_crtc_enabled && WARN_ON(was_visible))
  8686. was_visible = false;
  8687. /*
  8688. * Visibility is calculated as if the crtc was on, but
  8689. * after scaler setup everything depends on it being off
  8690. * when the crtc isn't active.
  8691. *
  8692. * FIXME this is wrong for watermarks. Watermarks should also
  8693. * be computed as if the pipe would be active. Perhaps move
  8694. * per-plane wm computation to the .check_plane() hook, and
  8695. * only combine the results from all planes in the current place?
  8696. */
  8697. if (!is_crtc_enabled) {
  8698. plane_state->visible = visible = false;
  8699. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8700. }
  8701. if (!was_visible && !visible)
  8702. return 0;
  8703. if (fb != old_plane_state->base.fb)
  8704. pipe_config->fb_changed = true;
  8705. turn_off = was_visible && (!visible || mode_changed);
  8706. turn_on = visible && (!was_visible || mode_changed);
  8707. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8708. intel_crtc->base.base.id, intel_crtc->base.name,
  8709. plane->base.base.id, plane->base.name,
  8710. fb ? fb->base.id : -1);
  8711. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8712. plane->base.base.id, plane->base.name,
  8713. was_visible, visible,
  8714. turn_off, turn_on, mode_changed);
  8715. if (turn_on) {
  8716. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8717. pipe_config->update_wm_pre = true;
  8718. /* must disable cxsr around plane enable/disable */
  8719. if (plane->id != PLANE_CURSOR)
  8720. pipe_config->disable_cxsr = true;
  8721. } else if (turn_off) {
  8722. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8723. pipe_config->update_wm_post = true;
  8724. /* must disable cxsr around plane enable/disable */
  8725. if (plane->id != PLANE_CURSOR)
  8726. pipe_config->disable_cxsr = true;
  8727. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8728. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8729. /* FIXME bollocks */
  8730. pipe_config->update_wm_pre = true;
  8731. pipe_config->update_wm_post = true;
  8732. }
  8733. }
  8734. if (visible || was_visible)
  8735. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8736. /*
  8737. * WaCxSRDisabledForSpriteScaling:ivb
  8738. *
  8739. * cstate->update_wm was already set above, so this flag will
  8740. * take effect when we commit and program watermarks.
  8741. */
  8742. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8743. needs_scaling(to_intel_plane_state(plane_state)) &&
  8744. !needs_scaling(old_plane_state))
  8745. pipe_config->disable_lp_wm = true;
  8746. return 0;
  8747. }
  8748. static bool encoders_cloneable(const struct intel_encoder *a,
  8749. const struct intel_encoder *b)
  8750. {
  8751. /* masks could be asymmetric, so check both ways */
  8752. return a == b || (a->cloneable & (1 << b->type) &&
  8753. b->cloneable & (1 << a->type));
  8754. }
  8755. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8756. struct intel_crtc *crtc,
  8757. struct intel_encoder *encoder)
  8758. {
  8759. struct intel_encoder *source_encoder;
  8760. struct drm_connector *connector;
  8761. struct drm_connector_state *connector_state;
  8762. int i;
  8763. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8764. if (connector_state->crtc != &crtc->base)
  8765. continue;
  8766. source_encoder =
  8767. to_intel_encoder(connector_state->best_encoder);
  8768. if (!encoders_cloneable(encoder, source_encoder))
  8769. return false;
  8770. }
  8771. return true;
  8772. }
  8773. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8774. struct drm_crtc_state *crtc_state)
  8775. {
  8776. struct drm_device *dev = crtc->dev;
  8777. struct drm_i915_private *dev_priv = to_i915(dev);
  8778. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8779. struct intel_crtc_state *pipe_config =
  8780. to_intel_crtc_state(crtc_state);
  8781. struct drm_atomic_state *state = crtc_state->state;
  8782. int ret;
  8783. bool mode_changed = needs_modeset(crtc_state);
  8784. if (mode_changed && !crtc_state->active)
  8785. pipe_config->update_wm_post = true;
  8786. if (mode_changed && crtc_state->enable &&
  8787. dev_priv->display.crtc_compute_clock &&
  8788. !WARN_ON(pipe_config->shared_dpll)) {
  8789. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8790. pipe_config);
  8791. if (ret)
  8792. return ret;
  8793. }
  8794. if (crtc_state->color_mgmt_changed) {
  8795. ret = intel_color_check(crtc, crtc_state);
  8796. if (ret)
  8797. return ret;
  8798. /*
  8799. * Changing color management on Intel hardware is
  8800. * handled as part of planes update.
  8801. */
  8802. crtc_state->planes_changed = true;
  8803. }
  8804. ret = 0;
  8805. if (dev_priv->display.compute_pipe_wm) {
  8806. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8807. if (ret) {
  8808. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8809. return ret;
  8810. }
  8811. }
  8812. if (dev_priv->display.compute_intermediate_wm &&
  8813. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8814. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8815. return 0;
  8816. /*
  8817. * Calculate 'intermediate' watermarks that satisfy both the
  8818. * old state and the new state. We can program these
  8819. * immediately.
  8820. */
  8821. ret = dev_priv->display.compute_intermediate_wm(dev,
  8822. intel_crtc,
  8823. pipe_config);
  8824. if (ret) {
  8825. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8826. return ret;
  8827. }
  8828. } else if (dev_priv->display.compute_intermediate_wm) {
  8829. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8830. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8831. }
  8832. if (INTEL_GEN(dev_priv) >= 9) {
  8833. if (mode_changed)
  8834. ret = skl_update_scaler_crtc(pipe_config);
  8835. if (!ret)
  8836. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8837. pipe_config);
  8838. if (!ret)
  8839. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8840. pipe_config);
  8841. }
  8842. return ret;
  8843. }
  8844. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8845. .atomic_begin = intel_begin_crtc_commit,
  8846. .atomic_flush = intel_finish_crtc_commit,
  8847. .atomic_check = intel_crtc_atomic_check,
  8848. };
  8849. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8850. {
  8851. struct intel_connector *connector;
  8852. struct drm_connector_list_iter conn_iter;
  8853. drm_connector_list_iter_begin(dev, &conn_iter);
  8854. for_each_intel_connector_iter(connector, &conn_iter) {
  8855. if (connector->base.state->crtc)
  8856. drm_connector_unreference(&connector->base);
  8857. if (connector->base.encoder) {
  8858. connector->base.state->best_encoder =
  8859. connector->base.encoder;
  8860. connector->base.state->crtc =
  8861. connector->base.encoder->crtc;
  8862. drm_connector_reference(&connector->base);
  8863. } else {
  8864. connector->base.state->best_encoder = NULL;
  8865. connector->base.state->crtc = NULL;
  8866. }
  8867. }
  8868. drm_connector_list_iter_end(&conn_iter);
  8869. }
  8870. static void
  8871. connected_sink_compute_bpp(struct intel_connector *connector,
  8872. struct intel_crtc_state *pipe_config)
  8873. {
  8874. const struct drm_display_info *info = &connector->base.display_info;
  8875. int bpp = pipe_config->pipe_bpp;
  8876. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8877. connector->base.base.id,
  8878. connector->base.name);
  8879. /* Don't use an invalid EDID bpc value */
  8880. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8881. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8882. bpp, info->bpc * 3);
  8883. pipe_config->pipe_bpp = info->bpc * 3;
  8884. }
  8885. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8886. if (info->bpc == 0 && bpp > 24) {
  8887. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8888. bpp);
  8889. pipe_config->pipe_bpp = 24;
  8890. }
  8891. }
  8892. static int
  8893. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8894. struct intel_crtc_state *pipe_config)
  8895. {
  8896. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8897. struct drm_atomic_state *state;
  8898. struct drm_connector *connector;
  8899. struct drm_connector_state *connector_state;
  8900. int bpp, i;
  8901. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8902. IS_CHERRYVIEW(dev_priv)))
  8903. bpp = 10*3;
  8904. else if (INTEL_GEN(dev_priv) >= 5)
  8905. bpp = 12*3;
  8906. else
  8907. bpp = 8*3;
  8908. pipe_config->pipe_bpp = bpp;
  8909. state = pipe_config->base.state;
  8910. /* Clamp display bpp to EDID value */
  8911. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8912. if (connector_state->crtc != &crtc->base)
  8913. continue;
  8914. connected_sink_compute_bpp(to_intel_connector(connector),
  8915. pipe_config);
  8916. }
  8917. return bpp;
  8918. }
  8919. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8920. {
  8921. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8922. "type: 0x%x flags: 0x%x\n",
  8923. mode->crtc_clock,
  8924. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8925. mode->crtc_hsync_end, mode->crtc_htotal,
  8926. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8927. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8928. }
  8929. static inline void
  8930. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8931. unsigned int lane_count, struct intel_link_m_n *m_n)
  8932. {
  8933. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8934. id, lane_count,
  8935. m_n->gmch_m, m_n->gmch_n,
  8936. m_n->link_m, m_n->link_n, m_n->tu);
  8937. }
  8938. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8939. struct intel_crtc_state *pipe_config,
  8940. const char *context)
  8941. {
  8942. struct drm_device *dev = crtc->base.dev;
  8943. struct drm_i915_private *dev_priv = to_i915(dev);
  8944. struct drm_plane *plane;
  8945. struct intel_plane *intel_plane;
  8946. struct intel_plane_state *state;
  8947. struct drm_framebuffer *fb;
  8948. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8949. crtc->base.base.id, crtc->base.name, context);
  8950. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8951. transcoder_name(pipe_config->cpu_transcoder),
  8952. pipe_config->pipe_bpp, pipe_config->dither);
  8953. if (pipe_config->has_pch_encoder)
  8954. intel_dump_m_n_config(pipe_config, "fdi",
  8955. pipe_config->fdi_lanes,
  8956. &pipe_config->fdi_m_n);
  8957. if (pipe_config->ycbcr420)
  8958. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8959. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8960. intel_dump_m_n_config(pipe_config, "dp m_n",
  8961. pipe_config->lane_count, &pipe_config->dp_m_n);
  8962. if (pipe_config->has_drrs)
  8963. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8964. pipe_config->lane_count,
  8965. &pipe_config->dp_m2_n2);
  8966. }
  8967. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8968. pipe_config->has_audio, pipe_config->has_infoframe);
  8969. DRM_DEBUG_KMS("requested mode:\n");
  8970. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8971. DRM_DEBUG_KMS("adjusted mode:\n");
  8972. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8973. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8974. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8975. pipe_config->port_clock,
  8976. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8977. pipe_config->pixel_rate);
  8978. if (INTEL_GEN(dev_priv) >= 9)
  8979. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8980. crtc->num_scalers,
  8981. pipe_config->scaler_state.scaler_users,
  8982. pipe_config->scaler_state.scaler_id);
  8983. if (HAS_GMCH_DISPLAY(dev_priv))
  8984. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8985. pipe_config->gmch_pfit.control,
  8986. pipe_config->gmch_pfit.pgm_ratios,
  8987. pipe_config->gmch_pfit.lvds_border_bits);
  8988. else
  8989. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8990. pipe_config->pch_pfit.pos,
  8991. pipe_config->pch_pfit.size,
  8992. enableddisabled(pipe_config->pch_pfit.enabled));
  8993. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8994. pipe_config->ips_enabled, pipe_config->double_wide);
  8995. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8996. DRM_DEBUG_KMS("planes on this crtc\n");
  8997. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8998. struct drm_format_name_buf format_name;
  8999. intel_plane = to_intel_plane(plane);
  9000. if (intel_plane->pipe != crtc->pipe)
  9001. continue;
  9002. state = to_intel_plane_state(plane->state);
  9003. fb = state->base.fb;
  9004. if (!fb) {
  9005. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9006. plane->base.id, plane->name, state->scaler_id);
  9007. continue;
  9008. }
  9009. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9010. plane->base.id, plane->name,
  9011. fb->base.id, fb->width, fb->height,
  9012. drm_get_format_name(fb->format->format, &format_name));
  9013. if (INTEL_GEN(dev_priv) >= 9)
  9014. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9015. state->scaler_id,
  9016. state->base.src.x1 >> 16,
  9017. state->base.src.y1 >> 16,
  9018. drm_rect_width(&state->base.src) >> 16,
  9019. drm_rect_height(&state->base.src) >> 16,
  9020. state->base.dst.x1, state->base.dst.y1,
  9021. drm_rect_width(&state->base.dst),
  9022. drm_rect_height(&state->base.dst));
  9023. }
  9024. }
  9025. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9026. {
  9027. struct drm_device *dev = state->dev;
  9028. struct drm_connector *connector;
  9029. struct drm_connector_list_iter conn_iter;
  9030. unsigned int used_ports = 0;
  9031. unsigned int used_mst_ports = 0;
  9032. /*
  9033. * Walk the connector list instead of the encoder
  9034. * list to detect the problem on ddi platforms
  9035. * where there's just one encoder per digital port.
  9036. */
  9037. drm_connector_list_iter_begin(dev, &conn_iter);
  9038. drm_for_each_connector_iter(connector, &conn_iter) {
  9039. struct drm_connector_state *connector_state;
  9040. struct intel_encoder *encoder;
  9041. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9042. if (!connector_state)
  9043. connector_state = connector->state;
  9044. if (!connector_state->best_encoder)
  9045. continue;
  9046. encoder = to_intel_encoder(connector_state->best_encoder);
  9047. WARN_ON(!connector_state->crtc);
  9048. switch (encoder->type) {
  9049. unsigned int port_mask;
  9050. case INTEL_OUTPUT_UNKNOWN:
  9051. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9052. break;
  9053. case INTEL_OUTPUT_DP:
  9054. case INTEL_OUTPUT_HDMI:
  9055. case INTEL_OUTPUT_EDP:
  9056. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9057. /* the same port mustn't appear more than once */
  9058. if (used_ports & port_mask)
  9059. return false;
  9060. used_ports |= port_mask;
  9061. break;
  9062. case INTEL_OUTPUT_DP_MST:
  9063. used_mst_ports |=
  9064. 1 << enc_to_mst(&encoder->base)->primary->port;
  9065. break;
  9066. default:
  9067. break;
  9068. }
  9069. }
  9070. drm_connector_list_iter_end(&conn_iter);
  9071. /* can't mix MST and SST/HDMI on the same port */
  9072. if (used_ports & used_mst_ports)
  9073. return false;
  9074. return true;
  9075. }
  9076. static void
  9077. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9078. {
  9079. struct drm_i915_private *dev_priv =
  9080. to_i915(crtc_state->base.crtc->dev);
  9081. struct intel_crtc_scaler_state scaler_state;
  9082. struct intel_dpll_hw_state dpll_hw_state;
  9083. struct intel_shared_dpll *shared_dpll;
  9084. struct intel_crtc_wm_state wm_state;
  9085. bool force_thru;
  9086. /* FIXME: before the switch to atomic started, a new pipe_config was
  9087. * kzalloc'd. Code that depends on any field being zero should be
  9088. * fixed, so that the crtc_state can be safely duplicated. For now,
  9089. * only fields that are know to not cause problems are preserved. */
  9090. scaler_state = crtc_state->scaler_state;
  9091. shared_dpll = crtc_state->shared_dpll;
  9092. dpll_hw_state = crtc_state->dpll_hw_state;
  9093. force_thru = crtc_state->pch_pfit.force_thru;
  9094. if (IS_G4X(dev_priv) ||
  9095. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9096. wm_state = crtc_state->wm;
  9097. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9098. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9099. memset(&crtc_state->base + 1, 0,
  9100. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9101. crtc_state->scaler_state = scaler_state;
  9102. crtc_state->shared_dpll = shared_dpll;
  9103. crtc_state->dpll_hw_state = dpll_hw_state;
  9104. crtc_state->pch_pfit.force_thru = force_thru;
  9105. if (IS_G4X(dev_priv) ||
  9106. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9107. crtc_state->wm = wm_state;
  9108. }
  9109. static int
  9110. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9111. struct intel_crtc_state *pipe_config)
  9112. {
  9113. struct drm_atomic_state *state = pipe_config->base.state;
  9114. struct intel_encoder *encoder;
  9115. struct drm_connector *connector;
  9116. struct drm_connector_state *connector_state;
  9117. int base_bpp, ret = -EINVAL;
  9118. int i;
  9119. bool retry = true;
  9120. clear_intel_crtc_state(pipe_config);
  9121. pipe_config->cpu_transcoder =
  9122. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9123. /*
  9124. * Sanitize sync polarity flags based on requested ones. If neither
  9125. * positive or negative polarity is requested, treat this as meaning
  9126. * negative polarity.
  9127. */
  9128. if (!(pipe_config->base.adjusted_mode.flags &
  9129. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9130. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9131. if (!(pipe_config->base.adjusted_mode.flags &
  9132. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9133. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9134. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9135. pipe_config);
  9136. if (base_bpp < 0)
  9137. goto fail;
  9138. /*
  9139. * Determine the real pipe dimensions. Note that stereo modes can
  9140. * increase the actual pipe size due to the frame doubling and
  9141. * insertion of additional space for blanks between the frame. This
  9142. * is stored in the crtc timings. We use the requested mode to do this
  9143. * computation to clearly distinguish it from the adjusted mode, which
  9144. * can be changed by the connectors in the below retry loop.
  9145. */
  9146. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9147. &pipe_config->pipe_src_w,
  9148. &pipe_config->pipe_src_h);
  9149. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9150. if (connector_state->crtc != crtc)
  9151. continue;
  9152. encoder = to_intel_encoder(connector_state->best_encoder);
  9153. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9154. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9155. goto fail;
  9156. }
  9157. /*
  9158. * Determine output_types before calling the .compute_config()
  9159. * hooks so that the hooks can use this information safely.
  9160. */
  9161. pipe_config->output_types |= 1 << encoder->type;
  9162. }
  9163. encoder_retry:
  9164. /* Ensure the port clock defaults are reset when retrying. */
  9165. pipe_config->port_clock = 0;
  9166. pipe_config->pixel_multiplier = 1;
  9167. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9168. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9169. CRTC_STEREO_DOUBLE);
  9170. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9171. * adjust it according to limitations or connector properties, and also
  9172. * a chance to reject the mode entirely.
  9173. */
  9174. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9175. if (connector_state->crtc != crtc)
  9176. continue;
  9177. encoder = to_intel_encoder(connector_state->best_encoder);
  9178. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9179. DRM_DEBUG_KMS("Encoder config failure\n");
  9180. goto fail;
  9181. }
  9182. }
  9183. /* Set default port clock if not overwritten by the encoder. Needs to be
  9184. * done afterwards in case the encoder adjusts the mode. */
  9185. if (!pipe_config->port_clock)
  9186. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9187. * pipe_config->pixel_multiplier;
  9188. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9189. if (ret < 0) {
  9190. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9191. goto fail;
  9192. }
  9193. if (ret == RETRY) {
  9194. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9195. ret = -EINVAL;
  9196. goto fail;
  9197. }
  9198. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9199. retry = false;
  9200. goto encoder_retry;
  9201. }
  9202. /* Dithering seems to not pass-through bits correctly when it should, so
  9203. * only enable it on 6bpc panels and when its not a compliance
  9204. * test requesting 6bpc video pattern.
  9205. */
  9206. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9207. !pipe_config->dither_force_disable;
  9208. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9209. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9210. fail:
  9211. return ret;
  9212. }
  9213. static void
  9214. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9215. {
  9216. struct drm_crtc *crtc;
  9217. struct drm_crtc_state *new_crtc_state;
  9218. int i;
  9219. /* Double check state. */
  9220. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9221. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9222. /*
  9223. * Update legacy state to satisfy fbc code. This can
  9224. * be removed when fbc uses the atomic state.
  9225. */
  9226. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9227. struct drm_plane_state *plane_state = crtc->primary->state;
  9228. crtc->primary->fb = plane_state->fb;
  9229. crtc->x = plane_state->src_x >> 16;
  9230. crtc->y = plane_state->src_y >> 16;
  9231. }
  9232. }
  9233. }
  9234. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9235. {
  9236. int diff;
  9237. if (clock1 == clock2)
  9238. return true;
  9239. if (!clock1 || !clock2)
  9240. return false;
  9241. diff = abs(clock1 - clock2);
  9242. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9243. return true;
  9244. return false;
  9245. }
  9246. static bool
  9247. intel_compare_m_n(unsigned int m, unsigned int n,
  9248. unsigned int m2, unsigned int n2,
  9249. bool exact)
  9250. {
  9251. if (m == m2 && n == n2)
  9252. return true;
  9253. if (exact || !m || !n || !m2 || !n2)
  9254. return false;
  9255. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9256. if (n > n2) {
  9257. while (n > n2) {
  9258. m2 <<= 1;
  9259. n2 <<= 1;
  9260. }
  9261. } else if (n < n2) {
  9262. while (n < n2) {
  9263. m <<= 1;
  9264. n <<= 1;
  9265. }
  9266. }
  9267. if (n != n2)
  9268. return false;
  9269. return intel_fuzzy_clock_check(m, m2);
  9270. }
  9271. static bool
  9272. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9273. struct intel_link_m_n *m2_n2,
  9274. bool adjust)
  9275. {
  9276. if (m_n->tu == m2_n2->tu &&
  9277. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9278. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9279. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9280. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9281. if (adjust)
  9282. *m2_n2 = *m_n;
  9283. return true;
  9284. }
  9285. return false;
  9286. }
  9287. static void __printf(3, 4)
  9288. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9289. {
  9290. char *level;
  9291. unsigned int category;
  9292. struct va_format vaf;
  9293. va_list args;
  9294. if (adjust) {
  9295. level = KERN_DEBUG;
  9296. category = DRM_UT_KMS;
  9297. } else {
  9298. level = KERN_ERR;
  9299. category = DRM_UT_NONE;
  9300. }
  9301. va_start(args, format);
  9302. vaf.fmt = format;
  9303. vaf.va = &args;
  9304. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9305. va_end(args);
  9306. }
  9307. static bool
  9308. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9309. struct intel_crtc_state *current_config,
  9310. struct intel_crtc_state *pipe_config,
  9311. bool adjust)
  9312. {
  9313. bool ret = true;
  9314. #define PIPE_CONF_CHECK_X(name) \
  9315. if (current_config->name != pipe_config->name) { \
  9316. pipe_config_err(adjust, __stringify(name), \
  9317. "(expected 0x%08x, found 0x%08x)\n", \
  9318. current_config->name, \
  9319. pipe_config->name); \
  9320. ret = false; \
  9321. }
  9322. #define PIPE_CONF_CHECK_I(name) \
  9323. if (current_config->name != pipe_config->name) { \
  9324. pipe_config_err(adjust, __stringify(name), \
  9325. "(expected %i, found %i)\n", \
  9326. current_config->name, \
  9327. pipe_config->name); \
  9328. ret = false; \
  9329. }
  9330. #define PIPE_CONF_CHECK_P(name) \
  9331. if (current_config->name != pipe_config->name) { \
  9332. pipe_config_err(adjust, __stringify(name), \
  9333. "(expected %p, found %p)\n", \
  9334. current_config->name, \
  9335. pipe_config->name); \
  9336. ret = false; \
  9337. }
  9338. #define PIPE_CONF_CHECK_M_N(name) \
  9339. if (!intel_compare_link_m_n(&current_config->name, \
  9340. &pipe_config->name,\
  9341. adjust)) { \
  9342. pipe_config_err(adjust, __stringify(name), \
  9343. "(expected tu %i gmch %i/%i link %i/%i, " \
  9344. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9345. current_config->name.tu, \
  9346. current_config->name.gmch_m, \
  9347. current_config->name.gmch_n, \
  9348. current_config->name.link_m, \
  9349. current_config->name.link_n, \
  9350. pipe_config->name.tu, \
  9351. pipe_config->name.gmch_m, \
  9352. pipe_config->name.gmch_n, \
  9353. pipe_config->name.link_m, \
  9354. pipe_config->name.link_n); \
  9355. ret = false; \
  9356. }
  9357. /* This is required for BDW+ where there is only one set of registers for
  9358. * switching between high and low RR.
  9359. * This macro can be used whenever a comparison has to be made between one
  9360. * hw state and multiple sw state variables.
  9361. */
  9362. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9363. if (!intel_compare_link_m_n(&current_config->name, \
  9364. &pipe_config->name, adjust) && \
  9365. !intel_compare_link_m_n(&current_config->alt_name, \
  9366. &pipe_config->name, adjust)) { \
  9367. pipe_config_err(adjust, __stringify(name), \
  9368. "(expected tu %i gmch %i/%i link %i/%i, " \
  9369. "or tu %i gmch %i/%i link %i/%i, " \
  9370. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9371. current_config->name.tu, \
  9372. current_config->name.gmch_m, \
  9373. current_config->name.gmch_n, \
  9374. current_config->name.link_m, \
  9375. current_config->name.link_n, \
  9376. current_config->alt_name.tu, \
  9377. current_config->alt_name.gmch_m, \
  9378. current_config->alt_name.gmch_n, \
  9379. current_config->alt_name.link_m, \
  9380. current_config->alt_name.link_n, \
  9381. pipe_config->name.tu, \
  9382. pipe_config->name.gmch_m, \
  9383. pipe_config->name.gmch_n, \
  9384. pipe_config->name.link_m, \
  9385. pipe_config->name.link_n); \
  9386. ret = false; \
  9387. }
  9388. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9389. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9390. pipe_config_err(adjust, __stringify(name), \
  9391. "(%x) (expected %i, found %i)\n", \
  9392. (mask), \
  9393. current_config->name & (mask), \
  9394. pipe_config->name & (mask)); \
  9395. ret = false; \
  9396. }
  9397. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9398. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9399. pipe_config_err(adjust, __stringify(name), \
  9400. "(expected %i, found %i)\n", \
  9401. current_config->name, \
  9402. pipe_config->name); \
  9403. ret = false; \
  9404. }
  9405. #define PIPE_CONF_QUIRK(quirk) \
  9406. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9407. PIPE_CONF_CHECK_I(cpu_transcoder);
  9408. PIPE_CONF_CHECK_I(has_pch_encoder);
  9409. PIPE_CONF_CHECK_I(fdi_lanes);
  9410. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9411. PIPE_CONF_CHECK_I(lane_count);
  9412. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9413. if (INTEL_GEN(dev_priv) < 8) {
  9414. PIPE_CONF_CHECK_M_N(dp_m_n);
  9415. if (current_config->has_drrs)
  9416. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9417. } else
  9418. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9419. PIPE_CONF_CHECK_X(output_types);
  9420. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9421. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9422. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9423. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9424. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9425. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9432. PIPE_CONF_CHECK_I(pixel_multiplier);
  9433. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9434. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9435. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9436. PIPE_CONF_CHECK_I(limited_color_range);
  9437. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9438. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9439. PIPE_CONF_CHECK_I(has_infoframe);
  9440. PIPE_CONF_CHECK_I(ycbcr420);
  9441. PIPE_CONF_CHECK_I(has_audio);
  9442. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9443. DRM_MODE_FLAG_INTERLACE);
  9444. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9445. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9446. DRM_MODE_FLAG_PHSYNC);
  9447. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9448. DRM_MODE_FLAG_NHSYNC);
  9449. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9450. DRM_MODE_FLAG_PVSYNC);
  9451. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9452. DRM_MODE_FLAG_NVSYNC);
  9453. }
  9454. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9455. /* pfit ratios are autocomputed by the hw on gen4+ */
  9456. if (INTEL_GEN(dev_priv) < 4)
  9457. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9458. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9459. if (!adjust) {
  9460. PIPE_CONF_CHECK_I(pipe_src_w);
  9461. PIPE_CONF_CHECK_I(pipe_src_h);
  9462. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9463. if (current_config->pch_pfit.enabled) {
  9464. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9465. PIPE_CONF_CHECK_X(pch_pfit.size);
  9466. }
  9467. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9468. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9469. }
  9470. /* BDW+ don't expose a synchronous way to read the state */
  9471. if (IS_HASWELL(dev_priv))
  9472. PIPE_CONF_CHECK_I(ips_enabled);
  9473. PIPE_CONF_CHECK_I(double_wide);
  9474. PIPE_CONF_CHECK_P(shared_dpll);
  9475. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9476. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9477. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9478. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9479. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9480. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9481. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9482. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9483. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9484. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9485. PIPE_CONF_CHECK_X(dsi_pll.div);
  9486. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9487. PIPE_CONF_CHECK_I(pipe_bpp);
  9488. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9489. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9490. #undef PIPE_CONF_CHECK_X
  9491. #undef PIPE_CONF_CHECK_I
  9492. #undef PIPE_CONF_CHECK_P
  9493. #undef PIPE_CONF_CHECK_FLAGS
  9494. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9495. #undef PIPE_CONF_QUIRK
  9496. return ret;
  9497. }
  9498. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9499. const struct intel_crtc_state *pipe_config)
  9500. {
  9501. if (pipe_config->has_pch_encoder) {
  9502. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9503. &pipe_config->fdi_m_n);
  9504. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9505. /*
  9506. * FDI already provided one idea for the dotclock.
  9507. * Yell if the encoder disagrees.
  9508. */
  9509. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9510. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9511. fdi_dotclock, dotclock);
  9512. }
  9513. }
  9514. static void verify_wm_state(struct drm_crtc *crtc,
  9515. struct drm_crtc_state *new_state)
  9516. {
  9517. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9518. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9519. struct skl_pipe_wm hw_wm, *sw_wm;
  9520. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9521. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9523. const enum pipe pipe = intel_crtc->pipe;
  9524. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9525. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9526. return;
  9527. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9528. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9529. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9530. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9531. /* planes */
  9532. for_each_universal_plane(dev_priv, pipe, plane) {
  9533. hw_plane_wm = &hw_wm.planes[plane];
  9534. sw_plane_wm = &sw_wm->planes[plane];
  9535. /* Watermarks */
  9536. for (level = 0; level <= max_level; level++) {
  9537. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9538. &sw_plane_wm->wm[level]))
  9539. continue;
  9540. 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",
  9541. pipe_name(pipe), plane + 1, level,
  9542. sw_plane_wm->wm[level].plane_en,
  9543. sw_plane_wm->wm[level].plane_res_b,
  9544. sw_plane_wm->wm[level].plane_res_l,
  9545. hw_plane_wm->wm[level].plane_en,
  9546. hw_plane_wm->wm[level].plane_res_b,
  9547. hw_plane_wm->wm[level].plane_res_l);
  9548. }
  9549. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9550. &sw_plane_wm->trans_wm)) {
  9551. 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",
  9552. pipe_name(pipe), plane + 1,
  9553. sw_plane_wm->trans_wm.plane_en,
  9554. sw_plane_wm->trans_wm.plane_res_b,
  9555. sw_plane_wm->trans_wm.plane_res_l,
  9556. hw_plane_wm->trans_wm.plane_en,
  9557. hw_plane_wm->trans_wm.plane_res_b,
  9558. hw_plane_wm->trans_wm.plane_res_l);
  9559. }
  9560. /* DDB */
  9561. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9562. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9563. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9564. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9565. pipe_name(pipe), plane + 1,
  9566. sw_ddb_entry->start, sw_ddb_entry->end,
  9567. hw_ddb_entry->start, hw_ddb_entry->end);
  9568. }
  9569. }
  9570. /*
  9571. * cursor
  9572. * If the cursor plane isn't active, we may not have updated it's ddb
  9573. * allocation. In that case since the ddb allocation will be updated
  9574. * once the plane becomes visible, we can skip this check
  9575. */
  9576. if (1) {
  9577. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9578. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9579. /* Watermarks */
  9580. for (level = 0; level <= max_level; level++) {
  9581. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9582. &sw_plane_wm->wm[level]))
  9583. continue;
  9584. 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",
  9585. pipe_name(pipe), level,
  9586. sw_plane_wm->wm[level].plane_en,
  9587. sw_plane_wm->wm[level].plane_res_b,
  9588. sw_plane_wm->wm[level].plane_res_l,
  9589. hw_plane_wm->wm[level].plane_en,
  9590. hw_plane_wm->wm[level].plane_res_b,
  9591. hw_plane_wm->wm[level].plane_res_l);
  9592. }
  9593. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9594. &sw_plane_wm->trans_wm)) {
  9595. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9596. pipe_name(pipe),
  9597. sw_plane_wm->trans_wm.plane_en,
  9598. sw_plane_wm->trans_wm.plane_res_b,
  9599. sw_plane_wm->trans_wm.plane_res_l,
  9600. hw_plane_wm->trans_wm.plane_en,
  9601. hw_plane_wm->trans_wm.plane_res_b,
  9602. hw_plane_wm->trans_wm.plane_res_l);
  9603. }
  9604. /* DDB */
  9605. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9606. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9607. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9608. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9609. pipe_name(pipe),
  9610. sw_ddb_entry->start, sw_ddb_entry->end,
  9611. hw_ddb_entry->start, hw_ddb_entry->end);
  9612. }
  9613. }
  9614. }
  9615. static void
  9616. verify_connector_state(struct drm_device *dev,
  9617. struct drm_atomic_state *state,
  9618. struct drm_crtc *crtc)
  9619. {
  9620. struct drm_connector *connector;
  9621. struct drm_connector_state *new_conn_state;
  9622. int i;
  9623. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9624. struct drm_encoder *encoder = connector->encoder;
  9625. struct drm_crtc_state *crtc_state = NULL;
  9626. if (new_conn_state->crtc != crtc)
  9627. continue;
  9628. if (crtc)
  9629. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9630. intel_connector_verify_state(crtc_state, new_conn_state);
  9631. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9632. "connector's atomic encoder doesn't match legacy encoder\n");
  9633. }
  9634. }
  9635. static void
  9636. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9637. {
  9638. struct intel_encoder *encoder;
  9639. struct drm_connector *connector;
  9640. struct drm_connector_state *old_conn_state, *new_conn_state;
  9641. int i;
  9642. for_each_intel_encoder(dev, encoder) {
  9643. bool enabled = false, found = false;
  9644. enum pipe pipe;
  9645. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9646. encoder->base.base.id,
  9647. encoder->base.name);
  9648. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9649. new_conn_state, i) {
  9650. if (old_conn_state->best_encoder == &encoder->base)
  9651. found = true;
  9652. if (new_conn_state->best_encoder != &encoder->base)
  9653. continue;
  9654. found = enabled = true;
  9655. I915_STATE_WARN(new_conn_state->crtc !=
  9656. encoder->base.crtc,
  9657. "connector's crtc doesn't match encoder crtc\n");
  9658. }
  9659. if (!found)
  9660. continue;
  9661. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9662. "encoder's enabled state mismatch "
  9663. "(expected %i, found %i)\n",
  9664. !!encoder->base.crtc, enabled);
  9665. if (!encoder->base.crtc) {
  9666. bool active;
  9667. active = encoder->get_hw_state(encoder, &pipe);
  9668. I915_STATE_WARN(active,
  9669. "encoder detached but still enabled on pipe %c.\n",
  9670. pipe_name(pipe));
  9671. }
  9672. }
  9673. }
  9674. static void
  9675. verify_crtc_state(struct drm_crtc *crtc,
  9676. struct drm_crtc_state *old_crtc_state,
  9677. struct drm_crtc_state *new_crtc_state)
  9678. {
  9679. struct drm_device *dev = crtc->dev;
  9680. struct drm_i915_private *dev_priv = to_i915(dev);
  9681. struct intel_encoder *encoder;
  9682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9683. struct intel_crtc_state *pipe_config, *sw_config;
  9684. struct drm_atomic_state *old_state;
  9685. bool active;
  9686. old_state = old_crtc_state->state;
  9687. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9688. pipe_config = to_intel_crtc_state(old_crtc_state);
  9689. memset(pipe_config, 0, sizeof(*pipe_config));
  9690. pipe_config->base.crtc = crtc;
  9691. pipe_config->base.state = old_state;
  9692. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9693. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9694. /* we keep both pipes enabled on 830 */
  9695. if (IS_I830(dev_priv))
  9696. active = new_crtc_state->active;
  9697. I915_STATE_WARN(new_crtc_state->active != active,
  9698. "crtc active state doesn't match with hw state "
  9699. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9700. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9701. "transitional active state does not match atomic hw state "
  9702. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9703. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9704. enum pipe pipe;
  9705. active = encoder->get_hw_state(encoder, &pipe);
  9706. I915_STATE_WARN(active != new_crtc_state->active,
  9707. "[ENCODER:%i] active %i with crtc active %i\n",
  9708. encoder->base.base.id, active, new_crtc_state->active);
  9709. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9710. "Encoder connected to wrong pipe %c\n",
  9711. pipe_name(pipe));
  9712. if (active) {
  9713. pipe_config->output_types |= 1 << encoder->type;
  9714. encoder->get_config(encoder, pipe_config);
  9715. }
  9716. }
  9717. intel_crtc_compute_pixel_rate(pipe_config);
  9718. if (!new_crtc_state->active)
  9719. return;
  9720. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9721. sw_config = to_intel_crtc_state(new_crtc_state);
  9722. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9723. pipe_config, false)) {
  9724. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9725. intel_dump_pipe_config(intel_crtc, pipe_config,
  9726. "[hw state]");
  9727. intel_dump_pipe_config(intel_crtc, sw_config,
  9728. "[sw state]");
  9729. }
  9730. }
  9731. static void
  9732. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9733. struct intel_shared_dpll *pll,
  9734. struct drm_crtc *crtc,
  9735. struct drm_crtc_state *new_state)
  9736. {
  9737. struct intel_dpll_hw_state dpll_hw_state;
  9738. unsigned crtc_mask;
  9739. bool active;
  9740. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9741. DRM_DEBUG_KMS("%s\n", pll->name);
  9742. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9743. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9744. I915_STATE_WARN(!pll->on && pll->active_mask,
  9745. "pll in active use but not on in sw tracking\n");
  9746. I915_STATE_WARN(pll->on && !pll->active_mask,
  9747. "pll is on but not used by any active crtc\n");
  9748. I915_STATE_WARN(pll->on != active,
  9749. "pll on state mismatch (expected %i, found %i)\n",
  9750. pll->on, active);
  9751. }
  9752. if (!crtc) {
  9753. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9754. "more active pll users than references: %x vs %x\n",
  9755. pll->active_mask, pll->state.crtc_mask);
  9756. return;
  9757. }
  9758. crtc_mask = 1 << drm_crtc_index(crtc);
  9759. if (new_state->active)
  9760. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9761. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9762. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9763. else
  9764. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9765. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9766. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9767. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9768. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9769. crtc_mask, pll->state.crtc_mask);
  9770. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9771. &dpll_hw_state,
  9772. sizeof(dpll_hw_state)),
  9773. "pll hw state mismatch\n");
  9774. }
  9775. static void
  9776. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9777. struct drm_crtc_state *old_crtc_state,
  9778. struct drm_crtc_state *new_crtc_state)
  9779. {
  9780. struct drm_i915_private *dev_priv = to_i915(dev);
  9781. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9782. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9783. if (new_state->shared_dpll)
  9784. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9785. if (old_state->shared_dpll &&
  9786. old_state->shared_dpll != new_state->shared_dpll) {
  9787. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9788. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9789. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9790. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9791. pipe_name(drm_crtc_index(crtc)));
  9792. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9793. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9794. pipe_name(drm_crtc_index(crtc)));
  9795. }
  9796. }
  9797. static void
  9798. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9799. struct drm_atomic_state *state,
  9800. struct drm_crtc_state *old_state,
  9801. struct drm_crtc_state *new_state)
  9802. {
  9803. if (!needs_modeset(new_state) &&
  9804. !to_intel_crtc_state(new_state)->update_pipe)
  9805. return;
  9806. verify_wm_state(crtc, new_state);
  9807. verify_connector_state(crtc->dev, state, crtc);
  9808. verify_crtc_state(crtc, old_state, new_state);
  9809. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9810. }
  9811. static void
  9812. verify_disabled_dpll_state(struct drm_device *dev)
  9813. {
  9814. struct drm_i915_private *dev_priv = to_i915(dev);
  9815. int i;
  9816. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9817. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9818. }
  9819. static void
  9820. intel_modeset_verify_disabled(struct drm_device *dev,
  9821. struct drm_atomic_state *state)
  9822. {
  9823. verify_encoder_state(dev, state);
  9824. verify_connector_state(dev, state, NULL);
  9825. verify_disabled_dpll_state(dev);
  9826. }
  9827. static void update_scanline_offset(struct intel_crtc *crtc)
  9828. {
  9829. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9830. /*
  9831. * The scanline counter increments at the leading edge of hsync.
  9832. *
  9833. * On most platforms it starts counting from vtotal-1 on the
  9834. * first active line. That means the scanline counter value is
  9835. * always one less than what we would expect. Ie. just after
  9836. * start of vblank, which also occurs at start of hsync (on the
  9837. * last active line), the scanline counter will read vblank_start-1.
  9838. *
  9839. * On gen2 the scanline counter starts counting from 1 instead
  9840. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9841. * to keep the value positive), instead of adding one.
  9842. *
  9843. * On HSW+ the behaviour of the scanline counter depends on the output
  9844. * type. For DP ports it behaves like most other platforms, but on HDMI
  9845. * there's an extra 1 line difference. So we need to add two instead of
  9846. * one to the value.
  9847. *
  9848. * On VLV/CHV DSI the scanline counter would appear to increment
  9849. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9850. * that means we can't tell whether we're in vblank or not while
  9851. * we're on that particular line. We must still set scanline_offset
  9852. * to 1 so that the vblank timestamps come out correct when we query
  9853. * the scanline counter from within the vblank interrupt handler.
  9854. * However if queried just before the start of vblank we'll get an
  9855. * answer that's slightly in the future.
  9856. */
  9857. if (IS_GEN2(dev_priv)) {
  9858. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9859. int vtotal;
  9860. vtotal = adjusted_mode->crtc_vtotal;
  9861. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9862. vtotal /= 2;
  9863. crtc->scanline_offset = vtotal - 1;
  9864. } else if (HAS_DDI(dev_priv) &&
  9865. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9866. crtc->scanline_offset = 2;
  9867. } else
  9868. crtc->scanline_offset = 1;
  9869. }
  9870. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9871. {
  9872. struct drm_device *dev = state->dev;
  9873. struct drm_i915_private *dev_priv = to_i915(dev);
  9874. struct drm_crtc *crtc;
  9875. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9876. int i;
  9877. if (!dev_priv->display.crtc_compute_clock)
  9878. return;
  9879. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9881. struct intel_shared_dpll *old_dpll =
  9882. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9883. if (!needs_modeset(new_crtc_state))
  9884. continue;
  9885. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9886. if (!old_dpll)
  9887. continue;
  9888. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9889. }
  9890. }
  9891. /*
  9892. * This implements the workaround described in the "notes" section of the mode
  9893. * set sequence documentation. When going from no pipes or single pipe to
  9894. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9895. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9896. */
  9897. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9898. {
  9899. struct drm_crtc_state *crtc_state;
  9900. struct intel_crtc *intel_crtc;
  9901. struct drm_crtc *crtc;
  9902. struct intel_crtc_state *first_crtc_state = NULL;
  9903. struct intel_crtc_state *other_crtc_state = NULL;
  9904. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9905. int i;
  9906. /* look at all crtc's that are going to be enabled in during modeset */
  9907. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9908. intel_crtc = to_intel_crtc(crtc);
  9909. if (!crtc_state->active || !needs_modeset(crtc_state))
  9910. continue;
  9911. if (first_crtc_state) {
  9912. other_crtc_state = to_intel_crtc_state(crtc_state);
  9913. break;
  9914. } else {
  9915. first_crtc_state = to_intel_crtc_state(crtc_state);
  9916. first_pipe = intel_crtc->pipe;
  9917. }
  9918. }
  9919. /* No workaround needed? */
  9920. if (!first_crtc_state)
  9921. return 0;
  9922. /* w/a possibly needed, check how many crtc's are already enabled. */
  9923. for_each_intel_crtc(state->dev, intel_crtc) {
  9924. struct intel_crtc_state *pipe_config;
  9925. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9926. if (IS_ERR(pipe_config))
  9927. return PTR_ERR(pipe_config);
  9928. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9929. if (!pipe_config->base.active ||
  9930. needs_modeset(&pipe_config->base))
  9931. continue;
  9932. /* 2 or more enabled crtcs means no need for w/a */
  9933. if (enabled_pipe != INVALID_PIPE)
  9934. return 0;
  9935. enabled_pipe = intel_crtc->pipe;
  9936. }
  9937. if (enabled_pipe != INVALID_PIPE)
  9938. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9939. else if (other_crtc_state)
  9940. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9941. return 0;
  9942. }
  9943. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9944. {
  9945. struct drm_crtc *crtc;
  9946. /* Add all pipes to the state */
  9947. for_each_crtc(state->dev, crtc) {
  9948. struct drm_crtc_state *crtc_state;
  9949. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9950. if (IS_ERR(crtc_state))
  9951. return PTR_ERR(crtc_state);
  9952. }
  9953. return 0;
  9954. }
  9955. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9956. {
  9957. struct drm_crtc *crtc;
  9958. /*
  9959. * Add all pipes to the state, and force
  9960. * a modeset on all the active ones.
  9961. */
  9962. for_each_crtc(state->dev, crtc) {
  9963. struct drm_crtc_state *crtc_state;
  9964. int ret;
  9965. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9966. if (IS_ERR(crtc_state))
  9967. return PTR_ERR(crtc_state);
  9968. if (!crtc_state->active || needs_modeset(crtc_state))
  9969. continue;
  9970. crtc_state->mode_changed = true;
  9971. ret = drm_atomic_add_affected_connectors(state, crtc);
  9972. if (ret)
  9973. return ret;
  9974. ret = drm_atomic_add_affected_planes(state, crtc);
  9975. if (ret)
  9976. return ret;
  9977. }
  9978. return 0;
  9979. }
  9980. static int intel_modeset_checks(struct drm_atomic_state *state)
  9981. {
  9982. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9983. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9984. struct drm_crtc *crtc;
  9985. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9986. int ret = 0, i;
  9987. if (!check_digital_port_conflicts(state)) {
  9988. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9989. return -EINVAL;
  9990. }
  9991. intel_state->modeset = true;
  9992. intel_state->active_crtcs = dev_priv->active_crtcs;
  9993. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9994. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  9995. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9996. if (new_crtc_state->active)
  9997. intel_state->active_crtcs |= 1 << i;
  9998. else
  9999. intel_state->active_crtcs &= ~(1 << i);
  10000. if (old_crtc_state->active != new_crtc_state->active)
  10001. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10002. }
  10003. /*
  10004. * See if the config requires any additional preparation, e.g.
  10005. * to adjust global state with pipes off. We need to do this
  10006. * here so we can get the modeset_pipe updated config for the new
  10007. * mode set on this crtc. For other crtcs we need to use the
  10008. * adjusted_mode bits in the crtc directly.
  10009. */
  10010. if (dev_priv->display.modeset_calc_cdclk) {
  10011. ret = dev_priv->display.modeset_calc_cdclk(state);
  10012. if (ret < 0)
  10013. return ret;
  10014. /*
  10015. * Writes to dev_priv->cdclk.logical must protected by
  10016. * holding all the crtc locks, even if we don't end up
  10017. * touching the hardware
  10018. */
  10019. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10020. &intel_state->cdclk.logical)) {
  10021. ret = intel_lock_all_pipes(state);
  10022. if (ret < 0)
  10023. return ret;
  10024. }
  10025. /* All pipes must be switched off while we change the cdclk. */
  10026. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10027. &intel_state->cdclk.actual)) {
  10028. ret = intel_modeset_all_pipes(state);
  10029. if (ret < 0)
  10030. return ret;
  10031. }
  10032. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10033. intel_state->cdclk.logical.cdclk,
  10034. intel_state->cdclk.actual.cdclk);
  10035. } else {
  10036. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10037. }
  10038. intel_modeset_clear_plls(state);
  10039. if (IS_HASWELL(dev_priv))
  10040. return haswell_mode_set_planes_workaround(state);
  10041. return 0;
  10042. }
  10043. /*
  10044. * Handle calculation of various watermark data at the end of the atomic check
  10045. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10046. * handlers to ensure that all derived state has been updated.
  10047. */
  10048. static int calc_watermark_data(struct drm_atomic_state *state)
  10049. {
  10050. struct drm_device *dev = state->dev;
  10051. struct drm_i915_private *dev_priv = to_i915(dev);
  10052. /* Is there platform-specific watermark information to calculate? */
  10053. if (dev_priv->display.compute_global_watermarks)
  10054. return dev_priv->display.compute_global_watermarks(state);
  10055. return 0;
  10056. }
  10057. /**
  10058. * intel_atomic_check - validate state object
  10059. * @dev: drm device
  10060. * @state: state to validate
  10061. */
  10062. static int intel_atomic_check(struct drm_device *dev,
  10063. struct drm_atomic_state *state)
  10064. {
  10065. struct drm_i915_private *dev_priv = to_i915(dev);
  10066. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10067. struct drm_crtc *crtc;
  10068. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10069. int ret, i;
  10070. bool any_ms = false;
  10071. ret = drm_atomic_helper_check_modeset(dev, state);
  10072. if (ret)
  10073. return ret;
  10074. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10075. struct intel_crtc_state *pipe_config =
  10076. to_intel_crtc_state(crtc_state);
  10077. /* Catch I915_MODE_FLAG_INHERITED */
  10078. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10079. crtc_state->mode_changed = true;
  10080. if (!needs_modeset(crtc_state))
  10081. continue;
  10082. if (!crtc_state->enable) {
  10083. any_ms = true;
  10084. continue;
  10085. }
  10086. /* FIXME: For only active_changed we shouldn't need to do any
  10087. * state recomputation at all. */
  10088. ret = drm_atomic_add_affected_connectors(state, crtc);
  10089. if (ret)
  10090. return ret;
  10091. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10092. if (ret) {
  10093. intel_dump_pipe_config(to_intel_crtc(crtc),
  10094. pipe_config, "[failed]");
  10095. return ret;
  10096. }
  10097. if (i915.fastboot &&
  10098. intel_pipe_config_compare(dev_priv,
  10099. to_intel_crtc_state(old_crtc_state),
  10100. pipe_config, true)) {
  10101. crtc_state->mode_changed = false;
  10102. pipe_config->update_pipe = true;
  10103. }
  10104. if (needs_modeset(crtc_state))
  10105. any_ms = true;
  10106. ret = drm_atomic_add_affected_planes(state, crtc);
  10107. if (ret)
  10108. return ret;
  10109. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10110. needs_modeset(crtc_state) ?
  10111. "[modeset]" : "[fastset]");
  10112. }
  10113. if (any_ms) {
  10114. ret = intel_modeset_checks(state);
  10115. if (ret)
  10116. return ret;
  10117. } else {
  10118. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10119. }
  10120. ret = drm_atomic_helper_check_planes(dev, state);
  10121. if (ret)
  10122. return ret;
  10123. intel_fbc_choose_crtc(dev_priv, state);
  10124. return calc_watermark_data(state);
  10125. }
  10126. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10127. struct drm_atomic_state *state)
  10128. {
  10129. return drm_atomic_helper_prepare_planes(dev, state);
  10130. }
  10131. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10132. {
  10133. struct drm_device *dev = crtc->base.dev;
  10134. if (!dev->max_vblank_count)
  10135. return drm_crtc_accurate_vblank_count(&crtc->base);
  10136. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10137. }
  10138. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10139. struct drm_i915_private *dev_priv,
  10140. unsigned crtc_mask)
  10141. {
  10142. unsigned last_vblank_count[I915_MAX_PIPES];
  10143. enum pipe pipe;
  10144. int ret;
  10145. if (!crtc_mask)
  10146. return;
  10147. for_each_pipe(dev_priv, pipe) {
  10148. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10149. pipe);
  10150. if (!((1 << pipe) & crtc_mask))
  10151. continue;
  10152. ret = drm_crtc_vblank_get(&crtc->base);
  10153. if (WARN_ON(ret != 0)) {
  10154. crtc_mask &= ~(1 << pipe);
  10155. continue;
  10156. }
  10157. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10158. }
  10159. for_each_pipe(dev_priv, pipe) {
  10160. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10161. pipe);
  10162. long lret;
  10163. if (!((1 << pipe) & crtc_mask))
  10164. continue;
  10165. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10166. last_vblank_count[pipe] !=
  10167. drm_crtc_vblank_count(&crtc->base),
  10168. msecs_to_jiffies(50));
  10169. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10170. drm_crtc_vblank_put(&crtc->base);
  10171. }
  10172. }
  10173. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10174. {
  10175. /* fb updated, need to unpin old fb */
  10176. if (crtc_state->fb_changed)
  10177. return true;
  10178. /* wm changes, need vblank before final wm's */
  10179. if (crtc_state->update_wm_post)
  10180. return true;
  10181. if (crtc_state->wm.need_postvbl_update)
  10182. return true;
  10183. return false;
  10184. }
  10185. static void intel_update_crtc(struct drm_crtc *crtc,
  10186. struct drm_atomic_state *state,
  10187. struct drm_crtc_state *old_crtc_state,
  10188. struct drm_crtc_state *new_crtc_state,
  10189. unsigned int *crtc_vblank_mask)
  10190. {
  10191. struct drm_device *dev = crtc->dev;
  10192. struct drm_i915_private *dev_priv = to_i915(dev);
  10193. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10194. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10195. bool modeset = needs_modeset(new_crtc_state);
  10196. if (modeset) {
  10197. update_scanline_offset(intel_crtc);
  10198. dev_priv->display.crtc_enable(pipe_config, state);
  10199. } else {
  10200. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10201. pipe_config);
  10202. }
  10203. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10204. intel_fbc_enable(
  10205. intel_crtc, pipe_config,
  10206. to_intel_plane_state(crtc->primary->state));
  10207. }
  10208. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10209. if (needs_vblank_wait(pipe_config))
  10210. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10211. }
  10212. static void intel_update_crtcs(struct drm_atomic_state *state,
  10213. unsigned int *crtc_vblank_mask)
  10214. {
  10215. struct drm_crtc *crtc;
  10216. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10217. int i;
  10218. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10219. if (!new_crtc_state->active)
  10220. continue;
  10221. intel_update_crtc(crtc, state, old_crtc_state,
  10222. new_crtc_state, crtc_vblank_mask);
  10223. }
  10224. }
  10225. static void skl_update_crtcs(struct drm_atomic_state *state,
  10226. unsigned int *crtc_vblank_mask)
  10227. {
  10228. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10229. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10230. struct drm_crtc *crtc;
  10231. struct intel_crtc *intel_crtc;
  10232. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10233. struct intel_crtc_state *cstate;
  10234. unsigned int updated = 0;
  10235. bool progress;
  10236. enum pipe pipe;
  10237. int i;
  10238. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10239. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10240. /* ignore allocations for crtc's that have been turned off. */
  10241. if (new_crtc_state->active)
  10242. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10243. /*
  10244. * Whenever the number of active pipes changes, we need to make sure we
  10245. * update the pipes in the right order so that their ddb allocations
  10246. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10247. * cause pipe underruns and other bad stuff.
  10248. */
  10249. do {
  10250. progress = false;
  10251. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10252. bool vbl_wait = false;
  10253. unsigned int cmask = drm_crtc_mask(crtc);
  10254. intel_crtc = to_intel_crtc(crtc);
  10255. cstate = to_intel_crtc_state(crtc->state);
  10256. pipe = intel_crtc->pipe;
  10257. if (updated & cmask || !cstate->base.active)
  10258. continue;
  10259. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10260. continue;
  10261. updated |= cmask;
  10262. entries[i] = &cstate->wm.skl.ddb;
  10263. /*
  10264. * If this is an already active pipe, it's DDB changed,
  10265. * and this isn't the last pipe that needs updating
  10266. * then we need to wait for a vblank to pass for the
  10267. * new ddb allocation to take effect.
  10268. */
  10269. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10270. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10271. !new_crtc_state->active_changed &&
  10272. intel_state->wm_results.dirty_pipes != updated)
  10273. vbl_wait = true;
  10274. intel_update_crtc(crtc, state, old_crtc_state,
  10275. new_crtc_state, crtc_vblank_mask);
  10276. if (vbl_wait)
  10277. intel_wait_for_vblank(dev_priv, pipe);
  10278. progress = true;
  10279. }
  10280. } while (progress);
  10281. }
  10282. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10283. {
  10284. struct intel_atomic_state *state, *next;
  10285. struct llist_node *freed;
  10286. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10287. llist_for_each_entry_safe(state, next, freed, freed)
  10288. drm_atomic_state_put(&state->base);
  10289. }
  10290. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10291. {
  10292. struct drm_i915_private *dev_priv =
  10293. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10294. intel_atomic_helper_free_state(dev_priv);
  10295. }
  10296. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10297. {
  10298. struct wait_queue_entry wait_fence, wait_reset;
  10299. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10300. init_wait_entry(&wait_fence, 0);
  10301. init_wait_entry(&wait_reset, 0);
  10302. for (;;) {
  10303. prepare_to_wait(&intel_state->commit_ready.wait,
  10304. &wait_fence, TASK_UNINTERRUPTIBLE);
  10305. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10306. &wait_reset, TASK_UNINTERRUPTIBLE);
  10307. if (i915_sw_fence_done(&intel_state->commit_ready)
  10308. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10309. break;
  10310. schedule();
  10311. }
  10312. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10313. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10314. }
  10315. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10316. {
  10317. struct drm_device *dev = state->dev;
  10318. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10319. struct drm_i915_private *dev_priv = to_i915(dev);
  10320. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10321. struct drm_crtc *crtc;
  10322. struct intel_crtc_state *intel_cstate;
  10323. u64 put_domains[I915_MAX_PIPES] = {};
  10324. unsigned crtc_vblank_mask = 0;
  10325. int i;
  10326. intel_atomic_commit_fence_wait(intel_state);
  10327. drm_atomic_helper_wait_for_dependencies(state);
  10328. if (intel_state->modeset)
  10329. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10330. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10332. if (needs_modeset(new_crtc_state) ||
  10333. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10334. put_domains[to_intel_crtc(crtc)->pipe] =
  10335. modeset_get_crtc_power_domains(crtc,
  10336. to_intel_crtc_state(new_crtc_state));
  10337. }
  10338. if (!needs_modeset(new_crtc_state))
  10339. continue;
  10340. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10341. to_intel_crtc_state(new_crtc_state));
  10342. if (old_crtc_state->active) {
  10343. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10344. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10345. intel_crtc->active = false;
  10346. intel_fbc_disable(intel_crtc);
  10347. intel_disable_shared_dpll(intel_crtc);
  10348. /*
  10349. * Underruns don't always raise
  10350. * interrupts, so check manually.
  10351. */
  10352. intel_check_cpu_fifo_underruns(dev_priv);
  10353. intel_check_pch_fifo_underruns(dev_priv);
  10354. if (!crtc->state->active) {
  10355. /*
  10356. * Make sure we don't call initial_watermarks
  10357. * for ILK-style watermark updates.
  10358. *
  10359. * No clue what this is supposed to achieve.
  10360. */
  10361. if (INTEL_GEN(dev_priv) >= 9)
  10362. dev_priv->display.initial_watermarks(intel_state,
  10363. to_intel_crtc_state(crtc->state));
  10364. }
  10365. }
  10366. }
  10367. /* Only after disabling all output pipelines that will be changed can we
  10368. * update the the output configuration. */
  10369. intel_modeset_update_crtc_state(state);
  10370. if (intel_state->modeset) {
  10371. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10372. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10373. /*
  10374. * SKL workaround: bspec recommends we disable the SAGV when we
  10375. * have more then one pipe enabled
  10376. */
  10377. if (!intel_can_enable_sagv(state))
  10378. intel_disable_sagv(dev_priv);
  10379. intel_modeset_verify_disabled(dev, state);
  10380. }
  10381. /* Complete the events for pipes that have now been disabled */
  10382. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10383. bool modeset = needs_modeset(new_crtc_state);
  10384. /* Complete events for now disable pipes here. */
  10385. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10386. spin_lock_irq(&dev->event_lock);
  10387. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10388. spin_unlock_irq(&dev->event_lock);
  10389. new_crtc_state->event = NULL;
  10390. }
  10391. }
  10392. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10393. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10394. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10395. * already, but still need the state for the delayed optimization. To
  10396. * fix this:
  10397. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10398. * - schedule that vblank worker _before_ calling hw_done
  10399. * - at the start of commit_tail, cancel it _synchrously
  10400. * - switch over to the vblank wait helper in the core after that since
  10401. * we don't need out special handling any more.
  10402. */
  10403. if (!state->legacy_cursor_update)
  10404. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10405. /*
  10406. * Now that the vblank has passed, we can go ahead and program the
  10407. * optimal watermarks on platforms that need two-step watermark
  10408. * programming.
  10409. *
  10410. * TODO: Move this (and other cleanup) to an async worker eventually.
  10411. */
  10412. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10413. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10414. if (dev_priv->display.optimize_watermarks)
  10415. dev_priv->display.optimize_watermarks(intel_state,
  10416. intel_cstate);
  10417. }
  10418. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10419. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10420. if (put_domains[i])
  10421. modeset_put_power_domains(dev_priv, put_domains[i]);
  10422. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10423. }
  10424. if (intel_state->modeset && intel_can_enable_sagv(state))
  10425. intel_enable_sagv(dev_priv);
  10426. drm_atomic_helper_commit_hw_done(state);
  10427. if (intel_state->modeset) {
  10428. /* As one of the primary mmio accessors, KMS has a high
  10429. * likelihood of triggering bugs in unclaimed access. After we
  10430. * finish modesetting, see if an error has been flagged, and if
  10431. * so enable debugging for the next modeset - and hope we catch
  10432. * the culprit.
  10433. */
  10434. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10435. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10436. }
  10437. drm_atomic_helper_cleanup_planes(dev, state);
  10438. drm_atomic_helper_commit_cleanup_done(state);
  10439. drm_atomic_state_put(state);
  10440. intel_atomic_helper_free_state(dev_priv);
  10441. }
  10442. static void intel_atomic_commit_work(struct work_struct *work)
  10443. {
  10444. struct drm_atomic_state *state =
  10445. container_of(work, struct drm_atomic_state, commit_work);
  10446. intel_atomic_commit_tail(state);
  10447. }
  10448. static int __i915_sw_fence_call
  10449. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10450. enum i915_sw_fence_notify notify)
  10451. {
  10452. struct intel_atomic_state *state =
  10453. container_of(fence, struct intel_atomic_state, commit_ready);
  10454. switch (notify) {
  10455. case FENCE_COMPLETE:
  10456. /* we do blocking waits in the worker, nothing to do here */
  10457. break;
  10458. case FENCE_FREE:
  10459. {
  10460. struct intel_atomic_helper *helper =
  10461. &to_i915(state->base.dev)->atomic_helper;
  10462. if (llist_add(&state->freed, &helper->free_list))
  10463. schedule_work(&helper->free_work);
  10464. break;
  10465. }
  10466. }
  10467. return NOTIFY_DONE;
  10468. }
  10469. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10470. {
  10471. struct drm_plane_state *old_plane_state, *new_plane_state;
  10472. struct drm_plane *plane;
  10473. int i;
  10474. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10475. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10476. intel_fb_obj(new_plane_state->fb),
  10477. to_intel_plane(plane)->frontbuffer_bit);
  10478. }
  10479. /**
  10480. * intel_atomic_commit - commit validated state object
  10481. * @dev: DRM device
  10482. * @state: the top-level driver state object
  10483. * @nonblock: nonblocking commit
  10484. *
  10485. * This function commits a top-level state object that has been validated
  10486. * with drm_atomic_helper_check().
  10487. *
  10488. * RETURNS
  10489. * Zero for success or -errno.
  10490. */
  10491. static int intel_atomic_commit(struct drm_device *dev,
  10492. struct drm_atomic_state *state,
  10493. bool nonblock)
  10494. {
  10495. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10496. struct drm_i915_private *dev_priv = to_i915(dev);
  10497. int ret = 0;
  10498. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10499. if (ret)
  10500. return ret;
  10501. drm_atomic_state_get(state);
  10502. i915_sw_fence_init(&intel_state->commit_ready,
  10503. intel_atomic_commit_ready);
  10504. ret = intel_atomic_prepare_commit(dev, state);
  10505. if (ret) {
  10506. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10507. i915_sw_fence_commit(&intel_state->commit_ready);
  10508. return ret;
  10509. }
  10510. /*
  10511. * The intel_legacy_cursor_update() fast path takes care
  10512. * of avoiding the vblank waits for simple cursor
  10513. * movement and flips. For cursor on/off and size changes,
  10514. * we want to perform the vblank waits so that watermark
  10515. * updates happen during the correct frames. Gen9+ have
  10516. * double buffered watermarks and so shouldn't need this.
  10517. *
  10518. * Do this after drm_atomic_helper_setup_commit() and
  10519. * intel_atomic_prepare_commit() because we still want
  10520. * to skip the flip and fb cleanup waits. Although that
  10521. * does risk yanking the mapping from under the display
  10522. * engine.
  10523. *
  10524. * FIXME doing watermarks and fb cleanup from a vblank worker
  10525. * (assuming we had any) would solve these problems.
  10526. */
  10527. if (INTEL_GEN(dev_priv) < 9)
  10528. state->legacy_cursor_update = false;
  10529. ret = drm_atomic_helper_swap_state(state, true);
  10530. if (ret) {
  10531. i915_sw_fence_commit(&intel_state->commit_ready);
  10532. drm_atomic_helper_cleanup_planes(dev, state);
  10533. return ret;
  10534. }
  10535. dev_priv->wm.distrust_bios_wm = false;
  10536. intel_shared_dpll_swap_state(state);
  10537. intel_atomic_track_fbs(state);
  10538. if (intel_state->modeset) {
  10539. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10540. sizeof(intel_state->min_pixclk));
  10541. dev_priv->active_crtcs = intel_state->active_crtcs;
  10542. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10543. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10544. }
  10545. drm_atomic_state_get(state);
  10546. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10547. i915_sw_fence_commit(&intel_state->commit_ready);
  10548. if (nonblock)
  10549. queue_work(system_unbound_wq, &state->commit_work);
  10550. else
  10551. intel_atomic_commit_tail(state);
  10552. return 0;
  10553. }
  10554. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10555. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10556. .set_config = drm_atomic_helper_set_config,
  10557. .destroy = intel_crtc_destroy,
  10558. .page_flip = drm_atomic_helper_page_flip,
  10559. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10560. .atomic_destroy_state = intel_crtc_destroy_state,
  10561. .set_crc_source = intel_crtc_set_crc_source,
  10562. };
  10563. /**
  10564. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10565. * @plane: drm plane to prepare for
  10566. * @fb: framebuffer to prepare for presentation
  10567. *
  10568. * Prepares a framebuffer for usage on a display plane. Generally this
  10569. * involves pinning the underlying object and updating the frontbuffer tracking
  10570. * bits. Some older platforms need special physical address handling for
  10571. * cursor planes.
  10572. *
  10573. * Must be called with struct_mutex held.
  10574. *
  10575. * Returns 0 on success, negative error code on failure.
  10576. */
  10577. int
  10578. intel_prepare_plane_fb(struct drm_plane *plane,
  10579. struct drm_plane_state *new_state)
  10580. {
  10581. struct intel_atomic_state *intel_state =
  10582. to_intel_atomic_state(new_state->state);
  10583. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10584. struct drm_framebuffer *fb = new_state->fb;
  10585. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10586. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10587. int ret;
  10588. if (old_obj) {
  10589. struct drm_crtc_state *crtc_state =
  10590. drm_atomic_get_existing_crtc_state(new_state->state,
  10591. plane->state->crtc);
  10592. /* Big Hammer, we also need to ensure that any pending
  10593. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10594. * current scanout is retired before unpinning the old
  10595. * framebuffer. Note that we rely on userspace rendering
  10596. * into the buffer attached to the pipe they are waiting
  10597. * on. If not, userspace generates a GPU hang with IPEHR
  10598. * point to the MI_WAIT_FOR_EVENT.
  10599. *
  10600. * This should only fail upon a hung GPU, in which case we
  10601. * can safely continue.
  10602. */
  10603. if (needs_modeset(crtc_state)) {
  10604. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10605. old_obj->resv, NULL,
  10606. false, 0,
  10607. GFP_KERNEL);
  10608. if (ret < 0)
  10609. return ret;
  10610. }
  10611. }
  10612. if (new_state->fence) { /* explicit fencing */
  10613. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10614. new_state->fence,
  10615. I915_FENCE_TIMEOUT,
  10616. GFP_KERNEL);
  10617. if (ret < 0)
  10618. return ret;
  10619. }
  10620. if (!obj)
  10621. return 0;
  10622. ret = i915_gem_object_pin_pages(obj);
  10623. if (ret)
  10624. return ret;
  10625. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10626. if (ret) {
  10627. i915_gem_object_unpin_pages(obj);
  10628. return ret;
  10629. }
  10630. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10631. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10632. const int align = intel_cursor_alignment(dev_priv);
  10633. ret = i915_gem_object_attach_phys(obj, align);
  10634. } else {
  10635. struct i915_vma *vma;
  10636. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10637. if (!IS_ERR(vma))
  10638. to_intel_plane_state(new_state)->vma = vma;
  10639. else
  10640. ret = PTR_ERR(vma);
  10641. }
  10642. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10643. mutex_unlock(&dev_priv->drm.struct_mutex);
  10644. i915_gem_object_unpin_pages(obj);
  10645. if (ret)
  10646. return ret;
  10647. if (!new_state->fence) { /* implicit fencing */
  10648. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10649. obj->resv, NULL,
  10650. false, I915_FENCE_TIMEOUT,
  10651. GFP_KERNEL);
  10652. if (ret < 0)
  10653. return ret;
  10654. }
  10655. return 0;
  10656. }
  10657. /**
  10658. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10659. * @plane: drm plane to clean up for
  10660. * @fb: old framebuffer that was on plane
  10661. *
  10662. * Cleans up a framebuffer that has just been removed from a plane.
  10663. *
  10664. * Must be called with struct_mutex held.
  10665. */
  10666. void
  10667. intel_cleanup_plane_fb(struct drm_plane *plane,
  10668. struct drm_plane_state *old_state)
  10669. {
  10670. struct i915_vma *vma;
  10671. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10672. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10673. if (vma) {
  10674. mutex_lock(&plane->dev->struct_mutex);
  10675. intel_unpin_fb_vma(vma);
  10676. mutex_unlock(&plane->dev->struct_mutex);
  10677. }
  10678. }
  10679. int
  10680. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10681. {
  10682. struct drm_i915_private *dev_priv;
  10683. int max_scale;
  10684. int crtc_clock, max_dotclk;
  10685. if (!intel_crtc || !crtc_state->base.enable)
  10686. return DRM_PLANE_HELPER_NO_SCALING;
  10687. dev_priv = to_i915(intel_crtc->base.dev);
  10688. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10689. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10690. if (IS_GEMINILAKE(dev_priv))
  10691. max_dotclk *= 2;
  10692. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10693. return DRM_PLANE_HELPER_NO_SCALING;
  10694. /*
  10695. * skl max scale is lower of:
  10696. * close to 3 but not 3, -1 is for that purpose
  10697. * or
  10698. * cdclk/crtc_clock
  10699. */
  10700. max_scale = min((1 << 16) * 3 - 1,
  10701. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10702. return max_scale;
  10703. }
  10704. static int
  10705. intel_check_primary_plane(struct intel_plane *plane,
  10706. struct intel_crtc_state *crtc_state,
  10707. struct intel_plane_state *state)
  10708. {
  10709. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10710. struct drm_crtc *crtc = state->base.crtc;
  10711. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10712. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10713. bool can_position = false;
  10714. int ret;
  10715. if (INTEL_GEN(dev_priv) >= 9) {
  10716. /* use scaler when colorkey is not required */
  10717. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10718. min_scale = 1;
  10719. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10720. }
  10721. can_position = true;
  10722. }
  10723. ret = drm_plane_helper_check_state(&state->base,
  10724. &state->clip,
  10725. min_scale, max_scale,
  10726. can_position, true);
  10727. if (ret)
  10728. return ret;
  10729. if (!state->base.fb)
  10730. return 0;
  10731. if (INTEL_GEN(dev_priv) >= 9) {
  10732. ret = skl_check_plane_surface(state);
  10733. if (ret)
  10734. return ret;
  10735. state->ctl = skl_plane_ctl(crtc_state, state);
  10736. } else {
  10737. ret = i9xx_check_plane_surface(state);
  10738. if (ret)
  10739. return ret;
  10740. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10741. }
  10742. return 0;
  10743. }
  10744. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10745. struct drm_crtc_state *old_crtc_state)
  10746. {
  10747. struct drm_device *dev = crtc->dev;
  10748. struct drm_i915_private *dev_priv = to_i915(dev);
  10749. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10750. struct intel_crtc_state *intel_cstate =
  10751. to_intel_crtc_state(crtc->state);
  10752. struct intel_crtc_state *old_intel_cstate =
  10753. to_intel_crtc_state(old_crtc_state);
  10754. struct intel_atomic_state *old_intel_state =
  10755. to_intel_atomic_state(old_crtc_state->state);
  10756. bool modeset = needs_modeset(crtc->state);
  10757. if (!modeset &&
  10758. (intel_cstate->base.color_mgmt_changed ||
  10759. intel_cstate->update_pipe)) {
  10760. intel_color_set_csc(crtc->state);
  10761. intel_color_load_luts(crtc->state);
  10762. }
  10763. /* Perform vblank evasion around commit operation */
  10764. intel_pipe_update_start(intel_crtc);
  10765. if (modeset)
  10766. goto out;
  10767. if (intel_cstate->update_pipe)
  10768. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  10769. else if (INTEL_GEN(dev_priv) >= 9)
  10770. skl_detach_scalers(intel_crtc);
  10771. out:
  10772. if (dev_priv->display.atomic_update_watermarks)
  10773. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10774. intel_cstate);
  10775. }
  10776. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10777. struct drm_crtc_state *old_crtc_state)
  10778. {
  10779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10780. intel_pipe_update_end(intel_crtc);
  10781. }
  10782. /**
  10783. * intel_plane_destroy - destroy a plane
  10784. * @plane: plane to destroy
  10785. *
  10786. * Common destruction function for all types of planes (primary, cursor,
  10787. * sprite).
  10788. */
  10789. void intel_plane_destroy(struct drm_plane *plane)
  10790. {
  10791. drm_plane_cleanup(plane);
  10792. kfree(to_intel_plane(plane));
  10793. }
  10794. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10795. {
  10796. switch (format) {
  10797. case DRM_FORMAT_C8:
  10798. case DRM_FORMAT_RGB565:
  10799. case DRM_FORMAT_XRGB1555:
  10800. case DRM_FORMAT_XRGB8888:
  10801. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10802. modifier == I915_FORMAT_MOD_X_TILED;
  10803. default:
  10804. return false;
  10805. }
  10806. }
  10807. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10808. {
  10809. switch (format) {
  10810. case DRM_FORMAT_C8:
  10811. case DRM_FORMAT_RGB565:
  10812. case DRM_FORMAT_XRGB8888:
  10813. case DRM_FORMAT_XBGR8888:
  10814. case DRM_FORMAT_XRGB2101010:
  10815. case DRM_FORMAT_XBGR2101010:
  10816. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10817. modifier == I915_FORMAT_MOD_X_TILED;
  10818. default:
  10819. return false;
  10820. }
  10821. }
  10822. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10823. {
  10824. switch (format) {
  10825. case DRM_FORMAT_XRGB8888:
  10826. case DRM_FORMAT_XBGR8888:
  10827. case DRM_FORMAT_ARGB8888:
  10828. case DRM_FORMAT_ABGR8888:
  10829. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10830. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10831. return true;
  10832. /* fall through */
  10833. case DRM_FORMAT_RGB565:
  10834. case DRM_FORMAT_XRGB2101010:
  10835. case DRM_FORMAT_XBGR2101010:
  10836. case DRM_FORMAT_YUYV:
  10837. case DRM_FORMAT_YVYU:
  10838. case DRM_FORMAT_UYVY:
  10839. case DRM_FORMAT_VYUY:
  10840. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10841. return true;
  10842. /* fall through */
  10843. case DRM_FORMAT_C8:
  10844. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10845. modifier == I915_FORMAT_MOD_X_TILED ||
  10846. modifier == I915_FORMAT_MOD_Y_TILED)
  10847. return true;
  10848. /* fall through */
  10849. default:
  10850. return false;
  10851. }
  10852. }
  10853. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10854. uint32_t format,
  10855. uint64_t modifier)
  10856. {
  10857. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10858. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10859. return false;
  10860. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10861. modifier != DRM_FORMAT_MOD_LINEAR)
  10862. return false;
  10863. if (INTEL_GEN(dev_priv) >= 9)
  10864. return skl_mod_supported(format, modifier);
  10865. else if (INTEL_GEN(dev_priv) >= 4)
  10866. return i965_mod_supported(format, modifier);
  10867. else
  10868. return i8xx_mod_supported(format, modifier);
  10869. unreachable();
  10870. }
  10871. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10872. uint32_t format,
  10873. uint64_t modifier)
  10874. {
  10875. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10876. return false;
  10877. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10878. }
  10879. static struct drm_plane_funcs intel_plane_funcs = {
  10880. .update_plane = drm_atomic_helper_update_plane,
  10881. .disable_plane = drm_atomic_helper_disable_plane,
  10882. .destroy = intel_plane_destroy,
  10883. .atomic_get_property = intel_plane_atomic_get_property,
  10884. .atomic_set_property = intel_plane_atomic_set_property,
  10885. .atomic_duplicate_state = intel_plane_duplicate_state,
  10886. .atomic_destroy_state = intel_plane_destroy_state,
  10887. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10888. };
  10889. static int
  10890. intel_legacy_cursor_update(struct drm_plane *plane,
  10891. struct drm_crtc *crtc,
  10892. struct drm_framebuffer *fb,
  10893. int crtc_x, int crtc_y,
  10894. unsigned int crtc_w, unsigned int crtc_h,
  10895. uint32_t src_x, uint32_t src_y,
  10896. uint32_t src_w, uint32_t src_h,
  10897. struct drm_modeset_acquire_ctx *ctx)
  10898. {
  10899. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10900. int ret;
  10901. struct drm_plane_state *old_plane_state, *new_plane_state;
  10902. struct intel_plane *intel_plane = to_intel_plane(plane);
  10903. struct drm_framebuffer *old_fb;
  10904. struct drm_crtc_state *crtc_state = crtc->state;
  10905. struct i915_vma *old_vma, *vma;
  10906. /*
  10907. * When crtc is inactive or there is a modeset pending,
  10908. * wait for it to complete in the slowpath
  10909. */
  10910. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10911. to_intel_crtc_state(crtc_state)->update_pipe)
  10912. goto slow;
  10913. old_plane_state = plane->state;
  10914. /*
  10915. * If any parameters change that may affect watermarks,
  10916. * take the slowpath. Only changing fb or position should be
  10917. * in the fastpath.
  10918. */
  10919. if (old_plane_state->crtc != crtc ||
  10920. old_plane_state->src_w != src_w ||
  10921. old_plane_state->src_h != src_h ||
  10922. old_plane_state->crtc_w != crtc_w ||
  10923. old_plane_state->crtc_h != crtc_h ||
  10924. !old_plane_state->fb != !fb)
  10925. goto slow;
  10926. new_plane_state = intel_plane_duplicate_state(plane);
  10927. if (!new_plane_state)
  10928. return -ENOMEM;
  10929. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10930. new_plane_state->src_x = src_x;
  10931. new_plane_state->src_y = src_y;
  10932. new_plane_state->src_w = src_w;
  10933. new_plane_state->src_h = src_h;
  10934. new_plane_state->crtc_x = crtc_x;
  10935. new_plane_state->crtc_y = crtc_y;
  10936. new_plane_state->crtc_w = crtc_w;
  10937. new_plane_state->crtc_h = crtc_h;
  10938. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10939. to_intel_plane_state(new_plane_state));
  10940. if (ret)
  10941. goto out_free;
  10942. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10943. if (ret)
  10944. goto out_free;
  10945. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10946. int align = intel_cursor_alignment(dev_priv);
  10947. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10948. if (ret) {
  10949. DRM_DEBUG_KMS("failed to attach phys object\n");
  10950. goto out_unlock;
  10951. }
  10952. } else {
  10953. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10954. if (IS_ERR(vma)) {
  10955. DRM_DEBUG_KMS("failed to pin object\n");
  10956. ret = PTR_ERR(vma);
  10957. goto out_unlock;
  10958. }
  10959. to_intel_plane_state(new_plane_state)->vma = vma;
  10960. }
  10961. old_fb = old_plane_state->fb;
  10962. old_vma = to_intel_plane_state(old_plane_state)->vma;
  10963. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10964. intel_plane->frontbuffer_bit);
  10965. /* Swap plane state */
  10966. new_plane_state->fence = old_plane_state->fence;
  10967. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  10968. new_plane_state->fence = NULL;
  10969. new_plane_state->fb = old_fb;
  10970. to_intel_plane_state(new_plane_state)->vma = NULL;
  10971. if (plane->state->visible) {
  10972. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10973. intel_plane->update_plane(intel_plane,
  10974. to_intel_crtc_state(crtc->state),
  10975. to_intel_plane_state(plane->state));
  10976. } else {
  10977. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10978. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10979. }
  10980. if (old_vma)
  10981. intel_unpin_fb_vma(old_vma);
  10982. out_unlock:
  10983. mutex_unlock(&dev_priv->drm.struct_mutex);
  10984. out_free:
  10985. intel_plane_destroy_state(plane, new_plane_state);
  10986. return ret;
  10987. slow:
  10988. return drm_atomic_helper_update_plane(plane, crtc, fb,
  10989. crtc_x, crtc_y, crtc_w, crtc_h,
  10990. src_x, src_y, src_w, src_h, ctx);
  10991. }
  10992. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10993. .update_plane = intel_legacy_cursor_update,
  10994. .disable_plane = drm_atomic_helper_disable_plane,
  10995. .destroy = intel_plane_destroy,
  10996. .atomic_get_property = intel_plane_atomic_get_property,
  10997. .atomic_set_property = intel_plane_atomic_set_property,
  10998. .atomic_duplicate_state = intel_plane_duplicate_state,
  10999. .atomic_destroy_state = intel_plane_destroy_state,
  11000. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  11001. };
  11002. static struct intel_plane *
  11003. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11004. {
  11005. struct intel_plane *primary = NULL;
  11006. struct intel_plane_state *state = NULL;
  11007. const uint32_t *intel_primary_formats;
  11008. unsigned int supported_rotations;
  11009. unsigned int num_formats;
  11010. const uint64_t *modifiers;
  11011. int ret;
  11012. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11013. if (!primary) {
  11014. ret = -ENOMEM;
  11015. goto fail;
  11016. }
  11017. state = intel_create_plane_state(&primary->base);
  11018. if (!state) {
  11019. ret = -ENOMEM;
  11020. goto fail;
  11021. }
  11022. primary->base.state = &state->base;
  11023. primary->can_scale = false;
  11024. primary->max_downscale = 1;
  11025. if (INTEL_GEN(dev_priv) >= 9) {
  11026. primary->can_scale = true;
  11027. state->scaler_id = -1;
  11028. }
  11029. primary->pipe = pipe;
  11030. /*
  11031. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11032. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11033. */
  11034. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11035. primary->plane = (enum plane) !pipe;
  11036. else
  11037. primary->plane = (enum plane) pipe;
  11038. primary->id = PLANE_PRIMARY;
  11039. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11040. primary->check_plane = intel_check_primary_plane;
  11041. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  11042. intel_primary_formats = skl_primary_formats;
  11043. num_formats = ARRAY_SIZE(skl_primary_formats);
  11044. modifiers = skl_format_modifiers_ccs;
  11045. primary->update_plane = skylake_update_primary_plane;
  11046. primary->disable_plane = skylake_disable_primary_plane;
  11047. } else if (INTEL_GEN(dev_priv) >= 9) {
  11048. intel_primary_formats = skl_primary_formats;
  11049. num_formats = ARRAY_SIZE(skl_primary_formats);
  11050. if (pipe < PIPE_C)
  11051. modifiers = skl_format_modifiers_ccs;
  11052. else
  11053. modifiers = skl_format_modifiers_noccs;
  11054. primary->update_plane = skylake_update_primary_plane;
  11055. primary->disable_plane = skylake_disable_primary_plane;
  11056. } else if (INTEL_GEN(dev_priv) >= 4) {
  11057. intel_primary_formats = i965_primary_formats;
  11058. num_formats = ARRAY_SIZE(i965_primary_formats);
  11059. modifiers = i9xx_format_modifiers;
  11060. primary->update_plane = i9xx_update_primary_plane;
  11061. primary->disable_plane = i9xx_disable_primary_plane;
  11062. } else {
  11063. intel_primary_formats = i8xx_primary_formats;
  11064. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11065. modifiers = i9xx_format_modifiers;
  11066. primary->update_plane = i9xx_update_primary_plane;
  11067. primary->disable_plane = i9xx_disable_primary_plane;
  11068. }
  11069. if (INTEL_GEN(dev_priv) >= 9)
  11070. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11071. 0, &intel_plane_funcs,
  11072. intel_primary_formats, num_formats,
  11073. modifiers,
  11074. DRM_PLANE_TYPE_PRIMARY,
  11075. "plane 1%c", pipe_name(pipe));
  11076. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11077. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11078. 0, &intel_plane_funcs,
  11079. intel_primary_formats, num_formats,
  11080. modifiers,
  11081. DRM_PLANE_TYPE_PRIMARY,
  11082. "primary %c", pipe_name(pipe));
  11083. else
  11084. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11085. 0, &intel_plane_funcs,
  11086. intel_primary_formats, num_formats,
  11087. modifiers,
  11088. DRM_PLANE_TYPE_PRIMARY,
  11089. "plane %c", plane_name(primary->plane));
  11090. if (ret)
  11091. goto fail;
  11092. if (INTEL_GEN(dev_priv) >= 9) {
  11093. supported_rotations =
  11094. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11095. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11096. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11097. supported_rotations =
  11098. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11099. DRM_MODE_REFLECT_X;
  11100. } else if (INTEL_GEN(dev_priv) >= 4) {
  11101. supported_rotations =
  11102. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11103. } else {
  11104. supported_rotations = DRM_MODE_ROTATE_0;
  11105. }
  11106. if (INTEL_GEN(dev_priv) >= 4)
  11107. drm_plane_create_rotation_property(&primary->base,
  11108. DRM_MODE_ROTATE_0,
  11109. supported_rotations);
  11110. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11111. return primary;
  11112. fail:
  11113. kfree(state);
  11114. kfree(primary);
  11115. return ERR_PTR(ret);
  11116. }
  11117. static struct intel_plane *
  11118. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11119. enum pipe pipe)
  11120. {
  11121. struct intel_plane *cursor = NULL;
  11122. struct intel_plane_state *state = NULL;
  11123. int ret;
  11124. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11125. if (!cursor) {
  11126. ret = -ENOMEM;
  11127. goto fail;
  11128. }
  11129. state = intel_create_plane_state(&cursor->base);
  11130. if (!state) {
  11131. ret = -ENOMEM;
  11132. goto fail;
  11133. }
  11134. cursor->base.state = &state->base;
  11135. cursor->can_scale = false;
  11136. cursor->max_downscale = 1;
  11137. cursor->pipe = pipe;
  11138. cursor->plane = pipe;
  11139. cursor->id = PLANE_CURSOR;
  11140. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11141. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11142. cursor->update_plane = i845_update_cursor;
  11143. cursor->disable_plane = i845_disable_cursor;
  11144. cursor->check_plane = i845_check_cursor;
  11145. } else {
  11146. cursor->update_plane = i9xx_update_cursor;
  11147. cursor->disable_plane = i9xx_disable_cursor;
  11148. cursor->check_plane = i9xx_check_cursor;
  11149. }
  11150. cursor->cursor.base = ~0;
  11151. cursor->cursor.cntl = ~0;
  11152. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11153. cursor->cursor.size = ~0;
  11154. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11155. 0, &intel_cursor_plane_funcs,
  11156. intel_cursor_formats,
  11157. ARRAY_SIZE(intel_cursor_formats),
  11158. cursor_format_modifiers,
  11159. DRM_PLANE_TYPE_CURSOR,
  11160. "cursor %c", pipe_name(pipe));
  11161. if (ret)
  11162. goto fail;
  11163. if (INTEL_GEN(dev_priv) >= 4)
  11164. drm_plane_create_rotation_property(&cursor->base,
  11165. DRM_MODE_ROTATE_0,
  11166. DRM_MODE_ROTATE_0 |
  11167. DRM_MODE_ROTATE_180);
  11168. if (INTEL_GEN(dev_priv) >= 9)
  11169. state->scaler_id = -1;
  11170. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11171. return cursor;
  11172. fail:
  11173. kfree(state);
  11174. kfree(cursor);
  11175. return ERR_PTR(ret);
  11176. }
  11177. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11178. struct intel_crtc_state *crtc_state)
  11179. {
  11180. struct intel_crtc_scaler_state *scaler_state =
  11181. &crtc_state->scaler_state;
  11182. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11183. int i;
  11184. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11185. if (!crtc->num_scalers)
  11186. return;
  11187. for (i = 0; i < crtc->num_scalers; i++) {
  11188. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11189. scaler->in_use = 0;
  11190. scaler->mode = PS_SCALER_MODE_DYN;
  11191. }
  11192. scaler_state->scaler_id = -1;
  11193. }
  11194. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11195. {
  11196. struct intel_crtc *intel_crtc;
  11197. struct intel_crtc_state *crtc_state = NULL;
  11198. struct intel_plane *primary = NULL;
  11199. struct intel_plane *cursor = NULL;
  11200. int sprite, ret;
  11201. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11202. if (!intel_crtc)
  11203. return -ENOMEM;
  11204. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11205. if (!crtc_state) {
  11206. ret = -ENOMEM;
  11207. goto fail;
  11208. }
  11209. intel_crtc->config = crtc_state;
  11210. intel_crtc->base.state = &crtc_state->base;
  11211. crtc_state->base.crtc = &intel_crtc->base;
  11212. primary = intel_primary_plane_create(dev_priv, pipe);
  11213. if (IS_ERR(primary)) {
  11214. ret = PTR_ERR(primary);
  11215. goto fail;
  11216. }
  11217. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11218. for_each_sprite(dev_priv, pipe, sprite) {
  11219. struct intel_plane *plane;
  11220. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11221. if (IS_ERR(plane)) {
  11222. ret = PTR_ERR(plane);
  11223. goto fail;
  11224. }
  11225. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11226. }
  11227. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11228. if (IS_ERR(cursor)) {
  11229. ret = PTR_ERR(cursor);
  11230. goto fail;
  11231. }
  11232. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11233. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11234. &primary->base, &cursor->base,
  11235. &intel_crtc_funcs,
  11236. "pipe %c", pipe_name(pipe));
  11237. if (ret)
  11238. goto fail;
  11239. intel_crtc->pipe = pipe;
  11240. intel_crtc->plane = primary->plane;
  11241. /* initialize shared scalers */
  11242. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11243. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11244. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11245. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11246. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11247. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11248. intel_color_init(&intel_crtc->base);
  11249. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11250. return 0;
  11251. fail:
  11252. /*
  11253. * drm_mode_config_cleanup() will free up any
  11254. * crtcs/planes already initialized.
  11255. */
  11256. kfree(crtc_state);
  11257. kfree(intel_crtc);
  11258. return ret;
  11259. }
  11260. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11261. {
  11262. struct drm_device *dev = connector->base.dev;
  11263. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11264. if (!connector->base.state->crtc)
  11265. return INVALID_PIPE;
  11266. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11267. }
  11268. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11269. struct drm_file *file)
  11270. {
  11271. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11272. struct drm_crtc *drmmode_crtc;
  11273. struct intel_crtc *crtc;
  11274. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11275. if (!drmmode_crtc)
  11276. return -ENOENT;
  11277. crtc = to_intel_crtc(drmmode_crtc);
  11278. pipe_from_crtc_id->pipe = crtc->pipe;
  11279. return 0;
  11280. }
  11281. static int intel_encoder_clones(struct intel_encoder *encoder)
  11282. {
  11283. struct drm_device *dev = encoder->base.dev;
  11284. struct intel_encoder *source_encoder;
  11285. int index_mask = 0;
  11286. int entry = 0;
  11287. for_each_intel_encoder(dev, source_encoder) {
  11288. if (encoders_cloneable(encoder, source_encoder))
  11289. index_mask |= (1 << entry);
  11290. entry++;
  11291. }
  11292. return index_mask;
  11293. }
  11294. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11295. {
  11296. if (!IS_MOBILE(dev_priv))
  11297. return false;
  11298. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11299. return false;
  11300. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11301. return false;
  11302. return true;
  11303. }
  11304. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11305. {
  11306. if (INTEL_GEN(dev_priv) >= 9)
  11307. return false;
  11308. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11309. return false;
  11310. if (IS_CHERRYVIEW(dev_priv))
  11311. return false;
  11312. if (HAS_PCH_LPT_H(dev_priv) &&
  11313. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11314. return false;
  11315. /* DDI E can't be used if DDI A requires 4 lanes */
  11316. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11317. return false;
  11318. if (!dev_priv->vbt.int_crt_support)
  11319. return false;
  11320. return true;
  11321. }
  11322. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11323. {
  11324. int pps_num;
  11325. int pps_idx;
  11326. if (HAS_DDI(dev_priv))
  11327. return;
  11328. /*
  11329. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11330. * everywhere where registers can be write protected.
  11331. */
  11332. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11333. pps_num = 2;
  11334. else
  11335. pps_num = 1;
  11336. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11337. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11338. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11339. I915_WRITE(PP_CONTROL(pps_idx), val);
  11340. }
  11341. }
  11342. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11343. {
  11344. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11345. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11346. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11347. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11348. else
  11349. dev_priv->pps_mmio_base = PPS_BASE;
  11350. intel_pps_unlock_regs_wa(dev_priv);
  11351. }
  11352. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11353. {
  11354. struct intel_encoder *encoder;
  11355. bool dpd_is_edp = false;
  11356. intel_pps_init(dev_priv);
  11357. /*
  11358. * intel_edp_init_connector() depends on this completing first, to
  11359. * prevent the registeration of both eDP and LVDS and the incorrect
  11360. * sharing of the PPS.
  11361. */
  11362. intel_lvds_init(dev_priv);
  11363. if (intel_crt_present(dev_priv))
  11364. intel_crt_init(dev_priv);
  11365. if (IS_GEN9_LP(dev_priv)) {
  11366. /*
  11367. * FIXME: Broxton doesn't support port detection via the
  11368. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11369. * detect the ports.
  11370. */
  11371. intel_ddi_init(dev_priv, PORT_A);
  11372. intel_ddi_init(dev_priv, PORT_B);
  11373. intel_ddi_init(dev_priv, PORT_C);
  11374. intel_dsi_init(dev_priv);
  11375. } else if (HAS_DDI(dev_priv)) {
  11376. int found;
  11377. /*
  11378. * Haswell uses DDI functions to detect digital outputs.
  11379. * On SKL pre-D0 the strap isn't connected, so we assume
  11380. * it's there.
  11381. */
  11382. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11383. /* WaIgnoreDDIAStrap: skl */
  11384. if (found || IS_GEN9_BC(dev_priv))
  11385. intel_ddi_init(dev_priv, PORT_A);
  11386. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11387. * register */
  11388. found = I915_READ(SFUSE_STRAP);
  11389. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11390. intel_ddi_init(dev_priv, PORT_B);
  11391. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11392. intel_ddi_init(dev_priv, PORT_C);
  11393. if (found & SFUSE_STRAP_DDID_DETECTED)
  11394. intel_ddi_init(dev_priv, PORT_D);
  11395. /*
  11396. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11397. */
  11398. if (IS_GEN9_BC(dev_priv) &&
  11399. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11400. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11401. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11402. intel_ddi_init(dev_priv, PORT_E);
  11403. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11404. int found;
  11405. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11406. if (has_edp_a(dev_priv))
  11407. intel_dp_init(dev_priv, DP_A, PORT_A);
  11408. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11409. /* PCH SDVOB multiplex with HDMIB */
  11410. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11411. if (!found)
  11412. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11413. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11414. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11415. }
  11416. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11417. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11418. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11419. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11420. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11421. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11422. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11423. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11424. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11425. bool has_edp, has_port;
  11426. /*
  11427. * The DP_DETECTED bit is the latched state of the DDC
  11428. * SDA pin at boot. However since eDP doesn't require DDC
  11429. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11430. * eDP ports may have been muxed to an alternate function.
  11431. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11432. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11433. * detect eDP ports.
  11434. *
  11435. * Sadly the straps seem to be missing sometimes even for HDMI
  11436. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11437. * and VBT for the presence of the port. Additionally we can't
  11438. * trust the port type the VBT declares as we've seen at least
  11439. * HDMI ports that the VBT claim are DP or eDP.
  11440. */
  11441. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11442. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11443. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11444. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11445. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11446. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11447. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11448. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11449. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11450. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11451. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11452. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11453. if (IS_CHERRYVIEW(dev_priv)) {
  11454. /*
  11455. * eDP not supported on port D,
  11456. * so no need to worry about it
  11457. */
  11458. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11459. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11460. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11461. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11462. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11463. }
  11464. intel_dsi_init(dev_priv);
  11465. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11466. bool found = false;
  11467. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11468. DRM_DEBUG_KMS("probing SDVOB\n");
  11469. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11470. if (!found && IS_G4X(dev_priv)) {
  11471. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11472. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11473. }
  11474. if (!found && IS_G4X(dev_priv))
  11475. intel_dp_init(dev_priv, DP_B, PORT_B);
  11476. }
  11477. /* Before G4X SDVOC doesn't have its own detect register */
  11478. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11479. DRM_DEBUG_KMS("probing SDVOC\n");
  11480. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11481. }
  11482. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11483. if (IS_G4X(dev_priv)) {
  11484. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11485. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11486. }
  11487. if (IS_G4X(dev_priv))
  11488. intel_dp_init(dev_priv, DP_C, PORT_C);
  11489. }
  11490. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11491. intel_dp_init(dev_priv, DP_D, PORT_D);
  11492. } else if (IS_GEN2(dev_priv))
  11493. intel_dvo_init(dev_priv);
  11494. if (SUPPORTS_TV(dev_priv))
  11495. intel_tv_init(dev_priv);
  11496. intel_psr_init(dev_priv);
  11497. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11498. encoder->base.possible_crtcs = encoder->crtc_mask;
  11499. encoder->base.possible_clones =
  11500. intel_encoder_clones(encoder);
  11501. }
  11502. intel_init_pch_refclk(dev_priv);
  11503. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11504. }
  11505. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11506. {
  11507. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11508. drm_framebuffer_cleanup(fb);
  11509. i915_gem_object_lock(intel_fb->obj);
  11510. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11511. i915_gem_object_unlock(intel_fb->obj);
  11512. i915_gem_object_put(intel_fb->obj);
  11513. kfree(intel_fb);
  11514. }
  11515. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11516. struct drm_file *file,
  11517. unsigned int *handle)
  11518. {
  11519. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11520. struct drm_i915_gem_object *obj = intel_fb->obj;
  11521. if (obj->userptr.mm) {
  11522. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11523. return -EINVAL;
  11524. }
  11525. return drm_gem_handle_create(file, &obj->base, handle);
  11526. }
  11527. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11528. struct drm_file *file,
  11529. unsigned flags, unsigned color,
  11530. struct drm_clip_rect *clips,
  11531. unsigned num_clips)
  11532. {
  11533. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11534. i915_gem_object_flush_if_display(obj);
  11535. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11536. return 0;
  11537. }
  11538. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11539. .destroy = intel_user_framebuffer_destroy,
  11540. .create_handle = intel_user_framebuffer_create_handle,
  11541. .dirty = intel_user_framebuffer_dirty,
  11542. };
  11543. static
  11544. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11545. uint64_t fb_modifier, uint32_t pixel_format)
  11546. {
  11547. u32 gen = INTEL_GEN(dev_priv);
  11548. if (gen >= 9) {
  11549. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11550. /* "The stride in bytes must not exceed the of the size of 8K
  11551. * pixels and 32K bytes."
  11552. */
  11553. return min(8192 * cpp, 32768);
  11554. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11555. return 32*1024;
  11556. } else if (gen >= 4) {
  11557. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11558. return 16*1024;
  11559. else
  11560. return 32*1024;
  11561. } else if (gen >= 3) {
  11562. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11563. return 8*1024;
  11564. else
  11565. return 16*1024;
  11566. } else {
  11567. /* XXX DSPC is limited to 4k tiled */
  11568. return 8*1024;
  11569. }
  11570. }
  11571. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11572. struct drm_i915_gem_object *obj,
  11573. struct drm_mode_fb_cmd2 *mode_cmd)
  11574. {
  11575. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11576. struct drm_framebuffer *fb = &intel_fb->base;
  11577. struct drm_format_name_buf format_name;
  11578. u32 pitch_limit;
  11579. unsigned int tiling, stride;
  11580. int ret = -EINVAL;
  11581. int i;
  11582. i915_gem_object_lock(obj);
  11583. obj->framebuffer_references++;
  11584. tiling = i915_gem_object_get_tiling(obj);
  11585. stride = i915_gem_object_get_stride(obj);
  11586. i915_gem_object_unlock(obj);
  11587. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11588. /*
  11589. * If there's a fence, enforce that
  11590. * the fb modifier and tiling mode match.
  11591. */
  11592. if (tiling != I915_TILING_NONE &&
  11593. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11594. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11595. goto err;
  11596. }
  11597. } else {
  11598. if (tiling == I915_TILING_X) {
  11599. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11600. } else if (tiling == I915_TILING_Y) {
  11601. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11602. goto err;
  11603. }
  11604. }
  11605. /* Passed in modifier sanity checking. */
  11606. switch (mode_cmd->modifier[0]) {
  11607. case I915_FORMAT_MOD_Y_TILED_CCS:
  11608. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11609. switch (mode_cmd->pixel_format) {
  11610. case DRM_FORMAT_XBGR8888:
  11611. case DRM_FORMAT_ABGR8888:
  11612. case DRM_FORMAT_XRGB8888:
  11613. case DRM_FORMAT_ARGB8888:
  11614. break;
  11615. default:
  11616. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11617. goto err;
  11618. }
  11619. /* fall through */
  11620. case I915_FORMAT_MOD_Y_TILED:
  11621. case I915_FORMAT_MOD_Yf_TILED:
  11622. if (INTEL_GEN(dev_priv) < 9) {
  11623. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11624. mode_cmd->modifier[0]);
  11625. goto err;
  11626. }
  11627. case DRM_FORMAT_MOD_LINEAR:
  11628. case I915_FORMAT_MOD_X_TILED:
  11629. break;
  11630. default:
  11631. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11632. mode_cmd->modifier[0]);
  11633. goto err;
  11634. }
  11635. /*
  11636. * gen2/3 display engine uses the fence if present,
  11637. * so the tiling mode must match the fb modifier exactly.
  11638. */
  11639. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11640. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11641. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11642. goto err;
  11643. }
  11644. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11645. mode_cmd->pixel_format);
  11646. if (mode_cmd->pitches[0] > pitch_limit) {
  11647. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11648. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11649. "tiled" : "linear",
  11650. mode_cmd->pitches[0], pitch_limit);
  11651. goto err;
  11652. }
  11653. /*
  11654. * If there's a fence, enforce that
  11655. * the fb pitch and fence stride match.
  11656. */
  11657. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11658. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11659. mode_cmd->pitches[0], stride);
  11660. goto err;
  11661. }
  11662. /* Reject formats not supported by any plane early. */
  11663. switch (mode_cmd->pixel_format) {
  11664. case DRM_FORMAT_C8:
  11665. case DRM_FORMAT_RGB565:
  11666. case DRM_FORMAT_XRGB8888:
  11667. case DRM_FORMAT_ARGB8888:
  11668. break;
  11669. case DRM_FORMAT_XRGB1555:
  11670. if (INTEL_GEN(dev_priv) > 3) {
  11671. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11672. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11673. goto err;
  11674. }
  11675. break;
  11676. case DRM_FORMAT_ABGR8888:
  11677. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11678. INTEL_GEN(dev_priv) < 9) {
  11679. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11680. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11681. goto err;
  11682. }
  11683. break;
  11684. case DRM_FORMAT_XBGR8888:
  11685. case DRM_FORMAT_XRGB2101010:
  11686. case DRM_FORMAT_XBGR2101010:
  11687. if (INTEL_GEN(dev_priv) < 4) {
  11688. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11689. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11690. goto err;
  11691. }
  11692. break;
  11693. case DRM_FORMAT_ABGR2101010:
  11694. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11695. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11696. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11697. goto err;
  11698. }
  11699. break;
  11700. case DRM_FORMAT_YUYV:
  11701. case DRM_FORMAT_UYVY:
  11702. case DRM_FORMAT_YVYU:
  11703. case DRM_FORMAT_VYUY:
  11704. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11705. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11706. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11707. goto err;
  11708. }
  11709. break;
  11710. default:
  11711. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11712. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11713. goto err;
  11714. }
  11715. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11716. if (mode_cmd->offsets[0] != 0)
  11717. goto err;
  11718. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11719. for (i = 0; i < fb->format->num_planes; i++) {
  11720. u32 stride_alignment;
  11721. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11722. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11723. goto err;
  11724. }
  11725. stride_alignment = intel_fb_stride_alignment(fb, i);
  11726. /*
  11727. * Display WA #0531: skl,bxt,kbl,glk
  11728. *
  11729. * Render decompression and plane width > 3840
  11730. * combined with horizontal panning requires the
  11731. * plane stride to be a multiple of 4. We'll just
  11732. * require the entire fb to accommodate that to avoid
  11733. * potential runtime errors at plane configuration time.
  11734. */
  11735. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11736. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11737. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11738. stride_alignment *= 4;
  11739. if (fb->pitches[i] & (stride_alignment - 1)) {
  11740. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11741. i, fb->pitches[i], stride_alignment);
  11742. goto err;
  11743. }
  11744. }
  11745. intel_fb->obj = obj;
  11746. ret = intel_fill_fb_info(dev_priv, fb);
  11747. if (ret)
  11748. goto err;
  11749. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11750. if (ret) {
  11751. DRM_ERROR("framebuffer init failed %d\n", ret);
  11752. goto err;
  11753. }
  11754. return 0;
  11755. err:
  11756. i915_gem_object_lock(obj);
  11757. obj->framebuffer_references--;
  11758. i915_gem_object_unlock(obj);
  11759. return ret;
  11760. }
  11761. static struct drm_framebuffer *
  11762. intel_user_framebuffer_create(struct drm_device *dev,
  11763. struct drm_file *filp,
  11764. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11765. {
  11766. struct drm_framebuffer *fb;
  11767. struct drm_i915_gem_object *obj;
  11768. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11769. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11770. if (!obj)
  11771. return ERR_PTR(-ENOENT);
  11772. fb = intel_framebuffer_create(obj, &mode_cmd);
  11773. if (IS_ERR(fb))
  11774. i915_gem_object_put(obj);
  11775. return fb;
  11776. }
  11777. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11778. {
  11779. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11780. drm_atomic_state_default_release(state);
  11781. i915_sw_fence_fini(&intel_state->commit_ready);
  11782. kfree(state);
  11783. }
  11784. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11785. .fb_create = intel_user_framebuffer_create,
  11786. .get_format_info = intel_get_format_info,
  11787. .output_poll_changed = intel_fbdev_output_poll_changed,
  11788. .atomic_check = intel_atomic_check,
  11789. .atomic_commit = intel_atomic_commit,
  11790. .atomic_state_alloc = intel_atomic_state_alloc,
  11791. .atomic_state_clear = intel_atomic_state_clear,
  11792. .atomic_state_free = intel_atomic_state_free,
  11793. };
  11794. /**
  11795. * intel_init_display_hooks - initialize the display modesetting hooks
  11796. * @dev_priv: device private
  11797. */
  11798. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11799. {
  11800. intel_init_cdclk_hooks(dev_priv);
  11801. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11802. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11803. dev_priv->display.get_initial_plane_config =
  11804. skylake_get_initial_plane_config;
  11805. dev_priv->display.crtc_compute_clock =
  11806. haswell_crtc_compute_clock;
  11807. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11808. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11809. } else if (HAS_DDI(dev_priv)) {
  11810. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11811. dev_priv->display.get_initial_plane_config =
  11812. ironlake_get_initial_plane_config;
  11813. dev_priv->display.crtc_compute_clock =
  11814. haswell_crtc_compute_clock;
  11815. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11816. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11817. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11818. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11819. dev_priv->display.get_initial_plane_config =
  11820. ironlake_get_initial_plane_config;
  11821. dev_priv->display.crtc_compute_clock =
  11822. ironlake_crtc_compute_clock;
  11823. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11824. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11825. } else if (IS_CHERRYVIEW(dev_priv)) {
  11826. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11827. dev_priv->display.get_initial_plane_config =
  11828. i9xx_get_initial_plane_config;
  11829. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11830. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11831. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11832. } else if (IS_VALLEYVIEW(dev_priv)) {
  11833. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11834. dev_priv->display.get_initial_plane_config =
  11835. i9xx_get_initial_plane_config;
  11836. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11837. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11838. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11839. } else if (IS_G4X(dev_priv)) {
  11840. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11841. dev_priv->display.get_initial_plane_config =
  11842. i9xx_get_initial_plane_config;
  11843. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11844. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11845. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11846. } else if (IS_PINEVIEW(dev_priv)) {
  11847. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11848. dev_priv->display.get_initial_plane_config =
  11849. i9xx_get_initial_plane_config;
  11850. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11851. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11852. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11853. } else if (!IS_GEN2(dev_priv)) {
  11854. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11855. dev_priv->display.get_initial_plane_config =
  11856. i9xx_get_initial_plane_config;
  11857. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11858. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11859. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11860. } else {
  11861. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11862. dev_priv->display.get_initial_plane_config =
  11863. i9xx_get_initial_plane_config;
  11864. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11865. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11866. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11867. }
  11868. if (IS_GEN5(dev_priv)) {
  11869. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11870. } else if (IS_GEN6(dev_priv)) {
  11871. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11872. } else if (IS_IVYBRIDGE(dev_priv)) {
  11873. /* FIXME: detect B0+ stepping and use auto training */
  11874. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11875. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11876. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11877. }
  11878. if (dev_priv->info.gen >= 9)
  11879. dev_priv->display.update_crtcs = skl_update_crtcs;
  11880. else
  11881. dev_priv->display.update_crtcs = intel_update_crtcs;
  11882. }
  11883. /*
  11884. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11885. */
  11886. static void quirk_ssc_force_disable(struct drm_device *dev)
  11887. {
  11888. struct drm_i915_private *dev_priv = to_i915(dev);
  11889. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11890. DRM_INFO("applying lvds SSC disable quirk\n");
  11891. }
  11892. /*
  11893. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11894. * brightness value
  11895. */
  11896. static void quirk_invert_brightness(struct drm_device *dev)
  11897. {
  11898. struct drm_i915_private *dev_priv = to_i915(dev);
  11899. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11900. DRM_INFO("applying inverted panel brightness quirk\n");
  11901. }
  11902. /* Some VBT's incorrectly indicate no backlight is present */
  11903. static void quirk_backlight_present(struct drm_device *dev)
  11904. {
  11905. struct drm_i915_private *dev_priv = to_i915(dev);
  11906. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11907. DRM_INFO("applying backlight present quirk\n");
  11908. }
  11909. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11910. * which is 300 ms greater than eDP spec T12 min.
  11911. */
  11912. static void quirk_increase_t12_delay(struct drm_device *dev)
  11913. {
  11914. struct drm_i915_private *dev_priv = to_i915(dev);
  11915. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11916. DRM_INFO("Applying T12 delay quirk\n");
  11917. }
  11918. struct intel_quirk {
  11919. int device;
  11920. int subsystem_vendor;
  11921. int subsystem_device;
  11922. void (*hook)(struct drm_device *dev);
  11923. };
  11924. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11925. struct intel_dmi_quirk {
  11926. void (*hook)(struct drm_device *dev);
  11927. const struct dmi_system_id (*dmi_id_list)[];
  11928. };
  11929. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11930. {
  11931. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11932. return 1;
  11933. }
  11934. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11935. {
  11936. .dmi_id_list = &(const struct dmi_system_id[]) {
  11937. {
  11938. .callback = intel_dmi_reverse_brightness,
  11939. .ident = "NCR Corporation",
  11940. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11941. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11942. },
  11943. },
  11944. { } /* terminating entry */
  11945. },
  11946. .hook = quirk_invert_brightness,
  11947. },
  11948. };
  11949. static struct intel_quirk intel_quirks[] = {
  11950. /* Lenovo U160 cannot use SSC on LVDS */
  11951. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11952. /* Sony Vaio Y cannot use SSC on LVDS */
  11953. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11954. /* Acer Aspire 5734Z must invert backlight brightness */
  11955. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11956. /* Acer/eMachines G725 */
  11957. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11958. /* Acer/eMachines e725 */
  11959. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11960. /* Acer/Packard Bell NCL20 */
  11961. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11962. /* Acer Aspire 4736Z */
  11963. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11964. /* Acer Aspire 5336 */
  11965. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11966. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11967. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11968. /* Acer C720 Chromebook (Core i3 4005U) */
  11969. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11970. /* Apple Macbook 2,1 (Core 2 T7400) */
  11971. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11972. /* Apple Macbook 4,1 */
  11973. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11974. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11975. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11976. /* HP Chromebook 14 (Celeron 2955U) */
  11977. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11978. /* Dell Chromebook 11 */
  11979. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11980. /* Dell Chromebook 11 (2015 version) */
  11981. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11982. /* Toshiba Satellite P50-C-18C */
  11983. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11984. };
  11985. static void intel_init_quirks(struct drm_device *dev)
  11986. {
  11987. struct pci_dev *d = dev->pdev;
  11988. int i;
  11989. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11990. struct intel_quirk *q = &intel_quirks[i];
  11991. if (d->device == q->device &&
  11992. (d->subsystem_vendor == q->subsystem_vendor ||
  11993. q->subsystem_vendor == PCI_ANY_ID) &&
  11994. (d->subsystem_device == q->subsystem_device ||
  11995. q->subsystem_device == PCI_ANY_ID))
  11996. q->hook(dev);
  11997. }
  11998. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11999. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12000. intel_dmi_quirks[i].hook(dev);
  12001. }
  12002. }
  12003. /* Disable the VGA plane that we never use */
  12004. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12005. {
  12006. struct pci_dev *pdev = dev_priv->drm.pdev;
  12007. u8 sr1;
  12008. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12009. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12010. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12011. outb(SR01, VGA_SR_INDEX);
  12012. sr1 = inb(VGA_SR_DATA);
  12013. outb(sr1 | 1<<5, VGA_SR_DATA);
  12014. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12015. udelay(300);
  12016. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12017. POSTING_READ(vga_reg);
  12018. }
  12019. void intel_modeset_init_hw(struct drm_device *dev)
  12020. {
  12021. struct drm_i915_private *dev_priv = to_i915(dev);
  12022. intel_update_cdclk(dev_priv);
  12023. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12024. intel_init_clock_gating(dev_priv);
  12025. }
  12026. /*
  12027. * Calculate what we think the watermarks should be for the state we've read
  12028. * out of the hardware and then immediately program those watermarks so that
  12029. * we ensure the hardware settings match our internal state.
  12030. *
  12031. * We can calculate what we think WM's should be by creating a duplicate of the
  12032. * current state (which was constructed during hardware readout) and running it
  12033. * through the atomic check code to calculate new watermark values in the
  12034. * state object.
  12035. */
  12036. static void sanitize_watermarks(struct drm_device *dev)
  12037. {
  12038. struct drm_i915_private *dev_priv = to_i915(dev);
  12039. struct drm_atomic_state *state;
  12040. struct intel_atomic_state *intel_state;
  12041. struct drm_crtc *crtc;
  12042. struct drm_crtc_state *cstate;
  12043. struct drm_modeset_acquire_ctx ctx;
  12044. int ret;
  12045. int i;
  12046. /* Only supported on platforms that use atomic watermark design */
  12047. if (!dev_priv->display.optimize_watermarks)
  12048. return;
  12049. /*
  12050. * We need to hold connection_mutex before calling duplicate_state so
  12051. * that the connector loop is protected.
  12052. */
  12053. drm_modeset_acquire_init(&ctx, 0);
  12054. retry:
  12055. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12056. if (ret == -EDEADLK) {
  12057. drm_modeset_backoff(&ctx);
  12058. goto retry;
  12059. } else if (WARN_ON(ret)) {
  12060. goto fail;
  12061. }
  12062. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12063. if (WARN_ON(IS_ERR(state)))
  12064. goto fail;
  12065. intel_state = to_intel_atomic_state(state);
  12066. /*
  12067. * Hardware readout is the only time we don't want to calculate
  12068. * intermediate watermarks (since we don't trust the current
  12069. * watermarks).
  12070. */
  12071. if (!HAS_GMCH_DISPLAY(dev_priv))
  12072. intel_state->skip_intermediate_wm = true;
  12073. ret = intel_atomic_check(dev, state);
  12074. if (ret) {
  12075. /*
  12076. * If we fail here, it means that the hardware appears to be
  12077. * programmed in a way that shouldn't be possible, given our
  12078. * understanding of watermark requirements. This might mean a
  12079. * mistake in the hardware readout code or a mistake in the
  12080. * watermark calculations for a given platform. Raise a WARN
  12081. * so that this is noticeable.
  12082. *
  12083. * If this actually happens, we'll have to just leave the
  12084. * BIOS-programmed watermarks untouched and hope for the best.
  12085. */
  12086. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12087. goto put_state;
  12088. }
  12089. /* Write calculated watermark values back */
  12090. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12091. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12092. cs->wm.need_postvbl_update = true;
  12093. dev_priv->display.optimize_watermarks(intel_state, cs);
  12094. }
  12095. put_state:
  12096. drm_atomic_state_put(state);
  12097. fail:
  12098. drm_modeset_drop_locks(&ctx);
  12099. drm_modeset_acquire_fini(&ctx);
  12100. }
  12101. int intel_modeset_init(struct drm_device *dev)
  12102. {
  12103. struct drm_i915_private *dev_priv = to_i915(dev);
  12104. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12105. enum pipe pipe;
  12106. struct intel_crtc *crtc;
  12107. drm_mode_config_init(dev);
  12108. dev->mode_config.min_width = 0;
  12109. dev->mode_config.min_height = 0;
  12110. dev->mode_config.preferred_depth = 24;
  12111. dev->mode_config.prefer_shadow = 1;
  12112. dev->mode_config.allow_fb_modifiers = true;
  12113. dev->mode_config.funcs = &intel_mode_funcs;
  12114. init_llist_head(&dev_priv->atomic_helper.free_list);
  12115. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12116. intel_atomic_helper_free_state_worker);
  12117. intel_init_quirks(dev);
  12118. intel_init_pm(dev_priv);
  12119. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12120. return 0;
  12121. /*
  12122. * There may be no VBT; and if the BIOS enabled SSC we can
  12123. * just keep using it to avoid unnecessary flicker. Whereas if the
  12124. * BIOS isn't using it, don't assume it will work even if the VBT
  12125. * indicates as much.
  12126. */
  12127. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12128. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12129. DREF_SSC1_ENABLE);
  12130. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12131. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12132. bios_lvds_use_ssc ? "en" : "dis",
  12133. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12134. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12135. }
  12136. }
  12137. if (IS_GEN2(dev_priv)) {
  12138. dev->mode_config.max_width = 2048;
  12139. dev->mode_config.max_height = 2048;
  12140. } else if (IS_GEN3(dev_priv)) {
  12141. dev->mode_config.max_width = 4096;
  12142. dev->mode_config.max_height = 4096;
  12143. } else {
  12144. dev->mode_config.max_width = 8192;
  12145. dev->mode_config.max_height = 8192;
  12146. }
  12147. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12148. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12149. dev->mode_config.cursor_height = 1023;
  12150. } else if (IS_GEN2(dev_priv)) {
  12151. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12152. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12153. } else {
  12154. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12155. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12156. }
  12157. dev->mode_config.fb_base = ggtt->mappable_base;
  12158. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12159. INTEL_INFO(dev_priv)->num_pipes,
  12160. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12161. for_each_pipe(dev_priv, pipe) {
  12162. int ret;
  12163. ret = intel_crtc_init(dev_priv, pipe);
  12164. if (ret) {
  12165. drm_mode_config_cleanup(dev);
  12166. return ret;
  12167. }
  12168. }
  12169. intel_shared_dpll_init(dev);
  12170. intel_update_czclk(dev_priv);
  12171. intel_modeset_init_hw(dev);
  12172. if (dev_priv->max_cdclk_freq == 0)
  12173. intel_update_max_cdclk(dev_priv);
  12174. /* Just disable it once at startup */
  12175. i915_disable_vga(dev_priv);
  12176. intel_setup_outputs(dev_priv);
  12177. drm_modeset_lock_all(dev);
  12178. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12179. drm_modeset_unlock_all(dev);
  12180. for_each_intel_crtc(dev, crtc) {
  12181. struct intel_initial_plane_config plane_config = {};
  12182. if (!crtc->active)
  12183. continue;
  12184. /*
  12185. * Note that reserving the BIOS fb up front prevents us
  12186. * from stuffing other stolen allocations like the ring
  12187. * on top. This prevents some ugliness at boot time, and
  12188. * can even allow for smooth boot transitions if the BIOS
  12189. * fb is large enough for the active pipe configuration.
  12190. */
  12191. dev_priv->display.get_initial_plane_config(crtc,
  12192. &plane_config);
  12193. /*
  12194. * If the fb is shared between multiple heads, we'll
  12195. * just get the first one.
  12196. */
  12197. intel_find_initial_plane_obj(crtc, &plane_config);
  12198. }
  12199. /*
  12200. * Make sure hardware watermarks really match the state we read out.
  12201. * Note that we need to do this after reconstructing the BIOS fb's
  12202. * since the watermark calculation done here will use pstate->fb.
  12203. */
  12204. if (!HAS_GMCH_DISPLAY(dev_priv))
  12205. sanitize_watermarks(dev);
  12206. return 0;
  12207. }
  12208. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12209. {
  12210. /* 640x480@60Hz, ~25175 kHz */
  12211. struct dpll clock = {
  12212. .m1 = 18,
  12213. .m2 = 7,
  12214. .p1 = 13,
  12215. .p2 = 4,
  12216. .n = 2,
  12217. };
  12218. u32 dpll, fp;
  12219. int i;
  12220. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12221. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12222. pipe_name(pipe), clock.vco, clock.dot);
  12223. fp = i9xx_dpll_compute_fp(&clock);
  12224. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12225. DPLL_VGA_MODE_DIS |
  12226. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12227. PLL_P2_DIVIDE_BY_4 |
  12228. PLL_REF_INPUT_DREFCLK |
  12229. DPLL_VCO_ENABLE;
  12230. I915_WRITE(FP0(pipe), fp);
  12231. I915_WRITE(FP1(pipe), fp);
  12232. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12233. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12234. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12235. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12236. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12237. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12238. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12239. /*
  12240. * Apparently we need to have VGA mode enabled prior to changing
  12241. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12242. * dividers, even though the register value does change.
  12243. */
  12244. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12245. I915_WRITE(DPLL(pipe), dpll);
  12246. /* Wait for the clocks to stabilize. */
  12247. POSTING_READ(DPLL(pipe));
  12248. udelay(150);
  12249. /* The pixel multiplier can only be updated once the
  12250. * DPLL is enabled and the clocks are stable.
  12251. *
  12252. * So write it again.
  12253. */
  12254. I915_WRITE(DPLL(pipe), dpll);
  12255. /* We do this three times for luck */
  12256. for (i = 0; i < 3 ; i++) {
  12257. I915_WRITE(DPLL(pipe), dpll);
  12258. POSTING_READ(DPLL(pipe));
  12259. udelay(150); /* wait for warmup */
  12260. }
  12261. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12262. POSTING_READ(PIPECONF(pipe));
  12263. }
  12264. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12265. {
  12266. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12267. pipe_name(pipe));
  12268. assert_plane_disabled(dev_priv, PLANE_A);
  12269. assert_plane_disabled(dev_priv, PLANE_B);
  12270. I915_WRITE(PIPECONF(pipe), 0);
  12271. POSTING_READ(PIPECONF(pipe));
  12272. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  12273. DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
  12274. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12275. POSTING_READ(DPLL(pipe));
  12276. }
  12277. static bool
  12278. intel_check_plane_mapping(struct intel_crtc *crtc)
  12279. {
  12280. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12281. u32 val;
  12282. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12283. return true;
  12284. val = I915_READ(DSPCNTR(!crtc->plane));
  12285. if ((val & DISPLAY_PLANE_ENABLE) &&
  12286. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12287. return false;
  12288. return true;
  12289. }
  12290. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12291. {
  12292. struct drm_device *dev = crtc->base.dev;
  12293. struct intel_encoder *encoder;
  12294. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12295. return true;
  12296. return false;
  12297. }
  12298. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12299. {
  12300. struct drm_device *dev = encoder->base.dev;
  12301. struct intel_connector *connector;
  12302. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12303. return connector;
  12304. return NULL;
  12305. }
  12306. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12307. enum transcoder pch_transcoder)
  12308. {
  12309. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12310. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12311. }
  12312. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12313. struct drm_modeset_acquire_ctx *ctx)
  12314. {
  12315. struct drm_device *dev = crtc->base.dev;
  12316. struct drm_i915_private *dev_priv = to_i915(dev);
  12317. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12318. /* Clear any frame start delays used for debugging left by the BIOS */
  12319. if (!transcoder_is_dsi(cpu_transcoder)) {
  12320. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12321. I915_WRITE(reg,
  12322. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12323. }
  12324. /* restore vblank interrupts to correct state */
  12325. drm_crtc_vblank_reset(&crtc->base);
  12326. if (crtc->active) {
  12327. struct intel_plane *plane;
  12328. drm_crtc_vblank_on(&crtc->base);
  12329. /* Disable everything but the primary plane */
  12330. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12331. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12332. continue;
  12333. trace_intel_disable_plane(&plane->base, crtc);
  12334. plane->disable_plane(plane, crtc);
  12335. }
  12336. }
  12337. /* We need to sanitize the plane -> pipe mapping first because this will
  12338. * disable the crtc (and hence change the state) if it is wrong. Note
  12339. * that gen4+ has a fixed plane -> pipe mapping. */
  12340. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12341. bool plane;
  12342. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12343. crtc->base.base.id, crtc->base.name);
  12344. /* Pipe has the wrong plane attached and the plane is active.
  12345. * Temporarily change the plane mapping and disable everything
  12346. * ... */
  12347. plane = crtc->plane;
  12348. crtc->base.primary->state->visible = true;
  12349. crtc->plane = !plane;
  12350. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12351. crtc->plane = plane;
  12352. }
  12353. /* Adjust the state of the output pipe according to whether we
  12354. * have active connectors/encoders. */
  12355. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12356. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12357. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12358. /*
  12359. * We start out with underrun reporting disabled to avoid races.
  12360. * For correct bookkeeping mark this on active crtcs.
  12361. *
  12362. * Also on gmch platforms we dont have any hardware bits to
  12363. * disable the underrun reporting. Which means we need to start
  12364. * out with underrun reporting disabled also on inactive pipes,
  12365. * since otherwise we'll complain about the garbage we read when
  12366. * e.g. coming up after runtime pm.
  12367. *
  12368. * No protection against concurrent access is required - at
  12369. * worst a fifo underrun happens which also sets this to false.
  12370. */
  12371. crtc->cpu_fifo_underrun_disabled = true;
  12372. /*
  12373. * We track the PCH trancoder underrun reporting state
  12374. * within the crtc. With crtc for pipe A housing the underrun
  12375. * reporting state for PCH transcoder A, crtc for pipe B housing
  12376. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12377. * and marking underrun reporting as disabled for the non-existing
  12378. * PCH transcoders B and C would prevent enabling the south
  12379. * error interrupt (see cpt_can_enable_serr_int()).
  12380. */
  12381. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12382. crtc->pch_fifo_underrun_disabled = true;
  12383. }
  12384. }
  12385. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12386. {
  12387. struct intel_connector *connector;
  12388. /* We need to check both for a crtc link (meaning that the
  12389. * encoder is active and trying to read from a pipe) and the
  12390. * pipe itself being active. */
  12391. bool has_active_crtc = encoder->base.crtc &&
  12392. to_intel_crtc(encoder->base.crtc)->active;
  12393. connector = intel_encoder_find_connector(encoder);
  12394. if (connector && !has_active_crtc) {
  12395. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12396. encoder->base.base.id,
  12397. encoder->base.name);
  12398. /* Connector is active, but has no active pipe. This is
  12399. * fallout from our resume register restoring. Disable
  12400. * the encoder manually again. */
  12401. if (encoder->base.crtc) {
  12402. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12403. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12404. encoder->base.base.id,
  12405. encoder->base.name);
  12406. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12407. if (encoder->post_disable)
  12408. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12409. }
  12410. encoder->base.crtc = NULL;
  12411. /* Inconsistent output/port/pipe state happens presumably due to
  12412. * a bug in one of the get_hw_state functions. Or someplace else
  12413. * in our code, like the register restore mess on resume. Clamp
  12414. * things to off as a safer default. */
  12415. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12416. connector->base.encoder = NULL;
  12417. }
  12418. /* Enabled encoders without active connectors will be fixed in
  12419. * the crtc fixup. */
  12420. }
  12421. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12422. {
  12423. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12424. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12425. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12426. i915_disable_vga(dev_priv);
  12427. }
  12428. }
  12429. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12430. {
  12431. /* This function can be called both from intel_modeset_setup_hw_state or
  12432. * at a very early point in our resume sequence, where the power well
  12433. * structures are not yet restored. Since this function is at a very
  12434. * paranoid "someone might have enabled VGA while we were not looking"
  12435. * level, just check if the power well is enabled instead of trying to
  12436. * follow the "don't touch the power well if we don't need it" policy
  12437. * the rest of the driver uses. */
  12438. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12439. return;
  12440. i915_redisable_vga_power_on(dev_priv);
  12441. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12442. }
  12443. static bool primary_get_hw_state(struct intel_plane *plane)
  12444. {
  12445. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12446. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12447. }
  12448. /* FIXME read out full plane state for all planes */
  12449. static void readout_plane_state(struct intel_crtc *crtc)
  12450. {
  12451. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12452. bool visible;
  12453. visible = crtc->active && primary_get_hw_state(primary);
  12454. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12455. to_intel_plane_state(primary->base.state),
  12456. visible);
  12457. }
  12458. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12459. {
  12460. struct drm_i915_private *dev_priv = to_i915(dev);
  12461. enum pipe pipe;
  12462. struct intel_crtc *crtc;
  12463. struct intel_encoder *encoder;
  12464. struct intel_connector *connector;
  12465. struct drm_connector_list_iter conn_iter;
  12466. int i;
  12467. dev_priv->active_crtcs = 0;
  12468. for_each_intel_crtc(dev, crtc) {
  12469. struct intel_crtc_state *crtc_state =
  12470. to_intel_crtc_state(crtc->base.state);
  12471. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12472. memset(crtc_state, 0, sizeof(*crtc_state));
  12473. crtc_state->base.crtc = &crtc->base;
  12474. crtc_state->base.active = crtc_state->base.enable =
  12475. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12476. crtc->base.enabled = crtc_state->base.enable;
  12477. crtc->active = crtc_state->base.active;
  12478. if (crtc_state->base.active)
  12479. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12480. readout_plane_state(crtc);
  12481. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12482. crtc->base.base.id, crtc->base.name,
  12483. enableddisabled(crtc_state->base.active));
  12484. }
  12485. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12486. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12487. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12488. &pll->state.hw_state);
  12489. pll->state.crtc_mask = 0;
  12490. for_each_intel_crtc(dev, crtc) {
  12491. struct intel_crtc_state *crtc_state =
  12492. to_intel_crtc_state(crtc->base.state);
  12493. if (crtc_state->base.active &&
  12494. crtc_state->shared_dpll == pll)
  12495. pll->state.crtc_mask |= 1 << crtc->pipe;
  12496. }
  12497. pll->active_mask = pll->state.crtc_mask;
  12498. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12499. pll->name, pll->state.crtc_mask, pll->on);
  12500. }
  12501. for_each_intel_encoder(dev, encoder) {
  12502. pipe = 0;
  12503. if (encoder->get_hw_state(encoder, &pipe)) {
  12504. struct intel_crtc_state *crtc_state;
  12505. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12506. crtc_state = to_intel_crtc_state(crtc->base.state);
  12507. encoder->base.crtc = &crtc->base;
  12508. crtc_state->output_types |= 1 << encoder->type;
  12509. encoder->get_config(encoder, crtc_state);
  12510. } else {
  12511. encoder->base.crtc = NULL;
  12512. }
  12513. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12514. encoder->base.base.id, encoder->base.name,
  12515. enableddisabled(encoder->base.crtc),
  12516. pipe_name(pipe));
  12517. }
  12518. drm_connector_list_iter_begin(dev, &conn_iter);
  12519. for_each_intel_connector_iter(connector, &conn_iter) {
  12520. if (connector->get_hw_state(connector)) {
  12521. connector->base.dpms = DRM_MODE_DPMS_ON;
  12522. encoder = connector->encoder;
  12523. connector->base.encoder = &encoder->base;
  12524. if (encoder->base.crtc &&
  12525. encoder->base.crtc->state->active) {
  12526. /*
  12527. * This has to be done during hardware readout
  12528. * because anything calling .crtc_disable may
  12529. * rely on the connector_mask being accurate.
  12530. */
  12531. encoder->base.crtc->state->connector_mask |=
  12532. 1 << drm_connector_index(&connector->base);
  12533. encoder->base.crtc->state->encoder_mask |=
  12534. 1 << drm_encoder_index(&encoder->base);
  12535. }
  12536. } else {
  12537. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12538. connector->base.encoder = NULL;
  12539. }
  12540. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12541. connector->base.base.id, connector->base.name,
  12542. enableddisabled(connector->base.encoder));
  12543. }
  12544. drm_connector_list_iter_end(&conn_iter);
  12545. for_each_intel_crtc(dev, crtc) {
  12546. struct intel_crtc_state *crtc_state =
  12547. to_intel_crtc_state(crtc->base.state);
  12548. int pixclk = 0;
  12549. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12550. if (crtc_state->base.active) {
  12551. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12552. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12553. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12554. /*
  12555. * The initial mode needs to be set in order to keep
  12556. * the atomic core happy. It wants a valid mode if the
  12557. * crtc's enabled, so we do the above call.
  12558. *
  12559. * But we don't set all the derived state fully, hence
  12560. * set a flag to indicate that a full recalculation is
  12561. * needed on the next commit.
  12562. */
  12563. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12564. intel_crtc_compute_pixel_rate(crtc_state);
  12565. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12566. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12567. pixclk = crtc_state->pixel_rate;
  12568. else
  12569. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12570. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12571. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12572. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12573. drm_calc_timestamping_constants(&crtc->base,
  12574. &crtc_state->base.adjusted_mode);
  12575. update_scanline_offset(crtc);
  12576. }
  12577. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12578. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12579. }
  12580. }
  12581. static void
  12582. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12583. {
  12584. struct intel_encoder *encoder;
  12585. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12586. u64 get_domains;
  12587. enum intel_display_power_domain domain;
  12588. if (!encoder->get_power_domains)
  12589. continue;
  12590. get_domains = encoder->get_power_domains(encoder);
  12591. for_each_power_domain(domain, get_domains)
  12592. intel_display_power_get(dev_priv, domain);
  12593. }
  12594. }
  12595. /* Scan out the current hw modeset state,
  12596. * and sanitizes it to the current state
  12597. */
  12598. static void
  12599. intel_modeset_setup_hw_state(struct drm_device *dev,
  12600. struct drm_modeset_acquire_ctx *ctx)
  12601. {
  12602. struct drm_i915_private *dev_priv = to_i915(dev);
  12603. enum pipe pipe;
  12604. struct intel_crtc *crtc;
  12605. struct intel_encoder *encoder;
  12606. int i;
  12607. intel_modeset_readout_hw_state(dev);
  12608. /* HW state is read out, now we need to sanitize this mess. */
  12609. get_encoder_power_domains(dev_priv);
  12610. for_each_intel_encoder(dev, encoder) {
  12611. intel_sanitize_encoder(encoder);
  12612. }
  12613. for_each_pipe(dev_priv, pipe) {
  12614. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12615. intel_sanitize_crtc(crtc, ctx);
  12616. intel_dump_pipe_config(crtc, crtc->config,
  12617. "[setup_hw_state]");
  12618. }
  12619. intel_modeset_update_connector_atomic_state(dev);
  12620. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12621. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12622. if (!pll->on || pll->active_mask)
  12623. continue;
  12624. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12625. pll->funcs.disable(dev_priv, pll);
  12626. pll->on = false;
  12627. }
  12628. if (IS_G4X(dev_priv)) {
  12629. g4x_wm_get_hw_state(dev);
  12630. g4x_wm_sanitize(dev_priv);
  12631. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12632. vlv_wm_get_hw_state(dev);
  12633. vlv_wm_sanitize(dev_priv);
  12634. } else if (INTEL_GEN(dev_priv) >= 9) {
  12635. skl_wm_get_hw_state(dev);
  12636. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12637. ilk_wm_get_hw_state(dev);
  12638. }
  12639. for_each_intel_crtc(dev, crtc) {
  12640. u64 put_domains;
  12641. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12642. if (WARN_ON(put_domains))
  12643. modeset_put_power_domains(dev_priv, put_domains);
  12644. }
  12645. intel_display_set_init_power(dev_priv, false);
  12646. intel_power_domains_verify_state(dev_priv);
  12647. intel_fbc_init_pipe_state(dev_priv);
  12648. }
  12649. void intel_display_resume(struct drm_device *dev)
  12650. {
  12651. struct drm_i915_private *dev_priv = to_i915(dev);
  12652. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12653. struct drm_modeset_acquire_ctx ctx;
  12654. int ret;
  12655. dev_priv->modeset_restore_state = NULL;
  12656. if (state)
  12657. state->acquire_ctx = &ctx;
  12658. drm_modeset_acquire_init(&ctx, 0);
  12659. while (1) {
  12660. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12661. if (ret != -EDEADLK)
  12662. break;
  12663. drm_modeset_backoff(&ctx);
  12664. }
  12665. if (!ret)
  12666. ret = __intel_display_resume(dev, state, &ctx);
  12667. drm_modeset_drop_locks(&ctx);
  12668. drm_modeset_acquire_fini(&ctx);
  12669. if (ret)
  12670. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12671. if (state)
  12672. drm_atomic_state_put(state);
  12673. }
  12674. void intel_modeset_gem_init(struct drm_device *dev)
  12675. {
  12676. struct drm_i915_private *dev_priv = to_i915(dev);
  12677. intel_init_gt_powersave(dev_priv);
  12678. intel_setup_overlay(dev_priv);
  12679. }
  12680. int intel_connector_register(struct drm_connector *connector)
  12681. {
  12682. struct intel_connector *intel_connector = to_intel_connector(connector);
  12683. int ret;
  12684. ret = intel_backlight_device_register(intel_connector);
  12685. if (ret)
  12686. goto err;
  12687. return 0;
  12688. err:
  12689. return ret;
  12690. }
  12691. void intel_connector_unregister(struct drm_connector *connector)
  12692. {
  12693. struct intel_connector *intel_connector = to_intel_connector(connector);
  12694. intel_backlight_device_unregister(intel_connector);
  12695. intel_panel_destroy_backlight(connector);
  12696. }
  12697. void intel_modeset_cleanup(struct drm_device *dev)
  12698. {
  12699. struct drm_i915_private *dev_priv = to_i915(dev);
  12700. flush_work(&dev_priv->atomic_helper.free_work);
  12701. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12702. intel_disable_gt_powersave(dev_priv);
  12703. /*
  12704. * Interrupts and polling as the first thing to avoid creating havoc.
  12705. * Too much stuff here (turning of connectors, ...) would
  12706. * experience fancy races otherwise.
  12707. */
  12708. intel_irq_uninstall(dev_priv);
  12709. /*
  12710. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12711. * poll handlers. Hence disable polling after hpd handling is shut down.
  12712. */
  12713. drm_kms_helper_poll_fini(dev);
  12714. /* poll work can call into fbdev, hence clean that up afterwards */
  12715. intel_fbdev_fini(dev_priv);
  12716. intel_unregister_dsm_handler();
  12717. intel_fbc_global_disable(dev_priv);
  12718. /* flush any delayed tasks or pending work */
  12719. flush_scheduled_work();
  12720. drm_mode_config_cleanup(dev);
  12721. intel_cleanup_overlay(dev_priv);
  12722. intel_cleanup_gt_powersave(dev_priv);
  12723. intel_teardown_gmbus(dev_priv);
  12724. }
  12725. void intel_connector_attach_encoder(struct intel_connector *connector,
  12726. struct intel_encoder *encoder)
  12727. {
  12728. connector->encoder = encoder;
  12729. drm_mode_connector_attach_encoder(&connector->base,
  12730. &encoder->base);
  12731. }
  12732. /*
  12733. * set vga decode state - true == enable VGA decode
  12734. */
  12735. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12736. {
  12737. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12738. u16 gmch_ctrl;
  12739. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12740. DRM_ERROR("failed to read control word\n");
  12741. return -EIO;
  12742. }
  12743. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12744. return 0;
  12745. if (state)
  12746. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12747. else
  12748. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12749. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12750. DRM_ERROR("failed to write control word\n");
  12751. return -EIO;
  12752. }
  12753. return 0;
  12754. }
  12755. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12756. struct intel_display_error_state {
  12757. u32 power_well_driver;
  12758. int num_transcoders;
  12759. struct intel_cursor_error_state {
  12760. u32 control;
  12761. u32 position;
  12762. u32 base;
  12763. u32 size;
  12764. } cursor[I915_MAX_PIPES];
  12765. struct intel_pipe_error_state {
  12766. bool power_domain_on;
  12767. u32 source;
  12768. u32 stat;
  12769. } pipe[I915_MAX_PIPES];
  12770. struct intel_plane_error_state {
  12771. u32 control;
  12772. u32 stride;
  12773. u32 size;
  12774. u32 pos;
  12775. u32 addr;
  12776. u32 surface;
  12777. u32 tile_offset;
  12778. } plane[I915_MAX_PIPES];
  12779. struct intel_transcoder_error_state {
  12780. bool power_domain_on;
  12781. enum transcoder cpu_transcoder;
  12782. u32 conf;
  12783. u32 htotal;
  12784. u32 hblank;
  12785. u32 hsync;
  12786. u32 vtotal;
  12787. u32 vblank;
  12788. u32 vsync;
  12789. } transcoder[4];
  12790. };
  12791. struct intel_display_error_state *
  12792. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12793. {
  12794. struct intel_display_error_state *error;
  12795. int transcoders[] = {
  12796. TRANSCODER_A,
  12797. TRANSCODER_B,
  12798. TRANSCODER_C,
  12799. TRANSCODER_EDP,
  12800. };
  12801. int i;
  12802. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12803. return NULL;
  12804. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12805. if (error == NULL)
  12806. return NULL;
  12807. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12808. error->power_well_driver =
  12809. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12810. for_each_pipe(dev_priv, i) {
  12811. error->pipe[i].power_domain_on =
  12812. __intel_display_power_is_enabled(dev_priv,
  12813. POWER_DOMAIN_PIPE(i));
  12814. if (!error->pipe[i].power_domain_on)
  12815. continue;
  12816. error->cursor[i].control = I915_READ(CURCNTR(i));
  12817. error->cursor[i].position = I915_READ(CURPOS(i));
  12818. error->cursor[i].base = I915_READ(CURBASE(i));
  12819. error->plane[i].control = I915_READ(DSPCNTR(i));
  12820. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12821. if (INTEL_GEN(dev_priv) <= 3) {
  12822. error->plane[i].size = I915_READ(DSPSIZE(i));
  12823. error->plane[i].pos = I915_READ(DSPPOS(i));
  12824. }
  12825. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12826. error->plane[i].addr = I915_READ(DSPADDR(i));
  12827. if (INTEL_GEN(dev_priv) >= 4) {
  12828. error->plane[i].surface = I915_READ(DSPSURF(i));
  12829. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12830. }
  12831. error->pipe[i].source = I915_READ(PIPESRC(i));
  12832. if (HAS_GMCH_DISPLAY(dev_priv))
  12833. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12834. }
  12835. /* Note: this does not include DSI transcoders. */
  12836. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12837. if (HAS_DDI(dev_priv))
  12838. error->num_transcoders++; /* Account for eDP. */
  12839. for (i = 0; i < error->num_transcoders; i++) {
  12840. enum transcoder cpu_transcoder = transcoders[i];
  12841. error->transcoder[i].power_domain_on =
  12842. __intel_display_power_is_enabled(dev_priv,
  12843. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12844. if (!error->transcoder[i].power_domain_on)
  12845. continue;
  12846. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12847. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12848. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12849. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12850. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12851. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12852. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12853. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12854. }
  12855. return error;
  12856. }
  12857. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12858. void
  12859. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12860. struct intel_display_error_state *error)
  12861. {
  12862. struct drm_i915_private *dev_priv = m->i915;
  12863. int i;
  12864. if (!error)
  12865. return;
  12866. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12867. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12868. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12869. error->power_well_driver);
  12870. for_each_pipe(dev_priv, i) {
  12871. err_printf(m, "Pipe [%d]:\n", i);
  12872. err_printf(m, " Power: %s\n",
  12873. onoff(error->pipe[i].power_domain_on));
  12874. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12875. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12876. err_printf(m, "Plane [%d]:\n", i);
  12877. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12878. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12879. if (INTEL_GEN(dev_priv) <= 3) {
  12880. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12881. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12882. }
  12883. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12884. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12885. if (INTEL_GEN(dev_priv) >= 4) {
  12886. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12887. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12888. }
  12889. err_printf(m, "Cursor [%d]:\n", i);
  12890. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12891. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12892. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12893. }
  12894. for (i = 0; i < error->num_transcoders; i++) {
  12895. err_printf(m, "CPU transcoder: %s\n",
  12896. transcoder_name(error->transcoder[i].cpu_transcoder));
  12897. err_printf(m, " Power: %s\n",
  12898. onoff(error->transcoder[i].power_domain_on));
  12899. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12900. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12901. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12902. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12903. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12904. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12905. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12906. }
  12907. }
  12908. #endif