intel_display.c 445 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint64_t skl_format_modifiers_noccs[] = {
  86. I915_FORMAT_MOD_Yf_TILED,
  87. I915_FORMAT_MOD_Y_TILED,
  88. I915_FORMAT_MOD_X_TILED,
  89. DRM_FORMAT_MOD_LINEAR,
  90. DRM_FORMAT_MOD_INVALID
  91. };
  92. static const uint64_t skl_format_modifiers_ccs[] = {
  93. I915_FORMAT_MOD_Yf_TILED_CCS,
  94. I915_FORMAT_MOD_Y_TILED_CCS,
  95. I915_FORMAT_MOD_Yf_TILED,
  96. I915_FORMAT_MOD_Y_TILED,
  97. I915_FORMAT_MOD_X_TILED,
  98. DRM_FORMAT_MOD_LINEAR,
  99. DRM_FORMAT_MOD_INVALID
  100. };
  101. /* Cursor formats */
  102. static const uint32_t intel_cursor_formats[] = {
  103. DRM_FORMAT_ARGB8888,
  104. };
  105. static const uint64_t cursor_format_modifiers[] = {
  106. DRM_FORMAT_MOD_LINEAR,
  107. DRM_FORMAT_MOD_INVALID
  108. };
  109. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  110. struct intel_crtc_state *pipe_config);
  111. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  112. struct intel_crtc_state *pipe_config);
  113. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  114. struct drm_i915_gem_object *obj,
  115. struct drm_mode_fb_cmd2 *mode_cmd);
  116. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  117. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  118. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  119. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  120. struct intel_link_m_n *m_n,
  121. struct intel_link_m_n *m2_n2);
  122. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  123. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  124. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  125. static void vlv_prepare_pll(struct intel_crtc *crtc,
  126. const struct intel_crtc_state *pipe_config);
  127. static void chv_prepare_pll(struct intel_crtc *crtc,
  128. const struct intel_crtc_state *pipe_config);
  129. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  130. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  131. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  132. struct intel_crtc_state *crtc_state);
  133. static void skylake_pfit_enable(struct intel_crtc *crtc);
  134. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  135. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  136. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  137. struct drm_modeset_acquire_ctx *ctx);
  138. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  139. struct intel_limit {
  140. struct {
  141. int min, max;
  142. } dot, vco, n, m, m1, m2, p, p1;
  143. struct {
  144. int dot_limit;
  145. int p2_slow, p2_fast;
  146. } p2;
  147. };
  148. /* returns HPLL frequency in kHz */
  149. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  150. {
  151. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  152. /* Obtain SKU information */
  153. mutex_lock(&dev_priv->sb_lock);
  154. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  155. CCK_FUSE_HPLL_FREQ_MASK;
  156. mutex_unlock(&dev_priv->sb_lock);
  157. return vco_freq[hpll_freq] * 1000;
  158. }
  159. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  160. const char *name, u32 reg, int ref_freq)
  161. {
  162. u32 val;
  163. int divider;
  164. mutex_lock(&dev_priv->sb_lock);
  165. val = vlv_cck_read(dev_priv, reg);
  166. mutex_unlock(&dev_priv->sb_lock);
  167. divider = val & CCK_FREQUENCY_VALUES;
  168. WARN((val & CCK_FREQUENCY_STATUS) !=
  169. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  170. "%s change in progress\n", name);
  171. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  172. }
  173. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  174. const char *name, u32 reg)
  175. {
  176. if (dev_priv->hpll_freq == 0)
  177. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  178. return vlv_get_cck_clock(dev_priv, name, reg,
  179. dev_priv->hpll_freq);
  180. }
  181. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  182. {
  183. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  184. return;
  185. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  186. CCK_CZ_CLOCK_CONTROL);
  187. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  188. }
  189. static inline u32 /* units of 100MHz */
  190. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  191. const struct intel_crtc_state *pipe_config)
  192. {
  193. if (HAS_DDI(dev_priv))
  194. return pipe_config->port_clock; /* SPLL */
  195. else
  196. return dev_priv->fdi_pll_freq;
  197. }
  198. static const struct intel_limit intel_limits_i8xx_dac = {
  199. .dot = { .min = 25000, .max = 350000 },
  200. .vco = { .min = 908000, .max = 1512000 },
  201. .n = { .min = 2, .max = 16 },
  202. .m = { .min = 96, .max = 140 },
  203. .m1 = { .min = 18, .max = 26 },
  204. .m2 = { .min = 6, .max = 16 },
  205. .p = { .min = 4, .max = 128 },
  206. .p1 = { .min = 2, .max = 33 },
  207. .p2 = { .dot_limit = 165000,
  208. .p2_slow = 4, .p2_fast = 2 },
  209. };
  210. static const struct intel_limit intel_limits_i8xx_dvo = {
  211. .dot = { .min = 25000, .max = 350000 },
  212. .vco = { .min = 908000, .max = 1512000 },
  213. .n = { .min = 2, .max = 16 },
  214. .m = { .min = 96, .max = 140 },
  215. .m1 = { .min = 18, .max = 26 },
  216. .m2 = { .min = 6, .max = 16 },
  217. .p = { .min = 4, .max = 128 },
  218. .p1 = { .min = 2, .max = 33 },
  219. .p2 = { .dot_limit = 165000,
  220. .p2_slow = 4, .p2_fast = 4 },
  221. };
  222. static const struct intel_limit intel_limits_i8xx_lvds = {
  223. .dot = { .min = 25000, .max = 350000 },
  224. .vco = { .min = 908000, .max = 1512000 },
  225. .n = { .min = 2, .max = 16 },
  226. .m = { .min = 96, .max = 140 },
  227. .m1 = { .min = 18, .max = 26 },
  228. .m2 = { .min = 6, .max = 16 },
  229. .p = { .min = 4, .max = 128 },
  230. .p1 = { .min = 1, .max = 6 },
  231. .p2 = { .dot_limit = 165000,
  232. .p2_slow = 14, .p2_fast = 7 },
  233. };
  234. static const struct intel_limit intel_limits_i9xx_sdvo = {
  235. .dot = { .min = 20000, .max = 400000 },
  236. .vco = { .min = 1400000, .max = 2800000 },
  237. .n = { .min = 1, .max = 6 },
  238. .m = { .min = 70, .max = 120 },
  239. .m1 = { .min = 8, .max = 18 },
  240. .m2 = { .min = 3, .max = 7 },
  241. .p = { .min = 5, .max = 80 },
  242. .p1 = { .min = 1, .max = 8 },
  243. .p2 = { .dot_limit = 200000,
  244. .p2_slow = 10, .p2_fast = 5 },
  245. };
  246. static const struct intel_limit intel_limits_i9xx_lvds = {
  247. .dot = { .min = 20000, .max = 400000 },
  248. .vco = { .min = 1400000, .max = 2800000 },
  249. .n = { .min = 1, .max = 6 },
  250. .m = { .min = 70, .max = 120 },
  251. .m1 = { .min = 8, .max = 18 },
  252. .m2 = { .min = 3, .max = 7 },
  253. .p = { .min = 7, .max = 98 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 112000,
  256. .p2_slow = 14, .p2_fast = 7 },
  257. };
  258. static const struct intel_limit intel_limits_g4x_sdvo = {
  259. .dot = { .min = 25000, .max = 270000 },
  260. .vco = { .min = 1750000, .max = 3500000},
  261. .n = { .min = 1, .max = 4 },
  262. .m = { .min = 104, .max = 138 },
  263. .m1 = { .min = 17, .max = 23 },
  264. .m2 = { .min = 5, .max = 11 },
  265. .p = { .min = 10, .max = 30 },
  266. .p1 = { .min = 1, .max = 3},
  267. .p2 = { .dot_limit = 270000,
  268. .p2_slow = 10,
  269. .p2_fast = 10
  270. },
  271. };
  272. static const struct intel_limit intel_limits_g4x_hdmi = {
  273. .dot = { .min = 22000, .max = 400000 },
  274. .vco = { .min = 1750000, .max = 3500000},
  275. .n = { .min = 1, .max = 4 },
  276. .m = { .min = 104, .max = 138 },
  277. .m1 = { .min = 16, .max = 23 },
  278. .m2 = { .min = 5, .max = 11 },
  279. .p = { .min = 5, .max = 80 },
  280. .p1 = { .min = 1, .max = 8},
  281. .p2 = { .dot_limit = 165000,
  282. .p2_slow = 10, .p2_fast = 5 },
  283. };
  284. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  285. .dot = { .min = 20000, .max = 115000 },
  286. .vco = { .min = 1750000, .max = 3500000 },
  287. .n = { .min = 1, .max = 3 },
  288. .m = { .min = 104, .max = 138 },
  289. .m1 = { .min = 17, .max = 23 },
  290. .m2 = { .min = 5, .max = 11 },
  291. .p = { .min = 28, .max = 112 },
  292. .p1 = { .min = 2, .max = 8 },
  293. .p2 = { .dot_limit = 0,
  294. .p2_slow = 14, .p2_fast = 14
  295. },
  296. };
  297. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  298. .dot = { .min = 80000, .max = 224000 },
  299. .vco = { .min = 1750000, .max = 3500000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 104, .max = 138 },
  302. .m1 = { .min = 17, .max = 23 },
  303. .m2 = { .min = 5, .max = 11 },
  304. .p = { .min = 14, .max = 42 },
  305. .p1 = { .min = 2, .max = 6 },
  306. .p2 = { .dot_limit = 0,
  307. .p2_slow = 7, .p2_fast = 7
  308. },
  309. };
  310. static const struct intel_limit intel_limits_pineview_sdvo = {
  311. .dot = { .min = 20000, .max = 400000},
  312. .vco = { .min = 1700000, .max = 3500000 },
  313. /* Pineview's Ncounter is a ring counter */
  314. .n = { .min = 3, .max = 6 },
  315. .m = { .min = 2, .max = 256 },
  316. /* Pineview only has one combined m divider, which we treat as m2. */
  317. .m1 = { .min = 0, .max = 0 },
  318. .m2 = { .min = 0, .max = 254 },
  319. .p = { .min = 5, .max = 80 },
  320. .p1 = { .min = 1, .max = 8 },
  321. .p2 = { .dot_limit = 200000,
  322. .p2_slow = 10, .p2_fast = 5 },
  323. };
  324. static const struct intel_limit intel_limits_pineview_lvds = {
  325. .dot = { .min = 20000, .max = 400000 },
  326. .vco = { .min = 1700000, .max = 3500000 },
  327. .n = { .min = 3, .max = 6 },
  328. .m = { .min = 2, .max = 256 },
  329. .m1 = { .min = 0, .max = 0 },
  330. .m2 = { .min = 0, .max = 254 },
  331. .p = { .min = 7, .max = 112 },
  332. .p1 = { .min = 1, .max = 8 },
  333. .p2 = { .dot_limit = 112000,
  334. .p2_slow = 14, .p2_fast = 14 },
  335. };
  336. /* Ironlake / Sandybridge
  337. *
  338. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  339. * the range value for them is (actual_value - 2).
  340. */
  341. static const struct intel_limit intel_limits_ironlake_dac = {
  342. .dot = { .min = 25000, .max = 350000 },
  343. .vco = { .min = 1760000, .max = 3510000 },
  344. .n = { .min = 1, .max = 5 },
  345. .m = { .min = 79, .max = 127 },
  346. .m1 = { .min = 12, .max = 22 },
  347. .m2 = { .min = 5, .max = 9 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 225000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  354. .dot = { .min = 25000, .max = 350000 },
  355. .vco = { .min = 1760000, .max = 3510000 },
  356. .n = { .min = 1, .max = 3 },
  357. .m = { .min = 79, .max = 118 },
  358. .m1 = { .min = 12, .max = 22 },
  359. .m2 = { .min = 5, .max = 9 },
  360. .p = { .min = 28, .max = 112 },
  361. .p1 = { .min = 2, .max = 8 },
  362. .p2 = { .dot_limit = 225000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  366. .dot = { .min = 25000, .max = 350000 },
  367. .vco = { .min = 1760000, .max = 3510000 },
  368. .n = { .min = 1, .max = 3 },
  369. .m = { .min = 79, .max = 127 },
  370. .m1 = { .min = 12, .max = 22 },
  371. .m2 = { .min = 5, .max = 9 },
  372. .p = { .min = 14, .max = 56 },
  373. .p1 = { .min = 2, .max = 8 },
  374. .p2 = { .dot_limit = 225000,
  375. .p2_slow = 7, .p2_fast = 7 },
  376. };
  377. /* LVDS 100mhz refclk limits. */
  378. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  379. .dot = { .min = 25000, .max = 350000 },
  380. .vco = { .min = 1760000, .max = 3510000 },
  381. .n = { .min = 1, .max = 2 },
  382. .m = { .min = 79, .max = 126 },
  383. .m1 = { .min = 12, .max = 22 },
  384. .m2 = { .min = 5, .max = 9 },
  385. .p = { .min = 28, .max = 112 },
  386. .p1 = { .min = 2, .max = 8 },
  387. .p2 = { .dot_limit = 225000,
  388. .p2_slow = 14, .p2_fast = 14 },
  389. };
  390. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  391. .dot = { .min = 25000, .max = 350000 },
  392. .vco = { .min = 1760000, .max = 3510000 },
  393. .n = { .min = 1, .max = 3 },
  394. .m = { .min = 79, .max = 126 },
  395. .m1 = { .min = 12, .max = 22 },
  396. .m2 = { .min = 5, .max = 9 },
  397. .p = { .min = 14, .max = 42 },
  398. .p1 = { .min = 2, .max = 6 },
  399. .p2 = { .dot_limit = 225000,
  400. .p2_slow = 7, .p2_fast = 7 },
  401. };
  402. static const struct intel_limit intel_limits_vlv = {
  403. /*
  404. * These are the data rate limits (measured in fast clocks)
  405. * since those are the strictest limits we have. The fast
  406. * clock and actual rate limits are more relaxed, so checking
  407. * them would make no difference.
  408. */
  409. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  410. .vco = { .min = 4000000, .max = 6000000 },
  411. .n = { .min = 1, .max = 7 },
  412. .m1 = { .min = 2, .max = 3 },
  413. .m2 = { .min = 11, .max = 156 },
  414. .p1 = { .min = 2, .max = 3 },
  415. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  416. };
  417. static const struct intel_limit intel_limits_chv = {
  418. /*
  419. * These are the data rate limits (measured in fast clocks)
  420. * since those are the strictest limits we have. The fast
  421. * clock and actual rate limits are more relaxed, so checking
  422. * them would make no difference.
  423. */
  424. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  425. .vco = { .min = 4800000, .max = 6480000 },
  426. .n = { .min = 1, .max = 1 },
  427. .m1 = { .min = 2, .max = 2 },
  428. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  429. .p1 = { .min = 2, .max = 4 },
  430. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  431. };
  432. static const struct intel_limit intel_limits_bxt = {
  433. /* FIXME: find real dot limits */
  434. .dot = { .min = 0, .max = INT_MAX },
  435. .vco = { .min = 4800000, .max = 6700000 },
  436. .n = { .min = 1, .max = 1 },
  437. .m1 = { .min = 2, .max = 2 },
  438. /* FIXME: find real m2 limits */
  439. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  440. .p1 = { .min = 2, .max = 4 },
  441. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  442. };
  443. static void
  444. skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
  445. {
  446. if (IS_SKYLAKE(dev_priv))
  447. return;
  448. if (enable)
  449. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  450. DUPS1_GATING_DIS | DUPS2_GATING_DIS);
  451. else
  452. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  453. I915_READ(CLKGATE_DIS_PSL(pipe)) &
  454. ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
  455. }
  456. static bool
  457. needs_modeset(const struct drm_crtc_state *state)
  458. {
  459. return drm_atomic_crtc_needs_modeset(state);
  460. }
  461. /*
  462. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  463. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  464. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  465. * The helpers' return value is the rate of the clock that is fed to the
  466. * display engine's pipe which can be the above fast dot clock rate or a
  467. * divided-down version of it.
  468. */
  469. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  470. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  471. {
  472. clock->m = clock->m2 + 2;
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n == 0 || clock->p == 0))
  475. return 0;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. return clock->dot;
  479. }
  480. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  481. {
  482. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  483. }
  484. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  485. {
  486. clock->m = i9xx_dpll_compute_m(clock);
  487. clock->p = clock->p1 * clock->p2;
  488. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  489. return 0;
  490. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  491. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  492. return clock->dot;
  493. }
  494. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  495. {
  496. clock->m = clock->m1 * clock->m2;
  497. clock->p = clock->p1 * clock->p2;
  498. if (WARN_ON(clock->n == 0 || clock->p == 0))
  499. return 0;
  500. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  501. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  502. return clock->dot / 5;
  503. }
  504. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  505. {
  506. clock->m = clock->m1 * clock->m2;
  507. clock->p = clock->p1 * clock->p2;
  508. if (WARN_ON(clock->n == 0 || clock->p == 0))
  509. return 0;
  510. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  511. clock->n << 22);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot / 5;
  514. }
  515. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  516. /*
  517. * Returns whether the given set of divisors are valid for a given refclk with
  518. * the given connectors.
  519. */
  520. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  521. const struct intel_limit *limit,
  522. const struct dpll *clock)
  523. {
  524. if (clock->n < limit->n.min || limit->n.max < clock->n)
  525. INTELPllInvalid("n out of range\n");
  526. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  527. INTELPllInvalid("p1 out of range\n");
  528. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  529. INTELPllInvalid("m2 out of range\n");
  530. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  531. INTELPllInvalid("m1 out of range\n");
  532. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  533. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  534. if (clock->m1 <= clock->m2)
  535. INTELPllInvalid("m1 <= m2\n");
  536. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  537. !IS_GEN9_LP(dev_priv)) {
  538. if (clock->p < limit->p.min || limit->p.max < clock->p)
  539. INTELPllInvalid("p out of range\n");
  540. if (clock->m < limit->m.min || limit->m.max < clock->m)
  541. INTELPllInvalid("m out of range\n");
  542. }
  543. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  544. INTELPllInvalid("vco out of range\n");
  545. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  546. * connector, etc., rather than just a single range.
  547. */
  548. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  549. INTELPllInvalid("dot out of range\n");
  550. return true;
  551. }
  552. static int
  553. i9xx_select_p2_div(const struct intel_limit *limit,
  554. const struct intel_crtc_state *crtc_state,
  555. int target)
  556. {
  557. struct drm_device *dev = crtc_state->base.crtc->dev;
  558. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  559. /*
  560. * For LVDS just rely on its current settings for dual-channel.
  561. * We haven't figured out how to reliably set up different
  562. * single/dual channel state, if we even can.
  563. */
  564. if (intel_is_dual_link_lvds(dev))
  565. return limit->p2.p2_fast;
  566. else
  567. return limit->p2.p2_slow;
  568. } else {
  569. if (target < limit->p2.dot_limit)
  570. return limit->p2.p2_slow;
  571. else
  572. return limit->p2.p2_fast;
  573. }
  574. }
  575. /*
  576. * Returns a set of divisors for the desired target clock with the given
  577. * refclk, or FALSE. The returned values represent the clock equation:
  578. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  579. *
  580. * Target and reference clocks are specified in kHz.
  581. *
  582. * If match_clock is provided, then best_clock P divider must match the P
  583. * divider from @match_clock used for LVDS downclocking.
  584. */
  585. static bool
  586. i9xx_find_best_dpll(const struct intel_limit *limit,
  587. struct intel_crtc_state *crtc_state,
  588. int target, int refclk, struct dpll *match_clock,
  589. struct dpll *best_clock)
  590. {
  591. struct drm_device *dev = crtc_state->base.crtc->dev;
  592. struct dpll clock;
  593. int err = target;
  594. memset(best_clock, 0, sizeof(*best_clock));
  595. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  596. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  597. clock.m1++) {
  598. for (clock.m2 = limit->m2.min;
  599. clock.m2 <= limit->m2.max; clock.m2++) {
  600. if (clock.m2 >= clock.m1)
  601. break;
  602. for (clock.n = limit->n.min;
  603. clock.n <= limit->n.max; clock.n++) {
  604. for (clock.p1 = limit->p1.min;
  605. clock.p1 <= limit->p1.max; clock.p1++) {
  606. int this_err;
  607. i9xx_calc_dpll_params(refclk, &clock);
  608. if (!intel_PLL_is_valid(to_i915(dev),
  609. limit,
  610. &clock))
  611. continue;
  612. if (match_clock &&
  613. clock.p != match_clock->p)
  614. continue;
  615. this_err = abs(clock.dot - target);
  616. if (this_err < err) {
  617. *best_clock = clock;
  618. err = this_err;
  619. }
  620. }
  621. }
  622. }
  623. }
  624. return (err != target);
  625. }
  626. /*
  627. * Returns a set of divisors for the desired target clock with the given
  628. * refclk, or FALSE. The returned values represent the clock equation:
  629. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  630. *
  631. * Target and reference clocks are specified in kHz.
  632. *
  633. * If match_clock is provided, then best_clock P divider must match the P
  634. * divider from @match_clock used for LVDS downclocking.
  635. */
  636. static bool
  637. pnv_find_best_dpll(const struct intel_limit *limit,
  638. struct intel_crtc_state *crtc_state,
  639. int target, int refclk, struct dpll *match_clock,
  640. struct dpll *best_clock)
  641. {
  642. struct drm_device *dev = crtc_state->base.crtc->dev;
  643. struct dpll clock;
  644. int err = target;
  645. memset(best_clock, 0, sizeof(*best_clock));
  646. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  647. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  648. clock.m1++) {
  649. for (clock.m2 = limit->m2.min;
  650. clock.m2 <= limit->m2.max; clock.m2++) {
  651. for (clock.n = limit->n.min;
  652. clock.n <= limit->n.max; clock.n++) {
  653. for (clock.p1 = limit->p1.min;
  654. clock.p1 <= limit->p1.max; clock.p1++) {
  655. int this_err;
  656. pnv_calc_dpll_params(refclk, &clock);
  657. if (!intel_PLL_is_valid(to_i915(dev),
  658. limit,
  659. &clock))
  660. continue;
  661. if (match_clock &&
  662. clock.p != match_clock->p)
  663. continue;
  664. this_err = abs(clock.dot - target);
  665. if (this_err < err) {
  666. *best_clock = clock;
  667. err = this_err;
  668. }
  669. }
  670. }
  671. }
  672. }
  673. return (err != target);
  674. }
  675. /*
  676. * Returns a set of divisors for the desired target clock with the given
  677. * refclk, or FALSE. The returned values represent the clock equation:
  678. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  679. *
  680. * Target and reference clocks are specified in kHz.
  681. *
  682. * If match_clock is provided, then best_clock P divider must match the P
  683. * divider from @match_clock used for LVDS downclocking.
  684. */
  685. static bool
  686. g4x_find_best_dpll(const struct intel_limit *limit,
  687. struct intel_crtc_state *crtc_state,
  688. int target, int refclk, struct dpll *match_clock,
  689. struct dpll *best_clock)
  690. {
  691. struct drm_device *dev = crtc_state->base.crtc->dev;
  692. struct dpll clock;
  693. int max_n;
  694. bool found = false;
  695. /* approximately equals target * 0.00585 */
  696. int err_most = (target >> 8) + (target >> 9);
  697. memset(best_clock, 0, sizeof(*best_clock));
  698. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  699. max_n = limit->n.max;
  700. /* based on hardware requirement, prefer smaller n to precision */
  701. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  702. /* based on hardware requirement, prefere larger m1,m2 */
  703. for (clock.m1 = limit->m1.max;
  704. clock.m1 >= limit->m1.min; clock.m1--) {
  705. for (clock.m2 = limit->m2.max;
  706. clock.m2 >= limit->m2.min; clock.m2--) {
  707. for (clock.p1 = limit->p1.max;
  708. clock.p1 >= limit->p1.min; clock.p1--) {
  709. int this_err;
  710. i9xx_calc_dpll_params(refclk, &clock);
  711. if (!intel_PLL_is_valid(to_i915(dev),
  712. limit,
  713. &clock))
  714. continue;
  715. this_err = abs(clock.dot - target);
  716. if (this_err < err_most) {
  717. *best_clock = clock;
  718. err_most = this_err;
  719. max_n = clock.n;
  720. found = true;
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return found;
  727. }
  728. /*
  729. * Check if the calculated PLL configuration is more optimal compared to the
  730. * best configuration and error found so far. Return the calculated error.
  731. */
  732. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  733. const struct dpll *calculated_clock,
  734. const struct dpll *best_clock,
  735. unsigned int best_error_ppm,
  736. unsigned int *error_ppm)
  737. {
  738. /*
  739. * For CHV ignore the error and consider only the P value.
  740. * Prefer a bigger P value based on HW requirements.
  741. */
  742. if (IS_CHERRYVIEW(to_i915(dev))) {
  743. *error_ppm = 0;
  744. return calculated_clock->p > best_clock->p;
  745. }
  746. if (WARN_ON_ONCE(!target_freq))
  747. return false;
  748. *error_ppm = div_u64(1000000ULL *
  749. abs(target_freq - calculated_clock->dot),
  750. target_freq);
  751. /*
  752. * Prefer a better P value over a better (smaller) error if the error
  753. * is small. Ensure this preference for future configurations too by
  754. * setting the error to 0.
  755. */
  756. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  757. *error_ppm = 0;
  758. return true;
  759. }
  760. return *error_ppm + 10 < best_error_ppm;
  761. }
  762. /*
  763. * Returns a set of divisors for the desired target clock with the given
  764. * refclk, or FALSE. The returned values represent the clock equation:
  765. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  766. */
  767. static bool
  768. vlv_find_best_dpll(const struct intel_limit *limit,
  769. struct intel_crtc_state *crtc_state,
  770. int target, int refclk, struct dpll *match_clock,
  771. struct dpll *best_clock)
  772. {
  773. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  774. struct drm_device *dev = crtc->base.dev;
  775. struct dpll clock;
  776. unsigned int bestppm = 1000000;
  777. /* min update 19.2 MHz */
  778. int max_n = min(limit->n.max, refclk / 19200);
  779. bool found = false;
  780. target *= 5; /* fast clock */
  781. memset(best_clock, 0, sizeof(*best_clock));
  782. /* based on hardware requirement, prefer smaller n to precision */
  783. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  784. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  785. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  786. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  787. clock.p = clock.p1 * clock.p2;
  788. /* based on hardware requirement, prefer bigger m1,m2 values */
  789. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  790. unsigned int ppm;
  791. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  792. refclk * clock.m1);
  793. vlv_calc_dpll_params(refclk, &clock);
  794. if (!intel_PLL_is_valid(to_i915(dev),
  795. limit,
  796. &clock))
  797. continue;
  798. if (!vlv_PLL_is_optimal(dev, target,
  799. &clock,
  800. best_clock,
  801. bestppm, &ppm))
  802. continue;
  803. *best_clock = clock;
  804. bestppm = ppm;
  805. found = true;
  806. }
  807. }
  808. }
  809. }
  810. return found;
  811. }
  812. /*
  813. * Returns a set of divisors for the desired target clock with the given
  814. * refclk, or FALSE. The returned values represent the clock equation:
  815. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  816. */
  817. static bool
  818. chv_find_best_dpll(const struct intel_limit *limit,
  819. struct intel_crtc_state *crtc_state,
  820. int target, int refclk, struct dpll *match_clock,
  821. struct dpll *best_clock)
  822. {
  823. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  824. struct drm_device *dev = crtc->base.dev;
  825. unsigned int best_error_ppm;
  826. struct dpll clock;
  827. uint64_t m2;
  828. int found = false;
  829. memset(best_clock, 0, sizeof(*best_clock));
  830. best_error_ppm = 1000000;
  831. /*
  832. * Based on hardware doc, the n always set to 1, and m1 always
  833. * set to 2. If requires to support 200Mhz refclk, we need to
  834. * revisit this because n may not 1 anymore.
  835. */
  836. clock.n = 1, clock.m1 = 2;
  837. target *= 5; /* fast clock */
  838. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  839. for (clock.p2 = limit->p2.p2_fast;
  840. clock.p2 >= limit->p2.p2_slow;
  841. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  842. unsigned int error_ppm;
  843. clock.p = clock.p1 * clock.p2;
  844. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  845. clock.n) << 22, refclk * clock.m1);
  846. if (m2 > INT_MAX/clock.m1)
  847. continue;
  848. clock.m2 = m2;
  849. chv_calc_dpll_params(refclk, &clock);
  850. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  851. continue;
  852. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  853. best_error_ppm, &error_ppm))
  854. continue;
  855. *best_clock = clock;
  856. best_error_ppm = error_ppm;
  857. found = true;
  858. }
  859. }
  860. return found;
  861. }
  862. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  863. struct dpll *best_clock)
  864. {
  865. int refclk = 100000;
  866. const struct intel_limit *limit = &intel_limits_bxt;
  867. return chv_find_best_dpll(limit, crtc_state,
  868. target_clock, refclk, NULL, best_clock);
  869. }
  870. bool intel_crtc_active(struct intel_crtc *crtc)
  871. {
  872. /* Be paranoid as we can arrive here with only partial
  873. * state retrieved from the hardware during setup.
  874. *
  875. * We can ditch the adjusted_mode.crtc_clock check as soon
  876. * as Haswell has gained clock readout/fastboot support.
  877. *
  878. * We can ditch the crtc->primary->fb check as soon as we can
  879. * properly reconstruct framebuffers.
  880. *
  881. * FIXME: The intel_crtc->active here should be switched to
  882. * crtc->state->active once we have proper CRTC states wired up
  883. * for atomic.
  884. */
  885. return crtc->active && crtc->base.primary->state->fb &&
  886. crtc->config->base.adjusted_mode.crtc_clock;
  887. }
  888. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  889. enum pipe pipe)
  890. {
  891. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  892. return crtc->config->cpu_transcoder;
  893. }
  894. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  895. enum pipe pipe)
  896. {
  897. i915_reg_t reg = PIPEDSL(pipe);
  898. u32 line1, line2;
  899. u32 line_mask;
  900. if (IS_GEN2(dev_priv))
  901. line_mask = DSL_LINEMASK_GEN2;
  902. else
  903. line_mask = DSL_LINEMASK_GEN3;
  904. line1 = I915_READ(reg) & line_mask;
  905. msleep(5);
  906. line2 = I915_READ(reg) & line_mask;
  907. return line1 != line2;
  908. }
  909. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  910. {
  911. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  912. enum pipe pipe = crtc->pipe;
  913. /* Wait for the display line to settle/start moving */
  914. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  915. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  916. pipe_name(pipe), onoff(state));
  917. }
  918. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  919. {
  920. wait_for_pipe_scanline_moving(crtc, false);
  921. }
  922. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  923. {
  924. wait_for_pipe_scanline_moving(crtc, true);
  925. }
  926. static void
  927. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  928. {
  929. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  930. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  931. if (INTEL_GEN(dev_priv) >= 4) {
  932. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  933. i915_reg_t reg = PIPECONF(cpu_transcoder);
  934. /* Wait for the Pipe State to go off */
  935. if (intel_wait_for_register(dev_priv,
  936. reg, I965_PIPECONF_ACTIVE, 0,
  937. 100))
  938. WARN(1, "pipe_off wait timed out\n");
  939. } else {
  940. intel_wait_for_pipe_scanline_stopped(crtc);
  941. }
  942. }
  943. /* Only for pre-ILK configs */
  944. void assert_pll(struct drm_i915_private *dev_priv,
  945. enum pipe pipe, bool state)
  946. {
  947. u32 val;
  948. bool cur_state;
  949. val = I915_READ(DPLL(pipe));
  950. cur_state = !!(val & DPLL_VCO_ENABLE);
  951. I915_STATE_WARN(cur_state != state,
  952. "PLL state assertion failure (expected %s, current %s)\n",
  953. onoff(state), onoff(cur_state));
  954. }
  955. /* XXX: the dsi pll is shared between MIPI DSI ports */
  956. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  957. {
  958. u32 val;
  959. bool cur_state;
  960. mutex_lock(&dev_priv->sb_lock);
  961. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  962. mutex_unlock(&dev_priv->sb_lock);
  963. cur_state = val & DSI_PLL_VCO_EN;
  964. I915_STATE_WARN(cur_state != state,
  965. "DSI PLL state assertion failure (expected %s, current %s)\n",
  966. onoff(state), onoff(cur_state));
  967. }
  968. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  969. enum pipe pipe, bool state)
  970. {
  971. bool cur_state;
  972. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  973. pipe);
  974. if (HAS_DDI(dev_priv)) {
  975. /* DDI does not have a specific FDI_TX register */
  976. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  977. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  978. } else {
  979. u32 val = I915_READ(FDI_TX_CTL(pipe));
  980. cur_state = !!(val & FDI_TX_ENABLE);
  981. }
  982. I915_STATE_WARN(cur_state != state,
  983. "FDI TX state assertion failure (expected %s, current %s)\n",
  984. onoff(state), onoff(cur_state));
  985. }
  986. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  987. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  988. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  989. enum pipe pipe, bool state)
  990. {
  991. u32 val;
  992. bool cur_state;
  993. val = I915_READ(FDI_RX_CTL(pipe));
  994. cur_state = !!(val & FDI_RX_ENABLE);
  995. I915_STATE_WARN(cur_state != state,
  996. "FDI RX state assertion failure (expected %s, current %s)\n",
  997. onoff(state), onoff(cur_state));
  998. }
  999. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1000. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1001. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1002. enum pipe pipe)
  1003. {
  1004. u32 val;
  1005. /* ILK FDI PLL is always enabled */
  1006. if (IS_GEN5(dev_priv))
  1007. return;
  1008. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1009. if (HAS_DDI(dev_priv))
  1010. return;
  1011. val = I915_READ(FDI_TX_CTL(pipe));
  1012. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1013. }
  1014. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1015. enum pipe pipe, bool state)
  1016. {
  1017. u32 val;
  1018. bool cur_state;
  1019. val = I915_READ(FDI_RX_CTL(pipe));
  1020. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1021. I915_STATE_WARN(cur_state != state,
  1022. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1023. onoff(state), onoff(cur_state));
  1024. }
  1025. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1026. {
  1027. i915_reg_t pp_reg;
  1028. u32 val;
  1029. enum pipe panel_pipe = PIPE_A;
  1030. bool locked = true;
  1031. if (WARN_ON(HAS_DDI(dev_priv)))
  1032. return;
  1033. if (HAS_PCH_SPLIT(dev_priv)) {
  1034. u32 port_sel;
  1035. pp_reg = PP_CONTROL(0);
  1036. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1037. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1038. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1039. panel_pipe = PIPE_B;
  1040. /* XXX: else fix for eDP */
  1041. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1042. /* presumably write lock depends on pipe, not port select */
  1043. pp_reg = PP_CONTROL(pipe);
  1044. panel_pipe = pipe;
  1045. } else {
  1046. pp_reg = PP_CONTROL(0);
  1047. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1048. panel_pipe = PIPE_B;
  1049. }
  1050. val = I915_READ(pp_reg);
  1051. if (!(val & PANEL_POWER_ON) ||
  1052. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1053. locked = false;
  1054. I915_STATE_WARN(panel_pipe == pipe && locked,
  1055. "panel assertion failure, pipe %c regs locked\n",
  1056. pipe_name(pipe));
  1057. }
  1058. void assert_pipe(struct drm_i915_private *dev_priv,
  1059. enum pipe pipe, bool state)
  1060. {
  1061. bool cur_state;
  1062. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1063. pipe);
  1064. enum intel_display_power_domain power_domain;
  1065. /* we keep both pipes enabled on 830 */
  1066. if (IS_I830(dev_priv))
  1067. state = true;
  1068. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1069. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1070. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1071. cur_state = !!(val & PIPECONF_ENABLE);
  1072. intel_display_power_put(dev_priv, power_domain);
  1073. } else {
  1074. cur_state = false;
  1075. }
  1076. I915_STATE_WARN(cur_state != state,
  1077. "pipe %c assertion failure (expected %s, current %s)\n",
  1078. pipe_name(pipe), onoff(state), onoff(cur_state));
  1079. }
  1080. static void assert_plane(struct intel_plane *plane, bool state)
  1081. {
  1082. bool cur_state = plane->get_hw_state(plane);
  1083. I915_STATE_WARN(cur_state != state,
  1084. "%s assertion failure (expected %s, current %s)\n",
  1085. plane->base.name, onoff(state), onoff(cur_state));
  1086. }
  1087. #define assert_plane_enabled(p) assert_plane(p, true)
  1088. #define assert_plane_disabled(p) assert_plane(p, false)
  1089. static void assert_planes_disabled(struct intel_crtc *crtc)
  1090. {
  1091. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1092. struct intel_plane *plane;
  1093. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1094. assert_plane_disabled(plane);
  1095. }
  1096. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1097. {
  1098. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1099. drm_crtc_vblank_put(crtc);
  1100. }
  1101. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe)
  1103. {
  1104. u32 val;
  1105. bool enabled;
  1106. val = I915_READ(PCH_TRANSCONF(pipe));
  1107. enabled = !!(val & TRANS_ENABLE);
  1108. I915_STATE_WARN(enabled,
  1109. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1110. pipe_name(pipe));
  1111. }
  1112. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1113. enum pipe pipe, u32 port_sel, u32 val)
  1114. {
  1115. if ((val & DP_PORT_EN) == 0)
  1116. return false;
  1117. if (HAS_PCH_CPT(dev_priv)) {
  1118. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1119. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1120. return false;
  1121. } else if (IS_CHERRYVIEW(dev_priv)) {
  1122. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1123. return false;
  1124. } else {
  1125. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1126. return false;
  1127. }
  1128. return true;
  1129. }
  1130. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1131. enum pipe pipe, u32 val)
  1132. {
  1133. if ((val & SDVO_ENABLE) == 0)
  1134. return false;
  1135. if (HAS_PCH_CPT(dev_priv)) {
  1136. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1137. return false;
  1138. } else if (IS_CHERRYVIEW(dev_priv)) {
  1139. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1140. return false;
  1141. } else {
  1142. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1143. return false;
  1144. }
  1145. return true;
  1146. }
  1147. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1148. enum pipe pipe, u32 val)
  1149. {
  1150. if ((val & LVDS_PORT_EN) == 0)
  1151. return false;
  1152. if (HAS_PCH_CPT(dev_priv)) {
  1153. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1154. return false;
  1155. } else {
  1156. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1157. return false;
  1158. }
  1159. return true;
  1160. }
  1161. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1162. enum pipe pipe, u32 val)
  1163. {
  1164. if ((val & ADPA_DAC_ENABLE) == 0)
  1165. return false;
  1166. if (HAS_PCH_CPT(dev_priv)) {
  1167. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1168. return false;
  1169. } else {
  1170. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1171. return false;
  1172. }
  1173. return true;
  1174. }
  1175. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1176. enum pipe pipe, i915_reg_t reg,
  1177. u32 port_sel)
  1178. {
  1179. u32 val = I915_READ(reg);
  1180. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1181. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1182. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1183. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1184. && (val & DP_PIPEB_SELECT),
  1185. "IBX PCH dp port still using transcoder B\n");
  1186. }
  1187. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1188. enum pipe pipe, i915_reg_t reg)
  1189. {
  1190. u32 val = I915_READ(reg);
  1191. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1192. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1193. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1194. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1195. && (val & SDVO_PIPE_B_SELECT),
  1196. "IBX PCH hdmi port still using transcoder B\n");
  1197. }
  1198. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe)
  1200. {
  1201. u32 val;
  1202. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1203. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1204. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1205. val = I915_READ(PCH_ADPA);
  1206. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1207. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1208. pipe_name(pipe));
  1209. val = I915_READ(PCH_LVDS);
  1210. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1211. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1212. pipe_name(pipe));
  1213. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1214. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1215. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1216. }
  1217. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1218. const struct intel_crtc_state *pipe_config)
  1219. {
  1220. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1221. enum pipe pipe = crtc->pipe;
  1222. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1223. POSTING_READ(DPLL(pipe));
  1224. udelay(150);
  1225. if (intel_wait_for_register(dev_priv,
  1226. DPLL(pipe),
  1227. DPLL_LOCK_VLV,
  1228. DPLL_LOCK_VLV,
  1229. 1))
  1230. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1231. }
  1232. static void vlv_enable_pll(struct intel_crtc *crtc,
  1233. const struct intel_crtc_state *pipe_config)
  1234. {
  1235. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1236. enum pipe pipe = crtc->pipe;
  1237. assert_pipe_disabled(dev_priv, pipe);
  1238. /* PLL is protected by panel, make sure we can write it */
  1239. assert_panel_unlocked(dev_priv, pipe);
  1240. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1241. _vlv_enable_pll(crtc, pipe_config);
  1242. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1243. POSTING_READ(DPLL_MD(pipe));
  1244. }
  1245. static void _chv_enable_pll(struct intel_crtc *crtc,
  1246. const struct intel_crtc_state *pipe_config)
  1247. {
  1248. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1249. enum pipe pipe = crtc->pipe;
  1250. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1251. u32 tmp;
  1252. mutex_lock(&dev_priv->sb_lock);
  1253. /* Enable back the 10bit clock to display controller */
  1254. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1255. tmp |= DPIO_DCLKP_EN;
  1256. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1257. mutex_unlock(&dev_priv->sb_lock);
  1258. /*
  1259. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1260. */
  1261. udelay(1);
  1262. /* Enable PLL */
  1263. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1264. /* Check PLL is locked */
  1265. if (intel_wait_for_register(dev_priv,
  1266. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1267. 1))
  1268. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1269. }
  1270. static void chv_enable_pll(struct intel_crtc *crtc,
  1271. const struct intel_crtc_state *pipe_config)
  1272. {
  1273. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1274. enum pipe pipe = crtc->pipe;
  1275. assert_pipe_disabled(dev_priv, pipe);
  1276. /* PLL is protected by panel, make sure we can write it */
  1277. assert_panel_unlocked(dev_priv, pipe);
  1278. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1279. _chv_enable_pll(crtc, pipe_config);
  1280. if (pipe != PIPE_A) {
  1281. /*
  1282. * WaPixelRepeatModeFixForC0:chv
  1283. *
  1284. * DPLLCMD is AWOL. Use chicken bits to propagate
  1285. * the value from DPLLBMD to either pipe B or C.
  1286. */
  1287. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1288. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1289. I915_WRITE(CBR4_VLV, 0);
  1290. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1291. /*
  1292. * DPLLB VGA mode also seems to cause problems.
  1293. * We should always have it disabled.
  1294. */
  1295. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1296. } else {
  1297. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1298. POSTING_READ(DPLL_MD(pipe));
  1299. }
  1300. }
  1301. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1302. {
  1303. struct intel_crtc *crtc;
  1304. int count = 0;
  1305. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1306. count += crtc->base.state->active &&
  1307. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1308. }
  1309. return count;
  1310. }
  1311. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1312. const struct intel_crtc_state *crtc_state)
  1313. {
  1314. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1315. i915_reg_t reg = DPLL(crtc->pipe);
  1316. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1317. int i;
  1318. assert_pipe_disabled(dev_priv, crtc->pipe);
  1319. /* PLL is protected by panel, make sure we can write it */
  1320. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1321. assert_panel_unlocked(dev_priv, crtc->pipe);
  1322. /* Enable DVO 2x clock on both PLLs if necessary */
  1323. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1324. /*
  1325. * It appears to be important that we don't enable this
  1326. * for the current pipe before otherwise configuring the
  1327. * PLL. No idea how this should be handled if multiple
  1328. * DVO outputs are enabled simultaneosly.
  1329. */
  1330. dpll |= DPLL_DVO_2X_MODE;
  1331. I915_WRITE(DPLL(!crtc->pipe),
  1332. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1333. }
  1334. /*
  1335. * Apparently we need to have VGA mode enabled prior to changing
  1336. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1337. * dividers, even though the register value does change.
  1338. */
  1339. I915_WRITE(reg, 0);
  1340. I915_WRITE(reg, dpll);
  1341. /* Wait for the clocks to stabilize. */
  1342. POSTING_READ(reg);
  1343. udelay(150);
  1344. if (INTEL_GEN(dev_priv) >= 4) {
  1345. I915_WRITE(DPLL_MD(crtc->pipe),
  1346. crtc_state->dpll_hw_state.dpll_md);
  1347. } else {
  1348. /* The pixel multiplier can only be updated once the
  1349. * DPLL is enabled and the clocks are stable.
  1350. *
  1351. * So write it again.
  1352. */
  1353. I915_WRITE(reg, dpll);
  1354. }
  1355. /* We do this three times for luck */
  1356. for (i = 0; i < 3; i++) {
  1357. I915_WRITE(reg, dpll);
  1358. POSTING_READ(reg);
  1359. udelay(150); /* wait for warmup */
  1360. }
  1361. }
  1362. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1363. {
  1364. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1365. enum pipe pipe = crtc->pipe;
  1366. /* Disable DVO 2x clock on both PLLs if necessary */
  1367. if (IS_I830(dev_priv) &&
  1368. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1369. !intel_num_dvo_pipes(dev_priv)) {
  1370. I915_WRITE(DPLL(PIPE_B),
  1371. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1372. I915_WRITE(DPLL(PIPE_A),
  1373. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1374. }
  1375. /* Don't disable pipe or pipe PLLs if needed */
  1376. if (IS_I830(dev_priv))
  1377. return;
  1378. /* Make sure the pipe isn't still relying on us */
  1379. assert_pipe_disabled(dev_priv, pipe);
  1380. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1381. POSTING_READ(DPLL(pipe));
  1382. }
  1383. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1384. {
  1385. u32 val;
  1386. /* Make sure the pipe isn't still relying on us */
  1387. assert_pipe_disabled(dev_priv, pipe);
  1388. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1389. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1390. if (pipe != PIPE_A)
  1391. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1392. I915_WRITE(DPLL(pipe), val);
  1393. POSTING_READ(DPLL(pipe));
  1394. }
  1395. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1396. {
  1397. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1398. u32 val;
  1399. /* Make sure the pipe isn't still relying on us */
  1400. assert_pipe_disabled(dev_priv, pipe);
  1401. val = DPLL_SSC_REF_CLK_CHV |
  1402. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1403. if (pipe != PIPE_A)
  1404. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1405. I915_WRITE(DPLL(pipe), val);
  1406. POSTING_READ(DPLL(pipe));
  1407. mutex_lock(&dev_priv->sb_lock);
  1408. /* Disable 10bit clock to display controller */
  1409. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1410. val &= ~DPIO_DCLKP_EN;
  1411. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1412. mutex_unlock(&dev_priv->sb_lock);
  1413. }
  1414. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1415. struct intel_digital_port *dport,
  1416. unsigned int expected_mask)
  1417. {
  1418. u32 port_mask;
  1419. i915_reg_t dpll_reg;
  1420. switch (dport->base.port) {
  1421. case PORT_B:
  1422. port_mask = DPLL_PORTB_READY_MASK;
  1423. dpll_reg = DPLL(0);
  1424. break;
  1425. case PORT_C:
  1426. port_mask = DPLL_PORTC_READY_MASK;
  1427. dpll_reg = DPLL(0);
  1428. expected_mask <<= 4;
  1429. break;
  1430. case PORT_D:
  1431. port_mask = DPLL_PORTD_READY_MASK;
  1432. dpll_reg = DPIO_PHY_STATUS;
  1433. break;
  1434. default:
  1435. BUG();
  1436. }
  1437. if (intel_wait_for_register(dev_priv,
  1438. dpll_reg, port_mask, expected_mask,
  1439. 1000))
  1440. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1441. port_name(dport->base.port),
  1442. I915_READ(dpll_reg) & port_mask, expected_mask);
  1443. }
  1444. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1445. enum pipe pipe)
  1446. {
  1447. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1448. pipe);
  1449. i915_reg_t reg;
  1450. uint32_t val, pipeconf_val;
  1451. /* Make sure PCH DPLL is enabled */
  1452. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1453. /* FDI must be feeding us bits for PCH ports */
  1454. assert_fdi_tx_enabled(dev_priv, pipe);
  1455. assert_fdi_rx_enabled(dev_priv, pipe);
  1456. if (HAS_PCH_CPT(dev_priv)) {
  1457. /* Workaround: Set the timing override bit before enabling the
  1458. * pch transcoder. */
  1459. reg = TRANS_CHICKEN2(pipe);
  1460. val = I915_READ(reg);
  1461. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1462. I915_WRITE(reg, val);
  1463. }
  1464. reg = PCH_TRANSCONF(pipe);
  1465. val = I915_READ(reg);
  1466. pipeconf_val = I915_READ(PIPECONF(pipe));
  1467. if (HAS_PCH_IBX(dev_priv)) {
  1468. /*
  1469. * Make the BPC in transcoder be consistent with
  1470. * that in pipeconf reg. For HDMI we must use 8bpc
  1471. * here for both 8bpc and 12bpc.
  1472. */
  1473. val &= ~PIPECONF_BPC_MASK;
  1474. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1475. val |= PIPECONF_8BPC;
  1476. else
  1477. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1478. }
  1479. val &= ~TRANS_INTERLACE_MASK;
  1480. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1481. if (HAS_PCH_IBX(dev_priv) &&
  1482. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1483. val |= TRANS_LEGACY_INTERLACED_ILK;
  1484. else
  1485. val |= TRANS_INTERLACED;
  1486. else
  1487. val |= TRANS_PROGRESSIVE;
  1488. I915_WRITE(reg, val | TRANS_ENABLE);
  1489. if (intel_wait_for_register(dev_priv,
  1490. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1491. 100))
  1492. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1493. }
  1494. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1495. enum transcoder cpu_transcoder)
  1496. {
  1497. u32 val, pipeconf_val;
  1498. /* FDI must be feeding us bits for PCH ports */
  1499. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1500. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1501. /* Workaround: set timing override bit. */
  1502. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1503. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1504. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1505. val = TRANS_ENABLE;
  1506. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1507. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1508. PIPECONF_INTERLACED_ILK)
  1509. val |= TRANS_INTERLACED;
  1510. else
  1511. val |= TRANS_PROGRESSIVE;
  1512. I915_WRITE(LPT_TRANSCONF, val);
  1513. if (intel_wait_for_register(dev_priv,
  1514. LPT_TRANSCONF,
  1515. TRANS_STATE_ENABLE,
  1516. TRANS_STATE_ENABLE,
  1517. 100))
  1518. DRM_ERROR("Failed to enable PCH transcoder\n");
  1519. }
  1520. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1521. enum pipe pipe)
  1522. {
  1523. i915_reg_t reg;
  1524. uint32_t val;
  1525. /* FDI relies on the transcoder */
  1526. assert_fdi_tx_disabled(dev_priv, pipe);
  1527. assert_fdi_rx_disabled(dev_priv, pipe);
  1528. /* Ports must be off as well */
  1529. assert_pch_ports_disabled(dev_priv, pipe);
  1530. reg = PCH_TRANSCONF(pipe);
  1531. val = I915_READ(reg);
  1532. val &= ~TRANS_ENABLE;
  1533. I915_WRITE(reg, val);
  1534. /* wait for PCH transcoder off, transcoder state */
  1535. if (intel_wait_for_register(dev_priv,
  1536. reg, TRANS_STATE_ENABLE, 0,
  1537. 50))
  1538. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1539. if (HAS_PCH_CPT(dev_priv)) {
  1540. /* Workaround: Clear the timing override chicken bit again. */
  1541. reg = TRANS_CHICKEN2(pipe);
  1542. val = I915_READ(reg);
  1543. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1544. I915_WRITE(reg, val);
  1545. }
  1546. }
  1547. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1548. {
  1549. u32 val;
  1550. val = I915_READ(LPT_TRANSCONF);
  1551. val &= ~TRANS_ENABLE;
  1552. I915_WRITE(LPT_TRANSCONF, val);
  1553. /* wait for PCH transcoder off, transcoder state */
  1554. if (intel_wait_for_register(dev_priv,
  1555. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1556. 50))
  1557. DRM_ERROR("Failed to disable PCH transcoder\n");
  1558. /* Workaround: clear timing override bit. */
  1559. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1560. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1561. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1562. }
  1563. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1564. {
  1565. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1566. if (HAS_PCH_LPT(dev_priv))
  1567. return PIPE_A;
  1568. else
  1569. return crtc->pipe;
  1570. }
  1571. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1572. {
  1573. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1574. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1575. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1576. enum pipe pipe = crtc->pipe;
  1577. i915_reg_t reg;
  1578. u32 val;
  1579. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1580. assert_planes_disabled(crtc);
  1581. /*
  1582. * A pipe without a PLL won't actually be able to drive bits from
  1583. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1584. * need the check.
  1585. */
  1586. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1587. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1588. assert_dsi_pll_enabled(dev_priv);
  1589. else
  1590. assert_pll_enabled(dev_priv, pipe);
  1591. } else {
  1592. if (new_crtc_state->has_pch_encoder) {
  1593. /* if driving the PCH, we need FDI enabled */
  1594. assert_fdi_rx_pll_enabled(dev_priv,
  1595. intel_crtc_pch_transcoder(crtc));
  1596. assert_fdi_tx_pll_enabled(dev_priv,
  1597. (enum pipe) cpu_transcoder);
  1598. }
  1599. /* FIXME: assert CPU port conditions for SNB+ */
  1600. }
  1601. reg = PIPECONF(cpu_transcoder);
  1602. val = I915_READ(reg);
  1603. if (val & PIPECONF_ENABLE) {
  1604. /* we keep both pipes enabled on 830 */
  1605. WARN_ON(!IS_I830(dev_priv));
  1606. return;
  1607. }
  1608. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1609. POSTING_READ(reg);
  1610. /*
  1611. * Until the pipe starts PIPEDSL reads will return a stale value,
  1612. * which causes an apparent vblank timestamp jump when PIPEDSL
  1613. * resets to its proper value. That also messes up the frame count
  1614. * when it's derived from the timestamps. So let's wait for the
  1615. * pipe to start properly before we call drm_crtc_vblank_on()
  1616. */
  1617. if (dev_priv->drm.max_vblank_count == 0)
  1618. intel_wait_for_pipe_scanline_moving(crtc);
  1619. }
  1620. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1621. {
  1622. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1623. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1624. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1625. enum pipe pipe = crtc->pipe;
  1626. i915_reg_t reg;
  1627. u32 val;
  1628. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1629. /*
  1630. * Make sure planes won't keep trying to pump pixels to us,
  1631. * or we might hang the display.
  1632. */
  1633. assert_planes_disabled(crtc);
  1634. reg = PIPECONF(cpu_transcoder);
  1635. val = I915_READ(reg);
  1636. if ((val & PIPECONF_ENABLE) == 0)
  1637. return;
  1638. /*
  1639. * Double wide has implications for planes
  1640. * so best keep it disabled when not needed.
  1641. */
  1642. if (old_crtc_state->double_wide)
  1643. val &= ~PIPECONF_DOUBLE_WIDE;
  1644. /* Don't disable pipe or pipe PLLs if needed */
  1645. if (!IS_I830(dev_priv))
  1646. val &= ~PIPECONF_ENABLE;
  1647. I915_WRITE(reg, val);
  1648. if ((val & PIPECONF_ENABLE) == 0)
  1649. intel_wait_for_pipe_off(old_crtc_state);
  1650. }
  1651. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1652. {
  1653. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1654. }
  1655. static unsigned int
  1656. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1657. {
  1658. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1659. unsigned int cpp = fb->format->cpp[plane];
  1660. switch (fb->modifier) {
  1661. case DRM_FORMAT_MOD_LINEAR:
  1662. return cpp;
  1663. case I915_FORMAT_MOD_X_TILED:
  1664. if (IS_GEN2(dev_priv))
  1665. return 128;
  1666. else
  1667. return 512;
  1668. case I915_FORMAT_MOD_Y_TILED_CCS:
  1669. if (plane == 1)
  1670. return 128;
  1671. /* fall through */
  1672. case I915_FORMAT_MOD_Y_TILED:
  1673. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1674. return 128;
  1675. else
  1676. return 512;
  1677. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1678. if (plane == 1)
  1679. return 128;
  1680. /* fall through */
  1681. case I915_FORMAT_MOD_Yf_TILED:
  1682. switch (cpp) {
  1683. case 1:
  1684. return 64;
  1685. case 2:
  1686. case 4:
  1687. return 128;
  1688. case 8:
  1689. case 16:
  1690. return 256;
  1691. default:
  1692. MISSING_CASE(cpp);
  1693. return cpp;
  1694. }
  1695. break;
  1696. default:
  1697. MISSING_CASE(fb->modifier);
  1698. return cpp;
  1699. }
  1700. }
  1701. static unsigned int
  1702. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1703. {
  1704. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1705. return 1;
  1706. else
  1707. return intel_tile_size(to_i915(fb->dev)) /
  1708. intel_tile_width_bytes(fb, plane);
  1709. }
  1710. /* Return the tile dimensions in pixel units */
  1711. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1712. unsigned int *tile_width,
  1713. unsigned int *tile_height)
  1714. {
  1715. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1716. unsigned int cpp = fb->format->cpp[plane];
  1717. *tile_width = tile_width_bytes / cpp;
  1718. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1719. }
  1720. unsigned int
  1721. intel_fb_align_height(const struct drm_framebuffer *fb,
  1722. int plane, unsigned int height)
  1723. {
  1724. unsigned int tile_height = intel_tile_height(fb, plane);
  1725. return ALIGN(height, tile_height);
  1726. }
  1727. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1728. {
  1729. unsigned int size = 0;
  1730. int i;
  1731. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1732. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1733. return size;
  1734. }
  1735. static void
  1736. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1737. const struct drm_framebuffer *fb,
  1738. unsigned int rotation)
  1739. {
  1740. view->type = I915_GGTT_VIEW_NORMAL;
  1741. if (drm_rotation_90_or_270(rotation)) {
  1742. view->type = I915_GGTT_VIEW_ROTATED;
  1743. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1744. }
  1745. }
  1746. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1747. {
  1748. if (IS_I830(dev_priv))
  1749. return 16 * 1024;
  1750. else if (IS_I85X(dev_priv))
  1751. return 256;
  1752. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1753. return 32;
  1754. else
  1755. return 4 * 1024;
  1756. }
  1757. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1758. {
  1759. if (INTEL_GEN(dev_priv) >= 9)
  1760. return 256 * 1024;
  1761. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1762. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1763. return 128 * 1024;
  1764. else if (INTEL_GEN(dev_priv) >= 4)
  1765. return 4 * 1024;
  1766. else
  1767. return 0;
  1768. }
  1769. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1770. int plane)
  1771. {
  1772. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1773. /* AUX_DIST needs only 4K alignment */
  1774. if (plane == 1)
  1775. return 4096;
  1776. switch (fb->modifier) {
  1777. case DRM_FORMAT_MOD_LINEAR:
  1778. return intel_linear_alignment(dev_priv);
  1779. case I915_FORMAT_MOD_X_TILED:
  1780. if (INTEL_GEN(dev_priv) >= 9)
  1781. return 256 * 1024;
  1782. return 0;
  1783. case I915_FORMAT_MOD_Y_TILED_CCS:
  1784. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1785. case I915_FORMAT_MOD_Y_TILED:
  1786. case I915_FORMAT_MOD_Yf_TILED:
  1787. return 1 * 1024 * 1024;
  1788. default:
  1789. MISSING_CASE(fb->modifier);
  1790. return 0;
  1791. }
  1792. }
  1793. static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
  1794. {
  1795. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1796. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1797. return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
  1798. }
  1799. struct i915_vma *
  1800. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1801. unsigned int rotation,
  1802. bool uses_fence,
  1803. unsigned long *out_flags)
  1804. {
  1805. struct drm_device *dev = fb->dev;
  1806. struct drm_i915_private *dev_priv = to_i915(dev);
  1807. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1808. struct i915_ggtt_view view;
  1809. struct i915_vma *vma;
  1810. unsigned int pinctl;
  1811. u32 alignment;
  1812. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1813. alignment = intel_surf_alignment(fb, 0);
  1814. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1815. /* Note that the w/a also requires 64 PTE of padding following the
  1816. * bo. We currently fill all unused PTE with the shadow page and so
  1817. * we should always have valid PTE following the scanout preventing
  1818. * the VT-d warning.
  1819. */
  1820. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1821. alignment = 256 * 1024;
  1822. /*
  1823. * Global gtt pte registers are special registers which actually forward
  1824. * writes to a chunk of system memory. Which means that there is no risk
  1825. * that the register values disappear as soon as we call
  1826. * intel_runtime_pm_put(), so it is correct to wrap only the
  1827. * pin/unpin/fence and not more.
  1828. */
  1829. intel_runtime_pm_get(dev_priv);
  1830. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1831. pinctl = 0;
  1832. /* Valleyview is definitely limited to scanning out the first
  1833. * 512MiB. Lets presume this behaviour was inherited from the
  1834. * g4x display engine and that all earlier gen are similarly
  1835. * limited. Testing suggests that it is a little more
  1836. * complicated than this. For example, Cherryview appears quite
  1837. * happy to scanout from anywhere within its global aperture.
  1838. */
  1839. if (HAS_GMCH_DISPLAY(dev_priv))
  1840. pinctl |= PIN_MAPPABLE;
  1841. vma = i915_gem_object_pin_to_display_plane(obj,
  1842. alignment, &view, pinctl);
  1843. if (IS_ERR(vma))
  1844. goto err;
  1845. if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
  1846. int ret;
  1847. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1848. * fence, whereas 965+ only requires a fence if using
  1849. * framebuffer compression. For simplicity, we always, when
  1850. * possible, install a fence as the cost is not that onerous.
  1851. *
  1852. * If we fail to fence the tiled scanout, then either the
  1853. * modeset will reject the change (which is highly unlikely as
  1854. * the affected systems, all but one, do not have unmappable
  1855. * space) or we will not be able to enable full powersaving
  1856. * techniques (also likely not to apply due to various limits
  1857. * FBC and the like impose on the size of the buffer, which
  1858. * presumably we violated anyway with this unmappable buffer).
  1859. * Anyway, it is presumably better to stumble onwards with
  1860. * something and try to run the system in a "less than optimal"
  1861. * mode that matches the user configuration.
  1862. */
  1863. ret = i915_vma_pin_fence(vma);
  1864. if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
  1865. i915_gem_object_unpin_from_display_plane(vma);
  1866. vma = ERR_PTR(ret);
  1867. goto err;
  1868. }
  1869. if (ret == 0 && vma->fence)
  1870. *out_flags |= PLANE_HAS_FENCE;
  1871. }
  1872. i915_vma_get(vma);
  1873. err:
  1874. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1875. intel_runtime_pm_put(dev_priv);
  1876. return vma;
  1877. }
  1878. void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
  1879. {
  1880. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1881. if (flags & PLANE_HAS_FENCE)
  1882. i915_vma_unpin_fence(vma);
  1883. i915_gem_object_unpin_from_display_plane(vma);
  1884. i915_vma_put(vma);
  1885. }
  1886. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1887. unsigned int rotation)
  1888. {
  1889. if (drm_rotation_90_or_270(rotation))
  1890. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1891. else
  1892. return fb->pitches[plane];
  1893. }
  1894. /*
  1895. * Convert the x/y offsets into a linear offset.
  1896. * Only valid with 0/180 degree rotation, which is fine since linear
  1897. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1898. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1899. */
  1900. u32 intel_fb_xy_to_linear(int x, int y,
  1901. const struct intel_plane_state *state,
  1902. int plane)
  1903. {
  1904. const struct drm_framebuffer *fb = state->base.fb;
  1905. unsigned int cpp = fb->format->cpp[plane];
  1906. unsigned int pitch = fb->pitches[plane];
  1907. return y * pitch + x * cpp;
  1908. }
  1909. /*
  1910. * Add the x/y offsets derived from fb->offsets[] to the user
  1911. * specified plane src x/y offsets. The resulting x/y offsets
  1912. * specify the start of scanout from the beginning of the gtt mapping.
  1913. */
  1914. void intel_add_fb_offsets(int *x, int *y,
  1915. const struct intel_plane_state *state,
  1916. int plane)
  1917. {
  1918. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1919. unsigned int rotation = state->base.rotation;
  1920. if (drm_rotation_90_or_270(rotation)) {
  1921. *x += intel_fb->rotated[plane].x;
  1922. *y += intel_fb->rotated[plane].y;
  1923. } else {
  1924. *x += intel_fb->normal[plane].x;
  1925. *y += intel_fb->normal[plane].y;
  1926. }
  1927. }
  1928. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1929. unsigned int tile_width,
  1930. unsigned int tile_height,
  1931. unsigned int tile_size,
  1932. unsigned int pitch_tiles,
  1933. u32 old_offset,
  1934. u32 new_offset)
  1935. {
  1936. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1937. unsigned int tiles;
  1938. WARN_ON(old_offset & (tile_size - 1));
  1939. WARN_ON(new_offset & (tile_size - 1));
  1940. WARN_ON(new_offset > old_offset);
  1941. tiles = (old_offset - new_offset) / tile_size;
  1942. *y += tiles / pitch_tiles * tile_height;
  1943. *x += tiles % pitch_tiles * tile_width;
  1944. /* minimize x in case it got needlessly big */
  1945. *y += *x / pitch_pixels * tile_height;
  1946. *x %= pitch_pixels;
  1947. return new_offset;
  1948. }
  1949. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1950. const struct drm_framebuffer *fb, int plane,
  1951. unsigned int rotation,
  1952. u32 old_offset, u32 new_offset)
  1953. {
  1954. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1955. unsigned int cpp = fb->format->cpp[plane];
  1956. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1957. WARN_ON(new_offset > old_offset);
  1958. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1959. unsigned int tile_size, tile_width, tile_height;
  1960. unsigned int pitch_tiles;
  1961. tile_size = intel_tile_size(dev_priv);
  1962. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1963. if (drm_rotation_90_or_270(rotation)) {
  1964. pitch_tiles = pitch / tile_height;
  1965. swap(tile_width, tile_height);
  1966. } else {
  1967. pitch_tiles = pitch / (tile_width * cpp);
  1968. }
  1969. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1970. tile_size, pitch_tiles,
  1971. old_offset, new_offset);
  1972. } else {
  1973. old_offset += *y * pitch + *x * cpp;
  1974. *y = (old_offset - new_offset) / pitch;
  1975. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1976. }
  1977. return new_offset;
  1978. }
  1979. /*
  1980. * Adjust the tile offset by moving the difference into
  1981. * the x/y offsets.
  1982. */
  1983. static u32 intel_adjust_tile_offset(int *x, int *y,
  1984. const struct intel_plane_state *state, int plane,
  1985. u32 old_offset, u32 new_offset)
  1986. {
  1987. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  1988. state->base.rotation,
  1989. old_offset, new_offset);
  1990. }
  1991. /*
  1992. * Computes the linear offset to the base tile and adjusts
  1993. * x, y. bytes per pixel is assumed to be a power-of-two.
  1994. *
  1995. * In the 90/270 rotated case, x and y are assumed
  1996. * to be already rotated to match the rotated GTT view, and
  1997. * pitch is the tile_height aligned framebuffer height.
  1998. *
  1999. * This function is used when computing the derived information
  2000. * under intel_framebuffer, so using any of that information
  2001. * here is not allowed. Anything under drm_framebuffer can be
  2002. * used. This is why the user has to pass in the pitch since it
  2003. * is specified in the rotated orientation.
  2004. */
  2005. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2006. int *x, int *y,
  2007. const struct drm_framebuffer *fb, int plane,
  2008. unsigned int pitch,
  2009. unsigned int rotation,
  2010. u32 alignment)
  2011. {
  2012. uint64_t fb_modifier = fb->modifier;
  2013. unsigned int cpp = fb->format->cpp[plane];
  2014. u32 offset, offset_aligned;
  2015. if (alignment)
  2016. alignment--;
  2017. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2018. unsigned int tile_size, tile_width, tile_height;
  2019. unsigned int tile_rows, tiles, pitch_tiles;
  2020. tile_size = intel_tile_size(dev_priv);
  2021. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2022. if (drm_rotation_90_or_270(rotation)) {
  2023. pitch_tiles = pitch / tile_height;
  2024. swap(tile_width, tile_height);
  2025. } else {
  2026. pitch_tiles = pitch / (tile_width * cpp);
  2027. }
  2028. tile_rows = *y / tile_height;
  2029. *y %= tile_height;
  2030. tiles = *x / tile_width;
  2031. *x %= tile_width;
  2032. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2033. offset_aligned = offset & ~alignment;
  2034. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2035. tile_size, pitch_tiles,
  2036. offset, offset_aligned);
  2037. } else {
  2038. offset = *y * pitch + *x * cpp;
  2039. offset_aligned = offset & ~alignment;
  2040. *y = (offset & alignment) / pitch;
  2041. *x = ((offset & alignment) - *y * pitch) / cpp;
  2042. }
  2043. return offset_aligned;
  2044. }
  2045. u32 intel_compute_tile_offset(int *x, int *y,
  2046. const struct intel_plane_state *state,
  2047. int plane)
  2048. {
  2049. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2050. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2051. const struct drm_framebuffer *fb = state->base.fb;
  2052. unsigned int rotation = state->base.rotation;
  2053. int pitch = intel_fb_pitch(fb, plane, rotation);
  2054. u32 alignment;
  2055. if (intel_plane->id == PLANE_CURSOR)
  2056. alignment = intel_cursor_alignment(dev_priv);
  2057. else
  2058. alignment = intel_surf_alignment(fb, plane);
  2059. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2060. rotation, alignment);
  2061. }
  2062. /* Convert the fb->offset[] into x/y offsets */
  2063. static int intel_fb_offset_to_xy(int *x, int *y,
  2064. const struct drm_framebuffer *fb, int plane)
  2065. {
  2066. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2067. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2068. fb->offsets[plane] % intel_tile_size(dev_priv))
  2069. return -EINVAL;
  2070. *x = 0;
  2071. *y = 0;
  2072. _intel_adjust_tile_offset(x, y,
  2073. fb, plane, DRM_MODE_ROTATE_0,
  2074. fb->offsets[plane], 0);
  2075. return 0;
  2076. }
  2077. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2078. {
  2079. switch (fb_modifier) {
  2080. case I915_FORMAT_MOD_X_TILED:
  2081. return I915_TILING_X;
  2082. case I915_FORMAT_MOD_Y_TILED:
  2083. case I915_FORMAT_MOD_Y_TILED_CCS:
  2084. return I915_TILING_Y;
  2085. default:
  2086. return I915_TILING_NONE;
  2087. }
  2088. }
  2089. /*
  2090. * From the Sky Lake PRM:
  2091. * "The Color Control Surface (CCS) contains the compression status of
  2092. * the cache-line pairs. The compression state of the cache-line pair
  2093. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2094. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2095. * cache-line-pairs. CCS is always Y tiled."
  2096. *
  2097. * Since cache line pairs refers to horizontally adjacent cache lines,
  2098. * each cache line in the CCS corresponds to an area of 32x16 cache
  2099. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2100. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2101. * main surface.
  2102. */
  2103. static const struct drm_format_info ccs_formats[] = {
  2104. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2105. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2106. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2107. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2108. };
  2109. static const struct drm_format_info *
  2110. lookup_format_info(const struct drm_format_info formats[],
  2111. int num_formats, u32 format)
  2112. {
  2113. int i;
  2114. for (i = 0; i < num_formats; i++) {
  2115. if (formats[i].format == format)
  2116. return &formats[i];
  2117. }
  2118. return NULL;
  2119. }
  2120. static const struct drm_format_info *
  2121. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2122. {
  2123. switch (cmd->modifier[0]) {
  2124. case I915_FORMAT_MOD_Y_TILED_CCS:
  2125. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2126. return lookup_format_info(ccs_formats,
  2127. ARRAY_SIZE(ccs_formats),
  2128. cmd->pixel_format);
  2129. default:
  2130. return NULL;
  2131. }
  2132. }
  2133. static int
  2134. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2135. struct drm_framebuffer *fb)
  2136. {
  2137. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2138. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2139. u32 gtt_offset_rotated = 0;
  2140. unsigned int max_size = 0;
  2141. int i, num_planes = fb->format->num_planes;
  2142. unsigned int tile_size = intel_tile_size(dev_priv);
  2143. for (i = 0; i < num_planes; i++) {
  2144. unsigned int width, height;
  2145. unsigned int cpp, size;
  2146. u32 offset;
  2147. int x, y;
  2148. int ret;
  2149. cpp = fb->format->cpp[i];
  2150. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2151. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2152. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2153. if (ret) {
  2154. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2155. i, fb->offsets[i]);
  2156. return ret;
  2157. }
  2158. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2159. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2160. int hsub = fb->format->hsub;
  2161. int vsub = fb->format->vsub;
  2162. int tile_width, tile_height;
  2163. int main_x, main_y;
  2164. int ccs_x, ccs_y;
  2165. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2166. tile_width *= hsub;
  2167. tile_height *= vsub;
  2168. ccs_x = (x * hsub) % tile_width;
  2169. ccs_y = (y * vsub) % tile_height;
  2170. main_x = intel_fb->normal[0].x % tile_width;
  2171. main_y = intel_fb->normal[0].y % tile_height;
  2172. /*
  2173. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2174. * x/y offsets must match between CCS and the main surface.
  2175. */
  2176. if (main_x != ccs_x || main_y != ccs_y) {
  2177. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2178. main_x, main_y,
  2179. ccs_x, ccs_y,
  2180. intel_fb->normal[0].x,
  2181. intel_fb->normal[0].y,
  2182. x, y);
  2183. return -EINVAL;
  2184. }
  2185. }
  2186. /*
  2187. * The fence (if used) is aligned to the start of the object
  2188. * so having the framebuffer wrap around across the edge of the
  2189. * fenced region doesn't really work. We have no API to configure
  2190. * the fence start offset within the object (nor could we probably
  2191. * on gen2/3). So it's just easier if we just require that the
  2192. * fb layout agrees with the fence layout. We already check that the
  2193. * fb stride matches the fence stride elsewhere.
  2194. */
  2195. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2196. (x + width) * cpp > fb->pitches[i]) {
  2197. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2198. i, fb->offsets[i]);
  2199. return -EINVAL;
  2200. }
  2201. /*
  2202. * First pixel of the framebuffer from
  2203. * the start of the normal gtt mapping.
  2204. */
  2205. intel_fb->normal[i].x = x;
  2206. intel_fb->normal[i].y = y;
  2207. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2208. fb, i, fb->pitches[i],
  2209. DRM_MODE_ROTATE_0, tile_size);
  2210. offset /= tile_size;
  2211. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2212. unsigned int tile_width, tile_height;
  2213. unsigned int pitch_tiles;
  2214. struct drm_rect r;
  2215. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2216. rot_info->plane[i].offset = offset;
  2217. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2218. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2219. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2220. intel_fb->rotated[i].pitch =
  2221. rot_info->plane[i].height * tile_height;
  2222. /* how many tiles does this plane need */
  2223. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2224. /*
  2225. * If the plane isn't horizontally tile aligned,
  2226. * we need one more tile.
  2227. */
  2228. if (x != 0)
  2229. size++;
  2230. /* rotate the x/y offsets to match the GTT view */
  2231. r.x1 = x;
  2232. r.y1 = y;
  2233. r.x2 = x + width;
  2234. r.y2 = y + height;
  2235. drm_rect_rotate(&r,
  2236. rot_info->plane[i].width * tile_width,
  2237. rot_info->plane[i].height * tile_height,
  2238. DRM_MODE_ROTATE_270);
  2239. x = r.x1;
  2240. y = r.y1;
  2241. /* rotate the tile dimensions to match the GTT view */
  2242. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2243. swap(tile_width, tile_height);
  2244. /*
  2245. * We only keep the x/y offsets, so push all of the
  2246. * gtt offset into the x/y offsets.
  2247. */
  2248. __intel_adjust_tile_offset(&x, &y,
  2249. tile_width, tile_height,
  2250. tile_size, pitch_tiles,
  2251. gtt_offset_rotated * tile_size, 0);
  2252. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2253. /*
  2254. * First pixel of the framebuffer from
  2255. * the start of the rotated gtt mapping.
  2256. */
  2257. intel_fb->rotated[i].x = x;
  2258. intel_fb->rotated[i].y = y;
  2259. } else {
  2260. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2261. x * cpp, tile_size);
  2262. }
  2263. /* how many tiles in total needed in the bo */
  2264. max_size = max(max_size, offset + size);
  2265. }
  2266. if (max_size * tile_size > intel_fb->obj->base.size) {
  2267. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2268. max_size * tile_size, intel_fb->obj->base.size);
  2269. return -EINVAL;
  2270. }
  2271. return 0;
  2272. }
  2273. static int i9xx_format_to_fourcc(int format)
  2274. {
  2275. switch (format) {
  2276. case DISPPLANE_8BPP:
  2277. return DRM_FORMAT_C8;
  2278. case DISPPLANE_BGRX555:
  2279. return DRM_FORMAT_XRGB1555;
  2280. case DISPPLANE_BGRX565:
  2281. return DRM_FORMAT_RGB565;
  2282. default:
  2283. case DISPPLANE_BGRX888:
  2284. return DRM_FORMAT_XRGB8888;
  2285. case DISPPLANE_RGBX888:
  2286. return DRM_FORMAT_XBGR8888;
  2287. case DISPPLANE_BGRX101010:
  2288. return DRM_FORMAT_XRGB2101010;
  2289. case DISPPLANE_RGBX101010:
  2290. return DRM_FORMAT_XBGR2101010;
  2291. }
  2292. }
  2293. int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2294. {
  2295. switch (format) {
  2296. case PLANE_CTL_FORMAT_RGB_565:
  2297. return DRM_FORMAT_RGB565;
  2298. case PLANE_CTL_FORMAT_NV12:
  2299. return DRM_FORMAT_NV12;
  2300. default:
  2301. case PLANE_CTL_FORMAT_XRGB_8888:
  2302. if (rgb_order) {
  2303. if (alpha)
  2304. return DRM_FORMAT_ABGR8888;
  2305. else
  2306. return DRM_FORMAT_XBGR8888;
  2307. } else {
  2308. if (alpha)
  2309. return DRM_FORMAT_ARGB8888;
  2310. else
  2311. return DRM_FORMAT_XRGB8888;
  2312. }
  2313. case PLANE_CTL_FORMAT_XRGB_2101010:
  2314. if (rgb_order)
  2315. return DRM_FORMAT_XBGR2101010;
  2316. else
  2317. return DRM_FORMAT_XRGB2101010;
  2318. }
  2319. }
  2320. static bool
  2321. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2322. struct intel_initial_plane_config *plane_config)
  2323. {
  2324. struct drm_device *dev = crtc->base.dev;
  2325. struct drm_i915_private *dev_priv = to_i915(dev);
  2326. struct drm_i915_gem_object *obj = NULL;
  2327. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2328. struct drm_framebuffer *fb = &plane_config->fb->base;
  2329. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2330. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2331. PAGE_SIZE);
  2332. size_aligned -= base_aligned;
  2333. if (plane_config->size == 0)
  2334. return false;
  2335. /* If the FB is too big, just don't use it since fbdev is not very
  2336. * important and we should probably use that space with FBC or other
  2337. * features. */
  2338. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2339. return false;
  2340. mutex_lock(&dev->struct_mutex);
  2341. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2342. base_aligned,
  2343. base_aligned,
  2344. size_aligned);
  2345. mutex_unlock(&dev->struct_mutex);
  2346. if (!obj)
  2347. return false;
  2348. if (plane_config->tiling == I915_TILING_X)
  2349. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2350. mode_cmd.pixel_format = fb->format->format;
  2351. mode_cmd.width = fb->width;
  2352. mode_cmd.height = fb->height;
  2353. mode_cmd.pitches[0] = fb->pitches[0];
  2354. mode_cmd.modifier[0] = fb->modifier;
  2355. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2356. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2357. DRM_DEBUG_KMS("intel fb init failed\n");
  2358. goto out_unref_obj;
  2359. }
  2360. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2361. return true;
  2362. out_unref_obj:
  2363. i915_gem_object_put(obj);
  2364. return false;
  2365. }
  2366. static void
  2367. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2368. struct intel_plane_state *plane_state,
  2369. bool visible)
  2370. {
  2371. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2372. plane_state->base.visible = visible;
  2373. /* FIXME pre-g4x don't work like this */
  2374. if (visible) {
  2375. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2376. crtc_state->active_planes |= BIT(plane->id);
  2377. } else {
  2378. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2379. crtc_state->active_planes &= ~BIT(plane->id);
  2380. }
  2381. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2382. crtc_state->base.crtc->name,
  2383. crtc_state->active_planes);
  2384. }
  2385. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2386. struct intel_plane *plane)
  2387. {
  2388. struct intel_crtc_state *crtc_state =
  2389. to_intel_crtc_state(crtc->base.state);
  2390. struct intel_plane_state *plane_state =
  2391. to_intel_plane_state(plane->base.state);
  2392. intel_set_plane_visible(crtc_state, plane_state, false);
  2393. if (plane->id == PLANE_PRIMARY)
  2394. intel_pre_disable_primary_noatomic(&crtc->base);
  2395. trace_intel_disable_plane(&plane->base, crtc);
  2396. plane->disable_plane(plane, crtc);
  2397. }
  2398. static void
  2399. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2400. struct intel_initial_plane_config *plane_config)
  2401. {
  2402. struct drm_device *dev = intel_crtc->base.dev;
  2403. struct drm_i915_private *dev_priv = to_i915(dev);
  2404. struct drm_crtc *c;
  2405. struct drm_i915_gem_object *obj;
  2406. struct drm_plane *primary = intel_crtc->base.primary;
  2407. struct drm_plane_state *plane_state = primary->state;
  2408. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2409. struct intel_plane *intel_plane = to_intel_plane(primary);
  2410. struct intel_plane_state *intel_state =
  2411. to_intel_plane_state(plane_state);
  2412. struct drm_framebuffer *fb;
  2413. if (!plane_config->fb)
  2414. return;
  2415. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2416. fb = &plane_config->fb->base;
  2417. goto valid_fb;
  2418. }
  2419. kfree(plane_config->fb);
  2420. /*
  2421. * Failed to alloc the obj, check to see if we should share
  2422. * an fb with another CRTC instead
  2423. */
  2424. for_each_crtc(dev, c) {
  2425. struct intel_plane_state *state;
  2426. if (c == &intel_crtc->base)
  2427. continue;
  2428. if (!to_intel_crtc(c)->active)
  2429. continue;
  2430. state = to_intel_plane_state(c->primary->state);
  2431. if (!state->vma)
  2432. continue;
  2433. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2434. fb = c->primary->fb;
  2435. drm_framebuffer_get(fb);
  2436. goto valid_fb;
  2437. }
  2438. }
  2439. /*
  2440. * We've failed to reconstruct the BIOS FB. Current display state
  2441. * indicates that the primary plane is visible, but has a NULL FB,
  2442. * which will lead to problems later if we don't fix it up. The
  2443. * simplest solution is to just disable the primary plane now and
  2444. * pretend the BIOS never had it enabled.
  2445. */
  2446. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2447. return;
  2448. valid_fb:
  2449. mutex_lock(&dev->struct_mutex);
  2450. intel_state->vma =
  2451. intel_pin_and_fence_fb_obj(fb,
  2452. primary->state->rotation,
  2453. intel_plane_uses_fence(intel_state),
  2454. &intel_state->flags);
  2455. mutex_unlock(&dev->struct_mutex);
  2456. if (IS_ERR(intel_state->vma)) {
  2457. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2458. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2459. intel_state->vma = NULL;
  2460. drm_framebuffer_put(fb);
  2461. return;
  2462. }
  2463. obj = intel_fb_obj(fb);
  2464. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  2465. plane_state->src_x = 0;
  2466. plane_state->src_y = 0;
  2467. plane_state->src_w = fb->width << 16;
  2468. plane_state->src_h = fb->height << 16;
  2469. plane_state->crtc_x = 0;
  2470. plane_state->crtc_y = 0;
  2471. plane_state->crtc_w = fb->width;
  2472. plane_state->crtc_h = fb->height;
  2473. intel_state->base.src = drm_plane_state_src(plane_state);
  2474. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2475. if (i915_gem_object_is_tiled(obj))
  2476. dev_priv->preserve_bios_swizzle = true;
  2477. drm_framebuffer_get(fb);
  2478. primary->fb = primary->state->fb = fb;
  2479. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2480. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2481. to_intel_plane_state(plane_state),
  2482. true);
  2483. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2484. &obj->frontbuffer_bits);
  2485. }
  2486. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2487. unsigned int rotation)
  2488. {
  2489. int cpp = fb->format->cpp[plane];
  2490. switch (fb->modifier) {
  2491. case DRM_FORMAT_MOD_LINEAR:
  2492. case I915_FORMAT_MOD_X_TILED:
  2493. switch (cpp) {
  2494. case 8:
  2495. return 4096;
  2496. case 4:
  2497. case 2:
  2498. case 1:
  2499. return 8192;
  2500. default:
  2501. MISSING_CASE(cpp);
  2502. break;
  2503. }
  2504. break;
  2505. case I915_FORMAT_MOD_Y_TILED_CCS:
  2506. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2507. /* FIXME AUX plane? */
  2508. case I915_FORMAT_MOD_Y_TILED:
  2509. case I915_FORMAT_MOD_Yf_TILED:
  2510. switch (cpp) {
  2511. case 8:
  2512. return 2048;
  2513. case 4:
  2514. return 4096;
  2515. case 2:
  2516. case 1:
  2517. return 8192;
  2518. default:
  2519. MISSING_CASE(cpp);
  2520. break;
  2521. }
  2522. break;
  2523. default:
  2524. MISSING_CASE(fb->modifier);
  2525. }
  2526. return 2048;
  2527. }
  2528. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2529. int main_x, int main_y, u32 main_offset)
  2530. {
  2531. const struct drm_framebuffer *fb = plane_state->base.fb;
  2532. int hsub = fb->format->hsub;
  2533. int vsub = fb->format->vsub;
  2534. int aux_x = plane_state->aux.x;
  2535. int aux_y = plane_state->aux.y;
  2536. u32 aux_offset = plane_state->aux.offset;
  2537. u32 alignment = intel_surf_alignment(fb, 1);
  2538. while (aux_offset >= main_offset && aux_y <= main_y) {
  2539. int x, y;
  2540. if (aux_x == main_x && aux_y == main_y)
  2541. break;
  2542. if (aux_offset == 0)
  2543. break;
  2544. x = aux_x / hsub;
  2545. y = aux_y / vsub;
  2546. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2547. aux_offset, aux_offset - alignment);
  2548. aux_x = x * hsub + aux_x % hsub;
  2549. aux_y = y * vsub + aux_y % vsub;
  2550. }
  2551. if (aux_x != main_x || aux_y != main_y)
  2552. return false;
  2553. plane_state->aux.offset = aux_offset;
  2554. plane_state->aux.x = aux_x;
  2555. plane_state->aux.y = aux_y;
  2556. return true;
  2557. }
  2558. static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
  2559. struct intel_plane_state *plane_state)
  2560. {
  2561. struct drm_i915_private *dev_priv =
  2562. to_i915(plane_state->base.plane->dev);
  2563. const struct drm_framebuffer *fb = plane_state->base.fb;
  2564. unsigned int rotation = plane_state->base.rotation;
  2565. int x = plane_state->base.src.x1 >> 16;
  2566. int y = plane_state->base.src.y1 >> 16;
  2567. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2568. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2569. int dst_x = plane_state->base.dst.x1;
  2570. int pipe_src_w = crtc_state->pipe_src_w;
  2571. int max_width = skl_max_plane_width(fb, 0, rotation);
  2572. int max_height = 4096;
  2573. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2574. if (w > max_width || h > max_height) {
  2575. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2576. w, h, max_width, max_height);
  2577. return -EINVAL;
  2578. }
  2579. /*
  2580. * Display WA #1175: cnl,glk
  2581. * Planes other than the cursor may cause FIFO underflow and display
  2582. * corruption if starting less than 4 pixels from the right edge of
  2583. * the screen.
  2584. * Besides the above WA fix the similar problem, where planes other
  2585. * than the cursor ending less than 4 pixels from the left edge of the
  2586. * screen may cause FIFO underflow and display corruption.
  2587. */
  2588. if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  2589. (dst_x + w < 4 || dst_x > pipe_src_w - 4)) {
  2590. DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
  2591. dst_x + w < 4 ? "end" : "start",
  2592. dst_x + w < 4 ? dst_x + w : dst_x,
  2593. 4, pipe_src_w - 4);
  2594. return -ERANGE;
  2595. }
  2596. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2597. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2598. alignment = intel_surf_alignment(fb, 0);
  2599. /*
  2600. * AUX surface offset is specified as the distance from the
  2601. * main surface offset, and it must be non-negative. Make
  2602. * sure that is what we will get.
  2603. */
  2604. if (offset > aux_offset)
  2605. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2606. offset, aux_offset & ~(alignment - 1));
  2607. /*
  2608. * When using an X-tiled surface, the plane blows up
  2609. * if the x offset + width exceed the stride.
  2610. *
  2611. * TODO: linear and Y-tiled seem fine, Yf untested,
  2612. */
  2613. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2614. int cpp = fb->format->cpp[0];
  2615. while ((x + w) * cpp > fb->pitches[0]) {
  2616. if (offset == 0) {
  2617. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2618. return -EINVAL;
  2619. }
  2620. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2621. offset, offset - alignment);
  2622. }
  2623. }
  2624. /*
  2625. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2626. * they match with the main surface x/y offsets.
  2627. */
  2628. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2629. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2630. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2631. if (offset == 0)
  2632. break;
  2633. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2634. offset, offset - alignment);
  2635. }
  2636. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2637. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2638. return -EINVAL;
  2639. }
  2640. }
  2641. plane_state->main.offset = offset;
  2642. plane_state->main.x = x;
  2643. plane_state->main.y = y;
  2644. return 0;
  2645. }
  2646. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2647. {
  2648. const struct drm_framebuffer *fb = plane_state->base.fb;
  2649. unsigned int rotation = plane_state->base.rotation;
  2650. int max_width = skl_max_plane_width(fb, 1, rotation);
  2651. int max_height = 4096;
  2652. int x = plane_state->base.src.x1 >> 17;
  2653. int y = plane_state->base.src.y1 >> 17;
  2654. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2655. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2656. u32 offset;
  2657. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2658. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2659. /* FIXME not quite sure how/if these apply to the chroma plane */
  2660. if (w > max_width || h > max_height) {
  2661. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2662. w, h, max_width, max_height);
  2663. return -EINVAL;
  2664. }
  2665. plane_state->aux.offset = offset;
  2666. plane_state->aux.x = x;
  2667. plane_state->aux.y = y;
  2668. return 0;
  2669. }
  2670. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2671. {
  2672. const struct drm_framebuffer *fb = plane_state->base.fb;
  2673. int src_x = plane_state->base.src.x1 >> 16;
  2674. int src_y = plane_state->base.src.y1 >> 16;
  2675. int hsub = fb->format->hsub;
  2676. int vsub = fb->format->vsub;
  2677. int x = src_x / hsub;
  2678. int y = src_y / vsub;
  2679. u32 offset;
  2680. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2681. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2682. plane_state->base.rotation);
  2683. return -EINVAL;
  2684. }
  2685. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2686. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2687. plane_state->aux.offset = offset;
  2688. plane_state->aux.x = x * hsub + src_x % hsub;
  2689. plane_state->aux.y = y * vsub + src_y % vsub;
  2690. return 0;
  2691. }
  2692. int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
  2693. struct intel_plane_state *plane_state)
  2694. {
  2695. const struct drm_framebuffer *fb = plane_state->base.fb;
  2696. unsigned int rotation = plane_state->base.rotation;
  2697. int ret;
  2698. if (rotation & DRM_MODE_REFLECT_X &&
  2699. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2700. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2701. return -EINVAL;
  2702. }
  2703. if (!plane_state->base.visible)
  2704. return 0;
  2705. /* Rotate src coordinates to match rotated GTT view */
  2706. if (drm_rotation_90_or_270(rotation))
  2707. drm_rect_rotate(&plane_state->base.src,
  2708. fb->width << 16, fb->height << 16,
  2709. DRM_MODE_ROTATE_270);
  2710. /*
  2711. * Handle the AUX surface first since
  2712. * the main surface setup depends on it.
  2713. */
  2714. if (fb->format->format == DRM_FORMAT_NV12) {
  2715. ret = skl_check_nv12_aux_surface(plane_state);
  2716. if (ret)
  2717. return ret;
  2718. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2719. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2720. ret = skl_check_ccs_aux_surface(plane_state);
  2721. if (ret)
  2722. return ret;
  2723. } else {
  2724. plane_state->aux.offset = ~0xfff;
  2725. plane_state->aux.x = 0;
  2726. plane_state->aux.y = 0;
  2727. }
  2728. ret = skl_check_main_surface(crtc_state, plane_state);
  2729. if (ret)
  2730. return ret;
  2731. return 0;
  2732. }
  2733. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2734. const struct intel_plane_state *plane_state)
  2735. {
  2736. struct drm_i915_private *dev_priv =
  2737. to_i915(plane_state->base.plane->dev);
  2738. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2739. const struct drm_framebuffer *fb = plane_state->base.fb;
  2740. unsigned int rotation = plane_state->base.rotation;
  2741. u32 dspcntr;
  2742. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2743. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2744. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2745. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2746. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2747. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2748. if (INTEL_GEN(dev_priv) < 5)
  2749. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2750. switch (fb->format->format) {
  2751. case DRM_FORMAT_C8:
  2752. dspcntr |= DISPPLANE_8BPP;
  2753. break;
  2754. case DRM_FORMAT_XRGB1555:
  2755. dspcntr |= DISPPLANE_BGRX555;
  2756. break;
  2757. case DRM_FORMAT_RGB565:
  2758. dspcntr |= DISPPLANE_BGRX565;
  2759. break;
  2760. case DRM_FORMAT_XRGB8888:
  2761. dspcntr |= DISPPLANE_BGRX888;
  2762. break;
  2763. case DRM_FORMAT_XBGR8888:
  2764. dspcntr |= DISPPLANE_RGBX888;
  2765. break;
  2766. case DRM_FORMAT_XRGB2101010:
  2767. dspcntr |= DISPPLANE_BGRX101010;
  2768. break;
  2769. case DRM_FORMAT_XBGR2101010:
  2770. dspcntr |= DISPPLANE_RGBX101010;
  2771. break;
  2772. default:
  2773. MISSING_CASE(fb->format->format);
  2774. return 0;
  2775. }
  2776. if (INTEL_GEN(dev_priv) >= 4 &&
  2777. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2778. dspcntr |= DISPPLANE_TILED;
  2779. if (rotation & DRM_MODE_ROTATE_180)
  2780. dspcntr |= DISPPLANE_ROTATE_180;
  2781. if (rotation & DRM_MODE_REFLECT_X)
  2782. dspcntr |= DISPPLANE_MIRROR;
  2783. return dspcntr;
  2784. }
  2785. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2786. {
  2787. struct drm_i915_private *dev_priv =
  2788. to_i915(plane_state->base.plane->dev);
  2789. int src_x = plane_state->base.src.x1 >> 16;
  2790. int src_y = plane_state->base.src.y1 >> 16;
  2791. u32 offset;
  2792. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2793. if (INTEL_GEN(dev_priv) >= 4)
  2794. offset = intel_compute_tile_offset(&src_x, &src_y,
  2795. plane_state, 0);
  2796. else
  2797. offset = 0;
  2798. /* HSW/BDW do this automagically in hardware */
  2799. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2800. unsigned int rotation = plane_state->base.rotation;
  2801. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2802. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2803. if (rotation & DRM_MODE_ROTATE_180) {
  2804. src_x += src_w - 1;
  2805. src_y += src_h - 1;
  2806. } else if (rotation & DRM_MODE_REFLECT_X) {
  2807. src_x += src_w - 1;
  2808. }
  2809. }
  2810. plane_state->main.offset = offset;
  2811. plane_state->main.x = src_x;
  2812. plane_state->main.y = src_y;
  2813. return 0;
  2814. }
  2815. static void i9xx_update_plane(struct intel_plane *plane,
  2816. const struct intel_crtc_state *crtc_state,
  2817. const struct intel_plane_state *plane_state)
  2818. {
  2819. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2820. const struct drm_framebuffer *fb = plane_state->base.fb;
  2821. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2822. u32 linear_offset;
  2823. u32 dspcntr = plane_state->ctl;
  2824. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2825. int x = plane_state->main.x;
  2826. int y = plane_state->main.y;
  2827. unsigned long irqflags;
  2828. u32 dspaddr_offset;
  2829. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2830. if (INTEL_GEN(dev_priv) >= 4)
  2831. dspaddr_offset = plane_state->main.offset;
  2832. else
  2833. dspaddr_offset = linear_offset;
  2834. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2835. if (INTEL_GEN(dev_priv) < 4) {
  2836. /* pipesrc and dspsize control the size that is scaled from,
  2837. * which should always be the user's requested size.
  2838. */
  2839. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2840. ((crtc_state->pipe_src_h - 1) << 16) |
  2841. (crtc_state->pipe_src_w - 1));
  2842. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2843. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2844. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2845. ((crtc_state->pipe_src_h - 1) << 16) |
  2846. (crtc_state->pipe_src_w - 1));
  2847. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2848. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2849. }
  2850. I915_WRITE_FW(reg, dspcntr);
  2851. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2852. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2853. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2854. intel_plane_ggtt_offset(plane_state) +
  2855. dspaddr_offset);
  2856. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2857. } else if (INTEL_GEN(dev_priv) >= 4) {
  2858. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2859. intel_plane_ggtt_offset(plane_state) +
  2860. dspaddr_offset);
  2861. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2862. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2863. } else {
  2864. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2865. intel_plane_ggtt_offset(plane_state) +
  2866. dspaddr_offset);
  2867. }
  2868. POSTING_READ_FW(reg);
  2869. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2870. }
  2871. static void i9xx_disable_plane(struct intel_plane *plane,
  2872. struct intel_crtc *crtc)
  2873. {
  2874. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2875. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2876. unsigned long irqflags;
  2877. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2878. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2879. if (INTEL_GEN(dev_priv) >= 4)
  2880. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2881. else
  2882. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2883. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2884. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2885. }
  2886. static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
  2887. {
  2888. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2889. enum intel_display_power_domain power_domain;
  2890. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2891. enum pipe pipe = plane->pipe;
  2892. bool ret;
  2893. /*
  2894. * Not 100% correct for planes that can move between pipes,
  2895. * but that's only the case for gen2-4 which don't have any
  2896. * display power wells.
  2897. */
  2898. power_domain = POWER_DOMAIN_PIPE(pipe);
  2899. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2900. return false;
  2901. ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
  2902. intel_display_power_put(dev_priv, power_domain);
  2903. return ret;
  2904. }
  2905. static u32
  2906. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2907. {
  2908. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2909. return 64;
  2910. else
  2911. return intel_tile_width_bytes(fb, plane);
  2912. }
  2913. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2914. {
  2915. struct drm_device *dev = intel_crtc->base.dev;
  2916. struct drm_i915_private *dev_priv = to_i915(dev);
  2917. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2918. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2919. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2920. }
  2921. /*
  2922. * This function detaches (aka. unbinds) unused scalers in hardware
  2923. */
  2924. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2925. {
  2926. struct intel_crtc_scaler_state *scaler_state;
  2927. int i;
  2928. scaler_state = &intel_crtc->config->scaler_state;
  2929. /* loop through and disable scalers that aren't in use */
  2930. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2931. if (!scaler_state->scalers[i].in_use)
  2932. skl_detach_scaler(intel_crtc, i);
  2933. }
  2934. }
  2935. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2936. unsigned int rotation)
  2937. {
  2938. u32 stride;
  2939. if (plane >= fb->format->num_planes)
  2940. return 0;
  2941. stride = intel_fb_pitch(fb, plane, rotation);
  2942. /*
  2943. * The stride is either expressed as a multiple of 64 bytes chunks for
  2944. * linear buffers or in number of tiles for tiled buffers.
  2945. */
  2946. if (drm_rotation_90_or_270(rotation))
  2947. stride /= intel_tile_height(fb, plane);
  2948. else
  2949. stride /= intel_fb_stride_alignment(fb, plane);
  2950. return stride;
  2951. }
  2952. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2953. {
  2954. switch (pixel_format) {
  2955. case DRM_FORMAT_C8:
  2956. return PLANE_CTL_FORMAT_INDEXED;
  2957. case DRM_FORMAT_RGB565:
  2958. return PLANE_CTL_FORMAT_RGB_565;
  2959. case DRM_FORMAT_XBGR8888:
  2960. case DRM_FORMAT_ABGR8888:
  2961. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2962. case DRM_FORMAT_XRGB8888:
  2963. case DRM_FORMAT_ARGB8888:
  2964. return PLANE_CTL_FORMAT_XRGB_8888;
  2965. case DRM_FORMAT_XRGB2101010:
  2966. return PLANE_CTL_FORMAT_XRGB_2101010;
  2967. case DRM_FORMAT_XBGR2101010:
  2968. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2969. case DRM_FORMAT_YUYV:
  2970. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2971. case DRM_FORMAT_YVYU:
  2972. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2973. case DRM_FORMAT_UYVY:
  2974. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2975. case DRM_FORMAT_VYUY:
  2976. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2977. case DRM_FORMAT_NV12:
  2978. return PLANE_CTL_FORMAT_NV12;
  2979. default:
  2980. MISSING_CASE(pixel_format);
  2981. }
  2982. return 0;
  2983. }
  2984. /*
  2985. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2986. * to be already pre-multiplied. We need to add a knob (or a different
  2987. * DRM_FORMAT) for user-space to configure that.
  2988. */
  2989. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  2990. {
  2991. switch (pixel_format) {
  2992. case DRM_FORMAT_ABGR8888:
  2993. case DRM_FORMAT_ARGB8888:
  2994. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2995. default:
  2996. return PLANE_CTL_ALPHA_DISABLE;
  2997. }
  2998. }
  2999. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  3000. {
  3001. switch (pixel_format) {
  3002. case DRM_FORMAT_ABGR8888:
  3003. case DRM_FORMAT_ARGB8888:
  3004. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  3005. default:
  3006. return PLANE_COLOR_ALPHA_DISABLE;
  3007. }
  3008. }
  3009. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  3010. {
  3011. switch (fb_modifier) {
  3012. case DRM_FORMAT_MOD_LINEAR:
  3013. break;
  3014. case I915_FORMAT_MOD_X_TILED:
  3015. return PLANE_CTL_TILED_X;
  3016. case I915_FORMAT_MOD_Y_TILED:
  3017. return PLANE_CTL_TILED_Y;
  3018. case I915_FORMAT_MOD_Y_TILED_CCS:
  3019. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  3020. case I915_FORMAT_MOD_Yf_TILED:
  3021. return PLANE_CTL_TILED_YF;
  3022. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3023. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  3024. default:
  3025. MISSING_CASE(fb_modifier);
  3026. }
  3027. return 0;
  3028. }
  3029. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  3030. {
  3031. switch (rotate) {
  3032. case DRM_MODE_ROTATE_0:
  3033. break;
  3034. /*
  3035. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3036. * while i915 HW rotation is clockwise, thats why this swapping.
  3037. */
  3038. case DRM_MODE_ROTATE_90:
  3039. return PLANE_CTL_ROTATE_270;
  3040. case DRM_MODE_ROTATE_180:
  3041. return PLANE_CTL_ROTATE_180;
  3042. case DRM_MODE_ROTATE_270:
  3043. return PLANE_CTL_ROTATE_90;
  3044. default:
  3045. MISSING_CASE(rotate);
  3046. }
  3047. return 0;
  3048. }
  3049. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3050. {
  3051. switch (reflect) {
  3052. case 0:
  3053. break;
  3054. case DRM_MODE_REFLECT_X:
  3055. return PLANE_CTL_FLIP_HORIZONTAL;
  3056. case DRM_MODE_REFLECT_Y:
  3057. default:
  3058. MISSING_CASE(reflect);
  3059. }
  3060. return 0;
  3061. }
  3062. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3063. const struct intel_plane_state *plane_state)
  3064. {
  3065. struct drm_i915_private *dev_priv =
  3066. to_i915(plane_state->base.plane->dev);
  3067. const struct drm_framebuffer *fb = plane_state->base.fb;
  3068. unsigned int rotation = plane_state->base.rotation;
  3069. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3070. u32 plane_ctl;
  3071. plane_ctl = PLANE_CTL_ENABLE;
  3072. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3073. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3074. plane_ctl |=
  3075. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3076. PLANE_CTL_PIPE_CSC_ENABLE |
  3077. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3078. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3079. plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
  3080. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3081. plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
  3082. }
  3083. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3084. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3085. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3086. if (INTEL_GEN(dev_priv) >= 10)
  3087. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3088. DRM_MODE_REFLECT_MASK);
  3089. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3090. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3091. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3092. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3093. return plane_ctl;
  3094. }
  3095. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3096. const struct intel_plane_state *plane_state)
  3097. {
  3098. struct drm_i915_private *dev_priv =
  3099. to_i915(plane_state->base.plane->dev);
  3100. const struct drm_framebuffer *fb = plane_state->base.fb;
  3101. u32 plane_color_ctl = 0;
  3102. if (INTEL_GEN(dev_priv) < 11) {
  3103. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3104. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3105. }
  3106. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3107. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3108. if (intel_format_is_yuv(fb->format->format)) {
  3109. if (fb->format->format == DRM_FORMAT_NV12) {
  3110. plane_color_ctl |=
  3111. PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3112. goto out;
  3113. }
  3114. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3115. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3116. else
  3117. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
  3118. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3119. plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
  3120. }
  3121. out:
  3122. return plane_color_ctl;
  3123. }
  3124. static int
  3125. __intel_display_resume(struct drm_device *dev,
  3126. struct drm_atomic_state *state,
  3127. struct drm_modeset_acquire_ctx *ctx)
  3128. {
  3129. struct drm_crtc_state *crtc_state;
  3130. struct drm_crtc *crtc;
  3131. int i, ret;
  3132. intel_modeset_setup_hw_state(dev, ctx);
  3133. i915_redisable_vga(to_i915(dev));
  3134. if (!state)
  3135. return 0;
  3136. /*
  3137. * We've duplicated the state, pointers to the old state are invalid.
  3138. *
  3139. * Don't attempt to use the old state until we commit the duplicated state.
  3140. */
  3141. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3142. /*
  3143. * Force recalculation even if we restore
  3144. * current state. With fast modeset this may not result
  3145. * in a modeset when the state is compatible.
  3146. */
  3147. crtc_state->mode_changed = true;
  3148. }
  3149. /* ignore any reset values/BIOS leftovers in the WM registers */
  3150. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3151. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3152. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3153. WARN_ON(ret == -EDEADLK);
  3154. return ret;
  3155. }
  3156. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3157. {
  3158. return intel_has_gpu_reset(dev_priv) &&
  3159. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3160. }
  3161. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3162. {
  3163. struct drm_device *dev = &dev_priv->drm;
  3164. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3165. struct drm_atomic_state *state;
  3166. int ret;
  3167. /* reset doesn't touch the display */
  3168. if (!i915_modparams.force_reset_modeset_test &&
  3169. !gpu_reset_clobbers_display(dev_priv))
  3170. return;
  3171. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3172. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3173. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3174. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3175. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3176. i915_gem_set_wedged(dev_priv);
  3177. }
  3178. /*
  3179. * Need mode_config.mutex so that we don't
  3180. * trample ongoing ->detect() and whatnot.
  3181. */
  3182. mutex_lock(&dev->mode_config.mutex);
  3183. drm_modeset_acquire_init(ctx, 0);
  3184. while (1) {
  3185. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3186. if (ret != -EDEADLK)
  3187. break;
  3188. drm_modeset_backoff(ctx);
  3189. }
  3190. /*
  3191. * Disabling the crtcs gracefully seems nicer. Also the
  3192. * g33 docs say we should at least disable all the planes.
  3193. */
  3194. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3195. if (IS_ERR(state)) {
  3196. ret = PTR_ERR(state);
  3197. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3198. return;
  3199. }
  3200. ret = drm_atomic_helper_disable_all(dev, ctx);
  3201. if (ret) {
  3202. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3203. drm_atomic_state_put(state);
  3204. return;
  3205. }
  3206. dev_priv->modeset_restore_state = state;
  3207. state->acquire_ctx = ctx;
  3208. }
  3209. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3210. {
  3211. struct drm_device *dev = &dev_priv->drm;
  3212. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3213. struct drm_atomic_state *state;
  3214. int ret;
  3215. /* reset doesn't touch the display */
  3216. if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  3217. return;
  3218. state = fetch_and_zero(&dev_priv->modeset_restore_state);
  3219. if (!state)
  3220. goto unlock;
  3221. /* reset doesn't touch the display */
  3222. if (!gpu_reset_clobbers_display(dev_priv)) {
  3223. /* for testing only restore the display */
  3224. ret = __intel_display_resume(dev, state, ctx);
  3225. if (ret)
  3226. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3227. } else {
  3228. /*
  3229. * The display has been reset as well,
  3230. * so need a full re-initialization.
  3231. */
  3232. intel_runtime_pm_disable_interrupts(dev_priv);
  3233. intel_runtime_pm_enable_interrupts(dev_priv);
  3234. intel_pps_unlock_regs_wa(dev_priv);
  3235. intel_modeset_init_hw(dev);
  3236. intel_init_clock_gating(dev_priv);
  3237. spin_lock_irq(&dev_priv->irq_lock);
  3238. if (dev_priv->display.hpd_irq_setup)
  3239. dev_priv->display.hpd_irq_setup(dev_priv);
  3240. spin_unlock_irq(&dev_priv->irq_lock);
  3241. ret = __intel_display_resume(dev, state, ctx);
  3242. if (ret)
  3243. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3244. intel_hpd_init(dev_priv);
  3245. }
  3246. drm_atomic_state_put(state);
  3247. unlock:
  3248. drm_modeset_drop_locks(ctx);
  3249. drm_modeset_acquire_fini(ctx);
  3250. mutex_unlock(&dev->mode_config.mutex);
  3251. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3252. }
  3253. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3254. const struct intel_crtc_state *new_crtc_state)
  3255. {
  3256. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3257. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3258. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3259. crtc->base.mode = new_crtc_state->base.mode;
  3260. /*
  3261. * Update pipe size and adjust fitter if needed: the reason for this is
  3262. * that in compute_mode_changes we check the native mode (not the pfit
  3263. * mode) to see if we can flip rather than do a full mode set. In the
  3264. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3265. * pfit state, we'll end up with a big fb scanned out into the wrong
  3266. * sized surface.
  3267. */
  3268. I915_WRITE(PIPESRC(crtc->pipe),
  3269. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3270. (new_crtc_state->pipe_src_h - 1));
  3271. /* on skylake this is done by detaching scalers */
  3272. if (INTEL_GEN(dev_priv) >= 9) {
  3273. skl_detach_scalers(crtc);
  3274. if (new_crtc_state->pch_pfit.enabled)
  3275. skylake_pfit_enable(crtc);
  3276. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3277. if (new_crtc_state->pch_pfit.enabled)
  3278. ironlake_pfit_enable(crtc);
  3279. else if (old_crtc_state->pch_pfit.enabled)
  3280. ironlake_pfit_disable(crtc, true);
  3281. }
  3282. }
  3283. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3284. {
  3285. struct drm_device *dev = crtc->base.dev;
  3286. struct drm_i915_private *dev_priv = to_i915(dev);
  3287. int pipe = crtc->pipe;
  3288. i915_reg_t reg;
  3289. u32 temp;
  3290. /* enable normal train */
  3291. reg = FDI_TX_CTL(pipe);
  3292. temp = I915_READ(reg);
  3293. if (IS_IVYBRIDGE(dev_priv)) {
  3294. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3295. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3296. } else {
  3297. temp &= ~FDI_LINK_TRAIN_NONE;
  3298. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3299. }
  3300. I915_WRITE(reg, temp);
  3301. reg = FDI_RX_CTL(pipe);
  3302. temp = I915_READ(reg);
  3303. if (HAS_PCH_CPT(dev_priv)) {
  3304. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3305. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3306. } else {
  3307. temp &= ~FDI_LINK_TRAIN_NONE;
  3308. temp |= FDI_LINK_TRAIN_NONE;
  3309. }
  3310. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3311. /* wait one idle pattern time */
  3312. POSTING_READ(reg);
  3313. udelay(1000);
  3314. /* IVB wants error correction enabled */
  3315. if (IS_IVYBRIDGE(dev_priv))
  3316. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3317. FDI_FE_ERRC_ENABLE);
  3318. }
  3319. /* The FDI link training functions for ILK/Ibexpeak. */
  3320. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3321. const struct intel_crtc_state *crtc_state)
  3322. {
  3323. struct drm_device *dev = crtc->base.dev;
  3324. struct drm_i915_private *dev_priv = to_i915(dev);
  3325. int pipe = crtc->pipe;
  3326. i915_reg_t reg;
  3327. u32 temp, tries;
  3328. /* FDI needs bits from pipe first */
  3329. assert_pipe_enabled(dev_priv, pipe);
  3330. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3331. for train result */
  3332. reg = FDI_RX_IMR(pipe);
  3333. temp = I915_READ(reg);
  3334. temp &= ~FDI_RX_SYMBOL_LOCK;
  3335. temp &= ~FDI_RX_BIT_LOCK;
  3336. I915_WRITE(reg, temp);
  3337. I915_READ(reg);
  3338. udelay(150);
  3339. /* enable CPU FDI TX and PCH FDI RX */
  3340. reg = FDI_TX_CTL(pipe);
  3341. temp = I915_READ(reg);
  3342. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3343. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3344. temp &= ~FDI_LINK_TRAIN_NONE;
  3345. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3346. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3347. reg = FDI_RX_CTL(pipe);
  3348. temp = I915_READ(reg);
  3349. temp &= ~FDI_LINK_TRAIN_NONE;
  3350. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3351. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3352. POSTING_READ(reg);
  3353. udelay(150);
  3354. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3355. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3356. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3357. FDI_RX_PHASE_SYNC_POINTER_EN);
  3358. reg = FDI_RX_IIR(pipe);
  3359. for (tries = 0; tries < 5; tries++) {
  3360. temp = I915_READ(reg);
  3361. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3362. if ((temp & FDI_RX_BIT_LOCK)) {
  3363. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3364. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3365. break;
  3366. }
  3367. }
  3368. if (tries == 5)
  3369. DRM_ERROR("FDI train 1 fail!\n");
  3370. /* Train 2 */
  3371. reg = FDI_TX_CTL(pipe);
  3372. temp = I915_READ(reg);
  3373. temp &= ~FDI_LINK_TRAIN_NONE;
  3374. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3375. I915_WRITE(reg, temp);
  3376. reg = FDI_RX_CTL(pipe);
  3377. temp = I915_READ(reg);
  3378. temp &= ~FDI_LINK_TRAIN_NONE;
  3379. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3380. I915_WRITE(reg, temp);
  3381. POSTING_READ(reg);
  3382. udelay(150);
  3383. reg = FDI_RX_IIR(pipe);
  3384. for (tries = 0; tries < 5; tries++) {
  3385. temp = I915_READ(reg);
  3386. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3387. if (temp & FDI_RX_SYMBOL_LOCK) {
  3388. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3389. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3390. break;
  3391. }
  3392. }
  3393. if (tries == 5)
  3394. DRM_ERROR("FDI train 2 fail!\n");
  3395. DRM_DEBUG_KMS("FDI train done\n");
  3396. }
  3397. static const int snb_b_fdi_train_param[] = {
  3398. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3399. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3400. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3401. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3402. };
  3403. /* The FDI link training functions for SNB/Cougarpoint. */
  3404. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3405. const struct intel_crtc_state *crtc_state)
  3406. {
  3407. struct drm_device *dev = crtc->base.dev;
  3408. struct drm_i915_private *dev_priv = to_i915(dev);
  3409. int pipe = crtc->pipe;
  3410. i915_reg_t reg;
  3411. u32 temp, i, retry;
  3412. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3413. for train result */
  3414. reg = FDI_RX_IMR(pipe);
  3415. temp = I915_READ(reg);
  3416. temp &= ~FDI_RX_SYMBOL_LOCK;
  3417. temp &= ~FDI_RX_BIT_LOCK;
  3418. I915_WRITE(reg, temp);
  3419. POSTING_READ(reg);
  3420. udelay(150);
  3421. /* enable CPU FDI TX and PCH FDI RX */
  3422. reg = FDI_TX_CTL(pipe);
  3423. temp = I915_READ(reg);
  3424. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3425. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3426. temp &= ~FDI_LINK_TRAIN_NONE;
  3427. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3428. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3429. /* SNB-B */
  3430. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3431. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3432. I915_WRITE(FDI_RX_MISC(pipe),
  3433. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3434. reg = FDI_RX_CTL(pipe);
  3435. temp = I915_READ(reg);
  3436. if (HAS_PCH_CPT(dev_priv)) {
  3437. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3438. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3439. } else {
  3440. temp &= ~FDI_LINK_TRAIN_NONE;
  3441. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3442. }
  3443. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3444. POSTING_READ(reg);
  3445. udelay(150);
  3446. for (i = 0; i < 4; i++) {
  3447. reg = FDI_TX_CTL(pipe);
  3448. temp = I915_READ(reg);
  3449. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3450. temp |= snb_b_fdi_train_param[i];
  3451. I915_WRITE(reg, temp);
  3452. POSTING_READ(reg);
  3453. udelay(500);
  3454. for (retry = 0; retry < 5; retry++) {
  3455. reg = FDI_RX_IIR(pipe);
  3456. temp = I915_READ(reg);
  3457. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3458. if (temp & FDI_RX_BIT_LOCK) {
  3459. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3460. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3461. break;
  3462. }
  3463. udelay(50);
  3464. }
  3465. if (retry < 5)
  3466. break;
  3467. }
  3468. if (i == 4)
  3469. DRM_ERROR("FDI train 1 fail!\n");
  3470. /* Train 2 */
  3471. reg = FDI_TX_CTL(pipe);
  3472. temp = I915_READ(reg);
  3473. temp &= ~FDI_LINK_TRAIN_NONE;
  3474. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3475. if (IS_GEN6(dev_priv)) {
  3476. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3477. /* SNB-B */
  3478. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3479. }
  3480. I915_WRITE(reg, temp);
  3481. reg = FDI_RX_CTL(pipe);
  3482. temp = I915_READ(reg);
  3483. if (HAS_PCH_CPT(dev_priv)) {
  3484. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3485. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3486. } else {
  3487. temp &= ~FDI_LINK_TRAIN_NONE;
  3488. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3489. }
  3490. I915_WRITE(reg, temp);
  3491. POSTING_READ(reg);
  3492. udelay(150);
  3493. for (i = 0; i < 4; i++) {
  3494. reg = FDI_TX_CTL(pipe);
  3495. temp = I915_READ(reg);
  3496. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3497. temp |= snb_b_fdi_train_param[i];
  3498. I915_WRITE(reg, temp);
  3499. POSTING_READ(reg);
  3500. udelay(500);
  3501. for (retry = 0; retry < 5; retry++) {
  3502. reg = FDI_RX_IIR(pipe);
  3503. temp = I915_READ(reg);
  3504. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3505. if (temp & FDI_RX_SYMBOL_LOCK) {
  3506. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3507. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3508. break;
  3509. }
  3510. udelay(50);
  3511. }
  3512. if (retry < 5)
  3513. break;
  3514. }
  3515. if (i == 4)
  3516. DRM_ERROR("FDI train 2 fail!\n");
  3517. DRM_DEBUG_KMS("FDI train done.\n");
  3518. }
  3519. /* Manual link training for Ivy Bridge A0 parts */
  3520. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3521. const struct intel_crtc_state *crtc_state)
  3522. {
  3523. struct drm_device *dev = crtc->base.dev;
  3524. struct drm_i915_private *dev_priv = to_i915(dev);
  3525. int pipe = crtc->pipe;
  3526. i915_reg_t reg;
  3527. u32 temp, i, j;
  3528. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3529. for train result */
  3530. reg = FDI_RX_IMR(pipe);
  3531. temp = I915_READ(reg);
  3532. temp &= ~FDI_RX_SYMBOL_LOCK;
  3533. temp &= ~FDI_RX_BIT_LOCK;
  3534. I915_WRITE(reg, temp);
  3535. POSTING_READ(reg);
  3536. udelay(150);
  3537. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3538. I915_READ(FDI_RX_IIR(pipe)));
  3539. /* Try each vswing and preemphasis setting twice before moving on */
  3540. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3541. /* disable first in case we need to retry */
  3542. reg = FDI_TX_CTL(pipe);
  3543. temp = I915_READ(reg);
  3544. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3545. temp &= ~FDI_TX_ENABLE;
  3546. I915_WRITE(reg, temp);
  3547. reg = FDI_RX_CTL(pipe);
  3548. temp = I915_READ(reg);
  3549. temp &= ~FDI_LINK_TRAIN_AUTO;
  3550. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3551. temp &= ~FDI_RX_ENABLE;
  3552. I915_WRITE(reg, temp);
  3553. /* enable CPU FDI TX and PCH FDI RX */
  3554. reg = FDI_TX_CTL(pipe);
  3555. temp = I915_READ(reg);
  3556. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3557. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3558. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3559. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3560. temp |= snb_b_fdi_train_param[j/2];
  3561. temp |= FDI_COMPOSITE_SYNC;
  3562. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3563. I915_WRITE(FDI_RX_MISC(pipe),
  3564. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3565. reg = FDI_RX_CTL(pipe);
  3566. temp = I915_READ(reg);
  3567. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3568. temp |= FDI_COMPOSITE_SYNC;
  3569. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3570. POSTING_READ(reg);
  3571. udelay(1); /* should be 0.5us */
  3572. for (i = 0; i < 4; i++) {
  3573. reg = FDI_RX_IIR(pipe);
  3574. temp = I915_READ(reg);
  3575. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3576. if (temp & FDI_RX_BIT_LOCK ||
  3577. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3578. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3579. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3580. i);
  3581. break;
  3582. }
  3583. udelay(1); /* should be 0.5us */
  3584. }
  3585. if (i == 4) {
  3586. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3587. continue;
  3588. }
  3589. /* Train 2 */
  3590. reg = FDI_TX_CTL(pipe);
  3591. temp = I915_READ(reg);
  3592. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3593. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3594. I915_WRITE(reg, temp);
  3595. reg = FDI_RX_CTL(pipe);
  3596. temp = I915_READ(reg);
  3597. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3598. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3599. I915_WRITE(reg, temp);
  3600. POSTING_READ(reg);
  3601. udelay(2); /* should be 1.5us */
  3602. for (i = 0; i < 4; i++) {
  3603. reg = FDI_RX_IIR(pipe);
  3604. temp = I915_READ(reg);
  3605. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3606. if (temp & FDI_RX_SYMBOL_LOCK ||
  3607. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3608. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3609. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3610. i);
  3611. goto train_done;
  3612. }
  3613. udelay(2); /* should be 1.5us */
  3614. }
  3615. if (i == 4)
  3616. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3617. }
  3618. train_done:
  3619. DRM_DEBUG_KMS("FDI train done.\n");
  3620. }
  3621. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3622. {
  3623. struct drm_device *dev = intel_crtc->base.dev;
  3624. struct drm_i915_private *dev_priv = to_i915(dev);
  3625. int pipe = intel_crtc->pipe;
  3626. i915_reg_t reg;
  3627. u32 temp;
  3628. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3629. reg = FDI_RX_CTL(pipe);
  3630. temp = I915_READ(reg);
  3631. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3632. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3633. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3634. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3635. POSTING_READ(reg);
  3636. udelay(200);
  3637. /* Switch from Rawclk to PCDclk */
  3638. temp = I915_READ(reg);
  3639. I915_WRITE(reg, temp | FDI_PCDCLK);
  3640. POSTING_READ(reg);
  3641. udelay(200);
  3642. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3643. reg = FDI_TX_CTL(pipe);
  3644. temp = I915_READ(reg);
  3645. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3646. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3647. POSTING_READ(reg);
  3648. udelay(100);
  3649. }
  3650. }
  3651. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3652. {
  3653. struct drm_device *dev = intel_crtc->base.dev;
  3654. struct drm_i915_private *dev_priv = to_i915(dev);
  3655. int pipe = intel_crtc->pipe;
  3656. i915_reg_t reg;
  3657. u32 temp;
  3658. /* Switch from PCDclk to Rawclk */
  3659. reg = FDI_RX_CTL(pipe);
  3660. temp = I915_READ(reg);
  3661. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3662. /* Disable CPU FDI TX PLL */
  3663. reg = FDI_TX_CTL(pipe);
  3664. temp = I915_READ(reg);
  3665. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3666. POSTING_READ(reg);
  3667. udelay(100);
  3668. reg = FDI_RX_CTL(pipe);
  3669. temp = I915_READ(reg);
  3670. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3671. /* Wait for the clocks to turn off. */
  3672. POSTING_READ(reg);
  3673. udelay(100);
  3674. }
  3675. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3676. {
  3677. struct drm_device *dev = crtc->dev;
  3678. struct drm_i915_private *dev_priv = to_i915(dev);
  3679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3680. int pipe = intel_crtc->pipe;
  3681. i915_reg_t reg;
  3682. u32 temp;
  3683. /* disable CPU FDI tx and PCH FDI rx */
  3684. reg = FDI_TX_CTL(pipe);
  3685. temp = I915_READ(reg);
  3686. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3687. POSTING_READ(reg);
  3688. reg = FDI_RX_CTL(pipe);
  3689. temp = I915_READ(reg);
  3690. temp &= ~(0x7 << 16);
  3691. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3692. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3693. POSTING_READ(reg);
  3694. udelay(100);
  3695. /* Ironlake workaround, disable clock pointer after downing FDI */
  3696. if (HAS_PCH_IBX(dev_priv))
  3697. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3698. /* still set train pattern 1 */
  3699. reg = FDI_TX_CTL(pipe);
  3700. temp = I915_READ(reg);
  3701. temp &= ~FDI_LINK_TRAIN_NONE;
  3702. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3703. I915_WRITE(reg, temp);
  3704. reg = FDI_RX_CTL(pipe);
  3705. temp = I915_READ(reg);
  3706. if (HAS_PCH_CPT(dev_priv)) {
  3707. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3708. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3709. } else {
  3710. temp &= ~FDI_LINK_TRAIN_NONE;
  3711. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3712. }
  3713. /* BPC in FDI rx is consistent with that in PIPECONF */
  3714. temp &= ~(0x07 << 16);
  3715. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3716. I915_WRITE(reg, temp);
  3717. POSTING_READ(reg);
  3718. udelay(100);
  3719. }
  3720. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3721. {
  3722. struct drm_crtc *crtc;
  3723. bool cleanup_done;
  3724. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3725. struct drm_crtc_commit *commit;
  3726. spin_lock(&crtc->commit_lock);
  3727. commit = list_first_entry_or_null(&crtc->commit_list,
  3728. struct drm_crtc_commit, commit_entry);
  3729. cleanup_done = commit ?
  3730. try_wait_for_completion(&commit->cleanup_done) : true;
  3731. spin_unlock(&crtc->commit_lock);
  3732. if (cleanup_done)
  3733. continue;
  3734. drm_crtc_wait_one_vblank(crtc);
  3735. return true;
  3736. }
  3737. return false;
  3738. }
  3739. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3740. {
  3741. u32 temp;
  3742. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3743. mutex_lock(&dev_priv->sb_lock);
  3744. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3745. temp |= SBI_SSCCTL_DISABLE;
  3746. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3747. mutex_unlock(&dev_priv->sb_lock);
  3748. }
  3749. /* Program iCLKIP clock to the desired frequency */
  3750. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3751. {
  3752. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3753. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3754. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3755. u32 temp;
  3756. lpt_disable_iclkip(dev_priv);
  3757. /* The iCLK virtual clock root frequency is in MHz,
  3758. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3759. * divisors, it is necessary to divide one by another, so we
  3760. * convert the virtual clock precision to KHz here for higher
  3761. * precision.
  3762. */
  3763. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3764. u32 iclk_virtual_root_freq = 172800 * 1000;
  3765. u32 iclk_pi_range = 64;
  3766. u32 desired_divisor;
  3767. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3768. clock << auxdiv);
  3769. divsel = (desired_divisor / iclk_pi_range) - 2;
  3770. phaseinc = desired_divisor % iclk_pi_range;
  3771. /*
  3772. * Near 20MHz is a corner case which is
  3773. * out of range for the 7-bit divisor
  3774. */
  3775. if (divsel <= 0x7f)
  3776. break;
  3777. }
  3778. /* This should not happen with any sane values */
  3779. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3780. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3781. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3782. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3783. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3784. clock,
  3785. auxdiv,
  3786. divsel,
  3787. phasedir,
  3788. phaseinc);
  3789. mutex_lock(&dev_priv->sb_lock);
  3790. /* Program SSCDIVINTPHASE6 */
  3791. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3792. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3793. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3794. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3795. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3796. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3797. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3798. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3799. /* Program SSCAUXDIV */
  3800. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3801. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3802. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3803. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3804. /* Enable modulator and associated divider */
  3805. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3806. temp &= ~SBI_SSCCTL_DISABLE;
  3807. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3808. mutex_unlock(&dev_priv->sb_lock);
  3809. /* Wait for initialization time */
  3810. udelay(24);
  3811. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3812. }
  3813. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3814. {
  3815. u32 divsel, phaseinc, auxdiv;
  3816. u32 iclk_virtual_root_freq = 172800 * 1000;
  3817. u32 iclk_pi_range = 64;
  3818. u32 desired_divisor;
  3819. u32 temp;
  3820. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3821. return 0;
  3822. mutex_lock(&dev_priv->sb_lock);
  3823. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3824. if (temp & SBI_SSCCTL_DISABLE) {
  3825. mutex_unlock(&dev_priv->sb_lock);
  3826. return 0;
  3827. }
  3828. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3829. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3830. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3831. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3832. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3833. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3834. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3835. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3836. mutex_unlock(&dev_priv->sb_lock);
  3837. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3838. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3839. desired_divisor << auxdiv);
  3840. }
  3841. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3842. enum pipe pch_transcoder)
  3843. {
  3844. struct drm_device *dev = crtc->base.dev;
  3845. struct drm_i915_private *dev_priv = to_i915(dev);
  3846. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3847. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3848. I915_READ(HTOTAL(cpu_transcoder)));
  3849. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3850. I915_READ(HBLANK(cpu_transcoder)));
  3851. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3852. I915_READ(HSYNC(cpu_transcoder)));
  3853. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3854. I915_READ(VTOTAL(cpu_transcoder)));
  3855. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3856. I915_READ(VBLANK(cpu_transcoder)));
  3857. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3858. I915_READ(VSYNC(cpu_transcoder)));
  3859. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3860. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3861. }
  3862. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3863. {
  3864. struct drm_i915_private *dev_priv = to_i915(dev);
  3865. uint32_t temp;
  3866. temp = I915_READ(SOUTH_CHICKEN1);
  3867. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3868. return;
  3869. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3870. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3871. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3872. if (enable)
  3873. temp |= FDI_BC_BIFURCATION_SELECT;
  3874. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3875. I915_WRITE(SOUTH_CHICKEN1, temp);
  3876. POSTING_READ(SOUTH_CHICKEN1);
  3877. }
  3878. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3879. {
  3880. struct drm_device *dev = intel_crtc->base.dev;
  3881. switch (intel_crtc->pipe) {
  3882. case PIPE_A:
  3883. break;
  3884. case PIPE_B:
  3885. if (intel_crtc->config->fdi_lanes > 2)
  3886. cpt_set_fdi_bc_bifurcation(dev, false);
  3887. else
  3888. cpt_set_fdi_bc_bifurcation(dev, true);
  3889. break;
  3890. case PIPE_C:
  3891. cpt_set_fdi_bc_bifurcation(dev, true);
  3892. break;
  3893. default:
  3894. BUG();
  3895. }
  3896. }
  3897. /* Return which DP Port should be selected for Transcoder DP control */
  3898. static enum port
  3899. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3900. {
  3901. struct drm_device *dev = crtc->base.dev;
  3902. struct intel_encoder *encoder;
  3903. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3904. if (encoder->type == INTEL_OUTPUT_DP ||
  3905. encoder->type == INTEL_OUTPUT_EDP)
  3906. return encoder->port;
  3907. }
  3908. return -1;
  3909. }
  3910. /*
  3911. * Enable PCH resources required for PCH ports:
  3912. * - PCH PLLs
  3913. * - FDI training & RX/TX
  3914. * - update transcoder timings
  3915. * - DP transcoding bits
  3916. * - transcoder
  3917. */
  3918. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3919. {
  3920. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3921. struct drm_device *dev = crtc->base.dev;
  3922. struct drm_i915_private *dev_priv = to_i915(dev);
  3923. int pipe = crtc->pipe;
  3924. u32 temp;
  3925. assert_pch_transcoder_disabled(dev_priv, pipe);
  3926. if (IS_IVYBRIDGE(dev_priv))
  3927. ivybridge_update_fdi_bc_bifurcation(crtc);
  3928. /* Write the TU size bits before fdi link training, so that error
  3929. * detection works. */
  3930. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3931. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3932. /* For PCH output, training FDI link */
  3933. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3934. /* We need to program the right clock selection before writing the pixel
  3935. * mutliplier into the DPLL. */
  3936. if (HAS_PCH_CPT(dev_priv)) {
  3937. u32 sel;
  3938. temp = I915_READ(PCH_DPLL_SEL);
  3939. temp |= TRANS_DPLL_ENABLE(pipe);
  3940. sel = TRANS_DPLLB_SEL(pipe);
  3941. if (crtc_state->shared_dpll ==
  3942. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3943. temp |= sel;
  3944. else
  3945. temp &= ~sel;
  3946. I915_WRITE(PCH_DPLL_SEL, temp);
  3947. }
  3948. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3949. * transcoder, and we actually should do this to not upset any PCH
  3950. * transcoder that already use the clock when we share it.
  3951. *
  3952. * Note that enable_shared_dpll tries to do the right thing, but
  3953. * get_shared_dpll unconditionally resets the pll - we need that to have
  3954. * the right LVDS enable sequence. */
  3955. intel_enable_shared_dpll(crtc);
  3956. /* set transcoder timing, panel must allow it */
  3957. assert_panel_unlocked(dev_priv, pipe);
  3958. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3959. intel_fdi_normal_train(crtc);
  3960. /* For PCH DP, enable TRANS_DP_CTL */
  3961. if (HAS_PCH_CPT(dev_priv) &&
  3962. intel_crtc_has_dp_encoder(crtc_state)) {
  3963. const struct drm_display_mode *adjusted_mode =
  3964. &crtc_state->base.adjusted_mode;
  3965. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3966. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3967. temp = I915_READ(reg);
  3968. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3969. TRANS_DP_SYNC_MASK |
  3970. TRANS_DP_BPC_MASK);
  3971. temp |= TRANS_DP_OUTPUT_ENABLE;
  3972. temp |= bpc << 9; /* same format but at 11:9 */
  3973. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3974. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3975. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3976. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3977. switch (intel_trans_dp_port_sel(crtc)) {
  3978. case PORT_B:
  3979. temp |= TRANS_DP_PORT_SEL_B;
  3980. break;
  3981. case PORT_C:
  3982. temp |= TRANS_DP_PORT_SEL_C;
  3983. break;
  3984. case PORT_D:
  3985. temp |= TRANS_DP_PORT_SEL_D;
  3986. break;
  3987. default:
  3988. BUG();
  3989. }
  3990. I915_WRITE(reg, temp);
  3991. }
  3992. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3993. }
  3994. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3995. {
  3996. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3997. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3998. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3999. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4000. lpt_program_iclkip(crtc);
  4001. /* Set transcoder timing. */
  4002. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4003. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4004. }
  4005. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4006. {
  4007. struct drm_i915_private *dev_priv = to_i915(dev);
  4008. i915_reg_t dslreg = PIPEDSL(pipe);
  4009. u32 temp;
  4010. temp = I915_READ(dslreg);
  4011. udelay(500);
  4012. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4013. if (wait_for(I915_READ(dslreg) != temp, 5))
  4014. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4015. }
  4016. }
  4017. static int
  4018. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4019. unsigned int scaler_user, int *scaler_id,
  4020. int src_w, int src_h, int dst_w, int dst_h,
  4021. bool plane_scaler_check,
  4022. uint32_t pixel_format)
  4023. {
  4024. struct intel_crtc_scaler_state *scaler_state =
  4025. &crtc_state->scaler_state;
  4026. struct intel_crtc *intel_crtc =
  4027. to_intel_crtc(crtc_state->base.crtc);
  4028. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4029. const struct drm_display_mode *adjusted_mode =
  4030. &crtc_state->base.adjusted_mode;
  4031. int need_scaling;
  4032. /*
  4033. * Src coordinates are already rotated by 270 degrees for
  4034. * the 90/270 degree plane rotation cases (to match the
  4035. * GTT mapping), hence no need to account for rotation here.
  4036. */
  4037. need_scaling = src_w != dst_w || src_h != dst_h;
  4038. if (plane_scaler_check)
  4039. if (pixel_format == DRM_FORMAT_NV12)
  4040. need_scaling = true;
  4041. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4042. need_scaling = true;
  4043. /*
  4044. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4045. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4046. * Once NV12 is enabled, handle it here while allocating scaler
  4047. * for NV12.
  4048. */
  4049. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4050. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4051. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4052. return -EINVAL;
  4053. }
  4054. /*
  4055. * if plane is being disabled or scaler is no more required or force detach
  4056. * - free scaler binded to this plane/crtc
  4057. * - in order to do this, update crtc->scaler_usage
  4058. *
  4059. * Here scaler state in crtc_state is set free so that
  4060. * scaler can be assigned to other user. Actual register
  4061. * update to free the scaler is done in plane/panel-fit programming.
  4062. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4063. */
  4064. if (force_detach || !need_scaling) {
  4065. if (*scaler_id >= 0) {
  4066. scaler_state->scaler_users &= ~(1 << scaler_user);
  4067. scaler_state->scalers[*scaler_id].in_use = 0;
  4068. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4069. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4070. intel_crtc->pipe, scaler_user, *scaler_id,
  4071. scaler_state->scaler_users);
  4072. *scaler_id = -1;
  4073. }
  4074. return 0;
  4075. }
  4076. if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
  4077. (src_h < SKL_MIN_YUV_420_SRC_H || (src_w % 4) != 0 ||
  4078. (src_h % 4) != 0)) {
  4079. DRM_DEBUG_KMS("NV12: src dimensions not met\n");
  4080. return -EINVAL;
  4081. }
  4082. /* range checks */
  4083. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4084. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4085. (IS_GEN11(dev_priv) &&
  4086. (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
  4087. dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
  4088. (!IS_GEN11(dev_priv) &&
  4089. (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4090. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
  4091. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4092. "size is out of scaler range\n",
  4093. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4094. return -EINVAL;
  4095. }
  4096. /* mark this plane as a scaler user in crtc_state */
  4097. scaler_state->scaler_users |= (1 << scaler_user);
  4098. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4099. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4100. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4101. scaler_state->scaler_users);
  4102. return 0;
  4103. }
  4104. /**
  4105. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4106. *
  4107. * @state: crtc's scaler state
  4108. *
  4109. * Return
  4110. * 0 - scaler_usage updated successfully
  4111. * error - requested scaling cannot be supported or other error condition
  4112. */
  4113. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4114. {
  4115. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4116. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4117. &state->scaler_state.scaler_id,
  4118. state->pipe_src_w, state->pipe_src_h,
  4119. adjusted_mode->crtc_hdisplay,
  4120. adjusted_mode->crtc_vdisplay, false, 0);
  4121. }
  4122. /**
  4123. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4124. * @crtc_state: crtc's scaler state
  4125. * @plane_state: atomic plane state to update
  4126. *
  4127. * Return
  4128. * 0 - scaler_usage updated successfully
  4129. * error - requested scaling cannot be supported or other error condition
  4130. */
  4131. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4132. struct intel_plane_state *plane_state)
  4133. {
  4134. struct intel_plane *intel_plane =
  4135. to_intel_plane(plane_state->base.plane);
  4136. struct drm_framebuffer *fb = plane_state->base.fb;
  4137. int ret;
  4138. bool force_detach = !fb || !plane_state->base.visible;
  4139. ret = skl_update_scaler(crtc_state, force_detach,
  4140. drm_plane_index(&intel_plane->base),
  4141. &plane_state->scaler_id,
  4142. drm_rect_width(&plane_state->base.src) >> 16,
  4143. drm_rect_height(&plane_state->base.src) >> 16,
  4144. drm_rect_width(&plane_state->base.dst),
  4145. drm_rect_height(&plane_state->base.dst),
  4146. fb ? true : false, fb ? fb->format->format : 0);
  4147. if (ret || plane_state->scaler_id < 0)
  4148. return ret;
  4149. /* check colorkey */
  4150. if (plane_state->ckey.flags) {
  4151. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4152. intel_plane->base.base.id,
  4153. intel_plane->base.name);
  4154. return -EINVAL;
  4155. }
  4156. /* Check src format */
  4157. switch (fb->format->format) {
  4158. case DRM_FORMAT_RGB565:
  4159. case DRM_FORMAT_XBGR8888:
  4160. case DRM_FORMAT_XRGB8888:
  4161. case DRM_FORMAT_ABGR8888:
  4162. case DRM_FORMAT_ARGB8888:
  4163. case DRM_FORMAT_XRGB2101010:
  4164. case DRM_FORMAT_XBGR2101010:
  4165. case DRM_FORMAT_YUYV:
  4166. case DRM_FORMAT_YVYU:
  4167. case DRM_FORMAT_UYVY:
  4168. case DRM_FORMAT_VYUY:
  4169. case DRM_FORMAT_NV12:
  4170. break;
  4171. default:
  4172. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4173. intel_plane->base.base.id, intel_plane->base.name,
  4174. fb->base.id, fb->format->format);
  4175. return -EINVAL;
  4176. }
  4177. return 0;
  4178. }
  4179. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4180. {
  4181. int i;
  4182. for (i = 0; i < crtc->num_scalers; i++)
  4183. skl_detach_scaler(crtc, i);
  4184. }
  4185. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4186. {
  4187. struct drm_device *dev = crtc->base.dev;
  4188. struct drm_i915_private *dev_priv = to_i915(dev);
  4189. int pipe = crtc->pipe;
  4190. struct intel_crtc_scaler_state *scaler_state =
  4191. &crtc->config->scaler_state;
  4192. if (crtc->config->pch_pfit.enabled) {
  4193. int id;
  4194. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4195. return;
  4196. id = scaler_state->scaler_id;
  4197. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4198. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4199. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4200. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4201. }
  4202. }
  4203. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4204. {
  4205. struct drm_device *dev = crtc->base.dev;
  4206. struct drm_i915_private *dev_priv = to_i915(dev);
  4207. int pipe = crtc->pipe;
  4208. if (crtc->config->pch_pfit.enabled) {
  4209. /* Force use of hard-coded filter coefficients
  4210. * as some pre-programmed values are broken,
  4211. * e.g. x201.
  4212. */
  4213. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4214. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4215. PF_PIPE_SEL_IVB(pipe));
  4216. else
  4217. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4218. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4219. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4220. }
  4221. }
  4222. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4223. {
  4224. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4225. struct drm_device *dev = crtc->base.dev;
  4226. struct drm_i915_private *dev_priv = to_i915(dev);
  4227. if (!crtc_state->ips_enabled)
  4228. return;
  4229. /*
  4230. * We can only enable IPS after we enable a plane and wait for a vblank
  4231. * This function is called from post_plane_update, which is run after
  4232. * a vblank wait.
  4233. */
  4234. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4235. if (IS_BROADWELL(dev_priv)) {
  4236. mutex_lock(&dev_priv->pcu_lock);
  4237. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4238. IPS_ENABLE | IPS_PCODE_CONTROL));
  4239. mutex_unlock(&dev_priv->pcu_lock);
  4240. /* Quoting Art Runyan: "its not safe to expect any particular
  4241. * value in IPS_CTL bit 31 after enabling IPS through the
  4242. * mailbox." Moreover, the mailbox may return a bogus state,
  4243. * so we need to just enable it and continue on.
  4244. */
  4245. } else {
  4246. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4247. /* The bit only becomes 1 in the next vblank, so this wait here
  4248. * is essentially intel_wait_for_vblank. If we don't have this
  4249. * and don't wait for vblanks until the end of crtc_enable, then
  4250. * the HW state readout code will complain that the expected
  4251. * IPS_CTL value is not the one we read. */
  4252. if (intel_wait_for_register(dev_priv,
  4253. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4254. 50))
  4255. DRM_ERROR("Timed out waiting for IPS enable\n");
  4256. }
  4257. }
  4258. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4259. {
  4260. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4261. struct drm_device *dev = crtc->base.dev;
  4262. struct drm_i915_private *dev_priv = to_i915(dev);
  4263. if (!crtc_state->ips_enabled)
  4264. return;
  4265. if (IS_BROADWELL(dev_priv)) {
  4266. mutex_lock(&dev_priv->pcu_lock);
  4267. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4268. mutex_unlock(&dev_priv->pcu_lock);
  4269. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4270. if (intel_wait_for_register(dev_priv,
  4271. IPS_CTL, IPS_ENABLE, 0,
  4272. 42))
  4273. DRM_ERROR("Timed out waiting for IPS disable\n");
  4274. } else {
  4275. I915_WRITE(IPS_CTL, 0);
  4276. POSTING_READ(IPS_CTL);
  4277. }
  4278. /* We need to wait for a vblank before we can disable the plane. */
  4279. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4280. }
  4281. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4282. {
  4283. if (intel_crtc->overlay) {
  4284. struct drm_device *dev = intel_crtc->base.dev;
  4285. mutex_lock(&dev->struct_mutex);
  4286. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4287. mutex_unlock(&dev->struct_mutex);
  4288. }
  4289. /* Let userspace switch the overlay on again. In most cases userspace
  4290. * has to recompute where to put it anyway.
  4291. */
  4292. }
  4293. /**
  4294. * intel_post_enable_primary - Perform operations after enabling primary plane
  4295. * @crtc: the CRTC whose primary plane was just enabled
  4296. * @new_crtc_state: the enabling state
  4297. *
  4298. * Performs potentially sleeping operations that must be done after the primary
  4299. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4300. * called due to an explicit primary plane update, or due to an implicit
  4301. * re-enable that is caused when a sprite plane is updated to no longer
  4302. * completely hide the primary plane.
  4303. */
  4304. static void
  4305. intel_post_enable_primary(struct drm_crtc *crtc,
  4306. const struct intel_crtc_state *new_crtc_state)
  4307. {
  4308. struct drm_device *dev = crtc->dev;
  4309. struct drm_i915_private *dev_priv = to_i915(dev);
  4310. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4311. int pipe = intel_crtc->pipe;
  4312. /*
  4313. * Gen2 reports pipe underruns whenever all planes are disabled.
  4314. * So don't enable underrun reporting before at least some planes
  4315. * are enabled.
  4316. * FIXME: Need to fix the logic to work when we turn off all planes
  4317. * but leave the pipe running.
  4318. */
  4319. if (IS_GEN2(dev_priv))
  4320. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4321. /* Underruns don't always raise interrupts, so check manually. */
  4322. intel_check_cpu_fifo_underruns(dev_priv);
  4323. intel_check_pch_fifo_underruns(dev_priv);
  4324. }
  4325. /* FIXME get rid of this and use pre_plane_update */
  4326. static void
  4327. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4328. {
  4329. struct drm_device *dev = crtc->dev;
  4330. struct drm_i915_private *dev_priv = to_i915(dev);
  4331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4332. int pipe = intel_crtc->pipe;
  4333. /*
  4334. * Gen2 reports pipe underruns whenever all planes are disabled.
  4335. * So disable underrun reporting before all the planes get disabled.
  4336. */
  4337. if (IS_GEN2(dev_priv))
  4338. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4339. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4340. /*
  4341. * Vblank time updates from the shadow to live plane control register
  4342. * are blocked if the memory self-refresh mode is active at that
  4343. * moment. So to make sure the plane gets truly disabled, disable
  4344. * first the self-refresh mode. The self-refresh enable bit in turn
  4345. * will be checked/applied by the HW only at the next frame start
  4346. * event which is after the vblank start event, so we need to have a
  4347. * wait-for-vblank between disabling the plane and the pipe.
  4348. */
  4349. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4350. intel_set_memory_cxsr(dev_priv, false))
  4351. intel_wait_for_vblank(dev_priv, pipe);
  4352. }
  4353. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4354. const struct intel_crtc_state *new_crtc_state)
  4355. {
  4356. if (!old_crtc_state->ips_enabled)
  4357. return false;
  4358. if (needs_modeset(&new_crtc_state->base))
  4359. return true;
  4360. return !new_crtc_state->ips_enabled;
  4361. }
  4362. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4363. const struct intel_crtc_state *new_crtc_state)
  4364. {
  4365. if (!new_crtc_state->ips_enabled)
  4366. return false;
  4367. if (needs_modeset(&new_crtc_state->base))
  4368. return true;
  4369. /*
  4370. * We can't read out IPS on broadwell, assume the worst and
  4371. * forcibly enable IPS on the first fastset.
  4372. */
  4373. if (new_crtc_state->update_pipe &&
  4374. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4375. return true;
  4376. return !old_crtc_state->ips_enabled;
  4377. }
  4378. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4379. {
  4380. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4381. struct drm_device *dev = crtc->base.dev;
  4382. struct drm_i915_private *dev_priv = to_i915(dev);
  4383. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4384. struct intel_crtc_state *pipe_config =
  4385. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4386. crtc);
  4387. struct drm_plane *primary = crtc->base.primary;
  4388. struct drm_plane_state *old_primary_state =
  4389. drm_atomic_get_old_plane_state(old_state, primary);
  4390. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4391. if (pipe_config->update_wm_post && pipe_config->base.active)
  4392. intel_update_watermarks(crtc);
  4393. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4394. hsw_enable_ips(pipe_config);
  4395. if (old_primary_state) {
  4396. struct drm_plane_state *new_primary_state =
  4397. drm_atomic_get_new_plane_state(old_state, primary);
  4398. struct drm_framebuffer *fb = new_primary_state->fb;
  4399. intel_fbc_post_update(crtc);
  4400. if (new_primary_state->visible &&
  4401. (needs_modeset(&pipe_config->base) ||
  4402. !old_primary_state->visible))
  4403. intel_post_enable_primary(&crtc->base, pipe_config);
  4404. /* Display WA 827 */
  4405. if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
  4406. IS_CANNONLAKE(dev_priv)) {
  4407. if (fb && fb->format->format == DRM_FORMAT_NV12)
  4408. skl_wa_clkgate(dev_priv, crtc->pipe, false);
  4409. }
  4410. }
  4411. }
  4412. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4413. struct intel_crtc_state *pipe_config)
  4414. {
  4415. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4416. struct drm_device *dev = crtc->base.dev;
  4417. struct drm_i915_private *dev_priv = to_i915(dev);
  4418. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4419. struct drm_plane *primary = crtc->base.primary;
  4420. struct drm_plane_state *old_primary_state =
  4421. drm_atomic_get_old_plane_state(old_state, primary);
  4422. bool modeset = needs_modeset(&pipe_config->base);
  4423. struct intel_atomic_state *old_intel_state =
  4424. to_intel_atomic_state(old_state);
  4425. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4426. hsw_disable_ips(old_crtc_state);
  4427. if (old_primary_state) {
  4428. struct intel_plane_state *new_primary_state =
  4429. intel_atomic_get_new_plane_state(old_intel_state,
  4430. to_intel_plane(primary));
  4431. struct drm_framebuffer *fb = new_primary_state->base.fb;
  4432. /* Display WA 827 */
  4433. if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
  4434. IS_CANNONLAKE(dev_priv)) {
  4435. if (fb && fb->format->format == DRM_FORMAT_NV12)
  4436. skl_wa_clkgate(dev_priv, crtc->pipe, true);
  4437. }
  4438. intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
  4439. /*
  4440. * Gen2 reports pipe underruns whenever all planes are disabled.
  4441. * So disable underrun reporting before all the planes get disabled.
  4442. */
  4443. if (IS_GEN2(dev_priv) && old_primary_state->visible &&
  4444. (modeset || !new_primary_state->base.visible))
  4445. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4446. }
  4447. /*
  4448. * Vblank time updates from the shadow to live plane control register
  4449. * are blocked if the memory self-refresh mode is active at that
  4450. * moment. So to make sure the plane gets truly disabled, disable
  4451. * first the self-refresh mode. The self-refresh enable bit in turn
  4452. * will be checked/applied by the HW only at the next frame start
  4453. * event which is after the vblank start event, so we need to have a
  4454. * wait-for-vblank between disabling the plane and the pipe.
  4455. */
  4456. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4457. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4458. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4459. /*
  4460. * IVB workaround: must disable low power watermarks for at least
  4461. * one frame before enabling scaling. LP watermarks can be re-enabled
  4462. * when scaling is disabled.
  4463. *
  4464. * WaCxSRDisabledForSpriteScaling:ivb
  4465. */
  4466. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4467. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4468. /*
  4469. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4470. * watermark programming here.
  4471. */
  4472. if (needs_modeset(&pipe_config->base))
  4473. return;
  4474. /*
  4475. * For platforms that support atomic watermarks, program the
  4476. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4477. * will be the intermediate values that are safe for both pre- and
  4478. * post- vblank; when vblank happens, the 'active' values will be set
  4479. * to the final 'target' values and we'll do this again to get the
  4480. * optimal watermarks. For gen9+ platforms, the values we program here
  4481. * will be the final target values which will get automatically latched
  4482. * at vblank time; no further programming will be necessary.
  4483. *
  4484. * If a platform hasn't been transitioned to atomic watermarks yet,
  4485. * we'll continue to update watermarks the old way, if flags tell
  4486. * us to.
  4487. */
  4488. if (dev_priv->display.initial_watermarks != NULL)
  4489. dev_priv->display.initial_watermarks(old_intel_state,
  4490. pipe_config);
  4491. else if (pipe_config->update_wm_pre)
  4492. intel_update_watermarks(crtc);
  4493. }
  4494. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4495. {
  4496. struct drm_device *dev = crtc->dev;
  4497. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4498. struct drm_plane *p;
  4499. int pipe = intel_crtc->pipe;
  4500. intel_crtc_dpms_overlay_disable(intel_crtc);
  4501. drm_for_each_plane_mask(p, dev, plane_mask)
  4502. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4503. /*
  4504. * FIXME: Once we grow proper nuclear flip support out of this we need
  4505. * to compute the mask of flip planes precisely. For the time being
  4506. * consider this a flip to a NULL plane.
  4507. */
  4508. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4509. }
  4510. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4511. struct intel_crtc_state *crtc_state,
  4512. struct drm_atomic_state *old_state)
  4513. {
  4514. struct drm_connector_state *conn_state;
  4515. struct drm_connector *conn;
  4516. int i;
  4517. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4518. struct intel_encoder *encoder =
  4519. to_intel_encoder(conn_state->best_encoder);
  4520. if (conn_state->crtc != crtc)
  4521. continue;
  4522. if (encoder->pre_pll_enable)
  4523. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4524. }
  4525. }
  4526. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4527. struct intel_crtc_state *crtc_state,
  4528. struct drm_atomic_state *old_state)
  4529. {
  4530. struct drm_connector_state *conn_state;
  4531. struct drm_connector *conn;
  4532. int i;
  4533. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4534. struct intel_encoder *encoder =
  4535. to_intel_encoder(conn_state->best_encoder);
  4536. if (conn_state->crtc != crtc)
  4537. continue;
  4538. if (encoder->pre_enable)
  4539. encoder->pre_enable(encoder, crtc_state, conn_state);
  4540. }
  4541. }
  4542. static void intel_encoders_enable(struct drm_crtc *crtc,
  4543. struct intel_crtc_state *crtc_state,
  4544. struct drm_atomic_state *old_state)
  4545. {
  4546. struct drm_connector_state *conn_state;
  4547. struct drm_connector *conn;
  4548. int i;
  4549. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4550. struct intel_encoder *encoder =
  4551. to_intel_encoder(conn_state->best_encoder);
  4552. if (conn_state->crtc != crtc)
  4553. continue;
  4554. encoder->enable(encoder, crtc_state, conn_state);
  4555. intel_opregion_notify_encoder(encoder, true);
  4556. }
  4557. }
  4558. static void intel_encoders_disable(struct drm_crtc *crtc,
  4559. struct intel_crtc_state *old_crtc_state,
  4560. struct drm_atomic_state *old_state)
  4561. {
  4562. struct drm_connector_state *old_conn_state;
  4563. struct drm_connector *conn;
  4564. int i;
  4565. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4566. struct intel_encoder *encoder =
  4567. to_intel_encoder(old_conn_state->best_encoder);
  4568. if (old_conn_state->crtc != crtc)
  4569. continue;
  4570. intel_opregion_notify_encoder(encoder, false);
  4571. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4572. }
  4573. }
  4574. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4575. struct intel_crtc_state *old_crtc_state,
  4576. struct drm_atomic_state *old_state)
  4577. {
  4578. struct drm_connector_state *old_conn_state;
  4579. struct drm_connector *conn;
  4580. int i;
  4581. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4582. struct intel_encoder *encoder =
  4583. to_intel_encoder(old_conn_state->best_encoder);
  4584. if (old_conn_state->crtc != crtc)
  4585. continue;
  4586. if (encoder->post_disable)
  4587. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4588. }
  4589. }
  4590. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4591. struct intel_crtc_state *old_crtc_state,
  4592. struct drm_atomic_state *old_state)
  4593. {
  4594. struct drm_connector_state *old_conn_state;
  4595. struct drm_connector *conn;
  4596. int i;
  4597. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4598. struct intel_encoder *encoder =
  4599. to_intel_encoder(old_conn_state->best_encoder);
  4600. if (old_conn_state->crtc != crtc)
  4601. continue;
  4602. if (encoder->post_pll_disable)
  4603. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4604. }
  4605. }
  4606. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4607. struct drm_atomic_state *old_state)
  4608. {
  4609. struct drm_crtc *crtc = pipe_config->base.crtc;
  4610. struct drm_device *dev = crtc->dev;
  4611. struct drm_i915_private *dev_priv = to_i915(dev);
  4612. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4613. int pipe = intel_crtc->pipe;
  4614. struct intel_atomic_state *old_intel_state =
  4615. to_intel_atomic_state(old_state);
  4616. if (WARN_ON(intel_crtc->active))
  4617. return;
  4618. /*
  4619. * Sometimes spurious CPU pipe underruns happen during FDI
  4620. * training, at least with VGA+HDMI cloning. Suppress them.
  4621. *
  4622. * On ILK we get an occasional spurious CPU pipe underruns
  4623. * between eDP port A enable and vdd enable. Also PCH port
  4624. * enable seems to result in the occasional CPU pipe underrun.
  4625. *
  4626. * Spurious PCH underruns also occur during PCH enabling.
  4627. */
  4628. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4629. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4630. if (intel_crtc->config->has_pch_encoder)
  4631. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4632. if (intel_crtc->config->has_pch_encoder)
  4633. intel_prepare_shared_dpll(intel_crtc);
  4634. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4635. intel_dp_set_m_n(intel_crtc, M1_N1);
  4636. intel_set_pipe_timings(intel_crtc);
  4637. intel_set_pipe_src_size(intel_crtc);
  4638. if (intel_crtc->config->has_pch_encoder) {
  4639. intel_cpu_transcoder_set_m_n(intel_crtc,
  4640. &intel_crtc->config->fdi_m_n, NULL);
  4641. }
  4642. ironlake_set_pipeconf(crtc);
  4643. intel_crtc->active = true;
  4644. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4645. if (intel_crtc->config->has_pch_encoder) {
  4646. /* Note: FDI PLL enabling _must_ be done before we enable the
  4647. * cpu pipes, hence this is separate from all the other fdi/pch
  4648. * enabling. */
  4649. ironlake_fdi_pll_enable(intel_crtc);
  4650. } else {
  4651. assert_fdi_tx_disabled(dev_priv, pipe);
  4652. assert_fdi_rx_disabled(dev_priv, pipe);
  4653. }
  4654. ironlake_pfit_enable(intel_crtc);
  4655. /*
  4656. * On ILK+ LUT must be loaded before the pipe is running but with
  4657. * clocks enabled
  4658. */
  4659. intel_color_load_luts(&pipe_config->base);
  4660. if (dev_priv->display.initial_watermarks != NULL)
  4661. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4662. intel_enable_pipe(pipe_config);
  4663. if (intel_crtc->config->has_pch_encoder)
  4664. ironlake_pch_enable(pipe_config);
  4665. assert_vblank_disabled(crtc);
  4666. drm_crtc_vblank_on(crtc);
  4667. intel_encoders_enable(crtc, pipe_config, old_state);
  4668. if (HAS_PCH_CPT(dev_priv))
  4669. cpt_verify_modeset(dev, intel_crtc->pipe);
  4670. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4671. if (intel_crtc->config->has_pch_encoder)
  4672. intel_wait_for_vblank(dev_priv, pipe);
  4673. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4674. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4675. }
  4676. /* IPS only exists on ULT machines and is tied to pipe A. */
  4677. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4678. {
  4679. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4680. }
  4681. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4682. enum pipe pipe, bool apply)
  4683. {
  4684. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4685. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4686. if (apply)
  4687. val |= mask;
  4688. else
  4689. val &= ~mask;
  4690. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4691. }
  4692. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4693. {
  4694. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4695. enum pipe pipe = crtc->pipe;
  4696. uint32_t val;
  4697. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4698. /* Program B credit equally to all pipes */
  4699. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4700. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4701. }
  4702. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4703. struct drm_atomic_state *old_state)
  4704. {
  4705. struct drm_crtc *crtc = pipe_config->base.crtc;
  4706. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4708. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4709. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4710. struct intel_atomic_state *old_intel_state =
  4711. to_intel_atomic_state(old_state);
  4712. bool psl_clkgate_wa;
  4713. if (WARN_ON(intel_crtc->active))
  4714. return;
  4715. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4716. if (intel_crtc->config->shared_dpll)
  4717. intel_enable_shared_dpll(intel_crtc);
  4718. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4719. intel_dp_set_m_n(intel_crtc, M1_N1);
  4720. if (!transcoder_is_dsi(cpu_transcoder))
  4721. intel_set_pipe_timings(intel_crtc);
  4722. intel_set_pipe_src_size(intel_crtc);
  4723. if (cpu_transcoder != TRANSCODER_EDP &&
  4724. !transcoder_is_dsi(cpu_transcoder)) {
  4725. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4726. intel_crtc->config->pixel_multiplier - 1);
  4727. }
  4728. if (intel_crtc->config->has_pch_encoder) {
  4729. intel_cpu_transcoder_set_m_n(intel_crtc,
  4730. &intel_crtc->config->fdi_m_n, NULL);
  4731. }
  4732. if (!transcoder_is_dsi(cpu_transcoder))
  4733. haswell_set_pipeconf(crtc);
  4734. haswell_set_pipemisc(crtc);
  4735. intel_color_set_csc(&pipe_config->base);
  4736. intel_crtc->active = true;
  4737. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4738. if (!transcoder_is_dsi(cpu_transcoder))
  4739. intel_ddi_enable_pipe_clock(pipe_config);
  4740. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4741. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4742. intel_crtc->config->pch_pfit.enabled;
  4743. if (psl_clkgate_wa)
  4744. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4745. if (INTEL_GEN(dev_priv) >= 9)
  4746. skylake_pfit_enable(intel_crtc);
  4747. else
  4748. ironlake_pfit_enable(intel_crtc);
  4749. /*
  4750. * On ILK+ LUT must be loaded before the pipe is running but with
  4751. * clocks enabled
  4752. */
  4753. intel_color_load_luts(&pipe_config->base);
  4754. intel_ddi_set_pipe_settings(pipe_config);
  4755. if (!transcoder_is_dsi(cpu_transcoder))
  4756. intel_ddi_enable_transcoder_func(pipe_config);
  4757. if (dev_priv->display.initial_watermarks != NULL)
  4758. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4759. if (INTEL_GEN(dev_priv) >= 11)
  4760. icl_pipe_mbus_enable(intel_crtc);
  4761. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4762. if (!transcoder_is_dsi(cpu_transcoder))
  4763. intel_enable_pipe(pipe_config);
  4764. if (intel_crtc->config->has_pch_encoder)
  4765. lpt_pch_enable(pipe_config);
  4766. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4767. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4768. assert_vblank_disabled(crtc);
  4769. drm_crtc_vblank_on(crtc);
  4770. intel_encoders_enable(crtc, pipe_config, old_state);
  4771. if (psl_clkgate_wa) {
  4772. intel_wait_for_vblank(dev_priv, pipe);
  4773. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4774. }
  4775. /* If we change the relative order between pipe/planes enabling, we need
  4776. * to change the workaround. */
  4777. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4778. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4779. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4780. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4781. }
  4782. }
  4783. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4784. {
  4785. struct drm_device *dev = crtc->base.dev;
  4786. struct drm_i915_private *dev_priv = to_i915(dev);
  4787. int pipe = crtc->pipe;
  4788. /* To avoid upsetting the power well on haswell only disable the pfit if
  4789. * it's in use. The hw state code will make sure we get this right. */
  4790. if (force || crtc->config->pch_pfit.enabled) {
  4791. I915_WRITE(PF_CTL(pipe), 0);
  4792. I915_WRITE(PF_WIN_POS(pipe), 0);
  4793. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4794. }
  4795. }
  4796. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4797. struct drm_atomic_state *old_state)
  4798. {
  4799. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4800. struct drm_device *dev = crtc->dev;
  4801. struct drm_i915_private *dev_priv = to_i915(dev);
  4802. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4803. int pipe = intel_crtc->pipe;
  4804. /*
  4805. * Sometimes spurious CPU pipe underruns happen when the
  4806. * pipe is already disabled, but FDI RX/TX is still enabled.
  4807. * Happens at least with VGA+HDMI cloning. Suppress them.
  4808. */
  4809. if (intel_crtc->config->has_pch_encoder) {
  4810. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4811. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4812. }
  4813. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4814. drm_crtc_vblank_off(crtc);
  4815. assert_vblank_disabled(crtc);
  4816. intel_disable_pipe(old_crtc_state);
  4817. ironlake_pfit_disable(intel_crtc, false);
  4818. if (intel_crtc->config->has_pch_encoder)
  4819. ironlake_fdi_disable(crtc);
  4820. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4821. if (intel_crtc->config->has_pch_encoder) {
  4822. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4823. if (HAS_PCH_CPT(dev_priv)) {
  4824. i915_reg_t reg;
  4825. u32 temp;
  4826. /* disable TRANS_DP_CTL */
  4827. reg = TRANS_DP_CTL(pipe);
  4828. temp = I915_READ(reg);
  4829. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4830. TRANS_DP_PORT_SEL_MASK);
  4831. temp |= TRANS_DP_PORT_SEL_NONE;
  4832. I915_WRITE(reg, temp);
  4833. /* disable DPLL_SEL */
  4834. temp = I915_READ(PCH_DPLL_SEL);
  4835. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4836. I915_WRITE(PCH_DPLL_SEL, temp);
  4837. }
  4838. ironlake_fdi_pll_disable(intel_crtc);
  4839. }
  4840. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4841. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4842. }
  4843. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4844. struct drm_atomic_state *old_state)
  4845. {
  4846. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4847. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4849. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4850. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4851. drm_crtc_vblank_off(crtc);
  4852. assert_vblank_disabled(crtc);
  4853. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4854. if (!transcoder_is_dsi(cpu_transcoder))
  4855. intel_disable_pipe(old_crtc_state);
  4856. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4857. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4858. if (!transcoder_is_dsi(cpu_transcoder))
  4859. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4860. if (INTEL_GEN(dev_priv) >= 9)
  4861. skylake_scaler_disable(intel_crtc);
  4862. else
  4863. ironlake_pfit_disable(intel_crtc, false);
  4864. if (!transcoder_is_dsi(cpu_transcoder))
  4865. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4866. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4867. }
  4868. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4869. {
  4870. struct drm_device *dev = crtc->base.dev;
  4871. struct drm_i915_private *dev_priv = to_i915(dev);
  4872. struct intel_crtc_state *pipe_config = crtc->config;
  4873. if (!pipe_config->gmch_pfit.control)
  4874. return;
  4875. /*
  4876. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4877. * according to register description and PRM.
  4878. */
  4879. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4880. assert_pipe_disabled(dev_priv, crtc->pipe);
  4881. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4882. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4883. /* Border color in case we don't scale up to the full screen. Black by
  4884. * default, change to something else for debugging. */
  4885. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4886. }
  4887. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4888. {
  4889. switch (port) {
  4890. case PORT_A:
  4891. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4892. case PORT_B:
  4893. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4894. case PORT_C:
  4895. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4896. case PORT_D:
  4897. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4898. case PORT_E:
  4899. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4900. case PORT_F:
  4901. return POWER_DOMAIN_PORT_DDI_F_LANES;
  4902. default:
  4903. MISSING_CASE(port);
  4904. return POWER_DOMAIN_PORT_OTHER;
  4905. }
  4906. }
  4907. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4908. struct intel_crtc_state *crtc_state)
  4909. {
  4910. struct drm_device *dev = crtc->dev;
  4911. struct drm_i915_private *dev_priv = to_i915(dev);
  4912. struct drm_encoder *encoder;
  4913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4914. enum pipe pipe = intel_crtc->pipe;
  4915. u64 mask;
  4916. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4917. if (!crtc_state->base.active)
  4918. return 0;
  4919. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  4920. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  4921. if (crtc_state->pch_pfit.enabled ||
  4922. crtc_state->pch_pfit.force_thru)
  4923. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4924. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4925. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4926. mask |= BIT_ULL(intel_encoder->power_domain);
  4927. }
  4928. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4929. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  4930. if (crtc_state->shared_dpll)
  4931. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4932. return mask;
  4933. }
  4934. static u64
  4935. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4936. struct intel_crtc_state *crtc_state)
  4937. {
  4938. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4939. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4940. enum intel_display_power_domain domain;
  4941. u64 domains, new_domains, old_domains;
  4942. old_domains = intel_crtc->enabled_power_domains;
  4943. intel_crtc->enabled_power_domains = new_domains =
  4944. get_crtc_power_domains(crtc, crtc_state);
  4945. domains = new_domains & ~old_domains;
  4946. for_each_power_domain(domain, domains)
  4947. intel_display_power_get(dev_priv, domain);
  4948. return old_domains & ~new_domains;
  4949. }
  4950. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4951. u64 domains)
  4952. {
  4953. enum intel_display_power_domain domain;
  4954. for_each_power_domain(domain, domains)
  4955. intel_display_power_put(dev_priv, domain);
  4956. }
  4957. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4958. struct drm_atomic_state *old_state)
  4959. {
  4960. struct intel_atomic_state *old_intel_state =
  4961. to_intel_atomic_state(old_state);
  4962. struct drm_crtc *crtc = pipe_config->base.crtc;
  4963. struct drm_device *dev = crtc->dev;
  4964. struct drm_i915_private *dev_priv = to_i915(dev);
  4965. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4966. int pipe = intel_crtc->pipe;
  4967. if (WARN_ON(intel_crtc->active))
  4968. return;
  4969. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4970. intel_dp_set_m_n(intel_crtc, M1_N1);
  4971. intel_set_pipe_timings(intel_crtc);
  4972. intel_set_pipe_src_size(intel_crtc);
  4973. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4974. struct drm_i915_private *dev_priv = to_i915(dev);
  4975. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4976. I915_WRITE(CHV_CANVAS(pipe), 0);
  4977. }
  4978. i9xx_set_pipeconf(intel_crtc);
  4979. intel_crtc->active = true;
  4980. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4981. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4982. if (IS_CHERRYVIEW(dev_priv)) {
  4983. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4984. chv_enable_pll(intel_crtc, intel_crtc->config);
  4985. } else {
  4986. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4987. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4988. }
  4989. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4990. i9xx_pfit_enable(intel_crtc);
  4991. intel_color_load_luts(&pipe_config->base);
  4992. dev_priv->display.initial_watermarks(old_intel_state,
  4993. pipe_config);
  4994. intel_enable_pipe(pipe_config);
  4995. assert_vblank_disabled(crtc);
  4996. drm_crtc_vblank_on(crtc);
  4997. intel_encoders_enable(crtc, pipe_config, old_state);
  4998. }
  4999. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5000. {
  5001. struct drm_device *dev = crtc->base.dev;
  5002. struct drm_i915_private *dev_priv = to_i915(dev);
  5003. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5004. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5005. }
  5006. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5007. struct drm_atomic_state *old_state)
  5008. {
  5009. struct intel_atomic_state *old_intel_state =
  5010. to_intel_atomic_state(old_state);
  5011. struct drm_crtc *crtc = pipe_config->base.crtc;
  5012. struct drm_device *dev = crtc->dev;
  5013. struct drm_i915_private *dev_priv = to_i915(dev);
  5014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5015. enum pipe pipe = intel_crtc->pipe;
  5016. if (WARN_ON(intel_crtc->active))
  5017. return;
  5018. i9xx_set_pll_dividers(intel_crtc);
  5019. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5020. intel_dp_set_m_n(intel_crtc, M1_N1);
  5021. intel_set_pipe_timings(intel_crtc);
  5022. intel_set_pipe_src_size(intel_crtc);
  5023. i9xx_set_pipeconf(intel_crtc);
  5024. intel_crtc->active = true;
  5025. if (!IS_GEN2(dev_priv))
  5026. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5027. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5028. i9xx_enable_pll(intel_crtc, pipe_config);
  5029. i9xx_pfit_enable(intel_crtc);
  5030. intel_color_load_luts(&pipe_config->base);
  5031. if (dev_priv->display.initial_watermarks != NULL)
  5032. dev_priv->display.initial_watermarks(old_intel_state,
  5033. intel_crtc->config);
  5034. else
  5035. intel_update_watermarks(intel_crtc);
  5036. intel_enable_pipe(pipe_config);
  5037. assert_vblank_disabled(crtc);
  5038. drm_crtc_vblank_on(crtc);
  5039. intel_encoders_enable(crtc, pipe_config, old_state);
  5040. }
  5041. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5042. {
  5043. struct drm_device *dev = crtc->base.dev;
  5044. struct drm_i915_private *dev_priv = to_i915(dev);
  5045. if (!crtc->config->gmch_pfit.control)
  5046. return;
  5047. assert_pipe_disabled(dev_priv, crtc->pipe);
  5048. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5049. I915_READ(PFIT_CONTROL));
  5050. I915_WRITE(PFIT_CONTROL, 0);
  5051. }
  5052. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5053. struct drm_atomic_state *old_state)
  5054. {
  5055. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5056. struct drm_device *dev = crtc->dev;
  5057. struct drm_i915_private *dev_priv = to_i915(dev);
  5058. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5059. int pipe = intel_crtc->pipe;
  5060. /*
  5061. * On gen2 planes are double buffered but the pipe isn't, so we must
  5062. * wait for planes to fully turn off before disabling the pipe.
  5063. */
  5064. if (IS_GEN2(dev_priv))
  5065. intel_wait_for_vblank(dev_priv, pipe);
  5066. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5067. drm_crtc_vblank_off(crtc);
  5068. assert_vblank_disabled(crtc);
  5069. intel_disable_pipe(old_crtc_state);
  5070. i9xx_pfit_disable(intel_crtc);
  5071. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5072. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5073. if (IS_CHERRYVIEW(dev_priv))
  5074. chv_disable_pll(dev_priv, pipe);
  5075. else if (IS_VALLEYVIEW(dev_priv))
  5076. vlv_disable_pll(dev_priv, pipe);
  5077. else
  5078. i9xx_disable_pll(intel_crtc);
  5079. }
  5080. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5081. if (!IS_GEN2(dev_priv))
  5082. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5083. if (!dev_priv->display.initial_watermarks)
  5084. intel_update_watermarks(intel_crtc);
  5085. /* clock the pipe down to 640x480@60 to potentially save power */
  5086. if (IS_I830(dev_priv))
  5087. i830_enable_pipe(dev_priv, pipe);
  5088. }
  5089. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5090. struct drm_modeset_acquire_ctx *ctx)
  5091. {
  5092. struct intel_encoder *encoder;
  5093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5094. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5095. enum intel_display_power_domain domain;
  5096. struct intel_plane *plane;
  5097. u64 domains;
  5098. struct drm_atomic_state *state;
  5099. struct intel_crtc_state *crtc_state;
  5100. int ret;
  5101. if (!intel_crtc->active)
  5102. return;
  5103. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5104. const struct intel_plane_state *plane_state =
  5105. to_intel_plane_state(plane->base.state);
  5106. if (plane_state->base.visible)
  5107. intel_plane_disable_noatomic(intel_crtc, plane);
  5108. }
  5109. state = drm_atomic_state_alloc(crtc->dev);
  5110. if (!state) {
  5111. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5112. crtc->base.id, crtc->name);
  5113. return;
  5114. }
  5115. state->acquire_ctx = ctx;
  5116. /* Everything's already locked, -EDEADLK can't happen. */
  5117. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5118. ret = drm_atomic_add_affected_connectors(state, crtc);
  5119. WARN_ON(IS_ERR(crtc_state) || ret);
  5120. dev_priv->display.crtc_disable(crtc_state, state);
  5121. drm_atomic_state_put(state);
  5122. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5123. crtc->base.id, crtc->name);
  5124. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5125. crtc->state->active = false;
  5126. intel_crtc->active = false;
  5127. crtc->enabled = false;
  5128. crtc->state->connector_mask = 0;
  5129. crtc->state->encoder_mask = 0;
  5130. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5131. encoder->base.crtc = NULL;
  5132. intel_fbc_disable(intel_crtc);
  5133. intel_update_watermarks(intel_crtc);
  5134. intel_disable_shared_dpll(intel_crtc);
  5135. domains = intel_crtc->enabled_power_domains;
  5136. for_each_power_domain(domain, domains)
  5137. intel_display_power_put(dev_priv, domain);
  5138. intel_crtc->enabled_power_domains = 0;
  5139. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5140. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5141. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5142. }
  5143. /*
  5144. * turn all crtc's off, but do not adjust state
  5145. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5146. */
  5147. int intel_display_suspend(struct drm_device *dev)
  5148. {
  5149. struct drm_i915_private *dev_priv = to_i915(dev);
  5150. struct drm_atomic_state *state;
  5151. int ret;
  5152. state = drm_atomic_helper_suspend(dev);
  5153. ret = PTR_ERR_OR_ZERO(state);
  5154. if (ret)
  5155. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5156. else
  5157. dev_priv->modeset_restore_state = state;
  5158. return ret;
  5159. }
  5160. void intel_encoder_destroy(struct drm_encoder *encoder)
  5161. {
  5162. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5163. drm_encoder_cleanup(encoder);
  5164. kfree(intel_encoder);
  5165. }
  5166. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5167. * internal consistency). */
  5168. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5169. struct drm_connector_state *conn_state)
  5170. {
  5171. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5172. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5173. connector->base.base.id,
  5174. connector->base.name);
  5175. if (connector->get_hw_state(connector)) {
  5176. struct intel_encoder *encoder = connector->encoder;
  5177. I915_STATE_WARN(!crtc_state,
  5178. "connector enabled without attached crtc\n");
  5179. if (!crtc_state)
  5180. return;
  5181. I915_STATE_WARN(!crtc_state->active,
  5182. "connector is active, but attached crtc isn't\n");
  5183. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5184. return;
  5185. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5186. "atomic encoder doesn't match attached encoder\n");
  5187. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5188. "attached encoder crtc differs from connector crtc\n");
  5189. } else {
  5190. I915_STATE_WARN(crtc_state && crtc_state->active,
  5191. "attached crtc is active, but connector isn't\n");
  5192. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5193. "best encoder set without crtc!\n");
  5194. }
  5195. }
  5196. int intel_connector_init(struct intel_connector *connector)
  5197. {
  5198. struct intel_digital_connector_state *conn_state;
  5199. /*
  5200. * Allocate enough memory to hold intel_digital_connector_state,
  5201. * This might be a few bytes too many, but for connectors that don't
  5202. * need it we'll free the state and allocate a smaller one on the first
  5203. * succesful commit anyway.
  5204. */
  5205. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5206. if (!conn_state)
  5207. return -ENOMEM;
  5208. __drm_atomic_helper_connector_reset(&connector->base,
  5209. &conn_state->base);
  5210. return 0;
  5211. }
  5212. struct intel_connector *intel_connector_alloc(void)
  5213. {
  5214. struct intel_connector *connector;
  5215. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5216. if (!connector)
  5217. return NULL;
  5218. if (intel_connector_init(connector) < 0) {
  5219. kfree(connector);
  5220. return NULL;
  5221. }
  5222. return connector;
  5223. }
  5224. /*
  5225. * Free the bits allocated by intel_connector_alloc.
  5226. * This should only be used after intel_connector_alloc has returned
  5227. * successfully, and before drm_connector_init returns successfully.
  5228. * Otherwise the destroy callbacks for the connector and the state should
  5229. * take care of proper cleanup/free
  5230. */
  5231. void intel_connector_free(struct intel_connector *connector)
  5232. {
  5233. kfree(to_intel_digital_connector_state(connector->base.state));
  5234. kfree(connector);
  5235. }
  5236. /* Simple connector->get_hw_state implementation for encoders that support only
  5237. * one connector and no cloning and hence the encoder state determines the state
  5238. * of the connector. */
  5239. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5240. {
  5241. enum pipe pipe = 0;
  5242. struct intel_encoder *encoder = connector->encoder;
  5243. return encoder->get_hw_state(encoder, &pipe);
  5244. }
  5245. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5246. {
  5247. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5248. return crtc_state->fdi_lanes;
  5249. return 0;
  5250. }
  5251. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5252. struct intel_crtc_state *pipe_config)
  5253. {
  5254. struct drm_i915_private *dev_priv = to_i915(dev);
  5255. struct drm_atomic_state *state = pipe_config->base.state;
  5256. struct intel_crtc *other_crtc;
  5257. struct intel_crtc_state *other_crtc_state;
  5258. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5259. pipe_name(pipe), pipe_config->fdi_lanes);
  5260. if (pipe_config->fdi_lanes > 4) {
  5261. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5262. pipe_name(pipe), pipe_config->fdi_lanes);
  5263. return -EINVAL;
  5264. }
  5265. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5266. if (pipe_config->fdi_lanes > 2) {
  5267. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5268. pipe_config->fdi_lanes);
  5269. return -EINVAL;
  5270. } else {
  5271. return 0;
  5272. }
  5273. }
  5274. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5275. return 0;
  5276. /* Ivybridge 3 pipe is really complicated */
  5277. switch (pipe) {
  5278. case PIPE_A:
  5279. return 0;
  5280. case PIPE_B:
  5281. if (pipe_config->fdi_lanes <= 2)
  5282. return 0;
  5283. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5284. other_crtc_state =
  5285. intel_atomic_get_crtc_state(state, other_crtc);
  5286. if (IS_ERR(other_crtc_state))
  5287. return PTR_ERR(other_crtc_state);
  5288. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5289. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5290. pipe_name(pipe), pipe_config->fdi_lanes);
  5291. return -EINVAL;
  5292. }
  5293. return 0;
  5294. case PIPE_C:
  5295. if (pipe_config->fdi_lanes > 2) {
  5296. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5297. pipe_name(pipe), pipe_config->fdi_lanes);
  5298. return -EINVAL;
  5299. }
  5300. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5301. other_crtc_state =
  5302. intel_atomic_get_crtc_state(state, other_crtc);
  5303. if (IS_ERR(other_crtc_state))
  5304. return PTR_ERR(other_crtc_state);
  5305. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5306. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5307. return -EINVAL;
  5308. }
  5309. return 0;
  5310. default:
  5311. BUG();
  5312. }
  5313. }
  5314. #define RETRY 1
  5315. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5316. struct intel_crtc_state *pipe_config)
  5317. {
  5318. struct drm_device *dev = intel_crtc->base.dev;
  5319. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5320. int lane, link_bw, fdi_dotclock, ret;
  5321. bool needs_recompute = false;
  5322. retry:
  5323. /* FDI is a binary signal running at ~2.7GHz, encoding
  5324. * each output octet as 10 bits. The actual frequency
  5325. * is stored as a divider into a 100MHz clock, and the
  5326. * mode pixel clock is stored in units of 1KHz.
  5327. * Hence the bw of each lane in terms of the mode signal
  5328. * is:
  5329. */
  5330. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5331. fdi_dotclock = adjusted_mode->crtc_clock;
  5332. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5333. pipe_config->pipe_bpp);
  5334. pipe_config->fdi_lanes = lane;
  5335. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5336. link_bw, &pipe_config->fdi_m_n, false);
  5337. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5338. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5339. pipe_config->pipe_bpp -= 2*3;
  5340. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5341. pipe_config->pipe_bpp);
  5342. needs_recompute = true;
  5343. pipe_config->bw_constrained = true;
  5344. goto retry;
  5345. }
  5346. if (needs_recompute)
  5347. return RETRY;
  5348. return ret;
  5349. }
  5350. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5351. {
  5352. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5353. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5354. /* IPS only exists on ULT machines and is tied to pipe A. */
  5355. if (!hsw_crtc_supports_ips(crtc))
  5356. return false;
  5357. if (!i915_modparams.enable_ips)
  5358. return false;
  5359. if (crtc_state->pipe_bpp > 24)
  5360. return false;
  5361. /*
  5362. * We compare against max which means we must take
  5363. * the increased cdclk requirement into account when
  5364. * calculating the new cdclk.
  5365. *
  5366. * Should measure whether using a lower cdclk w/o IPS
  5367. */
  5368. if (IS_BROADWELL(dev_priv) &&
  5369. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5370. return false;
  5371. return true;
  5372. }
  5373. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5374. {
  5375. struct drm_i915_private *dev_priv =
  5376. to_i915(crtc_state->base.crtc->dev);
  5377. struct intel_atomic_state *intel_state =
  5378. to_intel_atomic_state(crtc_state->base.state);
  5379. if (!hsw_crtc_state_ips_capable(crtc_state))
  5380. return false;
  5381. if (crtc_state->ips_force_disable)
  5382. return false;
  5383. /* IPS should be fine as long as at least one plane is enabled. */
  5384. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5385. return false;
  5386. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5387. if (IS_BROADWELL(dev_priv) &&
  5388. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5389. return false;
  5390. return true;
  5391. }
  5392. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5393. {
  5394. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5395. /* GDG double wide on either pipe, otherwise pipe A only */
  5396. return INTEL_GEN(dev_priv) < 4 &&
  5397. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5398. }
  5399. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5400. {
  5401. uint32_t pixel_rate;
  5402. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5403. /*
  5404. * We only use IF-ID interlacing. If we ever use
  5405. * PF-ID we'll need to adjust the pixel_rate here.
  5406. */
  5407. if (pipe_config->pch_pfit.enabled) {
  5408. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5409. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5410. pipe_w = pipe_config->pipe_src_w;
  5411. pipe_h = pipe_config->pipe_src_h;
  5412. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5413. pfit_h = pfit_size & 0xFFFF;
  5414. if (pipe_w < pfit_w)
  5415. pipe_w = pfit_w;
  5416. if (pipe_h < pfit_h)
  5417. pipe_h = pfit_h;
  5418. if (WARN_ON(!pfit_w || !pfit_h))
  5419. return pixel_rate;
  5420. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5421. pfit_w * pfit_h);
  5422. }
  5423. return pixel_rate;
  5424. }
  5425. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5426. {
  5427. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5428. if (HAS_GMCH_DISPLAY(dev_priv))
  5429. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5430. crtc_state->pixel_rate =
  5431. crtc_state->base.adjusted_mode.crtc_clock;
  5432. else
  5433. crtc_state->pixel_rate =
  5434. ilk_pipe_pixel_rate(crtc_state);
  5435. }
  5436. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5437. struct intel_crtc_state *pipe_config)
  5438. {
  5439. struct drm_device *dev = crtc->base.dev;
  5440. struct drm_i915_private *dev_priv = to_i915(dev);
  5441. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5442. int clock_limit = dev_priv->max_dotclk_freq;
  5443. if (INTEL_GEN(dev_priv) < 4) {
  5444. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5445. /*
  5446. * Enable double wide mode when the dot clock
  5447. * is > 90% of the (display) core speed.
  5448. */
  5449. if (intel_crtc_supports_double_wide(crtc) &&
  5450. adjusted_mode->crtc_clock > clock_limit) {
  5451. clock_limit = dev_priv->max_dotclk_freq;
  5452. pipe_config->double_wide = true;
  5453. }
  5454. }
  5455. if (adjusted_mode->crtc_clock > clock_limit) {
  5456. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5457. adjusted_mode->crtc_clock, clock_limit,
  5458. yesno(pipe_config->double_wide));
  5459. return -EINVAL;
  5460. }
  5461. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5462. /*
  5463. * There is only one pipe CSC unit per pipe, and we need that
  5464. * for output conversion from RGB->YCBCR. So if CTM is already
  5465. * applied we can't support YCBCR420 output.
  5466. */
  5467. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5468. return -EINVAL;
  5469. }
  5470. /*
  5471. * Pipe horizontal size must be even in:
  5472. * - DVO ganged mode
  5473. * - LVDS dual channel mode
  5474. * - Double wide pipe
  5475. */
  5476. if (pipe_config->pipe_src_w & 1) {
  5477. if (pipe_config->double_wide) {
  5478. DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
  5479. return -EINVAL;
  5480. }
  5481. if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5482. intel_is_dual_link_lvds(dev)) {
  5483. DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
  5484. return -EINVAL;
  5485. }
  5486. }
  5487. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5488. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5489. */
  5490. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5491. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5492. return -EINVAL;
  5493. intel_crtc_compute_pixel_rate(pipe_config);
  5494. if (pipe_config->has_pch_encoder)
  5495. return ironlake_fdi_compute_config(crtc, pipe_config);
  5496. return 0;
  5497. }
  5498. static void
  5499. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5500. {
  5501. while (*num > DATA_LINK_M_N_MASK ||
  5502. *den > DATA_LINK_M_N_MASK) {
  5503. *num >>= 1;
  5504. *den >>= 1;
  5505. }
  5506. }
  5507. static void compute_m_n(unsigned int m, unsigned int n,
  5508. uint32_t *ret_m, uint32_t *ret_n,
  5509. bool reduce_m_n)
  5510. {
  5511. /*
  5512. * Reduce M/N as much as possible without loss in precision. Several DP
  5513. * dongles in particular seem to be fussy about too large *link* M/N
  5514. * values. The passed in values are more likely to have the least
  5515. * significant bits zero than M after rounding below, so do this first.
  5516. */
  5517. if (reduce_m_n) {
  5518. while ((m & 1) == 0 && (n & 1) == 0) {
  5519. m >>= 1;
  5520. n >>= 1;
  5521. }
  5522. }
  5523. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5524. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5525. intel_reduce_m_n_ratio(ret_m, ret_n);
  5526. }
  5527. void
  5528. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5529. int pixel_clock, int link_clock,
  5530. struct intel_link_m_n *m_n,
  5531. bool reduce_m_n)
  5532. {
  5533. m_n->tu = 64;
  5534. compute_m_n(bits_per_pixel * pixel_clock,
  5535. link_clock * nlanes * 8,
  5536. &m_n->gmch_m, &m_n->gmch_n,
  5537. reduce_m_n);
  5538. compute_m_n(pixel_clock, link_clock,
  5539. &m_n->link_m, &m_n->link_n,
  5540. reduce_m_n);
  5541. }
  5542. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5543. {
  5544. if (i915_modparams.panel_use_ssc >= 0)
  5545. return i915_modparams.panel_use_ssc != 0;
  5546. return dev_priv->vbt.lvds_use_ssc
  5547. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5548. }
  5549. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5550. {
  5551. return (1 << dpll->n) << 16 | dpll->m2;
  5552. }
  5553. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5554. {
  5555. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5556. }
  5557. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5558. struct intel_crtc_state *crtc_state,
  5559. struct dpll *reduced_clock)
  5560. {
  5561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5562. u32 fp, fp2 = 0;
  5563. if (IS_PINEVIEW(dev_priv)) {
  5564. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5565. if (reduced_clock)
  5566. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5567. } else {
  5568. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5569. if (reduced_clock)
  5570. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5571. }
  5572. crtc_state->dpll_hw_state.fp0 = fp;
  5573. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5574. reduced_clock) {
  5575. crtc_state->dpll_hw_state.fp1 = fp2;
  5576. } else {
  5577. crtc_state->dpll_hw_state.fp1 = fp;
  5578. }
  5579. }
  5580. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5581. pipe)
  5582. {
  5583. u32 reg_val;
  5584. /*
  5585. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5586. * and set it to a reasonable value instead.
  5587. */
  5588. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5589. reg_val &= 0xffffff00;
  5590. reg_val |= 0x00000030;
  5591. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5592. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5593. reg_val &= 0x00ffffff;
  5594. reg_val |= 0x8c000000;
  5595. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5596. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5597. reg_val &= 0xffffff00;
  5598. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5599. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5600. reg_val &= 0x00ffffff;
  5601. reg_val |= 0xb0000000;
  5602. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5603. }
  5604. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5605. struct intel_link_m_n *m_n)
  5606. {
  5607. struct drm_device *dev = crtc->base.dev;
  5608. struct drm_i915_private *dev_priv = to_i915(dev);
  5609. int pipe = crtc->pipe;
  5610. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5611. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5612. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5613. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5614. }
  5615. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5616. struct intel_link_m_n *m_n,
  5617. struct intel_link_m_n *m2_n2)
  5618. {
  5619. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5620. int pipe = crtc->pipe;
  5621. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5622. if (INTEL_GEN(dev_priv) >= 5) {
  5623. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5624. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5625. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5626. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5627. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5628. * for gen < 8) and if DRRS is supported (to make sure the
  5629. * registers are not unnecessarily accessed).
  5630. */
  5631. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5632. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5633. I915_WRITE(PIPE_DATA_M2(transcoder),
  5634. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5635. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5636. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5637. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5638. }
  5639. } else {
  5640. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5641. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5642. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5643. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5644. }
  5645. }
  5646. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5647. {
  5648. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5649. if (m_n == M1_N1) {
  5650. dp_m_n = &crtc->config->dp_m_n;
  5651. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5652. } else if (m_n == M2_N2) {
  5653. /*
  5654. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5655. * needs to be programmed into M1_N1.
  5656. */
  5657. dp_m_n = &crtc->config->dp_m2_n2;
  5658. } else {
  5659. DRM_ERROR("Unsupported divider value\n");
  5660. return;
  5661. }
  5662. if (crtc->config->has_pch_encoder)
  5663. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5664. else
  5665. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5666. }
  5667. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5668. struct intel_crtc_state *pipe_config)
  5669. {
  5670. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5671. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5672. if (crtc->pipe != PIPE_A)
  5673. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5674. /* DPLL not used with DSI, but still need the rest set up */
  5675. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5676. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5677. DPLL_EXT_BUFFER_ENABLE_VLV;
  5678. pipe_config->dpll_hw_state.dpll_md =
  5679. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5680. }
  5681. static void chv_compute_dpll(struct intel_crtc *crtc,
  5682. struct intel_crtc_state *pipe_config)
  5683. {
  5684. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5685. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5686. if (crtc->pipe != PIPE_A)
  5687. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5688. /* DPLL not used with DSI, but still need the rest set up */
  5689. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5690. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5691. pipe_config->dpll_hw_state.dpll_md =
  5692. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5693. }
  5694. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5695. const struct intel_crtc_state *pipe_config)
  5696. {
  5697. struct drm_device *dev = crtc->base.dev;
  5698. struct drm_i915_private *dev_priv = to_i915(dev);
  5699. enum pipe pipe = crtc->pipe;
  5700. u32 mdiv;
  5701. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5702. u32 coreclk, reg_val;
  5703. /* Enable Refclk */
  5704. I915_WRITE(DPLL(pipe),
  5705. pipe_config->dpll_hw_state.dpll &
  5706. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5707. /* No need to actually set up the DPLL with DSI */
  5708. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5709. return;
  5710. mutex_lock(&dev_priv->sb_lock);
  5711. bestn = pipe_config->dpll.n;
  5712. bestm1 = pipe_config->dpll.m1;
  5713. bestm2 = pipe_config->dpll.m2;
  5714. bestp1 = pipe_config->dpll.p1;
  5715. bestp2 = pipe_config->dpll.p2;
  5716. /* See eDP HDMI DPIO driver vbios notes doc */
  5717. /* PLL B needs special handling */
  5718. if (pipe == PIPE_B)
  5719. vlv_pllb_recal_opamp(dev_priv, pipe);
  5720. /* Set up Tx target for periodic Rcomp update */
  5721. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5722. /* Disable target IRef on PLL */
  5723. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5724. reg_val &= 0x00ffffff;
  5725. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5726. /* Disable fast lock */
  5727. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5728. /* Set idtafcrecal before PLL is enabled */
  5729. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5730. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5731. mdiv |= ((bestn << DPIO_N_SHIFT));
  5732. mdiv |= (1 << DPIO_K_SHIFT);
  5733. /*
  5734. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5735. * but we don't support that).
  5736. * Note: don't use the DAC post divider as it seems unstable.
  5737. */
  5738. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5739. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5740. mdiv |= DPIO_ENABLE_CALIBRATION;
  5741. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5742. /* Set HBR and RBR LPF coefficients */
  5743. if (pipe_config->port_clock == 162000 ||
  5744. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5745. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5746. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5747. 0x009f0003);
  5748. else
  5749. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5750. 0x00d0000f);
  5751. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5752. /* Use SSC source */
  5753. if (pipe == PIPE_A)
  5754. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5755. 0x0df40000);
  5756. else
  5757. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5758. 0x0df70000);
  5759. } else { /* HDMI or VGA */
  5760. /* Use bend source */
  5761. if (pipe == PIPE_A)
  5762. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5763. 0x0df70000);
  5764. else
  5765. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5766. 0x0df40000);
  5767. }
  5768. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5769. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5770. if (intel_crtc_has_dp_encoder(crtc->config))
  5771. coreclk |= 0x01000000;
  5772. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5773. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5774. mutex_unlock(&dev_priv->sb_lock);
  5775. }
  5776. static void chv_prepare_pll(struct intel_crtc *crtc,
  5777. const struct intel_crtc_state *pipe_config)
  5778. {
  5779. struct drm_device *dev = crtc->base.dev;
  5780. struct drm_i915_private *dev_priv = to_i915(dev);
  5781. enum pipe pipe = crtc->pipe;
  5782. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5783. u32 loopfilter, tribuf_calcntr;
  5784. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5785. u32 dpio_val;
  5786. int vco;
  5787. /* Enable Refclk and SSC */
  5788. I915_WRITE(DPLL(pipe),
  5789. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5790. /* No need to actually set up the DPLL with DSI */
  5791. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5792. return;
  5793. bestn = pipe_config->dpll.n;
  5794. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5795. bestm1 = pipe_config->dpll.m1;
  5796. bestm2 = pipe_config->dpll.m2 >> 22;
  5797. bestp1 = pipe_config->dpll.p1;
  5798. bestp2 = pipe_config->dpll.p2;
  5799. vco = pipe_config->dpll.vco;
  5800. dpio_val = 0;
  5801. loopfilter = 0;
  5802. mutex_lock(&dev_priv->sb_lock);
  5803. /* p1 and p2 divider */
  5804. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5805. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5806. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5807. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5808. 1 << DPIO_CHV_K_DIV_SHIFT);
  5809. /* Feedback post-divider - m2 */
  5810. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5811. /* Feedback refclk divider - n and m1 */
  5812. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5813. DPIO_CHV_M1_DIV_BY_2 |
  5814. 1 << DPIO_CHV_N_DIV_SHIFT);
  5815. /* M2 fraction division */
  5816. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5817. /* M2 fraction division enable */
  5818. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5819. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5820. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5821. if (bestm2_frac)
  5822. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5823. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5824. /* Program digital lock detect threshold */
  5825. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5826. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5827. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5828. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5829. if (!bestm2_frac)
  5830. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5831. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5832. /* Loop filter */
  5833. if (vco == 5400000) {
  5834. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5835. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5836. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5837. tribuf_calcntr = 0x9;
  5838. } else if (vco <= 6200000) {
  5839. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5840. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5841. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5842. tribuf_calcntr = 0x9;
  5843. } else if (vco <= 6480000) {
  5844. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5845. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5846. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5847. tribuf_calcntr = 0x8;
  5848. } else {
  5849. /* Not supported. Apply the same limits as in the max case */
  5850. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5851. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5852. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5853. tribuf_calcntr = 0;
  5854. }
  5855. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5856. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5857. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5858. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5859. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5860. /* AFC Recal */
  5861. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5862. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5863. DPIO_AFC_RECAL);
  5864. mutex_unlock(&dev_priv->sb_lock);
  5865. }
  5866. /**
  5867. * vlv_force_pll_on - forcibly enable just the PLL
  5868. * @dev_priv: i915 private structure
  5869. * @pipe: pipe PLL to enable
  5870. * @dpll: PLL configuration
  5871. *
  5872. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5873. * in cases where we need the PLL enabled even when @pipe is not going to
  5874. * be enabled.
  5875. */
  5876. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5877. const struct dpll *dpll)
  5878. {
  5879. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5880. struct intel_crtc_state *pipe_config;
  5881. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5882. if (!pipe_config)
  5883. return -ENOMEM;
  5884. pipe_config->base.crtc = &crtc->base;
  5885. pipe_config->pixel_multiplier = 1;
  5886. pipe_config->dpll = *dpll;
  5887. if (IS_CHERRYVIEW(dev_priv)) {
  5888. chv_compute_dpll(crtc, pipe_config);
  5889. chv_prepare_pll(crtc, pipe_config);
  5890. chv_enable_pll(crtc, pipe_config);
  5891. } else {
  5892. vlv_compute_dpll(crtc, pipe_config);
  5893. vlv_prepare_pll(crtc, pipe_config);
  5894. vlv_enable_pll(crtc, pipe_config);
  5895. }
  5896. kfree(pipe_config);
  5897. return 0;
  5898. }
  5899. /**
  5900. * vlv_force_pll_off - forcibly disable just the PLL
  5901. * @dev_priv: i915 private structure
  5902. * @pipe: pipe PLL to disable
  5903. *
  5904. * Disable the PLL for @pipe. To be used in cases where we need
  5905. * the PLL enabled even when @pipe is not going to be enabled.
  5906. */
  5907. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5908. {
  5909. if (IS_CHERRYVIEW(dev_priv))
  5910. chv_disable_pll(dev_priv, pipe);
  5911. else
  5912. vlv_disable_pll(dev_priv, pipe);
  5913. }
  5914. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5915. struct intel_crtc_state *crtc_state,
  5916. struct dpll *reduced_clock)
  5917. {
  5918. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5919. u32 dpll;
  5920. struct dpll *clock = &crtc_state->dpll;
  5921. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5922. dpll = DPLL_VGA_MODE_DIS;
  5923. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5924. dpll |= DPLLB_MODE_LVDS;
  5925. else
  5926. dpll |= DPLLB_MODE_DAC_SERIAL;
  5927. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5928. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5929. dpll |= (crtc_state->pixel_multiplier - 1)
  5930. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5931. }
  5932. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5933. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5934. dpll |= DPLL_SDVO_HIGH_SPEED;
  5935. if (intel_crtc_has_dp_encoder(crtc_state))
  5936. dpll |= DPLL_SDVO_HIGH_SPEED;
  5937. /* compute bitmask from p1 value */
  5938. if (IS_PINEVIEW(dev_priv))
  5939. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5940. else {
  5941. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5942. if (IS_G4X(dev_priv) && reduced_clock)
  5943. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5944. }
  5945. switch (clock->p2) {
  5946. case 5:
  5947. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5948. break;
  5949. case 7:
  5950. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5951. break;
  5952. case 10:
  5953. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5954. break;
  5955. case 14:
  5956. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5957. break;
  5958. }
  5959. if (INTEL_GEN(dev_priv) >= 4)
  5960. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5961. if (crtc_state->sdvo_tv_clock)
  5962. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5963. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5964. intel_panel_use_ssc(dev_priv))
  5965. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5966. else
  5967. dpll |= PLL_REF_INPUT_DREFCLK;
  5968. dpll |= DPLL_VCO_ENABLE;
  5969. crtc_state->dpll_hw_state.dpll = dpll;
  5970. if (INTEL_GEN(dev_priv) >= 4) {
  5971. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5972. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5973. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5974. }
  5975. }
  5976. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5977. struct intel_crtc_state *crtc_state,
  5978. struct dpll *reduced_clock)
  5979. {
  5980. struct drm_device *dev = crtc->base.dev;
  5981. struct drm_i915_private *dev_priv = to_i915(dev);
  5982. u32 dpll;
  5983. struct dpll *clock = &crtc_state->dpll;
  5984. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5985. dpll = DPLL_VGA_MODE_DIS;
  5986. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5987. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5988. } else {
  5989. if (clock->p1 == 2)
  5990. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5991. else
  5992. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5993. if (clock->p2 == 4)
  5994. dpll |= PLL_P2_DIVIDE_BY_4;
  5995. }
  5996. if (!IS_I830(dev_priv) &&
  5997. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5998. dpll |= DPLL_DVO_2X_MODE;
  5999. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6000. intel_panel_use_ssc(dev_priv))
  6001. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6002. else
  6003. dpll |= PLL_REF_INPUT_DREFCLK;
  6004. dpll |= DPLL_VCO_ENABLE;
  6005. crtc_state->dpll_hw_state.dpll = dpll;
  6006. }
  6007. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6008. {
  6009. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6010. enum pipe pipe = intel_crtc->pipe;
  6011. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6012. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6013. uint32_t crtc_vtotal, crtc_vblank_end;
  6014. int vsyncshift = 0;
  6015. /* We need to be careful not to changed the adjusted mode, for otherwise
  6016. * the hw state checker will get angry at the mismatch. */
  6017. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6018. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6019. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6020. /* the chip adds 2 halflines automatically */
  6021. crtc_vtotal -= 1;
  6022. crtc_vblank_end -= 1;
  6023. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6024. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6025. else
  6026. vsyncshift = adjusted_mode->crtc_hsync_start -
  6027. adjusted_mode->crtc_htotal / 2;
  6028. if (vsyncshift < 0)
  6029. vsyncshift += adjusted_mode->crtc_htotal;
  6030. }
  6031. if (INTEL_GEN(dev_priv) > 3)
  6032. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6033. I915_WRITE(HTOTAL(cpu_transcoder),
  6034. (adjusted_mode->crtc_hdisplay - 1) |
  6035. ((adjusted_mode->crtc_htotal - 1) << 16));
  6036. I915_WRITE(HBLANK(cpu_transcoder),
  6037. (adjusted_mode->crtc_hblank_start - 1) |
  6038. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6039. I915_WRITE(HSYNC(cpu_transcoder),
  6040. (adjusted_mode->crtc_hsync_start - 1) |
  6041. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6042. I915_WRITE(VTOTAL(cpu_transcoder),
  6043. (adjusted_mode->crtc_vdisplay - 1) |
  6044. ((crtc_vtotal - 1) << 16));
  6045. I915_WRITE(VBLANK(cpu_transcoder),
  6046. (adjusted_mode->crtc_vblank_start - 1) |
  6047. ((crtc_vblank_end - 1) << 16));
  6048. I915_WRITE(VSYNC(cpu_transcoder),
  6049. (adjusted_mode->crtc_vsync_start - 1) |
  6050. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6051. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6052. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6053. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6054. * bits. */
  6055. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6056. (pipe == PIPE_B || pipe == PIPE_C))
  6057. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6058. }
  6059. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6060. {
  6061. struct drm_device *dev = intel_crtc->base.dev;
  6062. struct drm_i915_private *dev_priv = to_i915(dev);
  6063. enum pipe pipe = intel_crtc->pipe;
  6064. /* pipesrc controls the size that is scaled from, which should
  6065. * always be the user's requested size.
  6066. */
  6067. I915_WRITE(PIPESRC(pipe),
  6068. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6069. (intel_crtc->config->pipe_src_h - 1));
  6070. }
  6071. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6072. struct intel_crtc_state *pipe_config)
  6073. {
  6074. struct drm_device *dev = crtc->base.dev;
  6075. struct drm_i915_private *dev_priv = to_i915(dev);
  6076. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6077. uint32_t tmp;
  6078. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6079. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6080. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6081. tmp = I915_READ(HBLANK(cpu_transcoder));
  6082. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6083. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6084. tmp = I915_READ(HSYNC(cpu_transcoder));
  6085. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6086. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6087. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6088. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6089. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6090. tmp = I915_READ(VBLANK(cpu_transcoder));
  6091. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6092. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6093. tmp = I915_READ(VSYNC(cpu_transcoder));
  6094. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6095. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6096. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6097. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6098. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6099. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6100. }
  6101. }
  6102. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6103. struct intel_crtc_state *pipe_config)
  6104. {
  6105. struct drm_device *dev = crtc->base.dev;
  6106. struct drm_i915_private *dev_priv = to_i915(dev);
  6107. u32 tmp;
  6108. tmp = I915_READ(PIPESRC(crtc->pipe));
  6109. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6110. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6111. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6112. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6113. }
  6114. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6115. struct intel_crtc_state *pipe_config)
  6116. {
  6117. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6118. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6119. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6120. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6121. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6122. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6123. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6124. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6125. mode->flags = pipe_config->base.adjusted_mode.flags;
  6126. mode->type = DRM_MODE_TYPE_DRIVER;
  6127. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6128. mode->hsync = drm_mode_hsync(mode);
  6129. mode->vrefresh = drm_mode_vrefresh(mode);
  6130. drm_mode_set_name(mode);
  6131. }
  6132. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6133. {
  6134. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6135. uint32_t pipeconf;
  6136. pipeconf = 0;
  6137. /* we keep both pipes enabled on 830 */
  6138. if (IS_I830(dev_priv))
  6139. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6140. if (intel_crtc->config->double_wide)
  6141. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6142. /* only g4x and later have fancy bpc/dither controls */
  6143. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6144. IS_CHERRYVIEW(dev_priv)) {
  6145. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6146. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6147. pipeconf |= PIPECONF_DITHER_EN |
  6148. PIPECONF_DITHER_TYPE_SP;
  6149. switch (intel_crtc->config->pipe_bpp) {
  6150. case 18:
  6151. pipeconf |= PIPECONF_6BPC;
  6152. break;
  6153. case 24:
  6154. pipeconf |= PIPECONF_8BPC;
  6155. break;
  6156. case 30:
  6157. pipeconf |= PIPECONF_10BPC;
  6158. break;
  6159. default:
  6160. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6161. BUG();
  6162. }
  6163. }
  6164. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6165. if (INTEL_GEN(dev_priv) < 4 ||
  6166. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6167. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6168. else
  6169. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6170. } else
  6171. pipeconf |= PIPECONF_PROGRESSIVE;
  6172. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6173. intel_crtc->config->limited_color_range)
  6174. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6175. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6176. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6177. }
  6178. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6179. struct intel_crtc_state *crtc_state)
  6180. {
  6181. struct drm_device *dev = crtc->base.dev;
  6182. struct drm_i915_private *dev_priv = to_i915(dev);
  6183. const struct intel_limit *limit;
  6184. int refclk = 48000;
  6185. memset(&crtc_state->dpll_hw_state, 0,
  6186. sizeof(crtc_state->dpll_hw_state));
  6187. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6188. if (intel_panel_use_ssc(dev_priv)) {
  6189. refclk = dev_priv->vbt.lvds_ssc_freq;
  6190. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6191. }
  6192. limit = &intel_limits_i8xx_lvds;
  6193. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6194. limit = &intel_limits_i8xx_dvo;
  6195. } else {
  6196. limit = &intel_limits_i8xx_dac;
  6197. }
  6198. if (!crtc_state->clock_set &&
  6199. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6200. refclk, NULL, &crtc_state->dpll)) {
  6201. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6202. return -EINVAL;
  6203. }
  6204. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6205. return 0;
  6206. }
  6207. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6208. struct intel_crtc_state *crtc_state)
  6209. {
  6210. struct drm_device *dev = crtc->base.dev;
  6211. struct drm_i915_private *dev_priv = to_i915(dev);
  6212. const struct intel_limit *limit;
  6213. int refclk = 96000;
  6214. memset(&crtc_state->dpll_hw_state, 0,
  6215. sizeof(crtc_state->dpll_hw_state));
  6216. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6217. if (intel_panel_use_ssc(dev_priv)) {
  6218. refclk = dev_priv->vbt.lvds_ssc_freq;
  6219. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6220. }
  6221. if (intel_is_dual_link_lvds(dev))
  6222. limit = &intel_limits_g4x_dual_channel_lvds;
  6223. else
  6224. limit = &intel_limits_g4x_single_channel_lvds;
  6225. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6226. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6227. limit = &intel_limits_g4x_hdmi;
  6228. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6229. limit = &intel_limits_g4x_sdvo;
  6230. } else {
  6231. /* The option is for other outputs */
  6232. limit = &intel_limits_i9xx_sdvo;
  6233. }
  6234. if (!crtc_state->clock_set &&
  6235. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6236. refclk, NULL, &crtc_state->dpll)) {
  6237. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6238. return -EINVAL;
  6239. }
  6240. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6241. return 0;
  6242. }
  6243. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6244. struct intel_crtc_state *crtc_state)
  6245. {
  6246. struct drm_device *dev = crtc->base.dev;
  6247. struct drm_i915_private *dev_priv = to_i915(dev);
  6248. const struct intel_limit *limit;
  6249. int refclk = 96000;
  6250. memset(&crtc_state->dpll_hw_state, 0,
  6251. sizeof(crtc_state->dpll_hw_state));
  6252. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6253. if (intel_panel_use_ssc(dev_priv)) {
  6254. refclk = dev_priv->vbt.lvds_ssc_freq;
  6255. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6256. }
  6257. limit = &intel_limits_pineview_lvds;
  6258. } else {
  6259. limit = &intel_limits_pineview_sdvo;
  6260. }
  6261. if (!crtc_state->clock_set &&
  6262. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6263. refclk, NULL, &crtc_state->dpll)) {
  6264. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6265. return -EINVAL;
  6266. }
  6267. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6268. return 0;
  6269. }
  6270. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6271. struct intel_crtc_state *crtc_state)
  6272. {
  6273. struct drm_device *dev = crtc->base.dev;
  6274. struct drm_i915_private *dev_priv = to_i915(dev);
  6275. const struct intel_limit *limit;
  6276. int refclk = 96000;
  6277. memset(&crtc_state->dpll_hw_state, 0,
  6278. sizeof(crtc_state->dpll_hw_state));
  6279. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6280. if (intel_panel_use_ssc(dev_priv)) {
  6281. refclk = dev_priv->vbt.lvds_ssc_freq;
  6282. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6283. }
  6284. limit = &intel_limits_i9xx_lvds;
  6285. } else {
  6286. limit = &intel_limits_i9xx_sdvo;
  6287. }
  6288. if (!crtc_state->clock_set &&
  6289. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6290. refclk, NULL, &crtc_state->dpll)) {
  6291. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6292. return -EINVAL;
  6293. }
  6294. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6295. return 0;
  6296. }
  6297. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6298. struct intel_crtc_state *crtc_state)
  6299. {
  6300. int refclk = 100000;
  6301. const struct intel_limit *limit = &intel_limits_chv;
  6302. memset(&crtc_state->dpll_hw_state, 0,
  6303. sizeof(crtc_state->dpll_hw_state));
  6304. if (!crtc_state->clock_set &&
  6305. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6306. refclk, NULL, &crtc_state->dpll)) {
  6307. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6308. return -EINVAL;
  6309. }
  6310. chv_compute_dpll(crtc, crtc_state);
  6311. return 0;
  6312. }
  6313. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6314. struct intel_crtc_state *crtc_state)
  6315. {
  6316. int refclk = 100000;
  6317. const struct intel_limit *limit = &intel_limits_vlv;
  6318. memset(&crtc_state->dpll_hw_state, 0,
  6319. sizeof(crtc_state->dpll_hw_state));
  6320. if (!crtc_state->clock_set &&
  6321. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6322. refclk, NULL, &crtc_state->dpll)) {
  6323. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6324. return -EINVAL;
  6325. }
  6326. vlv_compute_dpll(crtc, crtc_state);
  6327. return 0;
  6328. }
  6329. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6330. struct intel_crtc_state *pipe_config)
  6331. {
  6332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6333. uint32_t tmp;
  6334. if (INTEL_GEN(dev_priv) <= 3 &&
  6335. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6336. return;
  6337. tmp = I915_READ(PFIT_CONTROL);
  6338. if (!(tmp & PFIT_ENABLE))
  6339. return;
  6340. /* Check whether the pfit is attached to our pipe. */
  6341. if (INTEL_GEN(dev_priv) < 4) {
  6342. if (crtc->pipe != PIPE_B)
  6343. return;
  6344. } else {
  6345. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6346. return;
  6347. }
  6348. pipe_config->gmch_pfit.control = tmp;
  6349. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6350. }
  6351. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6352. struct intel_crtc_state *pipe_config)
  6353. {
  6354. struct drm_device *dev = crtc->base.dev;
  6355. struct drm_i915_private *dev_priv = to_i915(dev);
  6356. int pipe = pipe_config->cpu_transcoder;
  6357. struct dpll clock;
  6358. u32 mdiv;
  6359. int refclk = 100000;
  6360. /* In case of DSI, DPLL will not be used */
  6361. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6362. return;
  6363. mutex_lock(&dev_priv->sb_lock);
  6364. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6365. mutex_unlock(&dev_priv->sb_lock);
  6366. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6367. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6368. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6369. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6370. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6371. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6372. }
  6373. static void
  6374. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6375. struct intel_initial_plane_config *plane_config)
  6376. {
  6377. struct drm_device *dev = crtc->base.dev;
  6378. struct drm_i915_private *dev_priv = to_i915(dev);
  6379. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6380. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6381. enum pipe pipe = crtc->pipe;
  6382. u32 val, base, offset;
  6383. int fourcc, pixel_format;
  6384. unsigned int aligned_height;
  6385. struct drm_framebuffer *fb;
  6386. struct intel_framebuffer *intel_fb;
  6387. if (!plane->get_hw_state(plane))
  6388. return;
  6389. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6390. if (!intel_fb) {
  6391. DRM_DEBUG_KMS("failed to alloc fb\n");
  6392. return;
  6393. }
  6394. fb = &intel_fb->base;
  6395. fb->dev = dev;
  6396. val = I915_READ(DSPCNTR(i9xx_plane));
  6397. if (INTEL_GEN(dev_priv) >= 4) {
  6398. if (val & DISPPLANE_TILED) {
  6399. plane_config->tiling = I915_TILING_X;
  6400. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6401. }
  6402. }
  6403. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6404. fourcc = i9xx_format_to_fourcc(pixel_format);
  6405. fb->format = drm_format_info(fourcc);
  6406. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6407. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6408. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6409. } else if (INTEL_GEN(dev_priv) >= 4) {
  6410. if (plane_config->tiling)
  6411. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6412. else
  6413. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6414. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6415. } else {
  6416. base = I915_READ(DSPADDR(i9xx_plane));
  6417. }
  6418. plane_config->base = base;
  6419. val = I915_READ(PIPESRC(pipe));
  6420. fb->width = ((val >> 16) & 0xfff) + 1;
  6421. fb->height = ((val >> 0) & 0xfff) + 1;
  6422. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6423. fb->pitches[0] = val & 0xffffffc0;
  6424. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6425. plane_config->size = fb->pitches[0] * aligned_height;
  6426. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6427. crtc->base.name, plane->base.name, fb->width, fb->height,
  6428. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6429. plane_config->size);
  6430. plane_config->fb = intel_fb;
  6431. }
  6432. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6433. struct intel_crtc_state *pipe_config)
  6434. {
  6435. struct drm_device *dev = crtc->base.dev;
  6436. struct drm_i915_private *dev_priv = to_i915(dev);
  6437. int pipe = pipe_config->cpu_transcoder;
  6438. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6439. struct dpll clock;
  6440. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6441. int refclk = 100000;
  6442. /* In case of DSI, DPLL will not be used */
  6443. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6444. return;
  6445. mutex_lock(&dev_priv->sb_lock);
  6446. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6447. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6448. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6449. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6450. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6451. mutex_unlock(&dev_priv->sb_lock);
  6452. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6453. clock.m2 = (pll_dw0 & 0xff) << 22;
  6454. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6455. clock.m2 |= pll_dw2 & 0x3fffff;
  6456. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6457. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6458. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6459. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6460. }
  6461. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6462. struct intel_crtc_state *pipe_config)
  6463. {
  6464. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6465. enum intel_display_power_domain power_domain;
  6466. uint32_t tmp;
  6467. bool ret;
  6468. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6469. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6470. return false;
  6471. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6472. pipe_config->shared_dpll = NULL;
  6473. ret = false;
  6474. tmp = I915_READ(PIPECONF(crtc->pipe));
  6475. if (!(tmp & PIPECONF_ENABLE))
  6476. goto out;
  6477. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6478. IS_CHERRYVIEW(dev_priv)) {
  6479. switch (tmp & PIPECONF_BPC_MASK) {
  6480. case PIPECONF_6BPC:
  6481. pipe_config->pipe_bpp = 18;
  6482. break;
  6483. case PIPECONF_8BPC:
  6484. pipe_config->pipe_bpp = 24;
  6485. break;
  6486. case PIPECONF_10BPC:
  6487. pipe_config->pipe_bpp = 30;
  6488. break;
  6489. default:
  6490. break;
  6491. }
  6492. }
  6493. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6494. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6495. pipe_config->limited_color_range = true;
  6496. if (INTEL_GEN(dev_priv) < 4)
  6497. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6498. intel_get_pipe_timings(crtc, pipe_config);
  6499. intel_get_pipe_src_size(crtc, pipe_config);
  6500. i9xx_get_pfit_config(crtc, pipe_config);
  6501. if (INTEL_GEN(dev_priv) >= 4) {
  6502. /* No way to read it out on pipes B and C */
  6503. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6504. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6505. else
  6506. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6507. pipe_config->pixel_multiplier =
  6508. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6509. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6510. pipe_config->dpll_hw_state.dpll_md = tmp;
  6511. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6512. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6513. tmp = I915_READ(DPLL(crtc->pipe));
  6514. pipe_config->pixel_multiplier =
  6515. ((tmp & SDVO_MULTIPLIER_MASK)
  6516. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6517. } else {
  6518. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6519. * port and will be fixed up in the encoder->get_config
  6520. * function. */
  6521. pipe_config->pixel_multiplier = 1;
  6522. }
  6523. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6524. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6525. /*
  6526. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6527. * on 830. Filter it out here so that we don't
  6528. * report errors due to that.
  6529. */
  6530. if (IS_I830(dev_priv))
  6531. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6532. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6533. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6534. } else {
  6535. /* Mask out read-only status bits. */
  6536. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6537. DPLL_PORTC_READY_MASK |
  6538. DPLL_PORTB_READY_MASK);
  6539. }
  6540. if (IS_CHERRYVIEW(dev_priv))
  6541. chv_crtc_clock_get(crtc, pipe_config);
  6542. else if (IS_VALLEYVIEW(dev_priv))
  6543. vlv_crtc_clock_get(crtc, pipe_config);
  6544. else
  6545. i9xx_crtc_clock_get(crtc, pipe_config);
  6546. /*
  6547. * Normally the dotclock is filled in by the encoder .get_config()
  6548. * but in case the pipe is enabled w/o any ports we need a sane
  6549. * default.
  6550. */
  6551. pipe_config->base.adjusted_mode.crtc_clock =
  6552. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6553. ret = true;
  6554. out:
  6555. intel_display_power_put(dev_priv, power_domain);
  6556. return ret;
  6557. }
  6558. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6559. {
  6560. struct intel_encoder *encoder;
  6561. int i;
  6562. u32 val, final;
  6563. bool has_lvds = false;
  6564. bool has_cpu_edp = false;
  6565. bool has_panel = false;
  6566. bool has_ck505 = false;
  6567. bool can_ssc = false;
  6568. bool using_ssc_source = false;
  6569. /* We need to take the global config into account */
  6570. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6571. switch (encoder->type) {
  6572. case INTEL_OUTPUT_LVDS:
  6573. has_panel = true;
  6574. has_lvds = true;
  6575. break;
  6576. case INTEL_OUTPUT_EDP:
  6577. has_panel = true;
  6578. if (encoder->port == PORT_A)
  6579. has_cpu_edp = true;
  6580. break;
  6581. default:
  6582. break;
  6583. }
  6584. }
  6585. if (HAS_PCH_IBX(dev_priv)) {
  6586. has_ck505 = dev_priv->vbt.display_clock_mode;
  6587. can_ssc = has_ck505;
  6588. } else {
  6589. has_ck505 = false;
  6590. can_ssc = true;
  6591. }
  6592. /* Check if any DPLLs are using the SSC source */
  6593. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6594. u32 temp = I915_READ(PCH_DPLL(i));
  6595. if (!(temp & DPLL_VCO_ENABLE))
  6596. continue;
  6597. if ((temp & PLL_REF_INPUT_MASK) ==
  6598. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6599. using_ssc_source = true;
  6600. break;
  6601. }
  6602. }
  6603. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6604. has_panel, has_lvds, has_ck505, using_ssc_source);
  6605. /* Ironlake: try to setup display ref clock before DPLL
  6606. * enabling. This is only under driver's control after
  6607. * PCH B stepping, previous chipset stepping should be
  6608. * ignoring this setting.
  6609. */
  6610. val = I915_READ(PCH_DREF_CONTROL);
  6611. /* As we must carefully and slowly disable/enable each source in turn,
  6612. * compute the final state we want first and check if we need to
  6613. * make any changes at all.
  6614. */
  6615. final = val;
  6616. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6617. if (has_ck505)
  6618. final |= DREF_NONSPREAD_CK505_ENABLE;
  6619. else
  6620. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6621. final &= ~DREF_SSC_SOURCE_MASK;
  6622. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6623. final &= ~DREF_SSC1_ENABLE;
  6624. if (has_panel) {
  6625. final |= DREF_SSC_SOURCE_ENABLE;
  6626. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6627. final |= DREF_SSC1_ENABLE;
  6628. if (has_cpu_edp) {
  6629. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6630. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6631. else
  6632. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6633. } else
  6634. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6635. } else if (using_ssc_source) {
  6636. final |= DREF_SSC_SOURCE_ENABLE;
  6637. final |= DREF_SSC1_ENABLE;
  6638. }
  6639. if (final == val)
  6640. return;
  6641. /* Always enable nonspread source */
  6642. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6643. if (has_ck505)
  6644. val |= DREF_NONSPREAD_CK505_ENABLE;
  6645. else
  6646. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6647. if (has_panel) {
  6648. val &= ~DREF_SSC_SOURCE_MASK;
  6649. val |= DREF_SSC_SOURCE_ENABLE;
  6650. /* SSC must be turned on before enabling the CPU output */
  6651. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6652. DRM_DEBUG_KMS("Using SSC on panel\n");
  6653. val |= DREF_SSC1_ENABLE;
  6654. } else
  6655. val &= ~DREF_SSC1_ENABLE;
  6656. /* Get SSC going before enabling the outputs */
  6657. I915_WRITE(PCH_DREF_CONTROL, val);
  6658. POSTING_READ(PCH_DREF_CONTROL);
  6659. udelay(200);
  6660. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6661. /* Enable CPU source on CPU attached eDP */
  6662. if (has_cpu_edp) {
  6663. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6664. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6665. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6666. } else
  6667. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6668. } else
  6669. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6670. I915_WRITE(PCH_DREF_CONTROL, val);
  6671. POSTING_READ(PCH_DREF_CONTROL);
  6672. udelay(200);
  6673. } else {
  6674. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6675. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6676. /* Turn off CPU output */
  6677. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6678. I915_WRITE(PCH_DREF_CONTROL, val);
  6679. POSTING_READ(PCH_DREF_CONTROL);
  6680. udelay(200);
  6681. if (!using_ssc_source) {
  6682. DRM_DEBUG_KMS("Disabling SSC source\n");
  6683. /* Turn off the SSC source */
  6684. val &= ~DREF_SSC_SOURCE_MASK;
  6685. val |= DREF_SSC_SOURCE_DISABLE;
  6686. /* Turn off SSC1 */
  6687. val &= ~DREF_SSC1_ENABLE;
  6688. I915_WRITE(PCH_DREF_CONTROL, val);
  6689. POSTING_READ(PCH_DREF_CONTROL);
  6690. udelay(200);
  6691. }
  6692. }
  6693. BUG_ON(val != final);
  6694. }
  6695. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6696. {
  6697. uint32_t tmp;
  6698. tmp = I915_READ(SOUTH_CHICKEN2);
  6699. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6700. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6701. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6702. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6703. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6704. tmp = I915_READ(SOUTH_CHICKEN2);
  6705. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6706. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6707. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6708. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6709. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6710. }
  6711. /* WaMPhyProgramming:hsw */
  6712. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6713. {
  6714. uint32_t tmp;
  6715. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6716. tmp &= ~(0xFF << 24);
  6717. tmp |= (0x12 << 24);
  6718. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6719. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6720. tmp |= (1 << 11);
  6721. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6722. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6723. tmp |= (1 << 11);
  6724. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6725. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6726. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6727. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6728. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6729. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6730. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6731. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6732. tmp &= ~(7 << 13);
  6733. tmp |= (5 << 13);
  6734. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6735. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6736. tmp &= ~(7 << 13);
  6737. tmp |= (5 << 13);
  6738. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6739. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6740. tmp &= ~0xFF;
  6741. tmp |= 0x1C;
  6742. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6743. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6744. tmp &= ~0xFF;
  6745. tmp |= 0x1C;
  6746. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6747. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6748. tmp &= ~(0xFF << 16);
  6749. tmp |= (0x1C << 16);
  6750. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6751. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6752. tmp &= ~(0xFF << 16);
  6753. tmp |= (0x1C << 16);
  6754. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6755. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6756. tmp |= (1 << 27);
  6757. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6758. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6759. tmp |= (1 << 27);
  6760. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6761. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6762. tmp &= ~(0xF << 28);
  6763. tmp |= (4 << 28);
  6764. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6765. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6766. tmp &= ~(0xF << 28);
  6767. tmp |= (4 << 28);
  6768. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6769. }
  6770. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6771. * Programming" based on the parameters passed:
  6772. * - Sequence to enable CLKOUT_DP
  6773. * - Sequence to enable CLKOUT_DP without spread
  6774. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6775. */
  6776. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6777. bool with_spread, bool with_fdi)
  6778. {
  6779. uint32_t reg, tmp;
  6780. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6781. with_spread = true;
  6782. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6783. with_fdi, "LP PCH doesn't have FDI\n"))
  6784. with_fdi = false;
  6785. mutex_lock(&dev_priv->sb_lock);
  6786. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6787. tmp &= ~SBI_SSCCTL_DISABLE;
  6788. tmp |= SBI_SSCCTL_PATHALT;
  6789. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6790. udelay(24);
  6791. if (with_spread) {
  6792. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6793. tmp &= ~SBI_SSCCTL_PATHALT;
  6794. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6795. if (with_fdi) {
  6796. lpt_reset_fdi_mphy(dev_priv);
  6797. lpt_program_fdi_mphy(dev_priv);
  6798. }
  6799. }
  6800. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6801. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6802. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6803. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6804. mutex_unlock(&dev_priv->sb_lock);
  6805. }
  6806. /* Sequence to disable CLKOUT_DP */
  6807. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6808. {
  6809. uint32_t reg, tmp;
  6810. mutex_lock(&dev_priv->sb_lock);
  6811. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6812. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6813. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6814. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6815. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6816. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6817. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6818. tmp |= SBI_SSCCTL_PATHALT;
  6819. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6820. udelay(32);
  6821. }
  6822. tmp |= SBI_SSCCTL_DISABLE;
  6823. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6824. }
  6825. mutex_unlock(&dev_priv->sb_lock);
  6826. }
  6827. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6828. static const uint16_t sscdivintphase[] = {
  6829. [BEND_IDX( 50)] = 0x3B23,
  6830. [BEND_IDX( 45)] = 0x3B23,
  6831. [BEND_IDX( 40)] = 0x3C23,
  6832. [BEND_IDX( 35)] = 0x3C23,
  6833. [BEND_IDX( 30)] = 0x3D23,
  6834. [BEND_IDX( 25)] = 0x3D23,
  6835. [BEND_IDX( 20)] = 0x3E23,
  6836. [BEND_IDX( 15)] = 0x3E23,
  6837. [BEND_IDX( 10)] = 0x3F23,
  6838. [BEND_IDX( 5)] = 0x3F23,
  6839. [BEND_IDX( 0)] = 0x0025,
  6840. [BEND_IDX( -5)] = 0x0025,
  6841. [BEND_IDX(-10)] = 0x0125,
  6842. [BEND_IDX(-15)] = 0x0125,
  6843. [BEND_IDX(-20)] = 0x0225,
  6844. [BEND_IDX(-25)] = 0x0225,
  6845. [BEND_IDX(-30)] = 0x0325,
  6846. [BEND_IDX(-35)] = 0x0325,
  6847. [BEND_IDX(-40)] = 0x0425,
  6848. [BEND_IDX(-45)] = 0x0425,
  6849. [BEND_IDX(-50)] = 0x0525,
  6850. };
  6851. /*
  6852. * Bend CLKOUT_DP
  6853. * steps -50 to 50 inclusive, in steps of 5
  6854. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6855. * change in clock period = -(steps / 10) * 5.787 ps
  6856. */
  6857. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6858. {
  6859. uint32_t tmp;
  6860. int idx = BEND_IDX(steps);
  6861. if (WARN_ON(steps % 5 != 0))
  6862. return;
  6863. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6864. return;
  6865. mutex_lock(&dev_priv->sb_lock);
  6866. if (steps % 10 != 0)
  6867. tmp = 0xAAAAAAAB;
  6868. else
  6869. tmp = 0x00000000;
  6870. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6871. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6872. tmp &= 0xffff0000;
  6873. tmp |= sscdivintphase[idx];
  6874. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6875. mutex_unlock(&dev_priv->sb_lock);
  6876. }
  6877. #undef BEND_IDX
  6878. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6879. {
  6880. struct intel_encoder *encoder;
  6881. bool has_vga = false;
  6882. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6883. switch (encoder->type) {
  6884. case INTEL_OUTPUT_ANALOG:
  6885. has_vga = true;
  6886. break;
  6887. default:
  6888. break;
  6889. }
  6890. }
  6891. if (has_vga) {
  6892. lpt_bend_clkout_dp(dev_priv, 0);
  6893. lpt_enable_clkout_dp(dev_priv, true, true);
  6894. } else {
  6895. lpt_disable_clkout_dp(dev_priv);
  6896. }
  6897. }
  6898. /*
  6899. * Initialize reference clocks when the driver loads
  6900. */
  6901. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6902. {
  6903. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6904. ironlake_init_pch_refclk(dev_priv);
  6905. else if (HAS_PCH_LPT(dev_priv))
  6906. lpt_init_pch_refclk(dev_priv);
  6907. }
  6908. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6909. {
  6910. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6912. int pipe = intel_crtc->pipe;
  6913. uint32_t val;
  6914. val = 0;
  6915. switch (intel_crtc->config->pipe_bpp) {
  6916. case 18:
  6917. val |= PIPECONF_6BPC;
  6918. break;
  6919. case 24:
  6920. val |= PIPECONF_8BPC;
  6921. break;
  6922. case 30:
  6923. val |= PIPECONF_10BPC;
  6924. break;
  6925. case 36:
  6926. val |= PIPECONF_12BPC;
  6927. break;
  6928. default:
  6929. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6930. BUG();
  6931. }
  6932. if (intel_crtc->config->dither)
  6933. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6934. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6935. val |= PIPECONF_INTERLACED_ILK;
  6936. else
  6937. val |= PIPECONF_PROGRESSIVE;
  6938. if (intel_crtc->config->limited_color_range)
  6939. val |= PIPECONF_COLOR_RANGE_SELECT;
  6940. I915_WRITE(PIPECONF(pipe), val);
  6941. POSTING_READ(PIPECONF(pipe));
  6942. }
  6943. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6944. {
  6945. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6947. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6948. u32 val = 0;
  6949. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6950. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6951. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6952. val |= PIPECONF_INTERLACED_ILK;
  6953. else
  6954. val |= PIPECONF_PROGRESSIVE;
  6955. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6956. POSTING_READ(PIPECONF(cpu_transcoder));
  6957. }
  6958. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6959. {
  6960. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6961. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6962. struct intel_crtc_state *config = intel_crtc->config;
  6963. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  6964. u32 val = 0;
  6965. switch (intel_crtc->config->pipe_bpp) {
  6966. case 18:
  6967. val |= PIPEMISC_DITHER_6_BPC;
  6968. break;
  6969. case 24:
  6970. val |= PIPEMISC_DITHER_8_BPC;
  6971. break;
  6972. case 30:
  6973. val |= PIPEMISC_DITHER_10_BPC;
  6974. break;
  6975. case 36:
  6976. val |= PIPEMISC_DITHER_12_BPC;
  6977. break;
  6978. default:
  6979. /* Case prevented by pipe_config_set_bpp. */
  6980. BUG();
  6981. }
  6982. if (intel_crtc->config->dither)
  6983. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6984. if (config->ycbcr420) {
  6985. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6986. PIPEMISC_YUV420_ENABLE |
  6987. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6988. }
  6989. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6990. }
  6991. }
  6992. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6993. {
  6994. /*
  6995. * Account for spread spectrum to avoid
  6996. * oversubscribing the link. Max center spread
  6997. * is 2.5%; use 5% for safety's sake.
  6998. */
  6999. u32 bps = target_clock * bpp * 21 / 20;
  7000. return DIV_ROUND_UP(bps, link_bw * 8);
  7001. }
  7002. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7003. {
  7004. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7005. }
  7006. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7007. struct intel_crtc_state *crtc_state,
  7008. struct dpll *reduced_clock)
  7009. {
  7010. struct drm_crtc *crtc = &intel_crtc->base;
  7011. struct drm_device *dev = crtc->dev;
  7012. struct drm_i915_private *dev_priv = to_i915(dev);
  7013. u32 dpll, fp, fp2;
  7014. int factor;
  7015. /* Enable autotuning of the PLL clock (if permissible) */
  7016. factor = 21;
  7017. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7018. if ((intel_panel_use_ssc(dev_priv) &&
  7019. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7020. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7021. factor = 25;
  7022. } else if (crtc_state->sdvo_tv_clock)
  7023. factor = 20;
  7024. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7025. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7026. fp |= FP_CB_TUNE;
  7027. if (reduced_clock) {
  7028. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7029. if (reduced_clock->m < factor * reduced_clock->n)
  7030. fp2 |= FP_CB_TUNE;
  7031. } else {
  7032. fp2 = fp;
  7033. }
  7034. dpll = 0;
  7035. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7036. dpll |= DPLLB_MODE_LVDS;
  7037. else
  7038. dpll |= DPLLB_MODE_DAC_SERIAL;
  7039. dpll |= (crtc_state->pixel_multiplier - 1)
  7040. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7041. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7042. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7043. dpll |= DPLL_SDVO_HIGH_SPEED;
  7044. if (intel_crtc_has_dp_encoder(crtc_state))
  7045. dpll |= DPLL_SDVO_HIGH_SPEED;
  7046. /*
  7047. * The high speed IO clock is only really required for
  7048. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7049. * possible to share the DPLL between CRT and HDMI. Enabling
  7050. * the clock needlessly does no real harm, except use up a
  7051. * bit of power potentially.
  7052. *
  7053. * We'll limit this to IVB with 3 pipes, since it has only two
  7054. * DPLLs and so DPLL sharing is the only way to get three pipes
  7055. * driving PCH ports at the same time. On SNB we could do this,
  7056. * and potentially avoid enabling the second DPLL, but it's not
  7057. * clear if it''s a win or loss power wise. No point in doing
  7058. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7059. */
  7060. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7061. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7062. dpll |= DPLL_SDVO_HIGH_SPEED;
  7063. /* compute bitmask from p1 value */
  7064. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7065. /* also FPA1 */
  7066. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7067. switch (crtc_state->dpll.p2) {
  7068. case 5:
  7069. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7070. break;
  7071. case 7:
  7072. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7073. break;
  7074. case 10:
  7075. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7076. break;
  7077. case 14:
  7078. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7079. break;
  7080. }
  7081. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7082. intel_panel_use_ssc(dev_priv))
  7083. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7084. else
  7085. dpll |= PLL_REF_INPUT_DREFCLK;
  7086. dpll |= DPLL_VCO_ENABLE;
  7087. crtc_state->dpll_hw_state.dpll = dpll;
  7088. crtc_state->dpll_hw_state.fp0 = fp;
  7089. crtc_state->dpll_hw_state.fp1 = fp2;
  7090. }
  7091. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7092. struct intel_crtc_state *crtc_state)
  7093. {
  7094. struct drm_device *dev = crtc->base.dev;
  7095. struct drm_i915_private *dev_priv = to_i915(dev);
  7096. const struct intel_limit *limit;
  7097. int refclk = 120000;
  7098. memset(&crtc_state->dpll_hw_state, 0,
  7099. sizeof(crtc_state->dpll_hw_state));
  7100. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7101. if (!crtc_state->has_pch_encoder)
  7102. return 0;
  7103. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7104. if (intel_panel_use_ssc(dev_priv)) {
  7105. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7106. dev_priv->vbt.lvds_ssc_freq);
  7107. refclk = dev_priv->vbt.lvds_ssc_freq;
  7108. }
  7109. if (intel_is_dual_link_lvds(dev)) {
  7110. if (refclk == 100000)
  7111. limit = &intel_limits_ironlake_dual_lvds_100m;
  7112. else
  7113. limit = &intel_limits_ironlake_dual_lvds;
  7114. } else {
  7115. if (refclk == 100000)
  7116. limit = &intel_limits_ironlake_single_lvds_100m;
  7117. else
  7118. limit = &intel_limits_ironlake_single_lvds;
  7119. }
  7120. } else {
  7121. limit = &intel_limits_ironlake_dac;
  7122. }
  7123. if (!crtc_state->clock_set &&
  7124. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7125. refclk, NULL, &crtc_state->dpll)) {
  7126. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7127. return -EINVAL;
  7128. }
  7129. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7130. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7131. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7132. pipe_name(crtc->pipe));
  7133. return -EINVAL;
  7134. }
  7135. return 0;
  7136. }
  7137. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7138. struct intel_link_m_n *m_n)
  7139. {
  7140. struct drm_device *dev = crtc->base.dev;
  7141. struct drm_i915_private *dev_priv = to_i915(dev);
  7142. enum pipe pipe = crtc->pipe;
  7143. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7144. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7145. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7146. & ~TU_SIZE_MASK;
  7147. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7148. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7149. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7150. }
  7151. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7152. enum transcoder transcoder,
  7153. struct intel_link_m_n *m_n,
  7154. struct intel_link_m_n *m2_n2)
  7155. {
  7156. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7157. enum pipe pipe = crtc->pipe;
  7158. if (INTEL_GEN(dev_priv) >= 5) {
  7159. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7160. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7161. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7162. & ~TU_SIZE_MASK;
  7163. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7164. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7165. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7166. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7167. * gen < 8) and if DRRS is supported (to make sure the
  7168. * registers are not unnecessarily read).
  7169. */
  7170. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7171. crtc->config->has_drrs) {
  7172. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7173. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7174. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7175. & ~TU_SIZE_MASK;
  7176. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7177. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7178. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7179. }
  7180. } else {
  7181. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7182. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7183. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7184. & ~TU_SIZE_MASK;
  7185. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7186. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7187. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7188. }
  7189. }
  7190. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7191. struct intel_crtc_state *pipe_config)
  7192. {
  7193. if (pipe_config->has_pch_encoder)
  7194. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7195. else
  7196. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7197. &pipe_config->dp_m_n,
  7198. &pipe_config->dp_m2_n2);
  7199. }
  7200. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7201. struct intel_crtc_state *pipe_config)
  7202. {
  7203. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7204. &pipe_config->fdi_m_n, NULL);
  7205. }
  7206. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7207. struct intel_crtc_state *pipe_config)
  7208. {
  7209. struct drm_device *dev = crtc->base.dev;
  7210. struct drm_i915_private *dev_priv = to_i915(dev);
  7211. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7212. uint32_t ps_ctrl = 0;
  7213. int id = -1;
  7214. int i;
  7215. /* find scaler attached to this pipe */
  7216. for (i = 0; i < crtc->num_scalers; i++) {
  7217. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7218. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7219. id = i;
  7220. pipe_config->pch_pfit.enabled = true;
  7221. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7222. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7223. break;
  7224. }
  7225. }
  7226. scaler_state->scaler_id = id;
  7227. if (id >= 0) {
  7228. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7229. } else {
  7230. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7231. }
  7232. }
  7233. static void
  7234. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7235. struct intel_initial_plane_config *plane_config)
  7236. {
  7237. struct drm_device *dev = crtc->base.dev;
  7238. struct drm_i915_private *dev_priv = to_i915(dev);
  7239. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7240. enum plane_id plane_id = plane->id;
  7241. enum pipe pipe = crtc->pipe;
  7242. u32 val, base, offset, stride_mult, tiling, alpha;
  7243. int fourcc, pixel_format;
  7244. unsigned int aligned_height;
  7245. struct drm_framebuffer *fb;
  7246. struct intel_framebuffer *intel_fb;
  7247. if (!plane->get_hw_state(plane))
  7248. return;
  7249. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7250. if (!intel_fb) {
  7251. DRM_DEBUG_KMS("failed to alloc fb\n");
  7252. return;
  7253. }
  7254. fb = &intel_fb->base;
  7255. fb->dev = dev;
  7256. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7257. if (INTEL_GEN(dev_priv) >= 11)
  7258. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7259. else
  7260. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7261. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7262. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7263. alpha &= PLANE_COLOR_ALPHA_MASK;
  7264. } else {
  7265. alpha = val & PLANE_CTL_ALPHA_MASK;
  7266. }
  7267. fourcc = skl_format_to_fourcc(pixel_format,
  7268. val & PLANE_CTL_ORDER_RGBX, alpha);
  7269. fb->format = drm_format_info(fourcc);
  7270. tiling = val & PLANE_CTL_TILED_MASK;
  7271. switch (tiling) {
  7272. case PLANE_CTL_TILED_LINEAR:
  7273. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7274. break;
  7275. case PLANE_CTL_TILED_X:
  7276. plane_config->tiling = I915_TILING_X;
  7277. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7278. break;
  7279. case PLANE_CTL_TILED_Y:
  7280. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7281. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7282. else
  7283. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7284. break;
  7285. case PLANE_CTL_TILED_YF:
  7286. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7287. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7288. else
  7289. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7290. break;
  7291. default:
  7292. MISSING_CASE(tiling);
  7293. goto error;
  7294. }
  7295. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7296. plane_config->base = base;
  7297. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7298. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7299. fb->height = ((val >> 16) & 0xfff) + 1;
  7300. fb->width = ((val >> 0) & 0x1fff) + 1;
  7301. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7302. stride_mult = intel_fb_stride_alignment(fb, 0);
  7303. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7304. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7305. plane_config->size = fb->pitches[0] * aligned_height;
  7306. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7307. crtc->base.name, plane->base.name, fb->width, fb->height,
  7308. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7309. plane_config->size);
  7310. plane_config->fb = intel_fb;
  7311. return;
  7312. error:
  7313. kfree(intel_fb);
  7314. }
  7315. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7316. struct intel_crtc_state *pipe_config)
  7317. {
  7318. struct drm_device *dev = crtc->base.dev;
  7319. struct drm_i915_private *dev_priv = to_i915(dev);
  7320. uint32_t tmp;
  7321. tmp = I915_READ(PF_CTL(crtc->pipe));
  7322. if (tmp & PF_ENABLE) {
  7323. pipe_config->pch_pfit.enabled = true;
  7324. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7325. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7326. /* We currently do not free assignements of panel fitters on
  7327. * ivb/hsw (since we don't use the higher upscaling modes which
  7328. * differentiates them) so just WARN about this case for now. */
  7329. if (IS_GEN7(dev_priv)) {
  7330. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7331. PF_PIPE_SEL_IVB(crtc->pipe));
  7332. }
  7333. }
  7334. }
  7335. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7336. struct intel_crtc_state *pipe_config)
  7337. {
  7338. struct drm_device *dev = crtc->base.dev;
  7339. struct drm_i915_private *dev_priv = to_i915(dev);
  7340. enum intel_display_power_domain power_domain;
  7341. uint32_t tmp;
  7342. bool ret;
  7343. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7344. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7345. return false;
  7346. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7347. pipe_config->shared_dpll = NULL;
  7348. ret = false;
  7349. tmp = I915_READ(PIPECONF(crtc->pipe));
  7350. if (!(tmp & PIPECONF_ENABLE))
  7351. goto out;
  7352. switch (tmp & PIPECONF_BPC_MASK) {
  7353. case PIPECONF_6BPC:
  7354. pipe_config->pipe_bpp = 18;
  7355. break;
  7356. case PIPECONF_8BPC:
  7357. pipe_config->pipe_bpp = 24;
  7358. break;
  7359. case PIPECONF_10BPC:
  7360. pipe_config->pipe_bpp = 30;
  7361. break;
  7362. case PIPECONF_12BPC:
  7363. pipe_config->pipe_bpp = 36;
  7364. break;
  7365. default:
  7366. break;
  7367. }
  7368. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7369. pipe_config->limited_color_range = true;
  7370. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7371. struct intel_shared_dpll *pll;
  7372. enum intel_dpll_id pll_id;
  7373. pipe_config->has_pch_encoder = true;
  7374. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7375. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7376. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7377. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7378. if (HAS_PCH_IBX(dev_priv)) {
  7379. /*
  7380. * The pipe->pch transcoder and pch transcoder->pll
  7381. * mapping is fixed.
  7382. */
  7383. pll_id = (enum intel_dpll_id) crtc->pipe;
  7384. } else {
  7385. tmp = I915_READ(PCH_DPLL_SEL);
  7386. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7387. pll_id = DPLL_ID_PCH_PLL_B;
  7388. else
  7389. pll_id= DPLL_ID_PCH_PLL_A;
  7390. }
  7391. pipe_config->shared_dpll =
  7392. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7393. pll = pipe_config->shared_dpll;
  7394. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7395. &pipe_config->dpll_hw_state));
  7396. tmp = pipe_config->dpll_hw_state.dpll;
  7397. pipe_config->pixel_multiplier =
  7398. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7399. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7400. ironlake_pch_clock_get(crtc, pipe_config);
  7401. } else {
  7402. pipe_config->pixel_multiplier = 1;
  7403. }
  7404. intel_get_pipe_timings(crtc, pipe_config);
  7405. intel_get_pipe_src_size(crtc, pipe_config);
  7406. ironlake_get_pfit_config(crtc, pipe_config);
  7407. ret = true;
  7408. out:
  7409. intel_display_power_put(dev_priv, power_domain);
  7410. return ret;
  7411. }
  7412. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7413. {
  7414. struct drm_device *dev = &dev_priv->drm;
  7415. struct intel_crtc *crtc;
  7416. for_each_intel_crtc(dev, crtc)
  7417. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7418. pipe_name(crtc->pipe));
  7419. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7420. "Display power well on\n");
  7421. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7422. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7423. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7424. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7425. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7426. "CPU PWM1 enabled\n");
  7427. if (IS_HASWELL(dev_priv))
  7428. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7429. "CPU PWM2 enabled\n");
  7430. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7431. "PCH PWM1 enabled\n");
  7432. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7433. "Utility pin enabled\n");
  7434. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7435. /*
  7436. * In theory we can still leave IRQs enabled, as long as only the HPD
  7437. * interrupts remain enabled. We used to check for that, but since it's
  7438. * gen-specific and since we only disable LCPLL after we fully disable
  7439. * the interrupts, the check below should be enough.
  7440. */
  7441. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7442. }
  7443. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7444. {
  7445. if (IS_HASWELL(dev_priv))
  7446. return I915_READ(D_COMP_HSW);
  7447. else
  7448. return I915_READ(D_COMP_BDW);
  7449. }
  7450. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7451. {
  7452. if (IS_HASWELL(dev_priv)) {
  7453. mutex_lock(&dev_priv->pcu_lock);
  7454. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7455. val))
  7456. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7457. mutex_unlock(&dev_priv->pcu_lock);
  7458. } else {
  7459. I915_WRITE(D_COMP_BDW, val);
  7460. POSTING_READ(D_COMP_BDW);
  7461. }
  7462. }
  7463. /*
  7464. * This function implements pieces of two sequences from BSpec:
  7465. * - Sequence for display software to disable LCPLL
  7466. * - Sequence for display software to allow package C8+
  7467. * The steps implemented here are just the steps that actually touch the LCPLL
  7468. * register. Callers should take care of disabling all the display engine
  7469. * functions, doing the mode unset, fixing interrupts, etc.
  7470. */
  7471. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7472. bool switch_to_fclk, bool allow_power_down)
  7473. {
  7474. uint32_t val;
  7475. assert_can_disable_lcpll(dev_priv);
  7476. val = I915_READ(LCPLL_CTL);
  7477. if (switch_to_fclk) {
  7478. val |= LCPLL_CD_SOURCE_FCLK;
  7479. I915_WRITE(LCPLL_CTL, val);
  7480. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7481. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7482. DRM_ERROR("Switching to FCLK failed\n");
  7483. val = I915_READ(LCPLL_CTL);
  7484. }
  7485. val |= LCPLL_PLL_DISABLE;
  7486. I915_WRITE(LCPLL_CTL, val);
  7487. POSTING_READ(LCPLL_CTL);
  7488. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7489. DRM_ERROR("LCPLL still locked\n");
  7490. val = hsw_read_dcomp(dev_priv);
  7491. val |= D_COMP_COMP_DISABLE;
  7492. hsw_write_dcomp(dev_priv, val);
  7493. ndelay(100);
  7494. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7495. 1))
  7496. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7497. if (allow_power_down) {
  7498. val = I915_READ(LCPLL_CTL);
  7499. val |= LCPLL_POWER_DOWN_ALLOW;
  7500. I915_WRITE(LCPLL_CTL, val);
  7501. POSTING_READ(LCPLL_CTL);
  7502. }
  7503. }
  7504. /*
  7505. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7506. * source.
  7507. */
  7508. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7509. {
  7510. uint32_t val;
  7511. val = I915_READ(LCPLL_CTL);
  7512. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7513. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7514. return;
  7515. /*
  7516. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7517. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7518. */
  7519. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7520. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7521. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7522. I915_WRITE(LCPLL_CTL, val);
  7523. POSTING_READ(LCPLL_CTL);
  7524. }
  7525. val = hsw_read_dcomp(dev_priv);
  7526. val |= D_COMP_COMP_FORCE;
  7527. val &= ~D_COMP_COMP_DISABLE;
  7528. hsw_write_dcomp(dev_priv, val);
  7529. val = I915_READ(LCPLL_CTL);
  7530. val &= ~LCPLL_PLL_DISABLE;
  7531. I915_WRITE(LCPLL_CTL, val);
  7532. if (intel_wait_for_register(dev_priv,
  7533. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7534. 5))
  7535. DRM_ERROR("LCPLL not locked yet\n");
  7536. if (val & LCPLL_CD_SOURCE_FCLK) {
  7537. val = I915_READ(LCPLL_CTL);
  7538. val &= ~LCPLL_CD_SOURCE_FCLK;
  7539. I915_WRITE(LCPLL_CTL, val);
  7540. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7541. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7542. DRM_ERROR("Switching back to LCPLL failed\n");
  7543. }
  7544. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7545. intel_update_cdclk(dev_priv);
  7546. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7547. }
  7548. /*
  7549. * Package states C8 and deeper are really deep PC states that can only be
  7550. * reached when all the devices on the system allow it, so even if the graphics
  7551. * device allows PC8+, it doesn't mean the system will actually get to these
  7552. * states. Our driver only allows PC8+ when going into runtime PM.
  7553. *
  7554. * The requirements for PC8+ are that all the outputs are disabled, the power
  7555. * well is disabled and most interrupts are disabled, and these are also
  7556. * requirements for runtime PM. When these conditions are met, we manually do
  7557. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7558. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7559. * hang the machine.
  7560. *
  7561. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7562. * the state of some registers, so when we come back from PC8+ we need to
  7563. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7564. * need to take care of the registers kept by RC6. Notice that this happens even
  7565. * if we don't put the device in PCI D3 state (which is what currently happens
  7566. * because of the runtime PM support).
  7567. *
  7568. * For more, read "Display Sequences for Package C8" on the hardware
  7569. * documentation.
  7570. */
  7571. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7572. {
  7573. uint32_t val;
  7574. DRM_DEBUG_KMS("Enabling package C8+\n");
  7575. if (HAS_PCH_LPT_LP(dev_priv)) {
  7576. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7577. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7578. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7579. }
  7580. lpt_disable_clkout_dp(dev_priv);
  7581. hsw_disable_lcpll(dev_priv, true, true);
  7582. }
  7583. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7584. {
  7585. uint32_t val;
  7586. DRM_DEBUG_KMS("Disabling package C8+\n");
  7587. hsw_restore_lcpll(dev_priv);
  7588. lpt_init_pch_refclk(dev_priv);
  7589. if (HAS_PCH_LPT_LP(dev_priv)) {
  7590. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7591. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7592. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7593. }
  7594. }
  7595. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7596. struct intel_crtc_state *crtc_state)
  7597. {
  7598. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7599. struct intel_encoder *encoder =
  7600. intel_ddi_get_crtc_new_encoder(crtc_state);
  7601. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7602. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7603. pipe_name(crtc->pipe));
  7604. return -EINVAL;
  7605. }
  7606. }
  7607. return 0;
  7608. }
  7609. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7610. enum port port,
  7611. struct intel_crtc_state *pipe_config)
  7612. {
  7613. enum intel_dpll_id id;
  7614. u32 temp;
  7615. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7616. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7617. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7618. return;
  7619. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7620. }
  7621. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7622. enum port port,
  7623. struct intel_crtc_state *pipe_config)
  7624. {
  7625. enum intel_dpll_id id;
  7626. switch (port) {
  7627. case PORT_A:
  7628. id = DPLL_ID_SKL_DPLL0;
  7629. break;
  7630. case PORT_B:
  7631. id = DPLL_ID_SKL_DPLL1;
  7632. break;
  7633. case PORT_C:
  7634. id = DPLL_ID_SKL_DPLL2;
  7635. break;
  7636. default:
  7637. DRM_ERROR("Incorrect port type\n");
  7638. return;
  7639. }
  7640. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7641. }
  7642. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7643. enum port port,
  7644. struct intel_crtc_state *pipe_config)
  7645. {
  7646. enum intel_dpll_id id;
  7647. u32 temp;
  7648. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7649. id = temp >> (port * 3 + 1);
  7650. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7651. return;
  7652. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7653. }
  7654. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7655. enum port port,
  7656. struct intel_crtc_state *pipe_config)
  7657. {
  7658. enum intel_dpll_id id;
  7659. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7660. switch (ddi_pll_sel) {
  7661. case PORT_CLK_SEL_WRPLL1:
  7662. id = DPLL_ID_WRPLL1;
  7663. break;
  7664. case PORT_CLK_SEL_WRPLL2:
  7665. id = DPLL_ID_WRPLL2;
  7666. break;
  7667. case PORT_CLK_SEL_SPLL:
  7668. id = DPLL_ID_SPLL;
  7669. break;
  7670. case PORT_CLK_SEL_LCPLL_810:
  7671. id = DPLL_ID_LCPLL_810;
  7672. break;
  7673. case PORT_CLK_SEL_LCPLL_1350:
  7674. id = DPLL_ID_LCPLL_1350;
  7675. break;
  7676. case PORT_CLK_SEL_LCPLL_2700:
  7677. id = DPLL_ID_LCPLL_2700;
  7678. break;
  7679. default:
  7680. MISSING_CASE(ddi_pll_sel);
  7681. /* fall through */
  7682. case PORT_CLK_SEL_NONE:
  7683. return;
  7684. }
  7685. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7686. }
  7687. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7688. struct intel_crtc_state *pipe_config,
  7689. u64 *power_domain_mask)
  7690. {
  7691. struct drm_device *dev = crtc->base.dev;
  7692. struct drm_i915_private *dev_priv = to_i915(dev);
  7693. enum intel_display_power_domain power_domain;
  7694. u32 tmp;
  7695. /*
  7696. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7697. * transcoder handled below.
  7698. */
  7699. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7700. /*
  7701. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7702. * consistency and less surprising code; it's in always on power).
  7703. */
  7704. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7705. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7706. enum pipe trans_edp_pipe;
  7707. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7708. default:
  7709. WARN(1, "unknown pipe linked to edp transcoder\n");
  7710. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7711. case TRANS_DDI_EDP_INPUT_A_ON:
  7712. trans_edp_pipe = PIPE_A;
  7713. break;
  7714. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7715. trans_edp_pipe = PIPE_B;
  7716. break;
  7717. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7718. trans_edp_pipe = PIPE_C;
  7719. break;
  7720. }
  7721. if (trans_edp_pipe == crtc->pipe)
  7722. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7723. }
  7724. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7725. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7726. return false;
  7727. *power_domain_mask |= BIT_ULL(power_domain);
  7728. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7729. return tmp & PIPECONF_ENABLE;
  7730. }
  7731. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7732. struct intel_crtc_state *pipe_config,
  7733. u64 *power_domain_mask)
  7734. {
  7735. struct drm_device *dev = crtc->base.dev;
  7736. struct drm_i915_private *dev_priv = to_i915(dev);
  7737. enum intel_display_power_domain power_domain;
  7738. enum port port;
  7739. enum transcoder cpu_transcoder;
  7740. u32 tmp;
  7741. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7742. if (port == PORT_A)
  7743. cpu_transcoder = TRANSCODER_DSI_A;
  7744. else
  7745. cpu_transcoder = TRANSCODER_DSI_C;
  7746. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7747. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7748. continue;
  7749. *power_domain_mask |= BIT_ULL(power_domain);
  7750. /*
  7751. * The PLL needs to be enabled with a valid divider
  7752. * configuration, otherwise accessing DSI registers will hang
  7753. * the machine. See BSpec North Display Engine
  7754. * registers/MIPI[BXT]. We can break out here early, since we
  7755. * need the same DSI PLL to be enabled for both DSI ports.
  7756. */
  7757. if (!intel_dsi_pll_is_enabled(dev_priv))
  7758. break;
  7759. /* XXX: this works for video mode only */
  7760. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7761. if (!(tmp & DPI_ENABLE))
  7762. continue;
  7763. tmp = I915_READ(MIPI_CTRL(port));
  7764. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7765. continue;
  7766. pipe_config->cpu_transcoder = cpu_transcoder;
  7767. break;
  7768. }
  7769. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7770. }
  7771. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7772. struct intel_crtc_state *pipe_config)
  7773. {
  7774. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7775. struct intel_shared_dpll *pll;
  7776. enum port port;
  7777. uint32_t tmp;
  7778. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7779. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7780. if (IS_CANNONLAKE(dev_priv))
  7781. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7782. else if (IS_GEN9_BC(dev_priv))
  7783. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7784. else if (IS_GEN9_LP(dev_priv))
  7785. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7786. else
  7787. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7788. pll = pipe_config->shared_dpll;
  7789. if (pll) {
  7790. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7791. &pipe_config->dpll_hw_state));
  7792. }
  7793. /*
  7794. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7795. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7796. * the PCH transcoder is on.
  7797. */
  7798. if (INTEL_GEN(dev_priv) < 9 &&
  7799. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7800. pipe_config->has_pch_encoder = true;
  7801. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7802. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7803. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7804. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7805. }
  7806. }
  7807. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7808. struct intel_crtc_state *pipe_config)
  7809. {
  7810. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7811. enum intel_display_power_domain power_domain;
  7812. u64 power_domain_mask;
  7813. bool active;
  7814. intel_crtc_init_scalers(crtc, pipe_config);
  7815. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7816. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7817. return false;
  7818. power_domain_mask = BIT_ULL(power_domain);
  7819. pipe_config->shared_dpll = NULL;
  7820. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7821. if (IS_GEN9_LP(dev_priv) &&
  7822. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7823. WARN_ON(active);
  7824. active = true;
  7825. }
  7826. if (!active)
  7827. goto out;
  7828. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7829. haswell_get_ddi_port_state(crtc, pipe_config);
  7830. intel_get_pipe_timings(crtc, pipe_config);
  7831. }
  7832. intel_get_pipe_src_size(crtc, pipe_config);
  7833. pipe_config->gamma_mode =
  7834. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7835. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7836. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7837. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7838. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7839. bool blend_mode_420 = tmp &
  7840. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7841. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7842. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7843. pipe_config->ycbcr420 != blend_mode_420)
  7844. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7845. } else if (clrspace_yuv) {
  7846. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7847. }
  7848. }
  7849. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7850. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7851. power_domain_mask |= BIT_ULL(power_domain);
  7852. if (INTEL_GEN(dev_priv) >= 9)
  7853. skylake_get_pfit_config(crtc, pipe_config);
  7854. else
  7855. ironlake_get_pfit_config(crtc, pipe_config);
  7856. }
  7857. if (hsw_crtc_supports_ips(crtc)) {
  7858. if (IS_HASWELL(dev_priv))
  7859. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7860. else {
  7861. /*
  7862. * We cannot readout IPS state on broadwell, set to
  7863. * true so we can set it to a defined state on first
  7864. * commit.
  7865. */
  7866. pipe_config->ips_enabled = true;
  7867. }
  7868. }
  7869. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7870. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7871. pipe_config->pixel_multiplier =
  7872. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7873. } else {
  7874. pipe_config->pixel_multiplier = 1;
  7875. }
  7876. out:
  7877. for_each_power_domain(power_domain, power_domain_mask)
  7878. intel_display_power_put(dev_priv, power_domain);
  7879. return active;
  7880. }
  7881. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7882. {
  7883. struct drm_i915_private *dev_priv =
  7884. to_i915(plane_state->base.plane->dev);
  7885. const struct drm_framebuffer *fb = plane_state->base.fb;
  7886. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7887. u32 base;
  7888. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7889. base = obj->phys_handle->busaddr;
  7890. else
  7891. base = intel_plane_ggtt_offset(plane_state);
  7892. base += plane_state->main.offset;
  7893. /* ILK+ do this automagically */
  7894. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7895. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7896. base += (plane_state->base.crtc_h *
  7897. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7898. return base;
  7899. }
  7900. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7901. {
  7902. int x = plane_state->base.crtc_x;
  7903. int y = plane_state->base.crtc_y;
  7904. u32 pos = 0;
  7905. if (x < 0) {
  7906. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7907. x = -x;
  7908. }
  7909. pos |= x << CURSOR_X_SHIFT;
  7910. if (y < 0) {
  7911. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7912. y = -y;
  7913. }
  7914. pos |= y << CURSOR_Y_SHIFT;
  7915. return pos;
  7916. }
  7917. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7918. {
  7919. const struct drm_mode_config *config =
  7920. &plane_state->base.plane->dev->mode_config;
  7921. int width = plane_state->base.crtc_w;
  7922. int height = plane_state->base.crtc_h;
  7923. return width > 0 && width <= config->cursor_width &&
  7924. height > 0 && height <= config->cursor_height;
  7925. }
  7926. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7927. struct intel_plane_state *plane_state)
  7928. {
  7929. const struct drm_framebuffer *fb = plane_state->base.fb;
  7930. int src_x, src_y;
  7931. u32 offset;
  7932. int ret;
  7933. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7934. &crtc_state->base,
  7935. DRM_PLANE_HELPER_NO_SCALING,
  7936. DRM_PLANE_HELPER_NO_SCALING,
  7937. true, true);
  7938. if (ret)
  7939. return ret;
  7940. if (!fb)
  7941. return 0;
  7942. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7943. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7944. return -EINVAL;
  7945. }
  7946. src_x = plane_state->base.src_x >> 16;
  7947. src_y = plane_state->base.src_y >> 16;
  7948. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7949. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7950. if (src_x != 0 || src_y != 0) {
  7951. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7952. return -EINVAL;
  7953. }
  7954. plane_state->main.offset = offset;
  7955. return 0;
  7956. }
  7957. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7958. const struct intel_plane_state *plane_state)
  7959. {
  7960. const struct drm_framebuffer *fb = plane_state->base.fb;
  7961. return CURSOR_ENABLE |
  7962. CURSOR_GAMMA_ENABLE |
  7963. CURSOR_FORMAT_ARGB |
  7964. CURSOR_STRIDE(fb->pitches[0]);
  7965. }
  7966. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7967. {
  7968. int width = plane_state->base.crtc_w;
  7969. /*
  7970. * 845g/865g are only limited by the width of their cursors,
  7971. * the height is arbitrary up to the precision of the register.
  7972. */
  7973. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7974. }
  7975. static int i845_check_cursor(struct intel_plane *plane,
  7976. struct intel_crtc_state *crtc_state,
  7977. struct intel_plane_state *plane_state)
  7978. {
  7979. const struct drm_framebuffer *fb = plane_state->base.fb;
  7980. int ret;
  7981. ret = intel_check_cursor(crtc_state, plane_state);
  7982. if (ret)
  7983. return ret;
  7984. /* if we want to turn off the cursor ignore width and height */
  7985. if (!fb)
  7986. return 0;
  7987. /* Check for which cursor types we support */
  7988. if (!i845_cursor_size_ok(plane_state)) {
  7989. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7990. plane_state->base.crtc_w,
  7991. plane_state->base.crtc_h);
  7992. return -EINVAL;
  7993. }
  7994. switch (fb->pitches[0]) {
  7995. case 256:
  7996. case 512:
  7997. case 1024:
  7998. case 2048:
  7999. break;
  8000. default:
  8001. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  8002. fb->pitches[0]);
  8003. return -EINVAL;
  8004. }
  8005. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  8006. return 0;
  8007. }
  8008. static void i845_update_cursor(struct intel_plane *plane,
  8009. const struct intel_crtc_state *crtc_state,
  8010. const struct intel_plane_state *plane_state)
  8011. {
  8012. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8013. u32 cntl = 0, base = 0, pos = 0, size = 0;
  8014. unsigned long irqflags;
  8015. if (plane_state && plane_state->base.visible) {
  8016. unsigned int width = plane_state->base.crtc_w;
  8017. unsigned int height = plane_state->base.crtc_h;
  8018. cntl = plane_state->ctl;
  8019. size = (height << 12) | width;
  8020. base = intel_cursor_base(plane_state);
  8021. pos = intel_cursor_position(plane_state);
  8022. }
  8023. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8024. /* On these chipsets we can only modify the base/size/stride
  8025. * whilst the cursor is disabled.
  8026. */
  8027. if (plane->cursor.base != base ||
  8028. plane->cursor.size != size ||
  8029. plane->cursor.cntl != cntl) {
  8030. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  8031. I915_WRITE_FW(CURBASE(PIPE_A), base);
  8032. I915_WRITE_FW(CURSIZE, size);
  8033. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8034. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  8035. plane->cursor.base = base;
  8036. plane->cursor.size = size;
  8037. plane->cursor.cntl = cntl;
  8038. } else {
  8039. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8040. }
  8041. POSTING_READ_FW(CURCNTR(PIPE_A));
  8042. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8043. }
  8044. static void i845_disable_cursor(struct intel_plane *plane,
  8045. struct intel_crtc *crtc)
  8046. {
  8047. i845_update_cursor(plane, NULL, NULL);
  8048. }
  8049. static bool i845_cursor_get_hw_state(struct intel_plane *plane)
  8050. {
  8051. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8052. enum intel_display_power_domain power_domain;
  8053. bool ret;
  8054. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  8055. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8056. return false;
  8057. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  8058. intel_display_power_put(dev_priv, power_domain);
  8059. return ret;
  8060. }
  8061. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8062. const struct intel_plane_state *plane_state)
  8063. {
  8064. struct drm_i915_private *dev_priv =
  8065. to_i915(plane_state->base.plane->dev);
  8066. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8067. u32 cntl;
  8068. cntl = MCURSOR_GAMMA_ENABLE;
  8069. if (HAS_DDI(dev_priv))
  8070. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8071. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8072. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8073. switch (plane_state->base.crtc_w) {
  8074. case 64:
  8075. cntl |= CURSOR_MODE_64_ARGB_AX;
  8076. break;
  8077. case 128:
  8078. cntl |= CURSOR_MODE_128_ARGB_AX;
  8079. break;
  8080. case 256:
  8081. cntl |= CURSOR_MODE_256_ARGB_AX;
  8082. break;
  8083. default:
  8084. MISSING_CASE(plane_state->base.crtc_w);
  8085. return 0;
  8086. }
  8087. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8088. cntl |= CURSOR_ROTATE_180;
  8089. return cntl;
  8090. }
  8091. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8092. {
  8093. struct drm_i915_private *dev_priv =
  8094. to_i915(plane_state->base.plane->dev);
  8095. int width = plane_state->base.crtc_w;
  8096. int height = plane_state->base.crtc_h;
  8097. if (!intel_cursor_size_ok(plane_state))
  8098. return false;
  8099. /* Cursor width is limited to a few power-of-two sizes */
  8100. switch (width) {
  8101. case 256:
  8102. case 128:
  8103. case 64:
  8104. break;
  8105. default:
  8106. return false;
  8107. }
  8108. /*
  8109. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8110. * height from 8 lines up to the cursor width, when the
  8111. * cursor is not rotated. Everything else requires square
  8112. * cursors.
  8113. */
  8114. if (HAS_CUR_FBC(dev_priv) &&
  8115. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8116. if (height < 8 || height > width)
  8117. return false;
  8118. } else {
  8119. if (height != width)
  8120. return false;
  8121. }
  8122. return true;
  8123. }
  8124. static int i9xx_check_cursor(struct intel_plane *plane,
  8125. struct intel_crtc_state *crtc_state,
  8126. struct intel_plane_state *plane_state)
  8127. {
  8128. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8129. const struct drm_framebuffer *fb = plane_state->base.fb;
  8130. enum pipe pipe = plane->pipe;
  8131. int ret;
  8132. ret = intel_check_cursor(crtc_state, plane_state);
  8133. if (ret)
  8134. return ret;
  8135. /* if we want to turn off the cursor ignore width and height */
  8136. if (!fb)
  8137. return 0;
  8138. /* Check for which cursor types we support */
  8139. if (!i9xx_cursor_size_ok(plane_state)) {
  8140. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8141. plane_state->base.crtc_w,
  8142. plane_state->base.crtc_h);
  8143. return -EINVAL;
  8144. }
  8145. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8146. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8147. fb->pitches[0], plane_state->base.crtc_w);
  8148. return -EINVAL;
  8149. }
  8150. /*
  8151. * There's something wrong with the cursor on CHV pipe C.
  8152. * If it straddles the left edge of the screen then
  8153. * moving it away from the edge or disabling it often
  8154. * results in a pipe underrun, and often that can lead to
  8155. * dead pipe (constant underrun reported, and it scans
  8156. * out just a solid color). To recover from that, the
  8157. * display power well must be turned off and on again.
  8158. * Refuse the put the cursor into that compromised position.
  8159. */
  8160. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8161. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8162. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8163. return -EINVAL;
  8164. }
  8165. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8166. return 0;
  8167. }
  8168. static void i9xx_update_cursor(struct intel_plane *plane,
  8169. const struct intel_crtc_state *crtc_state,
  8170. const struct intel_plane_state *plane_state)
  8171. {
  8172. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8173. enum pipe pipe = plane->pipe;
  8174. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8175. unsigned long irqflags;
  8176. if (plane_state && plane_state->base.visible) {
  8177. cntl = plane_state->ctl;
  8178. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8179. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8180. base = intel_cursor_base(plane_state);
  8181. pos = intel_cursor_position(plane_state);
  8182. }
  8183. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8184. /*
  8185. * On some platforms writing CURCNTR first will also
  8186. * cause CURPOS to be armed by the CURBASE write.
  8187. * Without the CURCNTR write the CURPOS write would
  8188. * arm itself. Thus we always start the full update
  8189. * with a CURCNTR write.
  8190. *
  8191. * On other platforms CURPOS always requires the
  8192. * CURBASE write to arm the update. Additonally
  8193. * a write to any of the cursor register will cancel
  8194. * an already armed cursor update. Thus leaving out
  8195. * the CURBASE write after CURPOS could lead to a
  8196. * cursor that doesn't appear to move, or even change
  8197. * shape. Thus we always write CURBASE.
  8198. *
  8199. * CURCNTR and CUR_FBC_CTL are always
  8200. * armed by the CURBASE write only.
  8201. */
  8202. if (plane->cursor.base != base ||
  8203. plane->cursor.size != fbc_ctl ||
  8204. plane->cursor.cntl != cntl) {
  8205. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8206. if (HAS_CUR_FBC(dev_priv))
  8207. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8208. I915_WRITE_FW(CURPOS(pipe), pos);
  8209. I915_WRITE_FW(CURBASE(pipe), base);
  8210. plane->cursor.base = base;
  8211. plane->cursor.size = fbc_ctl;
  8212. plane->cursor.cntl = cntl;
  8213. } else {
  8214. I915_WRITE_FW(CURPOS(pipe), pos);
  8215. I915_WRITE_FW(CURBASE(pipe), base);
  8216. }
  8217. POSTING_READ_FW(CURBASE(pipe));
  8218. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8219. }
  8220. static void i9xx_disable_cursor(struct intel_plane *plane,
  8221. struct intel_crtc *crtc)
  8222. {
  8223. i9xx_update_cursor(plane, NULL, NULL);
  8224. }
  8225. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
  8226. {
  8227. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8228. enum intel_display_power_domain power_domain;
  8229. enum pipe pipe = plane->pipe;
  8230. bool ret;
  8231. /*
  8232. * Not 100% correct for planes that can move between pipes,
  8233. * but that's only the case for gen2-3 which don't have any
  8234. * display power wells.
  8235. */
  8236. power_domain = POWER_DOMAIN_PIPE(pipe);
  8237. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8238. return false;
  8239. ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  8240. intel_display_power_put(dev_priv, power_domain);
  8241. return ret;
  8242. }
  8243. /* VESA 640x480x72Hz mode to set on the pipe */
  8244. static const struct drm_display_mode load_detect_mode = {
  8245. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8246. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8247. };
  8248. struct drm_framebuffer *
  8249. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8250. struct drm_mode_fb_cmd2 *mode_cmd)
  8251. {
  8252. struct intel_framebuffer *intel_fb;
  8253. int ret;
  8254. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8255. if (!intel_fb)
  8256. return ERR_PTR(-ENOMEM);
  8257. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8258. if (ret)
  8259. goto err;
  8260. return &intel_fb->base;
  8261. err:
  8262. kfree(intel_fb);
  8263. return ERR_PTR(ret);
  8264. }
  8265. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8266. struct drm_crtc *crtc)
  8267. {
  8268. struct drm_plane *plane;
  8269. struct drm_plane_state *plane_state;
  8270. int ret, i;
  8271. ret = drm_atomic_add_affected_planes(state, crtc);
  8272. if (ret)
  8273. return ret;
  8274. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8275. if (plane_state->crtc != crtc)
  8276. continue;
  8277. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8278. if (ret)
  8279. return ret;
  8280. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8281. }
  8282. return 0;
  8283. }
  8284. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8285. const struct drm_display_mode *mode,
  8286. struct intel_load_detect_pipe *old,
  8287. struct drm_modeset_acquire_ctx *ctx)
  8288. {
  8289. struct intel_crtc *intel_crtc;
  8290. struct intel_encoder *intel_encoder =
  8291. intel_attached_encoder(connector);
  8292. struct drm_crtc *possible_crtc;
  8293. struct drm_encoder *encoder = &intel_encoder->base;
  8294. struct drm_crtc *crtc = NULL;
  8295. struct drm_device *dev = encoder->dev;
  8296. struct drm_i915_private *dev_priv = to_i915(dev);
  8297. struct drm_mode_config *config = &dev->mode_config;
  8298. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8299. struct drm_connector_state *connector_state;
  8300. struct intel_crtc_state *crtc_state;
  8301. int ret, i = -1;
  8302. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8303. connector->base.id, connector->name,
  8304. encoder->base.id, encoder->name);
  8305. old->restore_state = NULL;
  8306. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8307. /*
  8308. * Algorithm gets a little messy:
  8309. *
  8310. * - if the connector already has an assigned crtc, use it (but make
  8311. * sure it's on first)
  8312. *
  8313. * - try to find the first unused crtc that can drive this connector,
  8314. * and use that if we find one
  8315. */
  8316. /* See if we already have a CRTC for this connector */
  8317. if (connector->state->crtc) {
  8318. crtc = connector->state->crtc;
  8319. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8320. if (ret)
  8321. goto fail;
  8322. /* Make sure the crtc and connector are running */
  8323. goto found;
  8324. }
  8325. /* Find an unused one (if possible) */
  8326. for_each_crtc(dev, possible_crtc) {
  8327. i++;
  8328. if (!(encoder->possible_crtcs & (1 << i)))
  8329. continue;
  8330. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8331. if (ret)
  8332. goto fail;
  8333. if (possible_crtc->state->enable) {
  8334. drm_modeset_unlock(&possible_crtc->mutex);
  8335. continue;
  8336. }
  8337. crtc = possible_crtc;
  8338. break;
  8339. }
  8340. /*
  8341. * If we didn't find an unused CRTC, don't use any.
  8342. */
  8343. if (!crtc) {
  8344. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8345. ret = -ENODEV;
  8346. goto fail;
  8347. }
  8348. found:
  8349. intel_crtc = to_intel_crtc(crtc);
  8350. state = drm_atomic_state_alloc(dev);
  8351. restore_state = drm_atomic_state_alloc(dev);
  8352. if (!state || !restore_state) {
  8353. ret = -ENOMEM;
  8354. goto fail;
  8355. }
  8356. state->acquire_ctx = ctx;
  8357. restore_state->acquire_ctx = ctx;
  8358. connector_state = drm_atomic_get_connector_state(state, connector);
  8359. if (IS_ERR(connector_state)) {
  8360. ret = PTR_ERR(connector_state);
  8361. goto fail;
  8362. }
  8363. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8364. if (ret)
  8365. goto fail;
  8366. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8367. if (IS_ERR(crtc_state)) {
  8368. ret = PTR_ERR(crtc_state);
  8369. goto fail;
  8370. }
  8371. crtc_state->base.active = crtc_state->base.enable = true;
  8372. if (!mode)
  8373. mode = &load_detect_mode;
  8374. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8375. if (ret)
  8376. goto fail;
  8377. ret = intel_modeset_disable_planes(state, crtc);
  8378. if (ret)
  8379. goto fail;
  8380. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8381. if (!ret)
  8382. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8383. if (ret) {
  8384. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8385. goto fail;
  8386. }
  8387. ret = drm_atomic_commit(state);
  8388. if (ret) {
  8389. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8390. goto fail;
  8391. }
  8392. old->restore_state = restore_state;
  8393. drm_atomic_state_put(state);
  8394. /* let the connector get through one full cycle before testing */
  8395. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8396. return true;
  8397. fail:
  8398. if (state) {
  8399. drm_atomic_state_put(state);
  8400. state = NULL;
  8401. }
  8402. if (restore_state) {
  8403. drm_atomic_state_put(restore_state);
  8404. restore_state = NULL;
  8405. }
  8406. if (ret == -EDEADLK)
  8407. return ret;
  8408. return false;
  8409. }
  8410. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8411. struct intel_load_detect_pipe *old,
  8412. struct drm_modeset_acquire_ctx *ctx)
  8413. {
  8414. struct intel_encoder *intel_encoder =
  8415. intel_attached_encoder(connector);
  8416. struct drm_encoder *encoder = &intel_encoder->base;
  8417. struct drm_atomic_state *state = old->restore_state;
  8418. int ret;
  8419. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8420. connector->base.id, connector->name,
  8421. encoder->base.id, encoder->name);
  8422. if (!state)
  8423. return;
  8424. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8425. if (ret)
  8426. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8427. drm_atomic_state_put(state);
  8428. }
  8429. static int i9xx_pll_refclk(struct drm_device *dev,
  8430. const struct intel_crtc_state *pipe_config)
  8431. {
  8432. struct drm_i915_private *dev_priv = to_i915(dev);
  8433. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8434. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8435. return dev_priv->vbt.lvds_ssc_freq;
  8436. else if (HAS_PCH_SPLIT(dev_priv))
  8437. return 120000;
  8438. else if (!IS_GEN2(dev_priv))
  8439. return 96000;
  8440. else
  8441. return 48000;
  8442. }
  8443. /* Returns the clock of the currently programmed mode of the given pipe. */
  8444. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8445. struct intel_crtc_state *pipe_config)
  8446. {
  8447. struct drm_device *dev = crtc->base.dev;
  8448. struct drm_i915_private *dev_priv = to_i915(dev);
  8449. int pipe = pipe_config->cpu_transcoder;
  8450. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8451. u32 fp;
  8452. struct dpll clock;
  8453. int port_clock;
  8454. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8455. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8456. fp = pipe_config->dpll_hw_state.fp0;
  8457. else
  8458. fp = pipe_config->dpll_hw_state.fp1;
  8459. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8460. if (IS_PINEVIEW(dev_priv)) {
  8461. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8462. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8463. } else {
  8464. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8465. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8466. }
  8467. if (!IS_GEN2(dev_priv)) {
  8468. if (IS_PINEVIEW(dev_priv))
  8469. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8470. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8471. else
  8472. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8473. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8474. switch (dpll & DPLL_MODE_MASK) {
  8475. case DPLLB_MODE_DAC_SERIAL:
  8476. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8477. 5 : 10;
  8478. break;
  8479. case DPLLB_MODE_LVDS:
  8480. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8481. 7 : 14;
  8482. break;
  8483. default:
  8484. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8485. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8486. return;
  8487. }
  8488. if (IS_PINEVIEW(dev_priv))
  8489. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8490. else
  8491. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8492. } else {
  8493. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8494. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8495. if (is_lvds) {
  8496. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8497. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8498. if (lvds & LVDS_CLKB_POWER_UP)
  8499. clock.p2 = 7;
  8500. else
  8501. clock.p2 = 14;
  8502. } else {
  8503. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8504. clock.p1 = 2;
  8505. else {
  8506. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8507. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8508. }
  8509. if (dpll & PLL_P2_DIVIDE_BY_4)
  8510. clock.p2 = 4;
  8511. else
  8512. clock.p2 = 2;
  8513. }
  8514. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8515. }
  8516. /*
  8517. * This value includes pixel_multiplier. We will use
  8518. * port_clock to compute adjusted_mode.crtc_clock in the
  8519. * encoder's get_config() function.
  8520. */
  8521. pipe_config->port_clock = port_clock;
  8522. }
  8523. int intel_dotclock_calculate(int link_freq,
  8524. const struct intel_link_m_n *m_n)
  8525. {
  8526. /*
  8527. * The calculation for the data clock is:
  8528. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8529. * But we want to avoid losing precison if possible, so:
  8530. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8531. *
  8532. * and the link clock is simpler:
  8533. * link_clock = (m * link_clock) / n
  8534. */
  8535. if (!m_n->link_n)
  8536. return 0;
  8537. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8538. }
  8539. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8540. struct intel_crtc_state *pipe_config)
  8541. {
  8542. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8543. /* read out port_clock from the DPLL */
  8544. i9xx_crtc_clock_get(crtc, pipe_config);
  8545. /*
  8546. * In case there is an active pipe without active ports,
  8547. * we may need some idea for the dotclock anyway.
  8548. * Calculate one based on the FDI configuration.
  8549. */
  8550. pipe_config->base.adjusted_mode.crtc_clock =
  8551. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8552. &pipe_config->fdi_m_n);
  8553. }
  8554. /* Returns the currently programmed mode of the given encoder. */
  8555. struct drm_display_mode *
  8556. intel_encoder_current_mode(struct intel_encoder *encoder)
  8557. {
  8558. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8559. struct intel_crtc_state *crtc_state;
  8560. struct drm_display_mode *mode;
  8561. struct intel_crtc *crtc;
  8562. enum pipe pipe;
  8563. if (!encoder->get_hw_state(encoder, &pipe))
  8564. return NULL;
  8565. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8566. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8567. if (!mode)
  8568. return NULL;
  8569. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8570. if (!crtc_state) {
  8571. kfree(mode);
  8572. return NULL;
  8573. }
  8574. crtc_state->base.crtc = &crtc->base;
  8575. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8576. kfree(crtc_state);
  8577. kfree(mode);
  8578. return NULL;
  8579. }
  8580. encoder->get_config(encoder, crtc_state);
  8581. intel_mode_from_pipe_config(mode, crtc_state);
  8582. kfree(crtc_state);
  8583. return mode;
  8584. }
  8585. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8586. {
  8587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8588. drm_crtc_cleanup(crtc);
  8589. kfree(intel_crtc);
  8590. }
  8591. /**
  8592. * intel_wm_need_update - Check whether watermarks need updating
  8593. * @plane: drm plane
  8594. * @state: new plane state
  8595. *
  8596. * Check current plane state versus the new one to determine whether
  8597. * watermarks need to be recalculated.
  8598. *
  8599. * Returns true or false.
  8600. */
  8601. static bool intel_wm_need_update(struct drm_plane *plane,
  8602. struct drm_plane_state *state)
  8603. {
  8604. struct intel_plane_state *new = to_intel_plane_state(state);
  8605. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8606. /* Update watermarks on tiling or size changes. */
  8607. if (new->base.visible != cur->base.visible)
  8608. return true;
  8609. if (!cur->base.fb || !new->base.fb)
  8610. return false;
  8611. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8612. cur->base.rotation != new->base.rotation ||
  8613. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8614. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8615. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8616. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8617. return true;
  8618. return false;
  8619. }
  8620. static bool needs_scaling(const struct intel_plane_state *state)
  8621. {
  8622. int src_w = drm_rect_width(&state->base.src) >> 16;
  8623. int src_h = drm_rect_height(&state->base.src) >> 16;
  8624. int dst_w = drm_rect_width(&state->base.dst);
  8625. int dst_h = drm_rect_height(&state->base.dst);
  8626. return (src_w != dst_w || src_h != dst_h);
  8627. }
  8628. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8629. struct drm_crtc_state *crtc_state,
  8630. const struct intel_plane_state *old_plane_state,
  8631. struct drm_plane_state *plane_state)
  8632. {
  8633. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8634. struct drm_crtc *crtc = crtc_state->crtc;
  8635. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8636. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8637. struct drm_device *dev = crtc->dev;
  8638. struct drm_i915_private *dev_priv = to_i915(dev);
  8639. bool mode_changed = needs_modeset(crtc_state);
  8640. bool was_crtc_enabled = old_crtc_state->base.active;
  8641. bool is_crtc_enabled = crtc_state->active;
  8642. bool turn_off, turn_on, visible, was_visible;
  8643. struct drm_framebuffer *fb = plane_state->fb;
  8644. int ret;
  8645. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8646. ret = skl_update_scaler_plane(
  8647. to_intel_crtc_state(crtc_state),
  8648. to_intel_plane_state(plane_state));
  8649. if (ret)
  8650. return ret;
  8651. }
  8652. was_visible = old_plane_state->base.visible;
  8653. visible = plane_state->visible;
  8654. if (!was_crtc_enabled && WARN_ON(was_visible))
  8655. was_visible = false;
  8656. /*
  8657. * Visibility is calculated as if the crtc was on, but
  8658. * after scaler setup everything depends on it being off
  8659. * when the crtc isn't active.
  8660. *
  8661. * FIXME this is wrong for watermarks. Watermarks should also
  8662. * be computed as if the pipe would be active. Perhaps move
  8663. * per-plane wm computation to the .check_plane() hook, and
  8664. * only combine the results from all planes in the current place?
  8665. */
  8666. if (!is_crtc_enabled) {
  8667. plane_state->visible = visible = false;
  8668. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8669. }
  8670. if (!was_visible && !visible)
  8671. return 0;
  8672. if (fb != old_plane_state->base.fb)
  8673. pipe_config->fb_changed = true;
  8674. turn_off = was_visible && (!visible || mode_changed);
  8675. turn_on = visible && (!was_visible || mode_changed);
  8676. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8677. intel_crtc->base.base.id, intel_crtc->base.name,
  8678. plane->base.base.id, plane->base.name,
  8679. fb ? fb->base.id : -1);
  8680. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8681. plane->base.base.id, plane->base.name,
  8682. was_visible, visible,
  8683. turn_off, turn_on, mode_changed);
  8684. if (turn_on) {
  8685. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8686. pipe_config->update_wm_pre = true;
  8687. /* must disable cxsr around plane enable/disable */
  8688. if (plane->id != PLANE_CURSOR)
  8689. pipe_config->disable_cxsr = true;
  8690. } else if (turn_off) {
  8691. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8692. pipe_config->update_wm_post = true;
  8693. /* must disable cxsr around plane enable/disable */
  8694. if (plane->id != PLANE_CURSOR)
  8695. pipe_config->disable_cxsr = true;
  8696. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8697. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8698. /* FIXME bollocks */
  8699. pipe_config->update_wm_pre = true;
  8700. pipe_config->update_wm_post = true;
  8701. }
  8702. }
  8703. if (visible || was_visible)
  8704. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8705. /*
  8706. * WaCxSRDisabledForSpriteScaling:ivb
  8707. *
  8708. * cstate->update_wm was already set above, so this flag will
  8709. * take effect when we commit and program watermarks.
  8710. */
  8711. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8712. needs_scaling(to_intel_plane_state(plane_state)) &&
  8713. !needs_scaling(old_plane_state))
  8714. pipe_config->disable_lp_wm = true;
  8715. return 0;
  8716. }
  8717. static bool encoders_cloneable(const struct intel_encoder *a,
  8718. const struct intel_encoder *b)
  8719. {
  8720. /* masks could be asymmetric, so check both ways */
  8721. return a == b || (a->cloneable & (1 << b->type) &&
  8722. b->cloneable & (1 << a->type));
  8723. }
  8724. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8725. struct intel_crtc *crtc,
  8726. struct intel_encoder *encoder)
  8727. {
  8728. struct intel_encoder *source_encoder;
  8729. struct drm_connector *connector;
  8730. struct drm_connector_state *connector_state;
  8731. int i;
  8732. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8733. if (connector_state->crtc != &crtc->base)
  8734. continue;
  8735. source_encoder =
  8736. to_intel_encoder(connector_state->best_encoder);
  8737. if (!encoders_cloneable(encoder, source_encoder))
  8738. return false;
  8739. }
  8740. return true;
  8741. }
  8742. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8743. struct drm_crtc_state *crtc_state)
  8744. {
  8745. struct drm_device *dev = crtc->dev;
  8746. struct drm_i915_private *dev_priv = to_i915(dev);
  8747. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8748. struct intel_crtc_state *pipe_config =
  8749. to_intel_crtc_state(crtc_state);
  8750. struct drm_atomic_state *state = crtc_state->state;
  8751. int ret;
  8752. bool mode_changed = needs_modeset(crtc_state);
  8753. if (mode_changed && !crtc_state->active)
  8754. pipe_config->update_wm_post = true;
  8755. if (mode_changed && crtc_state->enable &&
  8756. dev_priv->display.crtc_compute_clock &&
  8757. !WARN_ON(pipe_config->shared_dpll)) {
  8758. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8759. pipe_config);
  8760. if (ret)
  8761. return ret;
  8762. }
  8763. if (crtc_state->color_mgmt_changed) {
  8764. ret = intel_color_check(crtc, crtc_state);
  8765. if (ret)
  8766. return ret;
  8767. /*
  8768. * Changing color management on Intel hardware is
  8769. * handled as part of planes update.
  8770. */
  8771. crtc_state->planes_changed = true;
  8772. }
  8773. ret = 0;
  8774. if (dev_priv->display.compute_pipe_wm) {
  8775. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8776. if (ret) {
  8777. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8778. return ret;
  8779. }
  8780. }
  8781. if (dev_priv->display.compute_intermediate_wm &&
  8782. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8783. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8784. return 0;
  8785. /*
  8786. * Calculate 'intermediate' watermarks that satisfy both the
  8787. * old state and the new state. We can program these
  8788. * immediately.
  8789. */
  8790. ret = dev_priv->display.compute_intermediate_wm(dev,
  8791. intel_crtc,
  8792. pipe_config);
  8793. if (ret) {
  8794. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8795. return ret;
  8796. }
  8797. } else if (dev_priv->display.compute_intermediate_wm) {
  8798. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8799. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8800. }
  8801. if (INTEL_GEN(dev_priv) >= 9) {
  8802. if (mode_changed)
  8803. ret = skl_update_scaler_crtc(pipe_config);
  8804. if (!ret)
  8805. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8806. pipe_config);
  8807. if (!ret)
  8808. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8809. pipe_config);
  8810. }
  8811. if (HAS_IPS(dev_priv))
  8812. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8813. return ret;
  8814. }
  8815. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8816. .atomic_begin = intel_begin_crtc_commit,
  8817. .atomic_flush = intel_finish_crtc_commit,
  8818. .atomic_check = intel_crtc_atomic_check,
  8819. };
  8820. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8821. {
  8822. struct intel_connector *connector;
  8823. struct drm_connector_list_iter conn_iter;
  8824. drm_connector_list_iter_begin(dev, &conn_iter);
  8825. for_each_intel_connector_iter(connector, &conn_iter) {
  8826. if (connector->base.state->crtc)
  8827. drm_connector_unreference(&connector->base);
  8828. if (connector->base.encoder) {
  8829. connector->base.state->best_encoder =
  8830. connector->base.encoder;
  8831. connector->base.state->crtc =
  8832. connector->base.encoder->crtc;
  8833. drm_connector_reference(&connector->base);
  8834. } else {
  8835. connector->base.state->best_encoder = NULL;
  8836. connector->base.state->crtc = NULL;
  8837. }
  8838. }
  8839. drm_connector_list_iter_end(&conn_iter);
  8840. }
  8841. static void
  8842. connected_sink_compute_bpp(struct intel_connector *connector,
  8843. struct intel_crtc_state *pipe_config)
  8844. {
  8845. const struct drm_display_info *info = &connector->base.display_info;
  8846. int bpp = pipe_config->pipe_bpp;
  8847. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8848. connector->base.base.id,
  8849. connector->base.name);
  8850. /* Don't use an invalid EDID bpc value */
  8851. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8852. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8853. bpp, info->bpc * 3);
  8854. pipe_config->pipe_bpp = info->bpc * 3;
  8855. }
  8856. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8857. if (info->bpc == 0 && bpp > 24) {
  8858. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8859. bpp);
  8860. pipe_config->pipe_bpp = 24;
  8861. }
  8862. }
  8863. static int
  8864. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8865. struct intel_crtc_state *pipe_config)
  8866. {
  8867. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8868. struct drm_atomic_state *state;
  8869. struct drm_connector *connector;
  8870. struct drm_connector_state *connector_state;
  8871. int bpp, i;
  8872. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8873. IS_CHERRYVIEW(dev_priv)))
  8874. bpp = 10*3;
  8875. else if (INTEL_GEN(dev_priv) >= 5)
  8876. bpp = 12*3;
  8877. else
  8878. bpp = 8*3;
  8879. pipe_config->pipe_bpp = bpp;
  8880. state = pipe_config->base.state;
  8881. /* Clamp display bpp to EDID value */
  8882. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8883. if (connector_state->crtc != &crtc->base)
  8884. continue;
  8885. connected_sink_compute_bpp(to_intel_connector(connector),
  8886. pipe_config);
  8887. }
  8888. return bpp;
  8889. }
  8890. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8891. {
  8892. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8893. "type: 0x%x flags: 0x%x\n",
  8894. mode->crtc_clock,
  8895. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8896. mode->crtc_hsync_end, mode->crtc_htotal,
  8897. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8898. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8899. }
  8900. static inline void
  8901. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8902. unsigned int lane_count, struct intel_link_m_n *m_n)
  8903. {
  8904. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8905. id, lane_count,
  8906. m_n->gmch_m, m_n->gmch_n,
  8907. m_n->link_m, m_n->link_n, m_n->tu);
  8908. }
  8909. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8910. static const char * const output_type_str[] = {
  8911. OUTPUT_TYPE(UNUSED),
  8912. OUTPUT_TYPE(ANALOG),
  8913. OUTPUT_TYPE(DVO),
  8914. OUTPUT_TYPE(SDVO),
  8915. OUTPUT_TYPE(LVDS),
  8916. OUTPUT_TYPE(TVOUT),
  8917. OUTPUT_TYPE(HDMI),
  8918. OUTPUT_TYPE(DP),
  8919. OUTPUT_TYPE(EDP),
  8920. OUTPUT_TYPE(DSI),
  8921. OUTPUT_TYPE(DDI),
  8922. OUTPUT_TYPE(DP_MST),
  8923. };
  8924. #undef OUTPUT_TYPE
  8925. static void snprintf_output_types(char *buf, size_t len,
  8926. unsigned int output_types)
  8927. {
  8928. char *str = buf;
  8929. int i;
  8930. str[0] = '\0';
  8931. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8932. int r;
  8933. if ((output_types & BIT(i)) == 0)
  8934. continue;
  8935. r = snprintf(str, len, "%s%s",
  8936. str != buf ? "," : "", output_type_str[i]);
  8937. if (r >= len)
  8938. break;
  8939. str += r;
  8940. len -= r;
  8941. output_types &= ~BIT(i);
  8942. }
  8943. WARN_ON_ONCE(output_types != 0);
  8944. }
  8945. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8946. struct intel_crtc_state *pipe_config,
  8947. const char *context)
  8948. {
  8949. struct drm_device *dev = crtc->base.dev;
  8950. struct drm_i915_private *dev_priv = to_i915(dev);
  8951. struct drm_plane *plane;
  8952. struct intel_plane *intel_plane;
  8953. struct intel_plane_state *state;
  8954. struct drm_framebuffer *fb;
  8955. char buf[64];
  8956. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8957. crtc->base.base.id, crtc->base.name, context);
  8958. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  8959. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  8960. buf, pipe_config->output_types);
  8961. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8962. transcoder_name(pipe_config->cpu_transcoder),
  8963. pipe_config->pipe_bpp, pipe_config->dither);
  8964. if (pipe_config->has_pch_encoder)
  8965. intel_dump_m_n_config(pipe_config, "fdi",
  8966. pipe_config->fdi_lanes,
  8967. &pipe_config->fdi_m_n);
  8968. if (pipe_config->ycbcr420)
  8969. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8970. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8971. intel_dump_m_n_config(pipe_config, "dp m_n",
  8972. pipe_config->lane_count, &pipe_config->dp_m_n);
  8973. if (pipe_config->has_drrs)
  8974. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8975. pipe_config->lane_count,
  8976. &pipe_config->dp_m2_n2);
  8977. }
  8978. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8979. pipe_config->has_audio, pipe_config->has_infoframe);
  8980. DRM_DEBUG_KMS("requested mode:\n");
  8981. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8982. DRM_DEBUG_KMS("adjusted mode:\n");
  8983. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8984. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8985. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8986. pipe_config->port_clock,
  8987. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8988. pipe_config->pixel_rate);
  8989. if (INTEL_GEN(dev_priv) >= 9)
  8990. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8991. crtc->num_scalers,
  8992. pipe_config->scaler_state.scaler_users,
  8993. pipe_config->scaler_state.scaler_id);
  8994. if (HAS_GMCH_DISPLAY(dev_priv))
  8995. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8996. pipe_config->gmch_pfit.control,
  8997. pipe_config->gmch_pfit.pgm_ratios,
  8998. pipe_config->gmch_pfit.lvds_border_bits);
  8999. else
  9000. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9001. pipe_config->pch_pfit.pos,
  9002. pipe_config->pch_pfit.size,
  9003. enableddisabled(pipe_config->pch_pfit.enabled));
  9004. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9005. pipe_config->ips_enabled, pipe_config->double_wide);
  9006. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9007. DRM_DEBUG_KMS("planes on this crtc\n");
  9008. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9009. struct drm_format_name_buf format_name;
  9010. intel_plane = to_intel_plane(plane);
  9011. if (intel_plane->pipe != crtc->pipe)
  9012. continue;
  9013. state = to_intel_plane_state(plane->state);
  9014. fb = state->base.fb;
  9015. if (!fb) {
  9016. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9017. plane->base.id, plane->name, state->scaler_id);
  9018. continue;
  9019. }
  9020. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9021. plane->base.id, plane->name,
  9022. fb->base.id, fb->width, fb->height,
  9023. drm_get_format_name(fb->format->format, &format_name));
  9024. if (INTEL_GEN(dev_priv) >= 9)
  9025. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9026. state->scaler_id,
  9027. state->base.src.x1 >> 16,
  9028. state->base.src.y1 >> 16,
  9029. drm_rect_width(&state->base.src) >> 16,
  9030. drm_rect_height(&state->base.src) >> 16,
  9031. state->base.dst.x1, state->base.dst.y1,
  9032. drm_rect_width(&state->base.dst),
  9033. drm_rect_height(&state->base.dst));
  9034. }
  9035. }
  9036. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9037. {
  9038. struct drm_device *dev = state->dev;
  9039. struct drm_connector *connector;
  9040. struct drm_connector_list_iter conn_iter;
  9041. unsigned int used_ports = 0;
  9042. unsigned int used_mst_ports = 0;
  9043. bool ret = true;
  9044. /*
  9045. * Walk the connector list instead of the encoder
  9046. * list to detect the problem on ddi platforms
  9047. * where there's just one encoder per digital port.
  9048. */
  9049. drm_connector_list_iter_begin(dev, &conn_iter);
  9050. drm_for_each_connector_iter(connector, &conn_iter) {
  9051. struct drm_connector_state *connector_state;
  9052. struct intel_encoder *encoder;
  9053. connector_state = drm_atomic_get_new_connector_state(state, connector);
  9054. if (!connector_state)
  9055. connector_state = connector->state;
  9056. if (!connector_state->best_encoder)
  9057. continue;
  9058. encoder = to_intel_encoder(connector_state->best_encoder);
  9059. WARN_ON(!connector_state->crtc);
  9060. switch (encoder->type) {
  9061. unsigned int port_mask;
  9062. case INTEL_OUTPUT_DDI:
  9063. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9064. break;
  9065. case INTEL_OUTPUT_DP:
  9066. case INTEL_OUTPUT_HDMI:
  9067. case INTEL_OUTPUT_EDP:
  9068. port_mask = 1 << encoder->port;
  9069. /* the same port mustn't appear more than once */
  9070. if (used_ports & port_mask)
  9071. ret = false;
  9072. used_ports |= port_mask;
  9073. break;
  9074. case INTEL_OUTPUT_DP_MST:
  9075. used_mst_ports |=
  9076. 1 << encoder->port;
  9077. break;
  9078. default:
  9079. break;
  9080. }
  9081. }
  9082. drm_connector_list_iter_end(&conn_iter);
  9083. /* can't mix MST and SST/HDMI on the same port */
  9084. if (used_ports & used_mst_ports)
  9085. return false;
  9086. return ret;
  9087. }
  9088. static void
  9089. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9090. {
  9091. struct drm_i915_private *dev_priv =
  9092. to_i915(crtc_state->base.crtc->dev);
  9093. struct intel_crtc_scaler_state scaler_state;
  9094. struct intel_dpll_hw_state dpll_hw_state;
  9095. struct intel_shared_dpll *shared_dpll;
  9096. struct intel_crtc_wm_state wm_state;
  9097. bool force_thru, ips_force_disable;
  9098. /* FIXME: before the switch to atomic started, a new pipe_config was
  9099. * kzalloc'd. Code that depends on any field being zero should be
  9100. * fixed, so that the crtc_state can be safely duplicated. For now,
  9101. * only fields that are know to not cause problems are preserved. */
  9102. scaler_state = crtc_state->scaler_state;
  9103. shared_dpll = crtc_state->shared_dpll;
  9104. dpll_hw_state = crtc_state->dpll_hw_state;
  9105. force_thru = crtc_state->pch_pfit.force_thru;
  9106. ips_force_disable = crtc_state->ips_force_disable;
  9107. if (IS_G4X(dev_priv) ||
  9108. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9109. wm_state = crtc_state->wm;
  9110. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9111. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9112. memset(&crtc_state->base + 1, 0,
  9113. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9114. crtc_state->scaler_state = scaler_state;
  9115. crtc_state->shared_dpll = shared_dpll;
  9116. crtc_state->dpll_hw_state = dpll_hw_state;
  9117. crtc_state->pch_pfit.force_thru = force_thru;
  9118. crtc_state->ips_force_disable = ips_force_disable;
  9119. if (IS_G4X(dev_priv) ||
  9120. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9121. crtc_state->wm = wm_state;
  9122. }
  9123. static int
  9124. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9125. struct intel_crtc_state *pipe_config)
  9126. {
  9127. struct drm_atomic_state *state = pipe_config->base.state;
  9128. struct intel_encoder *encoder;
  9129. struct drm_connector *connector;
  9130. struct drm_connector_state *connector_state;
  9131. int base_bpp, ret = -EINVAL;
  9132. int i;
  9133. bool retry = true;
  9134. clear_intel_crtc_state(pipe_config);
  9135. pipe_config->cpu_transcoder =
  9136. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9137. /*
  9138. * Sanitize sync polarity flags based on requested ones. If neither
  9139. * positive or negative polarity is requested, treat this as meaning
  9140. * negative polarity.
  9141. */
  9142. if (!(pipe_config->base.adjusted_mode.flags &
  9143. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9144. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9145. if (!(pipe_config->base.adjusted_mode.flags &
  9146. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9147. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9148. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9149. pipe_config);
  9150. if (base_bpp < 0)
  9151. goto fail;
  9152. /*
  9153. * Determine the real pipe dimensions. Note that stereo modes can
  9154. * increase the actual pipe size due to the frame doubling and
  9155. * insertion of additional space for blanks between the frame. This
  9156. * is stored in the crtc timings. We use the requested mode to do this
  9157. * computation to clearly distinguish it from the adjusted mode, which
  9158. * can be changed by the connectors in the below retry loop.
  9159. */
  9160. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9161. &pipe_config->pipe_src_w,
  9162. &pipe_config->pipe_src_h);
  9163. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9164. if (connector_state->crtc != crtc)
  9165. continue;
  9166. encoder = to_intel_encoder(connector_state->best_encoder);
  9167. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9168. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9169. goto fail;
  9170. }
  9171. /*
  9172. * Determine output_types before calling the .compute_config()
  9173. * hooks so that the hooks can use this information safely.
  9174. */
  9175. if (encoder->compute_output_type)
  9176. pipe_config->output_types |=
  9177. BIT(encoder->compute_output_type(encoder, pipe_config,
  9178. connector_state));
  9179. else
  9180. pipe_config->output_types |= BIT(encoder->type);
  9181. }
  9182. encoder_retry:
  9183. /* Ensure the port clock defaults are reset when retrying. */
  9184. pipe_config->port_clock = 0;
  9185. pipe_config->pixel_multiplier = 1;
  9186. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9187. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9188. CRTC_STEREO_DOUBLE);
  9189. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9190. * adjust it according to limitations or connector properties, and also
  9191. * a chance to reject the mode entirely.
  9192. */
  9193. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9194. if (connector_state->crtc != crtc)
  9195. continue;
  9196. encoder = to_intel_encoder(connector_state->best_encoder);
  9197. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9198. DRM_DEBUG_KMS("Encoder config failure\n");
  9199. goto fail;
  9200. }
  9201. }
  9202. /* Set default port clock if not overwritten by the encoder. Needs to be
  9203. * done afterwards in case the encoder adjusts the mode. */
  9204. if (!pipe_config->port_clock)
  9205. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9206. * pipe_config->pixel_multiplier;
  9207. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9208. if (ret < 0) {
  9209. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9210. goto fail;
  9211. }
  9212. if (ret == RETRY) {
  9213. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9214. ret = -EINVAL;
  9215. goto fail;
  9216. }
  9217. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9218. retry = false;
  9219. goto encoder_retry;
  9220. }
  9221. /* Dithering seems to not pass-through bits correctly when it should, so
  9222. * only enable it on 6bpc panels and when its not a compliance
  9223. * test requesting 6bpc video pattern.
  9224. */
  9225. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9226. !pipe_config->dither_force_disable;
  9227. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9228. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9229. fail:
  9230. return ret;
  9231. }
  9232. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9233. {
  9234. int diff;
  9235. if (clock1 == clock2)
  9236. return true;
  9237. if (!clock1 || !clock2)
  9238. return false;
  9239. diff = abs(clock1 - clock2);
  9240. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9241. return true;
  9242. return false;
  9243. }
  9244. static bool
  9245. intel_compare_m_n(unsigned int m, unsigned int n,
  9246. unsigned int m2, unsigned int n2,
  9247. bool exact)
  9248. {
  9249. if (m == m2 && n == n2)
  9250. return true;
  9251. if (exact || !m || !n || !m2 || !n2)
  9252. return false;
  9253. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9254. if (n > n2) {
  9255. while (n > n2) {
  9256. m2 <<= 1;
  9257. n2 <<= 1;
  9258. }
  9259. } else if (n < n2) {
  9260. while (n < n2) {
  9261. m <<= 1;
  9262. n <<= 1;
  9263. }
  9264. }
  9265. if (n != n2)
  9266. return false;
  9267. return intel_fuzzy_clock_check(m, m2);
  9268. }
  9269. static bool
  9270. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9271. struct intel_link_m_n *m2_n2,
  9272. bool adjust)
  9273. {
  9274. if (m_n->tu == m2_n2->tu &&
  9275. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9276. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9277. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9278. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9279. if (adjust)
  9280. *m2_n2 = *m_n;
  9281. return true;
  9282. }
  9283. return false;
  9284. }
  9285. static void __printf(3, 4)
  9286. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9287. {
  9288. struct va_format vaf;
  9289. va_list args;
  9290. va_start(args, format);
  9291. vaf.fmt = format;
  9292. vaf.va = &args;
  9293. if (adjust)
  9294. drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
  9295. else
  9296. drm_err("mismatch in %s %pV", name, &vaf);
  9297. va_end(args);
  9298. }
  9299. static bool
  9300. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9301. struct intel_crtc_state *current_config,
  9302. struct intel_crtc_state *pipe_config,
  9303. bool adjust)
  9304. {
  9305. bool ret = true;
  9306. bool fixup_inherited = adjust &&
  9307. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9308. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9309. #define PIPE_CONF_CHECK_X(name) do { \
  9310. if (current_config->name != pipe_config->name) { \
  9311. pipe_config_err(adjust, __stringify(name), \
  9312. "(expected 0x%08x, found 0x%08x)\n", \
  9313. current_config->name, \
  9314. pipe_config->name); \
  9315. ret = false; \
  9316. } \
  9317. } while (0)
  9318. #define PIPE_CONF_CHECK_I(name) do { \
  9319. if (current_config->name != pipe_config->name) { \
  9320. pipe_config_err(adjust, __stringify(name), \
  9321. "(expected %i, found %i)\n", \
  9322. current_config->name, \
  9323. pipe_config->name); \
  9324. ret = false; \
  9325. } \
  9326. } while (0)
  9327. #define PIPE_CONF_CHECK_BOOL(name) do { \
  9328. if (current_config->name != pipe_config->name) { \
  9329. pipe_config_err(adjust, __stringify(name), \
  9330. "(expected %s, found %s)\n", \
  9331. yesno(current_config->name), \
  9332. yesno(pipe_config->name)); \
  9333. ret = false; \
  9334. } \
  9335. } while (0)
  9336. /*
  9337. * Checks state where we only read out the enabling, but not the entire
  9338. * state itself (like full infoframes or ELD for audio). These states
  9339. * require a full modeset on bootup to fix up.
  9340. */
  9341. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
  9342. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9343. PIPE_CONF_CHECK_BOOL(name); \
  9344. } else { \
  9345. pipe_config_err(adjust, __stringify(name), \
  9346. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9347. yesno(current_config->name), \
  9348. yesno(pipe_config->name)); \
  9349. ret = false; \
  9350. } \
  9351. } while (0)
  9352. #define PIPE_CONF_CHECK_P(name) do { \
  9353. if (current_config->name != pipe_config->name) { \
  9354. pipe_config_err(adjust, __stringify(name), \
  9355. "(expected %p, found %p)\n", \
  9356. current_config->name, \
  9357. pipe_config->name); \
  9358. ret = false; \
  9359. } \
  9360. } while (0)
  9361. #define PIPE_CONF_CHECK_M_N(name) do { \
  9362. if (!intel_compare_link_m_n(&current_config->name, \
  9363. &pipe_config->name,\
  9364. adjust)) { \
  9365. pipe_config_err(adjust, __stringify(name), \
  9366. "(expected tu %i gmch %i/%i link %i/%i, " \
  9367. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9368. current_config->name.tu, \
  9369. current_config->name.gmch_m, \
  9370. current_config->name.gmch_n, \
  9371. current_config->name.link_m, \
  9372. current_config->name.link_n, \
  9373. pipe_config->name.tu, \
  9374. pipe_config->name.gmch_m, \
  9375. pipe_config->name.gmch_n, \
  9376. pipe_config->name.link_m, \
  9377. pipe_config->name.link_n); \
  9378. ret = false; \
  9379. } \
  9380. } while (0)
  9381. /* This is required for BDW+ where there is only one set of registers for
  9382. * switching between high and low RR.
  9383. * This macro can be used whenever a comparison has to be made between one
  9384. * hw state and multiple sw state variables.
  9385. */
  9386. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
  9387. if (!intel_compare_link_m_n(&current_config->name, \
  9388. &pipe_config->name, adjust) && \
  9389. !intel_compare_link_m_n(&current_config->alt_name, \
  9390. &pipe_config->name, adjust)) { \
  9391. pipe_config_err(adjust, __stringify(name), \
  9392. "(expected tu %i gmch %i/%i link %i/%i, " \
  9393. "or tu %i gmch %i/%i link %i/%i, " \
  9394. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9395. current_config->name.tu, \
  9396. current_config->name.gmch_m, \
  9397. current_config->name.gmch_n, \
  9398. current_config->name.link_m, \
  9399. current_config->name.link_n, \
  9400. current_config->alt_name.tu, \
  9401. current_config->alt_name.gmch_m, \
  9402. current_config->alt_name.gmch_n, \
  9403. current_config->alt_name.link_m, \
  9404. current_config->alt_name.link_n, \
  9405. pipe_config->name.tu, \
  9406. pipe_config->name.gmch_m, \
  9407. pipe_config->name.gmch_n, \
  9408. pipe_config->name.link_m, \
  9409. pipe_config->name.link_n); \
  9410. ret = false; \
  9411. } \
  9412. } while (0)
  9413. #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
  9414. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9415. pipe_config_err(adjust, __stringify(name), \
  9416. "(%x) (expected %i, found %i)\n", \
  9417. (mask), \
  9418. current_config->name & (mask), \
  9419. pipe_config->name & (mask)); \
  9420. ret = false; \
  9421. } \
  9422. } while (0)
  9423. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
  9424. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9425. pipe_config_err(adjust, __stringify(name), \
  9426. "(expected %i, found %i)\n", \
  9427. current_config->name, \
  9428. pipe_config->name); \
  9429. ret = false; \
  9430. } \
  9431. } while (0)
  9432. #define PIPE_CONF_QUIRK(quirk) \
  9433. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9434. PIPE_CONF_CHECK_I(cpu_transcoder);
  9435. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9436. PIPE_CONF_CHECK_I(fdi_lanes);
  9437. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9438. PIPE_CONF_CHECK_I(lane_count);
  9439. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9440. if (INTEL_GEN(dev_priv) < 8) {
  9441. PIPE_CONF_CHECK_M_N(dp_m_n);
  9442. if (current_config->has_drrs)
  9443. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9444. } else
  9445. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9446. PIPE_CONF_CHECK_X(output_types);
  9447. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9448. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9449. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9450. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9451. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9452. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9453. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9454. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9455. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9456. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9457. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9458. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9459. PIPE_CONF_CHECK_I(pixel_multiplier);
  9460. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9461. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9462. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9463. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9464. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9465. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9466. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9467. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9468. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9469. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9470. DRM_MODE_FLAG_INTERLACE);
  9471. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9472. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9473. DRM_MODE_FLAG_PHSYNC);
  9474. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9475. DRM_MODE_FLAG_NHSYNC);
  9476. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9477. DRM_MODE_FLAG_PVSYNC);
  9478. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9479. DRM_MODE_FLAG_NVSYNC);
  9480. }
  9481. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9482. /* pfit ratios are autocomputed by the hw on gen4+ */
  9483. if (INTEL_GEN(dev_priv) < 4)
  9484. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9485. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9486. if (!adjust) {
  9487. PIPE_CONF_CHECK_I(pipe_src_w);
  9488. PIPE_CONF_CHECK_I(pipe_src_h);
  9489. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9490. if (current_config->pch_pfit.enabled) {
  9491. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9492. PIPE_CONF_CHECK_X(pch_pfit.size);
  9493. }
  9494. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9495. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9496. }
  9497. PIPE_CONF_CHECK_BOOL(double_wide);
  9498. PIPE_CONF_CHECK_P(shared_dpll);
  9499. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9500. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9501. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9502. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9503. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9504. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9505. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9506. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9507. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9508. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9509. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9510. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9511. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9512. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9513. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9514. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9515. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9516. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9517. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9518. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9519. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9520. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9521. PIPE_CONF_CHECK_X(dsi_pll.div);
  9522. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9523. PIPE_CONF_CHECK_I(pipe_bpp);
  9524. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9525. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9526. PIPE_CONF_CHECK_I(min_voltage_level);
  9527. #undef PIPE_CONF_CHECK_X
  9528. #undef PIPE_CONF_CHECK_I
  9529. #undef PIPE_CONF_CHECK_BOOL
  9530. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9531. #undef PIPE_CONF_CHECK_P
  9532. #undef PIPE_CONF_CHECK_FLAGS
  9533. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9534. #undef PIPE_CONF_QUIRK
  9535. return ret;
  9536. }
  9537. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9538. const struct intel_crtc_state *pipe_config)
  9539. {
  9540. if (pipe_config->has_pch_encoder) {
  9541. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9542. &pipe_config->fdi_m_n);
  9543. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9544. /*
  9545. * FDI already provided one idea for the dotclock.
  9546. * Yell if the encoder disagrees.
  9547. */
  9548. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9549. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9550. fdi_dotclock, dotclock);
  9551. }
  9552. }
  9553. static void verify_wm_state(struct drm_crtc *crtc,
  9554. struct drm_crtc_state *new_state)
  9555. {
  9556. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9557. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9558. struct skl_pipe_wm hw_wm, *sw_wm;
  9559. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9560. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9561. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9562. const enum pipe pipe = intel_crtc->pipe;
  9563. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9564. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9565. return;
  9566. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9567. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9568. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9569. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9570. if (INTEL_GEN(dev_priv) >= 11)
  9571. if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
  9572. DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
  9573. sw_ddb->enabled_slices,
  9574. hw_ddb.enabled_slices);
  9575. /* planes */
  9576. for_each_universal_plane(dev_priv, pipe, plane) {
  9577. hw_plane_wm = &hw_wm.planes[plane];
  9578. sw_plane_wm = &sw_wm->planes[plane];
  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 plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9585. pipe_name(pipe), plane + 1, 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 plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9596. pipe_name(pipe), plane + 1,
  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];
  9606. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9607. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9608. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9609. pipe_name(pipe), plane + 1,
  9610. sw_ddb_entry->start, sw_ddb_entry->end,
  9611. hw_ddb_entry->start, hw_ddb_entry->end);
  9612. }
  9613. }
  9614. /*
  9615. * cursor
  9616. * If the cursor plane isn't active, we may not have updated it's ddb
  9617. * allocation. In that case since the ddb allocation will be updated
  9618. * once the plane becomes visible, we can skip this check
  9619. */
  9620. if (1) {
  9621. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9622. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9623. /* Watermarks */
  9624. for (level = 0; level <= max_level; level++) {
  9625. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9626. &sw_plane_wm->wm[level]))
  9627. continue;
  9628. 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",
  9629. pipe_name(pipe), level,
  9630. sw_plane_wm->wm[level].plane_en,
  9631. sw_plane_wm->wm[level].plane_res_b,
  9632. sw_plane_wm->wm[level].plane_res_l,
  9633. hw_plane_wm->wm[level].plane_en,
  9634. hw_plane_wm->wm[level].plane_res_b,
  9635. hw_plane_wm->wm[level].plane_res_l);
  9636. }
  9637. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9638. &sw_plane_wm->trans_wm)) {
  9639. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9640. pipe_name(pipe),
  9641. sw_plane_wm->trans_wm.plane_en,
  9642. sw_plane_wm->trans_wm.plane_res_b,
  9643. sw_plane_wm->trans_wm.plane_res_l,
  9644. hw_plane_wm->trans_wm.plane_en,
  9645. hw_plane_wm->trans_wm.plane_res_b,
  9646. hw_plane_wm->trans_wm.plane_res_l);
  9647. }
  9648. /* DDB */
  9649. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9650. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9651. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9652. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9653. pipe_name(pipe),
  9654. sw_ddb_entry->start, sw_ddb_entry->end,
  9655. hw_ddb_entry->start, hw_ddb_entry->end);
  9656. }
  9657. }
  9658. }
  9659. static void
  9660. verify_connector_state(struct drm_device *dev,
  9661. struct drm_atomic_state *state,
  9662. struct drm_crtc *crtc)
  9663. {
  9664. struct drm_connector *connector;
  9665. struct drm_connector_state *new_conn_state;
  9666. int i;
  9667. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9668. struct drm_encoder *encoder = connector->encoder;
  9669. struct drm_crtc_state *crtc_state = NULL;
  9670. if (new_conn_state->crtc != crtc)
  9671. continue;
  9672. if (crtc)
  9673. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9674. intel_connector_verify_state(crtc_state, new_conn_state);
  9675. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9676. "connector's atomic encoder doesn't match legacy encoder\n");
  9677. }
  9678. }
  9679. static void
  9680. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9681. {
  9682. struct intel_encoder *encoder;
  9683. struct drm_connector *connector;
  9684. struct drm_connector_state *old_conn_state, *new_conn_state;
  9685. int i;
  9686. for_each_intel_encoder(dev, encoder) {
  9687. bool enabled = false, found = false;
  9688. enum pipe pipe;
  9689. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9690. encoder->base.base.id,
  9691. encoder->base.name);
  9692. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9693. new_conn_state, i) {
  9694. if (old_conn_state->best_encoder == &encoder->base)
  9695. found = true;
  9696. if (new_conn_state->best_encoder != &encoder->base)
  9697. continue;
  9698. found = enabled = true;
  9699. I915_STATE_WARN(new_conn_state->crtc !=
  9700. encoder->base.crtc,
  9701. "connector's crtc doesn't match encoder crtc\n");
  9702. }
  9703. if (!found)
  9704. continue;
  9705. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9706. "encoder's enabled state mismatch "
  9707. "(expected %i, found %i)\n",
  9708. !!encoder->base.crtc, enabled);
  9709. if (!encoder->base.crtc) {
  9710. bool active;
  9711. active = encoder->get_hw_state(encoder, &pipe);
  9712. I915_STATE_WARN(active,
  9713. "encoder detached but still enabled on pipe %c.\n",
  9714. pipe_name(pipe));
  9715. }
  9716. }
  9717. }
  9718. static void
  9719. verify_crtc_state(struct drm_crtc *crtc,
  9720. struct drm_crtc_state *old_crtc_state,
  9721. struct drm_crtc_state *new_crtc_state)
  9722. {
  9723. struct drm_device *dev = crtc->dev;
  9724. struct drm_i915_private *dev_priv = to_i915(dev);
  9725. struct intel_encoder *encoder;
  9726. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9727. struct intel_crtc_state *pipe_config, *sw_config;
  9728. struct drm_atomic_state *old_state;
  9729. bool active;
  9730. old_state = old_crtc_state->state;
  9731. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9732. pipe_config = to_intel_crtc_state(old_crtc_state);
  9733. memset(pipe_config, 0, sizeof(*pipe_config));
  9734. pipe_config->base.crtc = crtc;
  9735. pipe_config->base.state = old_state;
  9736. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9737. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9738. /* we keep both pipes enabled on 830 */
  9739. if (IS_I830(dev_priv))
  9740. active = new_crtc_state->active;
  9741. I915_STATE_WARN(new_crtc_state->active != active,
  9742. "crtc active state doesn't match with hw state "
  9743. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9744. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9745. "transitional active state does not match atomic hw state "
  9746. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9747. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9748. enum pipe pipe;
  9749. active = encoder->get_hw_state(encoder, &pipe);
  9750. I915_STATE_WARN(active != new_crtc_state->active,
  9751. "[ENCODER:%i] active %i with crtc active %i\n",
  9752. encoder->base.base.id, active, new_crtc_state->active);
  9753. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9754. "Encoder connected to wrong pipe %c\n",
  9755. pipe_name(pipe));
  9756. if (active)
  9757. encoder->get_config(encoder, pipe_config);
  9758. }
  9759. intel_crtc_compute_pixel_rate(pipe_config);
  9760. if (!new_crtc_state->active)
  9761. return;
  9762. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9763. sw_config = to_intel_crtc_state(new_crtc_state);
  9764. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9765. pipe_config, false)) {
  9766. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9767. intel_dump_pipe_config(intel_crtc, pipe_config,
  9768. "[hw state]");
  9769. intel_dump_pipe_config(intel_crtc, sw_config,
  9770. "[sw state]");
  9771. }
  9772. }
  9773. static void
  9774. intel_verify_planes(struct intel_atomic_state *state)
  9775. {
  9776. struct intel_plane *plane;
  9777. const struct intel_plane_state *plane_state;
  9778. int i;
  9779. for_each_new_intel_plane_in_state(state, plane,
  9780. plane_state, i)
  9781. assert_plane(plane, plane_state->base.visible);
  9782. }
  9783. static void
  9784. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9785. struct intel_shared_dpll *pll,
  9786. struct drm_crtc *crtc,
  9787. struct drm_crtc_state *new_state)
  9788. {
  9789. struct intel_dpll_hw_state dpll_hw_state;
  9790. unsigned crtc_mask;
  9791. bool active;
  9792. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9793. DRM_DEBUG_KMS("%s\n", pll->info->name);
  9794. active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9795. if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
  9796. I915_STATE_WARN(!pll->on && pll->active_mask,
  9797. "pll in active use but not on in sw tracking\n");
  9798. I915_STATE_WARN(pll->on && !pll->active_mask,
  9799. "pll is on but not used by any active crtc\n");
  9800. I915_STATE_WARN(pll->on != active,
  9801. "pll on state mismatch (expected %i, found %i)\n",
  9802. pll->on, active);
  9803. }
  9804. if (!crtc) {
  9805. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9806. "more active pll users than references: %x vs %x\n",
  9807. pll->active_mask, pll->state.crtc_mask);
  9808. return;
  9809. }
  9810. crtc_mask = 1 << drm_crtc_index(crtc);
  9811. if (new_state->active)
  9812. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9813. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9814. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9815. else
  9816. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9817. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9818. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9819. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9820. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9821. crtc_mask, pll->state.crtc_mask);
  9822. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9823. &dpll_hw_state,
  9824. sizeof(dpll_hw_state)),
  9825. "pll hw state mismatch\n");
  9826. }
  9827. static void
  9828. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9829. struct drm_crtc_state *old_crtc_state,
  9830. struct drm_crtc_state *new_crtc_state)
  9831. {
  9832. struct drm_i915_private *dev_priv = to_i915(dev);
  9833. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9834. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9835. if (new_state->shared_dpll)
  9836. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9837. if (old_state->shared_dpll &&
  9838. old_state->shared_dpll != new_state->shared_dpll) {
  9839. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9840. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9841. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9842. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9843. pipe_name(drm_crtc_index(crtc)));
  9844. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9845. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9846. pipe_name(drm_crtc_index(crtc)));
  9847. }
  9848. }
  9849. static void
  9850. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9851. struct drm_atomic_state *state,
  9852. struct drm_crtc_state *old_state,
  9853. struct drm_crtc_state *new_state)
  9854. {
  9855. if (!needs_modeset(new_state) &&
  9856. !to_intel_crtc_state(new_state)->update_pipe)
  9857. return;
  9858. verify_wm_state(crtc, new_state);
  9859. verify_connector_state(crtc->dev, state, crtc);
  9860. verify_crtc_state(crtc, old_state, new_state);
  9861. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9862. }
  9863. static void
  9864. verify_disabled_dpll_state(struct drm_device *dev)
  9865. {
  9866. struct drm_i915_private *dev_priv = to_i915(dev);
  9867. int i;
  9868. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9869. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9870. }
  9871. static void
  9872. intel_modeset_verify_disabled(struct drm_device *dev,
  9873. struct drm_atomic_state *state)
  9874. {
  9875. verify_encoder_state(dev, state);
  9876. verify_connector_state(dev, state, NULL);
  9877. verify_disabled_dpll_state(dev);
  9878. }
  9879. static void update_scanline_offset(struct intel_crtc *crtc)
  9880. {
  9881. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9882. /*
  9883. * The scanline counter increments at the leading edge of hsync.
  9884. *
  9885. * On most platforms it starts counting from vtotal-1 on the
  9886. * first active line. That means the scanline counter value is
  9887. * always one less than what we would expect. Ie. just after
  9888. * start of vblank, which also occurs at start of hsync (on the
  9889. * last active line), the scanline counter will read vblank_start-1.
  9890. *
  9891. * On gen2 the scanline counter starts counting from 1 instead
  9892. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9893. * to keep the value positive), instead of adding one.
  9894. *
  9895. * On HSW+ the behaviour of the scanline counter depends on the output
  9896. * type. For DP ports it behaves like most other platforms, but on HDMI
  9897. * there's an extra 1 line difference. So we need to add two instead of
  9898. * one to the value.
  9899. *
  9900. * On VLV/CHV DSI the scanline counter would appear to increment
  9901. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9902. * that means we can't tell whether we're in vblank or not while
  9903. * we're on that particular line. We must still set scanline_offset
  9904. * to 1 so that the vblank timestamps come out correct when we query
  9905. * the scanline counter from within the vblank interrupt handler.
  9906. * However if queried just before the start of vblank we'll get an
  9907. * answer that's slightly in the future.
  9908. */
  9909. if (IS_GEN2(dev_priv)) {
  9910. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9911. int vtotal;
  9912. vtotal = adjusted_mode->crtc_vtotal;
  9913. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9914. vtotal /= 2;
  9915. crtc->scanline_offset = vtotal - 1;
  9916. } else if (HAS_DDI(dev_priv) &&
  9917. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9918. crtc->scanline_offset = 2;
  9919. } else
  9920. crtc->scanline_offset = 1;
  9921. }
  9922. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9923. {
  9924. struct drm_device *dev = state->dev;
  9925. struct drm_i915_private *dev_priv = to_i915(dev);
  9926. struct drm_crtc *crtc;
  9927. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9928. int i;
  9929. if (!dev_priv->display.crtc_compute_clock)
  9930. return;
  9931. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9933. struct intel_shared_dpll *old_dpll =
  9934. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9935. if (!needs_modeset(new_crtc_state))
  9936. continue;
  9937. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9938. if (!old_dpll)
  9939. continue;
  9940. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9941. }
  9942. }
  9943. /*
  9944. * This implements the workaround described in the "notes" section of the mode
  9945. * set sequence documentation. When going from no pipes or single pipe to
  9946. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9947. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9948. */
  9949. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9950. {
  9951. struct drm_crtc_state *crtc_state;
  9952. struct intel_crtc *intel_crtc;
  9953. struct drm_crtc *crtc;
  9954. struct intel_crtc_state *first_crtc_state = NULL;
  9955. struct intel_crtc_state *other_crtc_state = NULL;
  9956. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9957. int i;
  9958. /* look at all crtc's that are going to be enabled in during modeset */
  9959. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9960. intel_crtc = to_intel_crtc(crtc);
  9961. if (!crtc_state->active || !needs_modeset(crtc_state))
  9962. continue;
  9963. if (first_crtc_state) {
  9964. other_crtc_state = to_intel_crtc_state(crtc_state);
  9965. break;
  9966. } else {
  9967. first_crtc_state = to_intel_crtc_state(crtc_state);
  9968. first_pipe = intel_crtc->pipe;
  9969. }
  9970. }
  9971. /* No workaround needed? */
  9972. if (!first_crtc_state)
  9973. return 0;
  9974. /* w/a possibly needed, check how many crtc's are already enabled. */
  9975. for_each_intel_crtc(state->dev, intel_crtc) {
  9976. struct intel_crtc_state *pipe_config;
  9977. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9978. if (IS_ERR(pipe_config))
  9979. return PTR_ERR(pipe_config);
  9980. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9981. if (!pipe_config->base.active ||
  9982. needs_modeset(&pipe_config->base))
  9983. continue;
  9984. /* 2 or more enabled crtcs means no need for w/a */
  9985. if (enabled_pipe != INVALID_PIPE)
  9986. return 0;
  9987. enabled_pipe = intel_crtc->pipe;
  9988. }
  9989. if (enabled_pipe != INVALID_PIPE)
  9990. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9991. else if (other_crtc_state)
  9992. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9993. return 0;
  9994. }
  9995. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9996. {
  9997. struct drm_crtc *crtc;
  9998. /* Add all pipes to the state */
  9999. for_each_crtc(state->dev, crtc) {
  10000. struct drm_crtc_state *crtc_state;
  10001. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10002. if (IS_ERR(crtc_state))
  10003. return PTR_ERR(crtc_state);
  10004. }
  10005. return 0;
  10006. }
  10007. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10008. {
  10009. struct drm_crtc *crtc;
  10010. /*
  10011. * Add all pipes to the state, and force
  10012. * a modeset on all the active ones.
  10013. */
  10014. for_each_crtc(state->dev, crtc) {
  10015. struct drm_crtc_state *crtc_state;
  10016. int ret;
  10017. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10018. if (IS_ERR(crtc_state))
  10019. return PTR_ERR(crtc_state);
  10020. if (!crtc_state->active || needs_modeset(crtc_state))
  10021. continue;
  10022. crtc_state->mode_changed = true;
  10023. ret = drm_atomic_add_affected_connectors(state, crtc);
  10024. if (ret)
  10025. return ret;
  10026. ret = drm_atomic_add_affected_planes(state, crtc);
  10027. if (ret)
  10028. return ret;
  10029. }
  10030. return 0;
  10031. }
  10032. static int intel_modeset_checks(struct drm_atomic_state *state)
  10033. {
  10034. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10035. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10036. struct drm_crtc *crtc;
  10037. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10038. int ret = 0, i;
  10039. if (!check_digital_port_conflicts(state)) {
  10040. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10041. return -EINVAL;
  10042. }
  10043. intel_state->modeset = true;
  10044. intel_state->active_crtcs = dev_priv->active_crtcs;
  10045. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10046. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10047. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10048. if (new_crtc_state->active)
  10049. intel_state->active_crtcs |= 1 << i;
  10050. else
  10051. intel_state->active_crtcs &= ~(1 << i);
  10052. if (old_crtc_state->active != new_crtc_state->active)
  10053. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10054. }
  10055. /*
  10056. * See if the config requires any additional preparation, e.g.
  10057. * to adjust global state with pipes off. We need to do this
  10058. * here so we can get the modeset_pipe updated config for the new
  10059. * mode set on this crtc. For other crtcs we need to use the
  10060. * adjusted_mode bits in the crtc directly.
  10061. */
  10062. if (dev_priv->display.modeset_calc_cdclk) {
  10063. ret = dev_priv->display.modeset_calc_cdclk(state);
  10064. if (ret < 0)
  10065. return ret;
  10066. /*
  10067. * Writes to dev_priv->cdclk.logical must protected by
  10068. * holding all the crtc locks, even if we don't end up
  10069. * touching the hardware
  10070. */
  10071. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10072. &intel_state->cdclk.logical)) {
  10073. ret = intel_lock_all_pipes(state);
  10074. if (ret < 0)
  10075. return ret;
  10076. }
  10077. /* All pipes must be switched off while we change the cdclk. */
  10078. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10079. &intel_state->cdclk.actual)) {
  10080. ret = intel_modeset_all_pipes(state);
  10081. if (ret < 0)
  10082. return ret;
  10083. }
  10084. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10085. intel_state->cdclk.logical.cdclk,
  10086. intel_state->cdclk.actual.cdclk);
  10087. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10088. intel_state->cdclk.logical.voltage_level,
  10089. intel_state->cdclk.actual.voltage_level);
  10090. } else {
  10091. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10092. }
  10093. intel_modeset_clear_plls(state);
  10094. if (IS_HASWELL(dev_priv))
  10095. return haswell_mode_set_planes_workaround(state);
  10096. return 0;
  10097. }
  10098. /*
  10099. * Handle calculation of various watermark data at the end of the atomic check
  10100. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10101. * handlers to ensure that all derived state has been updated.
  10102. */
  10103. static int calc_watermark_data(struct drm_atomic_state *state)
  10104. {
  10105. struct drm_device *dev = state->dev;
  10106. struct drm_i915_private *dev_priv = to_i915(dev);
  10107. /* Is there platform-specific watermark information to calculate? */
  10108. if (dev_priv->display.compute_global_watermarks)
  10109. return dev_priv->display.compute_global_watermarks(state);
  10110. return 0;
  10111. }
  10112. /**
  10113. * intel_atomic_check - validate state object
  10114. * @dev: drm device
  10115. * @state: state to validate
  10116. */
  10117. static int intel_atomic_check(struct drm_device *dev,
  10118. struct drm_atomic_state *state)
  10119. {
  10120. struct drm_i915_private *dev_priv = to_i915(dev);
  10121. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10122. struct drm_crtc *crtc;
  10123. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10124. int ret, i;
  10125. bool any_ms = false;
  10126. /* Catch I915_MODE_FLAG_INHERITED */
  10127. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  10128. crtc_state, i) {
  10129. if (crtc_state->mode.private_flags !=
  10130. old_crtc_state->mode.private_flags)
  10131. crtc_state->mode_changed = true;
  10132. }
  10133. ret = drm_atomic_helper_check_modeset(dev, state);
  10134. if (ret)
  10135. return ret;
  10136. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10137. struct intel_crtc_state *pipe_config =
  10138. to_intel_crtc_state(crtc_state);
  10139. if (!needs_modeset(crtc_state))
  10140. continue;
  10141. if (!crtc_state->enable) {
  10142. any_ms = true;
  10143. continue;
  10144. }
  10145. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10146. if (ret) {
  10147. intel_dump_pipe_config(to_intel_crtc(crtc),
  10148. pipe_config, "[failed]");
  10149. return ret;
  10150. }
  10151. if (i915_modparams.fastboot &&
  10152. intel_pipe_config_compare(dev_priv,
  10153. to_intel_crtc_state(old_crtc_state),
  10154. pipe_config, true)) {
  10155. crtc_state->mode_changed = false;
  10156. pipe_config->update_pipe = true;
  10157. }
  10158. if (needs_modeset(crtc_state))
  10159. any_ms = true;
  10160. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10161. needs_modeset(crtc_state) ?
  10162. "[modeset]" : "[fastset]");
  10163. }
  10164. if (any_ms) {
  10165. ret = intel_modeset_checks(state);
  10166. if (ret)
  10167. return ret;
  10168. } else {
  10169. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10170. }
  10171. ret = drm_atomic_helper_check_planes(dev, state);
  10172. if (ret)
  10173. return ret;
  10174. intel_fbc_choose_crtc(dev_priv, intel_state);
  10175. return calc_watermark_data(state);
  10176. }
  10177. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10178. struct drm_atomic_state *state)
  10179. {
  10180. return drm_atomic_helper_prepare_planes(dev, state);
  10181. }
  10182. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10183. {
  10184. struct drm_device *dev = crtc->base.dev;
  10185. if (!dev->max_vblank_count)
  10186. return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
  10187. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10188. }
  10189. static void intel_update_crtc(struct drm_crtc *crtc,
  10190. struct drm_atomic_state *state,
  10191. struct drm_crtc_state *old_crtc_state,
  10192. struct drm_crtc_state *new_crtc_state)
  10193. {
  10194. struct drm_device *dev = crtc->dev;
  10195. struct drm_i915_private *dev_priv = to_i915(dev);
  10196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10197. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10198. bool modeset = needs_modeset(new_crtc_state);
  10199. struct intel_plane_state *new_plane_state =
  10200. intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
  10201. to_intel_plane(crtc->primary));
  10202. if (modeset) {
  10203. update_scanline_offset(intel_crtc);
  10204. dev_priv->display.crtc_enable(pipe_config, state);
  10205. /* vblanks work again, re-enable pipe CRC. */
  10206. intel_crtc_enable_pipe_crc(intel_crtc);
  10207. } else {
  10208. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10209. pipe_config);
  10210. }
  10211. if (new_plane_state)
  10212. intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
  10213. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10214. }
  10215. static void intel_update_crtcs(struct drm_atomic_state *state)
  10216. {
  10217. struct drm_crtc *crtc;
  10218. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10219. int i;
  10220. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10221. if (!new_crtc_state->active)
  10222. continue;
  10223. intel_update_crtc(crtc, state, old_crtc_state,
  10224. new_crtc_state);
  10225. }
  10226. }
  10227. static void skl_update_crtcs(struct drm_atomic_state *state)
  10228. {
  10229. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10230. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10231. struct drm_crtc *crtc;
  10232. struct intel_crtc *intel_crtc;
  10233. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10234. struct intel_crtc_state *cstate;
  10235. unsigned int updated = 0;
  10236. bool progress;
  10237. enum pipe pipe;
  10238. int i;
  10239. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10240. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10241. /* ignore allocations for crtc's that have been turned off. */
  10242. if (new_crtc_state->active)
  10243. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10244. /*
  10245. * Whenever the number of active pipes changes, we need to make sure we
  10246. * update the pipes in the right order so that their ddb allocations
  10247. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10248. * cause pipe underruns and other bad stuff.
  10249. */
  10250. do {
  10251. progress = false;
  10252. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10253. bool vbl_wait = false;
  10254. unsigned int cmask = drm_crtc_mask(crtc);
  10255. intel_crtc = to_intel_crtc(crtc);
  10256. cstate = to_intel_crtc_state(new_crtc_state);
  10257. pipe = intel_crtc->pipe;
  10258. if (updated & cmask || !cstate->base.active)
  10259. continue;
  10260. if (skl_ddb_allocation_overlaps(dev_priv,
  10261. entries,
  10262. &cstate->wm.skl.ddb,
  10263. i))
  10264. continue;
  10265. updated |= cmask;
  10266. entries[i] = &cstate->wm.skl.ddb;
  10267. /*
  10268. * If this is an already active pipe, it's DDB changed,
  10269. * and this isn't the last pipe that needs updating
  10270. * then we need to wait for a vblank to pass for the
  10271. * new ddb allocation to take effect.
  10272. */
  10273. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10274. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10275. !new_crtc_state->active_changed &&
  10276. intel_state->wm_results.dirty_pipes != updated)
  10277. vbl_wait = true;
  10278. intel_update_crtc(crtc, state, old_crtc_state,
  10279. new_crtc_state);
  10280. if (vbl_wait)
  10281. intel_wait_for_vblank(dev_priv, pipe);
  10282. progress = true;
  10283. }
  10284. } while (progress);
  10285. }
  10286. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10287. {
  10288. struct intel_atomic_state *state, *next;
  10289. struct llist_node *freed;
  10290. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10291. llist_for_each_entry_safe(state, next, freed, freed)
  10292. drm_atomic_state_put(&state->base);
  10293. }
  10294. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10295. {
  10296. struct drm_i915_private *dev_priv =
  10297. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10298. intel_atomic_helper_free_state(dev_priv);
  10299. }
  10300. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10301. {
  10302. struct wait_queue_entry wait_fence, wait_reset;
  10303. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10304. init_wait_entry(&wait_fence, 0);
  10305. init_wait_entry(&wait_reset, 0);
  10306. for (;;) {
  10307. prepare_to_wait(&intel_state->commit_ready.wait,
  10308. &wait_fence, TASK_UNINTERRUPTIBLE);
  10309. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10310. &wait_reset, TASK_UNINTERRUPTIBLE);
  10311. if (i915_sw_fence_done(&intel_state->commit_ready)
  10312. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10313. break;
  10314. schedule();
  10315. }
  10316. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10317. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10318. }
  10319. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10320. {
  10321. struct drm_device *dev = state->dev;
  10322. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10323. struct drm_i915_private *dev_priv = to_i915(dev);
  10324. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10325. struct drm_crtc *crtc;
  10326. struct intel_crtc_state *intel_cstate;
  10327. u64 put_domains[I915_MAX_PIPES] = {};
  10328. int i;
  10329. intel_atomic_commit_fence_wait(intel_state);
  10330. drm_atomic_helper_wait_for_dependencies(state);
  10331. if (intel_state->modeset)
  10332. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10333. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10334. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10335. if (needs_modeset(new_crtc_state) ||
  10336. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10337. put_domains[to_intel_crtc(crtc)->pipe] =
  10338. modeset_get_crtc_power_domains(crtc,
  10339. to_intel_crtc_state(new_crtc_state));
  10340. }
  10341. if (!needs_modeset(new_crtc_state))
  10342. continue;
  10343. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10344. to_intel_crtc_state(new_crtc_state));
  10345. if (old_crtc_state->active) {
  10346. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10347. /*
  10348. * We need to disable pipe CRC before disabling the pipe,
  10349. * or we race against vblank off.
  10350. */
  10351. intel_crtc_disable_pipe_crc(intel_crtc);
  10352. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10353. intel_crtc->active = false;
  10354. intel_fbc_disable(intel_crtc);
  10355. intel_disable_shared_dpll(intel_crtc);
  10356. /*
  10357. * Underruns don't always raise
  10358. * interrupts, so check manually.
  10359. */
  10360. intel_check_cpu_fifo_underruns(dev_priv);
  10361. intel_check_pch_fifo_underruns(dev_priv);
  10362. if (!new_crtc_state->active) {
  10363. /*
  10364. * Make sure we don't call initial_watermarks
  10365. * for ILK-style watermark updates.
  10366. *
  10367. * No clue what this is supposed to achieve.
  10368. */
  10369. if (INTEL_GEN(dev_priv) >= 9)
  10370. dev_priv->display.initial_watermarks(intel_state,
  10371. to_intel_crtc_state(new_crtc_state));
  10372. }
  10373. }
  10374. }
  10375. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10376. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10377. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10378. if (intel_state->modeset) {
  10379. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10380. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10381. /*
  10382. * SKL workaround: bspec recommends we disable the SAGV when we
  10383. * have more then one pipe enabled
  10384. */
  10385. if (!intel_can_enable_sagv(state))
  10386. intel_disable_sagv(dev_priv);
  10387. intel_modeset_verify_disabled(dev, state);
  10388. }
  10389. /* Complete the events for pipes that have now been disabled */
  10390. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10391. bool modeset = needs_modeset(new_crtc_state);
  10392. /* Complete events for now disable pipes here. */
  10393. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10394. spin_lock_irq(&dev->event_lock);
  10395. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10396. spin_unlock_irq(&dev->event_lock);
  10397. new_crtc_state->event = NULL;
  10398. }
  10399. }
  10400. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10401. dev_priv->display.update_crtcs(state);
  10402. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10403. * already, but still need the state for the delayed optimization. To
  10404. * fix this:
  10405. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10406. * - schedule that vblank worker _before_ calling hw_done
  10407. * - at the start of commit_tail, cancel it _synchrously
  10408. * - switch over to the vblank wait helper in the core after that since
  10409. * we don't need out special handling any more.
  10410. */
  10411. drm_atomic_helper_wait_for_flip_done(dev, state);
  10412. /*
  10413. * Now that the vblank has passed, we can go ahead and program the
  10414. * optimal watermarks on platforms that need two-step watermark
  10415. * programming.
  10416. *
  10417. * TODO: Move this (and other cleanup) to an async worker eventually.
  10418. */
  10419. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10420. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10421. if (dev_priv->display.optimize_watermarks)
  10422. dev_priv->display.optimize_watermarks(intel_state,
  10423. intel_cstate);
  10424. }
  10425. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10426. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10427. if (put_domains[i])
  10428. modeset_put_power_domains(dev_priv, put_domains[i]);
  10429. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10430. }
  10431. if (intel_state->modeset)
  10432. intel_verify_planes(intel_state);
  10433. if (intel_state->modeset && intel_can_enable_sagv(state))
  10434. intel_enable_sagv(dev_priv);
  10435. drm_atomic_helper_commit_hw_done(state);
  10436. if (intel_state->modeset) {
  10437. /* As one of the primary mmio accessors, KMS has a high
  10438. * likelihood of triggering bugs in unclaimed access. After we
  10439. * finish modesetting, see if an error has been flagged, and if
  10440. * so enable debugging for the next modeset - and hope we catch
  10441. * the culprit.
  10442. */
  10443. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10444. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10445. }
  10446. drm_atomic_helper_cleanup_planes(dev, state);
  10447. drm_atomic_helper_commit_cleanup_done(state);
  10448. drm_atomic_state_put(state);
  10449. intel_atomic_helper_free_state(dev_priv);
  10450. }
  10451. static void intel_atomic_commit_work(struct work_struct *work)
  10452. {
  10453. struct drm_atomic_state *state =
  10454. container_of(work, struct drm_atomic_state, commit_work);
  10455. intel_atomic_commit_tail(state);
  10456. }
  10457. static int __i915_sw_fence_call
  10458. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10459. enum i915_sw_fence_notify notify)
  10460. {
  10461. struct intel_atomic_state *state =
  10462. container_of(fence, struct intel_atomic_state, commit_ready);
  10463. switch (notify) {
  10464. case FENCE_COMPLETE:
  10465. /* we do blocking waits in the worker, nothing to do here */
  10466. break;
  10467. case FENCE_FREE:
  10468. {
  10469. struct intel_atomic_helper *helper =
  10470. &to_i915(state->base.dev)->atomic_helper;
  10471. if (llist_add(&state->freed, &helper->free_list))
  10472. schedule_work(&helper->free_work);
  10473. break;
  10474. }
  10475. }
  10476. return NOTIFY_DONE;
  10477. }
  10478. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10479. {
  10480. struct drm_plane_state *old_plane_state, *new_plane_state;
  10481. struct drm_plane *plane;
  10482. int i;
  10483. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10484. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10485. intel_fb_obj(new_plane_state->fb),
  10486. to_intel_plane(plane)->frontbuffer_bit);
  10487. }
  10488. /**
  10489. * intel_atomic_commit - commit validated state object
  10490. * @dev: DRM device
  10491. * @state: the top-level driver state object
  10492. * @nonblock: nonblocking commit
  10493. *
  10494. * This function commits a top-level state object that has been validated
  10495. * with drm_atomic_helper_check().
  10496. *
  10497. * RETURNS
  10498. * Zero for success or -errno.
  10499. */
  10500. static int intel_atomic_commit(struct drm_device *dev,
  10501. struct drm_atomic_state *state,
  10502. bool nonblock)
  10503. {
  10504. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10505. struct drm_i915_private *dev_priv = to_i915(dev);
  10506. int ret = 0;
  10507. drm_atomic_state_get(state);
  10508. i915_sw_fence_init(&intel_state->commit_ready,
  10509. intel_atomic_commit_ready);
  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. * Unset state->legacy_cursor_update before the call to
  10519. * drm_atomic_helper_setup_commit() because otherwise
  10520. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10521. * we get FIFO underruns because we didn't wait
  10522. * for vblank.
  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 && state->legacy_cursor_update) {
  10528. struct intel_crtc_state *new_crtc_state;
  10529. struct intel_crtc *crtc;
  10530. int i;
  10531. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10532. if (new_crtc_state->wm.need_postvbl_update ||
  10533. new_crtc_state->update_wm_post)
  10534. state->legacy_cursor_update = false;
  10535. }
  10536. ret = intel_atomic_prepare_commit(dev, state);
  10537. if (ret) {
  10538. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10539. i915_sw_fence_commit(&intel_state->commit_ready);
  10540. return ret;
  10541. }
  10542. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10543. if (!ret)
  10544. ret = drm_atomic_helper_swap_state(state, true);
  10545. if (ret) {
  10546. i915_sw_fence_commit(&intel_state->commit_ready);
  10547. drm_atomic_helper_cleanup_planes(dev, state);
  10548. return ret;
  10549. }
  10550. dev_priv->wm.distrust_bios_wm = false;
  10551. intel_shared_dpll_swap_state(state);
  10552. intel_atomic_track_fbs(state);
  10553. if (intel_state->modeset) {
  10554. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10555. sizeof(intel_state->min_cdclk));
  10556. memcpy(dev_priv->min_voltage_level,
  10557. intel_state->min_voltage_level,
  10558. sizeof(intel_state->min_voltage_level));
  10559. dev_priv->active_crtcs = intel_state->active_crtcs;
  10560. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10561. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10562. }
  10563. drm_atomic_state_get(state);
  10564. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10565. i915_sw_fence_commit(&intel_state->commit_ready);
  10566. if (nonblock && intel_state->modeset) {
  10567. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10568. } else if (nonblock) {
  10569. queue_work(system_unbound_wq, &state->commit_work);
  10570. } else {
  10571. if (intel_state->modeset)
  10572. flush_workqueue(dev_priv->modeset_wq);
  10573. intel_atomic_commit_tail(state);
  10574. }
  10575. return 0;
  10576. }
  10577. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10578. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10579. .set_config = drm_atomic_helper_set_config,
  10580. .destroy = intel_crtc_destroy,
  10581. .page_flip = drm_atomic_helper_page_flip,
  10582. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10583. .atomic_destroy_state = intel_crtc_destroy_state,
  10584. .set_crc_source = intel_crtc_set_crc_source,
  10585. };
  10586. struct wait_rps_boost {
  10587. struct wait_queue_entry wait;
  10588. struct drm_crtc *crtc;
  10589. struct i915_request *request;
  10590. };
  10591. static int do_rps_boost(struct wait_queue_entry *_wait,
  10592. unsigned mode, int sync, void *key)
  10593. {
  10594. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10595. struct i915_request *rq = wait->request;
  10596. /*
  10597. * If we missed the vblank, but the request is already running it
  10598. * is reasonable to assume that it will complete before the next
  10599. * vblank without our intervention, so leave RPS alone.
  10600. */
  10601. if (!i915_request_started(rq))
  10602. gen6_rps_boost(rq, NULL);
  10603. i915_request_put(rq);
  10604. drm_crtc_vblank_put(wait->crtc);
  10605. list_del(&wait->wait.entry);
  10606. kfree(wait);
  10607. return 1;
  10608. }
  10609. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10610. struct dma_fence *fence)
  10611. {
  10612. struct wait_rps_boost *wait;
  10613. if (!dma_fence_is_i915(fence))
  10614. return;
  10615. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10616. return;
  10617. if (drm_crtc_vblank_get(crtc))
  10618. return;
  10619. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10620. if (!wait) {
  10621. drm_crtc_vblank_put(crtc);
  10622. return;
  10623. }
  10624. wait->request = to_request(dma_fence_get(fence));
  10625. wait->crtc = crtc;
  10626. wait->wait.func = do_rps_boost;
  10627. wait->wait.flags = 0;
  10628. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10629. }
  10630. static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
  10631. {
  10632. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  10633. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10634. struct drm_framebuffer *fb = plane_state->base.fb;
  10635. struct i915_vma *vma;
  10636. if (plane->id == PLANE_CURSOR &&
  10637. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10638. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10639. const int align = intel_cursor_alignment(dev_priv);
  10640. return i915_gem_object_attach_phys(obj, align);
  10641. }
  10642. vma = intel_pin_and_fence_fb_obj(fb,
  10643. plane_state->base.rotation,
  10644. intel_plane_uses_fence(plane_state),
  10645. &plane_state->flags);
  10646. if (IS_ERR(vma))
  10647. return PTR_ERR(vma);
  10648. plane_state->vma = vma;
  10649. return 0;
  10650. }
  10651. static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
  10652. {
  10653. struct i915_vma *vma;
  10654. vma = fetch_and_zero(&old_plane_state->vma);
  10655. if (vma)
  10656. intel_unpin_fb_vma(vma, old_plane_state->flags);
  10657. }
  10658. static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
  10659. {
  10660. struct i915_sched_attr attr = {
  10661. .priority = I915_PRIORITY_DISPLAY,
  10662. };
  10663. i915_gem_object_wait_priority(obj, 0, &attr);
  10664. }
  10665. /**
  10666. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10667. * @plane: drm plane to prepare for
  10668. * @new_state: the plane state being prepared
  10669. *
  10670. * Prepares a framebuffer for usage on a display plane. Generally this
  10671. * involves pinning the underlying object and updating the frontbuffer tracking
  10672. * bits. Some older platforms need special physical address handling for
  10673. * cursor planes.
  10674. *
  10675. * Must be called with struct_mutex held.
  10676. *
  10677. * Returns 0 on success, negative error code on failure.
  10678. */
  10679. int
  10680. intel_prepare_plane_fb(struct drm_plane *plane,
  10681. struct drm_plane_state *new_state)
  10682. {
  10683. struct intel_atomic_state *intel_state =
  10684. to_intel_atomic_state(new_state->state);
  10685. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10686. struct drm_framebuffer *fb = new_state->fb;
  10687. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10688. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10689. int ret;
  10690. if (old_obj) {
  10691. struct drm_crtc_state *crtc_state =
  10692. drm_atomic_get_new_crtc_state(new_state->state,
  10693. plane->state->crtc);
  10694. /* Big Hammer, we also need to ensure that any pending
  10695. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10696. * current scanout is retired before unpinning the old
  10697. * framebuffer. Note that we rely on userspace rendering
  10698. * into the buffer attached to the pipe they are waiting
  10699. * on. If not, userspace generates a GPU hang with IPEHR
  10700. * point to the MI_WAIT_FOR_EVENT.
  10701. *
  10702. * This should only fail upon a hung GPU, in which case we
  10703. * can safely continue.
  10704. */
  10705. if (needs_modeset(crtc_state)) {
  10706. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10707. old_obj->resv, NULL,
  10708. false, 0,
  10709. GFP_KERNEL);
  10710. if (ret < 0)
  10711. return ret;
  10712. }
  10713. }
  10714. if (new_state->fence) { /* explicit fencing */
  10715. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10716. new_state->fence,
  10717. I915_FENCE_TIMEOUT,
  10718. GFP_KERNEL);
  10719. if (ret < 0)
  10720. return ret;
  10721. }
  10722. if (!obj)
  10723. return 0;
  10724. ret = i915_gem_object_pin_pages(obj);
  10725. if (ret)
  10726. return ret;
  10727. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10728. if (ret) {
  10729. i915_gem_object_unpin_pages(obj);
  10730. return ret;
  10731. }
  10732. ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
  10733. fb_obj_bump_render_priority(obj);
  10734. mutex_unlock(&dev_priv->drm.struct_mutex);
  10735. i915_gem_object_unpin_pages(obj);
  10736. if (ret)
  10737. return ret;
  10738. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  10739. if (!new_state->fence) { /* implicit fencing */
  10740. struct dma_fence *fence;
  10741. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10742. obj->resv, NULL,
  10743. false, I915_FENCE_TIMEOUT,
  10744. GFP_KERNEL);
  10745. if (ret < 0)
  10746. return ret;
  10747. fence = reservation_object_get_excl_rcu(obj->resv);
  10748. if (fence) {
  10749. add_rps_boost_after_vblank(new_state->crtc, fence);
  10750. dma_fence_put(fence);
  10751. }
  10752. } else {
  10753. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10754. }
  10755. return 0;
  10756. }
  10757. /**
  10758. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10759. * @plane: drm plane to clean up for
  10760. * @old_state: the state from the previous modeset
  10761. *
  10762. * Cleans up a framebuffer that has just been removed from a plane.
  10763. *
  10764. * Must be called with struct_mutex held.
  10765. */
  10766. void
  10767. intel_cleanup_plane_fb(struct drm_plane *plane,
  10768. struct drm_plane_state *old_state)
  10769. {
  10770. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10771. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10772. mutex_lock(&dev_priv->drm.struct_mutex);
  10773. intel_plane_unpin_fb(to_intel_plane_state(old_state));
  10774. mutex_unlock(&dev_priv->drm.struct_mutex);
  10775. }
  10776. int
  10777. skl_max_scale(struct intel_crtc *intel_crtc,
  10778. struct intel_crtc_state *crtc_state,
  10779. uint32_t pixel_format)
  10780. {
  10781. struct drm_i915_private *dev_priv;
  10782. int max_scale, mult;
  10783. int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
  10784. if (!intel_crtc || !crtc_state->base.enable)
  10785. return DRM_PLANE_HELPER_NO_SCALING;
  10786. dev_priv = to_i915(intel_crtc->base.dev);
  10787. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10788. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10789. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10790. max_dotclk *= 2;
  10791. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10792. return DRM_PLANE_HELPER_NO_SCALING;
  10793. /*
  10794. * skl max scale is lower of:
  10795. * close to 3 but not 3, -1 is for that purpose
  10796. * or
  10797. * cdclk/crtc_clock
  10798. */
  10799. mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
  10800. tmpclk1 = (1 << 16) * mult - 1;
  10801. tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
  10802. max_scale = min(tmpclk1, tmpclk2);
  10803. return max_scale;
  10804. }
  10805. static int
  10806. intel_check_primary_plane(struct intel_plane *plane,
  10807. struct intel_crtc_state *crtc_state,
  10808. struct intel_plane_state *state)
  10809. {
  10810. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10811. struct drm_crtc *crtc = state->base.crtc;
  10812. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10813. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10814. bool can_position = false;
  10815. int ret;
  10816. uint32_t pixel_format = 0;
  10817. if (INTEL_GEN(dev_priv) >= 9) {
  10818. /* use scaler when colorkey is not required */
  10819. if (!state->ckey.flags) {
  10820. min_scale = 1;
  10821. if (state->base.fb)
  10822. pixel_format = state->base.fb->format->format;
  10823. max_scale = skl_max_scale(to_intel_crtc(crtc),
  10824. crtc_state, pixel_format);
  10825. }
  10826. can_position = true;
  10827. }
  10828. ret = drm_atomic_helper_check_plane_state(&state->base,
  10829. &crtc_state->base,
  10830. min_scale, max_scale,
  10831. can_position, true);
  10832. if (ret)
  10833. return ret;
  10834. if (!state->base.fb)
  10835. return 0;
  10836. if (INTEL_GEN(dev_priv) >= 9) {
  10837. ret = skl_check_plane_surface(crtc_state, state);
  10838. if (ret)
  10839. return ret;
  10840. state->ctl = skl_plane_ctl(crtc_state, state);
  10841. } else {
  10842. ret = i9xx_check_plane_surface(state);
  10843. if (ret)
  10844. return ret;
  10845. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10846. }
  10847. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10848. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10849. return 0;
  10850. }
  10851. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10852. struct drm_crtc_state *old_crtc_state)
  10853. {
  10854. struct drm_device *dev = crtc->dev;
  10855. struct drm_i915_private *dev_priv = to_i915(dev);
  10856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10857. struct intel_crtc_state *old_intel_cstate =
  10858. to_intel_crtc_state(old_crtc_state);
  10859. struct intel_atomic_state *old_intel_state =
  10860. to_intel_atomic_state(old_crtc_state->state);
  10861. struct intel_crtc_state *intel_cstate =
  10862. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10863. bool modeset = needs_modeset(&intel_cstate->base);
  10864. if (!modeset &&
  10865. (intel_cstate->base.color_mgmt_changed ||
  10866. intel_cstate->update_pipe)) {
  10867. intel_color_set_csc(&intel_cstate->base);
  10868. intel_color_load_luts(&intel_cstate->base);
  10869. }
  10870. /* Perform vblank evasion around commit operation */
  10871. intel_pipe_update_start(intel_cstate);
  10872. if (modeset)
  10873. goto out;
  10874. if (intel_cstate->update_pipe)
  10875. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10876. else if (INTEL_GEN(dev_priv) >= 9)
  10877. skl_detach_scalers(intel_crtc);
  10878. out:
  10879. if (dev_priv->display.atomic_update_watermarks)
  10880. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10881. intel_cstate);
  10882. }
  10883. void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
  10884. struct intel_crtc_state *crtc_state)
  10885. {
  10886. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10887. if (!IS_GEN2(dev_priv))
  10888. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
  10889. if (crtc_state->has_pch_encoder) {
  10890. enum pipe pch_transcoder =
  10891. intel_crtc_pch_transcoder(crtc);
  10892. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10893. }
  10894. }
  10895. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10896. struct drm_crtc_state *old_crtc_state)
  10897. {
  10898. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10899. struct intel_atomic_state *old_intel_state =
  10900. to_intel_atomic_state(old_crtc_state->state);
  10901. struct intel_crtc_state *new_crtc_state =
  10902. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10903. intel_pipe_update_end(new_crtc_state);
  10904. if (new_crtc_state->update_pipe &&
  10905. !needs_modeset(&new_crtc_state->base) &&
  10906. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
  10907. intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
  10908. }
  10909. /**
  10910. * intel_plane_destroy - destroy a plane
  10911. * @plane: plane to destroy
  10912. *
  10913. * Common destruction function for all types of planes (primary, cursor,
  10914. * sprite).
  10915. */
  10916. void intel_plane_destroy(struct drm_plane *plane)
  10917. {
  10918. drm_plane_cleanup(plane);
  10919. kfree(to_intel_plane(plane));
  10920. }
  10921. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10922. {
  10923. switch (format) {
  10924. case DRM_FORMAT_C8:
  10925. case DRM_FORMAT_RGB565:
  10926. case DRM_FORMAT_XRGB1555:
  10927. case DRM_FORMAT_XRGB8888:
  10928. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10929. modifier == I915_FORMAT_MOD_X_TILED;
  10930. default:
  10931. return false;
  10932. }
  10933. }
  10934. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10935. {
  10936. switch (format) {
  10937. case DRM_FORMAT_C8:
  10938. case DRM_FORMAT_RGB565:
  10939. case DRM_FORMAT_XRGB8888:
  10940. case DRM_FORMAT_XBGR8888:
  10941. case DRM_FORMAT_XRGB2101010:
  10942. case DRM_FORMAT_XBGR2101010:
  10943. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10944. modifier == I915_FORMAT_MOD_X_TILED;
  10945. default:
  10946. return false;
  10947. }
  10948. }
  10949. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10950. {
  10951. switch (format) {
  10952. case DRM_FORMAT_XRGB8888:
  10953. case DRM_FORMAT_XBGR8888:
  10954. case DRM_FORMAT_ARGB8888:
  10955. case DRM_FORMAT_ABGR8888:
  10956. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10957. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10958. return true;
  10959. /* fall through */
  10960. case DRM_FORMAT_RGB565:
  10961. case DRM_FORMAT_XRGB2101010:
  10962. case DRM_FORMAT_XBGR2101010:
  10963. case DRM_FORMAT_YUYV:
  10964. case DRM_FORMAT_YVYU:
  10965. case DRM_FORMAT_UYVY:
  10966. case DRM_FORMAT_VYUY:
  10967. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10968. return true;
  10969. /* fall through */
  10970. case DRM_FORMAT_C8:
  10971. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10972. modifier == I915_FORMAT_MOD_X_TILED ||
  10973. modifier == I915_FORMAT_MOD_Y_TILED)
  10974. return true;
  10975. /* fall through */
  10976. default:
  10977. return false;
  10978. }
  10979. }
  10980. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10981. uint32_t format,
  10982. uint64_t modifier)
  10983. {
  10984. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10985. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10986. return false;
  10987. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10988. modifier != DRM_FORMAT_MOD_LINEAR)
  10989. return false;
  10990. if (INTEL_GEN(dev_priv) >= 9)
  10991. return skl_mod_supported(format, modifier);
  10992. else if (INTEL_GEN(dev_priv) >= 4)
  10993. return i965_mod_supported(format, modifier);
  10994. else
  10995. return i8xx_mod_supported(format, modifier);
  10996. }
  10997. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10998. uint32_t format,
  10999. uint64_t modifier)
  11000. {
  11001. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  11002. return false;
  11003. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  11004. }
  11005. static struct drm_plane_funcs intel_plane_funcs = {
  11006. .update_plane = drm_atomic_helper_update_plane,
  11007. .disable_plane = drm_atomic_helper_disable_plane,
  11008. .destroy = intel_plane_destroy,
  11009. .atomic_get_property = intel_plane_atomic_get_property,
  11010. .atomic_set_property = intel_plane_atomic_set_property,
  11011. .atomic_duplicate_state = intel_plane_duplicate_state,
  11012. .atomic_destroy_state = intel_plane_destroy_state,
  11013. .format_mod_supported = intel_primary_plane_format_mod_supported,
  11014. };
  11015. static int
  11016. intel_legacy_cursor_update(struct drm_plane *plane,
  11017. struct drm_crtc *crtc,
  11018. struct drm_framebuffer *fb,
  11019. int crtc_x, int crtc_y,
  11020. unsigned int crtc_w, unsigned int crtc_h,
  11021. uint32_t src_x, uint32_t src_y,
  11022. uint32_t src_w, uint32_t src_h,
  11023. struct drm_modeset_acquire_ctx *ctx)
  11024. {
  11025. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11026. int ret;
  11027. struct drm_plane_state *old_plane_state, *new_plane_state;
  11028. struct intel_plane *intel_plane = to_intel_plane(plane);
  11029. struct drm_framebuffer *old_fb;
  11030. struct drm_crtc_state *crtc_state = crtc->state;
  11031. /*
  11032. * When crtc is inactive or there is a modeset pending,
  11033. * wait for it to complete in the slowpath
  11034. */
  11035. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11036. to_intel_crtc_state(crtc_state)->update_pipe)
  11037. goto slow;
  11038. old_plane_state = plane->state;
  11039. /*
  11040. * Don't do an async update if there is an outstanding commit modifying
  11041. * the plane. This prevents our async update's changes from getting
  11042. * overridden by a previous synchronous update's state.
  11043. */
  11044. if (old_plane_state->commit &&
  11045. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  11046. goto slow;
  11047. /*
  11048. * If any parameters change that may affect watermarks,
  11049. * take the slowpath. Only changing fb or position should be
  11050. * in the fastpath.
  11051. */
  11052. if (old_plane_state->crtc != crtc ||
  11053. old_plane_state->src_w != src_w ||
  11054. old_plane_state->src_h != src_h ||
  11055. old_plane_state->crtc_w != crtc_w ||
  11056. old_plane_state->crtc_h != crtc_h ||
  11057. !old_plane_state->fb != !fb)
  11058. goto slow;
  11059. new_plane_state = intel_plane_duplicate_state(plane);
  11060. if (!new_plane_state)
  11061. return -ENOMEM;
  11062. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11063. new_plane_state->src_x = src_x;
  11064. new_plane_state->src_y = src_y;
  11065. new_plane_state->src_w = src_w;
  11066. new_plane_state->src_h = src_h;
  11067. new_plane_state->crtc_x = crtc_x;
  11068. new_plane_state->crtc_y = crtc_y;
  11069. new_plane_state->crtc_w = crtc_w;
  11070. new_plane_state->crtc_h = crtc_h;
  11071. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11072. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  11073. to_intel_plane_state(plane->state),
  11074. to_intel_plane_state(new_plane_state));
  11075. if (ret)
  11076. goto out_free;
  11077. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11078. if (ret)
  11079. goto out_free;
  11080. ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
  11081. if (ret)
  11082. goto out_unlock;
  11083. intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
  11084. old_fb = old_plane_state->fb;
  11085. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11086. intel_plane->frontbuffer_bit);
  11087. /* Swap plane state */
  11088. plane->state = new_plane_state;
  11089. if (plane->state->visible) {
  11090. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11091. intel_plane->update_plane(intel_plane,
  11092. to_intel_crtc_state(crtc->state),
  11093. to_intel_plane_state(plane->state));
  11094. } else {
  11095. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11096. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11097. }
  11098. intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
  11099. out_unlock:
  11100. mutex_unlock(&dev_priv->drm.struct_mutex);
  11101. out_free:
  11102. if (ret)
  11103. intel_plane_destroy_state(plane, new_plane_state);
  11104. else
  11105. intel_plane_destroy_state(plane, old_plane_state);
  11106. return ret;
  11107. slow:
  11108. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11109. crtc_x, crtc_y, crtc_w, crtc_h,
  11110. src_x, src_y, src_w, src_h, ctx);
  11111. }
  11112. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11113. .update_plane = intel_legacy_cursor_update,
  11114. .disable_plane = drm_atomic_helper_disable_plane,
  11115. .destroy = intel_plane_destroy,
  11116. .atomic_get_property = intel_plane_atomic_get_property,
  11117. .atomic_set_property = intel_plane_atomic_set_property,
  11118. .atomic_duplicate_state = intel_plane_duplicate_state,
  11119. .atomic_destroy_state = intel_plane_destroy_state,
  11120. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  11121. };
  11122. static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
  11123. enum i9xx_plane_id i9xx_plane)
  11124. {
  11125. if (!HAS_FBC(dev_priv))
  11126. return false;
  11127. if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  11128. return i9xx_plane == PLANE_A; /* tied to pipe A */
  11129. else if (IS_IVYBRIDGE(dev_priv))
  11130. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
  11131. i9xx_plane == PLANE_C;
  11132. else if (INTEL_GEN(dev_priv) >= 4)
  11133. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
  11134. else
  11135. return i9xx_plane == PLANE_A;
  11136. }
  11137. static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
  11138. enum pipe pipe, enum plane_id plane_id)
  11139. {
  11140. if (!HAS_FBC(dev_priv))
  11141. return false;
  11142. return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
  11143. }
  11144. static struct intel_plane *
  11145. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11146. {
  11147. struct intel_plane *primary = NULL;
  11148. struct intel_plane_state *state = NULL;
  11149. const uint32_t *intel_primary_formats;
  11150. unsigned int supported_rotations;
  11151. unsigned int num_formats;
  11152. const uint64_t *modifiers;
  11153. int ret;
  11154. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11155. if (!primary) {
  11156. ret = -ENOMEM;
  11157. goto fail;
  11158. }
  11159. state = intel_create_plane_state(&primary->base);
  11160. if (!state) {
  11161. ret = -ENOMEM;
  11162. goto fail;
  11163. }
  11164. primary->base.state = &state->base;
  11165. primary->can_scale = false;
  11166. primary->max_downscale = 1;
  11167. if (INTEL_GEN(dev_priv) >= 9) {
  11168. primary->can_scale = true;
  11169. state->scaler_id = -1;
  11170. }
  11171. primary->pipe = pipe;
  11172. /*
  11173. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11174. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11175. */
  11176. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11177. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11178. else
  11179. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11180. primary->id = PLANE_PRIMARY;
  11181. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11182. if (INTEL_GEN(dev_priv) >= 9)
  11183. primary->has_fbc = skl_plane_has_fbc(dev_priv,
  11184. primary->pipe,
  11185. primary->id);
  11186. else
  11187. primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
  11188. primary->i9xx_plane);
  11189. if (primary->has_fbc) {
  11190. struct intel_fbc *fbc = &dev_priv->fbc;
  11191. fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
  11192. }
  11193. primary->check_plane = intel_check_primary_plane;
  11194. if (INTEL_GEN(dev_priv) >= 9) {
  11195. intel_primary_formats = skl_primary_formats;
  11196. num_formats = ARRAY_SIZE(skl_primary_formats);
  11197. if (skl_plane_has_ccs(dev_priv, pipe, PLANE_PRIMARY))
  11198. modifiers = skl_format_modifiers_ccs;
  11199. else
  11200. modifiers = skl_format_modifiers_noccs;
  11201. primary->update_plane = skl_update_plane;
  11202. primary->disable_plane = skl_disable_plane;
  11203. primary->get_hw_state = skl_plane_get_hw_state;
  11204. } else if (INTEL_GEN(dev_priv) >= 4) {
  11205. intel_primary_formats = i965_primary_formats;
  11206. num_formats = ARRAY_SIZE(i965_primary_formats);
  11207. modifiers = i9xx_format_modifiers;
  11208. primary->update_plane = i9xx_update_plane;
  11209. primary->disable_plane = i9xx_disable_plane;
  11210. primary->get_hw_state = i9xx_plane_get_hw_state;
  11211. } else {
  11212. intel_primary_formats = i8xx_primary_formats;
  11213. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11214. modifiers = i9xx_format_modifiers;
  11215. primary->update_plane = i9xx_update_plane;
  11216. primary->disable_plane = i9xx_disable_plane;
  11217. primary->get_hw_state = i9xx_plane_get_hw_state;
  11218. }
  11219. if (INTEL_GEN(dev_priv) >= 9)
  11220. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11221. 0, &intel_plane_funcs,
  11222. intel_primary_formats, num_formats,
  11223. modifiers,
  11224. DRM_PLANE_TYPE_PRIMARY,
  11225. "plane 1%c", pipe_name(pipe));
  11226. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11227. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11228. 0, &intel_plane_funcs,
  11229. intel_primary_formats, num_formats,
  11230. modifiers,
  11231. DRM_PLANE_TYPE_PRIMARY,
  11232. "primary %c", pipe_name(pipe));
  11233. else
  11234. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11235. 0, &intel_plane_funcs,
  11236. intel_primary_formats, num_formats,
  11237. modifiers,
  11238. DRM_PLANE_TYPE_PRIMARY,
  11239. "plane %c",
  11240. plane_name(primary->i9xx_plane));
  11241. if (ret)
  11242. goto fail;
  11243. if (INTEL_GEN(dev_priv) >= 10) {
  11244. supported_rotations =
  11245. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11246. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11247. DRM_MODE_REFLECT_X;
  11248. } else if (INTEL_GEN(dev_priv) >= 9) {
  11249. supported_rotations =
  11250. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11251. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11252. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11253. supported_rotations =
  11254. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11255. DRM_MODE_REFLECT_X;
  11256. } else if (INTEL_GEN(dev_priv) >= 4) {
  11257. supported_rotations =
  11258. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11259. } else {
  11260. supported_rotations = DRM_MODE_ROTATE_0;
  11261. }
  11262. if (INTEL_GEN(dev_priv) >= 4)
  11263. drm_plane_create_rotation_property(&primary->base,
  11264. DRM_MODE_ROTATE_0,
  11265. supported_rotations);
  11266. if (INTEL_GEN(dev_priv) >= 9)
  11267. drm_plane_create_color_properties(&primary->base,
  11268. BIT(DRM_COLOR_YCBCR_BT601) |
  11269. BIT(DRM_COLOR_YCBCR_BT709),
  11270. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  11271. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  11272. DRM_COLOR_YCBCR_BT709,
  11273. DRM_COLOR_YCBCR_LIMITED_RANGE);
  11274. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11275. return primary;
  11276. fail:
  11277. kfree(state);
  11278. kfree(primary);
  11279. return ERR_PTR(ret);
  11280. }
  11281. static struct intel_plane *
  11282. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11283. enum pipe pipe)
  11284. {
  11285. struct intel_plane *cursor = NULL;
  11286. struct intel_plane_state *state = NULL;
  11287. int ret;
  11288. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11289. if (!cursor) {
  11290. ret = -ENOMEM;
  11291. goto fail;
  11292. }
  11293. state = intel_create_plane_state(&cursor->base);
  11294. if (!state) {
  11295. ret = -ENOMEM;
  11296. goto fail;
  11297. }
  11298. cursor->base.state = &state->base;
  11299. cursor->can_scale = false;
  11300. cursor->max_downscale = 1;
  11301. cursor->pipe = pipe;
  11302. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11303. cursor->id = PLANE_CURSOR;
  11304. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11305. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11306. cursor->update_plane = i845_update_cursor;
  11307. cursor->disable_plane = i845_disable_cursor;
  11308. cursor->get_hw_state = i845_cursor_get_hw_state;
  11309. cursor->check_plane = i845_check_cursor;
  11310. } else {
  11311. cursor->update_plane = i9xx_update_cursor;
  11312. cursor->disable_plane = i9xx_disable_cursor;
  11313. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11314. cursor->check_plane = i9xx_check_cursor;
  11315. }
  11316. cursor->cursor.base = ~0;
  11317. cursor->cursor.cntl = ~0;
  11318. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11319. cursor->cursor.size = ~0;
  11320. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11321. 0, &intel_cursor_plane_funcs,
  11322. intel_cursor_formats,
  11323. ARRAY_SIZE(intel_cursor_formats),
  11324. cursor_format_modifiers,
  11325. DRM_PLANE_TYPE_CURSOR,
  11326. "cursor %c", pipe_name(pipe));
  11327. if (ret)
  11328. goto fail;
  11329. if (INTEL_GEN(dev_priv) >= 4)
  11330. drm_plane_create_rotation_property(&cursor->base,
  11331. DRM_MODE_ROTATE_0,
  11332. DRM_MODE_ROTATE_0 |
  11333. DRM_MODE_ROTATE_180);
  11334. if (INTEL_GEN(dev_priv) >= 9)
  11335. state->scaler_id = -1;
  11336. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11337. return cursor;
  11338. fail:
  11339. kfree(state);
  11340. kfree(cursor);
  11341. return ERR_PTR(ret);
  11342. }
  11343. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11344. struct intel_crtc_state *crtc_state)
  11345. {
  11346. struct intel_crtc_scaler_state *scaler_state =
  11347. &crtc_state->scaler_state;
  11348. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11349. int i;
  11350. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11351. if (!crtc->num_scalers)
  11352. return;
  11353. for (i = 0; i < crtc->num_scalers; i++) {
  11354. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11355. scaler->in_use = 0;
  11356. scaler->mode = PS_SCALER_MODE_DYN;
  11357. }
  11358. scaler_state->scaler_id = -1;
  11359. }
  11360. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11361. {
  11362. struct intel_crtc *intel_crtc;
  11363. struct intel_crtc_state *crtc_state = NULL;
  11364. struct intel_plane *primary = NULL;
  11365. struct intel_plane *cursor = NULL;
  11366. int sprite, ret;
  11367. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11368. if (!intel_crtc)
  11369. return -ENOMEM;
  11370. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11371. if (!crtc_state) {
  11372. ret = -ENOMEM;
  11373. goto fail;
  11374. }
  11375. intel_crtc->config = crtc_state;
  11376. intel_crtc->base.state = &crtc_state->base;
  11377. crtc_state->base.crtc = &intel_crtc->base;
  11378. primary = intel_primary_plane_create(dev_priv, pipe);
  11379. if (IS_ERR(primary)) {
  11380. ret = PTR_ERR(primary);
  11381. goto fail;
  11382. }
  11383. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11384. for_each_sprite(dev_priv, pipe, sprite) {
  11385. struct intel_plane *plane;
  11386. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11387. if (IS_ERR(plane)) {
  11388. ret = PTR_ERR(plane);
  11389. goto fail;
  11390. }
  11391. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11392. }
  11393. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11394. if (IS_ERR(cursor)) {
  11395. ret = PTR_ERR(cursor);
  11396. goto fail;
  11397. }
  11398. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11399. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11400. &primary->base, &cursor->base,
  11401. &intel_crtc_funcs,
  11402. "pipe %c", pipe_name(pipe));
  11403. if (ret)
  11404. goto fail;
  11405. intel_crtc->pipe = pipe;
  11406. /* initialize shared scalers */
  11407. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11408. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
  11409. dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
  11410. dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
  11411. if (INTEL_GEN(dev_priv) < 9) {
  11412. enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
  11413. BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11414. dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
  11415. dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
  11416. }
  11417. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11418. intel_color_init(&intel_crtc->base);
  11419. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11420. return 0;
  11421. fail:
  11422. /*
  11423. * drm_mode_config_cleanup() will free up any
  11424. * crtcs/planes already initialized.
  11425. */
  11426. kfree(crtc_state);
  11427. kfree(intel_crtc);
  11428. return ret;
  11429. }
  11430. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11431. {
  11432. struct drm_device *dev = connector->base.dev;
  11433. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11434. if (!connector->base.state->crtc)
  11435. return INVALID_PIPE;
  11436. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11437. }
  11438. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11439. struct drm_file *file)
  11440. {
  11441. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11442. struct drm_crtc *drmmode_crtc;
  11443. struct intel_crtc *crtc;
  11444. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11445. if (!drmmode_crtc)
  11446. return -ENOENT;
  11447. crtc = to_intel_crtc(drmmode_crtc);
  11448. pipe_from_crtc_id->pipe = crtc->pipe;
  11449. return 0;
  11450. }
  11451. static int intel_encoder_clones(struct intel_encoder *encoder)
  11452. {
  11453. struct drm_device *dev = encoder->base.dev;
  11454. struct intel_encoder *source_encoder;
  11455. int index_mask = 0;
  11456. int entry = 0;
  11457. for_each_intel_encoder(dev, source_encoder) {
  11458. if (encoders_cloneable(encoder, source_encoder))
  11459. index_mask |= (1 << entry);
  11460. entry++;
  11461. }
  11462. return index_mask;
  11463. }
  11464. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11465. {
  11466. if (!IS_MOBILE(dev_priv))
  11467. return false;
  11468. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11469. return false;
  11470. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11471. return false;
  11472. return true;
  11473. }
  11474. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11475. {
  11476. if (INTEL_GEN(dev_priv) >= 9)
  11477. return false;
  11478. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11479. return false;
  11480. if (IS_CHERRYVIEW(dev_priv))
  11481. return false;
  11482. if (HAS_PCH_LPT_H(dev_priv) &&
  11483. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11484. return false;
  11485. /* DDI E can't be used if DDI A requires 4 lanes */
  11486. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11487. return false;
  11488. if (!dev_priv->vbt.int_crt_support)
  11489. return false;
  11490. return true;
  11491. }
  11492. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11493. {
  11494. int pps_num;
  11495. int pps_idx;
  11496. if (HAS_DDI(dev_priv))
  11497. return;
  11498. /*
  11499. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11500. * everywhere where registers can be write protected.
  11501. */
  11502. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11503. pps_num = 2;
  11504. else
  11505. pps_num = 1;
  11506. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11507. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11508. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11509. I915_WRITE(PP_CONTROL(pps_idx), val);
  11510. }
  11511. }
  11512. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11513. {
  11514. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11515. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11516. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11517. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11518. else
  11519. dev_priv->pps_mmio_base = PPS_BASE;
  11520. intel_pps_unlock_regs_wa(dev_priv);
  11521. }
  11522. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11523. {
  11524. struct intel_encoder *encoder;
  11525. bool dpd_is_edp = false;
  11526. intel_pps_init(dev_priv);
  11527. /*
  11528. * intel_edp_init_connector() depends on this completing first, to
  11529. * prevent the registeration of both eDP and LVDS and the incorrect
  11530. * sharing of the PPS.
  11531. */
  11532. intel_lvds_init(dev_priv);
  11533. if (intel_crt_present(dev_priv))
  11534. intel_crt_init(dev_priv);
  11535. if (IS_GEN9_LP(dev_priv)) {
  11536. /*
  11537. * FIXME: Broxton doesn't support port detection via the
  11538. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11539. * detect the ports.
  11540. */
  11541. intel_ddi_init(dev_priv, PORT_A);
  11542. intel_ddi_init(dev_priv, PORT_B);
  11543. intel_ddi_init(dev_priv, PORT_C);
  11544. intel_dsi_init(dev_priv);
  11545. } else if (HAS_DDI(dev_priv)) {
  11546. int found;
  11547. /*
  11548. * Haswell uses DDI functions to detect digital outputs.
  11549. * On SKL pre-D0 the strap isn't connected, so we assume
  11550. * it's there.
  11551. */
  11552. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11553. /* WaIgnoreDDIAStrap: skl */
  11554. if (found || IS_GEN9_BC(dev_priv))
  11555. intel_ddi_init(dev_priv, PORT_A);
  11556. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11557. * register */
  11558. found = I915_READ(SFUSE_STRAP);
  11559. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11560. intel_ddi_init(dev_priv, PORT_B);
  11561. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11562. intel_ddi_init(dev_priv, PORT_C);
  11563. if (found & SFUSE_STRAP_DDID_DETECTED)
  11564. intel_ddi_init(dev_priv, PORT_D);
  11565. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11566. intel_ddi_init(dev_priv, PORT_F);
  11567. /*
  11568. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11569. */
  11570. if (IS_GEN9_BC(dev_priv) &&
  11571. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11572. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11573. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11574. intel_ddi_init(dev_priv, PORT_E);
  11575. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11576. int found;
  11577. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11578. if (has_edp_a(dev_priv))
  11579. intel_dp_init(dev_priv, DP_A, PORT_A);
  11580. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11581. /* PCH SDVOB multiplex with HDMIB */
  11582. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11583. if (!found)
  11584. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11585. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11586. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11587. }
  11588. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11589. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11590. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11591. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11592. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11593. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11594. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11595. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11596. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11597. bool has_edp, has_port;
  11598. /*
  11599. * The DP_DETECTED bit is the latched state of the DDC
  11600. * SDA pin at boot. However since eDP doesn't require DDC
  11601. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11602. * eDP ports may have been muxed to an alternate function.
  11603. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11604. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11605. * detect eDP ports.
  11606. *
  11607. * Sadly the straps seem to be missing sometimes even for HDMI
  11608. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11609. * and VBT for the presence of the port. Additionally we can't
  11610. * trust the port type the VBT declares as we've seen at least
  11611. * HDMI ports that the VBT claim are DP or eDP.
  11612. */
  11613. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11614. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11615. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11616. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11617. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11618. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11619. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11620. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11621. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11622. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11623. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11624. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11625. if (IS_CHERRYVIEW(dev_priv)) {
  11626. /*
  11627. * eDP not supported on port D,
  11628. * so no need to worry about it
  11629. */
  11630. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11631. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11632. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11633. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11634. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11635. }
  11636. intel_dsi_init(dev_priv);
  11637. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11638. bool found = false;
  11639. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11640. DRM_DEBUG_KMS("probing SDVOB\n");
  11641. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11642. if (!found && IS_G4X(dev_priv)) {
  11643. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11644. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11645. }
  11646. if (!found && IS_G4X(dev_priv))
  11647. intel_dp_init(dev_priv, DP_B, PORT_B);
  11648. }
  11649. /* Before G4X SDVOC doesn't have its own detect register */
  11650. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11651. DRM_DEBUG_KMS("probing SDVOC\n");
  11652. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11653. }
  11654. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11655. if (IS_G4X(dev_priv)) {
  11656. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11657. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11658. }
  11659. if (IS_G4X(dev_priv))
  11660. intel_dp_init(dev_priv, DP_C, PORT_C);
  11661. }
  11662. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11663. intel_dp_init(dev_priv, DP_D, PORT_D);
  11664. } else if (IS_GEN2(dev_priv))
  11665. intel_dvo_init(dev_priv);
  11666. if (SUPPORTS_TV(dev_priv))
  11667. intel_tv_init(dev_priv);
  11668. intel_psr_init(dev_priv);
  11669. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11670. encoder->base.possible_crtcs = encoder->crtc_mask;
  11671. encoder->base.possible_clones =
  11672. intel_encoder_clones(encoder);
  11673. }
  11674. intel_init_pch_refclk(dev_priv);
  11675. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11676. }
  11677. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11678. {
  11679. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11680. drm_framebuffer_cleanup(fb);
  11681. i915_gem_object_lock(intel_fb->obj);
  11682. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11683. i915_gem_object_unlock(intel_fb->obj);
  11684. i915_gem_object_put(intel_fb->obj);
  11685. kfree(intel_fb);
  11686. }
  11687. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11688. struct drm_file *file,
  11689. unsigned int *handle)
  11690. {
  11691. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11692. struct drm_i915_gem_object *obj = intel_fb->obj;
  11693. if (obj->userptr.mm) {
  11694. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11695. return -EINVAL;
  11696. }
  11697. return drm_gem_handle_create(file, &obj->base, handle);
  11698. }
  11699. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11700. struct drm_file *file,
  11701. unsigned flags, unsigned color,
  11702. struct drm_clip_rect *clips,
  11703. unsigned num_clips)
  11704. {
  11705. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11706. i915_gem_object_flush_if_display(obj);
  11707. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11708. return 0;
  11709. }
  11710. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11711. .destroy = intel_user_framebuffer_destroy,
  11712. .create_handle = intel_user_framebuffer_create_handle,
  11713. .dirty = intel_user_framebuffer_dirty,
  11714. };
  11715. static
  11716. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11717. uint64_t fb_modifier, uint32_t pixel_format)
  11718. {
  11719. u32 gen = INTEL_GEN(dev_priv);
  11720. if (gen >= 9) {
  11721. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11722. /* "The stride in bytes must not exceed the of the size of 8K
  11723. * pixels and 32K bytes."
  11724. */
  11725. return min(8192 * cpp, 32768);
  11726. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11727. return 32*1024;
  11728. } else if (gen >= 4) {
  11729. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11730. return 16*1024;
  11731. else
  11732. return 32*1024;
  11733. } else if (gen >= 3) {
  11734. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11735. return 8*1024;
  11736. else
  11737. return 16*1024;
  11738. } else {
  11739. /* XXX DSPC is limited to 4k tiled */
  11740. return 8*1024;
  11741. }
  11742. }
  11743. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11744. struct drm_i915_gem_object *obj,
  11745. struct drm_mode_fb_cmd2 *mode_cmd)
  11746. {
  11747. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11748. struct drm_framebuffer *fb = &intel_fb->base;
  11749. struct drm_format_name_buf format_name;
  11750. u32 pitch_limit;
  11751. unsigned int tiling, stride;
  11752. int ret = -EINVAL;
  11753. int i;
  11754. i915_gem_object_lock(obj);
  11755. obj->framebuffer_references++;
  11756. tiling = i915_gem_object_get_tiling(obj);
  11757. stride = i915_gem_object_get_stride(obj);
  11758. i915_gem_object_unlock(obj);
  11759. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11760. /*
  11761. * If there's a fence, enforce that
  11762. * the fb modifier and tiling mode match.
  11763. */
  11764. if (tiling != I915_TILING_NONE &&
  11765. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11766. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11767. goto err;
  11768. }
  11769. } else {
  11770. if (tiling == I915_TILING_X) {
  11771. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11772. } else if (tiling == I915_TILING_Y) {
  11773. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11774. goto err;
  11775. }
  11776. }
  11777. /* Passed in modifier sanity checking. */
  11778. switch (mode_cmd->modifier[0]) {
  11779. case I915_FORMAT_MOD_Y_TILED_CCS:
  11780. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11781. switch (mode_cmd->pixel_format) {
  11782. case DRM_FORMAT_XBGR8888:
  11783. case DRM_FORMAT_ABGR8888:
  11784. case DRM_FORMAT_XRGB8888:
  11785. case DRM_FORMAT_ARGB8888:
  11786. break;
  11787. default:
  11788. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11789. goto err;
  11790. }
  11791. /* fall through */
  11792. case I915_FORMAT_MOD_Y_TILED:
  11793. case I915_FORMAT_MOD_Yf_TILED:
  11794. if (INTEL_GEN(dev_priv) < 9) {
  11795. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11796. mode_cmd->modifier[0]);
  11797. goto err;
  11798. }
  11799. case DRM_FORMAT_MOD_LINEAR:
  11800. case I915_FORMAT_MOD_X_TILED:
  11801. break;
  11802. default:
  11803. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11804. mode_cmd->modifier[0]);
  11805. goto err;
  11806. }
  11807. /*
  11808. * gen2/3 display engine uses the fence if present,
  11809. * so the tiling mode must match the fb modifier exactly.
  11810. */
  11811. if (INTEL_GEN(dev_priv) < 4 &&
  11812. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11813. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11814. goto err;
  11815. }
  11816. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11817. mode_cmd->pixel_format);
  11818. if (mode_cmd->pitches[0] > pitch_limit) {
  11819. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11820. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11821. "tiled" : "linear",
  11822. mode_cmd->pitches[0], pitch_limit);
  11823. goto err;
  11824. }
  11825. /*
  11826. * If there's a fence, enforce that
  11827. * the fb pitch and fence stride match.
  11828. */
  11829. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11830. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11831. mode_cmd->pitches[0], stride);
  11832. goto err;
  11833. }
  11834. /* Reject formats not supported by any plane early. */
  11835. switch (mode_cmd->pixel_format) {
  11836. case DRM_FORMAT_C8:
  11837. case DRM_FORMAT_RGB565:
  11838. case DRM_FORMAT_XRGB8888:
  11839. case DRM_FORMAT_ARGB8888:
  11840. break;
  11841. case DRM_FORMAT_XRGB1555:
  11842. if (INTEL_GEN(dev_priv) > 3) {
  11843. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11844. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11845. goto err;
  11846. }
  11847. break;
  11848. case DRM_FORMAT_ABGR8888:
  11849. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11850. INTEL_GEN(dev_priv) < 9) {
  11851. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11852. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11853. goto err;
  11854. }
  11855. break;
  11856. case DRM_FORMAT_XBGR8888:
  11857. case DRM_FORMAT_XRGB2101010:
  11858. case DRM_FORMAT_XBGR2101010:
  11859. if (INTEL_GEN(dev_priv) < 4) {
  11860. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11861. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11862. goto err;
  11863. }
  11864. break;
  11865. case DRM_FORMAT_ABGR2101010:
  11866. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11867. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11868. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11869. goto err;
  11870. }
  11871. break;
  11872. case DRM_FORMAT_YUYV:
  11873. case DRM_FORMAT_UYVY:
  11874. case DRM_FORMAT_YVYU:
  11875. case DRM_FORMAT_VYUY:
  11876. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11877. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11878. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11879. goto err;
  11880. }
  11881. break;
  11882. default:
  11883. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11884. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11885. goto err;
  11886. }
  11887. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11888. if (mode_cmd->offsets[0] != 0)
  11889. goto err;
  11890. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11891. for (i = 0; i < fb->format->num_planes; i++) {
  11892. u32 stride_alignment;
  11893. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11894. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11895. goto err;
  11896. }
  11897. stride_alignment = intel_fb_stride_alignment(fb, i);
  11898. /*
  11899. * Display WA #0531: skl,bxt,kbl,glk
  11900. *
  11901. * Render decompression and plane width > 3840
  11902. * combined with horizontal panning requires the
  11903. * plane stride to be a multiple of 4. We'll just
  11904. * require the entire fb to accommodate that to avoid
  11905. * potential runtime errors at plane configuration time.
  11906. */
  11907. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11908. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11909. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11910. stride_alignment *= 4;
  11911. if (fb->pitches[i] & (stride_alignment - 1)) {
  11912. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11913. i, fb->pitches[i], stride_alignment);
  11914. goto err;
  11915. }
  11916. }
  11917. intel_fb->obj = obj;
  11918. ret = intel_fill_fb_info(dev_priv, fb);
  11919. if (ret)
  11920. goto err;
  11921. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11922. if (ret) {
  11923. DRM_ERROR("framebuffer init failed %d\n", ret);
  11924. goto err;
  11925. }
  11926. return 0;
  11927. err:
  11928. i915_gem_object_lock(obj);
  11929. obj->framebuffer_references--;
  11930. i915_gem_object_unlock(obj);
  11931. return ret;
  11932. }
  11933. static struct drm_framebuffer *
  11934. intel_user_framebuffer_create(struct drm_device *dev,
  11935. struct drm_file *filp,
  11936. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11937. {
  11938. struct drm_framebuffer *fb;
  11939. struct drm_i915_gem_object *obj;
  11940. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11941. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11942. if (!obj)
  11943. return ERR_PTR(-ENOENT);
  11944. fb = intel_framebuffer_create(obj, &mode_cmd);
  11945. if (IS_ERR(fb))
  11946. i915_gem_object_put(obj);
  11947. return fb;
  11948. }
  11949. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11950. {
  11951. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11952. drm_atomic_state_default_release(state);
  11953. i915_sw_fence_fini(&intel_state->commit_ready);
  11954. kfree(state);
  11955. }
  11956. static enum drm_mode_status
  11957. intel_mode_valid(struct drm_device *dev,
  11958. const struct drm_display_mode *mode)
  11959. {
  11960. if (mode->vscan > 1)
  11961. return MODE_NO_VSCAN;
  11962. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  11963. return MODE_NO_DBLESCAN;
  11964. if (mode->flags & DRM_MODE_FLAG_HSKEW)
  11965. return MODE_H_ILLEGAL;
  11966. if (mode->flags & (DRM_MODE_FLAG_CSYNC |
  11967. DRM_MODE_FLAG_NCSYNC |
  11968. DRM_MODE_FLAG_PCSYNC))
  11969. return MODE_HSYNC;
  11970. if (mode->flags & (DRM_MODE_FLAG_BCAST |
  11971. DRM_MODE_FLAG_PIXMUX |
  11972. DRM_MODE_FLAG_CLKDIV2))
  11973. return MODE_BAD;
  11974. return MODE_OK;
  11975. }
  11976. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11977. .fb_create = intel_user_framebuffer_create,
  11978. .get_format_info = intel_get_format_info,
  11979. .output_poll_changed = intel_fbdev_output_poll_changed,
  11980. .mode_valid = intel_mode_valid,
  11981. .atomic_check = intel_atomic_check,
  11982. .atomic_commit = intel_atomic_commit,
  11983. .atomic_state_alloc = intel_atomic_state_alloc,
  11984. .atomic_state_clear = intel_atomic_state_clear,
  11985. .atomic_state_free = intel_atomic_state_free,
  11986. };
  11987. /**
  11988. * intel_init_display_hooks - initialize the display modesetting hooks
  11989. * @dev_priv: device private
  11990. */
  11991. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11992. {
  11993. intel_init_cdclk_hooks(dev_priv);
  11994. if (INTEL_GEN(dev_priv) >= 9) {
  11995. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11996. dev_priv->display.get_initial_plane_config =
  11997. skylake_get_initial_plane_config;
  11998. dev_priv->display.crtc_compute_clock =
  11999. haswell_crtc_compute_clock;
  12000. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12001. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12002. } else if (HAS_DDI(dev_priv)) {
  12003. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12004. dev_priv->display.get_initial_plane_config =
  12005. i9xx_get_initial_plane_config;
  12006. dev_priv->display.crtc_compute_clock =
  12007. haswell_crtc_compute_clock;
  12008. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12009. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12010. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12011. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12012. dev_priv->display.get_initial_plane_config =
  12013. i9xx_get_initial_plane_config;
  12014. dev_priv->display.crtc_compute_clock =
  12015. ironlake_crtc_compute_clock;
  12016. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12017. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12018. } else if (IS_CHERRYVIEW(dev_priv)) {
  12019. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12020. dev_priv->display.get_initial_plane_config =
  12021. i9xx_get_initial_plane_config;
  12022. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12023. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12024. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12025. } else if (IS_VALLEYVIEW(dev_priv)) {
  12026. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12027. dev_priv->display.get_initial_plane_config =
  12028. i9xx_get_initial_plane_config;
  12029. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12030. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12031. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12032. } else if (IS_G4X(dev_priv)) {
  12033. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12034. dev_priv->display.get_initial_plane_config =
  12035. i9xx_get_initial_plane_config;
  12036. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12037. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12038. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12039. } else if (IS_PINEVIEW(dev_priv)) {
  12040. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12041. dev_priv->display.get_initial_plane_config =
  12042. i9xx_get_initial_plane_config;
  12043. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12044. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12045. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12046. } else if (!IS_GEN2(dev_priv)) {
  12047. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12048. dev_priv->display.get_initial_plane_config =
  12049. i9xx_get_initial_plane_config;
  12050. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12051. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12052. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12053. } else {
  12054. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12055. dev_priv->display.get_initial_plane_config =
  12056. i9xx_get_initial_plane_config;
  12057. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12058. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12059. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12060. }
  12061. if (IS_GEN5(dev_priv)) {
  12062. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12063. } else if (IS_GEN6(dev_priv)) {
  12064. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12065. } else if (IS_IVYBRIDGE(dev_priv)) {
  12066. /* FIXME: detect B0+ stepping and use auto training */
  12067. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12068. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12069. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12070. }
  12071. if (INTEL_GEN(dev_priv) >= 9)
  12072. dev_priv->display.update_crtcs = skl_update_crtcs;
  12073. else
  12074. dev_priv->display.update_crtcs = intel_update_crtcs;
  12075. }
  12076. /*
  12077. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12078. */
  12079. static void quirk_ssc_force_disable(struct drm_device *dev)
  12080. {
  12081. struct drm_i915_private *dev_priv = to_i915(dev);
  12082. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12083. DRM_INFO("applying lvds SSC disable quirk\n");
  12084. }
  12085. /*
  12086. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12087. * brightness value
  12088. */
  12089. static void quirk_invert_brightness(struct drm_device *dev)
  12090. {
  12091. struct drm_i915_private *dev_priv = to_i915(dev);
  12092. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12093. DRM_INFO("applying inverted panel brightness quirk\n");
  12094. }
  12095. /* Some VBT's incorrectly indicate no backlight is present */
  12096. static void quirk_backlight_present(struct drm_device *dev)
  12097. {
  12098. struct drm_i915_private *dev_priv = to_i915(dev);
  12099. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12100. DRM_INFO("applying backlight present quirk\n");
  12101. }
  12102. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  12103. * which is 300 ms greater than eDP spec T12 min.
  12104. */
  12105. static void quirk_increase_t12_delay(struct drm_device *dev)
  12106. {
  12107. struct drm_i915_private *dev_priv = to_i915(dev);
  12108. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  12109. DRM_INFO("Applying T12 delay quirk\n");
  12110. }
  12111. struct intel_quirk {
  12112. int device;
  12113. int subsystem_vendor;
  12114. int subsystem_device;
  12115. void (*hook)(struct drm_device *dev);
  12116. };
  12117. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12118. struct intel_dmi_quirk {
  12119. void (*hook)(struct drm_device *dev);
  12120. const struct dmi_system_id (*dmi_id_list)[];
  12121. };
  12122. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12123. {
  12124. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12125. return 1;
  12126. }
  12127. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12128. {
  12129. .dmi_id_list = &(const struct dmi_system_id[]) {
  12130. {
  12131. .callback = intel_dmi_reverse_brightness,
  12132. .ident = "NCR Corporation",
  12133. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12134. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12135. },
  12136. },
  12137. { } /* terminating entry */
  12138. },
  12139. .hook = quirk_invert_brightness,
  12140. },
  12141. };
  12142. static struct intel_quirk intel_quirks[] = {
  12143. /* Lenovo U160 cannot use SSC on LVDS */
  12144. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12145. /* Sony Vaio Y cannot use SSC on LVDS */
  12146. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12147. /* Acer Aspire 5734Z must invert backlight brightness */
  12148. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12149. /* Acer/eMachines G725 */
  12150. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12151. /* Acer/eMachines e725 */
  12152. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12153. /* Acer/Packard Bell NCL20 */
  12154. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12155. /* Acer Aspire 4736Z */
  12156. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12157. /* Acer Aspire 5336 */
  12158. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12159. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12160. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12161. /* Acer C720 Chromebook (Core i3 4005U) */
  12162. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12163. /* Apple Macbook 2,1 (Core 2 T7400) */
  12164. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12165. /* Apple Macbook 4,1 */
  12166. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12167. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12168. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12169. /* HP Chromebook 14 (Celeron 2955U) */
  12170. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12171. /* Dell Chromebook 11 */
  12172. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12173. /* Dell Chromebook 11 (2015 version) */
  12174. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12175. /* Toshiba Satellite P50-C-18C */
  12176. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12177. };
  12178. static void intel_init_quirks(struct drm_device *dev)
  12179. {
  12180. struct pci_dev *d = dev->pdev;
  12181. int i;
  12182. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12183. struct intel_quirk *q = &intel_quirks[i];
  12184. if (d->device == q->device &&
  12185. (d->subsystem_vendor == q->subsystem_vendor ||
  12186. q->subsystem_vendor == PCI_ANY_ID) &&
  12187. (d->subsystem_device == q->subsystem_device ||
  12188. q->subsystem_device == PCI_ANY_ID))
  12189. q->hook(dev);
  12190. }
  12191. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12192. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12193. intel_dmi_quirks[i].hook(dev);
  12194. }
  12195. }
  12196. /* Disable the VGA plane that we never use */
  12197. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12198. {
  12199. struct pci_dev *pdev = dev_priv->drm.pdev;
  12200. u8 sr1;
  12201. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12202. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12203. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12204. outb(SR01, VGA_SR_INDEX);
  12205. sr1 = inb(VGA_SR_DATA);
  12206. outb(sr1 | 1<<5, VGA_SR_DATA);
  12207. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12208. udelay(300);
  12209. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12210. POSTING_READ(vga_reg);
  12211. }
  12212. void intel_modeset_init_hw(struct drm_device *dev)
  12213. {
  12214. struct drm_i915_private *dev_priv = to_i915(dev);
  12215. intel_update_cdclk(dev_priv);
  12216. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12217. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12218. }
  12219. /*
  12220. * Calculate what we think the watermarks should be for the state we've read
  12221. * out of the hardware and then immediately program those watermarks so that
  12222. * we ensure the hardware settings match our internal state.
  12223. *
  12224. * We can calculate what we think WM's should be by creating a duplicate of the
  12225. * current state (which was constructed during hardware readout) and running it
  12226. * through the atomic check code to calculate new watermark values in the
  12227. * state object.
  12228. */
  12229. static void sanitize_watermarks(struct drm_device *dev)
  12230. {
  12231. struct drm_i915_private *dev_priv = to_i915(dev);
  12232. struct drm_atomic_state *state;
  12233. struct intel_atomic_state *intel_state;
  12234. struct drm_crtc *crtc;
  12235. struct drm_crtc_state *cstate;
  12236. struct drm_modeset_acquire_ctx ctx;
  12237. int ret;
  12238. int i;
  12239. /* Only supported on platforms that use atomic watermark design */
  12240. if (!dev_priv->display.optimize_watermarks)
  12241. return;
  12242. /*
  12243. * We need to hold connection_mutex before calling duplicate_state so
  12244. * that the connector loop is protected.
  12245. */
  12246. drm_modeset_acquire_init(&ctx, 0);
  12247. retry:
  12248. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12249. if (ret == -EDEADLK) {
  12250. drm_modeset_backoff(&ctx);
  12251. goto retry;
  12252. } else if (WARN_ON(ret)) {
  12253. goto fail;
  12254. }
  12255. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12256. if (WARN_ON(IS_ERR(state)))
  12257. goto fail;
  12258. intel_state = to_intel_atomic_state(state);
  12259. /*
  12260. * Hardware readout is the only time we don't want to calculate
  12261. * intermediate watermarks (since we don't trust the current
  12262. * watermarks).
  12263. */
  12264. if (!HAS_GMCH_DISPLAY(dev_priv))
  12265. intel_state->skip_intermediate_wm = true;
  12266. ret = intel_atomic_check(dev, state);
  12267. if (ret) {
  12268. /*
  12269. * If we fail here, it means that the hardware appears to be
  12270. * programmed in a way that shouldn't be possible, given our
  12271. * understanding of watermark requirements. This might mean a
  12272. * mistake in the hardware readout code or a mistake in the
  12273. * watermark calculations for a given platform. Raise a WARN
  12274. * so that this is noticeable.
  12275. *
  12276. * If this actually happens, we'll have to just leave the
  12277. * BIOS-programmed watermarks untouched and hope for the best.
  12278. */
  12279. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12280. goto put_state;
  12281. }
  12282. /* Write calculated watermark values back */
  12283. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12284. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12285. cs->wm.need_postvbl_update = true;
  12286. dev_priv->display.optimize_watermarks(intel_state, cs);
  12287. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12288. }
  12289. put_state:
  12290. drm_atomic_state_put(state);
  12291. fail:
  12292. drm_modeset_drop_locks(&ctx);
  12293. drm_modeset_acquire_fini(&ctx);
  12294. }
  12295. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12296. {
  12297. if (IS_GEN5(dev_priv)) {
  12298. u32 fdi_pll_clk =
  12299. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12300. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12301. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12302. dev_priv->fdi_pll_freq = 270000;
  12303. } else {
  12304. return;
  12305. }
  12306. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12307. }
  12308. int intel_modeset_init(struct drm_device *dev)
  12309. {
  12310. struct drm_i915_private *dev_priv = to_i915(dev);
  12311. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12312. enum pipe pipe;
  12313. struct intel_crtc *crtc;
  12314. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12315. drm_mode_config_init(dev);
  12316. dev->mode_config.min_width = 0;
  12317. dev->mode_config.min_height = 0;
  12318. dev->mode_config.preferred_depth = 24;
  12319. dev->mode_config.prefer_shadow = 1;
  12320. dev->mode_config.allow_fb_modifiers = true;
  12321. dev->mode_config.funcs = &intel_mode_funcs;
  12322. init_llist_head(&dev_priv->atomic_helper.free_list);
  12323. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12324. intel_atomic_helper_free_state_worker);
  12325. intel_init_quirks(dev);
  12326. intel_init_pm(dev_priv);
  12327. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12328. return 0;
  12329. /*
  12330. * There may be no VBT; and if the BIOS enabled SSC we can
  12331. * just keep using it to avoid unnecessary flicker. Whereas if the
  12332. * BIOS isn't using it, don't assume it will work even if the VBT
  12333. * indicates as much.
  12334. */
  12335. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12336. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12337. DREF_SSC1_ENABLE);
  12338. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12339. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12340. bios_lvds_use_ssc ? "en" : "dis",
  12341. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12342. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12343. }
  12344. }
  12345. if (IS_GEN2(dev_priv)) {
  12346. dev->mode_config.max_width = 2048;
  12347. dev->mode_config.max_height = 2048;
  12348. } else if (IS_GEN3(dev_priv)) {
  12349. dev->mode_config.max_width = 4096;
  12350. dev->mode_config.max_height = 4096;
  12351. } else {
  12352. dev->mode_config.max_width = 8192;
  12353. dev->mode_config.max_height = 8192;
  12354. }
  12355. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12356. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12357. dev->mode_config.cursor_height = 1023;
  12358. } else if (IS_GEN2(dev_priv)) {
  12359. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12360. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12361. } else {
  12362. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12363. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12364. }
  12365. dev->mode_config.fb_base = ggtt->gmadr.start;
  12366. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12367. INTEL_INFO(dev_priv)->num_pipes,
  12368. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12369. for_each_pipe(dev_priv, pipe) {
  12370. int ret;
  12371. ret = intel_crtc_init(dev_priv, pipe);
  12372. if (ret) {
  12373. drm_mode_config_cleanup(dev);
  12374. return ret;
  12375. }
  12376. }
  12377. intel_shared_dpll_init(dev);
  12378. intel_update_fdi_pll_freq(dev_priv);
  12379. intel_update_czclk(dev_priv);
  12380. intel_modeset_init_hw(dev);
  12381. if (dev_priv->max_cdclk_freq == 0)
  12382. intel_update_max_cdclk(dev_priv);
  12383. /* Just disable it once at startup */
  12384. i915_disable_vga(dev_priv);
  12385. intel_setup_outputs(dev_priv);
  12386. drm_modeset_lock_all(dev);
  12387. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12388. drm_modeset_unlock_all(dev);
  12389. for_each_intel_crtc(dev, crtc) {
  12390. struct intel_initial_plane_config plane_config = {};
  12391. if (!crtc->active)
  12392. continue;
  12393. /*
  12394. * Note that reserving the BIOS fb up front prevents us
  12395. * from stuffing other stolen allocations like the ring
  12396. * on top. This prevents some ugliness at boot time, and
  12397. * can even allow for smooth boot transitions if the BIOS
  12398. * fb is large enough for the active pipe configuration.
  12399. */
  12400. dev_priv->display.get_initial_plane_config(crtc,
  12401. &plane_config);
  12402. /*
  12403. * If the fb is shared between multiple heads, we'll
  12404. * just get the first one.
  12405. */
  12406. intel_find_initial_plane_obj(crtc, &plane_config);
  12407. }
  12408. /*
  12409. * Make sure hardware watermarks really match the state we read out.
  12410. * Note that we need to do this after reconstructing the BIOS fb's
  12411. * since the watermark calculation done here will use pstate->fb.
  12412. */
  12413. if (!HAS_GMCH_DISPLAY(dev_priv))
  12414. sanitize_watermarks(dev);
  12415. return 0;
  12416. }
  12417. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12418. {
  12419. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12420. /* 640x480@60Hz, ~25175 kHz */
  12421. struct dpll clock = {
  12422. .m1 = 18,
  12423. .m2 = 7,
  12424. .p1 = 13,
  12425. .p2 = 4,
  12426. .n = 2,
  12427. };
  12428. u32 dpll, fp;
  12429. int i;
  12430. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12431. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12432. pipe_name(pipe), clock.vco, clock.dot);
  12433. fp = i9xx_dpll_compute_fp(&clock);
  12434. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12435. DPLL_VGA_MODE_DIS |
  12436. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12437. PLL_P2_DIVIDE_BY_4 |
  12438. PLL_REF_INPUT_DREFCLK |
  12439. DPLL_VCO_ENABLE;
  12440. I915_WRITE(FP0(pipe), fp);
  12441. I915_WRITE(FP1(pipe), fp);
  12442. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12443. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12444. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12445. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12446. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12447. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12448. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12449. /*
  12450. * Apparently we need to have VGA mode enabled prior to changing
  12451. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12452. * dividers, even though the register value does change.
  12453. */
  12454. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12455. I915_WRITE(DPLL(pipe), dpll);
  12456. /* Wait for the clocks to stabilize. */
  12457. POSTING_READ(DPLL(pipe));
  12458. udelay(150);
  12459. /* The pixel multiplier can only be updated once the
  12460. * DPLL is enabled and the clocks are stable.
  12461. *
  12462. * So write it again.
  12463. */
  12464. I915_WRITE(DPLL(pipe), dpll);
  12465. /* We do this three times for luck */
  12466. for (i = 0; i < 3 ; i++) {
  12467. I915_WRITE(DPLL(pipe), dpll);
  12468. POSTING_READ(DPLL(pipe));
  12469. udelay(150); /* wait for warmup */
  12470. }
  12471. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12472. POSTING_READ(PIPECONF(pipe));
  12473. intel_wait_for_pipe_scanline_moving(crtc);
  12474. }
  12475. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12476. {
  12477. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12478. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12479. pipe_name(pipe));
  12480. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12481. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12482. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12483. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
  12484. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
  12485. I915_WRITE(PIPECONF(pipe), 0);
  12486. POSTING_READ(PIPECONF(pipe));
  12487. intel_wait_for_pipe_scanline_stopped(crtc);
  12488. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12489. POSTING_READ(DPLL(pipe));
  12490. }
  12491. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12492. struct intel_plane *plane)
  12493. {
  12494. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12495. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  12496. u32 val = I915_READ(DSPCNTR(i9xx_plane));
  12497. return (val & DISPLAY_PLANE_ENABLE) == 0 ||
  12498. (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
  12499. }
  12500. static void
  12501. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12502. {
  12503. struct intel_crtc *crtc;
  12504. if (INTEL_GEN(dev_priv) >= 4)
  12505. return;
  12506. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12507. struct intel_plane *plane =
  12508. to_intel_plane(crtc->base.primary);
  12509. if (intel_plane_mapping_ok(crtc, plane))
  12510. continue;
  12511. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12512. plane->base.name);
  12513. intel_plane_disable_noatomic(crtc, plane);
  12514. }
  12515. }
  12516. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12517. {
  12518. struct drm_device *dev = crtc->base.dev;
  12519. struct intel_encoder *encoder;
  12520. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12521. return true;
  12522. return false;
  12523. }
  12524. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12525. {
  12526. struct drm_device *dev = encoder->base.dev;
  12527. struct intel_connector *connector;
  12528. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12529. return connector;
  12530. return NULL;
  12531. }
  12532. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12533. enum pipe pch_transcoder)
  12534. {
  12535. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12536. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12537. }
  12538. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12539. struct drm_modeset_acquire_ctx *ctx)
  12540. {
  12541. struct drm_device *dev = crtc->base.dev;
  12542. struct drm_i915_private *dev_priv = to_i915(dev);
  12543. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12544. /* Clear any frame start delays used for debugging left by the BIOS */
  12545. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12546. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12547. I915_WRITE(reg,
  12548. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12549. }
  12550. /* restore vblank interrupts to correct state */
  12551. drm_crtc_vblank_reset(&crtc->base);
  12552. if (crtc->active) {
  12553. struct intel_plane *plane;
  12554. drm_crtc_vblank_on(&crtc->base);
  12555. /* Disable everything but the primary plane */
  12556. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12557. const struct intel_plane_state *plane_state =
  12558. to_intel_plane_state(plane->base.state);
  12559. if (plane_state->base.visible &&
  12560. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12561. intel_plane_disable_noatomic(crtc, plane);
  12562. }
  12563. }
  12564. /* Adjust the state of the output pipe according to whether we
  12565. * have active connectors/encoders. */
  12566. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12567. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12568. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12569. /*
  12570. * We start out with underrun reporting disabled to avoid races.
  12571. * For correct bookkeeping mark this on active crtcs.
  12572. *
  12573. * Also on gmch platforms we dont have any hardware bits to
  12574. * disable the underrun reporting. Which means we need to start
  12575. * out with underrun reporting disabled also on inactive pipes,
  12576. * since otherwise we'll complain about the garbage we read when
  12577. * e.g. coming up after runtime pm.
  12578. *
  12579. * No protection against concurrent access is required - at
  12580. * worst a fifo underrun happens which also sets this to false.
  12581. */
  12582. crtc->cpu_fifo_underrun_disabled = true;
  12583. /*
  12584. * We track the PCH trancoder underrun reporting state
  12585. * within the crtc. With crtc for pipe A housing the underrun
  12586. * reporting state for PCH transcoder A, crtc for pipe B housing
  12587. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12588. * and marking underrun reporting as disabled for the non-existing
  12589. * PCH transcoders B and C would prevent enabling the south
  12590. * error interrupt (see cpt_can_enable_serr_int()).
  12591. */
  12592. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12593. crtc->pch_fifo_underrun_disabled = true;
  12594. }
  12595. }
  12596. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12597. {
  12598. struct intel_connector *connector;
  12599. /* We need to check both for a crtc link (meaning that the
  12600. * encoder is active and trying to read from a pipe) and the
  12601. * pipe itself being active. */
  12602. bool has_active_crtc = encoder->base.crtc &&
  12603. to_intel_crtc(encoder->base.crtc)->active;
  12604. connector = intel_encoder_find_connector(encoder);
  12605. if (connector && !has_active_crtc) {
  12606. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12607. encoder->base.base.id,
  12608. encoder->base.name);
  12609. /* Connector is active, but has no active pipe. This is
  12610. * fallout from our resume register restoring. Disable
  12611. * the encoder manually again. */
  12612. if (encoder->base.crtc) {
  12613. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12614. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12615. encoder->base.base.id,
  12616. encoder->base.name);
  12617. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12618. if (encoder->post_disable)
  12619. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12620. }
  12621. encoder->base.crtc = NULL;
  12622. /* Inconsistent output/port/pipe state happens presumably due to
  12623. * a bug in one of the get_hw_state functions. Or someplace else
  12624. * in our code, like the register restore mess on resume. Clamp
  12625. * things to off as a safer default. */
  12626. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12627. connector->base.encoder = NULL;
  12628. }
  12629. }
  12630. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12631. {
  12632. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12633. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12634. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12635. i915_disable_vga(dev_priv);
  12636. }
  12637. }
  12638. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12639. {
  12640. /* This function can be called both from intel_modeset_setup_hw_state or
  12641. * at a very early point in our resume sequence, where the power well
  12642. * structures are not yet restored. Since this function is at a very
  12643. * paranoid "someone might have enabled VGA while we were not looking"
  12644. * level, just check if the power well is enabled instead of trying to
  12645. * follow the "don't touch the power well if we don't need it" policy
  12646. * the rest of the driver uses. */
  12647. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12648. return;
  12649. i915_redisable_vga_power_on(dev_priv);
  12650. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12651. }
  12652. /* FIXME read out full plane state for all planes */
  12653. static void readout_plane_state(struct intel_crtc *crtc)
  12654. {
  12655. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12656. struct intel_crtc_state *crtc_state =
  12657. to_intel_crtc_state(crtc->base.state);
  12658. struct intel_plane *plane;
  12659. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12660. struct intel_plane_state *plane_state =
  12661. to_intel_plane_state(plane->base.state);
  12662. bool visible = plane->get_hw_state(plane);
  12663. intel_set_plane_visible(crtc_state, plane_state, visible);
  12664. }
  12665. }
  12666. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12667. {
  12668. struct drm_i915_private *dev_priv = to_i915(dev);
  12669. enum pipe pipe;
  12670. struct intel_crtc *crtc;
  12671. struct intel_encoder *encoder;
  12672. struct intel_connector *connector;
  12673. struct drm_connector_list_iter conn_iter;
  12674. int i;
  12675. dev_priv->active_crtcs = 0;
  12676. for_each_intel_crtc(dev, crtc) {
  12677. struct intel_crtc_state *crtc_state =
  12678. to_intel_crtc_state(crtc->base.state);
  12679. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12680. memset(crtc_state, 0, sizeof(*crtc_state));
  12681. crtc_state->base.crtc = &crtc->base;
  12682. crtc_state->base.active = crtc_state->base.enable =
  12683. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12684. crtc->base.enabled = crtc_state->base.enable;
  12685. crtc->active = crtc_state->base.active;
  12686. if (crtc_state->base.active)
  12687. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12688. readout_plane_state(crtc);
  12689. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12690. crtc->base.base.id, crtc->base.name,
  12691. enableddisabled(crtc_state->base.active));
  12692. }
  12693. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12694. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12695. pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
  12696. &pll->state.hw_state);
  12697. pll->state.crtc_mask = 0;
  12698. for_each_intel_crtc(dev, crtc) {
  12699. struct intel_crtc_state *crtc_state =
  12700. to_intel_crtc_state(crtc->base.state);
  12701. if (crtc_state->base.active &&
  12702. crtc_state->shared_dpll == pll)
  12703. pll->state.crtc_mask |= 1 << crtc->pipe;
  12704. }
  12705. pll->active_mask = pll->state.crtc_mask;
  12706. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12707. pll->info->name, pll->state.crtc_mask, pll->on);
  12708. }
  12709. for_each_intel_encoder(dev, encoder) {
  12710. pipe = 0;
  12711. if (encoder->get_hw_state(encoder, &pipe)) {
  12712. struct intel_crtc_state *crtc_state;
  12713. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12714. crtc_state = to_intel_crtc_state(crtc->base.state);
  12715. encoder->base.crtc = &crtc->base;
  12716. encoder->get_config(encoder, crtc_state);
  12717. } else {
  12718. encoder->base.crtc = NULL;
  12719. }
  12720. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12721. encoder->base.base.id, encoder->base.name,
  12722. enableddisabled(encoder->base.crtc),
  12723. pipe_name(pipe));
  12724. }
  12725. drm_connector_list_iter_begin(dev, &conn_iter);
  12726. for_each_intel_connector_iter(connector, &conn_iter) {
  12727. if (connector->get_hw_state(connector)) {
  12728. connector->base.dpms = DRM_MODE_DPMS_ON;
  12729. encoder = connector->encoder;
  12730. connector->base.encoder = &encoder->base;
  12731. if (encoder->base.crtc &&
  12732. encoder->base.crtc->state->active) {
  12733. /*
  12734. * This has to be done during hardware readout
  12735. * because anything calling .crtc_disable may
  12736. * rely on the connector_mask being accurate.
  12737. */
  12738. encoder->base.crtc->state->connector_mask |=
  12739. 1 << drm_connector_index(&connector->base);
  12740. encoder->base.crtc->state->encoder_mask |=
  12741. 1 << drm_encoder_index(&encoder->base);
  12742. }
  12743. } else {
  12744. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12745. connector->base.encoder = NULL;
  12746. }
  12747. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12748. connector->base.base.id, connector->base.name,
  12749. enableddisabled(connector->base.encoder));
  12750. }
  12751. drm_connector_list_iter_end(&conn_iter);
  12752. for_each_intel_crtc(dev, crtc) {
  12753. struct intel_crtc_state *crtc_state =
  12754. to_intel_crtc_state(crtc->base.state);
  12755. int min_cdclk = 0;
  12756. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12757. if (crtc_state->base.active) {
  12758. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12759. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12760. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12761. /*
  12762. * The initial mode needs to be set in order to keep
  12763. * the atomic core happy. It wants a valid mode if the
  12764. * crtc's enabled, so we do the above call.
  12765. *
  12766. * But we don't set all the derived state fully, hence
  12767. * set a flag to indicate that a full recalculation is
  12768. * needed on the next commit.
  12769. */
  12770. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12771. intel_crtc_compute_pixel_rate(crtc_state);
  12772. if (dev_priv->display.modeset_calc_cdclk) {
  12773. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12774. if (WARN_ON(min_cdclk < 0))
  12775. min_cdclk = 0;
  12776. }
  12777. drm_calc_timestamping_constants(&crtc->base,
  12778. &crtc_state->base.adjusted_mode);
  12779. update_scanline_offset(crtc);
  12780. }
  12781. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12782. dev_priv->min_voltage_level[crtc->pipe] =
  12783. crtc_state->min_voltage_level;
  12784. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12785. }
  12786. }
  12787. static void
  12788. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12789. {
  12790. struct intel_encoder *encoder;
  12791. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12792. u64 get_domains;
  12793. enum intel_display_power_domain domain;
  12794. if (!encoder->get_power_domains)
  12795. continue;
  12796. get_domains = encoder->get_power_domains(encoder);
  12797. for_each_power_domain(domain, get_domains)
  12798. intel_display_power_get(dev_priv, domain);
  12799. }
  12800. }
  12801. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12802. {
  12803. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12804. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12805. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12806. DARBF_GATING_DIS);
  12807. if (IS_HASWELL(dev_priv)) {
  12808. /*
  12809. * WaRsPkgCStateDisplayPMReq:hsw
  12810. * System hang if this isn't done before disabling all planes!
  12811. */
  12812. I915_WRITE(CHICKEN_PAR1_1,
  12813. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12814. }
  12815. }
  12816. /* Scan out the current hw modeset state,
  12817. * and sanitizes it to the current state
  12818. */
  12819. static void
  12820. intel_modeset_setup_hw_state(struct drm_device *dev,
  12821. struct drm_modeset_acquire_ctx *ctx)
  12822. {
  12823. struct drm_i915_private *dev_priv = to_i915(dev);
  12824. enum pipe pipe;
  12825. struct intel_crtc *crtc;
  12826. struct intel_encoder *encoder;
  12827. int i;
  12828. intel_early_display_was(dev_priv);
  12829. intel_modeset_readout_hw_state(dev);
  12830. /* HW state is read out, now we need to sanitize this mess. */
  12831. get_encoder_power_domains(dev_priv);
  12832. intel_sanitize_plane_mapping(dev_priv);
  12833. for_each_intel_encoder(dev, encoder) {
  12834. intel_sanitize_encoder(encoder);
  12835. }
  12836. for_each_pipe(dev_priv, pipe) {
  12837. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12838. intel_sanitize_crtc(crtc, ctx);
  12839. intel_dump_pipe_config(crtc, crtc->config,
  12840. "[setup_hw_state]");
  12841. }
  12842. intel_modeset_update_connector_atomic_state(dev);
  12843. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12844. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12845. if (!pll->on || pll->active_mask)
  12846. continue;
  12847. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
  12848. pll->info->name);
  12849. pll->info->funcs->disable(dev_priv, pll);
  12850. pll->on = false;
  12851. }
  12852. if (IS_G4X(dev_priv)) {
  12853. g4x_wm_get_hw_state(dev);
  12854. g4x_wm_sanitize(dev_priv);
  12855. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12856. vlv_wm_get_hw_state(dev);
  12857. vlv_wm_sanitize(dev_priv);
  12858. } else if (INTEL_GEN(dev_priv) >= 9) {
  12859. skl_wm_get_hw_state(dev);
  12860. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12861. ilk_wm_get_hw_state(dev);
  12862. }
  12863. for_each_intel_crtc(dev, crtc) {
  12864. u64 put_domains;
  12865. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12866. if (WARN_ON(put_domains))
  12867. modeset_put_power_domains(dev_priv, put_domains);
  12868. }
  12869. intel_display_set_init_power(dev_priv, false);
  12870. intel_power_domains_verify_state(dev_priv);
  12871. intel_fbc_init_pipe_state(dev_priv);
  12872. }
  12873. void intel_display_resume(struct drm_device *dev)
  12874. {
  12875. struct drm_i915_private *dev_priv = to_i915(dev);
  12876. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12877. struct drm_modeset_acquire_ctx ctx;
  12878. int ret;
  12879. dev_priv->modeset_restore_state = NULL;
  12880. if (state)
  12881. state->acquire_ctx = &ctx;
  12882. drm_modeset_acquire_init(&ctx, 0);
  12883. while (1) {
  12884. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12885. if (ret != -EDEADLK)
  12886. break;
  12887. drm_modeset_backoff(&ctx);
  12888. }
  12889. if (!ret)
  12890. ret = __intel_display_resume(dev, state, &ctx);
  12891. intel_enable_ipc(dev_priv);
  12892. drm_modeset_drop_locks(&ctx);
  12893. drm_modeset_acquire_fini(&ctx);
  12894. if (ret)
  12895. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12896. if (state)
  12897. drm_atomic_state_put(state);
  12898. }
  12899. int intel_connector_register(struct drm_connector *connector)
  12900. {
  12901. struct intel_connector *intel_connector = to_intel_connector(connector);
  12902. int ret;
  12903. ret = intel_backlight_device_register(intel_connector);
  12904. if (ret)
  12905. goto err;
  12906. return 0;
  12907. err:
  12908. return ret;
  12909. }
  12910. void intel_connector_unregister(struct drm_connector *connector)
  12911. {
  12912. struct intel_connector *intel_connector = to_intel_connector(connector);
  12913. intel_backlight_device_unregister(intel_connector);
  12914. intel_panel_destroy_backlight(connector);
  12915. }
  12916. static void intel_hpd_poll_fini(struct drm_device *dev)
  12917. {
  12918. struct intel_connector *connector;
  12919. struct drm_connector_list_iter conn_iter;
  12920. /* Kill all the work that may have been queued by hpd. */
  12921. drm_connector_list_iter_begin(dev, &conn_iter);
  12922. for_each_intel_connector_iter(connector, &conn_iter) {
  12923. if (connector->modeset_retry_work.func)
  12924. cancel_work_sync(&connector->modeset_retry_work);
  12925. if (connector->hdcp_shim) {
  12926. cancel_delayed_work_sync(&connector->hdcp_check_work);
  12927. cancel_work_sync(&connector->hdcp_prop_work);
  12928. }
  12929. }
  12930. drm_connector_list_iter_end(&conn_iter);
  12931. }
  12932. void intel_modeset_cleanup(struct drm_device *dev)
  12933. {
  12934. struct drm_i915_private *dev_priv = to_i915(dev);
  12935. flush_work(&dev_priv->atomic_helper.free_work);
  12936. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12937. intel_disable_gt_powersave(dev_priv);
  12938. /*
  12939. * Interrupts and polling as the first thing to avoid creating havoc.
  12940. * Too much stuff here (turning of connectors, ...) would
  12941. * experience fancy races otherwise.
  12942. */
  12943. intel_irq_uninstall(dev_priv);
  12944. /*
  12945. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12946. * poll handlers. Hence disable polling after hpd handling is shut down.
  12947. */
  12948. intel_hpd_poll_fini(dev);
  12949. /* poll work can call into fbdev, hence clean that up afterwards */
  12950. intel_fbdev_fini(dev_priv);
  12951. intel_unregister_dsm_handler();
  12952. intel_fbc_global_disable(dev_priv);
  12953. /* flush any delayed tasks or pending work */
  12954. flush_scheduled_work();
  12955. drm_mode_config_cleanup(dev);
  12956. intel_cleanup_overlay(dev_priv);
  12957. intel_cleanup_gt_powersave(dev_priv);
  12958. intel_teardown_gmbus(dev_priv);
  12959. destroy_workqueue(dev_priv->modeset_wq);
  12960. }
  12961. void intel_connector_attach_encoder(struct intel_connector *connector,
  12962. struct intel_encoder *encoder)
  12963. {
  12964. connector->encoder = encoder;
  12965. drm_mode_connector_attach_encoder(&connector->base,
  12966. &encoder->base);
  12967. }
  12968. /*
  12969. * set vga decode state - true == enable VGA decode
  12970. */
  12971. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12972. {
  12973. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12974. u16 gmch_ctrl;
  12975. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12976. DRM_ERROR("failed to read control word\n");
  12977. return -EIO;
  12978. }
  12979. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12980. return 0;
  12981. if (state)
  12982. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12983. else
  12984. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12985. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12986. DRM_ERROR("failed to write control word\n");
  12987. return -EIO;
  12988. }
  12989. return 0;
  12990. }
  12991. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12992. struct intel_display_error_state {
  12993. u32 power_well_driver;
  12994. int num_transcoders;
  12995. struct intel_cursor_error_state {
  12996. u32 control;
  12997. u32 position;
  12998. u32 base;
  12999. u32 size;
  13000. } cursor[I915_MAX_PIPES];
  13001. struct intel_pipe_error_state {
  13002. bool power_domain_on;
  13003. u32 source;
  13004. u32 stat;
  13005. } pipe[I915_MAX_PIPES];
  13006. struct intel_plane_error_state {
  13007. u32 control;
  13008. u32 stride;
  13009. u32 size;
  13010. u32 pos;
  13011. u32 addr;
  13012. u32 surface;
  13013. u32 tile_offset;
  13014. } plane[I915_MAX_PIPES];
  13015. struct intel_transcoder_error_state {
  13016. bool power_domain_on;
  13017. enum transcoder cpu_transcoder;
  13018. u32 conf;
  13019. u32 htotal;
  13020. u32 hblank;
  13021. u32 hsync;
  13022. u32 vtotal;
  13023. u32 vblank;
  13024. u32 vsync;
  13025. } transcoder[4];
  13026. };
  13027. struct intel_display_error_state *
  13028. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13029. {
  13030. struct intel_display_error_state *error;
  13031. int transcoders[] = {
  13032. TRANSCODER_A,
  13033. TRANSCODER_B,
  13034. TRANSCODER_C,
  13035. TRANSCODER_EDP,
  13036. };
  13037. int i;
  13038. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13039. return NULL;
  13040. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13041. if (error == NULL)
  13042. return NULL;
  13043. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13044. error->power_well_driver =
  13045. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  13046. for_each_pipe(dev_priv, i) {
  13047. error->pipe[i].power_domain_on =
  13048. __intel_display_power_is_enabled(dev_priv,
  13049. POWER_DOMAIN_PIPE(i));
  13050. if (!error->pipe[i].power_domain_on)
  13051. continue;
  13052. error->cursor[i].control = I915_READ(CURCNTR(i));
  13053. error->cursor[i].position = I915_READ(CURPOS(i));
  13054. error->cursor[i].base = I915_READ(CURBASE(i));
  13055. error->plane[i].control = I915_READ(DSPCNTR(i));
  13056. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13057. if (INTEL_GEN(dev_priv) <= 3) {
  13058. error->plane[i].size = I915_READ(DSPSIZE(i));
  13059. error->plane[i].pos = I915_READ(DSPPOS(i));
  13060. }
  13061. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13062. error->plane[i].addr = I915_READ(DSPADDR(i));
  13063. if (INTEL_GEN(dev_priv) >= 4) {
  13064. error->plane[i].surface = I915_READ(DSPSURF(i));
  13065. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13066. }
  13067. error->pipe[i].source = I915_READ(PIPESRC(i));
  13068. if (HAS_GMCH_DISPLAY(dev_priv))
  13069. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13070. }
  13071. /* Note: this does not include DSI transcoders. */
  13072. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13073. if (HAS_DDI(dev_priv))
  13074. error->num_transcoders++; /* Account for eDP. */
  13075. for (i = 0; i < error->num_transcoders; i++) {
  13076. enum transcoder cpu_transcoder = transcoders[i];
  13077. error->transcoder[i].power_domain_on =
  13078. __intel_display_power_is_enabled(dev_priv,
  13079. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13080. if (!error->transcoder[i].power_domain_on)
  13081. continue;
  13082. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13083. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13084. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13085. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13086. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13087. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13088. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13089. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13090. }
  13091. return error;
  13092. }
  13093. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13094. void
  13095. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13096. struct intel_display_error_state *error)
  13097. {
  13098. struct drm_i915_private *dev_priv = m->i915;
  13099. int i;
  13100. if (!error)
  13101. return;
  13102. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13103. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13104. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13105. error->power_well_driver);
  13106. for_each_pipe(dev_priv, i) {
  13107. err_printf(m, "Pipe [%d]:\n", i);
  13108. err_printf(m, " Power: %s\n",
  13109. onoff(error->pipe[i].power_domain_on));
  13110. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13111. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13112. err_printf(m, "Plane [%d]:\n", i);
  13113. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13114. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13115. if (INTEL_GEN(dev_priv) <= 3) {
  13116. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13117. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13118. }
  13119. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13120. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13121. if (INTEL_GEN(dev_priv) >= 4) {
  13122. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13123. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13124. }
  13125. err_printf(m, "Cursor [%d]:\n", i);
  13126. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13127. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13128. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13129. }
  13130. for (i = 0; i < error->num_transcoders; i++) {
  13131. err_printf(m, "CPU transcoder: %s\n",
  13132. transcoder_name(error->transcoder[i].cpu_transcoder));
  13133. err_printf(m, " Power: %s\n",
  13134. onoff(error->transcoder[i].power_domain_on));
  13135. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13136. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13137. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13138. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13139. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13140. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13141. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13142. }
  13143. }
  13144. #endif