intel_display.c 437 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint64_t skl_format_modifiers_noccs[] = {
  86. I915_FORMAT_MOD_Yf_TILED,
  87. I915_FORMAT_MOD_Y_TILED,
  88. I915_FORMAT_MOD_X_TILED,
  89. DRM_FORMAT_MOD_LINEAR,
  90. DRM_FORMAT_MOD_INVALID
  91. };
  92. static const uint64_t skl_format_modifiers_ccs[] = {
  93. I915_FORMAT_MOD_Yf_TILED_CCS,
  94. I915_FORMAT_MOD_Y_TILED_CCS,
  95. I915_FORMAT_MOD_Yf_TILED,
  96. I915_FORMAT_MOD_Y_TILED,
  97. I915_FORMAT_MOD_X_TILED,
  98. DRM_FORMAT_MOD_LINEAR,
  99. DRM_FORMAT_MOD_INVALID
  100. };
  101. /* Cursor formats */
  102. static const uint32_t intel_cursor_formats[] = {
  103. DRM_FORMAT_ARGB8888,
  104. };
  105. static const uint64_t cursor_format_modifiers[] = {
  106. DRM_FORMAT_MOD_LINEAR,
  107. DRM_FORMAT_MOD_INVALID
  108. };
  109. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  110. struct intel_crtc_state *pipe_config);
  111. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  112. struct intel_crtc_state *pipe_config);
  113. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  114. struct drm_i915_gem_object *obj,
  115. struct drm_mode_fb_cmd2 *mode_cmd);
  116. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  117. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  118. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  119. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  120. struct intel_link_m_n *m_n,
  121. struct intel_link_m_n *m2_n2);
  122. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  123. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  124. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  125. static void vlv_prepare_pll(struct intel_crtc *crtc,
  126. const struct intel_crtc_state *pipe_config);
  127. static void chv_prepare_pll(struct intel_crtc *crtc,
  128. const struct intel_crtc_state *pipe_config);
  129. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  130. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  131. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  132. struct intel_crtc_state *crtc_state);
  133. static void skylake_pfit_enable(struct intel_crtc *crtc);
  134. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  135. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  136. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  137. struct drm_modeset_acquire_ctx *ctx);
  138. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  139. struct intel_limit {
  140. struct {
  141. int min, max;
  142. } dot, vco, n, m, m1, m2, p, p1;
  143. struct {
  144. int dot_limit;
  145. int p2_slow, p2_fast;
  146. } p2;
  147. };
  148. /* returns HPLL frequency in kHz */
  149. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  150. {
  151. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  152. /* Obtain SKU information */
  153. mutex_lock(&dev_priv->sb_lock);
  154. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  155. CCK_FUSE_HPLL_FREQ_MASK;
  156. mutex_unlock(&dev_priv->sb_lock);
  157. return vco_freq[hpll_freq] * 1000;
  158. }
  159. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  160. const char *name, u32 reg, int ref_freq)
  161. {
  162. u32 val;
  163. int divider;
  164. mutex_lock(&dev_priv->sb_lock);
  165. val = vlv_cck_read(dev_priv, reg);
  166. mutex_unlock(&dev_priv->sb_lock);
  167. divider = val & CCK_FREQUENCY_VALUES;
  168. WARN((val & CCK_FREQUENCY_STATUS) !=
  169. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  170. "%s change in progress\n", name);
  171. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  172. }
  173. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  174. const char *name, u32 reg)
  175. {
  176. if (dev_priv->hpll_freq == 0)
  177. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  178. return vlv_get_cck_clock(dev_priv, name, reg,
  179. dev_priv->hpll_freq);
  180. }
  181. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  182. {
  183. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  184. return;
  185. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  186. CCK_CZ_CLOCK_CONTROL);
  187. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  188. }
  189. static inline u32 /* units of 100MHz */
  190. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  191. const struct intel_crtc_state *pipe_config)
  192. {
  193. if (HAS_DDI(dev_priv))
  194. return pipe_config->port_clock; /* SPLL */
  195. else if (IS_GEN5(dev_priv))
  196. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  197. else
  198. return 270000;
  199. }
  200. static const struct intel_limit intel_limits_i8xx_dac = {
  201. .dot = { .min = 25000, .max = 350000 },
  202. .vco = { .min = 908000, .max = 1512000 },
  203. .n = { .min = 2, .max = 16 },
  204. .m = { .min = 96, .max = 140 },
  205. .m1 = { .min = 18, .max = 26 },
  206. .m2 = { .min = 6, .max = 16 },
  207. .p = { .min = 4, .max = 128 },
  208. .p1 = { .min = 2, .max = 33 },
  209. .p2 = { .dot_limit = 165000,
  210. .p2_slow = 4, .p2_fast = 2 },
  211. };
  212. static const struct intel_limit intel_limits_i8xx_dvo = {
  213. .dot = { .min = 25000, .max = 350000 },
  214. .vco = { .min = 908000, .max = 1512000 },
  215. .n = { .min = 2, .max = 16 },
  216. .m = { .min = 96, .max = 140 },
  217. .m1 = { .min = 18, .max = 26 },
  218. .m2 = { .min = 6, .max = 16 },
  219. .p = { .min = 4, .max = 128 },
  220. .p1 = { .min = 2, .max = 33 },
  221. .p2 = { .dot_limit = 165000,
  222. .p2_slow = 4, .p2_fast = 4 },
  223. };
  224. static const struct intel_limit intel_limits_i8xx_lvds = {
  225. .dot = { .min = 25000, .max = 350000 },
  226. .vco = { .min = 908000, .max = 1512000 },
  227. .n = { .min = 2, .max = 16 },
  228. .m = { .min = 96, .max = 140 },
  229. .m1 = { .min = 18, .max = 26 },
  230. .m2 = { .min = 6, .max = 16 },
  231. .p = { .min = 4, .max = 128 },
  232. .p1 = { .min = 1, .max = 6 },
  233. .p2 = { .dot_limit = 165000,
  234. .p2_slow = 14, .p2_fast = 7 },
  235. };
  236. static const struct intel_limit intel_limits_i9xx_sdvo = {
  237. .dot = { .min = 20000, .max = 400000 },
  238. .vco = { .min = 1400000, .max = 2800000 },
  239. .n = { .min = 1, .max = 6 },
  240. .m = { .min = 70, .max = 120 },
  241. .m1 = { .min = 8, .max = 18 },
  242. .m2 = { .min = 3, .max = 7 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 200000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const struct intel_limit intel_limits_i9xx_lvds = {
  249. .dot = { .min = 20000, .max = 400000 },
  250. .vco = { .min = 1400000, .max = 2800000 },
  251. .n = { .min = 1, .max = 6 },
  252. .m = { .min = 70, .max = 120 },
  253. .m1 = { .min = 8, .max = 18 },
  254. .m2 = { .min = 3, .max = 7 },
  255. .p = { .min = 7, .max = 98 },
  256. .p1 = { .min = 1, .max = 8 },
  257. .p2 = { .dot_limit = 112000,
  258. .p2_slow = 14, .p2_fast = 7 },
  259. };
  260. static const struct intel_limit intel_limits_g4x_sdvo = {
  261. .dot = { .min = 25000, .max = 270000 },
  262. .vco = { .min = 1750000, .max = 3500000},
  263. .n = { .min = 1, .max = 4 },
  264. .m = { .min = 104, .max = 138 },
  265. .m1 = { .min = 17, .max = 23 },
  266. .m2 = { .min = 5, .max = 11 },
  267. .p = { .min = 10, .max = 30 },
  268. .p1 = { .min = 1, .max = 3},
  269. .p2 = { .dot_limit = 270000,
  270. .p2_slow = 10,
  271. .p2_fast = 10
  272. },
  273. };
  274. static const struct intel_limit intel_limits_g4x_hdmi = {
  275. .dot = { .min = 22000, .max = 400000 },
  276. .vco = { .min = 1750000, .max = 3500000},
  277. .n = { .min = 1, .max = 4 },
  278. .m = { .min = 104, .max = 138 },
  279. .m1 = { .min = 16, .max = 23 },
  280. .m2 = { .min = 5, .max = 11 },
  281. .p = { .min = 5, .max = 80 },
  282. .p1 = { .min = 1, .max = 8},
  283. .p2 = { .dot_limit = 165000,
  284. .p2_slow = 10, .p2_fast = 5 },
  285. };
  286. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  287. .dot = { .min = 20000, .max = 115000 },
  288. .vco = { .min = 1750000, .max = 3500000 },
  289. .n = { .min = 1, .max = 3 },
  290. .m = { .min = 104, .max = 138 },
  291. .m1 = { .min = 17, .max = 23 },
  292. .m2 = { .min = 5, .max = 11 },
  293. .p = { .min = 28, .max = 112 },
  294. .p1 = { .min = 2, .max = 8 },
  295. .p2 = { .dot_limit = 0,
  296. .p2_slow = 14, .p2_fast = 14
  297. },
  298. };
  299. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  300. .dot = { .min = 80000, .max = 224000 },
  301. .vco = { .min = 1750000, .max = 3500000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 104, .max = 138 },
  304. .m1 = { .min = 17, .max = 23 },
  305. .m2 = { .min = 5, .max = 11 },
  306. .p = { .min = 14, .max = 42 },
  307. .p1 = { .min = 2, .max = 6 },
  308. .p2 = { .dot_limit = 0,
  309. .p2_slow = 7, .p2_fast = 7
  310. },
  311. };
  312. static const struct intel_limit intel_limits_pineview_sdvo = {
  313. .dot = { .min = 20000, .max = 400000},
  314. .vco = { .min = 1700000, .max = 3500000 },
  315. /* Pineview's Ncounter is a ring counter */
  316. .n = { .min = 3, .max = 6 },
  317. .m = { .min = 2, .max = 256 },
  318. /* Pineview only has one combined m divider, which we treat as m2. */
  319. .m1 = { .min = 0, .max = 0 },
  320. .m2 = { .min = 0, .max = 254 },
  321. .p = { .min = 5, .max = 80 },
  322. .p1 = { .min = 1, .max = 8 },
  323. .p2 = { .dot_limit = 200000,
  324. .p2_slow = 10, .p2_fast = 5 },
  325. };
  326. static const struct intel_limit intel_limits_pineview_lvds = {
  327. .dot = { .min = 20000, .max = 400000 },
  328. .vco = { .min = 1700000, .max = 3500000 },
  329. .n = { .min = 3, .max = 6 },
  330. .m = { .min = 2, .max = 256 },
  331. .m1 = { .min = 0, .max = 0 },
  332. .m2 = { .min = 0, .max = 254 },
  333. .p = { .min = 7, .max = 112 },
  334. .p1 = { .min = 1, .max = 8 },
  335. .p2 = { .dot_limit = 112000,
  336. .p2_slow = 14, .p2_fast = 14 },
  337. };
  338. /* Ironlake / Sandybridge
  339. *
  340. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  341. * the range value for them is (actual_value - 2).
  342. */
  343. static const struct intel_limit intel_limits_ironlake_dac = {
  344. .dot = { .min = 25000, .max = 350000 },
  345. .vco = { .min = 1760000, .max = 3510000 },
  346. .n = { .min = 1, .max = 5 },
  347. .m = { .min = 79, .max = 127 },
  348. .m1 = { .min = 12, .max = 22 },
  349. .m2 = { .min = 5, .max = 9 },
  350. .p = { .min = 5, .max = 80 },
  351. .p1 = { .min = 1, .max = 8 },
  352. .p2 = { .dot_limit = 225000,
  353. .p2_slow = 10, .p2_fast = 5 },
  354. };
  355. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  356. .dot = { .min = 25000, .max = 350000 },
  357. .vco = { .min = 1760000, .max = 3510000 },
  358. .n = { .min = 1, .max = 3 },
  359. .m = { .min = 79, .max = 118 },
  360. .m1 = { .min = 12, .max = 22 },
  361. .m2 = { .min = 5, .max = 9 },
  362. .p = { .min = 28, .max = 112 },
  363. .p1 = { .min = 2, .max = 8 },
  364. .p2 = { .dot_limit = 225000,
  365. .p2_slow = 14, .p2_fast = 14 },
  366. };
  367. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  368. .dot = { .min = 25000, .max = 350000 },
  369. .vco = { .min = 1760000, .max = 3510000 },
  370. .n = { .min = 1, .max = 3 },
  371. .m = { .min = 79, .max = 127 },
  372. .m1 = { .min = 12, .max = 22 },
  373. .m2 = { .min = 5, .max = 9 },
  374. .p = { .min = 14, .max = 56 },
  375. .p1 = { .min = 2, .max = 8 },
  376. .p2 = { .dot_limit = 225000,
  377. .p2_slow = 7, .p2_fast = 7 },
  378. };
  379. /* LVDS 100mhz refclk limits. */
  380. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  381. .dot = { .min = 25000, .max = 350000 },
  382. .vco = { .min = 1760000, .max = 3510000 },
  383. .n = { .min = 1, .max = 2 },
  384. .m = { .min = 79, .max = 126 },
  385. .m1 = { .min = 12, .max = 22 },
  386. .m2 = { .min = 5, .max = 9 },
  387. .p = { .min = 28, .max = 112 },
  388. .p1 = { .min = 2, .max = 8 },
  389. .p2 = { .dot_limit = 225000,
  390. .p2_slow = 14, .p2_fast = 14 },
  391. };
  392. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  393. .dot = { .min = 25000, .max = 350000 },
  394. .vco = { .min = 1760000, .max = 3510000 },
  395. .n = { .min = 1, .max = 3 },
  396. .m = { .min = 79, .max = 126 },
  397. .m1 = { .min = 12, .max = 22 },
  398. .m2 = { .min = 5, .max = 9 },
  399. .p = { .min = 14, .max = 42 },
  400. .p1 = { .min = 2, .max = 6 },
  401. .p2 = { .dot_limit = 225000,
  402. .p2_slow = 7, .p2_fast = 7 },
  403. };
  404. static const struct intel_limit intel_limits_vlv = {
  405. /*
  406. * These are the data rate limits (measured in fast clocks)
  407. * since those are the strictest limits we have. The fast
  408. * clock and actual rate limits are more relaxed, so checking
  409. * them would make no difference.
  410. */
  411. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  412. .vco = { .min = 4000000, .max = 6000000 },
  413. .n = { .min = 1, .max = 7 },
  414. .m1 = { .min = 2, .max = 3 },
  415. .m2 = { .min = 11, .max = 156 },
  416. .p1 = { .min = 2, .max = 3 },
  417. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  418. };
  419. static const struct intel_limit intel_limits_chv = {
  420. /*
  421. * These are the data rate limits (measured in fast clocks)
  422. * since those are the strictest limits we have. The fast
  423. * clock and actual rate limits are more relaxed, so checking
  424. * them would make no difference.
  425. */
  426. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  427. .vco = { .min = 4800000, .max = 6480000 },
  428. .n = { .min = 1, .max = 1 },
  429. .m1 = { .min = 2, .max = 2 },
  430. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  431. .p1 = { .min = 2, .max = 4 },
  432. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  433. };
  434. static const struct intel_limit intel_limits_bxt = {
  435. /* FIXME: find real dot limits */
  436. .dot = { .min = 0, .max = INT_MAX },
  437. .vco = { .min = 4800000, .max = 6700000 },
  438. .n = { .min = 1, .max = 1 },
  439. .m1 = { .min = 2, .max = 2 },
  440. /* FIXME: find real m2 limits */
  441. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  442. .p1 = { .min = 2, .max = 4 },
  443. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  444. };
  445. static bool
  446. needs_modeset(struct drm_crtc_state *state)
  447. {
  448. return drm_atomic_crtc_needs_modeset(state);
  449. }
  450. /*
  451. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  452. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  453. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  454. * The helpers' return value is the rate of the clock that is fed to the
  455. * display engine's pipe which can be the above fast dot clock rate or a
  456. * divided-down version of it.
  457. */
  458. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  459. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  460. {
  461. clock->m = clock->m2 + 2;
  462. clock->p = clock->p1 * clock->p2;
  463. if (WARN_ON(clock->n == 0 || clock->p == 0))
  464. return 0;
  465. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  466. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  467. return clock->dot;
  468. }
  469. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  470. {
  471. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  472. }
  473. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  474. {
  475. clock->m = i9xx_dpll_compute_m(clock);
  476. clock->p = clock->p1 * clock->p2;
  477. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  478. return 0;
  479. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  480. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  481. return clock->dot;
  482. }
  483. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  484. {
  485. clock->m = clock->m1 * clock->m2;
  486. clock->p = clock->p1 * clock->p2;
  487. if (WARN_ON(clock->n == 0 || clock->p == 0))
  488. return 0;
  489. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  490. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  491. return clock->dot / 5;
  492. }
  493. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  494. {
  495. clock->m = clock->m1 * clock->m2;
  496. clock->p = clock->p1 * clock->p2;
  497. if (WARN_ON(clock->n == 0 || clock->p == 0))
  498. return 0;
  499. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  500. clock->n << 22);
  501. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  502. return clock->dot / 5;
  503. }
  504. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  505. /**
  506. * Returns whether the given set of divisors are valid for a given refclk with
  507. * the given connectors.
  508. */
  509. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  510. const struct intel_limit *limit,
  511. const struct dpll *clock)
  512. {
  513. if (clock->n < limit->n.min || limit->n.max < clock->n)
  514. INTELPllInvalid("n out of range\n");
  515. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  516. INTELPllInvalid("p1 out of range\n");
  517. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  518. INTELPllInvalid("m2 out of range\n");
  519. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  520. INTELPllInvalid("m1 out of range\n");
  521. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  522. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  523. if (clock->m1 <= clock->m2)
  524. INTELPllInvalid("m1 <= m2\n");
  525. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  526. !IS_GEN9_LP(dev_priv)) {
  527. if (clock->p < limit->p.min || limit->p.max < clock->p)
  528. INTELPllInvalid("p out of range\n");
  529. if (clock->m < limit->m.min || limit->m.max < clock->m)
  530. INTELPllInvalid("m out of range\n");
  531. }
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static int
  542. i9xx_select_p2_div(const struct intel_limit *limit,
  543. const struct intel_crtc_state *crtc_state,
  544. int target)
  545. {
  546. struct drm_device *dev = crtc_state->base.crtc->dev;
  547. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  548. /*
  549. * For LVDS just rely on its current settings for dual-channel.
  550. * We haven't figured out how to reliably set up different
  551. * single/dual channel state, if we even can.
  552. */
  553. if (intel_is_dual_link_lvds(dev))
  554. return limit->p2.p2_fast;
  555. else
  556. return limit->p2.p2_slow;
  557. } else {
  558. if (target < limit->p2.dot_limit)
  559. return limit->p2.p2_slow;
  560. else
  561. return limit->p2.p2_fast;
  562. }
  563. }
  564. /*
  565. * Returns a set of divisors for the desired target clock with the given
  566. * refclk, or FALSE. The returned values represent the clock equation:
  567. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  568. *
  569. * Target and reference clocks are specified in kHz.
  570. *
  571. * If match_clock is provided, then best_clock P divider must match the P
  572. * divider from @match_clock used for LVDS downclocking.
  573. */
  574. static bool
  575. i9xx_find_best_dpll(const struct intel_limit *limit,
  576. struct intel_crtc_state *crtc_state,
  577. int target, int refclk, struct dpll *match_clock,
  578. struct dpll *best_clock)
  579. {
  580. struct drm_device *dev = crtc_state->base.crtc->dev;
  581. struct dpll clock;
  582. int err = target;
  583. memset(best_clock, 0, sizeof(*best_clock));
  584. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  585. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  586. clock.m1++) {
  587. for (clock.m2 = limit->m2.min;
  588. clock.m2 <= limit->m2.max; clock.m2++) {
  589. if (clock.m2 >= clock.m1)
  590. break;
  591. for (clock.n = limit->n.min;
  592. clock.n <= limit->n.max; clock.n++) {
  593. for (clock.p1 = limit->p1.min;
  594. clock.p1 <= limit->p1.max; clock.p1++) {
  595. int this_err;
  596. i9xx_calc_dpll_params(refclk, &clock);
  597. if (!intel_PLL_is_valid(to_i915(dev),
  598. limit,
  599. &clock))
  600. continue;
  601. if (match_clock &&
  602. clock.p != match_clock->p)
  603. continue;
  604. this_err = abs(clock.dot - target);
  605. if (this_err < err) {
  606. *best_clock = clock;
  607. err = this_err;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. return (err != target);
  614. }
  615. /*
  616. * Returns a set of divisors for the desired target clock with the given
  617. * refclk, or FALSE. The returned values represent the clock equation:
  618. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  619. *
  620. * Target and reference clocks are specified in kHz.
  621. *
  622. * If match_clock is provided, then best_clock P divider must match the P
  623. * divider from @match_clock used for LVDS downclocking.
  624. */
  625. static bool
  626. pnv_find_best_dpll(const struct intel_limit *limit,
  627. struct intel_crtc_state *crtc_state,
  628. int target, int refclk, struct dpll *match_clock,
  629. struct dpll *best_clock)
  630. {
  631. struct drm_device *dev = crtc_state->base.crtc->dev;
  632. struct dpll clock;
  633. int err = target;
  634. memset(best_clock, 0, sizeof(*best_clock));
  635. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  636. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  637. clock.m1++) {
  638. for (clock.m2 = limit->m2.min;
  639. clock.m2 <= limit->m2.max; clock.m2++) {
  640. for (clock.n = limit->n.min;
  641. clock.n <= limit->n.max; clock.n++) {
  642. for (clock.p1 = limit->p1.min;
  643. clock.p1 <= limit->p1.max; clock.p1++) {
  644. int this_err;
  645. pnv_calc_dpll_params(refclk, &clock);
  646. if (!intel_PLL_is_valid(to_i915(dev),
  647. limit,
  648. &clock))
  649. continue;
  650. if (match_clock &&
  651. clock.p != match_clock->p)
  652. continue;
  653. this_err = abs(clock.dot - target);
  654. if (this_err < err) {
  655. *best_clock = clock;
  656. err = this_err;
  657. }
  658. }
  659. }
  660. }
  661. }
  662. return (err != target);
  663. }
  664. /*
  665. * Returns a set of divisors for the desired target clock with the given
  666. * refclk, or FALSE. The returned values represent the clock equation:
  667. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  668. *
  669. * Target and reference clocks are specified in kHz.
  670. *
  671. * If match_clock is provided, then best_clock P divider must match the P
  672. * divider from @match_clock used for LVDS downclocking.
  673. */
  674. static bool
  675. g4x_find_best_dpll(const struct intel_limit *limit,
  676. struct intel_crtc_state *crtc_state,
  677. int target, int refclk, struct dpll *match_clock,
  678. struct dpll *best_clock)
  679. {
  680. struct drm_device *dev = crtc_state->base.crtc->dev;
  681. struct dpll clock;
  682. int max_n;
  683. bool found = false;
  684. /* approximately equals target * 0.00585 */
  685. int err_most = (target >> 8) + (target >> 9);
  686. memset(best_clock, 0, sizeof(*best_clock));
  687. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  688. max_n = limit->n.max;
  689. /* based on hardware requirement, prefer smaller n to precision */
  690. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  691. /* based on hardware requirement, prefere larger m1,m2 */
  692. for (clock.m1 = limit->m1.max;
  693. clock.m1 >= limit->m1.min; clock.m1--) {
  694. for (clock.m2 = limit->m2.max;
  695. clock.m2 >= limit->m2.min; clock.m2--) {
  696. for (clock.p1 = limit->p1.max;
  697. clock.p1 >= limit->p1.min; clock.p1--) {
  698. int this_err;
  699. i9xx_calc_dpll_params(refclk, &clock);
  700. if (!intel_PLL_is_valid(to_i915(dev),
  701. limit,
  702. &clock))
  703. continue;
  704. this_err = abs(clock.dot - target);
  705. if (this_err < err_most) {
  706. *best_clock = clock;
  707. err_most = this_err;
  708. max_n = clock.n;
  709. found = true;
  710. }
  711. }
  712. }
  713. }
  714. }
  715. return found;
  716. }
  717. /*
  718. * Check if the calculated PLL configuration is more optimal compared to the
  719. * best configuration and error found so far. Return the calculated error.
  720. */
  721. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  722. const struct dpll *calculated_clock,
  723. const struct dpll *best_clock,
  724. unsigned int best_error_ppm,
  725. unsigned int *error_ppm)
  726. {
  727. /*
  728. * For CHV ignore the error and consider only the P value.
  729. * Prefer a bigger P value based on HW requirements.
  730. */
  731. if (IS_CHERRYVIEW(to_i915(dev))) {
  732. *error_ppm = 0;
  733. return calculated_clock->p > best_clock->p;
  734. }
  735. if (WARN_ON_ONCE(!target_freq))
  736. return false;
  737. *error_ppm = div_u64(1000000ULL *
  738. abs(target_freq - calculated_clock->dot),
  739. target_freq);
  740. /*
  741. * Prefer a better P value over a better (smaller) error if the error
  742. * is small. Ensure this preference for future configurations too by
  743. * setting the error to 0.
  744. */
  745. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  746. *error_ppm = 0;
  747. return true;
  748. }
  749. return *error_ppm + 10 < best_error_ppm;
  750. }
  751. /*
  752. * Returns a set of divisors for the desired target clock with the given
  753. * refclk, or FALSE. The returned values represent the clock equation:
  754. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  755. */
  756. static bool
  757. vlv_find_best_dpll(const struct intel_limit *limit,
  758. struct intel_crtc_state *crtc_state,
  759. int target, int refclk, struct dpll *match_clock,
  760. struct dpll *best_clock)
  761. {
  762. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  763. struct drm_device *dev = crtc->base.dev;
  764. struct dpll clock;
  765. unsigned int bestppm = 1000000;
  766. /* min update 19.2 MHz */
  767. int max_n = min(limit->n.max, refclk / 19200);
  768. bool found = false;
  769. target *= 5; /* fast clock */
  770. memset(best_clock, 0, sizeof(*best_clock));
  771. /* based on hardware requirement, prefer smaller n to precision */
  772. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  773. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  774. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  775. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  776. clock.p = clock.p1 * clock.p2;
  777. /* based on hardware requirement, prefer bigger m1,m2 values */
  778. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  779. unsigned int ppm;
  780. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  781. refclk * clock.m1);
  782. vlv_calc_dpll_params(refclk, &clock);
  783. if (!intel_PLL_is_valid(to_i915(dev),
  784. limit,
  785. &clock))
  786. continue;
  787. if (!vlv_PLL_is_optimal(dev, target,
  788. &clock,
  789. best_clock,
  790. bestppm, &ppm))
  791. continue;
  792. *best_clock = clock;
  793. bestppm = ppm;
  794. found = true;
  795. }
  796. }
  797. }
  798. }
  799. return found;
  800. }
  801. /*
  802. * Returns a set of divisors for the desired target clock with the given
  803. * refclk, or FALSE. The returned values represent the clock equation:
  804. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  805. */
  806. static bool
  807. chv_find_best_dpll(const struct intel_limit *limit,
  808. struct intel_crtc_state *crtc_state,
  809. int target, int refclk, struct dpll *match_clock,
  810. struct dpll *best_clock)
  811. {
  812. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  813. struct drm_device *dev = crtc->base.dev;
  814. unsigned int best_error_ppm;
  815. struct dpll clock;
  816. uint64_t m2;
  817. int found = false;
  818. memset(best_clock, 0, sizeof(*best_clock));
  819. best_error_ppm = 1000000;
  820. /*
  821. * Based on hardware doc, the n always set to 1, and m1 always
  822. * set to 2. If requires to support 200Mhz refclk, we need to
  823. * revisit this because n may not 1 anymore.
  824. */
  825. clock.n = 1, clock.m1 = 2;
  826. target *= 5; /* fast clock */
  827. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  828. for (clock.p2 = limit->p2.p2_fast;
  829. clock.p2 >= limit->p2.p2_slow;
  830. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  831. unsigned int error_ppm;
  832. clock.p = clock.p1 * clock.p2;
  833. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  834. clock.n) << 22, refclk * clock.m1);
  835. if (m2 > INT_MAX/clock.m1)
  836. continue;
  837. clock.m2 = m2;
  838. chv_calc_dpll_params(refclk, &clock);
  839. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  840. continue;
  841. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  842. best_error_ppm, &error_ppm))
  843. continue;
  844. *best_clock = clock;
  845. best_error_ppm = error_ppm;
  846. found = true;
  847. }
  848. }
  849. return found;
  850. }
  851. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  852. struct dpll *best_clock)
  853. {
  854. int refclk = 100000;
  855. const struct intel_limit *limit = &intel_limits_bxt;
  856. return chv_find_best_dpll(limit, crtc_state,
  857. target_clock, refclk, NULL, best_clock);
  858. }
  859. bool intel_crtc_active(struct intel_crtc *crtc)
  860. {
  861. /* Be paranoid as we can arrive here with only partial
  862. * state retrieved from the hardware during setup.
  863. *
  864. * We can ditch the adjusted_mode.crtc_clock check as soon
  865. * as Haswell has gained clock readout/fastboot support.
  866. *
  867. * We can ditch the crtc->primary->fb check as soon as we can
  868. * properly reconstruct framebuffers.
  869. *
  870. * FIXME: The intel_crtc->active here should be switched to
  871. * crtc->state->active once we have proper CRTC states wired up
  872. * for atomic.
  873. */
  874. return crtc->active && crtc->base.primary->state->fb &&
  875. crtc->config->base.adjusted_mode.crtc_clock;
  876. }
  877. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  878. enum pipe pipe)
  879. {
  880. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  881. return crtc->config->cpu_transcoder;
  882. }
  883. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  884. {
  885. i915_reg_t reg = PIPEDSL(pipe);
  886. u32 line1, line2;
  887. u32 line_mask;
  888. if (IS_GEN2(dev_priv))
  889. line_mask = DSL_LINEMASK_GEN2;
  890. else
  891. line_mask = DSL_LINEMASK_GEN3;
  892. line1 = I915_READ(reg) & line_mask;
  893. msleep(5);
  894. line2 = I915_READ(reg) & line_mask;
  895. return line1 == line2;
  896. }
  897. /*
  898. * intel_wait_for_pipe_off - wait for pipe to turn off
  899. * @crtc: crtc whose pipe to wait for
  900. *
  901. * After disabling a pipe, we can't wait for vblank in the usual way,
  902. * spinning on the vblank interrupt status bit, since we won't actually
  903. * see an interrupt when the pipe is disabled.
  904. *
  905. * On Gen4 and above:
  906. * wait for the pipe register state bit to turn off
  907. *
  908. * Otherwise:
  909. * wait for the display line value to settle (it usually
  910. * ends up stopping at the start of the next frame).
  911. *
  912. */
  913. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  914. {
  915. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  916. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  917. enum pipe pipe = crtc->pipe;
  918. if (INTEL_GEN(dev_priv) >= 4) {
  919. i915_reg_t reg = PIPECONF(cpu_transcoder);
  920. /* Wait for the Pipe State to go off */
  921. if (intel_wait_for_register(dev_priv,
  922. reg, I965_PIPECONF_ACTIVE, 0,
  923. 100))
  924. WARN(1, "pipe_off wait timed out\n");
  925. } else {
  926. /* Wait for the display line to settle */
  927. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  928. WARN(1, "pipe_off wait timed out\n");
  929. }
  930. }
  931. /* Only for pre-ILK configs */
  932. void assert_pll(struct drm_i915_private *dev_priv,
  933. enum pipe pipe, bool state)
  934. {
  935. u32 val;
  936. bool cur_state;
  937. val = I915_READ(DPLL(pipe));
  938. cur_state = !!(val & DPLL_VCO_ENABLE);
  939. I915_STATE_WARN(cur_state != state,
  940. "PLL state assertion failure (expected %s, current %s)\n",
  941. onoff(state), onoff(cur_state));
  942. }
  943. /* XXX: the dsi pll is shared between MIPI DSI ports */
  944. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  945. {
  946. u32 val;
  947. bool cur_state;
  948. mutex_lock(&dev_priv->sb_lock);
  949. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  950. mutex_unlock(&dev_priv->sb_lock);
  951. cur_state = val & DSI_PLL_VCO_EN;
  952. I915_STATE_WARN(cur_state != state,
  953. "DSI PLL state assertion failure (expected %s, current %s)\n",
  954. onoff(state), onoff(cur_state));
  955. }
  956. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  957. enum pipe pipe, bool state)
  958. {
  959. bool cur_state;
  960. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  961. pipe);
  962. if (HAS_DDI(dev_priv)) {
  963. /* DDI does not have a specific FDI_TX register */
  964. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  965. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  966. } else {
  967. u32 val = I915_READ(FDI_TX_CTL(pipe));
  968. cur_state = !!(val & FDI_TX_ENABLE);
  969. }
  970. I915_STATE_WARN(cur_state != state,
  971. "FDI TX state assertion failure (expected %s, current %s)\n",
  972. onoff(state), onoff(cur_state));
  973. }
  974. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  975. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  976. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  977. enum pipe pipe, bool state)
  978. {
  979. u32 val;
  980. bool cur_state;
  981. val = I915_READ(FDI_RX_CTL(pipe));
  982. cur_state = !!(val & FDI_RX_ENABLE);
  983. I915_STATE_WARN(cur_state != state,
  984. "FDI RX state assertion failure (expected %s, current %s)\n",
  985. onoff(state), onoff(cur_state));
  986. }
  987. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  988. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  989. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (IS_GEN5(dev_priv))
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv))
  998. return;
  999. val = I915_READ(FDI_TX_CTL(pipe));
  1000. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1001. }
  1002. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe, bool state)
  1004. {
  1005. u32 val;
  1006. bool cur_state;
  1007. val = I915_READ(FDI_RX_CTL(pipe));
  1008. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1009. I915_STATE_WARN(cur_state != state,
  1010. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1011. onoff(state), onoff(cur_state));
  1012. }
  1013. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1014. {
  1015. i915_reg_t pp_reg;
  1016. u32 val;
  1017. enum pipe panel_pipe = PIPE_A;
  1018. bool locked = true;
  1019. if (WARN_ON(HAS_DDI(dev_priv)))
  1020. return;
  1021. if (HAS_PCH_SPLIT(dev_priv)) {
  1022. u32 port_sel;
  1023. pp_reg = PP_CONTROL(0);
  1024. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1025. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1026. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1027. panel_pipe = PIPE_B;
  1028. /* XXX: else fix for eDP */
  1029. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1030. /* presumably write lock depends on pipe, not port select */
  1031. pp_reg = PP_CONTROL(pipe);
  1032. panel_pipe = pipe;
  1033. } else {
  1034. pp_reg = PP_CONTROL(0);
  1035. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1036. panel_pipe = PIPE_B;
  1037. }
  1038. val = I915_READ(pp_reg);
  1039. if (!(val & PANEL_POWER_ON) ||
  1040. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1041. locked = false;
  1042. I915_STATE_WARN(panel_pipe == pipe && locked,
  1043. "panel assertion failure, pipe %c regs locked\n",
  1044. pipe_name(pipe));
  1045. }
  1046. static void assert_cursor(struct drm_i915_private *dev_priv,
  1047. enum pipe pipe, bool state)
  1048. {
  1049. bool cur_state;
  1050. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1051. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1052. else
  1053. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1054. I915_STATE_WARN(cur_state != state,
  1055. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1056. pipe_name(pipe), onoff(state), onoff(cur_state));
  1057. }
  1058. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1059. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1060. void assert_pipe(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe, bool state)
  1062. {
  1063. bool cur_state;
  1064. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1065. pipe);
  1066. enum intel_display_power_domain power_domain;
  1067. /* we keep both pipes enabled on 830 */
  1068. if (IS_I830(dev_priv))
  1069. state = true;
  1070. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1071. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1072. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1073. cur_state = !!(val & PIPECONF_ENABLE);
  1074. intel_display_power_put(dev_priv, power_domain);
  1075. } else {
  1076. cur_state = false;
  1077. }
  1078. I915_STATE_WARN(cur_state != state,
  1079. "pipe %c assertion failure (expected %s, current %s)\n",
  1080. pipe_name(pipe), onoff(state), onoff(cur_state));
  1081. }
  1082. static void assert_plane(struct drm_i915_private *dev_priv,
  1083. enum plane plane, bool state)
  1084. {
  1085. u32 val;
  1086. bool cur_state;
  1087. val = I915_READ(DSPCNTR(plane));
  1088. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1089. I915_STATE_WARN(cur_state != state,
  1090. "plane %c assertion failure (expected %s, current %s)\n",
  1091. plane_name(plane), onoff(state), onoff(cur_state));
  1092. }
  1093. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1094. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1095. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1096. enum pipe pipe)
  1097. {
  1098. int i;
  1099. /* Primary planes are fixed to pipes on gen4+ */
  1100. if (INTEL_GEN(dev_priv) >= 4) {
  1101. u32 val = I915_READ(DSPCNTR(pipe));
  1102. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1103. "plane %c assertion failure, should be disabled but not\n",
  1104. plane_name(pipe));
  1105. return;
  1106. }
  1107. /* Need to check both planes against the pipe */
  1108. for_each_pipe(dev_priv, i) {
  1109. u32 val = I915_READ(DSPCNTR(i));
  1110. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1111. DISPPLANE_SEL_PIPE_SHIFT;
  1112. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1113. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1114. plane_name(i), pipe_name(pipe));
  1115. }
  1116. }
  1117. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe)
  1119. {
  1120. int sprite;
  1121. if (INTEL_GEN(dev_priv) >= 9) {
  1122. for_each_sprite(dev_priv, pipe, sprite) {
  1123. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1124. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1125. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1126. sprite, pipe_name(pipe));
  1127. }
  1128. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1129. for_each_sprite(dev_priv, pipe, sprite) {
  1130. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1131. I915_STATE_WARN(val & SP_ENABLE,
  1132. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1133. sprite_name(pipe, sprite), pipe_name(pipe));
  1134. }
  1135. } else if (INTEL_GEN(dev_priv) >= 7) {
  1136. u32 val = I915_READ(SPRCTL(pipe));
  1137. I915_STATE_WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  1141. u32 val = I915_READ(DVSCNTR(pipe));
  1142. I915_STATE_WARN(val & DVS_ENABLE,
  1143. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1144. plane_name(pipe), pipe_name(pipe));
  1145. }
  1146. }
  1147. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1148. {
  1149. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1150. drm_crtc_vblank_put(crtc);
  1151. }
  1152. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. u32 val;
  1156. bool enabled;
  1157. val = I915_READ(PCH_TRANSCONF(pipe));
  1158. enabled = !!(val & TRANS_ENABLE);
  1159. I915_STATE_WARN(enabled,
  1160. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1161. pipe_name(pipe));
  1162. }
  1163. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1164. enum pipe pipe, u32 port_sel, u32 val)
  1165. {
  1166. if ((val & DP_PORT_EN) == 0)
  1167. return false;
  1168. if (HAS_PCH_CPT(dev_priv)) {
  1169. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1170. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1171. return false;
  1172. } else if (IS_CHERRYVIEW(dev_priv)) {
  1173. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1174. return false;
  1175. } else {
  1176. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1177. return false;
  1178. }
  1179. return true;
  1180. }
  1181. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1182. enum pipe pipe, u32 val)
  1183. {
  1184. if ((val & SDVO_ENABLE) == 0)
  1185. return false;
  1186. if (HAS_PCH_CPT(dev_priv)) {
  1187. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1188. return false;
  1189. } else if (IS_CHERRYVIEW(dev_priv)) {
  1190. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1191. return false;
  1192. } else {
  1193. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1194. return false;
  1195. }
  1196. return true;
  1197. }
  1198. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe, u32 val)
  1200. {
  1201. if ((val & LVDS_PORT_EN) == 0)
  1202. return false;
  1203. if (HAS_PCH_CPT(dev_priv)) {
  1204. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1205. return false;
  1206. } else {
  1207. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1208. return false;
  1209. }
  1210. return true;
  1211. }
  1212. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1213. enum pipe pipe, u32 val)
  1214. {
  1215. if ((val & ADPA_DAC_ENABLE) == 0)
  1216. return false;
  1217. if (HAS_PCH_CPT(dev_priv)) {
  1218. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1219. return false;
  1220. } else {
  1221. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1222. return false;
  1223. }
  1224. return true;
  1225. }
  1226. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1227. enum pipe pipe, i915_reg_t reg,
  1228. u32 port_sel)
  1229. {
  1230. u32 val = I915_READ(reg);
  1231. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1232. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1233. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1234. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1235. && (val & DP_PIPEB_SELECT),
  1236. "IBX PCH dp port still using transcoder B\n");
  1237. }
  1238. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1239. enum pipe pipe, i915_reg_t reg)
  1240. {
  1241. u32 val = I915_READ(reg);
  1242. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1243. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1244. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1245. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1246. && (val & SDVO_PIPE_B_SELECT),
  1247. "IBX PCH hdmi port still using transcoder B\n");
  1248. }
  1249. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe)
  1251. {
  1252. u32 val;
  1253. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1254. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1255. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1256. val = I915_READ(PCH_ADPA);
  1257. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1258. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1259. pipe_name(pipe));
  1260. val = I915_READ(PCH_LVDS);
  1261. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1262. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1263. pipe_name(pipe));
  1264. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1265. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1266. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1267. }
  1268. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1269. const struct intel_crtc_state *pipe_config)
  1270. {
  1271. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1272. enum pipe pipe = crtc->pipe;
  1273. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1274. POSTING_READ(DPLL(pipe));
  1275. udelay(150);
  1276. if (intel_wait_for_register(dev_priv,
  1277. DPLL(pipe),
  1278. DPLL_LOCK_VLV,
  1279. DPLL_LOCK_VLV,
  1280. 1))
  1281. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1282. }
  1283. static void vlv_enable_pll(struct intel_crtc *crtc,
  1284. const struct intel_crtc_state *pipe_config)
  1285. {
  1286. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1287. enum pipe pipe = crtc->pipe;
  1288. assert_pipe_disabled(dev_priv, pipe);
  1289. /* PLL is protected by panel, make sure we can write it */
  1290. assert_panel_unlocked(dev_priv, pipe);
  1291. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1292. _vlv_enable_pll(crtc, pipe_config);
  1293. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1294. POSTING_READ(DPLL_MD(pipe));
  1295. }
  1296. static void _chv_enable_pll(struct intel_crtc *crtc,
  1297. const struct intel_crtc_state *pipe_config)
  1298. {
  1299. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1300. enum pipe pipe = crtc->pipe;
  1301. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1302. u32 tmp;
  1303. mutex_lock(&dev_priv->sb_lock);
  1304. /* Enable back the 10bit clock to display controller */
  1305. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1306. tmp |= DPIO_DCLKP_EN;
  1307. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1308. mutex_unlock(&dev_priv->sb_lock);
  1309. /*
  1310. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1311. */
  1312. udelay(1);
  1313. /* Enable PLL */
  1314. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1315. /* Check PLL is locked */
  1316. if (intel_wait_for_register(dev_priv,
  1317. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1318. 1))
  1319. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1320. }
  1321. static void chv_enable_pll(struct intel_crtc *crtc,
  1322. const struct intel_crtc_state *pipe_config)
  1323. {
  1324. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1325. enum pipe pipe = crtc->pipe;
  1326. assert_pipe_disabled(dev_priv, pipe);
  1327. /* PLL is protected by panel, make sure we can write it */
  1328. assert_panel_unlocked(dev_priv, pipe);
  1329. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1330. _chv_enable_pll(crtc, pipe_config);
  1331. if (pipe != PIPE_A) {
  1332. /*
  1333. * WaPixelRepeatModeFixForC0:chv
  1334. *
  1335. * DPLLCMD is AWOL. Use chicken bits to propagate
  1336. * the value from DPLLBMD to either pipe B or C.
  1337. */
  1338. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1339. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1340. I915_WRITE(CBR4_VLV, 0);
  1341. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1342. /*
  1343. * DPLLB VGA mode also seems to cause problems.
  1344. * We should always have it disabled.
  1345. */
  1346. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1347. } else {
  1348. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1349. POSTING_READ(DPLL_MD(pipe));
  1350. }
  1351. }
  1352. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1353. {
  1354. struct intel_crtc *crtc;
  1355. int count = 0;
  1356. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1357. count += crtc->base.state->active &&
  1358. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1359. }
  1360. return count;
  1361. }
  1362. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1363. {
  1364. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1365. i915_reg_t reg = DPLL(crtc->pipe);
  1366. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1367. int i;
  1368. assert_pipe_disabled(dev_priv, crtc->pipe);
  1369. /* PLL is protected by panel, make sure we can write it */
  1370. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1371. assert_panel_unlocked(dev_priv, crtc->pipe);
  1372. /* Enable DVO 2x clock on both PLLs if necessary */
  1373. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1374. /*
  1375. * It appears to be important that we don't enable this
  1376. * for the current pipe before otherwise configuring the
  1377. * PLL. No idea how this should be handled if multiple
  1378. * DVO outputs are enabled simultaneosly.
  1379. */
  1380. dpll |= DPLL_DVO_2X_MODE;
  1381. I915_WRITE(DPLL(!crtc->pipe),
  1382. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1383. }
  1384. /*
  1385. * Apparently we need to have VGA mode enabled prior to changing
  1386. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1387. * dividers, even though the register value does change.
  1388. */
  1389. I915_WRITE(reg, 0);
  1390. I915_WRITE(reg, dpll);
  1391. /* Wait for the clocks to stabilize. */
  1392. POSTING_READ(reg);
  1393. udelay(150);
  1394. if (INTEL_GEN(dev_priv) >= 4) {
  1395. I915_WRITE(DPLL_MD(crtc->pipe),
  1396. crtc->config->dpll_hw_state.dpll_md);
  1397. } else {
  1398. /* The pixel multiplier can only be updated once the
  1399. * DPLL is enabled and the clocks are stable.
  1400. *
  1401. * So write it again.
  1402. */
  1403. I915_WRITE(reg, dpll);
  1404. }
  1405. /* We do this three times for luck */
  1406. for (i = 0; i < 3; i++) {
  1407. I915_WRITE(reg, dpll);
  1408. POSTING_READ(reg);
  1409. udelay(150); /* wait for warmup */
  1410. }
  1411. }
  1412. /**
  1413. * i9xx_disable_pll - disable a PLL
  1414. * @dev_priv: i915 private structure
  1415. * @pipe: pipe PLL to disable
  1416. *
  1417. * Disable the PLL for @pipe, making sure the pipe is off first.
  1418. *
  1419. * Note! This is for pre-ILK only.
  1420. */
  1421. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1422. {
  1423. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1424. enum pipe pipe = crtc->pipe;
  1425. /* Disable DVO 2x clock on both PLLs if necessary */
  1426. if (IS_I830(dev_priv) &&
  1427. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1428. !intel_num_dvo_pipes(dev_priv)) {
  1429. I915_WRITE(DPLL(PIPE_B),
  1430. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1431. I915_WRITE(DPLL(PIPE_A),
  1432. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1433. }
  1434. /* Don't disable pipe or pipe PLLs if needed */
  1435. if (IS_I830(dev_priv))
  1436. return;
  1437. /* Make sure the pipe isn't still relying on us */
  1438. assert_pipe_disabled(dev_priv, pipe);
  1439. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1440. POSTING_READ(DPLL(pipe));
  1441. }
  1442. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1443. {
  1444. u32 val;
  1445. /* Make sure the pipe isn't still relying on us */
  1446. assert_pipe_disabled(dev_priv, pipe);
  1447. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1448. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1449. if (pipe != PIPE_A)
  1450. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1451. I915_WRITE(DPLL(pipe), val);
  1452. POSTING_READ(DPLL(pipe));
  1453. }
  1454. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1455. {
  1456. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1457. u32 val;
  1458. /* Make sure the pipe isn't still relying on us */
  1459. assert_pipe_disabled(dev_priv, pipe);
  1460. val = DPLL_SSC_REF_CLK_CHV |
  1461. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1462. if (pipe != PIPE_A)
  1463. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1464. I915_WRITE(DPLL(pipe), val);
  1465. POSTING_READ(DPLL(pipe));
  1466. mutex_lock(&dev_priv->sb_lock);
  1467. /* Disable 10bit clock to display controller */
  1468. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1469. val &= ~DPIO_DCLKP_EN;
  1470. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1471. mutex_unlock(&dev_priv->sb_lock);
  1472. }
  1473. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1474. struct intel_digital_port *dport,
  1475. unsigned int expected_mask)
  1476. {
  1477. u32 port_mask;
  1478. i915_reg_t dpll_reg;
  1479. switch (dport->port) {
  1480. case PORT_B:
  1481. port_mask = DPLL_PORTB_READY_MASK;
  1482. dpll_reg = DPLL(0);
  1483. break;
  1484. case PORT_C:
  1485. port_mask = DPLL_PORTC_READY_MASK;
  1486. dpll_reg = DPLL(0);
  1487. expected_mask <<= 4;
  1488. break;
  1489. case PORT_D:
  1490. port_mask = DPLL_PORTD_READY_MASK;
  1491. dpll_reg = DPIO_PHY_STATUS;
  1492. break;
  1493. default:
  1494. BUG();
  1495. }
  1496. if (intel_wait_for_register(dev_priv,
  1497. dpll_reg, port_mask, expected_mask,
  1498. 1000))
  1499. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1500. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1501. }
  1502. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1503. enum pipe pipe)
  1504. {
  1505. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1506. pipe);
  1507. i915_reg_t reg;
  1508. uint32_t val, pipeconf_val;
  1509. /* Make sure PCH DPLL is enabled */
  1510. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1511. /* FDI must be feeding us bits for PCH ports */
  1512. assert_fdi_tx_enabled(dev_priv, pipe);
  1513. assert_fdi_rx_enabled(dev_priv, pipe);
  1514. if (HAS_PCH_CPT(dev_priv)) {
  1515. /* Workaround: Set the timing override bit before enabling the
  1516. * pch transcoder. */
  1517. reg = TRANS_CHICKEN2(pipe);
  1518. val = I915_READ(reg);
  1519. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1520. I915_WRITE(reg, val);
  1521. }
  1522. reg = PCH_TRANSCONF(pipe);
  1523. val = I915_READ(reg);
  1524. pipeconf_val = I915_READ(PIPECONF(pipe));
  1525. if (HAS_PCH_IBX(dev_priv)) {
  1526. /*
  1527. * Make the BPC in transcoder be consistent with
  1528. * that in pipeconf reg. For HDMI we must use 8bpc
  1529. * here for both 8bpc and 12bpc.
  1530. */
  1531. val &= ~PIPECONF_BPC_MASK;
  1532. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1533. val |= PIPECONF_8BPC;
  1534. else
  1535. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1536. }
  1537. val &= ~TRANS_INTERLACE_MASK;
  1538. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1539. if (HAS_PCH_IBX(dev_priv) &&
  1540. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1541. val |= TRANS_LEGACY_INTERLACED_ILK;
  1542. else
  1543. val |= TRANS_INTERLACED;
  1544. else
  1545. val |= TRANS_PROGRESSIVE;
  1546. I915_WRITE(reg, val | TRANS_ENABLE);
  1547. if (intel_wait_for_register(dev_priv,
  1548. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1549. 100))
  1550. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1551. }
  1552. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1553. enum transcoder cpu_transcoder)
  1554. {
  1555. u32 val, pipeconf_val;
  1556. /* FDI must be feeding us bits for PCH ports */
  1557. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1558. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1559. /* Workaround: set timing override bit. */
  1560. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1561. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1562. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1563. val = TRANS_ENABLE;
  1564. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1565. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1566. PIPECONF_INTERLACED_ILK)
  1567. val |= TRANS_INTERLACED;
  1568. else
  1569. val |= TRANS_PROGRESSIVE;
  1570. I915_WRITE(LPT_TRANSCONF, val);
  1571. if (intel_wait_for_register(dev_priv,
  1572. LPT_TRANSCONF,
  1573. TRANS_STATE_ENABLE,
  1574. TRANS_STATE_ENABLE,
  1575. 100))
  1576. DRM_ERROR("Failed to enable PCH transcoder\n");
  1577. }
  1578. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1579. enum pipe pipe)
  1580. {
  1581. i915_reg_t reg;
  1582. uint32_t val;
  1583. /* FDI relies on the transcoder */
  1584. assert_fdi_tx_disabled(dev_priv, pipe);
  1585. assert_fdi_rx_disabled(dev_priv, pipe);
  1586. /* Ports must be off as well */
  1587. assert_pch_ports_disabled(dev_priv, pipe);
  1588. reg = PCH_TRANSCONF(pipe);
  1589. val = I915_READ(reg);
  1590. val &= ~TRANS_ENABLE;
  1591. I915_WRITE(reg, val);
  1592. /* wait for PCH transcoder off, transcoder state */
  1593. if (intel_wait_for_register(dev_priv,
  1594. reg, TRANS_STATE_ENABLE, 0,
  1595. 50))
  1596. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1597. if (HAS_PCH_CPT(dev_priv)) {
  1598. /* Workaround: Clear the timing override chicken bit again. */
  1599. reg = TRANS_CHICKEN2(pipe);
  1600. val = I915_READ(reg);
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(reg, val);
  1603. }
  1604. }
  1605. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1606. {
  1607. u32 val;
  1608. val = I915_READ(LPT_TRANSCONF);
  1609. val &= ~TRANS_ENABLE;
  1610. I915_WRITE(LPT_TRANSCONF, val);
  1611. /* wait for PCH transcoder off, transcoder state */
  1612. if (intel_wait_for_register(dev_priv,
  1613. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1614. 50))
  1615. DRM_ERROR("Failed to disable PCH transcoder\n");
  1616. /* Workaround: clear timing override bit. */
  1617. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1618. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1619. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1620. }
  1621. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1622. {
  1623. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1624. WARN_ON(!crtc->config->has_pch_encoder);
  1625. if (HAS_PCH_LPT(dev_priv))
  1626. return PIPE_A;
  1627. else
  1628. return crtc->pipe;
  1629. }
  1630. /**
  1631. * intel_enable_pipe - enable a pipe, asserting requirements
  1632. * @crtc: crtc responsible for the pipe
  1633. *
  1634. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1635. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1636. */
  1637. static void intel_enable_pipe(struct intel_crtc *crtc)
  1638. {
  1639. struct drm_device *dev = crtc->base.dev;
  1640. struct drm_i915_private *dev_priv = to_i915(dev);
  1641. enum pipe pipe = crtc->pipe;
  1642. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1643. i915_reg_t reg;
  1644. u32 val;
  1645. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1646. assert_planes_disabled(dev_priv, pipe);
  1647. assert_cursor_disabled(dev_priv, pipe);
  1648. assert_sprites_disabled(dev_priv, pipe);
  1649. /*
  1650. * A pipe without a PLL won't actually be able to drive bits from
  1651. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1652. * need the check.
  1653. */
  1654. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1655. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1656. assert_dsi_pll_enabled(dev_priv);
  1657. else
  1658. assert_pll_enabled(dev_priv, pipe);
  1659. } else {
  1660. if (crtc->config->has_pch_encoder) {
  1661. /* if driving the PCH, we need FDI enabled */
  1662. assert_fdi_rx_pll_enabled(dev_priv,
  1663. intel_crtc_pch_transcoder(crtc));
  1664. assert_fdi_tx_pll_enabled(dev_priv,
  1665. (enum pipe) cpu_transcoder);
  1666. }
  1667. /* FIXME: assert CPU port conditions for SNB+ */
  1668. }
  1669. reg = PIPECONF(cpu_transcoder);
  1670. val = I915_READ(reg);
  1671. if (val & PIPECONF_ENABLE) {
  1672. /* we keep both pipes enabled on 830 */
  1673. WARN_ON(!IS_I830(dev_priv));
  1674. return;
  1675. }
  1676. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1677. POSTING_READ(reg);
  1678. /*
  1679. * Until the pipe starts DSL will read as 0, which would cause
  1680. * an apparent vblank timestamp jump, which messes up also the
  1681. * frame count when it's derived from the timestamps. So let's
  1682. * wait for the pipe to start properly before we call
  1683. * drm_crtc_vblank_on()
  1684. */
  1685. if (dev->max_vblank_count == 0 &&
  1686. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1687. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1688. }
  1689. /**
  1690. * intel_disable_pipe - disable a pipe, asserting requirements
  1691. * @crtc: crtc whose pipes is to be disabled
  1692. *
  1693. * Disable the pipe of @crtc, making sure that various hardware
  1694. * specific requirements are met, if applicable, e.g. plane
  1695. * disabled, panel fitter off, etc.
  1696. *
  1697. * Will wait until the pipe has shut down before returning.
  1698. */
  1699. static void intel_disable_pipe(struct intel_crtc *crtc)
  1700. {
  1701. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1702. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1703. enum pipe pipe = crtc->pipe;
  1704. i915_reg_t reg;
  1705. u32 val;
  1706. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1707. /*
  1708. * Make sure planes won't keep trying to pump pixels to us,
  1709. * or we might hang the display.
  1710. */
  1711. assert_planes_disabled(dev_priv, pipe);
  1712. assert_cursor_disabled(dev_priv, pipe);
  1713. assert_sprites_disabled(dev_priv, pipe);
  1714. reg = PIPECONF(cpu_transcoder);
  1715. val = I915_READ(reg);
  1716. if ((val & PIPECONF_ENABLE) == 0)
  1717. return;
  1718. /*
  1719. * Double wide has implications for planes
  1720. * so best keep it disabled when not needed.
  1721. */
  1722. if (crtc->config->double_wide)
  1723. val &= ~PIPECONF_DOUBLE_WIDE;
  1724. /* Don't disable pipe or pipe PLLs if needed */
  1725. if (!IS_I830(dev_priv))
  1726. val &= ~PIPECONF_ENABLE;
  1727. I915_WRITE(reg, val);
  1728. if ((val & PIPECONF_ENABLE) == 0)
  1729. intel_wait_for_pipe_off(crtc);
  1730. }
  1731. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1732. {
  1733. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1734. }
  1735. static unsigned int
  1736. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1739. unsigned int cpp = fb->format->cpp[plane];
  1740. switch (fb->modifier) {
  1741. case DRM_FORMAT_MOD_LINEAR:
  1742. return cpp;
  1743. case I915_FORMAT_MOD_X_TILED:
  1744. if (IS_GEN2(dev_priv))
  1745. return 128;
  1746. else
  1747. return 512;
  1748. case I915_FORMAT_MOD_Y_TILED_CCS:
  1749. if (plane == 1)
  1750. return 128;
  1751. /* fall through */
  1752. case I915_FORMAT_MOD_Y_TILED:
  1753. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1754. return 128;
  1755. else
  1756. return 512;
  1757. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1758. if (plane == 1)
  1759. return 128;
  1760. /* fall through */
  1761. case I915_FORMAT_MOD_Yf_TILED:
  1762. switch (cpp) {
  1763. case 1:
  1764. return 64;
  1765. case 2:
  1766. case 4:
  1767. return 128;
  1768. case 8:
  1769. case 16:
  1770. return 256;
  1771. default:
  1772. MISSING_CASE(cpp);
  1773. return cpp;
  1774. }
  1775. break;
  1776. default:
  1777. MISSING_CASE(fb->modifier);
  1778. return cpp;
  1779. }
  1780. }
  1781. static unsigned int
  1782. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1783. {
  1784. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1785. return 1;
  1786. else
  1787. return intel_tile_size(to_i915(fb->dev)) /
  1788. intel_tile_width_bytes(fb, plane);
  1789. }
  1790. /* Return the tile dimensions in pixel units */
  1791. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1792. unsigned int *tile_width,
  1793. unsigned int *tile_height)
  1794. {
  1795. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1796. unsigned int cpp = fb->format->cpp[plane];
  1797. *tile_width = tile_width_bytes / cpp;
  1798. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1799. }
  1800. unsigned int
  1801. intel_fb_align_height(const struct drm_framebuffer *fb,
  1802. int plane, unsigned int height)
  1803. {
  1804. unsigned int tile_height = intel_tile_height(fb, plane);
  1805. return ALIGN(height, tile_height);
  1806. }
  1807. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1808. {
  1809. unsigned int size = 0;
  1810. int i;
  1811. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1812. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1813. return size;
  1814. }
  1815. static void
  1816. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1817. const struct drm_framebuffer *fb,
  1818. unsigned int rotation)
  1819. {
  1820. view->type = I915_GGTT_VIEW_NORMAL;
  1821. if (drm_rotation_90_or_270(rotation)) {
  1822. view->type = I915_GGTT_VIEW_ROTATED;
  1823. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1824. }
  1825. }
  1826. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1827. {
  1828. if (IS_I830(dev_priv))
  1829. return 16 * 1024;
  1830. else if (IS_I85X(dev_priv))
  1831. return 256;
  1832. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1833. return 32;
  1834. else
  1835. return 4 * 1024;
  1836. }
  1837. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1838. {
  1839. if (INTEL_INFO(dev_priv)->gen >= 9)
  1840. return 256 * 1024;
  1841. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1842. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1843. return 128 * 1024;
  1844. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1845. return 4 * 1024;
  1846. else
  1847. return 0;
  1848. }
  1849. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1850. int plane)
  1851. {
  1852. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1853. /* AUX_DIST needs only 4K alignment */
  1854. if (plane == 1)
  1855. return 4096;
  1856. switch (fb->modifier) {
  1857. case DRM_FORMAT_MOD_LINEAR:
  1858. return intel_linear_alignment(dev_priv);
  1859. case I915_FORMAT_MOD_X_TILED:
  1860. if (INTEL_GEN(dev_priv) >= 9)
  1861. return 256 * 1024;
  1862. return 0;
  1863. case I915_FORMAT_MOD_Y_TILED_CCS:
  1864. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1865. case I915_FORMAT_MOD_Y_TILED:
  1866. case I915_FORMAT_MOD_Yf_TILED:
  1867. return 1 * 1024 * 1024;
  1868. default:
  1869. MISSING_CASE(fb->modifier);
  1870. return 0;
  1871. }
  1872. }
  1873. struct i915_vma *
  1874. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1875. {
  1876. struct drm_device *dev = fb->dev;
  1877. struct drm_i915_private *dev_priv = to_i915(dev);
  1878. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1879. struct i915_ggtt_view view;
  1880. struct i915_vma *vma;
  1881. u32 alignment;
  1882. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1883. alignment = intel_surf_alignment(fb, 0);
  1884. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1885. /* Note that the w/a also requires 64 PTE of padding following the
  1886. * bo. We currently fill all unused PTE with the shadow page and so
  1887. * we should always have valid PTE following the scanout preventing
  1888. * the VT-d warning.
  1889. */
  1890. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1891. alignment = 256 * 1024;
  1892. /*
  1893. * Global gtt pte registers are special registers which actually forward
  1894. * writes to a chunk of system memory. Which means that there is no risk
  1895. * that the register values disappear as soon as we call
  1896. * intel_runtime_pm_put(), so it is correct to wrap only the
  1897. * pin/unpin/fence and not more.
  1898. */
  1899. intel_runtime_pm_get(dev_priv);
  1900. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1901. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1902. if (IS_ERR(vma))
  1903. goto err;
  1904. if (i915_vma_is_map_and_fenceable(vma)) {
  1905. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1906. * fence, whereas 965+ only requires a fence if using
  1907. * framebuffer compression. For simplicity, we always, when
  1908. * possible, install a fence as the cost is not that onerous.
  1909. *
  1910. * If we fail to fence the tiled scanout, then either the
  1911. * modeset will reject the change (which is highly unlikely as
  1912. * the affected systems, all but one, do not have unmappable
  1913. * space) or we will not be able to enable full powersaving
  1914. * techniques (also likely not to apply due to various limits
  1915. * FBC and the like impose on the size of the buffer, which
  1916. * presumably we violated anyway with this unmappable buffer).
  1917. * Anyway, it is presumably better to stumble onwards with
  1918. * something and try to run the system in a "less than optimal"
  1919. * mode that matches the user configuration.
  1920. */
  1921. if (i915_vma_get_fence(vma) == 0)
  1922. i915_vma_pin_fence(vma);
  1923. }
  1924. i915_vma_get(vma);
  1925. err:
  1926. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1927. intel_runtime_pm_put(dev_priv);
  1928. return vma;
  1929. }
  1930. void intel_unpin_fb_vma(struct i915_vma *vma)
  1931. {
  1932. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1933. i915_vma_unpin_fence(vma);
  1934. i915_gem_object_unpin_from_display_plane(vma);
  1935. i915_vma_put(vma);
  1936. }
  1937. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1938. unsigned int rotation)
  1939. {
  1940. if (drm_rotation_90_or_270(rotation))
  1941. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1942. else
  1943. return fb->pitches[plane];
  1944. }
  1945. /*
  1946. * Convert the x/y offsets into a linear offset.
  1947. * Only valid with 0/180 degree rotation, which is fine since linear
  1948. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1949. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1950. */
  1951. u32 intel_fb_xy_to_linear(int x, int y,
  1952. const struct intel_plane_state *state,
  1953. int plane)
  1954. {
  1955. const struct drm_framebuffer *fb = state->base.fb;
  1956. unsigned int cpp = fb->format->cpp[plane];
  1957. unsigned int pitch = fb->pitches[plane];
  1958. return y * pitch + x * cpp;
  1959. }
  1960. /*
  1961. * Add the x/y offsets derived from fb->offsets[] to the user
  1962. * specified plane src x/y offsets. The resulting x/y offsets
  1963. * specify the start of scanout from the beginning of the gtt mapping.
  1964. */
  1965. void intel_add_fb_offsets(int *x, int *y,
  1966. const struct intel_plane_state *state,
  1967. int plane)
  1968. {
  1969. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1970. unsigned int rotation = state->base.rotation;
  1971. if (drm_rotation_90_or_270(rotation)) {
  1972. *x += intel_fb->rotated[plane].x;
  1973. *y += intel_fb->rotated[plane].y;
  1974. } else {
  1975. *x += intel_fb->normal[plane].x;
  1976. *y += intel_fb->normal[plane].y;
  1977. }
  1978. }
  1979. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1980. unsigned int tile_width,
  1981. unsigned int tile_height,
  1982. unsigned int tile_size,
  1983. unsigned int pitch_tiles,
  1984. u32 old_offset,
  1985. u32 new_offset)
  1986. {
  1987. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1988. unsigned int tiles;
  1989. WARN_ON(old_offset & (tile_size - 1));
  1990. WARN_ON(new_offset & (tile_size - 1));
  1991. WARN_ON(new_offset > old_offset);
  1992. tiles = (old_offset - new_offset) / tile_size;
  1993. *y += tiles / pitch_tiles * tile_height;
  1994. *x += tiles % pitch_tiles * tile_width;
  1995. /* minimize x in case it got needlessly big */
  1996. *y += *x / pitch_pixels * tile_height;
  1997. *x %= pitch_pixels;
  1998. return new_offset;
  1999. }
  2000. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2001. const struct drm_framebuffer *fb, int plane,
  2002. unsigned int rotation,
  2003. u32 old_offset, u32 new_offset)
  2004. {
  2005. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2006. unsigned int cpp = fb->format->cpp[plane];
  2007. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2008. WARN_ON(new_offset > old_offset);
  2009. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2010. unsigned int tile_size, tile_width, tile_height;
  2011. unsigned int pitch_tiles;
  2012. tile_size = intel_tile_size(dev_priv);
  2013. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2014. if (drm_rotation_90_or_270(rotation)) {
  2015. pitch_tiles = pitch / tile_height;
  2016. swap(tile_width, tile_height);
  2017. } else {
  2018. pitch_tiles = pitch / (tile_width * cpp);
  2019. }
  2020. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2021. tile_size, pitch_tiles,
  2022. old_offset, new_offset);
  2023. } else {
  2024. old_offset += *y * pitch + *x * cpp;
  2025. *y = (old_offset - new_offset) / pitch;
  2026. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2027. }
  2028. return new_offset;
  2029. }
  2030. /*
  2031. * Adjust the tile offset by moving the difference into
  2032. * the x/y offsets.
  2033. */
  2034. static u32 intel_adjust_tile_offset(int *x, int *y,
  2035. const struct intel_plane_state *state, int plane,
  2036. u32 old_offset, u32 new_offset)
  2037. {
  2038. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  2039. state->base.rotation,
  2040. old_offset, new_offset);
  2041. }
  2042. /*
  2043. * Computes the linear offset to the base tile and adjusts
  2044. * x, y. bytes per pixel is assumed to be a power-of-two.
  2045. *
  2046. * In the 90/270 rotated case, x and y are assumed
  2047. * to be already rotated to match the rotated GTT view, and
  2048. * pitch is the tile_height aligned framebuffer height.
  2049. *
  2050. * This function is used when computing the derived information
  2051. * under intel_framebuffer, so using any of that information
  2052. * here is not allowed. Anything under drm_framebuffer can be
  2053. * used. This is why the user has to pass in the pitch since it
  2054. * is specified in the rotated orientation.
  2055. */
  2056. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2057. int *x, int *y,
  2058. const struct drm_framebuffer *fb, int plane,
  2059. unsigned int pitch,
  2060. unsigned int rotation,
  2061. u32 alignment)
  2062. {
  2063. uint64_t fb_modifier = fb->modifier;
  2064. unsigned int cpp = fb->format->cpp[plane];
  2065. u32 offset, offset_aligned;
  2066. if (alignment)
  2067. alignment--;
  2068. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2069. unsigned int tile_size, tile_width, tile_height;
  2070. unsigned int tile_rows, tiles, pitch_tiles;
  2071. tile_size = intel_tile_size(dev_priv);
  2072. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2073. if (drm_rotation_90_or_270(rotation)) {
  2074. pitch_tiles = pitch / tile_height;
  2075. swap(tile_width, tile_height);
  2076. } else {
  2077. pitch_tiles = pitch / (tile_width * cpp);
  2078. }
  2079. tile_rows = *y / tile_height;
  2080. *y %= tile_height;
  2081. tiles = *x / tile_width;
  2082. *x %= tile_width;
  2083. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2084. offset_aligned = offset & ~alignment;
  2085. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2086. tile_size, pitch_tiles,
  2087. offset, offset_aligned);
  2088. } else {
  2089. offset = *y * pitch + *x * cpp;
  2090. offset_aligned = offset & ~alignment;
  2091. *y = (offset & alignment) / pitch;
  2092. *x = ((offset & alignment) - *y * pitch) / cpp;
  2093. }
  2094. return offset_aligned;
  2095. }
  2096. u32 intel_compute_tile_offset(int *x, int *y,
  2097. const struct intel_plane_state *state,
  2098. int plane)
  2099. {
  2100. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2101. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2102. const struct drm_framebuffer *fb = state->base.fb;
  2103. unsigned int rotation = state->base.rotation;
  2104. int pitch = intel_fb_pitch(fb, plane, rotation);
  2105. u32 alignment;
  2106. if (intel_plane->id == PLANE_CURSOR)
  2107. alignment = intel_cursor_alignment(dev_priv);
  2108. else
  2109. alignment = intel_surf_alignment(fb, plane);
  2110. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2111. rotation, alignment);
  2112. }
  2113. /* Convert the fb->offset[] into x/y offsets */
  2114. static int intel_fb_offset_to_xy(int *x, int *y,
  2115. const struct drm_framebuffer *fb, int plane)
  2116. {
  2117. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2118. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2119. fb->offsets[plane] % intel_tile_size(dev_priv))
  2120. return -EINVAL;
  2121. *x = 0;
  2122. *y = 0;
  2123. _intel_adjust_tile_offset(x, y,
  2124. fb, plane, DRM_MODE_ROTATE_0,
  2125. fb->offsets[plane], 0);
  2126. return 0;
  2127. }
  2128. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2129. {
  2130. switch (fb_modifier) {
  2131. case I915_FORMAT_MOD_X_TILED:
  2132. return I915_TILING_X;
  2133. case I915_FORMAT_MOD_Y_TILED:
  2134. case I915_FORMAT_MOD_Y_TILED_CCS:
  2135. return I915_TILING_Y;
  2136. default:
  2137. return I915_TILING_NONE;
  2138. }
  2139. }
  2140. static const struct drm_format_info ccs_formats[] = {
  2141. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2142. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2143. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2144. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2145. };
  2146. static const struct drm_format_info *
  2147. lookup_format_info(const struct drm_format_info formats[],
  2148. int num_formats, u32 format)
  2149. {
  2150. int i;
  2151. for (i = 0; i < num_formats; i++) {
  2152. if (formats[i].format == format)
  2153. return &formats[i];
  2154. }
  2155. return NULL;
  2156. }
  2157. static const struct drm_format_info *
  2158. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2159. {
  2160. switch (cmd->modifier[0]) {
  2161. case I915_FORMAT_MOD_Y_TILED_CCS:
  2162. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2163. return lookup_format_info(ccs_formats,
  2164. ARRAY_SIZE(ccs_formats),
  2165. cmd->pixel_format);
  2166. default:
  2167. return NULL;
  2168. }
  2169. }
  2170. static int
  2171. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2172. struct drm_framebuffer *fb)
  2173. {
  2174. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2175. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2176. u32 gtt_offset_rotated = 0;
  2177. unsigned int max_size = 0;
  2178. int i, num_planes = fb->format->num_planes;
  2179. unsigned int tile_size = intel_tile_size(dev_priv);
  2180. for (i = 0; i < num_planes; i++) {
  2181. unsigned int width, height;
  2182. unsigned int cpp, size;
  2183. u32 offset;
  2184. int x, y;
  2185. int ret;
  2186. cpp = fb->format->cpp[i];
  2187. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2188. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2189. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2190. if (ret) {
  2191. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2192. i, fb->offsets[i]);
  2193. return ret;
  2194. }
  2195. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2196. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2197. int hsub = fb->format->hsub;
  2198. int vsub = fb->format->vsub;
  2199. int tile_width, tile_height;
  2200. int main_x, main_y;
  2201. int ccs_x, ccs_y;
  2202. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2203. tile_width *= hsub;
  2204. tile_height *= vsub;
  2205. ccs_x = (x * hsub) % tile_width;
  2206. ccs_y = (y * vsub) % tile_height;
  2207. main_x = intel_fb->normal[0].x % tile_width;
  2208. main_y = intel_fb->normal[0].y % tile_height;
  2209. /*
  2210. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2211. * x/y offsets must match between CCS and the main surface.
  2212. */
  2213. if (main_x != ccs_x || main_y != ccs_y) {
  2214. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2215. main_x, main_y,
  2216. ccs_x, ccs_y,
  2217. intel_fb->normal[0].x,
  2218. intel_fb->normal[0].y,
  2219. x, y);
  2220. return -EINVAL;
  2221. }
  2222. }
  2223. /*
  2224. * The fence (if used) is aligned to the start of the object
  2225. * so having the framebuffer wrap around across the edge of the
  2226. * fenced region doesn't really work. We have no API to configure
  2227. * the fence start offset within the object (nor could we probably
  2228. * on gen2/3). So it's just easier if we just require that the
  2229. * fb layout agrees with the fence layout. We already check that the
  2230. * fb stride matches the fence stride elsewhere.
  2231. */
  2232. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2233. (x + width) * cpp > fb->pitches[i]) {
  2234. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2235. i, fb->offsets[i]);
  2236. return -EINVAL;
  2237. }
  2238. /*
  2239. * First pixel of the framebuffer from
  2240. * the start of the normal gtt mapping.
  2241. */
  2242. intel_fb->normal[i].x = x;
  2243. intel_fb->normal[i].y = y;
  2244. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2245. fb, i, fb->pitches[i],
  2246. DRM_MODE_ROTATE_0, tile_size);
  2247. offset /= tile_size;
  2248. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2249. unsigned int tile_width, tile_height;
  2250. unsigned int pitch_tiles;
  2251. struct drm_rect r;
  2252. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2253. rot_info->plane[i].offset = offset;
  2254. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2255. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2256. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2257. intel_fb->rotated[i].pitch =
  2258. rot_info->plane[i].height * tile_height;
  2259. /* how many tiles does this plane need */
  2260. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2261. /*
  2262. * If the plane isn't horizontally tile aligned,
  2263. * we need one more tile.
  2264. */
  2265. if (x != 0)
  2266. size++;
  2267. /* rotate the x/y offsets to match the GTT view */
  2268. r.x1 = x;
  2269. r.y1 = y;
  2270. r.x2 = x + width;
  2271. r.y2 = y + height;
  2272. drm_rect_rotate(&r,
  2273. rot_info->plane[i].width * tile_width,
  2274. rot_info->plane[i].height * tile_height,
  2275. DRM_MODE_ROTATE_270);
  2276. x = r.x1;
  2277. y = r.y1;
  2278. /* rotate the tile dimensions to match the GTT view */
  2279. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2280. swap(tile_width, tile_height);
  2281. /*
  2282. * We only keep the x/y offsets, so push all of the
  2283. * gtt offset into the x/y offsets.
  2284. */
  2285. __intel_adjust_tile_offset(&x, &y,
  2286. tile_width, tile_height,
  2287. tile_size, pitch_tiles,
  2288. gtt_offset_rotated * tile_size, 0);
  2289. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2290. /*
  2291. * First pixel of the framebuffer from
  2292. * the start of the rotated gtt mapping.
  2293. */
  2294. intel_fb->rotated[i].x = x;
  2295. intel_fb->rotated[i].y = y;
  2296. } else {
  2297. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2298. x * cpp, tile_size);
  2299. }
  2300. /* how many tiles in total needed in the bo */
  2301. max_size = max(max_size, offset + size);
  2302. }
  2303. if (max_size * tile_size > intel_fb->obj->base.size) {
  2304. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2305. max_size * tile_size, intel_fb->obj->base.size);
  2306. return -EINVAL;
  2307. }
  2308. return 0;
  2309. }
  2310. static int i9xx_format_to_fourcc(int format)
  2311. {
  2312. switch (format) {
  2313. case DISPPLANE_8BPP:
  2314. return DRM_FORMAT_C8;
  2315. case DISPPLANE_BGRX555:
  2316. return DRM_FORMAT_XRGB1555;
  2317. case DISPPLANE_BGRX565:
  2318. return DRM_FORMAT_RGB565;
  2319. default:
  2320. case DISPPLANE_BGRX888:
  2321. return DRM_FORMAT_XRGB8888;
  2322. case DISPPLANE_RGBX888:
  2323. return DRM_FORMAT_XBGR8888;
  2324. case DISPPLANE_BGRX101010:
  2325. return DRM_FORMAT_XRGB2101010;
  2326. case DISPPLANE_RGBX101010:
  2327. return DRM_FORMAT_XBGR2101010;
  2328. }
  2329. }
  2330. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2331. {
  2332. switch (format) {
  2333. case PLANE_CTL_FORMAT_RGB_565:
  2334. return DRM_FORMAT_RGB565;
  2335. default:
  2336. case PLANE_CTL_FORMAT_XRGB_8888:
  2337. if (rgb_order) {
  2338. if (alpha)
  2339. return DRM_FORMAT_ABGR8888;
  2340. else
  2341. return DRM_FORMAT_XBGR8888;
  2342. } else {
  2343. if (alpha)
  2344. return DRM_FORMAT_ARGB8888;
  2345. else
  2346. return DRM_FORMAT_XRGB8888;
  2347. }
  2348. case PLANE_CTL_FORMAT_XRGB_2101010:
  2349. if (rgb_order)
  2350. return DRM_FORMAT_XBGR2101010;
  2351. else
  2352. return DRM_FORMAT_XRGB2101010;
  2353. }
  2354. }
  2355. static bool
  2356. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2357. struct intel_initial_plane_config *plane_config)
  2358. {
  2359. struct drm_device *dev = crtc->base.dev;
  2360. struct drm_i915_private *dev_priv = to_i915(dev);
  2361. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2362. struct drm_i915_gem_object *obj = NULL;
  2363. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2364. struct drm_framebuffer *fb = &plane_config->fb->base;
  2365. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2366. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2367. PAGE_SIZE);
  2368. size_aligned -= base_aligned;
  2369. if (plane_config->size == 0)
  2370. return false;
  2371. /* If the FB is too big, just don't use it since fbdev is not very
  2372. * important and we should probably use that space with FBC or other
  2373. * features. */
  2374. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2375. return false;
  2376. mutex_lock(&dev->struct_mutex);
  2377. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2378. base_aligned,
  2379. base_aligned,
  2380. size_aligned);
  2381. mutex_unlock(&dev->struct_mutex);
  2382. if (!obj)
  2383. return false;
  2384. if (plane_config->tiling == I915_TILING_X)
  2385. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2386. mode_cmd.pixel_format = fb->format->format;
  2387. mode_cmd.width = fb->width;
  2388. mode_cmd.height = fb->height;
  2389. mode_cmd.pitches[0] = fb->pitches[0];
  2390. mode_cmd.modifier[0] = fb->modifier;
  2391. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2392. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2393. DRM_DEBUG_KMS("intel fb init failed\n");
  2394. goto out_unref_obj;
  2395. }
  2396. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2397. return true;
  2398. out_unref_obj:
  2399. i915_gem_object_put(obj);
  2400. return false;
  2401. }
  2402. static void
  2403. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2404. struct intel_plane_state *plane_state,
  2405. bool visible)
  2406. {
  2407. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2408. plane_state->base.visible = visible;
  2409. /* FIXME pre-g4x don't work like this */
  2410. if (visible) {
  2411. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2412. crtc_state->active_planes |= BIT(plane->id);
  2413. } else {
  2414. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2415. crtc_state->active_planes &= ~BIT(plane->id);
  2416. }
  2417. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2418. crtc_state->base.crtc->name,
  2419. crtc_state->active_planes);
  2420. }
  2421. static void
  2422. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2423. struct intel_initial_plane_config *plane_config)
  2424. {
  2425. struct drm_device *dev = intel_crtc->base.dev;
  2426. struct drm_i915_private *dev_priv = to_i915(dev);
  2427. struct drm_crtc *c;
  2428. struct drm_i915_gem_object *obj;
  2429. struct drm_plane *primary = intel_crtc->base.primary;
  2430. struct drm_plane_state *plane_state = primary->state;
  2431. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2432. struct intel_plane *intel_plane = to_intel_plane(primary);
  2433. struct intel_plane_state *intel_state =
  2434. to_intel_plane_state(plane_state);
  2435. struct drm_framebuffer *fb;
  2436. if (!plane_config->fb)
  2437. return;
  2438. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2439. fb = &plane_config->fb->base;
  2440. goto valid_fb;
  2441. }
  2442. kfree(plane_config->fb);
  2443. /*
  2444. * Failed to alloc the obj, check to see if we should share
  2445. * an fb with another CRTC instead
  2446. */
  2447. for_each_crtc(dev, c) {
  2448. struct intel_plane_state *state;
  2449. if (c == &intel_crtc->base)
  2450. continue;
  2451. if (!to_intel_crtc(c)->active)
  2452. continue;
  2453. state = to_intel_plane_state(c->primary->state);
  2454. if (!state->vma)
  2455. continue;
  2456. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2457. fb = c->primary->fb;
  2458. drm_framebuffer_reference(fb);
  2459. goto valid_fb;
  2460. }
  2461. }
  2462. /*
  2463. * We've failed to reconstruct the BIOS FB. Current display state
  2464. * indicates that the primary plane is visible, but has a NULL FB,
  2465. * which will lead to problems later if we don't fix it up. The
  2466. * simplest solution is to just disable the primary plane now and
  2467. * pretend the BIOS never had it enabled.
  2468. */
  2469. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2470. to_intel_plane_state(plane_state),
  2471. false);
  2472. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2473. trace_intel_disable_plane(primary, intel_crtc);
  2474. intel_plane->disable_plane(intel_plane, intel_crtc);
  2475. return;
  2476. valid_fb:
  2477. mutex_lock(&dev->struct_mutex);
  2478. intel_state->vma =
  2479. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2480. mutex_unlock(&dev->struct_mutex);
  2481. if (IS_ERR(intel_state->vma)) {
  2482. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2483. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2484. intel_state->vma = NULL;
  2485. drm_framebuffer_unreference(fb);
  2486. return;
  2487. }
  2488. plane_state->src_x = 0;
  2489. plane_state->src_y = 0;
  2490. plane_state->src_w = fb->width << 16;
  2491. plane_state->src_h = fb->height << 16;
  2492. plane_state->crtc_x = 0;
  2493. plane_state->crtc_y = 0;
  2494. plane_state->crtc_w = fb->width;
  2495. plane_state->crtc_h = fb->height;
  2496. intel_state->base.src = drm_plane_state_src(plane_state);
  2497. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2498. obj = intel_fb_obj(fb);
  2499. if (i915_gem_object_is_tiled(obj))
  2500. dev_priv->preserve_bios_swizzle = true;
  2501. drm_framebuffer_reference(fb);
  2502. primary->fb = primary->state->fb = fb;
  2503. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2504. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2505. to_intel_plane_state(plane_state),
  2506. true);
  2507. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2508. &obj->frontbuffer_bits);
  2509. }
  2510. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2511. unsigned int rotation)
  2512. {
  2513. int cpp = fb->format->cpp[plane];
  2514. switch (fb->modifier) {
  2515. case DRM_FORMAT_MOD_LINEAR:
  2516. case I915_FORMAT_MOD_X_TILED:
  2517. switch (cpp) {
  2518. case 8:
  2519. return 4096;
  2520. case 4:
  2521. case 2:
  2522. case 1:
  2523. return 8192;
  2524. default:
  2525. MISSING_CASE(cpp);
  2526. break;
  2527. }
  2528. break;
  2529. case I915_FORMAT_MOD_Y_TILED_CCS:
  2530. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2531. /* FIXME AUX plane? */
  2532. case I915_FORMAT_MOD_Y_TILED:
  2533. case I915_FORMAT_MOD_Yf_TILED:
  2534. switch (cpp) {
  2535. case 8:
  2536. return 2048;
  2537. case 4:
  2538. return 4096;
  2539. case 2:
  2540. case 1:
  2541. return 8192;
  2542. default:
  2543. MISSING_CASE(cpp);
  2544. break;
  2545. }
  2546. break;
  2547. default:
  2548. MISSING_CASE(fb->modifier);
  2549. }
  2550. return 2048;
  2551. }
  2552. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2553. int main_x, int main_y, u32 main_offset)
  2554. {
  2555. const struct drm_framebuffer *fb = plane_state->base.fb;
  2556. int hsub = fb->format->hsub;
  2557. int vsub = fb->format->vsub;
  2558. int aux_x = plane_state->aux.x;
  2559. int aux_y = plane_state->aux.y;
  2560. u32 aux_offset = plane_state->aux.offset;
  2561. u32 alignment = intel_surf_alignment(fb, 1);
  2562. while (aux_offset >= main_offset && aux_y <= main_y) {
  2563. int x, y;
  2564. if (aux_x == main_x && aux_y == main_y)
  2565. break;
  2566. if (aux_offset == 0)
  2567. break;
  2568. x = aux_x / hsub;
  2569. y = aux_y / vsub;
  2570. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2571. aux_offset, aux_offset - alignment);
  2572. aux_x = x * hsub + aux_x % hsub;
  2573. aux_y = y * vsub + aux_y % vsub;
  2574. }
  2575. if (aux_x != main_x || aux_y != main_y)
  2576. return false;
  2577. plane_state->aux.offset = aux_offset;
  2578. plane_state->aux.x = aux_x;
  2579. plane_state->aux.y = aux_y;
  2580. return true;
  2581. }
  2582. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2583. {
  2584. const struct drm_framebuffer *fb = plane_state->base.fb;
  2585. unsigned int rotation = plane_state->base.rotation;
  2586. int x = plane_state->base.src.x1 >> 16;
  2587. int y = plane_state->base.src.y1 >> 16;
  2588. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2589. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2590. int max_width = skl_max_plane_width(fb, 0, rotation);
  2591. int max_height = 4096;
  2592. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2593. if (w > max_width || h > max_height) {
  2594. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2595. w, h, max_width, max_height);
  2596. return -EINVAL;
  2597. }
  2598. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2599. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2600. alignment = intel_surf_alignment(fb, 0);
  2601. /*
  2602. * AUX surface offset is specified as the distance from the
  2603. * main surface offset, and it must be non-negative. Make
  2604. * sure that is what we will get.
  2605. */
  2606. if (offset > aux_offset)
  2607. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2608. offset, aux_offset & ~(alignment - 1));
  2609. /*
  2610. * When using an X-tiled surface, the plane blows up
  2611. * if the x offset + width exceed the stride.
  2612. *
  2613. * TODO: linear and Y-tiled seem fine, Yf untested,
  2614. */
  2615. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2616. int cpp = fb->format->cpp[0];
  2617. while ((x + w) * cpp > fb->pitches[0]) {
  2618. if (offset == 0) {
  2619. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2620. return -EINVAL;
  2621. }
  2622. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2623. offset, offset - alignment);
  2624. }
  2625. }
  2626. /*
  2627. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2628. * they match with the main surface x/y offsets.
  2629. */
  2630. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2631. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2632. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2633. if (offset == 0)
  2634. break;
  2635. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2636. offset, offset - alignment);
  2637. }
  2638. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2639. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2640. return -EINVAL;
  2641. }
  2642. }
  2643. plane_state->main.offset = offset;
  2644. plane_state->main.x = x;
  2645. plane_state->main.y = y;
  2646. return 0;
  2647. }
  2648. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2649. {
  2650. const struct drm_framebuffer *fb = plane_state->base.fb;
  2651. unsigned int rotation = plane_state->base.rotation;
  2652. int max_width = skl_max_plane_width(fb, 1, rotation);
  2653. int max_height = 4096;
  2654. int x = plane_state->base.src.x1 >> 17;
  2655. int y = plane_state->base.src.y1 >> 17;
  2656. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2657. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2658. u32 offset;
  2659. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2660. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2661. /* FIXME not quite sure how/if these apply to the chroma plane */
  2662. if (w > max_width || h > max_height) {
  2663. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2664. w, h, max_width, max_height);
  2665. return -EINVAL;
  2666. }
  2667. plane_state->aux.offset = offset;
  2668. plane_state->aux.x = x;
  2669. plane_state->aux.y = y;
  2670. return 0;
  2671. }
  2672. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2673. {
  2674. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2675. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2676. const struct drm_framebuffer *fb = plane_state->base.fb;
  2677. int src_x = plane_state->base.src.x1 >> 16;
  2678. int src_y = plane_state->base.src.y1 >> 16;
  2679. int hsub = fb->format->hsub;
  2680. int vsub = fb->format->vsub;
  2681. int x = src_x / hsub;
  2682. int y = src_y / vsub;
  2683. u32 offset;
  2684. switch (plane->id) {
  2685. case PLANE_PRIMARY:
  2686. case PLANE_SPRITE0:
  2687. break;
  2688. default:
  2689. DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
  2690. return -EINVAL;
  2691. }
  2692. if (crtc->pipe == PIPE_C) {
  2693. DRM_DEBUG_KMS("No RC support on pipe C\n");
  2694. return -EINVAL;
  2695. }
  2696. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2697. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2698. plane_state->base.rotation);
  2699. return -EINVAL;
  2700. }
  2701. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2702. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2703. plane_state->aux.offset = offset;
  2704. plane_state->aux.x = x * hsub + src_x % hsub;
  2705. plane_state->aux.y = y * vsub + src_y % vsub;
  2706. return 0;
  2707. }
  2708. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2709. {
  2710. const struct drm_framebuffer *fb = plane_state->base.fb;
  2711. unsigned int rotation = plane_state->base.rotation;
  2712. int ret;
  2713. if (!plane_state->base.visible)
  2714. return 0;
  2715. /* Rotate src coordinates to match rotated GTT view */
  2716. if (drm_rotation_90_or_270(rotation))
  2717. drm_rect_rotate(&plane_state->base.src,
  2718. fb->width << 16, fb->height << 16,
  2719. DRM_MODE_ROTATE_270);
  2720. /*
  2721. * Handle the AUX surface first since
  2722. * the main surface setup depends on it.
  2723. */
  2724. if (fb->format->format == DRM_FORMAT_NV12) {
  2725. ret = skl_check_nv12_aux_surface(plane_state);
  2726. if (ret)
  2727. return ret;
  2728. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2729. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2730. ret = skl_check_ccs_aux_surface(plane_state);
  2731. if (ret)
  2732. return ret;
  2733. } else {
  2734. plane_state->aux.offset = ~0xfff;
  2735. plane_state->aux.x = 0;
  2736. plane_state->aux.y = 0;
  2737. }
  2738. ret = skl_check_main_surface(plane_state);
  2739. if (ret)
  2740. return ret;
  2741. return 0;
  2742. }
  2743. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2744. const struct intel_plane_state *plane_state)
  2745. {
  2746. struct drm_i915_private *dev_priv =
  2747. to_i915(plane_state->base.plane->dev);
  2748. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2749. const struct drm_framebuffer *fb = plane_state->base.fb;
  2750. unsigned int rotation = plane_state->base.rotation;
  2751. u32 dspcntr;
  2752. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2753. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2754. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2755. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2756. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2757. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2758. if (INTEL_GEN(dev_priv) < 4)
  2759. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2760. switch (fb->format->format) {
  2761. case DRM_FORMAT_C8:
  2762. dspcntr |= DISPPLANE_8BPP;
  2763. break;
  2764. case DRM_FORMAT_XRGB1555:
  2765. dspcntr |= DISPPLANE_BGRX555;
  2766. break;
  2767. case DRM_FORMAT_RGB565:
  2768. dspcntr |= DISPPLANE_BGRX565;
  2769. break;
  2770. case DRM_FORMAT_XRGB8888:
  2771. dspcntr |= DISPPLANE_BGRX888;
  2772. break;
  2773. case DRM_FORMAT_XBGR8888:
  2774. dspcntr |= DISPPLANE_RGBX888;
  2775. break;
  2776. case DRM_FORMAT_XRGB2101010:
  2777. dspcntr |= DISPPLANE_BGRX101010;
  2778. break;
  2779. case DRM_FORMAT_XBGR2101010:
  2780. dspcntr |= DISPPLANE_RGBX101010;
  2781. break;
  2782. default:
  2783. MISSING_CASE(fb->format->format);
  2784. return 0;
  2785. }
  2786. if (INTEL_GEN(dev_priv) >= 4 &&
  2787. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2788. dspcntr |= DISPPLANE_TILED;
  2789. if (rotation & DRM_MODE_ROTATE_180)
  2790. dspcntr |= DISPPLANE_ROTATE_180;
  2791. if (rotation & DRM_MODE_REFLECT_X)
  2792. dspcntr |= DISPPLANE_MIRROR;
  2793. return dspcntr;
  2794. }
  2795. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2796. {
  2797. struct drm_i915_private *dev_priv =
  2798. to_i915(plane_state->base.plane->dev);
  2799. int src_x = plane_state->base.src.x1 >> 16;
  2800. int src_y = plane_state->base.src.y1 >> 16;
  2801. u32 offset;
  2802. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2803. if (INTEL_GEN(dev_priv) >= 4)
  2804. offset = intel_compute_tile_offset(&src_x, &src_y,
  2805. plane_state, 0);
  2806. else
  2807. offset = 0;
  2808. /* HSW/BDW do this automagically in hardware */
  2809. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2810. unsigned int rotation = plane_state->base.rotation;
  2811. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2812. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2813. if (rotation & DRM_MODE_ROTATE_180) {
  2814. src_x += src_w - 1;
  2815. src_y += src_h - 1;
  2816. } else if (rotation & DRM_MODE_REFLECT_X) {
  2817. src_x += src_w - 1;
  2818. }
  2819. }
  2820. plane_state->main.offset = offset;
  2821. plane_state->main.x = src_x;
  2822. plane_state->main.y = src_y;
  2823. return 0;
  2824. }
  2825. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2826. const struct intel_crtc_state *crtc_state,
  2827. const struct intel_plane_state *plane_state)
  2828. {
  2829. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2830. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2831. const struct drm_framebuffer *fb = plane_state->base.fb;
  2832. enum plane plane = primary->plane;
  2833. u32 linear_offset;
  2834. u32 dspcntr = plane_state->ctl;
  2835. i915_reg_t reg = DSPCNTR(plane);
  2836. int x = plane_state->main.x;
  2837. int y = plane_state->main.y;
  2838. unsigned long irqflags;
  2839. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2840. if (INTEL_GEN(dev_priv) >= 4)
  2841. crtc->dspaddr_offset = plane_state->main.offset;
  2842. else
  2843. crtc->dspaddr_offset = linear_offset;
  2844. crtc->adjusted_x = x;
  2845. crtc->adjusted_y = y;
  2846. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2847. if (INTEL_GEN(dev_priv) < 4) {
  2848. /* pipesrc and dspsize control the size that is scaled from,
  2849. * which should always be the user's requested size.
  2850. */
  2851. I915_WRITE_FW(DSPSIZE(plane),
  2852. ((crtc_state->pipe_src_h - 1) << 16) |
  2853. (crtc_state->pipe_src_w - 1));
  2854. I915_WRITE_FW(DSPPOS(plane), 0);
  2855. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2856. I915_WRITE_FW(PRIMSIZE(plane),
  2857. ((crtc_state->pipe_src_h - 1) << 16) |
  2858. (crtc_state->pipe_src_w - 1));
  2859. I915_WRITE_FW(PRIMPOS(plane), 0);
  2860. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2861. }
  2862. I915_WRITE_FW(reg, dspcntr);
  2863. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2864. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2865. I915_WRITE_FW(DSPSURF(plane),
  2866. intel_plane_ggtt_offset(plane_state) +
  2867. crtc->dspaddr_offset);
  2868. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2869. } else if (INTEL_GEN(dev_priv) >= 4) {
  2870. I915_WRITE_FW(DSPSURF(plane),
  2871. intel_plane_ggtt_offset(plane_state) +
  2872. crtc->dspaddr_offset);
  2873. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2874. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2875. } else {
  2876. I915_WRITE_FW(DSPADDR(plane),
  2877. intel_plane_ggtt_offset(plane_state) +
  2878. crtc->dspaddr_offset);
  2879. }
  2880. POSTING_READ_FW(reg);
  2881. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2882. }
  2883. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2884. struct intel_crtc *crtc)
  2885. {
  2886. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2887. enum plane plane = primary->plane;
  2888. unsigned long irqflags;
  2889. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2890. I915_WRITE_FW(DSPCNTR(plane), 0);
  2891. if (INTEL_INFO(dev_priv)->gen >= 4)
  2892. I915_WRITE_FW(DSPSURF(plane), 0);
  2893. else
  2894. I915_WRITE_FW(DSPADDR(plane), 0);
  2895. POSTING_READ_FW(DSPCNTR(plane));
  2896. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2897. }
  2898. static u32
  2899. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2900. {
  2901. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2902. return 64;
  2903. else
  2904. return intel_tile_width_bytes(fb, plane);
  2905. }
  2906. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2907. {
  2908. struct drm_device *dev = intel_crtc->base.dev;
  2909. struct drm_i915_private *dev_priv = to_i915(dev);
  2910. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2911. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2912. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2913. }
  2914. /*
  2915. * This function detaches (aka. unbinds) unused scalers in hardware
  2916. */
  2917. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2918. {
  2919. struct intel_crtc_scaler_state *scaler_state;
  2920. int i;
  2921. scaler_state = &intel_crtc->config->scaler_state;
  2922. /* loop through and disable scalers that aren't in use */
  2923. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2924. if (!scaler_state->scalers[i].in_use)
  2925. skl_detach_scaler(intel_crtc, i);
  2926. }
  2927. }
  2928. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2929. unsigned int rotation)
  2930. {
  2931. u32 stride;
  2932. if (plane >= fb->format->num_planes)
  2933. return 0;
  2934. stride = intel_fb_pitch(fb, plane, rotation);
  2935. /*
  2936. * The stride is either expressed as a multiple of 64 bytes chunks for
  2937. * linear buffers or in number of tiles for tiled buffers.
  2938. */
  2939. if (drm_rotation_90_or_270(rotation))
  2940. stride /= intel_tile_height(fb, plane);
  2941. else
  2942. stride /= intel_fb_stride_alignment(fb, plane);
  2943. return stride;
  2944. }
  2945. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2946. {
  2947. switch (pixel_format) {
  2948. case DRM_FORMAT_C8:
  2949. return PLANE_CTL_FORMAT_INDEXED;
  2950. case DRM_FORMAT_RGB565:
  2951. return PLANE_CTL_FORMAT_RGB_565;
  2952. case DRM_FORMAT_XBGR8888:
  2953. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2954. case DRM_FORMAT_XRGB8888:
  2955. return PLANE_CTL_FORMAT_XRGB_8888;
  2956. /*
  2957. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2958. * to be already pre-multiplied. We need to add a knob (or a different
  2959. * DRM_FORMAT) for user-space to configure that.
  2960. */
  2961. case DRM_FORMAT_ABGR8888:
  2962. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2963. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2964. case DRM_FORMAT_ARGB8888:
  2965. return PLANE_CTL_FORMAT_XRGB_8888 |
  2966. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2967. case DRM_FORMAT_XRGB2101010:
  2968. return PLANE_CTL_FORMAT_XRGB_2101010;
  2969. case DRM_FORMAT_XBGR2101010:
  2970. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2971. case DRM_FORMAT_YUYV:
  2972. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2973. case DRM_FORMAT_YVYU:
  2974. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2975. case DRM_FORMAT_UYVY:
  2976. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2977. case DRM_FORMAT_VYUY:
  2978. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2979. default:
  2980. MISSING_CASE(pixel_format);
  2981. }
  2982. return 0;
  2983. }
  2984. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2985. {
  2986. switch (fb_modifier) {
  2987. case DRM_FORMAT_MOD_LINEAR:
  2988. break;
  2989. case I915_FORMAT_MOD_X_TILED:
  2990. return PLANE_CTL_TILED_X;
  2991. case I915_FORMAT_MOD_Y_TILED:
  2992. return PLANE_CTL_TILED_Y;
  2993. case I915_FORMAT_MOD_Y_TILED_CCS:
  2994. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2995. case I915_FORMAT_MOD_Yf_TILED:
  2996. return PLANE_CTL_TILED_YF;
  2997. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2998. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  2999. default:
  3000. MISSING_CASE(fb_modifier);
  3001. }
  3002. return 0;
  3003. }
  3004. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  3005. {
  3006. switch (rotation) {
  3007. case DRM_MODE_ROTATE_0:
  3008. break;
  3009. /*
  3010. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3011. * while i915 HW rotation is clockwise, thats why this swapping.
  3012. */
  3013. case DRM_MODE_ROTATE_90:
  3014. return PLANE_CTL_ROTATE_270;
  3015. case DRM_MODE_ROTATE_180:
  3016. return PLANE_CTL_ROTATE_180;
  3017. case DRM_MODE_ROTATE_270:
  3018. return PLANE_CTL_ROTATE_90;
  3019. default:
  3020. MISSING_CASE(rotation);
  3021. }
  3022. return 0;
  3023. }
  3024. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3025. const struct intel_plane_state *plane_state)
  3026. {
  3027. struct drm_i915_private *dev_priv =
  3028. to_i915(plane_state->base.plane->dev);
  3029. const struct drm_framebuffer *fb = plane_state->base.fb;
  3030. unsigned int rotation = plane_state->base.rotation;
  3031. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3032. u32 plane_ctl;
  3033. plane_ctl = PLANE_CTL_ENABLE;
  3034. if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
  3035. plane_ctl |=
  3036. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3037. PLANE_CTL_PIPE_CSC_ENABLE |
  3038. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3039. }
  3040. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3041. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3042. plane_ctl |= skl_plane_ctl_rotation(rotation);
  3043. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3044. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3045. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3046. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3047. return plane_ctl;
  3048. }
  3049. static void skylake_update_primary_plane(struct intel_plane *plane,
  3050. const struct intel_crtc_state *crtc_state,
  3051. const struct intel_plane_state *plane_state)
  3052. {
  3053. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  3054. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3055. const struct drm_framebuffer *fb = plane_state->base.fb;
  3056. enum plane_id plane_id = plane->id;
  3057. enum pipe pipe = plane->pipe;
  3058. u32 plane_ctl = plane_state->ctl;
  3059. unsigned int rotation = plane_state->base.rotation;
  3060. u32 stride = skl_plane_stride(fb, 0, rotation);
  3061. u32 aux_stride = skl_plane_stride(fb, 1, rotation);
  3062. u32 surf_addr = plane_state->main.offset;
  3063. int scaler_id = plane_state->scaler_id;
  3064. int src_x = plane_state->main.x;
  3065. int src_y = plane_state->main.y;
  3066. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  3067. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  3068. int dst_x = plane_state->base.dst.x1;
  3069. int dst_y = plane_state->base.dst.y1;
  3070. int dst_w = drm_rect_width(&plane_state->base.dst);
  3071. int dst_h = drm_rect_height(&plane_state->base.dst);
  3072. unsigned long irqflags;
  3073. /* Sizes are 0 based */
  3074. src_w--;
  3075. src_h--;
  3076. dst_w--;
  3077. dst_h--;
  3078. crtc->dspaddr_offset = surf_addr;
  3079. crtc->adjusted_x = src_x;
  3080. crtc->adjusted_y = src_y;
  3081. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3082. if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
  3083. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  3084. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  3085. PLANE_COLOR_PIPE_CSC_ENABLE |
  3086. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  3087. }
  3088. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  3089. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  3090. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  3091. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  3092. I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
  3093. (plane_state->aux.offset - surf_addr) | aux_stride);
  3094. I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
  3095. (plane_state->aux.y << 16) | plane_state->aux.x);
  3096. if (scaler_id >= 0) {
  3097. uint32_t ps_ctrl = 0;
  3098. WARN_ON(!dst_w || !dst_h);
  3099. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  3100. crtc_state->scaler_state.scalers[scaler_id].mode;
  3101. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  3102. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  3103. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  3104. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  3105. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  3106. } else {
  3107. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  3108. }
  3109. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  3110. intel_plane_ggtt_offset(plane_state) + surf_addr);
  3111. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  3112. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3113. }
  3114. static void skylake_disable_primary_plane(struct intel_plane *primary,
  3115. struct intel_crtc *crtc)
  3116. {
  3117. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  3118. enum plane_id plane_id = primary->id;
  3119. enum pipe pipe = primary->pipe;
  3120. unsigned long irqflags;
  3121. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3122. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  3123. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  3124. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  3125. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3126. }
  3127. static int
  3128. __intel_display_resume(struct drm_device *dev,
  3129. struct drm_atomic_state *state,
  3130. struct drm_modeset_acquire_ctx *ctx)
  3131. {
  3132. struct drm_crtc_state *crtc_state;
  3133. struct drm_crtc *crtc;
  3134. int i, ret;
  3135. intel_modeset_setup_hw_state(dev, ctx);
  3136. i915_redisable_vga(to_i915(dev));
  3137. if (!state)
  3138. return 0;
  3139. /*
  3140. * We've duplicated the state, pointers to the old state are invalid.
  3141. *
  3142. * Don't attempt to use the old state until we commit the duplicated state.
  3143. */
  3144. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3145. /*
  3146. * Force recalculation even if we restore
  3147. * current state. With fast modeset this may not result
  3148. * in a modeset when the state is compatible.
  3149. */
  3150. crtc_state->mode_changed = true;
  3151. }
  3152. /* ignore any reset values/BIOS leftovers in the WM registers */
  3153. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3154. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3155. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3156. WARN_ON(ret == -EDEADLK);
  3157. return ret;
  3158. }
  3159. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3160. {
  3161. return intel_has_gpu_reset(dev_priv) &&
  3162. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3163. }
  3164. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3165. {
  3166. struct drm_device *dev = &dev_priv->drm;
  3167. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3168. struct drm_atomic_state *state;
  3169. int ret;
  3170. /* reset doesn't touch the display */
  3171. if (!i915.force_reset_modeset_test &&
  3172. !gpu_reset_clobbers_display(dev_priv))
  3173. return;
  3174. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3175. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3176. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3177. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3178. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3179. i915_gem_set_wedged(dev_priv);
  3180. }
  3181. /*
  3182. * Need mode_config.mutex so that we don't
  3183. * trample ongoing ->detect() and whatnot.
  3184. */
  3185. mutex_lock(&dev->mode_config.mutex);
  3186. drm_modeset_acquire_init(ctx, 0);
  3187. while (1) {
  3188. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3189. if (ret != -EDEADLK)
  3190. break;
  3191. drm_modeset_backoff(ctx);
  3192. }
  3193. /*
  3194. * Disabling the crtcs gracefully seems nicer. Also the
  3195. * g33 docs say we should at least disable all the planes.
  3196. */
  3197. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3198. if (IS_ERR(state)) {
  3199. ret = PTR_ERR(state);
  3200. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3201. return;
  3202. }
  3203. ret = drm_atomic_helper_disable_all(dev, ctx);
  3204. if (ret) {
  3205. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3206. drm_atomic_state_put(state);
  3207. return;
  3208. }
  3209. dev_priv->modeset_restore_state = state;
  3210. state->acquire_ctx = ctx;
  3211. }
  3212. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3213. {
  3214. struct drm_device *dev = &dev_priv->drm;
  3215. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3216. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3217. int ret;
  3218. /* reset doesn't touch the display */
  3219. if (!i915.force_reset_modeset_test &&
  3220. !gpu_reset_clobbers_display(dev_priv))
  3221. return;
  3222. if (!state)
  3223. goto unlock;
  3224. dev_priv->modeset_restore_state = NULL;
  3225. /* reset doesn't touch the display */
  3226. if (!gpu_reset_clobbers_display(dev_priv)) {
  3227. /* for testing only restore the display */
  3228. ret = __intel_display_resume(dev, state, ctx);
  3229. if (ret)
  3230. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3231. } else {
  3232. /*
  3233. * The display has been reset as well,
  3234. * so need a full re-initialization.
  3235. */
  3236. intel_runtime_pm_disable_interrupts(dev_priv);
  3237. intel_runtime_pm_enable_interrupts(dev_priv);
  3238. intel_pps_unlock_regs_wa(dev_priv);
  3239. intel_modeset_init_hw(dev);
  3240. spin_lock_irq(&dev_priv->irq_lock);
  3241. if (dev_priv->display.hpd_irq_setup)
  3242. dev_priv->display.hpd_irq_setup(dev_priv);
  3243. spin_unlock_irq(&dev_priv->irq_lock);
  3244. ret = __intel_display_resume(dev, state, ctx);
  3245. if (ret)
  3246. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3247. intel_hpd_init(dev_priv);
  3248. }
  3249. drm_atomic_state_put(state);
  3250. unlock:
  3251. drm_modeset_drop_locks(ctx);
  3252. drm_modeset_acquire_fini(ctx);
  3253. mutex_unlock(&dev->mode_config.mutex);
  3254. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3255. }
  3256. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3257. const struct intel_crtc_state *new_crtc_state)
  3258. {
  3259. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3260. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3261. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3262. crtc->base.mode = new_crtc_state->base.mode;
  3263. /*
  3264. * Update pipe size and adjust fitter if needed: the reason for this is
  3265. * that in compute_mode_changes we check the native mode (not the pfit
  3266. * mode) to see if we can flip rather than do a full mode set. In the
  3267. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3268. * pfit state, we'll end up with a big fb scanned out into the wrong
  3269. * sized surface.
  3270. */
  3271. I915_WRITE(PIPESRC(crtc->pipe),
  3272. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3273. (new_crtc_state->pipe_src_h - 1));
  3274. /* on skylake this is done by detaching scalers */
  3275. if (INTEL_GEN(dev_priv) >= 9) {
  3276. skl_detach_scalers(crtc);
  3277. if (new_crtc_state->pch_pfit.enabled)
  3278. skylake_pfit_enable(crtc);
  3279. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3280. if (new_crtc_state->pch_pfit.enabled)
  3281. ironlake_pfit_enable(crtc);
  3282. else if (old_crtc_state->pch_pfit.enabled)
  3283. ironlake_pfit_disable(crtc, true);
  3284. }
  3285. }
  3286. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3287. {
  3288. struct drm_device *dev = crtc->base.dev;
  3289. struct drm_i915_private *dev_priv = to_i915(dev);
  3290. int pipe = crtc->pipe;
  3291. i915_reg_t reg;
  3292. u32 temp;
  3293. /* enable normal train */
  3294. reg = FDI_TX_CTL(pipe);
  3295. temp = I915_READ(reg);
  3296. if (IS_IVYBRIDGE(dev_priv)) {
  3297. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3298. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3299. } else {
  3300. temp &= ~FDI_LINK_TRAIN_NONE;
  3301. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3302. }
  3303. I915_WRITE(reg, temp);
  3304. reg = FDI_RX_CTL(pipe);
  3305. temp = I915_READ(reg);
  3306. if (HAS_PCH_CPT(dev_priv)) {
  3307. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3308. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3309. } else {
  3310. temp &= ~FDI_LINK_TRAIN_NONE;
  3311. temp |= FDI_LINK_TRAIN_NONE;
  3312. }
  3313. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3314. /* wait one idle pattern time */
  3315. POSTING_READ(reg);
  3316. udelay(1000);
  3317. /* IVB wants error correction enabled */
  3318. if (IS_IVYBRIDGE(dev_priv))
  3319. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3320. FDI_FE_ERRC_ENABLE);
  3321. }
  3322. /* The FDI link training functions for ILK/Ibexpeak. */
  3323. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3324. const struct intel_crtc_state *crtc_state)
  3325. {
  3326. struct drm_device *dev = crtc->base.dev;
  3327. struct drm_i915_private *dev_priv = to_i915(dev);
  3328. int pipe = crtc->pipe;
  3329. i915_reg_t reg;
  3330. u32 temp, tries;
  3331. /* FDI needs bits from pipe first */
  3332. assert_pipe_enabled(dev_priv, pipe);
  3333. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3334. for train result */
  3335. reg = FDI_RX_IMR(pipe);
  3336. temp = I915_READ(reg);
  3337. temp &= ~FDI_RX_SYMBOL_LOCK;
  3338. temp &= ~FDI_RX_BIT_LOCK;
  3339. I915_WRITE(reg, temp);
  3340. I915_READ(reg);
  3341. udelay(150);
  3342. /* enable CPU FDI TX and PCH FDI RX */
  3343. reg = FDI_TX_CTL(pipe);
  3344. temp = I915_READ(reg);
  3345. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3346. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3347. temp &= ~FDI_LINK_TRAIN_NONE;
  3348. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3349. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3350. reg = FDI_RX_CTL(pipe);
  3351. temp = I915_READ(reg);
  3352. temp &= ~FDI_LINK_TRAIN_NONE;
  3353. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3354. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3355. POSTING_READ(reg);
  3356. udelay(150);
  3357. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3358. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3359. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3360. FDI_RX_PHASE_SYNC_POINTER_EN);
  3361. reg = FDI_RX_IIR(pipe);
  3362. for (tries = 0; tries < 5; tries++) {
  3363. temp = I915_READ(reg);
  3364. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3365. if ((temp & FDI_RX_BIT_LOCK)) {
  3366. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3367. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3368. break;
  3369. }
  3370. }
  3371. if (tries == 5)
  3372. DRM_ERROR("FDI train 1 fail!\n");
  3373. /* Train 2 */
  3374. reg = FDI_TX_CTL(pipe);
  3375. temp = I915_READ(reg);
  3376. temp &= ~FDI_LINK_TRAIN_NONE;
  3377. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3378. I915_WRITE(reg, temp);
  3379. reg = FDI_RX_CTL(pipe);
  3380. temp = I915_READ(reg);
  3381. temp &= ~FDI_LINK_TRAIN_NONE;
  3382. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3383. I915_WRITE(reg, temp);
  3384. POSTING_READ(reg);
  3385. udelay(150);
  3386. reg = FDI_RX_IIR(pipe);
  3387. for (tries = 0; tries < 5; tries++) {
  3388. temp = I915_READ(reg);
  3389. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3390. if (temp & FDI_RX_SYMBOL_LOCK) {
  3391. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3392. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3393. break;
  3394. }
  3395. }
  3396. if (tries == 5)
  3397. DRM_ERROR("FDI train 2 fail!\n");
  3398. DRM_DEBUG_KMS("FDI train done\n");
  3399. }
  3400. static const int snb_b_fdi_train_param[] = {
  3401. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3402. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3403. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3404. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3405. };
  3406. /* The FDI link training functions for SNB/Cougarpoint. */
  3407. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3408. const struct intel_crtc_state *crtc_state)
  3409. {
  3410. struct drm_device *dev = crtc->base.dev;
  3411. struct drm_i915_private *dev_priv = to_i915(dev);
  3412. int pipe = crtc->pipe;
  3413. i915_reg_t reg;
  3414. u32 temp, i, retry;
  3415. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3416. for train result */
  3417. reg = FDI_RX_IMR(pipe);
  3418. temp = I915_READ(reg);
  3419. temp &= ~FDI_RX_SYMBOL_LOCK;
  3420. temp &= ~FDI_RX_BIT_LOCK;
  3421. I915_WRITE(reg, temp);
  3422. POSTING_READ(reg);
  3423. udelay(150);
  3424. /* enable CPU FDI TX and PCH FDI RX */
  3425. reg = FDI_TX_CTL(pipe);
  3426. temp = I915_READ(reg);
  3427. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3428. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3429. temp &= ~FDI_LINK_TRAIN_NONE;
  3430. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3431. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3432. /* SNB-B */
  3433. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3434. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3435. I915_WRITE(FDI_RX_MISC(pipe),
  3436. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3437. reg = FDI_RX_CTL(pipe);
  3438. temp = I915_READ(reg);
  3439. if (HAS_PCH_CPT(dev_priv)) {
  3440. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3441. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3442. } else {
  3443. temp &= ~FDI_LINK_TRAIN_NONE;
  3444. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3445. }
  3446. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3447. POSTING_READ(reg);
  3448. udelay(150);
  3449. for (i = 0; i < 4; i++) {
  3450. reg = FDI_TX_CTL(pipe);
  3451. temp = I915_READ(reg);
  3452. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3453. temp |= snb_b_fdi_train_param[i];
  3454. I915_WRITE(reg, temp);
  3455. POSTING_READ(reg);
  3456. udelay(500);
  3457. for (retry = 0; retry < 5; retry++) {
  3458. reg = FDI_RX_IIR(pipe);
  3459. temp = I915_READ(reg);
  3460. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3461. if (temp & FDI_RX_BIT_LOCK) {
  3462. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3463. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3464. break;
  3465. }
  3466. udelay(50);
  3467. }
  3468. if (retry < 5)
  3469. break;
  3470. }
  3471. if (i == 4)
  3472. DRM_ERROR("FDI train 1 fail!\n");
  3473. /* Train 2 */
  3474. reg = FDI_TX_CTL(pipe);
  3475. temp = I915_READ(reg);
  3476. temp &= ~FDI_LINK_TRAIN_NONE;
  3477. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3478. if (IS_GEN6(dev_priv)) {
  3479. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3480. /* SNB-B */
  3481. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3482. }
  3483. I915_WRITE(reg, temp);
  3484. reg = FDI_RX_CTL(pipe);
  3485. temp = I915_READ(reg);
  3486. if (HAS_PCH_CPT(dev_priv)) {
  3487. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3488. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3489. } else {
  3490. temp &= ~FDI_LINK_TRAIN_NONE;
  3491. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3492. }
  3493. I915_WRITE(reg, temp);
  3494. POSTING_READ(reg);
  3495. udelay(150);
  3496. for (i = 0; i < 4; i++) {
  3497. reg = FDI_TX_CTL(pipe);
  3498. temp = I915_READ(reg);
  3499. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3500. temp |= snb_b_fdi_train_param[i];
  3501. I915_WRITE(reg, temp);
  3502. POSTING_READ(reg);
  3503. udelay(500);
  3504. for (retry = 0; retry < 5; retry++) {
  3505. reg = FDI_RX_IIR(pipe);
  3506. temp = I915_READ(reg);
  3507. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3508. if (temp & FDI_RX_SYMBOL_LOCK) {
  3509. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3510. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3511. break;
  3512. }
  3513. udelay(50);
  3514. }
  3515. if (retry < 5)
  3516. break;
  3517. }
  3518. if (i == 4)
  3519. DRM_ERROR("FDI train 2 fail!\n");
  3520. DRM_DEBUG_KMS("FDI train done.\n");
  3521. }
  3522. /* Manual link training for Ivy Bridge A0 parts */
  3523. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3524. const struct intel_crtc_state *crtc_state)
  3525. {
  3526. struct drm_device *dev = crtc->base.dev;
  3527. struct drm_i915_private *dev_priv = to_i915(dev);
  3528. int pipe = crtc->pipe;
  3529. i915_reg_t reg;
  3530. u32 temp, i, j;
  3531. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3532. for train result */
  3533. reg = FDI_RX_IMR(pipe);
  3534. temp = I915_READ(reg);
  3535. temp &= ~FDI_RX_SYMBOL_LOCK;
  3536. temp &= ~FDI_RX_BIT_LOCK;
  3537. I915_WRITE(reg, temp);
  3538. POSTING_READ(reg);
  3539. udelay(150);
  3540. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3541. I915_READ(FDI_RX_IIR(pipe)));
  3542. /* Try each vswing and preemphasis setting twice before moving on */
  3543. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3544. /* disable first in case we need to retry */
  3545. reg = FDI_TX_CTL(pipe);
  3546. temp = I915_READ(reg);
  3547. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3548. temp &= ~FDI_TX_ENABLE;
  3549. I915_WRITE(reg, temp);
  3550. reg = FDI_RX_CTL(pipe);
  3551. temp = I915_READ(reg);
  3552. temp &= ~FDI_LINK_TRAIN_AUTO;
  3553. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3554. temp &= ~FDI_RX_ENABLE;
  3555. I915_WRITE(reg, temp);
  3556. /* enable CPU FDI TX and PCH FDI RX */
  3557. reg = FDI_TX_CTL(pipe);
  3558. temp = I915_READ(reg);
  3559. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3560. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3561. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3562. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3563. temp |= snb_b_fdi_train_param[j/2];
  3564. temp |= FDI_COMPOSITE_SYNC;
  3565. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3566. I915_WRITE(FDI_RX_MISC(pipe),
  3567. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3568. reg = FDI_RX_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3571. temp |= FDI_COMPOSITE_SYNC;
  3572. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3573. POSTING_READ(reg);
  3574. udelay(1); /* should be 0.5us */
  3575. for (i = 0; i < 4; i++) {
  3576. reg = FDI_RX_IIR(pipe);
  3577. temp = I915_READ(reg);
  3578. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3579. if (temp & FDI_RX_BIT_LOCK ||
  3580. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3581. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3582. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3583. i);
  3584. break;
  3585. }
  3586. udelay(1); /* should be 0.5us */
  3587. }
  3588. if (i == 4) {
  3589. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3590. continue;
  3591. }
  3592. /* Train 2 */
  3593. reg = FDI_TX_CTL(pipe);
  3594. temp = I915_READ(reg);
  3595. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3596. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3597. I915_WRITE(reg, temp);
  3598. reg = FDI_RX_CTL(pipe);
  3599. temp = I915_READ(reg);
  3600. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3601. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3602. I915_WRITE(reg, temp);
  3603. POSTING_READ(reg);
  3604. udelay(2); /* should be 1.5us */
  3605. for (i = 0; i < 4; i++) {
  3606. reg = FDI_RX_IIR(pipe);
  3607. temp = I915_READ(reg);
  3608. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3609. if (temp & FDI_RX_SYMBOL_LOCK ||
  3610. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3611. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3612. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3613. i);
  3614. goto train_done;
  3615. }
  3616. udelay(2); /* should be 1.5us */
  3617. }
  3618. if (i == 4)
  3619. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3620. }
  3621. train_done:
  3622. DRM_DEBUG_KMS("FDI train done.\n");
  3623. }
  3624. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3625. {
  3626. struct drm_device *dev = intel_crtc->base.dev;
  3627. struct drm_i915_private *dev_priv = to_i915(dev);
  3628. int pipe = intel_crtc->pipe;
  3629. i915_reg_t reg;
  3630. u32 temp;
  3631. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3632. reg = FDI_RX_CTL(pipe);
  3633. temp = I915_READ(reg);
  3634. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3635. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3636. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3637. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3638. POSTING_READ(reg);
  3639. udelay(200);
  3640. /* Switch from Rawclk to PCDclk */
  3641. temp = I915_READ(reg);
  3642. I915_WRITE(reg, temp | FDI_PCDCLK);
  3643. POSTING_READ(reg);
  3644. udelay(200);
  3645. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3646. reg = FDI_TX_CTL(pipe);
  3647. temp = I915_READ(reg);
  3648. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3649. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3650. POSTING_READ(reg);
  3651. udelay(100);
  3652. }
  3653. }
  3654. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3655. {
  3656. struct drm_device *dev = intel_crtc->base.dev;
  3657. struct drm_i915_private *dev_priv = to_i915(dev);
  3658. int pipe = intel_crtc->pipe;
  3659. i915_reg_t reg;
  3660. u32 temp;
  3661. /* Switch from PCDclk to Rawclk */
  3662. reg = FDI_RX_CTL(pipe);
  3663. temp = I915_READ(reg);
  3664. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3665. /* Disable CPU FDI TX PLL */
  3666. reg = FDI_TX_CTL(pipe);
  3667. temp = I915_READ(reg);
  3668. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3669. POSTING_READ(reg);
  3670. udelay(100);
  3671. reg = FDI_RX_CTL(pipe);
  3672. temp = I915_READ(reg);
  3673. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3674. /* Wait for the clocks to turn off. */
  3675. POSTING_READ(reg);
  3676. udelay(100);
  3677. }
  3678. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3679. {
  3680. struct drm_device *dev = crtc->dev;
  3681. struct drm_i915_private *dev_priv = to_i915(dev);
  3682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3683. int pipe = intel_crtc->pipe;
  3684. i915_reg_t reg;
  3685. u32 temp;
  3686. /* disable CPU FDI tx and PCH FDI rx */
  3687. reg = FDI_TX_CTL(pipe);
  3688. temp = I915_READ(reg);
  3689. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3690. POSTING_READ(reg);
  3691. reg = FDI_RX_CTL(pipe);
  3692. temp = I915_READ(reg);
  3693. temp &= ~(0x7 << 16);
  3694. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3695. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3696. POSTING_READ(reg);
  3697. udelay(100);
  3698. /* Ironlake workaround, disable clock pointer after downing FDI */
  3699. if (HAS_PCH_IBX(dev_priv))
  3700. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3701. /* still set train pattern 1 */
  3702. reg = FDI_TX_CTL(pipe);
  3703. temp = I915_READ(reg);
  3704. temp &= ~FDI_LINK_TRAIN_NONE;
  3705. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3706. I915_WRITE(reg, temp);
  3707. reg = FDI_RX_CTL(pipe);
  3708. temp = I915_READ(reg);
  3709. if (HAS_PCH_CPT(dev_priv)) {
  3710. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3711. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3712. } else {
  3713. temp &= ~FDI_LINK_TRAIN_NONE;
  3714. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3715. }
  3716. /* BPC in FDI rx is consistent with that in PIPECONF */
  3717. temp &= ~(0x07 << 16);
  3718. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3719. I915_WRITE(reg, temp);
  3720. POSTING_READ(reg);
  3721. udelay(100);
  3722. }
  3723. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3724. {
  3725. struct drm_crtc *crtc;
  3726. bool cleanup_done;
  3727. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3728. struct drm_crtc_commit *commit;
  3729. spin_lock(&crtc->commit_lock);
  3730. commit = list_first_entry_or_null(&crtc->commit_list,
  3731. struct drm_crtc_commit, commit_entry);
  3732. cleanup_done = commit ?
  3733. try_wait_for_completion(&commit->cleanup_done) : true;
  3734. spin_unlock(&crtc->commit_lock);
  3735. if (cleanup_done)
  3736. continue;
  3737. drm_crtc_wait_one_vblank(crtc);
  3738. return true;
  3739. }
  3740. return false;
  3741. }
  3742. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3743. {
  3744. u32 temp;
  3745. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3746. mutex_lock(&dev_priv->sb_lock);
  3747. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3748. temp |= SBI_SSCCTL_DISABLE;
  3749. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3750. mutex_unlock(&dev_priv->sb_lock);
  3751. }
  3752. /* Program iCLKIP clock to the desired frequency */
  3753. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3754. {
  3755. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3756. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3757. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3758. u32 temp;
  3759. lpt_disable_iclkip(dev_priv);
  3760. /* The iCLK virtual clock root frequency is in MHz,
  3761. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3762. * divisors, it is necessary to divide one by another, so we
  3763. * convert the virtual clock precision to KHz here for higher
  3764. * precision.
  3765. */
  3766. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3767. u32 iclk_virtual_root_freq = 172800 * 1000;
  3768. u32 iclk_pi_range = 64;
  3769. u32 desired_divisor;
  3770. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3771. clock << auxdiv);
  3772. divsel = (desired_divisor / iclk_pi_range) - 2;
  3773. phaseinc = desired_divisor % iclk_pi_range;
  3774. /*
  3775. * Near 20MHz is a corner case which is
  3776. * out of range for the 7-bit divisor
  3777. */
  3778. if (divsel <= 0x7f)
  3779. break;
  3780. }
  3781. /* This should not happen with any sane values */
  3782. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3783. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3784. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3785. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3786. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3787. clock,
  3788. auxdiv,
  3789. divsel,
  3790. phasedir,
  3791. phaseinc);
  3792. mutex_lock(&dev_priv->sb_lock);
  3793. /* Program SSCDIVINTPHASE6 */
  3794. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3795. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3796. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3797. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3798. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3799. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3800. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3801. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3802. /* Program SSCAUXDIV */
  3803. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3804. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3805. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3806. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3807. /* Enable modulator and associated divider */
  3808. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3809. temp &= ~SBI_SSCCTL_DISABLE;
  3810. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3811. mutex_unlock(&dev_priv->sb_lock);
  3812. /* Wait for initialization time */
  3813. udelay(24);
  3814. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3815. }
  3816. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3817. {
  3818. u32 divsel, phaseinc, auxdiv;
  3819. u32 iclk_virtual_root_freq = 172800 * 1000;
  3820. u32 iclk_pi_range = 64;
  3821. u32 desired_divisor;
  3822. u32 temp;
  3823. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3824. return 0;
  3825. mutex_lock(&dev_priv->sb_lock);
  3826. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3827. if (temp & SBI_SSCCTL_DISABLE) {
  3828. mutex_unlock(&dev_priv->sb_lock);
  3829. return 0;
  3830. }
  3831. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3832. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3833. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3834. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3835. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3836. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3837. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3838. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3839. mutex_unlock(&dev_priv->sb_lock);
  3840. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3841. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3842. desired_divisor << auxdiv);
  3843. }
  3844. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3845. enum pipe pch_transcoder)
  3846. {
  3847. struct drm_device *dev = crtc->base.dev;
  3848. struct drm_i915_private *dev_priv = to_i915(dev);
  3849. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3850. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3851. I915_READ(HTOTAL(cpu_transcoder)));
  3852. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3853. I915_READ(HBLANK(cpu_transcoder)));
  3854. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3855. I915_READ(HSYNC(cpu_transcoder)));
  3856. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3857. I915_READ(VTOTAL(cpu_transcoder)));
  3858. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3859. I915_READ(VBLANK(cpu_transcoder)));
  3860. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3861. I915_READ(VSYNC(cpu_transcoder)));
  3862. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3863. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3864. }
  3865. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3866. {
  3867. struct drm_i915_private *dev_priv = to_i915(dev);
  3868. uint32_t temp;
  3869. temp = I915_READ(SOUTH_CHICKEN1);
  3870. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3871. return;
  3872. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3873. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3874. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3875. if (enable)
  3876. temp |= FDI_BC_BIFURCATION_SELECT;
  3877. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3878. I915_WRITE(SOUTH_CHICKEN1, temp);
  3879. POSTING_READ(SOUTH_CHICKEN1);
  3880. }
  3881. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3882. {
  3883. struct drm_device *dev = intel_crtc->base.dev;
  3884. switch (intel_crtc->pipe) {
  3885. case PIPE_A:
  3886. break;
  3887. case PIPE_B:
  3888. if (intel_crtc->config->fdi_lanes > 2)
  3889. cpt_set_fdi_bc_bifurcation(dev, false);
  3890. else
  3891. cpt_set_fdi_bc_bifurcation(dev, true);
  3892. break;
  3893. case PIPE_C:
  3894. cpt_set_fdi_bc_bifurcation(dev, true);
  3895. break;
  3896. default:
  3897. BUG();
  3898. }
  3899. }
  3900. /* Return which DP Port should be selected for Transcoder DP control */
  3901. static enum port
  3902. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3903. {
  3904. struct drm_device *dev = crtc->base.dev;
  3905. struct intel_encoder *encoder;
  3906. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3907. if (encoder->type == INTEL_OUTPUT_DP ||
  3908. encoder->type == INTEL_OUTPUT_EDP)
  3909. return enc_to_dig_port(&encoder->base)->port;
  3910. }
  3911. return -1;
  3912. }
  3913. /*
  3914. * Enable PCH resources required for PCH ports:
  3915. * - PCH PLLs
  3916. * - FDI training & RX/TX
  3917. * - update transcoder timings
  3918. * - DP transcoding bits
  3919. * - transcoder
  3920. */
  3921. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3922. {
  3923. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3924. struct drm_device *dev = crtc->base.dev;
  3925. struct drm_i915_private *dev_priv = to_i915(dev);
  3926. int pipe = crtc->pipe;
  3927. u32 temp;
  3928. assert_pch_transcoder_disabled(dev_priv, pipe);
  3929. if (IS_IVYBRIDGE(dev_priv))
  3930. ivybridge_update_fdi_bc_bifurcation(crtc);
  3931. /* Write the TU size bits before fdi link training, so that error
  3932. * detection works. */
  3933. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3934. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3935. /* For PCH output, training FDI link */
  3936. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3937. /* We need to program the right clock selection before writing the pixel
  3938. * mutliplier into the DPLL. */
  3939. if (HAS_PCH_CPT(dev_priv)) {
  3940. u32 sel;
  3941. temp = I915_READ(PCH_DPLL_SEL);
  3942. temp |= TRANS_DPLL_ENABLE(pipe);
  3943. sel = TRANS_DPLLB_SEL(pipe);
  3944. if (crtc_state->shared_dpll ==
  3945. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3946. temp |= sel;
  3947. else
  3948. temp &= ~sel;
  3949. I915_WRITE(PCH_DPLL_SEL, temp);
  3950. }
  3951. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3952. * transcoder, and we actually should do this to not upset any PCH
  3953. * transcoder that already use the clock when we share it.
  3954. *
  3955. * Note that enable_shared_dpll tries to do the right thing, but
  3956. * get_shared_dpll unconditionally resets the pll - we need that to have
  3957. * the right LVDS enable sequence. */
  3958. intel_enable_shared_dpll(crtc);
  3959. /* set transcoder timing, panel must allow it */
  3960. assert_panel_unlocked(dev_priv, pipe);
  3961. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3962. intel_fdi_normal_train(crtc);
  3963. /* For PCH DP, enable TRANS_DP_CTL */
  3964. if (HAS_PCH_CPT(dev_priv) &&
  3965. intel_crtc_has_dp_encoder(crtc_state)) {
  3966. const struct drm_display_mode *adjusted_mode =
  3967. &crtc_state->base.adjusted_mode;
  3968. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3969. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3970. temp = I915_READ(reg);
  3971. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3972. TRANS_DP_SYNC_MASK |
  3973. TRANS_DP_BPC_MASK);
  3974. temp |= TRANS_DP_OUTPUT_ENABLE;
  3975. temp |= bpc << 9; /* same format but at 11:9 */
  3976. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3977. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3978. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3979. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3980. switch (intel_trans_dp_port_sel(crtc)) {
  3981. case PORT_B:
  3982. temp |= TRANS_DP_PORT_SEL_B;
  3983. break;
  3984. case PORT_C:
  3985. temp |= TRANS_DP_PORT_SEL_C;
  3986. break;
  3987. case PORT_D:
  3988. temp |= TRANS_DP_PORT_SEL_D;
  3989. break;
  3990. default:
  3991. BUG();
  3992. }
  3993. I915_WRITE(reg, temp);
  3994. }
  3995. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3996. }
  3997. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3998. {
  3999. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4000. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4001. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  4002. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4003. lpt_program_iclkip(crtc);
  4004. /* Set transcoder timing. */
  4005. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4006. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4007. }
  4008. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4009. {
  4010. struct drm_i915_private *dev_priv = to_i915(dev);
  4011. i915_reg_t dslreg = PIPEDSL(pipe);
  4012. u32 temp;
  4013. temp = I915_READ(dslreg);
  4014. udelay(500);
  4015. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4016. if (wait_for(I915_READ(dslreg) != temp, 5))
  4017. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4018. }
  4019. }
  4020. static int
  4021. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4022. unsigned int scaler_user, int *scaler_id,
  4023. int src_w, int src_h, int dst_w, int dst_h)
  4024. {
  4025. struct intel_crtc_scaler_state *scaler_state =
  4026. &crtc_state->scaler_state;
  4027. struct intel_crtc *intel_crtc =
  4028. to_intel_crtc(crtc_state->base.crtc);
  4029. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4030. const struct drm_display_mode *adjusted_mode =
  4031. &crtc_state->base.adjusted_mode;
  4032. int need_scaling;
  4033. /*
  4034. * Src coordinates are already rotated by 270 degrees for
  4035. * the 90/270 degree plane rotation cases (to match the
  4036. * GTT mapping), hence no need to account for rotation here.
  4037. */
  4038. need_scaling = src_w != dst_w || src_h != dst_h;
  4039. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4040. need_scaling = true;
  4041. /*
  4042. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4043. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4044. * Once NV12 is enabled, handle it here while allocating scaler
  4045. * for NV12.
  4046. */
  4047. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4048. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4049. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4050. return -EINVAL;
  4051. }
  4052. /*
  4053. * if plane is being disabled or scaler is no more required or force detach
  4054. * - free scaler binded to this plane/crtc
  4055. * - in order to do this, update crtc->scaler_usage
  4056. *
  4057. * Here scaler state in crtc_state is set free so that
  4058. * scaler can be assigned to other user. Actual register
  4059. * update to free the scaler is done in plane/panel-fit programming.
  4060. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4061. */
  4062. if (force_detach || !need_scaling) {
  4063. if (*scaler_id >= 0) {
  4064. scaler_state->scaler_users &= ~(1 << scaler_user);
  4065. scaler_state->scalers[*scaler_id].in_use = 0;
  4066. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4067. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4068. intel_crtc->pipe, scaler_user, *scaler_id,
  4069. scaler_state->scaler_users);
  4070. *scaler_id = -1;
  4071. }
  4072. return 0;
  4073. }
  4074. /* range checks */
  4075. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4076. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4077. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4078. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4079. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4080. "size is out of scaler range\n",
  4081. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4082. return -EINVAL;
  4083. }
  4084. /* mark this plane as a scaler user in crtc_state */
  4085. scaler_state->scaler_users |= (1 << scaler_user);
  4086. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4087. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4088. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4089. scaler_state->scaler_users);
  4090. return 0;
  4091. }
  4092. /**
  4093. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4094. *
  4095. * @state: crtc's scaler state
  4096. *
  4097. * Return
  4098. * 0 - scaler_usage updated successfully
  4099. * error - requested scaling cannot be supported or other error condition
  4100. */
  4101. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4102. {
  4103. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4104. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4105. &state->scaler_state.scaler_id,
  4106. state->pipe_src_w, state->pipe_src_h,
  4107. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4108. }
  4109. /**
  4110. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4111. *
  4112. * @state: crtc's scaler state
  4113. * @plane_state: atomic plane state to update
  4114. *
  4115. * Return
  4116. * 0 - scaler_usage updated successfully
  4117. * error - requested scaling cannot be supported or other error condition
  4118. */
  4119. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4120. struct intel_plane_state *plane_state)
  4121. {
  4122. struct intel_plane *intel_plane =
  4123. to_intel_plane(plane_state->base.plane);
  4124. struct drm_framebuffer *fb = plane_state->base.fb;
  4125. int ret;
  4126. bool force_detach = !fb || !plane_state->base.visible;
  4127. ret = skl_update_scaler(crtc_state, force_detach,
  4128. drm_plane_index(&intel_plane->base),
  4129. &plane_state->scaler_id,
  4130. drm_rect_width(&plane_state->base.src) >> 16,
  4131. drm_rect_height(&plane_state->base.src) >> 16,
  4132. drm_rect_width(&plane_state->base.dst),
  4133. drm_rect_height(&plane_state->base.dst));
  4134. if (ret || plane_state->scaler_id < 0)
  4135. return ret;
  4136. /* check colorkey */
  4137. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4138. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4139. intel_plane->base.base.id,
  4140. intel_plane->base.name);
  4141. return -EINVAL;
  4142. }
  4143. /* Check src format */
  4144. switch (fb->format->format) {
  4145. case DRM_FORMAT_RGB565:
  4146. case DRM_FORMAT_XBGR8888:
  4147. case DRM_FORMAT_XRGB8888:
  4148. case DRM_FORMAT_ABGR8888:
  4149. case DRM_FORMAT_ARGB8888:
  4150. case DRM_FORMAT_XRGB2101010:
  4151. case DRM_FORMAT_XBGR2101010:
  4152. case DRM_FORMAT_YUYV:
  4153. case DRM_FORMAT_YVYU:
  4154. case DRM_FORMAT_UYVY:
  4155. case DRM_FORMAT_VYUY:
  4156. break;
  4157. default:
  4158. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4159. intel_plane->base.base.id, intel_plane->base.name,
  4160. fb->base.id, fb->format->format);
  4161. return -EINVAL;
  4162. }
  4163. return 0;
  4164. }
  4165. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4166. {
  4167. int i;
  4168. for (i = 0; i < crtc->num_scalers; i++)
  4169. skl_detach_scaler(crtc, i);
  4170. }
  4171. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4172. {
  4173. struct drm_device *dev = crtc->base.dev;
  4174. struct drm_i915_private *dev_priv = to_i915(dev);
  4175. int pipe = crtc->pipe;
  4176. struct intel_crtc_scaler_state *scaler_state =
  4177. &crtc->config->scaler_state;
  4178. if (crtc->config->pch_pfit.enabled) {
  4179. int id;
  4180. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4181. return;
  4182. id = scaler_state->scaler_id;
  4183. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4184. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4185. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4186. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4187. }
  4188. }
  4189. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4190. {
  4191. struct drm_device *dev = crtc->base.dev;
  4192. struct drm_i915_private *dev_priv = to_i915(dev);
  4193. int pipe = crtc->pipe;
  4194. if (crtc->config->pch_pfit.enabled) {
  4195. /* Force use of hard-coded filter coefficients
  4196. * as some pre-programmed values are broken,
  4197. * e.g. x201.
  4198. */
  4199. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4200. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4201. PF_PIPE_SEL_IVB(pipe));
  4202. else
  4203. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4204. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4205. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4206. }
  4207. }
  4208. void hsw_enable_ips(struct intel_crtc *crtc)
  4209. {
  4210. struct drm_device *dev = crtc->base.dev;
  4211. struct drm_i915_private *dev_priv = to_i915(dev);
  4212. if (!crtc->config->ips_enabled)
  4213. return;
  4214. /*
  4215. * We can only enable IPS after we enable a plane and wait for a vblank
  4216. * This function is called from post_plane_update, which is run after
  4217. * a vblank wait.
  4218. */
  4219. assert_plane_enabled(dev_priv, crtc->plane);
  4220. if (IS_BROADWELL(dev_priv)) {
  4221. mutex_lock(&dev_priv->rps.hw_lock);
  4222. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4223. mutex_unlock(&dev_priv->rps.hw_lock);
  4224. /* Quoting Art Runyan: "its not safe to expect any particular
  4225. * value in IPS_CTL bit 31 after enabling IPS through the
  4226. * mailbox." Moreover, the mailbox may return a bogus state,
  4227. * so we need to just enable it and continue on.
  4228. */
  4229. } else {
  4230. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4231. /* The bit only becomes 1 in the next vblank, so this wait here
  4232. * is essentially intel_wait_for_vblank. If we don't have this
  4233. * and don't wait for vblanks until the end of crtc_enable, then
  4234. * the HW state readout code will complain that the expected
  4235. * IPS_CTL value is not the one we read. */
  4236. if (intel_wait_for_register(dev_priv,
  4237. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4238. 50))
  4239. DRM_ERROR("Timed out waiting for IPS enable\n");
  4240. }
  4241. }
  4242. void hsw_disable_ips(struct intel_crtc *crtc)
  4243. {
  4244. struct drm_device *dev = crtc->base.dev;
  4245. struct drm_i915_private *dev_priv = to_i915(dev);
  4246. if (!crtc->config->ips_enabled)
  4247. return;
  4248. assert_plane_enabled(dev_priv, crtc->plane);
  4249. if (IS_BROADWELL(dev_priv)) {
  4250. mutex_lock(&dev_priv->rps.hw_lock);
  4251. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4252. mutex_unlock(&dev_priv->rps.hw_lock);
  4253. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4254. if (intel_wait_for_register(dev_priv,
  4255. IPS_CTL, IPS_ENABLE, 0,
  4256. 42))
  4257. DRM_ERROR("Timed out waiting for IPS disable\n");
  4258. } else {
  4259. I915_WRITE(IPS_CTL, 0);
  4260. POSTING_READ(IPS_CTL);
  4261. }
  4262. /* We need to wait for a vblank before we can disable the plane. */
  4263. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4264. }
  4265. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4266. {
  4267. if (intel_crtc->overlay) {
  4268. struct drm_device *dev = intel_crtc->base.dev;
  4269. mutex_lock(&dev->struct_mutex);
  4270. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4271. mutex_unlock(&dev->struct_mutex);
  4272. }
  4273. /* Let userspace switch the overlay on again. In most cases userspace
  4274. * has to recompute where to put it anyway.
  4275. */
  4276. }
  4277. /**
  4278. * intel_post_enable_primary - Perform operations after enabling primary plane
  4279. * @crtc: the CRTC whose primary plane was just enabled
  4280. *
  4281. * Performs potentially sleeping operations that must be done after the primary
  4282. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4283. * called due to an explicit primary plane update, or due to an implicit
  4284. * re-enable that is caused when a sprite plane is updated to no longer
  4285. * completely hide the primary plane.
  4286. */
  4287. static void
  4288. intel_post_enable_primary(struct drm_crtc *crtc)
  4289. {
  4290. struct drm_device *dev = crtc->dev;
  4291. struct drm_i915_private *dev_priv = to_i915(dev);
  4292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4293. int pipe = intel_crtc->pipe;
  4294. /*
  4295. * FIXME IPS should be fine as long as one plane is
  4296. * enabled, but in practice it seems to have problems
  4297. * when going from primary only to sprite only and vice
  4298. * versa.
  4299. */
  4300. hsw_enable_ips(intel_crtc);
  4301. /*
  4302. * Gen2 reports pipe underruns whenever all planes are disabled.
  4303. * So don't enable underrun reporting before at least some planes
  4304. * are enabled.
  4305. * FIXME: Need to fix the logic to work when we turn off all planes
  4306. * but leave the pipe running.
  4307. */
  4308. if (IS_GEN2(dev_priv))
  4309. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4310. /* Underruns don't always raise interrupts, so check manually. */
  4311. intel_check_cpu_fifo_underruns(dev_priv);
  4312. intel_check_pch_fifo_underruns(dev_priv);
  4313. }
  4314. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4315. static void
  4316. intel_pre_disable_primary(struct drm_crtc *crtc)
  4317. {
  4318. struct drm_device *dev = crtc->dev;
  4319. struct drm_i915_private *dev_priv = to_i915(dev);
  4320. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4321. int pipe = intel_crtc->pipe;
  4322. /*
  4323. * Gen2 reports pipe underruns whenever all planes are disabled.
  4324. * So diasble underrun reporting before all the planes get disabled.
  4325. * FIXME: Need to fix the logic to work when we turn off all planes
  4326. * but leave the pipe running.
  4327. */
  4328. if (IS_GEN2(dev_priv))
  4329. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4330. /*
  4331. * FIXME IPS should be fine as long as one plane is
  4332. * enabled, but in practice it seems to have problems
  4333. * when going from primary only to sprite only and vice
  4334. * versa.
  4335. */
  4336. hsw_disable_ips(intel_crtc);
  4337. }
  4338. /* FIXME get rid of this and use pre_plane_update */
  4339. static void
  4340. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4341. {
  4342. struct drm_device *dev = crtc->dev;
  4343. struct drm_i915_private *dev_priv = to_i915(dev);
  4344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4345. int pipe = intel_crtc->pipe;
  4346. intel_pre_disable_primary(crtc);
  4347. /*
  4348. * Vblank time updates from the shadow to live plane control register
  4349. * are blocked if the memory self-refresh mode is active at that
  4350. * moment. So to make sure the plane gets truly disabled, disable
  4351. * first the self-refresh mode. The self-refresh enable bit in turn
  4352. * will be checked/applied by the HW only at the next frame start
  4353. * event which is after the vblank start event, so we need to have a
  4354. * wait-for-vblank between disabling the plane and the pipe.
  4355. */
  4356. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4357. intel_set_memory_cxsr(dev_priv, false))
  4358. intel_wait_for_vblank(dev_priv, pipe);
  4359. }
  4360. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4361. {
  4362. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4363. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4364. struct intel_crtc_state *pipe_config =
  4365. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4366. crtc);
  4367. struct drm_plane *primary = crtc->base.primary;
  4368. struct drm_plane_state *old_pri_state =
  4369. drm_atomic_get_existing_plane_state(old_state, primary);
  4370. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4371. if (pipe_config->update_wm_post && pipe_config->base.active)
  4372. intel_update_watermarks(crtc);
  4373. if (old_pri_state) {
  4374. struct intel_plane_state *primary_state =
  4375. intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
  4376. to_intel_plane(primary));
  4377. struct intel_plane_state *old_primary_state =
  4378. to_intel_plane_state(old_pri_state);
  4379. intel_fbc_post_update(crtc);
  4380. if (primary_state->base.visible &&
  4381. (needs_modeset(&pipe_config->base) ||
  4382. !old_primary_state->base.visible))
  4383. intel_post_enable_primary(&crtc->base);
  4384. }
  4385. }
  4386. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4387. struct intel_crtc_state *pipe_config)
  4388. {
  4389. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4390. struct drm_device *dev = crtc->base.dev;
  4391. struct drm_i915_private *dev_priv = to_i915(dev);
  4392. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4393. struct drm_plane *primary = crtc->base.primary;
  4394. struct drm_plane_state *old_pri_state =
  4395. drm_atomic_get_existing_plane_state(old_state, primary);
  4396. bool modeset = needs_modeset(&pipe_config->base);
  4397. struct intel_atomic_state *old_intel_state =
  4398. to_intel_atomic_state(old_state);
  4399. if (old_pri_state) {
  4400. struct intel_plane_state *primary_state =
  4401. intel_atomic_get_new_plane_state(old_intel_state,
  4402. to_intel_plane(primary));
  4403. struct intel_plane_state *old_primary_state =
  4404. to_intel_plane_state(old_pri_state);
  4405. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4406. if (old_primary_state->base.visible &&
  4407. (modeset || !primary_state->base.visible))
  4408. intel_pre_disable_primary(&crtc->base);
  4409. }
  4410. /*
  4411. * Vblank time updates from the shadow to live plane control register
  4412. * are blocked if the memory self-refresh mode is active at that
  4413. * moment. So to make sure the plane gets truly disabled, disable
  4414. * first the self-refresh mode. The self-refresh enable bit in turn
  4415. * will be checked/applied by the HW only at the next frame start
  4416. * event which is after the vblank start event, so we need to have a
  4417. * wait-for-vblank between disabling the plane and the pipe.
  4418. */
  4419. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4420. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4421. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4422. /*
  4423. * IVB workaround: must disable low power watermarks for at least
  4424. * one frame before enabling scaling. LP watermarks can be re-enabled
  4425. * when scaling is disabled.
  4426. *
  4427. * WaCxSRDisabledForSpriteScaling:ivb
  4428. */
  4429. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4430. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4431. /*
  4432. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4433. * watermark programming here.
  4434. */
  4435. if (needs_modeset(&pipe_config->base))
  4436. return;
  4437. /*
  4438. * For platforms that support atomic watermarks, program the
  4439. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4440. * will be the intermediate values that are safe for both pre- and
  4441. * post- vblank; when vblank happens, the 'active' values will be set
  4442. * to the final 'target' values and we'll do this again to get the
  4443. * optimal watermarks. For gen9+ platforms, the values we program here
  4444. * will be the final target values which will get automatically latched
  4445. * at vblank time; no further programming will be necessary.
  4446. *
  4447. * If a platform hasn't been transitioned to atomic watermarks yet,
  4448. * we'll continue to update watermarks the old way, if flags tell
  4449. * us to.
  4450. */
  4451. if (dev_priv->display.initial_watermarks != NULL)
  4452. dev_priv->display.initial_watermarks(old_intel_state,
  4453. pipe_config);
  4454. else if (pipe_config->update_wm_pre)
  4455. intel_update_watermarks(crtc);
  4456. }
  4457. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4458. {
  4459. struct drm_device *dev = crtc->dev;
  4460. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4461. struct drm_plane *p;
  4462. int pipe = intel_crtc->pipe;
  4463. intel_crtc_dpms_overlay_disable(intel_crtc);
  4464. drm_for_each_plane_mask(p, dev, plane_mask)
  4465. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4466. /*
  4467. * FIXME: Once we grow proper nuclear flip support out of this we need
  4468. * to compute the mask of flip planes precisely. For the time being
  4469. * consider this a flip to a NULL plane.
  4470. */
  4471. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4472. }
  4473. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4474. struct intel_crtc_state *crtc_state,
  4475. struct drm_atomic_state *old_state)
  4476. {
  4477. struct drm_connector_state *conn_state;
  4478. struct drm_connector *conn;
  4479. int i;
  4480. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4481. struct intel_encoder *encoder =
  4482. to_intel_encoder(conn_state->best_encoder);
  4483. if (conn_state->crtc != crtc)
  4484. continue;
  4485. if (encoder->pre_pll_enable)
  4486. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4487. }
  4488. }
  4489. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4490. struct intel_crtc_state *crtc_state,
  4491. struct drm_atomic_state *old_state)
  4492. {
  4493. struct drm_connector_state *conn_state;
  4494. struct drm_connector *conn;
  4495. int i;
  4496. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4497. struct intel_encoder *encoder =
  4498. to_intel_encoder(conn_state->best_encoder);
  4499. if (conn_state->crtc != crtc)
  4500. continue;
  4501. if (encoder->pre_enable)
  4502. encoder->pre_enable(encoder, crtc_state, conn_state);
  4503. }
  4504. }
  4505. static void intel_encoders_enable(struct drm_crtc *crtc,
  4506. struct intel_crtc_state *crtc_state,
  4507. struct drm_atomic_state *old_state)
  4508. {
  4509. struct drm_connector_state *conn_state;
  4510. struct drm_connector *conn;
  4511. int i;
  4512. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4513. struct intel_encoder *encoder =
  4514. to_intel_encoder(conn_state->best_encoder);
  4515. if (conn_state->crtc != crtc)
  4516. continue;
  4517. encoder->enable(encoder, crtc_state, conn_state);
  4518. intel_opregion_notify_encoder(encoder, true);
  4519. }
  4520. }
  4521. static void intel_encoders_disable(struct drm_crtc *crtc,
  4522. struct intel_crtc_state *old_crtc_state,
  4523. struct drm_atomic_state *old_state)
  4524. {
  4525. struct drm_connector_state *old_conn_state;
  4526. struct drm_connector *conn;
  4527. int i;
  4528. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4529. struct intel_encoder *encoder =
  4530. to_intel_encoder(old_conn_state->best_encoder);
  4531. if (old_conn_state->crtc != crtc)
  4532. continue;
  4533. intel_opregion_notify_encoder(encoder, false);
  4534. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4535. }
  4536. }
  4537. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4538. struct intel_crtc_state *old_crtc_state,
  4539. struct drm_atomic_state *old_state)
  4540. {
  4541. struct drm_connector_state *old_conn_state;
  4542. struct drm_connector *conn;
  4543. int i;
  4544. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4545. struct intel_encoder *encoder =
  4546. to_intel_encoder(old_conn_state->best_encoder);
  4547. if (old_conn_state->crtc != crtc)
  4548. continue;
  4549. if (encoder->post_disable)
  4550. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4551. }
  4552. }
  4553. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4554. struct intel_crtc_state *old_crtc_state,
  4555. struct drm_atomic_state *old_state)
  4556. {
  4557. struct drm_connector_state *old_conn_state;
  4558. struct drm_connector *conn;
  4559. int i;
  4560. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4561. struct intel_encoder *encoder =
  4562. to_intel_encoder(old_conn_state->best_encoder);
  4563. if (old_conn_state->crtc != crtc)
  4564. continue;
  4565. if (encoder->post_pll_disable)
  4566. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4567. }
  4568. }
  4569. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4570. struct drm_atomic_state *old_state)
  4571. {
  4572. struct drm_crtc *crtc = pipe_config->base.crtc;
  4573. struct drm_device *dev = crtc->dev;
  4574. struct drm_i915_private *dev_priv = to_i915(dev);
  4575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4576. int pipe = intel_crtc->pipe;
  4577. struct intel_atomic_state *old_intel_state =
  4578. to_intel_atomic_state(old_state);
  4579. if (WARN_ON(intel_crtc->active))
  4580. return;
  4581. /*
  4582. * Sometimes spurious CPU pipe underruns happen during FDI
  4583. * training, at least with VGA+HDMI cloning. Suppress them.
  4584. *
  4585. * On ILK we get an occasional spurious CPU pipe underruns
  4586. * between eDP port A enable and vdd enable. Also PCH port
  4587. * enable seems to result in the occasional CPU pipe underrun.
  4588. *
  4589. * Spurious PCH underruns also occur during PCH enabling.
  4590. */
  4591. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4592. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4593. if (intel_crtc->config->has_pch_encoder)
  4594. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4595. if (intel_crtc->config->has_pch_encoder)
  4596. intel_prepare_shared_dpll(intel_crtc);
  4597. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4598. intel_dp_set_m_n(intel_crtc, M1_N1);
  4599. intel_set_pipe_timings(intel_crtc);
  4600. intel_set_pipe_src_size(intel_crtc);
  4601. if (intel_crtc->config->has_pch_encoder) {
  4602. intel_cpu_transcoder_set_m_n(intel_crtc,
  4603. &intel_crtc->config->fdi_m_n, NULL);
  4604. }
  4605. ironlake_set_pipeconf(crtc);
  4606. intel_crtc->active = true;
  4607. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4608. if (intel_crtc->config->has_pch_encoder) {
  4609. /* Note: FDI PLL enabling _must_ be done before we enable the
  4610. * cpu pipes, hence this is separate from all the other fdi/pch
  4611. * enabling. */
  4612. ironlake_fdi_pll_enable(intel_crtc);
  4613. } else {
  4614. assert_fdi_tx_disabled(dev_priv, pipe);
  4615. assert_fdi_rx_disabled(dev_priv, pipe);
  4616. }
  4617. ironlake_pfit_enable(intel_crtc);
  4618. /*
  4619. * On ILK+ LUT must be loaded before the pipe is running but with
  4620. * clocks enabled
  4621. */
  4622. intel_color_load_luts(&pipe_config->base);
  4623. if (dev_priv->display.initial_watermarks != NULL)
  4624. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4625. intel_enable_pipe(intel_crtc);
  4626. if (intel_crtc->config->has_pch_encoder)
  4627. ironlake_pch_enable(pipe_config);
  4628. assert_vblank_disabled(crtc);
  4629. drm_crtc_vblank_on(crtc);
  4630. intel_encoders_enable(crtc, pipe_config, old_state);
  4631. if (HAS_PCH_CPT(dev_priv))
  4632. cpt_verify_modeset(dev, intel_crtc->pipe);
  4633. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4634. if (intel_crtc->config->has_pch_encoder)
  4635. intel_wait_for_vblank(dev_priv, pipe);
  4636. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4637. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4638. }
  4639. /* IPS only exists on ULT machines and is tied to pipe A. */
  4640. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4641. {
  4642. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4643. }
  4644. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4645. struct drm_atomic_state *old_state)
  4646. {
  4647. struct drm_crtc *crtc = pipe_config->base.crtc;
  4648. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4649. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4650. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4651. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4652. struct intel_atomic_state *old_intel_state =
  4653. to_intel_atomic_state(old_state);
  4654. if (WARN_ON(intel_crtc->active))
  4655. return;
  4656. if (intel_crtc->config->has_pch_encoder)
  4657. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4658. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4659. if (intel_crtc->config->shared_dpll)
  4660. intel_enable_shared_dpll(intel_crtc);
  4661. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4662. intel_dp_set_m_n(intel_crtc, M1_N1);
  4663. if (!transcoder_is_dsi(cpu_transcoder))
  4664. intel_set_pipe_timings(intel_crtc);
  4665. intel_set_pipe_src_size(intel_crtc);
  4666. if (cpu_transcoder != TRANSCODER_EDP &&
  4667. !transcoder_is_dsi(cpu_transcoder)) {
  4668. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4669. intel_crtc->config->pixel_multiplier - 1);
  4670. }
  4671. if (intel_crtc->config->has_pch_encoder) {
  4672. intel_cpu_transcoder_set_m_n(intel_crtc,
  4673. &intel_crtc->config->fdi_m_n, NULL);
  4674. }
  4675. if (!transcoder_is_dsi(cpu_transcoder))
  4676. haswell_set_pipeconf(crtc);
  4677. haswell_set_pipemisc(crtc);
  4678. intel_color_set_csc(&pipe_config->base);
  4679. intel_crtc->active = true;
  4680. if (intel_crtc->config->has_pch_encoder)
  4681. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4682. else
  4683. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4684. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4685. if (intel_crtc->config->has_pch_encoder)
  4686. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4687. if (!transcoder_is_dsi(cpu_transcoder))
  4688. intel_ddi_enable_pipe_clock(pipe_config);
  4689. if (INTEL_GEN(dev_priv) >= 9)
  4690. skylake_pfit_enable(intel_crtc);
  4691. else
  4692. ironlake_pfit_enable(intel_crtc);
  4693. /*
  4694. * On ILK+ LUT must be loaded before the pipe is running but with
  4695. * clocks enabled
  4696. */
  4697. intel_color_load_luts(&pipe_config->base);
  4698. intel_ddi_set_pipe_settings(pipe_config);
  4699. if (!transcoder_is_dsi(cpu_transcoder))
  4700. intel_ddi_enable_transcoder_func(pipe_config);
  4701. if (dev_priv->display.initial_watermarks != NULL)
  4702. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4703. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4704. if (!transcoder_is_dsi(cpu_transcoder))
  4705. intel_enable_pipe(intel_crtc);
  4706. if (intel_crtc->config->has_pch_encoder)
  4707. lpt_pch_enable(pipe_config);
  4708. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4709. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4710. assert_vblank_disabled(crtc);
  4711. drm_crtc_vblank_on(crtc);
  4712. intel_encoders_enable(crtc, pipe_config, old_state);
  4713. if (intel_crtc->config->has_pch_encoder) {
  4714. intel_wait_for_vblank(dev_priv, pipe);
  4715. intel_wait_for_vblank(dev_priv, pipe);
  4716. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4717. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4718. }
  4719. /* If we change the relative order between pipe/planes enabling, we need
  4720. * to change the workaround. */
  4721. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4722. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4723. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4724. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4725. }
  4726. }
  4727. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4728. {
  4729. struct drm_device *dev = crtc->base.dev;
  4730. struct drm_i915_private *dev_priv = to_i915(dev);
  4731. int pipe = crtc->pipe;
  4732. /* To avoid upsetting the power well on haswell only disable the pfit if
  4733. * it's in use. The hw state code will make sure we get this right. */
  4734. if (force || crtc->config->pch_pfit.enabled) {
  4735. I915_WRITE(PF_CTL(pipe), 0);
  4736. I915_WRITE(PF_WIN_POS(pipe), 0);
  4737. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4738. }
  4739. }
  4740. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4741. struct drm_atomic_state *old_state)
  4742. {
  4743. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4744. struct drm_device *dev = crtc->dev;
  4745. struct drm_i915_private *dev_priv = to_i915(dev);
  4746. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4747. int pipe = intel_crtc->pipe;
  4748. /*
  4749. * Sometimes spurious CPU pipe underruns happen when the
  4750. * pipe is already disabled, but FDI RX/TX is still enabled.
  4751. * Happens at least with VGA+HDMI cloning. Suppress them.
  4752. */
  4753. if (intel_crtc->config->has_pch_encoder) {
  4754. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4755. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4756. }
  4757. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4758. drm_crtc_vblank_off(crtc);
  4759. assert_vblank_disabled(crtc);
  4760. intel_disable_pipe(intel_crtc);
  4761. ironlake_pfit_disable(intel_crtc, false);
  4762. if (intel_crtc->config->has_pch_encoder)
  4763. ironlake_fdi_disable(crtc);
  4764. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4765. if (intel_crtc->config->has_pch_encoder) {
  4766. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4767. if (HAS_PCH_CPT(dev_priv)) {
  4768. i915_reg_t reg;
  4769. u32 temp;
  4770. /* disable TRANS_DP_CTL */
  4771. reg = TRANS_DP_CTL(pipe);
  4772. temp = I915_READ(reg);
  4773. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4774. TRANS_DP_PORT_SEL_MASK);
  4775. temp |= TRANS_DP_PORT_SEL_NONE;
  4776. I915_WRITE(reg, temp);
  4777. /* disable DPLL_SEL */
  4778. temp = I915_READ(PCH_DPLL_SEL);
  4779. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4780. I915_WRITE(PCH_DPLL_SEL, temp);
  4781. }
  4782. ironlake_fdi_pll_disable(intel_crtc);
  4783. }
  4784. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4785. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4786. }
  4787. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4788. struct drm_atomic_state *old_state)
  4789. {
  4790. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4791. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4792. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4793. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4794. if (intel_crtc->config->has_pch_encoder)
  4795. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4796. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4797. drm_crtc_vblank_off(crtc);
  4798. assert_vblank_disabled(crtc);
  4799. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4800. if (!transcoder_is_dsi(cpu_transcoder))
  4801. intel_disable_pipe(intel_crtc);
  4802. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4803. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4804. if (!transcoder_is_dsi(cpu_transcoder))
  4805. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4806. if (INTEL_GEN(dev_priv) >= 9)
  4807. skylake_scaler_disable(intel_crtc);
  4808. else
  4809. ironlake_pfit_disable(intel_crtc, false);
  4810. if (!transcoder_is_dsi(cpu_transcoder))
  4811. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4812. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4813. if (old_crtc_state->has_pch_encoder)
  4814. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4815. }
  4816. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4817. {
  4818. struct drm_device *dev = crtc->base.dev;
  4819. struct drm_i915_private *dev_priv = to_i915(dev);
  4820. struct intel_crtc_state *pipe_config = crtc->config;
  4821. if (!pipe_config->gmch_pfit.control)
  4822. return;
  4823. /*
  4824. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4825. * according to register description and PRM.
  4826. */
  4827. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4828. assert_pipe_disabled(dev_priv, crtc->pipe);
  4829. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4830. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4831. /* Border color in case we don't scale up to the full screen. Black by
  4832. * default, change to something else for debugging. */
  4833. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4834. }
  4835. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4836. {
  4837. switch (port) {
  4838. case PORT_A:
  4839. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4840. case PORT_B:
  4841. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4842. case PORT_C:
  4843. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4844. case PORT_D:
  4845. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4846. case PORT_E:
  4847. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4848. default:
  4849. MISSING_CASE(port);
  4850. return POWER_DOMAIN_PORT_OTHER;
  4851. }
  4852. }
  4853. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4854. struct intel_crtc_state *crtc_state)
  4855. {
  4856. struct drm_device *dev = crtc->dev;
  4857. struct drm_i915_private *dev_priv = to_i915(dev);
  4858. struct drm_encoder *encoder;
  4859. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4860. enum pipe pipe = intel_crtc->pipe;
  4861. u64 mask;
  4862. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4863. if (!crtc_state->base.active)
  4864. return 0;
  4865. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4866. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4867. if (crtc_state->pch_pfit.enabled ||
  4868. crtc_state->pch_pfit.force_thru)
  4869. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4870. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4871. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4872. mask |= BIT_ULL(intel_encoder->power_domain);
  4873. }
  4874. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4875. mask |= BIT(POWER_DOMAIN_AUDIO);
  4876. if (crtc_state->shared_dpll)
  4877. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4878. return mask;
  4879. }
  4880. static u64
  4881. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4882. struct intel_crtc_state *crtc_state)
  4883. {
  4884. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4885. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4886. enum intel_display_power_domain domain;
  4887. u64 domains, new_domains, old_domains;
  4888. old_domains = intel_crtc->enabled_power_domains;
  4889. intel_crtc->enabled_power_domains = new_domains =
  4890. get_crtc_power_domains(crtc, crtc_state);
  4891. domains = new_domains & ~old_domains;
  4892. for_each_power_domain(domain, domains)
  4893. intel_display_power_get(dev_priv, domain);
  4894. return old_domains & ~new_domains;
  4895. }
  4896. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4897. u64 domains)
  4898. {
  4899. enum intel_display_power_domain domain;
  4900. for_each_power_domain(domain, domains)
  4901. intel_display_power_put(dev_priv, domain);
  4902. }
  4903. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4904. struct drm_atomic_state *old_state)
  4905. {
  4906. struct intel_atomic_state *old_intel_state =
  4907. to_intel_atomic_state(old_state);
  4908. struct drm_crtc *crtc = pipe_config->base.crtc;
  4909. struct drm_device *dev = crtc->dev;
  4910. struct drm_i915_private *dev_priv = to_i915(dev);
  4911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4912. int pipe = intel_crtc->pipe;
  4913. if (WARN_ON(intel_crtc->active))
  4914. return;
  4915. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4916. intel_dp_set_m_n(intel_crtc, M1_N1);
  4917. intel_set_pipe_timings(intel_crtc);
  4918. intel_set_pipe_src_size(intel_crtc);
  4919. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4920. struct drm_i915_private *dev_priv = to_i915(dev);
  4921. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4922. I915_WRITE(CHV_CANVAS(pipe), 0);
  4923. }
  4924. i9xx_set_pipeconf(intel_crtc);
  4925. intel_crtc->active = true;
  4926. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4927. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4928. if (IS_CHERRYVIEW(dev_priv)) {
  4929. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4930. chv_enable_pll(intel_crtc, intel_crtc->config);
  4931. } else {
  4932. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4933. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4934. }
  4935. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4936. i9xx_pfit_enable(intel_crtc);
  4937. intel_color_load_luts(&pipe_config->base);
  4938. dev_priv->display.initial_watermarks(old_intel_state,
  4939. pipe_config);
  4940. intel_enable_pipe(intel_crtc);
  4941. assert_vblank_disabled(crtc);
  4942. drm_crtc_vblank_on(crtc);
  4943. intel_encoders_enable(crtc, pipe_config, old_state);
  4944. }
  4945. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4946. {
  4947. struct drm_device *dev = crtc->base.dev;
  4948. struct drm_i915_private *dev_priv = to_i915(dev);
  4949. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4950. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4951. }
  4952. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4953. struct drm_atomic_state *old_state)
  4954. {
  4955. struct intel_atomic_state *old_intel_state =
  4956. to_intel_atomic_state(old_state);
  4957. struct drm_crtc *crtc = pipe_config->base.crtc;
  4958. struct drm_device *dev = crtc->dev;
  4959. struct drm_i915_private *dev_priv = to_i915(dev);
  4960. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4961. enum pipe pipe = intel_crtc->pipe;
  4962. if (WARN_ON(intel_crtc->active))
  4963. return;
  4964. i9xx_set_pll_dividers(intel_crtc);
  4965. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4966. intel_dp_set_m_n(intel_crtc, M1_N1);
  4967. intel_set_pipe_timings(intel_crtc);
  4968. intel_set_pipe_src_size(intel_crtc);
  4969. i9xx_set_pipeconf(intel_crtc);
  4970. intel_crtc->active = true;
  4971. if (!IS_GEN2(dev_priv))
  4972. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4973. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4974. i9xx_enable_pll(intel_crtc);
  4975. i9xx_pfit_enable(intel_crtc);
  4976. intel_color_load_luts(&pipe_config->base);
  4977. if (dev_priv->display.initial_watermarks != NULL)
  4978. dev_priv->display.initial_watermarks(old_intel_state,
  4979. intel_crtc->config);
  4980. else
  4981. intel_update_watermarks(intel_crtc);
  4982. intel_enable_pipe(intel_crtc);
  4983. assert_vblank_disabled(crtc);
  4984. drm_crtc_vblank_on(crtc);
  4985. intel_encoders_enable(crtc, pipe_config, old_state);
  4986. }
  4987. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4988. {
  4989. struct drm_device *dev = crtc->base.dev;
  4990. struct drm_i915_private *dev_priv = to_i915(dev);
  4991. if (!crtc->config->gmch_pfit.control)
  4992. return;
  4993. assert_pipe_disabled(dev_priv, crtc->pipe);
  4994. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4995. I915_READ(PFIT_CONTROL));
  4996. I915_WRITE(PFIT_CONTROL, 0);
  4997. }
  4998. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4999. struct drm_atomic_state *old_state)
  5000. {
  5001. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5002. struct drm_device *dev = crtc->dev;
  5003. struct drm_i915_private *dev_priv = to_i915(dev);
  5004. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5005. int pipe = intel_crtc->pipe;
  5006. /*
  5007. * On gen2 planes are double buffered but the pipe isn't, so we must
  5008. * wait for planes to fully turn off before disabling the pipe.
  5009. */
  5010. if (IS_GEN2(dev_priv))
  5011. intel_wait_for_vblank(dev_priv, pipe);
  5012. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5013. drm_crtc_vblank_off(crtc);
  5014. assert_vblank_disabled(crtc);
  5015. intel_disable_pipe(intel_crtc);
  5016. i9xx_pfit_disable(intel_crtc);
  5017. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5018. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5019. if (IS_CHERRYVIEW(dev_priv))
  5020. chv_disable_pll(dev_priv, pipe);
  5021. else if (IS_VALLEYVIEW(dev_priv))
  5022. vlv_disable_pll(dev_priv, pipe);
  5023. else
  5024. i9xx_disable_pll(intel_crtc);
  5025. }
  5026. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5027. if (!IS_GEN2(dev_priv))
  5028. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5029. if (!dev_priv->display.initial_watermarks)
  5030. intel_update_watermarks(intel_crtc);
  5031. /* clock the pipe down to 640x480@60 to potentially save power */
  5032. if (IS_I830(dev_priv))
  5033. i830_enable_pipe(dev_priv, pipe);
  5034. }
  5035. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5036. struct drm_modeset_acquire_ctx *ctx)
  5037. {
  5038. struct intel_encoder *encoder;
  5039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5040. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5041. enum intel_display_power_domain domain;
  5042. u64 domains;
  5043. struct drm_atomic_state *state;
  5044. struct intel_crtc_state *crtc_state;
  5045. int ret;
  5046. if (!intel_crtc->active)
  5047. return;
  5048. if (crtc->primary->state->visible) {
  5049. intel_pre_disable_primary_noatomic(crtc);
  5050. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5051. crtc->primary->state->visible = false;
  5052. }
  5053. state = drm_atomic_state_alloc(crtc->dev);
  5054. if (!state) {
  5055. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5056. crtc->base.id, crtc->name);
  5057. return;
  5058. }
  5059. state->acquire_ctx = ctx;
  5060. /* Everything's already locked, -EDEADLK can't happen. */
  5061. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5062. ret = drm_atomic_add_affected_connectors(state, crtc);
  5063. WARN_ON(IS_ERR(crtc_state) || ret);
  5064. dev_priv->display.crtc_disable(crtc_state, state);
  5065. drm_atomic_state_put(state);
  5066. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5067. crtc->base.id, crtc->name);
  5068. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5069. crtc->state->active = false;
  5070. intel_crtc->active = false;
  5071. crtc->enabled = false;
  5072. crtc->state->connector_mask = 0;
  5073. crtc->state->encoder_mask = 0;
  5074. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5075. encoder->base.crtc = NULL;
  5076. intel_fbc_disable(intel_crtc);
  5077. intel_update_watermarks(intel_crtc);
  5078. intel_disable_shared_dpll(intel_crtc);
  5079. domains = intel_crtc->enabled_power_domains;
  5080. for_each_power_domain(domain, domains)
  5081. intel_display_power_put(dev_priv, domain);
  5082. intel_crtc->enabled_power_domains = 0;
  5083. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5084. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5085. }
  5086. /*
  5087. * turn all crtc's off, but do not adjust state
  5088. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5089. */
  5090. int intel_display_suspend(struct drm_device *dev)
  5091. {
  5092. struct drm_i915_private *dev_priv = to_i915(dev);
  5093. struct drm_atomic_state *state;
  5094. int ret;
  5095. state = drm_atomic_helper_suspend(dev);
  5096. ret = PTR_ERR_OR_ZERO(state);
  5097. if (ret)
  5098. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5099. else
  5100. dev_priv->modeset_restore_state = state;
  5101. return ret;
  5102. }
  5103. void intel_encoder_destroy(struct drm_encoder *encoder)
  5104. {
  5105. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5106. drm_encoder_cleanup(encoder);
  5107. kfree(intel_encoder);
  5108. }
  5109. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5110. * internal consistency). */
  5111. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5112. struct drm_connector_state *conn_state)
  5113. {
  5114. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5115. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5116. connector->base.base.id,
  5117. connector->base.name);
  5118. if (connector->get_hw_state(connector)) {
  5119. struct intel_encoder *encoder = connector->encoder;
  5120. I915_STATE_WARN(!crtc_state,
  5121. "connector enabled without attached crtc\n");
  5122. if (!crtc_state)
  5123. return;
  5124. I915_STATE_WARN(!crtc_state->active,
  5125. "connector is active, but attached crtc isn't\n");
  5126. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5127. return;
  5128. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5129. "atomic encoder doesn't match attached encoder\n");
  5130. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5131. "attached encoder crtc differs from connector crtc\n");
  5132. } else {
  5133. I915_STATE_WARN(crtc_state && crtc_state->active,
  5134. "attached crtc is active, but connector isn't\n");
  5135. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5136. "best encoder set without crtc!\n");
  5137. }
  5138. }
  5139. int intel_connector_init(struct intel_connector *connector)
  5140. {
  5141. struct intel_digital_connector_state *conn_state;
  5142. /*
  5143. * Allocate enough memory to hold intel_digital_connector_state,
  5144. * This might be a few bytes too many, but for connectors that don't
  5145. * need it we'll free the state and allocate a smaller one on the first
  5146. * succesful commit anyway.
  5147. */
  5148. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5149. if (!conn_state)
  5150. return -ENOMEM;
  5151. __drm_atomic_helper_connector_reset(&connector->base,
  5152. &conn_state->base);
  5153. return 0;
  5154. }
  5155. struct intel_connector *intel_connector_alloc(void)
  5156. {
  5157. struct intel_connector *connector;
  5158. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5159. if (!connector)
  5160. return NULL;
  5161. if (intel_connector_init(connector) < 0) {
  5162. kfree(connector);
  5163. return NULL;
  5164. }
  5165. return connector;
  5166. }
  5167. /* Simple connector->get_hw_state implementation for encoders that support only
  5168. * one connector and no cloning and hence the encoder state determines the state
  5169. * of the connector. */
  5170. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5171. {
  5172. enum pipe pipe = 0;
  5173. struct intel_encoder *encoder = connector->encoder;
  5174. return encoder->get_hw_state(encoder, &pipe);
  5175. }
  5176. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5177. {
  5178. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5179. return crtc_state->fdi_lanes;
  5180. return 0;
  5181. }
  5182. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5183. struct intel_crtc_state *pipe_config)
  5184. {
  5185. struct drm_i915_private *dev_priv = to_i915(dev);
  5186. struct drm_atomic_state *state = pipe_config->base.state;
  5187. struct intel_crtc *other_crtc;
  5188. struct intel_crtc_state *other_crtc_state;
  5189. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5190. pipe_name(pipe), pipe_config->fdi_lanes);
  5191. if (pipe_config->fdi_lanes > 4) {
  5192. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5193. pipe_name(pipe), pipe_config->fdi_lanes);
  5194. return -EINVAL;
  5195. }
  5196. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5197. if (pipe_config->fdi_lanes > 2) {
  5198. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5199. pipe_config->fdi_lanes);
  5200. return -EINVAL;
  5201. } else {
  5202. return 0;
  5203. }
  5204. }
  5205. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5206. return 0;
  5207. /* Ivybridge 3 pipe is really complicated */
  5208. switch (pipe) {
  5209. case PIPE_A:
  5210. return 0;
  5211. case PIPE_B:
  5212. if (pipe_config->fdi_lanes <= 2)
  5213. return 0;
  5214. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5215. other_crtc_state =
  5216. intel_atomic_get_crtc_state(state, other_crtc);
  5217. if (IS_ERR(other_crtc_state))
  5218. return PTR_ERR(other_crtc_state);
  5219. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5220. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5221. pipe_name(pipe), pipe_config->fdi_lanes);
  5222. return -EINVAL;
  5223. }
  5224. return 0;
  5225. case PIPE_C:
  5226. if (pipe_config->fdi_lanes > 2) {
  5227. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5228. pipe_name(pipe), pipe_config->fdi_lanes);
  5229. return -EINVAL;
  5230. }
  5231. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5232. other_crtc_state =
  5233. intel_atomic_get_crtc_state(state, other_crtc);
  5234. if (IS_ERR(other_crtc_state))
  5235. return PTR_ERR(other_crtc_state);
  5236. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5237. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5238. return -EINVAL;
  5239. }
  5240. return 0;
  5241. default:
  5242. BUG();
  5243. }
  5244. }
  5245. #define RETRY 1
  5246. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5247. struct intel_crtc_state *pipe_config)
  5248. {
  5249. struct drm_device *dev = intel_crtc->base.dev;
  5250. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5251. int lane, link_bw, fdi_dotclock, ret;
  5252. bool needs_recompute = false;
  5253. retry:
  5254. /* FDI is a binary signal running at ~2.7GHz, encoding
  5255. * each output octet as 10 bits. The actual frequency
  5256. * is stored as a divider into a 100MHz clock, and the
  5257. * mode pixel clock is stored in units of 1KHz.
  5258. * Hence the bw of each lane in terms of the mode signal
  5259. * is:
  5260. */
  5261. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5262. fdi_dotclock = adjusted_mode->crtc_clock;
  5263. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5264. pipe_config->pipe_bpp);
  5265. pipe_config->fdi_lanes = lane;
  5266. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5267. link_bw, &pipe_config->fdi_m_n, false);
  5268. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5269. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5270. pipe_config->pipe_bpp -= 2*3;
  5271. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5272. pipe_config->pipe_bpp);
  5273. needs_recompute = true;
  5274. pipe_config->bw_constrained = true;
  5275. goto retry;
  5276. }
  5277. if (needs_recompute)
  5278. return RETRY;
  5279. return ret;
  5280. }
  5281. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5282. struct intel_crtc_state *pipe_config)
  5283. {
  5284. if (pipe_config->ips_force_disable)
  5285. return false;
  5286. if (pipe_config->pipe_bpp > 24)
  5287. return false;
  5288. /* HSW can handle pixel rate up to cdclk? */
  5289. if (IS_HASWELL(dev_priv))
  5290. return true;
  5291. /*
  5292. * We compare against max which means we must take
  5293. * the increased cdclk requirement into account when
  5294. * calculating the new cdclk.
  5295. *
  5296. * Should measure whether using a lower cdclk w/o IPS
  5297. */
  5298. return pipe_config->pixel_rate <=
  5299. dev_priv->max_cdclk_freq * 95 / 100;
  5300. }
  5301. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5302. struct intel_crtc_state *pipe_config)
  5303. {
  5304. struct drm_device *dev = crtc->base.dev;
  5305. struct drm_i915_private *dev_priv = to_i915(dev);
  5306. pipe_config->ips_enabled = i915.enable_ips &&
  5307. hsw_crtc_supports_ips(crtc) &&
  5308. pipe_config_supports_ips(dev_priv, pipe_config);
  5309. }
  5310. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5311. {
  5312. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5313. /* GDG double wide on either pipe, otherwise pipe A only */
  5314. return INTEL_INFO(dev_priv)->gen < 4 &&
  5315. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5316. }
  5317. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5318. {
  5319. uint32_t pixel_rate;
  5320. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5321. /*
  5322. * We only use IF-ID interlacing. If we ever use
  5323. * PF-ID we'll need to adjust the pixel_rate here.
  5324. */
  5325. if (pipe_config->pch_pfit.enabled) {
  5326. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5327. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5328. pipe_w = pipe_config->pipe_src_w;
  5329. pipe_h = pipe_config->pipe_src_h;
  5330. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5331. pfit_h = pfit_size & 0xFFFF;
  5332. if (pipe_w < pfit_w)
  5333. pipe_w = pfit_w;
  5334. if (pipe_h < pfit_h)
  5335. pipe_h = pfit_h;
  5336. if (WARN_ON(!pfit_w || !pfit_h))
  5337. return pixel_rate;
  5338. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5339. pfit_w * pfit_h);
  5340. }
  5341. return pixel_rate;
  5342. }
  5343. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5344. {
  5345. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5346. if (HAS_GMCH_DISPLAY(dev_priv))
  5347. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5348. crtc_state->pixel_rate =
  5349. crtc_state->base.adjusted_mode.crtc_clock;
  5350. else
  5351. crtc_state->pixel_rate =
  5352. ilk_pipe_pixel_rate(crtc_state);
  5353. }
  5354. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5355. struct intel_crtc_state *pipe_config)
  5356. {
  5357. struct drm_device *dev = crtc->base.dev;
  5358. struct drm_i915_private *dev_priv = to_i915(dev);
  5359. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5360. int clock_limit = dev_priv->max_dotclk_freq;
  5361. if (INTEL_GEN(dev_priv) < 4) {
  5362. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5363. /*
  5364. * Enable double wide mode when the dot clock
  5365. * is > 90% of the (display) core speed.
  5366. */
  5367. if (intel_crtc_supports_double_wide(crtc) &&
  5368. adjusted_mode->crtc_clock > clock_limit) {
  5369. clock_limit = dev_priv->max_dotclk_freq;
  5370. pipe_config->double_wide = true;
  5371. }
  5372. }
  5373. if (adjusted_mode->crtc_clock > clock_limit) {
  5374. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5375. adjusted_mode->crtc_clock, clock_limit,
  5376. yesno(pipe_config->double_wide));
  5377. return -EINVAL;
  5378. }
  5379. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5380. /*
  5381. * There is only one pipe CSC unit per pipe, and we need that
  5382. * for output conversion from RGB->YCBCR. So if CTM is already
  5383. * applied we can't support YCBCR420 output.
  5384. */
  5385. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5386. return -EINVAL;
  5387. }
  5388. /*
  5389. * Pipe horizontal size must be even in:
  5390. * - DVO ganged mode
  5391. * - LVDS dual channel mode
  5392. * - Double wide pipe
  5393. */
  5394. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5395. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5396. pipe_config->pipe_src_w &= ~1;
  5397. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5398. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5399. */
  5400. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5401. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5402. return -EINVAL;
  5403. intel_crtc_compute_pixel_rate(pipe_config);
  5404. if (HAS_IPS(dev_priv))
  5405. hsw_compute_ips_config(crtc, pipe_config);
  5406. if (pipe_config->has_pch_encoder)
  5407. return ironlake_fdi_compute_config(crtc, pipe_config);
  5408. return 0;
  5409. }
  5410. static void
  5411. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5412. {
  5413. while (*num > DATA_LINK_M_N_MASK ||
  5414. *den > DATA_LINK_M_N_MASK) {
  5415. *num >>= 1;
  5416. *den >>= 1;
  5417. }
  5418. }
  5419. static void compute_m_n(unsigned int m, unsigned int n,
  5420. uint32_t *ret_m, uint32_t *ret_n,
  5421. bool reduce_m_n)
  5422. {
  5423. /*
  5424. * Reduce M/N as much as possible without loss in precision. Several DP
  5425. * dongles in particular seem to be fussy about too large *link* M/N
  5426. * values. The passed in values are more likely to have the least
  5427. * significant bits zero than M after rounding below, so do this first.
  5428. */
  5429. if (reduce_m_n) {
  5430. while ((m & 1) == 0 && (n & 1) == 0) {
  5431. m >>= 1;
  5432. n >>= 1;
  5433. }
  5434. }
  5435. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5436. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5437. intel_reduce_m_n_ratio(ret_m, ret_n);
  5438. }
  5439. void
  5440. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5441. int pixel_clock, int link_clock,
  5442. struct intel_link_m_n *m_n,
  5443. bool reduce_m_n)
  5444. {
  5445. m_n->tu = 64;
  5446. compute_m_n(bits_per_pixel * pixel_clock,
  5447. link_clock * nlanes * 8,
  5448. &m_n->gmch_m, &m_n->gmch_n,
  5449. reduce_m_n);
  5450. compute_m_n(pixel_clock, link_clock,
  5451. &m_n->link_m, &m_n->link_n,
  5452. reduce_m_n);
  5453. }
  5454. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5455. {
  5456. if (i915.panel_use_ssc >= 0)
  5457. return i915.panel_use_ssc != 0;
  5458. return dev_priv->vbt.lvds_use_ssc
  5459. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5460. }
  5461. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5462. {
  5463. return (1 << dpll->n) << 16 | dpll->m2;
  5464. }
  5465. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5466. {
  5467. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5468. }
  5469. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5470. struct intel_crtc_state *crtc_state,
  5471. struct dpll *reduced_clock)
  5472. {
  5473. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5474. u32 fp, fp2 = 0;
  5475. if (IS_PINEVIEW(dev_priv)) {
  5476. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5477. if (reduced_clock)
  5478. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5479. } else {
  5480. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5481. if (reduced_clock)
  5482. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5483. }
  5484. crtc_state->dpll_hw_state.fp0 = fp;
  5485. crtc->lowfreq_avail = false;
  5486. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5487. reduced_clock) {
  5488. crtc_state->dpll_hw_state.fp1 = fp2;
  5489. crtc->lowfreq_avail = true;
  5490. } else {
  5491. crtc_state->dpll_hw_state.fp1 = fp;
  5492. }
  5493. }
  5494. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5495. pipe)
  5496. {
  5497. u32 reg_val;
  5498. /*
  5499. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5500. * and set it to a reasonable value instead.
  5501. */
  5502. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5503. reg_val &= 0xffffff00;
  5504. reg_val |= 0x00000030;
  5505. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5506. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5507. reg_val &= 0x00ffffff;
  5508. reg_val |= 0x8c000000;
  5509. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5510. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5511. reg_val &= 0xffffff00;
  5512. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5513. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5514. reg_val &= 0x00ffffff;
  5515. reg_val |= 0xb0000000;
  5516. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5517. }
  5518. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5519. struct intel_link_m_n *m_n)
  5520. {
  5521. struct drm_device *dev = crtc->base.dev;
  5522. struct drm_i915_private *dev_priv = to_i915(dev);
  5523. int pipe = crtc->pipe;
  5524. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5525. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5526. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5527. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5528. }
  5529. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5530. struct intel_link_m_n *m_n,
  5531. struct intel_link_m_n *m2_n2)
  5532. {
  5533. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5534. int pipe = crtc->pipe;
  5535. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5536. if (INTEL_GEN(dev_priv) >= 5) {
  5537. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5538. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5539. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5540. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5541. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5542. * for gen < 8) and if DRRS is supported (to make sure the
  5543. * registers are not unnecessarily accessed).
  5544. */
  5545. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5546. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5547. I915_WRITE(PIPE_DATA_M2(transcoder),
  5548. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5549. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5550. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5551. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5552. }
  5553. } else {
  5554. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5555. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5556. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5557. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5558. }
  5559. }
  5560. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5561. {
  5562. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5563. if (m_n == M1_N1) {
  5564. dp_m_n = &crtc->config->dp_m_n;
  5565. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5566. } else if (m_n == M2_N2) {
  5567. /*
  5568. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5569. * needs to be programmed into M1_N1.
  5570. */
  5571. dp_m_n = &crtc->config->dp_m2_n2;
  5572. } else {
  5573. DRM_ERROR("Unsupported divider value\n");
  5574. return;
  5575. }
  5576. if (crtc->config->has_pch_encoder)
  5577. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5578. else
  5579. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5580. }
  5581. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5582. struct intel_crtc_state *pipe_config)
  5583. {
  5584. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5585. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5586. if (crtc->pipe != PIPE_A)
  5587. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5588. /* DPLL not used with DSI, but still need the rest set up */
  5589. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5590. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5591. DPLL_EXT_BUFFER_ENABLE_VLV;
  5592. pipe_config->dpll_hw_state.dpll_md =
  5593. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5594. }
  5595. static void chv_compute_dpll(struct intel_crtc *crtc,
  5596. struct intel_crtc_state *pipe_config)
  5597. {
  5598. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5599. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5600. if (crtc->pipe != PIPE_A)
  5601. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5602. /* DPLL not used with DSI, but still need the rest set up */
  5603. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5604. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5605. pipe_config->dpll_hw_state.dpll_md =
  5606. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5607. }
  5608. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5609. const struct intel_crtc_state *pipe_config)
  5610. {
  5611. struct drm_device *dev = crtc->base.dev;
  5612. struct drm_i915_private *dev_priv = to_i915(dev);
  5613. enum pipe pipe = crtc->pipe;
  5614. u32 mdiv;
  5615. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5616. u32 coreclk, reg_val;
  5617. /* Enable Refclk */
  5618. I915_WRITE(DPLL(pipe),
  5619. pipe_config->dpll_hw_state.dpll &
  5620. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5621. /* No need to actually set up the DPLL with DSI */
  5622. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5623. return;
  5624. mutex_lock(&dev_priv->sb_lock);
  5625. bestn = pipe_config->dpll.n;
  5626. bestm1 = pipe_config->dpll.m1;
  5627. bestm2 = pipe_config->dpll.m2;
  5628. bestp1 = pipe_config->dpll.p1;
  5629. bestp2 = pipe_config->dpll.p2;
  5630. /* See eDP HDMI DPIO driver vbios notes doc */
  5631. /* PLL B needs special handling */
  5632. if (pipe == PIPE_B)
  5633. vlv_pllb_recal_opamp(dev_priv, pipe);
  5634. /* Set up Tx target for periodic Rcomp update */
  5635. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5636. /* Disable target IRef on PLL */
  5637. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5638. reg_val &= 0x00ffffff;
  5639. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5640. /* Disable fast lock */
  5641. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5642. /* Set idtafcrecal before PLL is enabled */
  5643. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5644. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5645. mdiv |= ((bestn << DPIO_N_SHIFT));
  5646. mdiv |= (1 << DPIO_K_SHIFT);
  5647. /*
  5648. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5649. * but we don't support that).
  5650. * Note: don't use the DAC post divider as it seems unstable.
  5651. */
  5652. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5653. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5654. mdiv |= DPIO_ENABLE_CALIBRATION;
  5655. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5656. /* Set HBR and RBR LPF coefficients */
  5657. if (pipe_config->port_clock == 162000 ||
  5658. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5659. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5660. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5661. 0x009f0003);
  5662. else
  5663. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5664. 0x00d0000f);
  5665. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5666. /* Use SSC source */
  5667. if (pipe == PIPE_A)
  5668. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5669. 0x0df40000);
  5670. else
  5671. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5672. 0x0df70000);
  5673. } else { /* HDMI or VGA */
  5674. /* Use bend source */
  5675. if (pipe == PIPE_A)
  5676. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5677. 0x0df70000);
  5678. else
  5679. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5680. 0x0df40000);
  5681. }
  5682. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5683. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5684. if (intel_crtc_has_dp_encoder(crtc->config))
  5685. coreclk |= 0x01000000;
  5686. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5687. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5688. mutex_unlock(&dev_priv->sb_lock);
  5689. }
  5690. static void chv_prepare_pll(struct intel_crtc *crtc,
  5691. const struct intel_crtc_state *pipe_config)
  5692. {
  5693. struct drm_device *dev = crtc->base.dev;
  5694. struct drm_i915_private *dev_priv = to_i915(dev);
  5695. enum pipe pipe = crtc->pipe;
  5696. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5697. u32 loopfilter, tribuf_calcntr;
  5698. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5699. u32 dpio_val;
  5700. int vco;
  5701. /* Enable Refclk and SSC */
  5702. I915_WRITE(DPLL(pipe),
  5703. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5704. /* No need to actually set up the DPLL with DSI */
  5705. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5706. return;
  5707. bestn = pipe_config->dpll.n;
  5708. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5709. bestm1 = pipe_config->dpll.m1;
  5710. bestm2 = pipe_config->dpll.m2 >> 22;
  5711. bestp1 = pipe_config->dpll.p1;
  5712. bestp2 = pipe_config->dpll.p2;
  5713. vco = pipe_config->dpll.vco;
  5714. dpio_val = 0;
  5715. loopfilter = 0;
  5716. mutex_lock(&dev_priv->sb_lock);
  5717. /* p1 and p2 divider */
  5718. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5719. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5720. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5721. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5722. 1 << DPIO_CHV_K_DIV_SHIFT);
  5723. /* Feedback post-divider - m2 */
  5724. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5725. /* Feedback refclk divider - n and m1 */
  5726. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5727. DPIO_CHV_M1_DIV_BY_2 |
  5728. 1 << DPIO_CHV_N_DIV_SHIFT);
  5729. /* M2 fraction division */
  5730. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5731. /* M2 fraction division enable */
  5732. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5733. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5734. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5735. if (bestm2_frac)
  5736. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5737. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5738. /* Program digital lock detect threshold */
  5739. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5740. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5741. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5742. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5743. if (!bestm2_frac)
  5744. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5745. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5746. /* Loop filter */
  5747. if (vco == 5400000) {
  5748. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5749. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5750. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5751. tribuf_calcntr = 0x9;
  5752. } else if (vco <= 6200000) {
  5753. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5754. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5755. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5756. tribuf_calcntr = 0x9;
  5757. } else if (vco <= 6480000) {
  5758. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5759. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5760. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5761. tribuf_calcntr = 0x8;
  5762. } else {
  5763. /* Not supported. Apply the same limits as in the max case */
  5764. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5765. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5766. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5767. tribuf_calcntr = 0;
  5768. }
  5769. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5770. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5771. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5772. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5773. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5774. /* AFC Recal */
  5775. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5776. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5777. DPIO_AFC_RECAL);
  5778. mutex_unlock(&dev_priv->sb_lock);
  5779. }
  5780. /**
  5781. * vlv_force_pll_on - forcibly enable just the PLL
  5782. * @dev_priv: i915 private structure
  5783. * @pipe: pipe PLL to enable
  5784. * @dpll: PLL configuration
  5785. *
  5786. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5787. * in cases where we need the PLL enabled even when @pipe is not going to
  5788. * be enabled.
  5789. */
  5790. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5791. const struct dpll *dpll)
  5792. {
  5793. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5794. struct intel_crtc_state *pipe_config;
  5795. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5796. if (!pipe_config)
  5797. return -ENOMEM;
  5798. pipe_config->base.crtc = &crtc->base;
  5799. pipe_config->pixel_multiplier = 1;
  5800. pipe_config->dpll = *dpll;
  5801. if (IS_CHERRYVIEW(dev_priv)) {
  5802. chv_compute_dpll(crtc, pipe_config);
  5803. chv_prepare_pll(crtc, pipe_config);
  5804. chv_enable_pll(crtc, pipe_config);
  5805. } else {
  5806. vlv_compute_dpll(crtc, pipe_config);
  5807. vlv_prepare_pll(crtc, pipe_config);
  5808. vlv_enable_pll(crtc, pipe_config);
  5809. }
  5810. kfree(pipe_config);
  5811. return 0;
  5812. }
  5813. /**
  5814. * vlv_force_pll_off - forcibly disable just the PLL
  5815. * @dev_priv: i915 private structure
  5816. * @pipe: pipe PLL to disable
  5817. *
  5818. * Disable the PLL for @pipe. To be used in cases where we need
  5819. * the PLL enabled even when @pipe is not going to be enabled.
  5820. */
  5821. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5822. {
  5823. if (IS_CHERRYVIEW(dev_priv))
  5824. chv_disable_pll(dev_priv, pipe);
  5825. else
  5826. vlv_disable_pll(dev_priv, pipe);
  5827. }
  5828. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5829. struct intel_crtc_state *crtc_state,
  5830. struct dpll *reduced_clock)
  5831. {
  5832. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5833. u32 dpll;
  5834. struct dpll *clock = &crtc_state->dpll;
  5835. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5836. dpll = DPLL_VGA_MODE_DIS;
  5837. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5838. dpll |= DPLLB_MODE_LVDS;
  5839. else
  5840. dpll |= DPLLB_MODE_DAC_SERIAL;
  5841. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5842. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5843. dpll |= (crtc_state->pixel_multiplier - 1)
  5844. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5845. }
  5846. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5847. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5848. dpll |= DPLL_SDVO_HIGH_SPEED;
  5849. if (intel_crtc_has_dp_encoder(crtc_state))
  5850. dpll |= DPLL_SDVO_HIGH_SPEED;
  5851. /* compute bitmask from p1 value */
  5852. if (IS_PINEVIEW(dev_priv))
  5853. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5854. else {
  5855. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5856. if (IS_G4X(dev_priv) && reduced_clock)
  5857. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5858. }
  5859. switch (clock->p2) {
  5860. case 5:
  5861. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5862. break;
  5863. case 7:
  5864. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5865. break;
  5866. case 10:
  5867. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5868. break;
  5869. case 14:
  5870. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5871. break;
  5872. }
  5873. if (INTEL_GEN(dev_priv) >= 4)
  5874. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5875. if (crtc_state->sdvo_tv_clock)
  5876. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5877. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5878. intel_panel_use_ssc(dev_priv))
  5879. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5880. else
  5881. dpll |= PLL_REF_INPUT_DREFCLK;
  5882. dpll |= DPLL_VCO_ENABLE;
  5883. crtc_state->dpll_hw_state.dpll = dpll;
  5884. if (INTEL_GEN(dev_priv) >= 4) {
  5885. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5886. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5887. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5888. }
  5889. }
  5890. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5891. struct intel_crtc_state *crtc_state,
  5892. struct dpll *reduced_clock)
  5893. {
  5894. struct drm_device *dev = crtc->base.dev;
  5895. struct drm_i915_private *dev_priv = to_i915(dev);
  5896. u32 dpll;
  5897. struct dpll *clock = &crtc_state->dpll;
  5898. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5899. dpll = DPLL_VGA_MODE_DIS;
  5900. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5901. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5902. } else {
  5903. if (clock->p1 == 2)
  5904. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5905. else
  5906. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5907. if (clock->p2 == 4)
  5908. dpll |= PLL_P2_DIVIDE_BY_4;
  5909. }
  5910. if (!IS_I830(dev_priv) &&
  5911. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5912. dpll |= DPLL_DVO_2X_MODE;
  5913. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5914. intel_panel_use_ssc(dev_priv))
  5915. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5916. else
  5917. dpll |= PLL_REF_INPUT_DREFCLK;
  5918. dpll |= DPLL_VCO_ENABLE;
  5919. crtc_state->dpll_hw_state.dpll = dpll;
  5920. }
  5921. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5922. {
  5923. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5924. enum pipe pipe = intel_crtc->pipe;
  5925. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5926. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5927. uint32_t crtc_vtotal, crtc_vblank_end;
  5928. int vsyncshift = 0;
  5929. /* We need to be careful not to changed the adjusted mode, for otherwise
  5930. * the hw state checker will get angry at the mismatch. */
  5931. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5932. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5933. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5934. /* the chip adds 2 halflines automatically */
  5935. crtc_vtotal -= 1;
  5936. crtc_vblank_end -= 1;
  5937. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5938. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5939. else
  5940. vsyncshift = adjusted_mode->crtc_hsync_start -
  5941. adjusted_mode->crtc_htotal / 2;
  5942. if (vsyncshift < 0)
  5943. vsyncshift += adjusted_mode->crtc_htotal;
  5944. }
  5945. if (INTEL_GEN(dev_priv) > 3)
  5946. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5947. I915_WRITE(HTOTAL(cpu_transcoder),
  5948. (adjusted_mode->crtc_hdisplay - 1) |
  5949. ((adjusted_mode->crtc_htotal - 1) << 16));
  5950. I915_WRITE(HBLANK(cpu_transcoder),
  5951. (adjusted_mode->crtc_hblank_start - 1) |
  5952. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5953. I915_WRITE(HSYNC(cpu_transcoder),
  5954. (adjusted_mode->crtc_hsync_start - 1) |
  5955. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5956. I915_WRITE(VTOTAL(cpu_transcoder),
  5957. (adjusted_mode->crtc_vdisplay - 1) |
  5958. ((crtc_vtotal - 1) << 16));
  5959. I915_WRITE(VBLANK(cpu_transcoder),
  5960. (adjusted_mode->crtc_vblank_start - 1) |
  5961. ((crtc_vblank_end - 1) << 16));
  5962. I915_WRITE(VSYNC(cpu_transcoder),
  5963. (adjusted_mode->crtc_vsync_start - 1) |
  5964. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5965. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5966. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5967. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5968. * bits. */
  5969. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5970. (pipe == PIPE_B || pipe == PIPE_C))
  5971. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5972. }
  5973. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5974. {
  5975. struct drm_device *dev = intel_crtc->base.dev;
  5976. struct drm_i915_private *dev_priv = to_i915(dev);
  5977. enum pipe pipe = intel_crtc->pipe;
  5978. /* pipesrc controls the size that is scaled from, which should
  5979. * always be the user's requested size.
  5980. */
  5981. I915_WRITE(PIPESRC(pipe),
  5982. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5983. (intel_crtc->config->pipe_src_h - 1));
  5984. }
  5985. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5986. struct intel_crtc_state *pipe_config)
  5987. {
  5988. struct drm_device *dev = crtc->base.dev;
  5989. struct drm_i915_private *dev_priv = to_i915(dev);
  5990. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5991. uint32_t tmp;
  5992. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5993. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5994. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5995. tmp = I915_READ(HBLANK(cpu_transcoder));
  5996. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5997. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5998. tmp = I915_READ(HSYNC(cpu_transcoder));
  5999. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6000. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6001. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6002. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6003. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6004. tmp = I915_READ(VBLANK(cpu_transcoder));
  6005. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6006. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6007. tmp = I915_READ(VSYNC(cpu_transcoder));
  6008. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6009. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6010. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6011. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6012. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6013. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6014. }
  6015. }
  6016. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6017. struct intel_crtc_state *pipe_config)
  6018. {
  6019. struct drm_device *dev = crtc->base.dev;
  6020. struct drm_i915_private *dev_priv = to_i915(dev);
  6021. u32 tmp;
  6022. tmp = I915_READ(PIPESRC(crtc->pipe));
  6023. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6024. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6025. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6026. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6027. }
  6028. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6029. struct intel_crtc_state *pipe_config)
  6030. {
  6031. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6032. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6033. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6034. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6035. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6036. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6037. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6038. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6039. mode->flags = pipe_config->base.adjusted_mode.flags;
  6040. mode->type = DRM_MODE_TYPE_DRIVER;
  6041. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6042. mode->hsync = drm_mode_hsync(mode);
  6043. mode->vrefresh = drm_mode_vrefresh(mode);
  6044. drm_mode_set_name(mode);
  6045. }
  6046. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6047. {
  6048. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6049. uint32_t pipeconf;
  6050. pipeconf = 0;
  6051. /* we keep both pipes enabled on 830 */
  6052. if (IS_I830(dev_priv))
  6053. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6054. if (intel_crtc->config->double_wide)
  6055. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6056. /* only g4x and later have fancy bpc/dither controls */
  6057. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6058. IS_CHERRYVIEW(dev_priv)) {
  6059. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6060. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6061. pipeconf |= PIPECONF_DITHER_EN |
  6062. PIPECONF_DITHER_TYPE_SP;
  6063. switch (intel_crtc->config->pipe_bpp) {
  6064. case 18:
  6065. pipeconf |= PIPECONF_6BPC;
  6066. break;
  6067. case 24:
  6068. pipeconf |= PIPECONF_8BPC;
  6069. break;
  6070. case 30:
  6071. pipeconf |= PIPECONF_10BPC;
  6072. break;
  6073. default:
  6074. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6075. BUG();
  6076. }
  6077. }
  6078. if (HAS_PIPE_CXSR(dev_priv)) {
  6079. if (intel_crtc->lowfreq_avail) {
  6080. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6081. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6082. } else {
  6083. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6084. }
  6085. }
  6086. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6087. if (INTEL_GEN(dev_priv) < 4 ||
  6088. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6089. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6090. else
  6091. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6092. } else
  6093. pipeconf |= PIPECONF_PROGRESSIVE;
  6094. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6095. intel_crtc->config->limited_color_range)
  6096. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6097. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6098. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6099. }
  6100. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6101. struct intel_crtc_state *crtc_state)
  6102. {
  6103. struct drm_device *dev = crtc->base.dev;
  6104. struct drm_i915_private *dev_priv = to_i915(dev);
  6105. const struct intel_limit *limit;
  6106. int refclk = 48000;
  6107. memset(&crtc_state->dpll_hw_state, 0,
  6108. sizeof(crtc_state->dpll_hw_state));
  6109. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6110. if (intel_panel_use_ssc(dev_priv)) {
  6111. refclk = dev_priv->vbt.lvds_ssc_freq;
  6112. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6113. }
  6114. limit = &intel_limits_i8xx_lvds;
  6115. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6116. limit = &intel_limits_i8xx_dvo;
  6117. } else {
  6118. limit = &intel_limits_i8xx_dac;
  6119. }
  6120. if (!crtc_state->clock_set &&
  6121. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6122. refclk, NULL, &crtc_state->dpll)) {
  6123. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6124. return -EINVAL;
  6125. }
  6126. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6127. return 0;
  6128. }
  6129. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6130. struct intel_crtc_state *crtc_state)
  6131. {
  6132. struct drm_device *dev = crtc->base.dev;
  6133. struct drm_i915_private *dev_priv = to_i915(dev);
  6134. const struct intel_limit *limit;
  6135. int refclk = 96000;
  6136. memset(&crtc_state->dpll_hw_state, 0,
  6137. sizeof(crtc_state->dpll_hw_state));
  6138. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6139. if (intel_panel_use_ssc(dev_priv)) {
  6140. refclk = dev_priv->vbt.lvds_ssc_freq;
  6141. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6142. }
  6143. if (intel_is_dual_link_lvds(dev))
  6144. limit = &intel_limits_g4x_dual_channel_lvds;
  6145. else
  6146. limit = &intel_limits_g4x_single_channel_lvds;
  6147. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6148. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6149. limit = &intel_limits_g4x_hdmi;
  6150. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6151. limit = &intel_limits_g4x_sdvo;
  6152. } else {
  6153. /* The option is for other outputs */
  6154. limit = &intel_limits_i9xx_sdvo;
  6155. }
  6156. if (!crtc_state->clock_set &&
  6157. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6158. refclk, NULL, &crtc_state->dpll)) {
  6159. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6160. return -EINVAL;
  6161. }
  6162. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6163. return 0;
  6164. }
  6165. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6166. struct intel_crtc_state *crtc_state)
  6167. {
  6168. struct drm_device *dev = crtc->base.dev;
  6169. struct drm_i915_private *dev_priv = to_i915(dev);
  6170. const struct intel_limit *limit;
  6171. int refclk = 96000;
  6172. memset(&crtc_state->dpll_hw_state, 0,
  6173. sizeof(crtc_state->dpll_hw_state));
  6174. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6175. if (intel_panel_use_ssc(dev_priv)) {
  6176. refclk = dev_priv->vbt.lvds_ssc_freq;
  6177. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6178. }
  6179. limit = &intel_limits_pineview_lvds;
  6180. } else {
  6181. limit = &intel_limits_pineview_sdvo;
  6182. }
  6183. if (!crtc_state->clock_set &&
  6184. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6185. refclk, NULL, &crtc_state->dpll)) {
  6186. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6187. return -EINVAL;
  6188. }
  6189. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6190. return 0;
  6191. }
  6192. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6193. struct intel_crtc_state *crtc_state)
  6194. {
  6195. struct drm_device *dev = crtc->base.dev;
  6196. struct drm_i915_private *dev_priv = to_i915(dev);
  6197. const struct intel_limit *limit;
  6198. int refclk = 96000;
  6199. memset(&crtc_state->dpll_hw_state, 0,
  6200. sizeof(crtc_state->dpll_hw_state));
  6201. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6202. if (intel_panel_use_ssc(dev_priv)) {
  6203. refclk = dev_priv->vbt.lvds_ssc_freq;
  6204. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6205. }
  6206. limit = &intel_limits_i9xx_lvds;
  6207. } else {
  6208. limit = &intel_limits_i9xx_sdvo;
  6209. }
  6210. if (!crtc_state->clock_set &&
  6211. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6212. refclk, NULL, &crtc_state->dpll)) {
  6213. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6214. return -EINVAL;
  6215. }
  6216. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6217. return 0;
  6218. }
  6219. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6220. struct intel_crtc_state *crtc_state)
  6221. {
  6222. int refclk = 100000;
  6223. const struct intel_limit *limit = &intel_limits_chv;
  6224. memset(&crtc_state->dpll_hw_state, 0,
  6225. sizeof(crtc_state->dpll_hw_state));
  6226. if (!crtc_state->clock_set &&
  6227. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6228. refclk, NULL, &crtc_state->dpll)) {
  6229. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6230. return -EINVAL;
  6231. }
  6232. chv_compute_dpll(crtc, crtc_state);
  6233. return 0;
  6234. }
  6235. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6236. struct intel_crtc_state *crtc_state)
  6237. {
  6238. int refclk = 100000;
  6239. const struct intel_limit *limit = &intel_limits_vlv;
  6240. memset(&crtc_state->dpll_hw_state, 0,
  6241. sizeof(crtc_state->dpll_hw_state));
  6242. if (!crtc_state->clock_set &&
  6243. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6244. refclk, NULL, &crtc_state->dpll)) {
  6245. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6246. return -EINVAL;
  6247. }
  6248. vlv_compute_dpll(crtc, crtc_state);
  6249. return 0;
  6250. }
  6251. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6252. struct intel_crtc_state *pipe_config)
  6253. {
  6254. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6255. uint32_t tmp;
  6256. if (INTEL_GEN(dev_priv) <= 3 &&
  6257. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6258. return;
  6259. tmp = I915_READ(PFIT_CONTROL);
  6260. if (!(tmp & PFIT_ENABLE))
  6261. return;
  6262. /* Check whether the pfit is attached to our pipe. */
  6263. if (INTEL_GEN(dev_priv) < 4) {
  6264. if (crtc->pipe != PIPE_B)
  6265. return;
  6266. } else {
  6267. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6268. return;
  6269. }
  6270. pipe_config->gmch_pfit.control = tmp;
  6271. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6272. }
  6273. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6274. struct intel_crtc_state *pipe_config)
  6275. {
  6276. struct drm_device *dev = crtc->base.dev;
  6277. struct drm_i915_private *dev_priv = to_i915(dev);
  6278. int pipe = pipe_config->cpu_transcoder;
  6279. struct dpll clock;
  6280. u32 mdiv;
  6281. int refclk = 100000;
  6282. /* In case of DSI, DPLL will not be used */
  6283. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6284. return;
  6285. mutex_lock(&dev_priv->sb_lock);
  6286. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6287. mutex_unlock(&dev_priv->sb_lock);
  6288. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6289. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6290. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6291. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6292. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6293. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6294. }
  6295. static void
  6296. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6297. struct intel_initial_plane_config *plane_config)
  6298. {
  6299. struct drm_device *dev = crtc->base.dev;
  6300. struct drm_i915_private *dev_priv = to_i915(dev);
  6301. u32 val, base, offset;
  6302. int pipe = crtc->pipe, plane = crtc->plane;
  6303. int fourcc, pixel_format;
  6304. unsigned int aligned_height;
  6305. struct drm_framebuffer *fb;
  6306. struct intel_framebuffer *intel_fb;
  6307. val = I915_READ(DSPCNTR(plane));
  6308. if (!(val & DISPLAY_PLANE_ENABLE))
  6309. return;
  6310. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6311. if (!intel_fb) {
  6312. DRM_DEBUG_KMS("failed to alloc fb\n");
  6313. return;
  6314. }
  6315. fb = &intel_fb->base;
  6316. fb->dev = dev;
  6317. if (INTEL_GEN(dev_priv) >= 4) {
  6318. if (val & DISPPLANE_TILED) {
  6319. plane_config->tiling = I915_TILING_X;
  6320. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6321. }
  6322. }
  6323. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6324. fourcc = i9xx_format_to_fourcc(pixel_format);
  6325. fb->format = drm_format_info(fourcc);
  6326. if (INTEL_GEN(dev_priv) >= 4) {
  6327. if (plane_config->tiling)
  6328. offset = I915_READ(DSPTILEOFF(plane));
  6329. else
  6330. offset = I915_READ(DSPLINOFF(plane));
  6331. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6332. } else {
  6333. base = I915_READ(DSPADDR(plane));
  6334. }
  6335. plane_config->base = base;
  6336. val = I915_READ(PIPESRC(pipe));
  6337. fb->width = ((val >> 16) & 0xfff) + 1;
  6338. fb->height = ((val >> 0) & 0xfff) + 1;
  6339. val = I915_READ(DSPSTRIDE(pipe));
  6340. fb->pitches[0] = val & 0xffffffc0;
  6341. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6342. plane_config->size = fb->pitches[0] * aligned_height;
  6343. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6344. pipe_name(pipe), plane, fb->width, fb->height,
  6345. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6346. plane_config->size);
  6347. plane_config->fb = intel_fb;
  6348. }
  6349. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6350. struct intel_crtc_state *pipe_config)
  6351. {
  6352. struct drm_device *dev = crtc->base.dev;
  6353. struct drm_i915_private *dev_priv = to_i915(dev);
  6354. int pipe = pipe_config->cpu_transcoder;
  6355. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6356. struct dpll clock;
  6357. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6358. int refclk = 100000;
  6359. /* In case of DSI, DPLL will not be used */
  6360. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6361. return;
  6362. mutex_lock(&dev_priv->sb_lock);
  6363. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6364. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6365. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6366. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6367. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6368. mutex_unlock(&dev_priv->sb_lock);
  6369. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6370. clock.m2 = (pll_dw0 & 0xff) << 22;
  6371. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6372. clock.m2 |= pll_dw2 & 0x3fffff;
  6373. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6374. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6375. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6376. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6377. }
  6378. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6379. struct intel_crtc_state *pipe_config)
  6380. {
  6381. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6382. enum intel_display_power_domain power_domain;
  6383. uint32_t tmp;
  6384. bool ret;
  6385. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6386. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6387. return false;
  6388. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6389. pipe_config->shared_dpll = NULL;
  6390. ret = false;
  6391. tmp = I915_READ(PIPECONF(crtc->pipe));
  6392. if (!(tmp & PIPECONF_ENABLE))
  6393. goto out;
  6394. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6395. IS_CHERRYVIEW(dev_priv)) {
  6396. switch (tmp & PIPECONF_BPC_MASK) {
  6397. case PIPECONF_6BPC:
  6398. pipe_config->pipe_bpp = 18;
  6399. break;
  6400. case PIPECONF_8BPC:
  6401. pipe_config->pipe_bpp = 24;
  6402. break;
  6403. case PIPECONF_10BPC:
  6404. pipe_config->pipe_bpp = 30;
  6405. break;
  6406. default:
  6407. break;
  6408. }
  6409. }
  6410. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6411. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6412. pipe_config->limited_color_range = true;
  6413. if (INTEL_GEN(dev_priv) < 4)
  6414. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6415. intel_get_pipe_timings(crtc, pipe_config);
  6416. intel_get_pipe_src_size(crtc, pipe_config);
  6417. i9xx_get_pfit_config(crtc, pipe_config);
  6418. if (INTEL_GEN(dev_priv) >= 4) {
  6419. /* No way to read it out on pipes B and C */
  6420. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6421. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6422. else
  6423. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6424. pipe_config->pixel_multiplier =
  6425. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6426. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6427. pipe_config->dpll_hw_state.dpll_md = tmp;
  6428. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6429. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6430. tmp = I915_READ(DPLL(crtc->pipe));
  6431. pipe_config->pixel_multiplier =
  6432. ((tmp & SDVO_MULTIPLIER_MASK)
  6433. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6434. } else {
  6435. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6436. * port and will be fixed up in the encoder->get_config
  6437. * function. */
  6438. pipe_config->pixel_multiplier = 1;
  6439. }
  6440. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6441. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6442. /*
  6443. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6444. * on 830. Filter it out here so that we don't
  6445. * report errors due to that.
  6446. */
  6447. if (IS_I830(dev_priv))
  6448. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6449. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6450. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6451. } else {
  6452. /* Mask out read-only status bits. */
  6453. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6454. DPLL_PORTC_READY_MASK |
  6455. DPLL_PORTB_READY_MASK);
  6456. }
  6457. if (IS_CHERRYVIEW(dev_priv))
  6458. chv_crtc_clock_get(crtc, pipe_config);
  6459. else if (IS_VALLEYVIEW(dev_priv))
  6460. vlv_crtc_clock_get(crtc, pipe_config);
  6461. else
  6462. i9xx_crtc_clock_get(crtc, pipe_config);
  6463. /*
  6464. * Normally the dotclock is filled in by the encoder .get_config()
  6465. * but in case the pipe is enabled w/o any ports we need a sane
  6466. * default.
  6467. */
  6468. pipe_config->base.adjusted_mode.crtc_clock =
  6469. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6470. ret = true;
  6471. out:
  6472. intel_display_power_put(dev_priv, power_domain);
  6473. return ret;
  6474. }
  6475. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6476. {
  6477. struct intel_encoder *encoder;
  6478. int i;
  6479. u32 val, final;
  6480. bool has_lvds = false;
  6481. bool has_cpu_edp = false;
  6482. bool has_panel = false;
  6483. bool has_ck505 = false;
  6484. bool can_ssc = false;
  6485. bool using_ssc_source = false;
  6486. /* We need to take the global config into account */
  6487. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6488. switch (encoder->type) {
  6489. case INTEL_OUTPUT_LVDS:
  6490. has_panel = true;
  6491. has_lvds = true;
  6492. break;
  6493. case INTEL_OUTPUT_EDP:
  6494. has_panel = true;
  6495. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6496. has_cpu_edp = true;
  6497. break;
  6498. default:
  6499. break;
  6500. }
  6501. }
  6502. if (HAS_PCH_IBX(dev_priv)) {
  6503. has_ck505 = dev_priv->vbt.display_clock_mode;
  6504. can_ssc = has_ck505;
  6505. } else {
  6506. has_ck505 = false;
  6507. can_ssc = true;
  6508. }
  6509. /* Check if any DPLLs are using the SSC source */
  6510. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6511. u32 temp = I915_READ(PCH_DPLL(i));
  6512. if (!(temp & DPLL_VCO_ENABLE))
  6513. continue;
  6514. if ((temp & PLL_REF_INPUT_MASK) ==
  6515. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6516. using_ssc_source = true;
  6517. break;
  6518. }
  6519. }
  6520. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6521. has_panel, has_lvds, has_ck505, using_ssc_source);
  6522. /* Ironlake: try to setup display ref clock before DPLL
  6523. * enabling. This is only under driver's control after
  6524. * PCH B stepping, previous chipset stepping should be
  6525. * ignoring this setting.
  6526. */
  6527. val = I915_READ(PCH_DREF_CONTROL);
  6528. /* As we must carefully and slowly disable/enable each source in turn,
  6529. * compute the final state we want first and check if we need to
  6530. * make any changes at all.
  6531. */
  6532. final = val;
  6533. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6534. if (has_ck505)
  6535. final |= DREF_NONSPREAD_CK505_ENABLE;
  6536. else
  6537. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6538. final &= ~DREF_SSC_SOURCE_MASK;
  6539. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6540. final &= ~DREF_SSC1_ENABLE;
  6541. if (has_panel) {
  6542. final |= DREF_SSC_SOURCE_ENABLE;
  6543. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6544. final |= DREF_SSC1_ENABLE;
  6545. if (has_cpu_edp) {
  6546. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6547. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6548. else
  6549. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6550. } else
  6551. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6552. } else if (using_ssc_source) {
  6553. final |= DREF_SSC_SOURCE_ENABLE;
  6554. final |= DREF_SSC1_ENABLE;
  6555. }
  6556. if (final == val)
  6557. return;
  6558. /* Always enable nonspread source */
  6559. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6560. if (has_ck505)
  6561. val |= DREF_NONSPREAD_CK505_ENABLE;
  6562. else
  6563. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6564. if (has_panel) {
  6565. val &= ~DREF_SSC_SOURCE_MASK;
  6566. val |= DREF_SSC_SOURCE_ENABLE;
  6567. /* SSC must be turned on before enabling the CPU output */
  6568. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6569. DRM_DEBUG_KMS("Using SSC on panel\n");
  6570. val |= DREF_SSC1_ENABLE;
  6571. } else
  6572. val &= ~DREF_SSC1_ENABLE;
  6573. /* Get SSC going before enabling the outputs */
  6574. I915_WRITE(PCH_DREF_CONTROL, val);
  6575. POSTING_READ(PCH_DREF_CONTROL);
  6576. udelay(200);
  6577. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6578. /* Enable CPU source on CPU attached eDP */
  6579. if (has_cpu_edp) {
  6580. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6581. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6582. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6583. } else
  6584. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6585. } else
  6586. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6587. I915_WRITE(PCH_DREF_CONTROL, val);
  6588. POSTING_READ(PCH_DREF_CONTROL);
  6589. udelay(200);
  6590. } else {
  6591. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6592. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6593. /* Turn off CPU output */
  6594. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6595. I915_WRITE(PCH_DREF_CONTROL, val);
  6596. POSTING_READ(PCH_DREF_CONTROL);
  6597. udelay(200);
  6598. if (!using_ssc_source) {
  6599. DRM_DEBUG_KMS("Disabling SSC source\n");
  6600. /* Turn off the SSC source */
  6601. val &= ~DREF_SSC_SOURCE_MASK;
  6602. val |= DREF_SSC_SOURCE_DISABLE;
  6603. /* Turn off SSC1 */
  6604. val &= ~DREF_SSC1_ENABLE;
  6605. I915_WRITE(PCH_DREF_CONTROL, val);
  6606. POSTING_READ(PCH_DREF_CONTROL);
  6607. udelay(200);
  6608. }
  6609. }
  6610. BUG_ON(val != final);
  6611. }
  6612. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6613. {
  6614. uint32_t tmp;
  6615. tmp = I915_READ(SOUTH_CHICKEN2);
  6616. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6617. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6618. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6619. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6620. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6621. tmp = I915_READ(SOUTH_CHICKEN2);
  6622. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6623. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6624. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6625. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6626. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6627. }
  6628. /* WaMPhyProgramming:hsw */
  6629. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6630. {
  6631. uint32_t tmp;
  6632. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6633. tmp &= ~(0xFF << 24);
  6634. tmp |= (0x12 << 24);
  6635. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6636. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6637. tmp |= (1 << 11);
  6638. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6639. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6640. tmp |= (1 << 11);
  6641. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6642. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6643. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6644. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6645. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6646. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6647. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6648. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6649. tmp &= ~(7 << 13);
  6650. tmp |= (5 << 13);
  6651. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6652. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6653. tmp &= ~(7 << 13);
  6654. tmp |= (5 << 13);
  6655. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6656. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6657. tmp &= ~0xFF;
  6658. tmp |= 0x1C;
  6659. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6660. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6661. tmp &= ~0xFF;
  6662. tmp |= 0x1C;
  6663. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6664. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6665. tmp &= ~(0xFF << 16);
  6666. tmp |= (0x1C << 16);
  6667. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6668. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6669. tmp &= ~(0xFF << 16);
  6670. tmp |= (0x1C << 16);
  6671. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6672. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6673. tmp |= (1 << 27);
  6674. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6675. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6676. tmp |= (1 << 27);
  6677. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6678. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6679. tmp &= ~(0xF << 28);
  6680. tmp |= (4 << 28);
  6681. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6682. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6683. tmp &= ~(0xF << 28);
  6684. tmp |= (4 << 28);
  6685. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6686. }
  6687. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6688. * Programming" based on the parameters passed:
  6689. * - Sequence to enable CLKOUT_DP
  6690. * - Sequence to enable CLKOUT_DP without spread
  6691. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6692. */
  6693. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6694. bool with_spread, bool with_fdi)
  6695. {
  6696. uint32_t reg, tmp;
  6697. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6698. with_spread = true;
  6699. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6700. with_fdi, "LP PCH doesn't have FDI\n"))
  6701. with_fdi = false;
  6702. mutex_lock(&dev_priv->sb_lock);
  6703. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6704. tmp &= ~SBI_SSCCTL_DISABLE;
  6705. tmp |= SBI_SSCCTL_PATHALT;
  6706. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6707. udelay(24);
  6708. if (with_spread) {
  6709. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6710. tmp &= ~SBI_SSCCTL_PATHALT;
  6711. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6712. if (with_fdi) {
  6713. lpt_reset_fdi_mphy(dev_priv);
  6714. lpt_program_fdi_mphy(dev_priv);
  6715. }
  6716. }
  6717. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6718. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6719. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6720. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6721. mutex_unlock(&dev_priv->sb_lock);
  6722. }
  6723. /* Sequence to disable CLKOUT_DP */
  6724. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6725. {
  6726. uint32_t reg, tmp;
  6727. mutex_lock(&dev_priv->sb_lock);
  6728. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6729. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6730. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6731. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6732. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6733. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6734. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6735. tmp |= SBI_SSCCTL_PATHALT;
  6736. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6737. udelay(32);
  6738. }
  6739. tmp |= SBI_SSCCTL_DISABLE;
  6740. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6741. }
  6742. mutex_unlock(&dev_priv->sb_lock);
  6743. }
  6744. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6745. static const uint16_t sscdivintphase[] = {
  6746. [BEND_IDX( 50)] = 0x3B23,
  6747. [BEND_IDX( 45)] = 0x3B23,
  6748. [BEND_IDX( 40)] = 0x3C23,
  6749. [BEND_IDX( 35)] = 0x3C23,
  6750. [BEND_IDX( 30)] = 0x3D23,
  6751. [BEND_IDX( 25)] = 0x3D23,
  6752. [BEND_IDX( 20)] = 0x3E23,
  6753. [BEND_IDX( 15)] = 0x3E23,
  6754. [BEND_IDX( 10)] = 0x3F23,
  6755. [BEND_IDX( 5)] = 0x3F23,
  6756. [BEND_IDX( 0)] = 0x0025,
  6757. [BEND_IDX( -5)] = 0x0025,
  6758. [BEND_IDX(-10)] = 0x0125,
  6759. [BEND_IDX(-15)] = 0x0125,
  6760. [BEND_IDX(-20)] = 0x0225,
  6761. [BEND_IDX(-25)] = 0x0225,
  6762. [BEND_IDX(-30)] = 0x0325,
  6763. [BEND_IDX(-35)] = 0x0325,
  6764. [BEND_IDX(-40)] = 0x0425,
  6765. [BEND_IDX(-45)] = 0x0425,
  6766. [BEND_IDX(-50)] = 0x0525,
  6767. };
  6768. /*
  6769. * Bend CLKOUT_DP
  6770. * steps -50 to 50 inclusive, in steps of 5
  6771. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6772. * change in clock period = -(steps / 10) * 5.787 ps
  6773. */
  6774. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6775. {
  6776. uint32_t tmp;
  6777. int idx = BEND_IDX(steps);
  6778. if (WARN_ON(steps % 5 != 0))
  6779. return;
  6780. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6781. return;
  6782. mutex_lock(&dev_priv->sb_lock);
  6783. if (steps % 10 != 0)
  6784. tmp = 0xAAAAAAAB;
  6785. else
  6786. tmp = 0x00000000;
  6787. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6788. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6789. tmp &= 0xffff0000;
  6790. tmp |= sscdivintphase[idx];
  6791. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6792. mutex_unlock(&dev_priv->sb_lock);
  6793. }
  6794. #undef BEND_IDX
  6795. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6796. {
  6797. struct intel_encoder *encoder;
  6798. bool has_vga = false;
  6799. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6800. switch (encoder->type) {
  6801. case INTEL_OUTPUT_ANALOG:
  6802. has_vga = true;
  6803. break;
  6804. default:
  6805. break;
  6806. }
  6807. }
  6808. if (has_vga) {
  6809. lpt_bend_clkout_dp(dev_priv, 0);
  6810. lpt_enable_clkout_dp(dev_priv, true, true);
  6811. } else {
  6812. lpt_disable_clkout_dp(dev_priv);
  6813. }
  6814. }
  6815. /*
  6816. * Initialize reference clocks when the driver loads
  6817. */
  6818. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6819. {
  6820. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6821. ironlake_init_pch_refclk(dev_priv);
  6822. else if (HAS_PCH_LPT(dev_priv))
  6823. lpt_init_pch_refclk(dev_priv);
  6824. }
  6825. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6826. {
  6827. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6829. int pipe = intel_crtc->pipe;
  6830. uint32_t val;
  6831. val = 0;
  6832. switch (intel_crtc->config->pipe_bpp) {
  6833. case 18:
  6834. val |= PIPECONF_6BPC;
  6835. break;
  6836. case 24:
  6837. val |= PIPECONF_8BPC;
  6838. break;
  6839. case 30:
  6840. val |= PIPECONF_10BPC;
  6841. break;
  6842. case 36:
  6843. val |= PIPECONF_12BPC;
  6844. break;
  6845. default:
  6846. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6847. BUG();
  6848. }
  6849. if (intel_crtc->config->dither)
  6850. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6851. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6852. val |= PIPECONF_INTERLACED_ILK;
  6853. else
  6854. val |= PIPECONF_PROGRESSIVE;
  6855. if (intel_crtc->config->limited_color_range)
  6856. val |= PIPECONF_COLOR_RANGE_SELECT;
  6857. I915_WRITE(PIPECONF(pipe), val);
  6858. POSTING_READ(PIPECONF(pipe));
  6859. }
  6860. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6861. {
  6862. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6863. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6864. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6865. u32 val = 0;
  6866. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6867. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6868. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6869. val |= PIPECONF_INTERLACED_ILK;
  6870. else
  6871. val |= PIPECONF_PROGRESSIVE;
  6872. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6873. POSTING_READ(PIPECONF(cpu_transcoder));
  6874. }
  6875. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6876. {
  6877. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6878. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6879. struct intel_crtc_state *config = intel_crtc->config;
  6880. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6881. u32 val = 0;
  6882. switch (intel_crtc->config->pipe_bpp) {
  6883. case 18:
  6884. val |= PIPEMISC_DITHER_6_BPC;
  6885. break;
  6886. case 24:
  6887. val |= PIPEMISC_DITHER_8_BPC;
  6888. break;
  6889. case 30:
  6890. val |= PIPEMISC_DITHER_10_BPC;
  6891. break;
  6892. case 36:
  6893. val |= PIPEMISC_DITHER_12_BPC;
  6894. break;
  6895. default:
  6896. /* Case prevented by pipe_config_set_bpp. */
  6897. BUG();
  6898. }
  6899. if (intel_crtc->config->dither)
  6900. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6901. if (config->ycbcr420) {
  6902. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6903. PIPEMISC_YUV420_ENABLE |
  6904. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6905. }
  6906. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6907. }
  6908. }
  6909. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6910. {
  6911. /*
  6912. * Account for spread spectrum to avoid
  6913. * oversubscribing the link. Max center spread
  6914. * is 2.5%; use 5% for safety's sake.
  6915. */
  6916. u32 bps = target_clock * bpp * 21 / 20;
  6917. return DIV_ROUND_UP(bps, link_bw * 8);
  6918. }
  6919. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6920. {
  6921. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6922. }
  6923. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6924. struct intel_crtc_state *crtc_state,
  6925. struct dpll *reduced_clock)
  6926. {
  6927. struct drm_crtc *crtc = &intel_crtc->base;
  6928. struct drm_device *dev = crtc->dev;
  6929. struct drm_i915_private *dev_priv = to_i915(dev);
  6930. u32 dpll, fp, fp2;
  6931. int factor;
  6932. /* Enable autotuning of the PLL clock (if permissible) */
  6933. factor = 21;
  6934. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6935. if ((intel_panel_use_ssc(dev_priv) &&
  6936. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6937. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6938. factor = 25;
  6939. } else if (crtc_state->sdvo_tv_clock)
  6940. factor = 20;
  6941. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6942. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6943. fp |= FP_CB_TUNE;
  6944. if (reduced_clock) {
  6945. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6946. if (reduced_clock->m < factor * reduced_clock->n)
  6947. fp2 |= FP_CB_TUNE;
  6948. } else {
  6949. fp2 = fp;
  6950. }
  6951. dpll = 0;
  6952. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6953. dpll |= DPLLB_MODE_LVDS;
  6954. else
  6955. dpll |= DPLLB_MODE_DAC_SERIAL;
  6956. dpll |= (crtc_state->pixel_multiplier - 1)
  6957. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6958. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6959. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6960. dpll |= DPLL_SDVO_HIGH_SPEED;
  6961. if (intel_crtc_has_dp_encoder(crtc_state))
  6962. dpll |= DPLL_SDVO_HIGH_SPEED;
  6963. /*
  6964. * The high speed IO clock is only really required for
  6965. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6966. * possible to share the DPLL between CRT and HDMI. Enabling
  6967. * the clock needlessly does no real harm, except use up a
  6968. * bit of power potentially.
  6969. *
  6970. * We'll limit this to IVB with 3 pipes, since it has only two
  6971. * DPLLs and so DPLL sharing is the only way to get three pipes
  6972. * driving PCH ports at the same time. On SNB we could do this,
  6973. * and potentially avoid enabling the second DPLL, but it's not
  6974. * clear if it''s a win or loss power wise. No point in doing
  6975. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6976. */
  6977. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6978. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6979. dpll |= DPLL_SDVO_HIGH_SPEED;
  6980. /* compute bitmask from p1 value */
  6981. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6982. /* also FPA1 */
  6983. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6984. switch (crtc_state->dpll.p2) {
  6985. case 5:
  6986. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6987. break;
  6988. case 7:
  6989. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6990. break;
  6991. case 10:
  6992. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6993. break;
  6994. case 14:
  6995. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6996. break;
  6997. }
  6998. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6999. intel_panel_use_ssc(dev_priv))
  7000. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7001. else
  7002. dpll |= PLL_REF_INPUT_DREFCLK;
  7003. dpll |= DPLL_VCO_ENABLE;
  7004. crtc_state->dpll_hw_state.dpll = dpll;
  7005. crtc_state->dpll_hw_state.fp0 = fp;
  7006. crtc_state->dpll_hw_state.fp1 = fp2;
  7007. }
  7008. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7009. struct intel_crtc_state *crtc_state)
  7010. {
  7011. struct drm_device *dev = crtc->base.dev;
  7012. struct drm_i915_private *dev_priv = to_i915(dev);
  7013. const struct intel_limit *limit;
  7014. int refclk = 120000;
  7015. memset(&crtc_state->dpll_hw_state, 0,
  7016. sizeof(crtc_state->dpll_hw_state));
  7017. crtc->lowfreq_avail = false;
  7018. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7019. if (!crtc_state->has_pch_encoder)
  7020. return 0;
  7021. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7022. if (intel_panel_use_ssc(dev_priv)) {
  7023. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7024. dev_priv->vbt.lvds_ssc_freq);
  7025. refclk = dev_priv->vbt.lvds_ssc_freq;
  7026. }
  7027. if (intel_is_dual_link_lvds(dev)) {
  7028. if (refclk == 100000)
  7029. limit = &intel_limits_ironlake_dual_lvds_100m;
  7030. else
  7031. limit = &intel_limits_ironlake_dual_lvds;
  7032. } else {
  7033. if (refclk == 100000)
  7034. limit = &intel_limits_ironlake_single_lvds_100m;
  7035. else
  7036. limit = &intel_limits_ironlake_single_lvds;
  7037. }
  7038. } else {
  7039. limit = &intel_limits_ironlake_dac;
  7040. }
  7041. if (!crtc_state->clock_set &&
  7042. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7043. refclk, NULL, &crtc_state->dpll)) {
  7044. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7045. return -EINVAL;
  7046. }
  7047. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7048. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7049. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7050. pipe_name(crtc->pipe));
  7051. return -EINVAL;
  7052. }
  7053. return 0;
  7054. }
  7055. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7056. struct intel_link_m_n *m_n)
  7057. {
  7058. struct drm_device *dev = crtc->base.dev;
  7059. struct drm_i915_private *dev_priv = to_i915(dev);
  7060. enum pipe pipe = crtc->pipe;
  7061. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7062. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7063. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7064. & ~TU_SIZE_MASK;
  7065. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7066. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7067. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7068. }
  7069. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7070. enum transcoder transcoder,
  7071. struct intel_link_m_n *m_n,
  7072. struct intel_link_m_n *m2_n2)
  7073. {
  7074. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7075. enum pipe pipe = crtc->pipe;
  7076. if (INTEL_GEN(dev_priv) >= 5) {
  7077. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7078. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7079. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7080. & ~TU_SIZE_MASK;
  7081. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7082. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7083. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7084. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7085. * gen < 8) and if DRRS is supported (to make sure the
  7086. * registers are not unnecessarily read).
  7087. */
  7088. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7089. crtc->config->has_drrs) {
  7090. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7091. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7092. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7093. & ~TU_SIZE_MASK;
  7094. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7095. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7096. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7097. }
  7098. } else {
  7099. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7100. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7101. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7102. & ~TU_SIZE_MASK;
  7103. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7104. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7105. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7106. }
  7107. }
  7108. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7109. struct intel_crtc_state *pipe_config)
  7110. {
  7111. if (pipe_config->has_pch_encoder)
  7112. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7113. else
  7114. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7115. &pipe_config->dp_m_n,
  7116. &pipe_config->dp_m2_n2);
  7117. }
  7118. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7119. struct intel_crtc_state *pipe_config)
  7120. {
  7121. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7122. &pipe_config->fdi_m_n, NULL);
  7123. }
  7124. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7125. struct intel_crtc_state *pipe_config)
  7126. {
  7127. struct drm_device *dev = crtc->base.dev;
  7128. struct drm_i915_private *dev_priv = to_i915(dev);
  7129. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7130. uint32_t ps_ctrl = 0;
  7131. int id = -1;
  7132. int i;
  7133. /* find scaler attached to this pipe */
  7134. for (i = 0; i < crtc->num_scalers; i++) {
  7135. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7136. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7137. id = i;
  7138. pipe_config->pch_pfit.enabled = true;
  7139. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7140. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7141. break;
  7142. }
  7143. }
  7144. scaler_state->scaler_id = id;
  7145. if (id >= 0) {
  7146. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7147. } else {
  7148. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7149. }
  7150. }
  7151. static void
  7152. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7153. struct intel_initial_plane_config *plane_config)
  7154. {
  7155. struct drm_device *dev = crtc->base.dev;
  7156. struct drm_i915_private *dev_priv = to_i915(dev);
  7157. u32 val, base, offset, stride_mult, tiling;
  7158. int pipe = crtc->pipe;
  7159. int fourcc, pixel_format;
  7160. unsigned int aligned_height;
  7161. struct drm_framebuffer *fb;
  7162. struct intel_framebuffer *intel_fb;
  7163. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7164. if (!intel_fb) {
  7165. DRM_DEBUG_KMS("failed to alloc fb\n");
  7166. return;
  7167. }
  7168. fb = &intel_fb->base;
  7169. fb->dev = dev;
  7170. val = I915_READ(PLANE_CTL(pipe, 0));
  7171. if (!(val & PLANE_CTL_ENABLE))
  7172. goto error;
  7173. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7174. fourcc = skl_format_to_fourcc(pixel_format,
  7175. val & PLANE_CTL_ORDER_RGBX,
  7176. val & PLANE_CTL_ALPHA_MASK);
  7177. fb->format = drm_format_info(fourcc);
  7178. tiling = val & PLANE_CTL_TILED_MASK;
  7179. switch (tiling) {
  7180. case PLANE_CTL_TILED_LINEAR:
  7181. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7182. break;
  7183. case PLANE_CTL_TILED_X:
  7184. plane_config->tiling = I915_TILING_X;
  7185. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7186. break;
  7187. case PLANE_CTL_TILED_Y:
  7188. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7189. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7190. else
  7191. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7192. break;
  7193. case PLANE_CTL_TILED_YF:
  7194. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7195. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7196. else
  7197. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7198. break;
  7199. default:
  7200. MISSING_CASE(tiling);
  7201. goto error;
  7202. }
  7203. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7204. plane_config->base = base;
  7205. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7206. val = I915_READ(PLANE_SIZE(pipe, 0));
  7207. fb->height = ((val >> 16) & 0xfff) + 1;
  7208. fb->width = ((val >> 0) & 0x1fff) + 1;
  7209. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7210. stride_mult = intel_fb_stride_alignment(fb, 0);
  7211. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7212. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7213. plane_config->size = fb->pitches[0] * aligned_height;
  7214. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7215. pipe_name(pipe), fb->width, fb->height,
  7216. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7217. plane_config->size);
  7218. plane_config->fb = intel_fb;
  7219. return;
  7220. error:
  7221. kfree(intel_fb);
  7222. }
  7223. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7224. struct intel_crtc_state *pipe_config)
  7225. {
  7226. struct drm_device *dev = crtc->base.dev;
  7227. struct drm_i915_private *dev_priv = to_i915(dev);
  7228. uint32_t tmp;
  7229. tmp = I915_READ(PF_CTL(crtc->pipe));
  7230. if (tmp & PF_ENABLE) {
  7231. pipe_config->pch_pfit.enabled = true;
  7232. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7233. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7234. /* We currently do not free assignements of panel fitters on
  7235. * ivb/hsw (since we don't use the higher upscaling modes which
  7236. * differentiates them) so just WARN about this case for now. */
  7237. if (IS_GEN7(dev_priv)) {
  7238. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7239. PF_PIPE_SEL_IVB(crtc->pipe));
  7240. }
  7241. }
  7242. }
  7243. static void
  7244. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7245. struct intel_initial_plane_config *plane_config)
  7246. {
  7247. struct drm_device *dev = crtc->base.dev;
  7248. struct drm_i915_private *dev_priv = to_i915(dev);
  7249. u32 val, base, offset;
  7250. int pipe = crtc->pipe;
  7251. int fourcc, pixel_format;
  7252. unsigned int aligned_height;
  7253. struct drm_framebuffer *fb;
  7254. struct intel_framebuffer *intel_fb;
  7255. val = I915_READ(DSPCNTR(pipe));
  7256. if (!(val & DISPLAY_PLANE_ENABLE))
  7257. return;
  7258. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7259. if (!intel_fb) {
  7260. DRM_DEBUG_KMS("failed to alloc fb\n");
  7261. return;
  7262. }
  7263. fb = &intel_fb->base;
  7264. fb->dev = dev;
  7265. if (INTEL_GEN(dev_priv) >= 4) {
  7266. if (val & DISPPLANE_TILED) {
  7267. plane_config->tiling = I915_TILING_X;
  7268. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7269. }
  7270. }
  7271. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7272. fourcc = i9xx_format_to_fourcc(pixel_format);
  7273. fb->format = drm_format_info(fourcc);
  7274. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7275. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7276. offset = I915_READ(DSPOFFSET(pipe));
  7277. } else {
  7278. if (plane_config->tiling)
  7279. offset = I915_READ(DSPTILEOFF(pipe));
  7280. else
  7281. offset = I915_READ(DSPLINOFF(pipe));
  7282. }
  7283. plane_config->base = base;
  7284. val = I915_READ(PIPESRC(pipe));
  7285. fb->width = ((val >> 16) & 0xfff) + 1;
  7286. fb->height = ((val >> 0) & 0xfff) + 1;
  7287. val = I915_READ(DSPSTRIDE(pipe));
  7288. fb->pitches[0] = val & 0xffffffc0;
  7289. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7290. plane_config->size = fb->pitches[0] * aligned_height;
  7291. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7292. pipe_name(pipe), fb->width, fb->height,
  7293. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7294. plane_config->size);
  7295. plane_config->fb = intel_fb;
  7296. }
  7297. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7298. struct intel_crtc_state *pipe_config)
  7299. {
  7300. struct drm_device *dev = crtc->base.dev;
  7301. struct drm_i915_private *dev_priv = to_i915(dev);
  7302. enum intel_display_power_domain power_domain;
  7303. uint32_t tmp;
  7304. bool ret;
  7305. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7306. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7307. return false;
  7308. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7309. pipe_config->shared_dpll = NULL;
  7310. ret = false;
  7311. tmp = I915_READ(PIPECONF(crtc->pipe));
  7312. if (!(tmp & PIPECONF_ENABLE))
  7313. goto out;
  7314. switch (tmp & PIPECONF_BPC_MASK) {
  7315. case PIPECONF_6BPC:
  7316. pipe_config->pipe_bpp = 18;
  7317. break;
  7318. case PIPECONF_8BPC:
  7319. pipe_config->pipe_bpp = 24;
  7320. break;
  7321. case PIPECONF_10BPC:
  7322. pipe_config->pipe_bpp = 30;
  7323. break;
  7324. case PIPECONF_12BPC:
  7325. pipe_config->pipe_bpp = 36;
  7326. break;
  7327. default:
  7328. break;
  7329. }
  7330. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7331. pipe_config->limited_color_range = true;
  7332. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7333. struct intel_shared_dpll *pll;
  7334. enum intel_dpll_id pll_id;
  7335. pipe_config->has_pch_encoder = true;
  7336. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7337. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7338. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7339. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7340. if (HAS_PCH_IBX(dev_priv)) {
  7341. /*
  7342. * The pipe->pch transcoder and pch transcoder->pll
  7343. * mapping is fixed.
  7344. */
  7345. pll_id = (enum intel_dpll_id) crtc->pipe;
  7346. } else {
  7347. tmp = I915_READ(PCH_DPLL_SEL);
  7348. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7349. pll_id = DPLL_ID_PCH_PLL_B;
  7350. else
  7351. pll_id= DPLL_ID_PCH_PLL_A;
  7352. }
  7353. pipe_config->shared_dpll =
  7354. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7355. pll = pipe_config->shared_dpll;
  7356. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7357. &pipe_config->dpll_hw_state));
  7358. tmp = pipe_config->dpll_hw_state.dpll;
  7359. pipe_config->pixel_multiplier =
  7360. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7361. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7362. ironlake_pch_clock_get(crtc, pipe_config);
  7363. } else {
  7364. pipe_config->pixel_multiplier = 1;
  7365. }
  7366. intel_get_pipe_timings(crtc, pipe_config);
  7367. intel_get_pipe_src_size(crtc, pipe_config);
  7368. ironlake_get_pfit_config(crtc, pipe_config);
  7369. ret = true;
  7370. out:
  7371. intel_display_power_put(dev_priv, power_domain);
  7372. return ret;
  7373. }
  7374. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7375. {
  7376. struct drm_device *dev = &dev_priv->drm;
  7377. struct intel_crtc *crtc;
  7378. for_each_intel_crtc(dev, crtc)
  7379. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7380. pipe_name(crtc->pipe));
  7381. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7382. "Display power well on\n");
  7383. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7384. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7385. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7386. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7387. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7388. "CPU PWM1 enabled\n");
  7389. if (IS_HASWELL(dev_priv))
  7390. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7391. "CPU PWM2 enabled\n");
  7392. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7393. "PCH PWM1 enabled\n");
  7394. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7395. "Utility pin enabled\n");
  7396. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7397. /*
  7398. * In theory we can still leave IRQs enabled, as long as only the HPD
  7399. * interrupts remain enabled. We used to check for that, but since it's
  7400. * gen-specific and since we only disable LCPLL after we fully disable
  7401. * the interrupts, the check below should be enough.
  7402. */
  7403. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7404. }
  7405. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7406. {
  7407. if (IS_HASWELL(dev_priv))
  7408. return I915_READ(D_COMP_HSW);
  7409. else
  7410. return I915_READ(D_COMP_BDW);
  7411. }
  7412. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7413. {
  7414. if (IS_HASWELL(dev_priv)) {
  7415. mutex_lock(&dev_priv->rps.hw_lock);
  7416. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7417. val))
  7418. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7419. mutex_unlock(&dev_priv->rps.hw_lock);
  7420. } else {
  7421. I915_WRITE(D_COMP_BDW, val);
  7422. POSTING_READ(D_COMP_BDW);
  7423. }
  7424. }
  7425. /*
  7426. * This function implements pieces of two sequences from BSpec:
  7427. * - Sequence for display software to disable LCPLL
  7428. * - Sequence for display software to allow package C8+
  7429. * The steps implemented here are just the steps that actually touch the LCPLL
  7430. * register. Callers should take care of disabling all the display engine
  7431. * functions, doing the mode unset, fixing interrupts, etc.
  7432. */
  7433. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7434. bool switch_to_fclk, bool allow_power_down)
  7435. {
  7436. uint32_t val;
  7437. assert_can_disable_lcpll(dev_priv);
  7438. val = I915_READ(LCPLL_CTL);
  7439. if (switch_to_fclk) {
  7440. val |= LCPLL_CD_SOURCE_FCLK;
  7441. I915_WRITE(LCPLL_CTL, val);
  7442. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7443. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7444. DRM_ERROR("Switching to FCLK failed\n");
  7445. val = I915_READ(LCPLL_CTL);
  7446. }
  7447. val |= LCPLL_PLL_DISABLE;
  7448. I915_WRITE(LCPLL_CTL, val);
  7449. POSTING_READ(LCPLL_CTL);
  7450. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7451. DRM_ERROR("LCPLL still locked\n");
  7452. val = hsw_read_dcomp(dev_priv);
  7453. val |= D_COMP_COMP_DISABLE;
  7454. hsw_write_dcomp(dev_priv, val);
  7455. ndelay(100);
  7456. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7457. 1))
  7458. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7459. if (allow_power_down) {
  7460. val = I915_READ(LCPLL_CTL);
  7461. val |= LCPLL_POWER_DOWN_ALLOW;
  7462. I915_WRITE(LCPLL_CTL, val);
  7463. POSTING_READ(LCPLL_CTL);
  7464. }
  7465. }
  7466. /*
  7467. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7468. * source.
  7469. */
  7470. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7471. {
  7472. uint32_t val;
  7473. val = I915_READ(LCPLL_CTL);
  7474. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7475. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7476. return;
  7477. /*
  7478. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7479. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7480. */
  7481. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7482. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7483. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7484. I915_WRITE(LCPLL_CTL, val);
  7485. POSTING_READ(LCPLL_CTL);
  7486. }
  7487. val = hsw_read_dcomp(dev_priv);
  7488. val |= D_COMP_COMP_FORCE;
  7489. val &= ~D_COMP_COMP_DISABLE;
  7490. hsw_write_dcomp(dev_priv, val);
  7491. val = I915_READ(LCPLL_CTL);
  7492. val &= ~LCPLL_PLL_DISABLE;
  7493. I915_WRITE(LCPLL_CTL, val);
  7494. if (intel_wait_for_register(dev_priv,
  7495. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7496. 5))
  7497. DRM_ERROR("LCPLL not locked yet\n");
  7498. if (val & LCPLL_CD_SOURCE_FCLK) {
  7499. val = I915_READ(LCPLL_CTL);
  7500. val &= ~LCPLL_CD_SOURCE_FCLK;
  7501. I915_WRITE(LCPLL_CTL, val);
  7502. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7503. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7504. DRM_ERROR("Switching back to LCPLL failed\n");
  7505. }
  7506. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7507. intel_update_cdclk(dev_priv);
  7508. }
  7509. /*
  7510. * Package states C8 and deeper are really deep PC states that can only be
  7511. * reached when all the devices on the system allow it, so even if the graphics
  7512. * device allows PC8+, it doesn't mean the system will actually get to these
  7513. * states. Our driver only allows PC8+ when going into runtime PM.
  7514. *
  7515. * The requirements for PC8+ are that all the outputs are disabled, the power
  7516. * well is disabled and most interrupts are disabled, and these are also
  7517. * requirements for runtime PM. When these conditions are met, we manually do
  7518. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7519. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7520. * hang the machine.
  7521. *
  7522. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7523. * the state of some registers, so when we come back from PC8+ we need to
  7524. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7525. * need to take care of the registers kept by RC6. Notice that this happens even
  7526. * if we don't put the device in PCI D3 state (which is what currently happens
  7527. * because of the runtime PM support).
  7528. *
  7529. * For more, read "Display Sequences for Package C8" on the hardware
  7530. * documentation.
  7531. */
  7532. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7533. {
  7534. uint32_t val;
  7535. DRM_DEBUG_KMS("Enabling package C8+\n");
  7536. if (HAS_PCH_LPT_LP(dev_priv)) {
  7537. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7538. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7539. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7540. }
  7541. lpt_disable_clkout_dp(dev_priv);
  7542. hsw_disable_lcpll(dev_priv, true, true);
  7543. }
  7544. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7545. {
  7546. uint32_t val;
  7547. DRM_DEBUG_KMS("Disabling package C8+\n");
  7548. hsw_restore_lcpll(dev_priv);
  7549. lpt_init_pch_refclk(dev_priv);
  7550. if (HAS_PCH_LPT_LP(dev_priv)) {
  7551. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7552. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7553. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7554. }
  7555. }
  7556. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7557. struct intel_crtc_state *crtc_state)
  7558. {
  7559. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7560. struct intel_encoder *encoder =
  7561. intel_ddi_get_crtc_new_encoder(crtc_state);
  7562. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7563. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7564. pipe_name(crtc->pipe));
  7565. return -EINVAL;
  7566. }
  7567. }
  7568. crtc->lowfreq_avail = false;
  7569. return 0;
  7570. }
  7571. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7572. enum port port,
  7573. struct intel_crtc_state *pipe_config)
  7574. {
  7575. enum intel_dpll_id id;
  7576. u32 temp;
  7577. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7578. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7579. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7580. return;
  7581. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7582. }
  7583. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7584. enum port port,
  7585. struct intel_crtc_state *pipe_config)
  7586. {
  7587. enum intel_dpll_id id;
  7588. switch (port) {
  7589. case PORT_A:
  7590. id = DPLL_ID_SKL_DPLL0;
  7591. break;
  7592. case PORT_B:
  7593. id = DPLL_ID_SKL_DPLL1;
  7594. break;
  7595. case PORT_C:
  7596. id = DPLL_ID_SKL_DPLL2;
  7597. break;
  7598. default:
  7599. DRM_ERROR("Incorrect port type\n");
  7600. return;
  7601. }
  7602. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7603. }
  7604. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7605. enum port port,
  7606. struct intel_crtc_state *pipe_config)
  7607. {
  7608. enum intel_dpll_id id;
  7609. u32 temp;
  7610. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7611. id = temp >> (port * 3 + 1);
  7612. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7613. return;
  7614. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7615. }
  7616. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7617. enum port port,
  7618. struct intel_crtc_state *pipe_config)
  7619. {
  7620. enum intel_dpll_id id;
  7621. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7622. switch (ddi_pll_sel) {
  7623. case PORT_CLK_SEL_WRPLL1:
  7624. id = DPLL_ID_WRPLL1;
  7625. break;
  7626. case PORT_CLK_SEL_WRPLL2:
  7627. id = DPLL_ID_WRPLL2;
  7628. break;
  7629. case PORT_CLK_SEL_SPLL:
  7630. id = DPLL_ID_SPLL;
  7631. break;
  7632. case PORT_CLK_SEL_LCPLL_810:
  7633. id = DPLL_ID_LCPLL_810;
  7634. break;
  7635. case PORT_CLK_SEL_LCPLL_1350:
  7636. id = DPLL_ID_LCPLL_1350;
  7637. break;
  7638. case PORT_CLK_SEL_LCPLL_2700:
  7639. id = DPLL_ID_LCPLL_2700;
  7640. break;
  7641. default:
  7642. MISSING_CASE(ddi_pll_sel);
  7643. /* fall through */
  7644. case PORT_CLK_SEL_NONE:
  7645. return;
  7646. }
  7647. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7648. }
  7649. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7650. struct intel_crtc_state *pipe_config,
  7651. u64 *power_domain_mask)
  7652. {
  7653. struct drm_device *dev = crtc->base.dev;
  7654. struct drm_i915_private *dev_priv = to_i915(dev);
  7655. enum intel_display_power_domain power_domain;
  7656. u32 tmp;
  7657. /*
  7658. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7659. * transcoder handled below.
  7660. */
  7661. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7662. /*
  7663. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7664. * consistency and less surprising code; it's in always on power).
  7665. */
  7666. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7667. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7668. enum pipe trans_edp_pipe;
  7669. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7670. default:
  7671. WARN(1, "unknown pipe linked to edp transcoder\n");
  7672. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7673. case TRANS_DDI_EDP_INPUT_A_ON:
  7674. trans_edp_pipe = PIPE_A;
  7675. break;
  7676. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7677. trans_edp_pipe = PIPE_B;
  7678. break;
  7679. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7680. trans_edp_pipe = PIPE_C;
  7681. break;
  7682. }
  7683. if (trans_edp_pipe == crtc->pipe)
  7684. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7685. }
  7686. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7687. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7688. return false;
  7689. *power_domain_mask |= BIT_ULL(power_domain);
  7690. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7691. return tmp & PIPECONF_ENABLE;
  7692. }
  7693. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7694. struct intel_crtc_state *pipe_config,
  7695. u64 *power_domain_mask)
  7696. {
  7697. struct drm_device *dev = crtc->base.dev;
  7698. struct drm_i915_private *dev_priv = to_i915(dev);
  7699. enum intel_display_power_domain power_domain;
  7700. enum port port;
  7701. enum transcoder cpu_transcoder;
  7702. u32 tmp;
  7703. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7704. if (port == PORT_A)
  7705. cpu_transcoder = TRANSCODER_DSI_A;
  7706. else
  7707. cpu_transcoder = TRANSCODER_DSI_C;
  7708. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7709. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7710. continue;
  7711. *power_domain_mask |= BIT_ULL(power_domain);
  7712. /*
  7713. * The PLL needs to be enabled with a valid divider
  7714. * configuration, otherwise accessing DSI registers will hang
  7715. * the machine. See BSpec North Display Engine
  7716. * registers/MIPI[BXT]. We can break out here early, since we
  7717. * need the same DSI PLL to be enabled for both DSI ports.
  7718. */
  7719. if (!intel_dsi_pll_is_enabled(dev_priv))
  7720. break;
  7721. /* XXX: this works for video mode only */
  7722. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7723. if (!(tmp & DPI_ENABLE))
  7724. continue;
  7725. tmp = I915_READ(MIPI_CTRL(port));
  7726. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7727. continue;
  7728. pipe_config->cpu_transcoder = cpu_transcoder;
  7729. break;
  7730. }
  7731. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7732. }
  7733. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7734. struct intel_crtc_state *pipe_config)
  7735. {
  7736. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7737. struct intel_shared_dpll *pll;
  7738. enum port port;
  7739. uint32_t tmp;
  7740. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7741. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7742. if (IS_CANNONLAKE(dev_priv))
  7743. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7744. else if (IS_GEN9_BC(dev_priv))
  7745. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7746. else if (IS_GEN9_LP(dev_priv))
  7747. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7748. else
  7749. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7750. pll = pipe_config->shared_dpll;
  7751. if (pll) {
  7752. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7753. &pipe_config->dpll_hw_state));
  7754. }
  7755. /*
  7756. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7757. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7758. * the PCH transcoder is on.
  7759. */
  7760. if (INTEL_GEN(dev_priv) < 9 &&
  7761. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7762. pipe_config->has_pch_encoder = true;
  7763. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7764. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7765. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7766. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7767. }
  7768. }
  7769. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7770. struct intel_crtc_state *pipe_config)
  7771. {
  7772. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7773. enum intel_display_power_domain power_domain;
  7774. u64 power_domain_mask;
  7775. bool active;
  7776. intel_crtc_init_scalers(crtc, pipe_config);
  7777. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7778. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7779. return false;
  7780. power_domain_mask = BIT_ULL(power_domain);
  7781. pipe_config->shared_dpll = NULL;
  7782. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7783. if (IS_GEN9_LP(dev_priv) &&
  7784. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7785. WARN_ON(active);
  7786. active = true;
  7787. }
  7788. if (!active)
  7789. goto out;
  7790. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7791. haswell_get_ddi_port_state(crtc, pipe_config);
  7792. intel_get_pipe_timings(crtc, pipe_config);
  7793. }
  7794. intel_get_pipe_src_size(crtc, pipe_config);
  7795. pipe_config->gamma_mode =
  7796. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7797. if (IS_BROADWELL(dev_priv) || dev_priv->info.gen >= 9) {
  7798. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7799. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7800. if (IS_GEMINILAKE(dev_priv) || dev_priv->info.gen >= 10) {
  7801. bool blend_mode_420 = tmp &
  7802. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7803. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7804. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7805. pipe_config->ycbcr420 != blend_mode_420)
  7806. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7807. } else if (clrspace_yuv) {
  7808. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7809. }
  7810. }
  7811. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7812. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7813. power_domain_mask |= BIT_ULL(power_domain);
  7814. if (INTEL_GEN(dev_priv) >= 9)
  7815. skylake_get_pfit_config(crtc, pipe_config);
  7816. else
  7817. ironlake_get_pfit_config(crtc, pipe_config);
  7818. }
  7819. if (IS_HASWELL(dev_priv))
  7820. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7821. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7822. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7823. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7824. pipe_config->pixel_multiplier =
  7825. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7826. } else {
  7827. pipe_config->pixel_multiplier = 1;
  7828. }
  7829. out:
  7830. for_each_power_domain(power_domain, power_domain_mask)
  7831. intel_display_power_put(dev_priv, power_domain);
  7832. return active;
  7833. }
  7834. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7835. {
  7836. struct drm_i915_private *dev_priv =
  7837. to_i915(plane_state->base.plane->dev);
  7838. const struct drm_framebuffer *fb = plane_state->base.fb;
  7839. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7840. u32 base;
  7841. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7842. base = obj->phys_handle->busaddr;
  7843. else
  7844. base = intel_plane_ggtt_offset(plane_state);
  7845. base += plane_state->main.offset;
  7846. /* ILK+ do this automagically */
  7847. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7848. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7849. base += (plane_state->base.crtc_h *
  7850. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7851. return base;
  7852. }
  7853. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7854. {
  7855. int x = plane_state->base.crtc_x;
  7856. int y = plane_state->base.crtc_y;
  7857. u32 pos = 0;
  7858. if (x < 0) {
  7859. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7860. x = -x;
  7861. }
  7862. pos |= x << CURSOR_X_SHIFT;
  7863. if (y < 0) {
  7864. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7865. y = -y;
  7866. }
  7867. pos |= y << CURSOR_Y_SHIFT;
  7868. return pos;
  7869. }
  7870. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7871. {
  7872. const struct drm_mode_config *config =
  7873. &plane_state->base.plane->dev->mode_config;
  7874. int width = plane_state->base.crtc_w;
  7875. int height = plane_state->base.crtc_h;
  7876. return width > 0 && width <= config->cursor_width &&
  7877. height > 0 && height <= config->cursor_height;
  7878. }
  7879. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7880. struct intel_plane_state *plane_state)
  7881. {
  7882. const struct drm_framebuffer *fb = plane_state->base.fb;
  7883. int src_x, src_y;
  7884. u32 offset;
  7885. int ret;
  7886. ret = drm_plane_helper_check_state(&plane_state->base,
  7887. &plane_state->clip,
  7888. DRM_PLANE_HELPER_NO_SCALING,
  7889. DRM_PLANE_HELPER_NO_SCALING,
  7890. true, true);
  7891. if (ret)
  7892. return ret;
  7893. if (!fb)
  7894. return 0;
  7895. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7896. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7897. return -EINVAL;
  7898. }
  7899. src_x = plane_state->base.src_x >> 16;
  7900. src_y = plane_state->base.src_y >> 16;
  7901. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7902. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7903. if (src_x != 0 || src_y != 0) {
  7904. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7905. return -EINVAL;
  7906. }
  7907. plane_state->main.offset = offset;
  7908. return 0;
  7909. }
  7910. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7911. const struct intel_plane_state *plane_state)
  7912. {
  7913. const struct drm_framebuffer *fb = plane_state->base.fb;
  7914. return CURSOR_ENABLE |
  7915. CURSOR_GAMMA_ENABLE |
  7916. CURSOR_FORMAT_ARGB |
  7917. CURSOR_STRIDE(fb->pitches[0]);
  7918. }
  7919. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7920. {
  7921. int width = plane_state->base.crtc_w;
  7922. /*
  7923. * 845g/865g are only limited by the width of their cursors,
  7924. * the height is arbitrary up to the precision of the register.
  7925. */
  7926. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7927. }
  7928. static int i845_check_cursor(struct intel_plane *plane,
  7929. struct intel_crtc_state *crtc_state,
  7930. struct intel_plane_state *plane_state)
  7931. {
  7932. const struct drm_framebuffer *fb = plane_state->base.fb;
  7933. int ret;
  7934. ret = intel_check_cursor(crtc_state, plane_state);
  7935. if (ret)
  7936. return ret;
  7937. /* if we want to turn off the cursor ignore width and height */
  7938. if (!fb)
  7939. return 0;
  7940. /* Check for which cursor types we support */
  7941. if (!i845_cursor_size_ok(plane_state)) {
  7942. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7943. plane_state->base.crtc_w,
  7944. plane_state->base.crtc_h);
  7945. return -EINVAL;
  7946. }
  7947. switch (fb->pitches[0]) {
  7948. case 256:
  7949. case 512:
  7950. case 1024:
  7951. case 2048:
  7952. break;
  7953. default:
  7954. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7955. fb->pitches[0]);
  7956. return -EINVAL;
  7957. }
  7958. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7959. return 0;
  7960. }
  7961. static void i845_update_cursor(struct intel_plane *plane,
  7962. const struct intel_crtc_state *crtc_state,
  7963. const struct intel_plane_state *plane_state)
  7964. {
  7965. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7966. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7967. unsigned long irqflags;
  7968. if (plane_state && plane_state->base.visible) {
  7969. unsigned int width = plane_state->base.crtc_w;
  7970. unsigned int height = plane_state->base.crtc_h;
  7971. cntl = plane_state->ctl;
  7972. size = (height << 12) | width;
  7973. base = intel_cursor_base(plane_state);
  7974. pos = intel_cursor_position(plane_state);
  7975. }
  7976. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7977. /* On these chipsets we can only modify the base/size/stride
  7978. * whilst the cursor is disabled.
  7979. */
  7980. if (plane->cursor.base != base ||
  7981. plane->cursor.size != size ||
  7982. plane->cursor.cntl != cntl) {
  7983. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7984. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7985. I915_WRITE_FW(CURSIZE, size);
  7986. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7987. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7988. plane->cursor.base = base;
  7989. plane->cursor.size = size;
  7990. plane->cursor.cntl = cntl;
  7991. } else {
  7992. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7993. }
  7994. POSTING_READ_FW(CURCNTR(PIPE_A));
  7995. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7996. }
  7997. static void i845_disable_cursor(struct intel_plane *plane,
  7998. struct intel_crtc *crtc)
  7999. {
  8000. i845_update_cursor(plane, NULL, NULL);
  8001. }
  8002. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8003. const struct intel_plane_state *plane_state)
  8004. {
  8005. struct drm_i915_private *dev_priv =
  8006. to_i915(plane_state->base.plane->dev);
  8007. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8008. u32 cntl;
  8009. cntl = MCURSOR_GAMMA_ENABLE;
  8010. if (HAS_DDI(dev_priv))
  8011. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8012. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8013. switch (plane_state->base.crtc_w) {
  8014. case 64:
  8015. cntl |= CURSOR_MODE_64_ARGB_AX;
  8016. break;
  8017. case 128:
  8018. cntl |= CURSOR_MODE_128_ARGB_AX;
  8019. break;
  8020. case 256:
  8021. cntl |= CURSOR_MODE_256_ARGB_AX;
  8022. break;
  8023. default:
  8024. MISSING_CASE(plane_state->base.crtc_w);
  8025. return 0;
  8026. }
  8027. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8028. cntl |= CURSOR_ROTATE_180;
  8029. return cntl;
  8030. }
  8031. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8032. {
  8033. struct drm_i915_private *dev_priv =
  8034. to_i915(plane_state->base.plane->dev);
  8035. int width = plane_state->base.crtc_w;
  8036. int height = plane_state->base.crtc_h;
  8037. if (!intel_cursor_size_ok(plane_state))
  8038. return false;
  8039. /* Cursor width is limited to a few power-of-two sizes */
  8040. switch (width) {
  8041. case 256:
  8042. case 128:
  8043. case 64:
  8044. break;
  8045. default:
  8046. return false;
  8047. }
  8048. /*
  8049. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8050. * height from 8 lines up to the cursor width, when the
  8051. * cursor is not rotated. Everything else requires square
  8052. * cursors.
  8053. */
  8054. if (HAS_CUR_FBC(dev_priv) &&
  8055. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8056. if (height < 8 || height > width)
  8057. return false;
  8058. } else {
  8059. if (height != width)
  8060. return false;
  8061. }
  8062. return true;
  8063. }
  8064. static int i9xx_check_cursor(struct intel_plane *plane,
  8065. struct intel_crtc_state *crtc_state,
  8066. struct intel_plane_state *plane_state)
  8067. {
  8068. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8069. const struct drm_framebuffer *fb = plane_state->base.fb;
  8070. enum pipe pipe = plane->pipe;
  8071. int ret;
  8072. ret = intel_check_cursor(crtc_state, plane_state);
  8073. if (ret)
  8074. return ret;
  8075. /* if we want to turn off the cursor ignore width and height */
  8076. if (!fb)
  8077. return 0;
  8078. /* Check for which cursor types we support */
  8079. if (!i9xx_cursor_size_ok(plane_state)) {
  8080. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8081. plane_state->base.crtc_w,
  8082. plane_state->base.crtc_h);
  8083. return -EINVAL;
  8084. }
  8085. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8086. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8087. fb->pitches[0], plane_state->base.crtc_w);
  8088. return -EINVAL;
  8089. }
  8090. /*
  8091. * There's something wrong with the cursor on CHV pipe C.
  8092. * If it straddles the left edge of the screen then
  8093. * moving it away from the edge or disabling it often
  8094. * results in a pipe underrun, and often that can lead to
  8095. * dead pipe (constant underrun reported, and it scans
  8096. * out just a solid color). To recover from that, the
  8097. * display power well must be turned off and on again.
  8098. * Refuse the put the cursor into that compromised position.
  8099. */
  8100. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8101. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8102. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8103. return -EINVAL;
  8104. }
  8105. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8106. return 0;
  8107. }
  8108. static void i9xx_update_cursor(struct intel_plane *plane,
  8109. const struct intel_crtc_state *crtc_state,
  8110. const struct intel_plane_state *plane_state)
  8111. {
  8112. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8113. enum pipe pipe = plane->pipe;
  8114. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8115. unsigned long irqflags;
  8116. if (plane_state && plane_state->base.visible) {
  8117. cntl = plane_state->ctl;
  8118. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8119. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8120. base = intel_cursor_base(plane_state);
  8121. pos = intel_cursor_position(plane_state);
  8122. }
  8123. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8124. /*
  8125. * On some platforms writing CURCNTR first will also
  8126. * cause CURPOS to be armed by the CURBASE write.
  8127. * Without the CURCNTR write the CURPOS write would
  8128. * arm itself. Thus we always start the full update
  8129. * with a CURCNTR write.
  8130. *
  8131. * On other platforms CURPOS always requires the
  8132. * CURBASE write to arm the update. Additonally
  8133. * a write to any of the cursor register will cancel
  8134. * an already armed cursor update. Thus leaving out
  8135. * the CURBASE write after CURPOS could lead to a
  8136. * cursor that doesn't appear to move, or even change
  8137. * shape. Thus we always write CURBASE.
  8138. *
  8139. * CURCNTR and CUR_FBC_CTL are always
  8140. * armed by the CURBASE write only.
  8141. */
  8142. if (plane->cursor.base != base ||
  8143. plane->cursor.size != fbc_ctl ||
  8144. plane->cursor.cntl != cntl) {
  8145. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8146. if (HAS_CUR_FBC(dev_priv))
  8147. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8148. I915_WRITE_FW(CURPOS(pipe), pos);
  8149. I915_WRITE_FW(CURBASE(pipe), base);
  8150. plane->cursor.base = base;
  8151. plane->cursor.size = fbc_ctl;
  8152. plane->cursor.cntl = cntl;
  8153. } else {
  8154. I915_WRITE_FW(CURPOS(pipe), pos);
  8155. I915_WRITE_FW(CURBASE(pipe), base);
  8156. }
  8157. POSTING_READ_FW(CURBASE(pipe));
  8158. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8159. }
  8160. static void i9xx_disable_cursor(struct intel_plane *plane,
  8161. struct intel_crtc *crtc)
  8162. {
  8163. i9xx_update_cursor(plane, NULL, NULL);
  8164. }
  8165. /* VESA 640x480x72Hz mode to set on the pipe */
  8166. static const struct drm_display_mode load_detect_mode = {
  8167. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8168. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8169. };
  8170. struct drm_framebuffer *
  8171. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8172. struct drm_mode_fb_cmd2 *mode_cmd)
  8173. {
  8174. struct intel_framebuffer *intel_fb;
  8175. int ret;
  8176. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8177. if (!intel_fb)
  8178. return ERR_PTR(-ENOMEM);
  8179. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8180. if (ret)
  8181. goto err;
  8182. return &intel_fb->base;
  8183. err:
  8184. kfree(intel_fb);
  8185. return ERR_PTR(ret);
  8186. }
  8187. static u32
  8188. intel_framebuffer_pitch_for_width(int width, int bpp)
  8189. {
  8190. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8191. return ALIGN(pitch, 64);
  8192. }
  8193. static u32
  8194. intel_framebuffer_size_for_mode(const struct drm_display_mode *mode, int bpp)
  8195. {
  8196. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8197. return PAGE_ALIGN(pitch * mode->vdisplay);
  8198. }
  8199. static struct drm_framebuffer *
  8200. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8201. const struct drm_display_mode *mode,
  8202. int depth, int bpp)
  8203. {
  8204. struct drm_framebuffer *fb;
  8205. struct drm_i915_gem_object *obj;
  8206. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8207. obj = i915_gem_object_create(to_i915(dev),
  8208. intel_framebuffer_size_for_mode(mode, bpp));
  8209. if (IS_ERR(obj))
  8210. return ERR_CAST(obj);
  8211. mode_cmd.width = mode->hdisplay;
  8212. mode_cmd.height = mode->vdisplay;
  8213. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8214. bpp);
  8215. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8216. fb = intel_framebuffer_create(obj, &mode_cmd);
  8217. if (IS_ERR(fb))
  8218. i915_gem_object_put(obj);
  8219. return fb;
  8220. }
  8221. static struct drm_framebuffer *
  8222. mode_fits_in_fbdev(struct drm_device *dev,
  8223. const struct drm_display_mode *mode)
  8224. {
  8225. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8226. struct drm_i915_private *dev_priv = to_i915(dev);
  8227. struct drm_i915_gem_object *obj;
  8228. struct drm_framebuffer *fb;
  8229. if (!dev_priv->fbdev)
  8230. return NULL;
  8231. if (!dev_priv->fbdev->fb)
  8232. return NULL;
  8233. obj = dev_priv->fbdev->fb->obj;
  8234. BUG_ON(!obj);
  8235. fb = &dev_priv->fbdev->fb->base;
  8236. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8237. fb->format->cpp[0] * 8))
  8238. return NULL;
  8239. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8240. return NULL;
  8241. drm_framebuffer_reference(fb);
  8242. return fb;
  8243. #else
  8244. return NULL;
  8245. #endif
  8246. }
  8247. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8248. struct drm_crtc *crtc,
  8249. const struct drm_display_mode *mode,
  8250. struct drm_framebuffer *fb,
  8251. int x, int y)
  8252. {
  8253. struct drm_plane_state *plane_state;
  8254. int hdisplay, vdisplay;
  8255. int ret;
  8256. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8257. if (IS_ERR(plane_state))
  8258. return PTR_ERR(plane_state);
  8259. if (mode)
  8260. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  8261. else
  8262. hdisplay = vdisplay = 0;
  8263. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8264. if (ret)
  8265. return ret;
  8266. drm_atomic_set_fb_for_plane(plane_state, fb);
  8267. plane_state->crtc_x = 0;
  8268. plane_state->crtc_y = 0;
  8269. plane_state->crtc_w = hdisplay;
  8270. plane_state->crtc_h = vdisplay;
  8271. plane_state->src_x = x << 16;
  8272. plane_state->src_y = y << 16;
  8273. plane_state->src_w = hdisplay << 16;
  8274. plane_state->src_h = vdisplay << 16;
  8275. return 0;
  8276. }
  8277. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8278. const struct drm_display_mode *mode,
  8279. struct intel_load_detect_pipe *old,
  8280. struct drm_modeset_acquire_ctx *ctx)
  8281. {
  8282. struct intel_crtc *intel_crtc;
  8283. struct intel_encoder *intel_encoder =
  8284. intel_attached_encoder(connector);
  8285. struct drm_crtc *possible_crtc;
  8286. struct drm_encoder *encoder = &intel_encoder->base;
  8287. struct drm_crtc *crtc = NULL;
  8288. struct drm_device *dev = encoder->dev;
  8289. struct drm_i915_private *dev_priv = to_i915(dev);
  8290. struct drm_framebuffer *fb;
  8291. struct drm_mode_config *config = &dev->mode_config;
  8292. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8293. struct drm_connector_state *connector_state;
  8294. struct intel_crtc_state *crtc_state;
  8295. int ret, i = -1;
  8296. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8297. connector->base.id, connector->name,
  8298. encoder->base.id, encoder->name);
  8299. old->restore_state = NULL;
  8300. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8301. /*
  8302. * Algorithm gets a little messy:
  8303. *
  8304. * - if the connector already has an assigned crtc, use it (but make
  8305. * sure it's on first)
  8306. *
  8307. * - try to find the first unused crtc that can drive this connector,
  8308. * and use that if we find one
  8309. */
  8310. /* See if we already have a CRTC for this connector */
  8311. if (connector->state->crtc) {
  8312. crtc = connector->state->crtc;
  8313. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8314. if (ret)
  8315. goto fail;
  8316. /* Make sure the crtc and connector are running */
  8317. goto found;
  8318. }
  8319. /* Find an unused one (if possible) */
  8320. for_each_crtc(dev, possible_crtc) {
  8321. i++;
  8322. if (!(encoder->possible_crtcs & (1 << i)))
  8323. continue;
  8324. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8325. if (ret)
  8326. goto fail;
  8327. if (possible_crtc->state->enable) {
  8328. drm_modeset_unlock(&possible_crtc->mutex);
  8329. continue;
  8330. }
  8331. crtc = possible_crtc;
  8332. break;
  8333. }
  8334. /*
  8335. * If we didn't find an unused CRTC, don't use any.
  8336. */
  8337. if (!crtc) {
  8338. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8339. ret = -ENODEV;
  8340. goto fail;
  8341. }
  8342. found:
  8343. intel_crtc = to_intel_crtc(crtc);
  8344. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8345. if (ret)
  8346. goto fail;
  8347. state = drm_atomic_state_alloc(dev);
  8348. restore_state = drm_atomic_state_alloc(dev);
  8349. if (!state || !restore_state) {
  8350. ret = -ENOMEM;
  8351. goto fail;
  8352. }
  8353. state->acquire_ctx = ctx;
  8354. restore_state->acquire_ctx = ctx;
  8355. connector_state = drm_atomic_get_connector_state(state, connector);
  8356. if (IS_ERR(connector_state)) {
  8357. ret = PTR_ERR(connector_state);
  8358. goto fail;
  8359. }
  8360. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8361. if (ret)
  8362. goto fail;
  8363. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8364. if (IS_ERR(crtc_state)) {
  8365. ret = PTR_ERR(crtc_state);
  8366. goto fail;
  8367. }
  8368. crtc_state->base.active = crtc_state->base.enable = true;
  8369. if (!mode)
  8370. mode = &load_detect_mode;
  8371. /* We need a framebuffer large enough to accommodate all accesses
  8372. * that the plane may generate whilst we perform load detection.
  8373. * We can not rely on the fbcon either being present (we get called
  8374. * during its initialisation to detect all boot displays, or it may
  8375. * not even exist) or that it is large enough to satisfy the
  8376. * requested mode.
  8377. */
  8378. fb = mode_fits_in_fbdev(dev, mode);
  8379. if (fb == NULL) {
  8380. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8381. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8382. } else
  8383. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8384. if (IS_ERR(fb)) {
  8385. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8386. ret = PTR_ERR(fb);
  8387. goto fail;
  8388. }
  8389. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8390. if (ret)
  8391. goto fail;
  8392. drm_framebuffer_unreference(fb);
  8393. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8394. if (ret)
  8395. goto fail;
  8396. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8397. if (!ret)
  8398. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8399. if (!ret)
  8400. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8401. if (ret) {
  8402. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8403. goto fail;
  8404. }
  8405. ret = drm_atomic_commit(state);
  8406. if (ret) {
  8407. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8408. goto fail;
  8409. }
  8410. old->restore_state = restore_state;
  8411. drm_atomic_state_put(state);
  8412. /* let the connector get through one full cycle before testing */
  8413. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8414. return true;
  8415. fail:
  8416. if (state) {
  8417. drm_atomic_state_put(state);
  8418. state = NULL;
  8419. }
  8420. if (restore_state) {
  8421. drm_atomic_state_put(restore_state);
  8422. restore_state = NULL;
  8423. }
  8424. if (ret == -EDEADLK)
  8425. return ret;
  8426. return false;
  8427. }
  8428. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8429. struct intel_load_detect_pipe *old,
  8430. struct drm_modeset_acquire_ctx *ctx)
  8431. {
  8432. struct intel_encoder *intel_encoder =
  8433. intel_attached_encoder(connector);
  8434. struct drm_encoder *encoder = &intel_encoder->base;
  8435. struct drm_atomic_state *state = old->restore_state;
  8436. int ret;
  8437. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8438. connector->base.id, connector->name,
  8439. encoder->base.id, encoder->name);
  8440. if (!state)
  8441. return;
  8442. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8443. if (ret)
  8444. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8445. drm_atomic_state_put(state);
  8446. }
  8447. static int i9xx_pll_refclk(struct drm_device *dev,
  8448. const struct intel_crtc_state *pipe_config)
  8449. {
  8450. struct drm_i915_private *dev_priv = to_i915(dev);
  8451. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8452. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8453. return dev_priv->vbt.lvds_ssc_freq;
  8454. else if (HAS_PCH_SPLIT(dev_priv))
  8455. return 120000;
  8456. else if (!IS_GEN2(dev_priv))
  8457. return 96000;
  8458. else
  8459. return 48000;
  8460. }
  8461. /* Returns the clock of the currently programmed mode of the given pipe. */
  8462. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8463. struct intel_crtc_state *pipe_config)
  8464. {
  8465. struct drm_device *dev = crtc->base.dev;
  8466. struct drm_i915_private *dev_priv = to_i915(dev);
  8467. int pipe = pipe_config->cpu_transcoder;
  8468. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8469. u32 fp;
  8470. struct dpll clock;
  8471. int port_clock;
  8472. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8473. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8474. fp = pipe_config->dpll_hw_state.fp0;
  8475. else
  8476. fp = pipe_config->dpll_hw_state.fp1;
  8477. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8478. if (IS_PINEVIEW(dev_priv)) {
  8479. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8480. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8481. } else {
  8482. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8483. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8484. }
  8485. if (!IS_GEN2(dev_priv)) {
  8486. if (IS_PINEVIEW(dev_priv))
  8487. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8488. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8489. else
  8490. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8491. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8492. switch (dpll & DPLL_MODE_MASK) {
  8493. case DPLLB_MODE_DAC_SERIAL:
  8494. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8495. 5 : 10;
  8496. break;
  8497. case DPLLB_MODE_LVDS:
  8498. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8499. 7 : 14;
  8500. break;
  8501. default:
  8502. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8503. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8504. return;
  8505. }
  8506. if (IS_PINEVIEW(dev_priv))
  8507. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8508. else
  8509. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8510. } else {
  8511. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8512. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8513. if (is_lvds) {
  8514. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8515. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8516. if (lvds & LVDS_CLKB_POWER_UP)
  8517. clock.p2 = 7;
  8518. else
  8519. clock.p2 = 14;
  8520. } else {
  8521. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8522. clock.p1 = 2;
  8523. else {
  8524. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8525. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8526. }
  8527. if (dpll & PLL_P2_DIVIDE_BY_4)
  8528. clock.p2 = 4;
  8529. else
  8530. clock.p2 = 2;
  8531. }
  8532. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8533. }
  8534. /*
  8535. * This value includes pixel_multiplier. We will use
  8536. * port_clock to compute adjusted_mode.crtc_clock in the
  8537. * encoder's get_config() function.
  8538. */
  8539. pipe_config->port_clock = port_clock;
  8540. }
  8541. int intel_dotclock_calculate(int link_freq,
  8542. const struct intel_link_m_n *m_n)
  8543. {
  8544. /*
  8545. * The calculation for the data clock is:
  8546. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8547. * But we want to avoid losing precison if possible, so:
  8548. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8549. *
  8550. * and the link clock is simpler:
  8551. * link_clock = (m * link_clock) / n
  8552. */
  8553. if (!m_n->link_n)
  8554. return 0;
  8555. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8556. }
  8557. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8558. struct intel_crtc_state *pipe_config)
  8559. {
  8560. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8561. /* read out port_clock from the DPLL */
  8562. i9xx_crtc_clock_get(crtc, pipe_config);
  8563. /*
  8564. * In case there is an active pipe without active ports,
  8565. * we may need some idea for the dotclock anyway.
  8566. * Calculate one based on the FDI configuration.
  8567. */
  8568. pipe_config->base.adjusted_mode.crtc_clock =
  8569. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8570. &pipe_config->fdi_m_n);
  8571. }
  8572. /** Returns the currently programmed mode of the given pipe. */
  8573. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8574. struct drm_crtc *crtc)
  8575. {
  8576. struct drm_i915_private *dev_priv = to_i915(dev);
  8577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8578. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8579. struct drm_display_mode *mode;
  8580. struct intel_crtc_state *pipe_config;
  8581. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8582. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8583. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8584. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8585. enum pipe pipe = intel_crtc->pipe;
  8586. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8587. if (!mode)
  8588. return NULL;
  8589. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8590. if (!pipe_config) {
  8591. kfree(mode);
  8592. return NULL;
  8593. }
  8594. /*
  8595. * Construct a pipe_config sufficient for getting the clock info
  8596. * back out of crtc_clock_get.
  8597. *
  8598. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8599. * to use a real value here instead.
  8600. */
  8601. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8602. pipe_config->pixel_multiplier = 1;
  8603. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8604. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8605. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8606. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8607. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8608. mode->hdisplay = (htot & 0xffff) + 1;
  8609. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8610. mode->hsync_start = (hsync & 0xffff) + 1;
  8611. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8612. mode->vdisplay = (vtot & 0xffff) + 1;
  8613. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8614. mode->vsync_start = (vsync & 0xffff) + 1;
  8615. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8616. drm_mode_set_name(mode);
  8617. kfree(pipe_config);
  8618. return mode;
  8619. }
  8620. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8621. {
  8622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8623. drm_crtc_cleanup(crtc);
  8624. kfree(intel_crtc);
  8625. }
  8626. /**
  8627. * intel_wm_need_update - Check whether watermarks need updating
  8628. * @plane: drm plane
  8629. * @state: new plane state
  8630. *
  8631. * Check current plane state versus the new one to determine whether
  8632. * watermarks need to be recalculated.
  8633. *
  8634. * Returns true or false.
  8635. */
  8636. static bool intel_wm_need_update(struct drm_plane *plane,
  8637. struct drm_plane_state *state)
  8638. {
  8639. struct intel_plane_state *new = to_intel_plane_state(state);
  8640. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8641. /* Update watermarks on tiling or size changes. */
  8642. if (new->base.visible != cur->base.visible)
  8643. return true;
  8644. if (!cur->base.fb || !new->base.fb)
  8645. return false;
  8646. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8647. cur->base.rotation != new->base.rotation ||
  8648. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8649. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8650. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8651. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8652. return true;
  8653. return false;
  8654. }
  8655. static bool needs_scaling(const struct intel_plane_state *state)
  8656. {
  8657. int src_w = drm_rect_width(&state->base.src) >> 16;
  8658. int src_h = drm_rect_height(&state->base.src) >> 16;
  8659. int dst_w = drm_rect_width(&state->base.dst);
  8660. int dst_h = drm_rect_height(&state->base.dst);
  8661. return (src_w != dst_w || src_h != dst_h);
  8662. }
  8663. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8664. struct drm_crtc_state *crtc_state,
  8665. const struct intel_plane_state *old_plane_state,
  8666. struct drm_plane_state *plane_state)
  8667. {
  8668. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8669. struct drm_crtc *crtc = crtc_state->crtc;
  8670. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8671. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8672. struct drm_device *dev = crtc->dev;
  8673. struct drm_i915_private *dev_priv = to_i915(dev);
  8674. bool mode_changed = needs_modeset(crtc_state);
  8675. bool was_crtc_enabled = old_crtc_state->base.active;
  8676. bool is_crtc_enabled = crtc_state->active;
  8677. bool turn_off, turn_on, visible, was_visible;
  8678. struct drm_framebuffer *fb = plane_state->fb;
  8679. int ret;
  8680. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8681. ret = skl_update_scaler_plane(
  8682. to_intel_crtc_state(crtc_state),
  8683. to_intel_plane_state(plane_state));
  8684. if (ret)
  8685. return ret;
  8686. }
  8687. was_visible = old_plane_state->base.visible;
  8688. visible = plane_state->visible;
  8689. if (!was_crtc_enabled && WARN_ON(was_visible))
  8690. was_visible = false;
  8691. /*
  8692. * Visibility is calculated as if the crtc was on, but
  8693. * after scaler setup everything depends on it being off
  8694. * when the crtc isn't active.
  8695. *
  8696. * FIXME this is wrong for watermarks. Watermarks should also
  8697. * be computed as if the pipe would be active. Perhaps move
  8698. * per-plane wm computation to the .check_plane() hook, and
  8699. * only combine the results from all planes in the current place?
  8700. */
  8701. if (!is_crtc_enabled) {
  8702. plane_state->visible = visible = false;
  8703. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8704. }
  8705. if (!was_visible && !visible)
  8706. return 0;
  8707. if (fb != old_plane_state->base.fb)
  8708. pipe_config->fb_changed = true;
  8709. turn_off = was_visible && (!visible || mode_changed);
  8710. turn_on = visible && (!was_visible || mode_changed);
  8711. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8712. intel_crtc->base.base.id, intel_crtc->base.name,
  8713. plane->base.base.id, plane->base.name,
  8714. fb ? fb->base.id : -1);
  8715. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8716. plane->base.base.id, plane->base.name,
  8717. was_visible, visible,
  8718. turn_off, turn_on, mode_changed);
  8719. if (turn_on) {
  8720. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8721. pipe_config->update_wm_pre = true;
  8722. /* must disable cxsr around plane enable/disable */
  8723. if (plane->id != PLANE_CURSOR)
  8724. pipe_config->disable_cxsr = true;
  8725. } else if (turn_off) {
  8726. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8727. pipe_config->update_wm_post = true;
  8728. /* must disable cxsr around plane enable/disable */
  8729. if (plane->id != PLANE_CURSOR)
  8730. pipe_config->disable_cxsr = true;
  8731. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8732. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8733. /* FIXME bollocks */
  8734. pipe_config->update_wm_pre = true;
  8735. pipe_config->update_wm_post = true;
  8736. }
  8737. }
  8738. if (visible || was_visible)
  8739. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8740. /*
  8741. * WaCxSRDisabledForSpriteScaling:ivb
  8742. *
  8743. * cstate->update_wm was already set above, so this flag will
  8744. * take effect when we commit and program watermarks.
  8745. */
  8746. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8747. needs_scaling(to_intel_plane_state(plane_state)) &&
  8748. !needs_scaling(old_plane_state))
  8749. pipe_config->disable_lp_wm = true;
  8750. return 0;
  8751. }
  8752. static bool encoders_cloneable(const struct intel_encoder *a,
  8753. const struct intel_encoder *b)
  8754. {
  8755. /* masks could be asymmetric, so check both ways */
  8756. return a == b || (a->cloneable & (1 << b->type) &&
  8757. b->cloneable & (1 << a->type));
  8758. }
  8759. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8760. struct intel_crtc *crtc,
  8761. struct intel_encoder *encoder)
  8762. {
  8763. struct intel_encoder *source_encoder;
  8764. struct drm_connector *connector;
  8765. struct drm_connector_state *connector_state;
  8766. int i;
  8767. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8768. if (connector_state->crtc != &crtc->base)
  8769. continue;
  8770. source_encoder =
  8771. to_intel_encoder(connector_state->best_encoder);
  8772. if (!encoders_cloneable(encoder, source_encoder))
  8773. return false;
  8774. }
  8775. return true;
  8776. }
  8777. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8778. struct drm_crtc_state *crtc_state)
  8779. {
  8780. struct drm_device *dev = crtc->dev;
  8781. struct drm_i915_private *dev_priv = to_i915(dev);
  8782. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8783. struct intel_crtc_state *pipe_config =
  8784. to_intel_crtc_state(crtc_state);
  8785. struct drm_atomic_state *state = crtc_state->state;
  8786. int ret;
  8787. bool mode_changed = needs_modeset(crtc_state);
  8788. if (mode_changed && !crtc_state->active)
  8789. pipe_config->update_wm_post = true;
  8790. if (mode_changed && crtc_state->enable &&
  8791. dev_priv->display.crtc_compute_clock &&
  8792. !WARN_ON(pipe_config->shared_dpll)) {
  8793. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8794. pipe_config);
  8795. if (ret)
  8796. return ret;
  8797. }
  8798. if (crtc_state->color_mgmt_changed) {
  8799. ret = intel_color_check(crtc, crtc_state);
  8800. if (ret)
  8801. return ret;
  8802. /*
  8803. * Changing color management on Intel hardware is
  8804. * handled as part of planes update.
  8805. */
  8806. crtc_state->planes_changed = true;
  8807. }
  8808. ret = 0;
  8809. if (dev_priv->display.compute_pipe_wm) {
  8810. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8811. if (ret) {
  8812. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8813. return ret;
  8814. }
  8815. }
  8816. if (dev_priv->display.compute_intermediate_wm &&
  8817. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8818. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8819. return 0;
  8820. /*
  8821. * Calculate 'intermediate' watermarks that satisfy both the
  8822. * old state and the new state. We can program these
  8823. * immediately.
  8824. */
  8825. ret = dev_priv->display.compute_intermediate_wm(dev,
  8826. intel_crtc,
  8827. pipe_config);
  8828. if (ret) {
  8829. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8830. return ret;
  8831. }
  8832. } else if (dev_priv->display.compute_intermediate_wm) {
  8833. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8834. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8835. }
  8836. if (INTEL_GEN(dev_priv) >= 9) {
  8837. if (mode_changed)
  8838. ret = skl_update_scaler_crtc(pipe_config);
  8839. if (!ret)
  8840. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8841. pipe_config);
  8842. if (!ret)
  8843. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8844. pipe_config);
  8845. }
  8846. return ret;
  8847. }
  8848. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8849. .atomic_begin = intel_begin_crtc_commit,
  8850. .atomic_flush = intel_finish_crtc_commit,
  8851. .atomic_check = intel_crtc_atomic_check,
  8852. };
  8853. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8854. {
  8855. struct intel_connector *connector;
  8856. struct drm_connector_list_iter conn_iter;
  8857. drm_connector_list_iter_begin(dev, &conn_iter);
  8858. for_each_intel_connector_iter(connector, &conn_iter) {
  8859. if (connector->base.state->crtc)
  8860. drm_connector_unreference(&connector->base);
  8861. if (connector->base.encoder) {
  8862. connector->base.state->best_encoder =
  8863. connector->base.encoder;
  8864. connector->base.state->crtc =
  8865. connector->base.encoder->crtc;
  8866. drm_connector_reference(&connector->base);
  8867. } else {
  8868. connector->base.state->best_encoder = NULL;
  8869. connector->base.state->crtc = NULL;
  8870. }
  8871. }
  8872. drm_connector_list_iter_end(&conn_iter);
  8873. }
  8874. static void
  8875. connected_sink_compute_bpp(struct intel_connector *connector,
  8876. struct intel_crtc_state *pipe_config)
  8877. {
  8878. const struct drm_display_info *info = &connector->base.display_info;
  8879. int bpp = pipe_config->pipe_bpp;
  8880. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8881. connector->base.base.id,
  8882. connector->base.name);
  8883. /* Don't use an invalid EDID bpc value */
  8884. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8885. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8886. bpp, info->bpc * 3);
  8887. pipe_config->pipe_bpp = info->bpc * 3;
  8888. }
  8889. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8890. if (info->bpc == 0 && bpp > 24) {
  8891. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8892. bpp);
  8893. pipe_config->pipe_bpp = 24;
  8894. }
  8895. }
  8896. static int
  8897. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8898. struct intel_crtc_state *pipe_config)
  8899. {
  8900. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8901. struct drm_atomic_state *state;
  8902. struct drm_connector *connector;
  8903. struct drm_connector_state *connector_state;
  8904. int bpp, i;
  8905. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8906. IS_CHERRYVIEW(dev_priv)))
  8907. bpp = 10*3;
  8908. else if (INTEL_GEN(dev_priv) >= 5)
  8909. bpp = 12*3;
  8910. else
  8911. bpp = 8*3;
  8912. pipe_config->pipe_bpp = bpp;
  8913. state = pipe_config->base.state;
  8914. /* Clamp display bpp to EDID value */
  8915. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8916. if (connector_state->crtc != &crtc->base)
  8917. continue;
  8918. connected_sink_compute_bpp(to_intel_connector(connector),
  8919. pipe_config);
  8920. }
  8921. return bpp;
  8922. }
  8923. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8924. {
  8925. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8926. "type: 0x%x flags: 0x%x\n",
  8927. mode->crtc_clock,
  8928. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8929. mode->crtc_hsync_end, mode->crtc_htotal,
  8930. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8931. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8932. }
  8933. static inline void
  8934. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8935. unsigned int lane_count, struct intel_link_m_n *m_n)
  8936. {
  8937. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8938. id, lane_count,
  8939. m_n->gmch_m, m_n->gmch_n,
  8940. m_n->link_m, m_n->link_n, m_n->tu);
  8941. }
  8942. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8943. struct intel_crtc_state *pipe_config,
  8944. const char *context)
  8945. {
  8946. struct drm_device *dev = crtc->base.dev;
  8947. struct drm_i915_private *dev_priv = to_i915(dev);
  8948. struct drm_plane *plane;
  8949. struct intel_plane *intel_plane;
  8950. struct intel_plane_state *state;
  8951. struct drm_framebuffer *fb;
  8952. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8953. crtc->base.base.id, crtc->base.name, context);
  8954. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8955. transcoder_name(pipe_config->cpu_transcoder),
  8956. pipe_config->pipe_bpp, pipe_config->dither);
  8957. if (pipe_config->has_pch_encoder)
  8958. intel_dump_m_n_config(pipe_config, "fdi",
  8959. pipe_config->fdi_lanes,
  8960. &pipe_config->fdi_m_n);
  8961. if (pipe_config->ycbcr420)
  8962. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8963. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8964. intel_dump_m_n_config(pipe_config, "dp m_n",
  8965. pipe_config->lane_count, &pipe_config->dp_m_n);
  8966. if (pipe_config->has_drrs)
  8967. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8968. pipe_config->lane_count,
  8969. &pipe_config->dp_m2_n2);
  8970. }
  8971. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8972. pipe_config->has_audio, pipe_config->has_infoframe);
  8973. DRM_DEBUG_KMS("requested mode:\n");
  8974. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8975. DRM_DEBUG_KMS("adjusted mode:\n");
  8976. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8977. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8978. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8979. pipe_config->port_clock,
  8980. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8981. pipe_config->pixel_rate);
  8982. if (INTEL_GEN(dev_priv) >= 9)
  8983. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8984. crtc->num_scalers,
  8985. pipe_config->scaler_state.scaler_users,
  8986. pipe_config->scaler_state.scaler_id);
  8987. if (HAS_GMCH_DISPLAY(dev_priv))
  8988. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8989. pipe_config->gmch_pfit.control,
  8990. pipe_config->gmch_pfit.pgm_ratios,
  8991. pipe_config->gmch_pfit.lvds_border_bits);
  8992. else
  8993. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8994. pipe_config->pch_pfit.pos,
  8995. pipe_config->pch_pfit.size,
  8996. enableddisabled(pipe_config->pch_pfit.enabled));
  8997. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8998. pipe_config->ips_enabled, pipe_config->double_wide);
  8999. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9000. DRM_DEBUG_KMS("planes on this crtc\n");
  9001. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9002. struct drm_format_name_buf format_name;
  9003. intel_plane = to_intel_plane(plane);
  9004. if (intel_plane->pipe != crtc->pipe)
  9005. continue;
  9006. state = to_intel_plane_state(plane->state);
  9007. fb = state->base.fb;
  9008. if (!fb) {
  9009. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9010. plane->base.id, plane->name, state->scaler_id);
  9011. continue;
  9012. }
  9013. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9014. plane->base.id, plane->name,
  9015. fb->base.id, fb->width, fb->height,
  9016. drm_get_format_name(fb->format->format, &format_name));
  9017. if (INTEL_GEN(dev_priv) >= 9)
  9018. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9019. state->scaler_id,
  9020. state->base.src.x1 >> 16,
  9021. state->base.src.y1 >> 16,
  9022. drm_rect_width(&state->base.src) >> 16,
  9023. drm_rect_height(&state->base.src) >> 16,
  9024. state->base.dst.x1, state->base.dst.y1,
  9025. drm_rect_width(&state->base.dst),
  9026. drm_rect_height(&state->base.dst));
  9027. }
  9028. }
  9029. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9030. {
  9031. struct drm_device *dev = state->dev;
  9032. struct drm_connector *connector;
  9033. struct drm_connector_list_iter conn_iter;
  9034. unsigned int used_ports = 0;
  9035. unsigned int used_mst_ports = 0;
  9036. /*
  9037. * Walk the connector list instead of the encoder
  9038. * list to detect the problem on ddi platforms
  9039. * where there's just one encoder per digital port.
  9040. */
  9041. drm_connector_list_iter_begin(dev, &conn_iter);
  9042. drm_for_each_connector_iter(connector, &conn_iter) {
  9043. struct drm_connector_state *connector_state;
  9044. struct intel_encoder *encoder;
  9045. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9046. if (!connector_state)
  9047. connector_state = connector->state;
  9048. if (!connector_state->best_encoder)
  9049. continue;
  9050. encoder = to_intel_encoder(connector_state->best_encoder);
  9051. WARN_ON(!connector_state->crtc);
  9052. switch (encoder->type) {
  9053. unsigned int port_mask;
  9054. case INTEL_OUTPUT_UNKNOWN:
  9055. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9056. break;
  9057. case INTEL_OUTPUT_DP:
  9058. case INTEL_OUTPUT_HDMI:
  9059. case INTEL_OUTPUT_EDP:
  9060. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9061. /* the same port mustn't appear more than once */
  9062. if (used_ports & port_mask)
  9063. return false;
  9064. used_ports |= port_mask;
  9065. break;
  9066. case INTEL_OUTPUT_DP_MST:
  9067. used_mst_ports |=
  9068. 1 << enc_to_mst(&encoder->base)->primary->port;
  9069. break;
  9070. default:
  9071. break;
  9072. }
  9073. }
  9074. drm_connector_list_iter_end(&conn_iter);
  9075. /* can't mix MST and SST/HDMI on the same port */
  9076. if (used_ports & used_mst_ports)
  9077. return false;
  9078. return true;
  9079. }
  9080. static void
  9081. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9082. {
  9083. struct drm_i915_private *dev_priv =
  9084. to_i915(crtc_state->base.crtc->dev);
  9085. struct intel_crtc_scaler_state scaler_state;
  9086. struct intel_dpll_hw_state dpll_hw_state;
  9087. struct intel_shared_dpll *shared_dpll;
  9088. struct intel_crtc_wm_state wm_state;
  9089. bool force_thru, ips_force_disable;
  9090. /* FIXME: before the switch to atomic started, a new pipe_config was
  9091. * kzalloc'd. Code that depends on any field being zero should be
  9092. * fixed, so that the crtc_state can be safely duplicated. For now,
  9093. * only fields that are know to not cause problems are preserved. */
  9094. scaler_state = crtc_state->scaler_state;
  9095. shared_dpll = crtc_state->shared_dpll;
  9096. dpll_hw_state = crtc_state->dpll_hw_state;
  9097. force_thru = crtc_state->pch_pfit.force_thru;
  9098. ips_force_disable = crtc_state->ips_force_disable;
  9099. if (IS_G4X(dev_priv) ||
  9100. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9101. wm_state = crtc_state->wm;
  9102. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9103. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9104. memset(&crtc_state->base + 1, 0,
  9105. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9106. crtc_state->scaler_state = scaler_state;
  9107. crtc_state->shared_dpll = shared_dpll;
  9108. crtc_state->dpll_hw_state = dpll_hw_state;
  9109. crtc_state->pch_pfit.force_thru = force_thru;
  9110. crtc_state->ips_force_disable = ips_force_disable;
  9111. if (IS_G4X(dev_priv) ||
  9112. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9113. crtc_state->wm = wm_state;
  9114. }
  9115. static int
  9116. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9117. struct intel_crtc_state *pipe_config)
  9118. {
  9119. struct drm_atomic_state *state = pipe_config->base.state;
  9120. struct intel_encoder *encoder;
  9121. struct drm_connector *connector;
  9122. struct drm_connector_state *connector_state;
  9123. int base_bpp, ret = -EINVAL;
  9124. int i;
  9125. bool retry = true;
  9126. clear_intel_crtc_state(pipe_config);
  9127. pipe_config->cpu_transcoder =
  9128. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9129. /*
  9130. * Sanitize sync polarity flags based on requested ones. If neither
  9131. * positive or negative polarity is requested, treat this as meaning
  9132. * negative polarity.
  9133. */
  9134. if (!(pipe_config->base.adjusted_mode.flags &
  9135. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9136. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9137. if (!(pipe_config->base.adjusted_mode.flags &
  9138. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9139. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9140. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9141. pipe_config);
  9142. if (base_bpp < 0)
  9143. goto fail;
  9144. /*
  9145. * Determine the real pipe dimensions. Note that stereo modes can
  9146. * increase the actual pipe size due to the frame doubling and
  9147. * insertion of additional space for blanks between the frame. This
  9148. * is stored in the crtc timings. We use the requested mode to do this
  9149. * computation to clearly distinguish it from the adjusted mode, which
  9150. * can be changed by the connectors in the below retry loop.
  9151. */
  9152. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9153. &pipe_config->pipe_src_w,
  9154. &pipe_config->pipe_src_h);
  9155. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9156. if (connector_state->crtc != crtc)
  9157. continue;
  9158. encoder = to_intel_encoder(connector_state->best_encoder);
  9159. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9160. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9161. goto fail;
  9162. }
  9163. /*
  9164. * Determine output_types before calling the .compute_config()
  9165. * hooks so that the hooks can use this information safely.
  9166. */
  9167. pipe_config->output_types |= 1 << encoder->type;
  9168. }
  9169. encoder_retry:
  9170. /* Ensure the port clock defaults are reset when retrying. */
  9171. pipe_config->port_clock = 0;
  9172. pipe_config->pixel_multiplier = 1;
  9173. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9174. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9175. CRTC_STEREO_DOUBLE);
  9176. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9177. * adjust it according to limitations or connector properties, and also
  9178. * a chance to reject the mode entirely.
  9179. */
  9180. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9181. if (connector_state->crtc != crtc)
  9182. continue;
  9183. encoder = to_intel_encoder(connector_state->best_encoder);
  9184. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9185. DRM_DEBUG_KMS("Encoder config failure\n");
  9186. goto fail;
  9187. }
  9188. }
  9189. /* Set default port clock if not overwritten by the encoder. Needs to be
  9190. * done afterwards in case the encoder adjusts the mode. */
  9191. if (!pipe_config->port_clock)
  9192. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9193. * pipe_config->pixel_multiplier;
  9194. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9195. if (ret < 0) {
  9196. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9197. goto fail;
  9198. }
  9199. if (ret == RETRY) {
  9200. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9201. ret = -EINVAL;
  9202. goto fail;
  9203. }
  9204. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9205. retry = false;
  9206. goto encoder_retry;
  9207. }
  9208. /* Dithering seems to not pass-through bits correctly when it should, so
  9209. * only enable it on 6bpc panels and when its not a compliance
  9210. * test requesting 6bpc video pattern.
  9211. */
  9212. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9213. !pipe_config->dither_force_disable;
  9214. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9215. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9216. fail:
  9217. return ret;
  9218. }
  9219. static void
  9220. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9221. {
  9222. struct drm_crtc *crtc;
  9223. struct drm_crtc_state *new_crtc_state;
  9224. int i;
  9225. /* Double check state. */
  9226. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9227. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9228. /*
  9229. * Update legacy state to satisfy fbc code. This can
  9230. * be removed when fbc uses the atomic state.
  9231. */
  9232. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9233. struct drm_plane_state *plane_state = crtc->primary->state;
  9234. crtc->primary->fb = plane_state->fb;
  9235. crtc->x = plane_state->src_x >> 16;
  9236. crtc->y = plane_state->src_y >> 16;
  9237. }
  9238. }
  9239. }
  9240. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9241. {
  9242. int diff;
  9243. if (clock1 == clock2)
  9244. return true;
  9245. if (!clock1 || !clock2)
  9246. return false;
  9247. diff = abs(clock1 - clock2);
  9248. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9249. return true;
  9250. return false;
  9251. }
  9252. static bool
  9253. intel_compare_m_n(unsigned int m, unsigned int n,
  9254. unsigned int m2, unsigned int n2,
  9255. bool exact)
  9256. {
  9257. if (m == m2 && n == n2)
  9258. return true;
  9259. if (exact || !m || !n || !m2 || !n2)
  9260. return false;
  9261. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9262. if (n > n2) {
  9263. while (n > n2) {
  9264. m2 <<= 1;
  9265. n2 <<= 1;
  9266. }
  9267. } else if (n < n2) {
  9268. while (n < n2) {
  9269. m <<= 1;
  9270. n <<= 1;
  9271. }
  9272. }
  9273. if (n != n2)
  9274. return false;
  9275. return intel_fuzzy_clock_check(m, m2);
  9276. }
  9277. static bool
  9278. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9279. struct intel_link_m_n *m2_n2,
  9280. bool adjust)
  9281. {
  9282. if (m_n->tu == m2_n2->tu &&
  9283. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9284. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9285. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9286. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9287. if (adjust)
  9288. *m2_n2 = *m_n;
  9289. return true;
  9290. }
  9291. return false;
  9292. }
  9293. static void __printf(3, 4)
  9294. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9295. {
  9296. char *level;
  9297. unsigned int category;
  9298. struct va_format vaf;
  9299. va_list args;
  9300. if (adjust) {
  9301. level = KERN_DEBUG;
  9302. category = DRM_UT_KMS;
  9303. } else {
  9304. level = KERN_ERR;
  9305. category = DRM_UT_NONE;
  9306. }
  9307. va_start(args, format);
  9308. vaf.fmt = format;
  9309. vaf.va = &args;
  9310. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9311. va_end(args);
  9312. }
  9313. static bool
  9314. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9315. struct intel_crtc_state *current_config,
  9316. struct intel_crtc_state *pipe_config,
  9317. bool adjust)
  9318. {
  9319. bool ret = true;
  9320. #define PIPE_CONF_CHECK_X(name) \
  9321. if (current_config->name != pipe_config->name) { \
  9322. pipe_config_err(adjust, __stringify(name), \
  9323. "(expected 0x%08x, found 0x%08x)\n", \
  9324. current_config->name, \
  9325. pipe_config->name); \
  9326. ret = false; \
  9327. }
  9328. #define PIPE_CONF_CHECK_I(name) \
  9329. if (current_config->name != pipe_config->name) { \
  9330. pipe_config_err(adjust, __stringify(name), \
  9331. "(expected %i, found %i)\n", \
  9332. current_config->name, \
  9333. pipe_config->name); \
  9334. ret = false; \
  9335. }
  9336. #define PIPE_CONF_CHECK_P(name) \
  9337. if (current_config->name != pipe_config->name) { \
  9338. pipe_config_err(adjust, __stringify(name), \
  9339. "(expected %p, found %p)\n", \
  9340. current_config->name, \
  9341. pipe_config->name); \
  9342. ret = false; \
  9343. }
  9344. #define PIPE_CONF_CHECK_M_N(name) \
  9345. if (!intel_compare_link_m_n(&current_config->name, \
  9346. &pipe_config->name,\
  9347. adjust)) { \
  9348. pipe_config_err(adjust, __stringify(name), \
  9349. "(expected tu %i gmch %i/%i link %i/%i, " \
  9350. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9351. current_config->name.tu, \
  9352. current_config->name.gmch_m, \
  9353. current_config->name.gmch_n, \
  9354. current_config->name.link_m, \
  9355. current_config->name.link_n, \
  9356. pipe_config->name.tu, \
  9357. pipe_config->name.gmch_m, \
  9358. pipe_config->name.gmch_n, \
  9359. pipe_config->name.link_m, \
  9360. pipe_config->name.link_n); \
  9361. ret = false; \
  9362. }
  9363. /* This is required for BDW+ where there is only one set of registers for
  9364. * switching between high and low RR.
  9365. * This macro can be used whenever a comparison has to be made between one
  9366. * hw state and multiple sw state variables.
  9367. */
  9368. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9369. if (!intel_compare_link_m_n(&current_config->name, \
  9370. &pipe_config->name, adjust) && \
  9371. !intel_compare_link_m_n(&current_config->alt_name, \
  9372. &pipe_config->name, adjust)) { \
  9373. pipe_config_err(adjust, __stringify(name), \
  9374. "(expected tu %i gmch %i/%i link %i/%i, " \
  9375. "or tu %i gmch %i/%i link %i/%i, " \
  9376. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9377. current_config->name.tu, \
  9378. current_config->name.gmch_m, \
  9379. current_config->name.gmch_n, \
  9380. current_config->name.link_m, \
  9381. current_config->name.link_n, \
  9382. current_config->alt_name.tu, \
  9383. current_config->alt_name.gmch_m, \
  9384. current_config->alt_name.gmch_n, \
  9385. current_config->alt_name.link_m, \
  9386. current_config->alt_name.link_n, \
  9387. pipe_config->name.tu, \
  9388. pipe_config->name.gmch_m, \
  9389. pipe_config->name.gmch_n, \
  9390. pipe_config->name.link_m, \
  9391. pipe_config->name.link_n); \
  9392. ret = false; \
  9393. }
  9394. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9395. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9396. pipe_config_err(adjust, __stringify(name), \
  9397. "(%x) (expected %i, found %i)\n", \
  9398. (mask), \
  9399. current_config->name & (mask), \
  9400. pipe_config->name & (mask)); \
  9401. ret = false; \
  9402. }
  9403. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9404. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9405. pipe_config_err(adjust, __stringify(name), \
  9406. "(expected %i, found %i)\n", \
  9407. current_config->name, \
  9408. pipe_config->name); \
  9409. ret = false; \
  9410. }
  9411. #define PIPE_CONF_QUIRK(quirk) \
  9412. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9413. PIPE_CONF_CHECK_I(cpu_transcoder);
  9414. PIPE_CONF_CHECK_I(has_pch_encoder);
  9415. PIPE_CONF_CHECK_I(fdi_lanes);
  9416. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9417. PIPE_CONF_CHECK_I(lane_count);
  9418. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9419. if (INTEL_GEN(dev_priv) < 8) {
  9420. PIPE_CONF_CHECK_M_N(dp_m_n);
  9421. if (current_config->has_drrs)
  9422. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9423. } else
  9424. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9425. PIPE_CONF_CHECK_X(output_types);
  9426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9432. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9433. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9434. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9435. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9436. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9437. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9438. PIPE_CONF_CHECK_I(pixel_multiplier);
  9439. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9440. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9441. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9442. PIPE_CONF_CHECK_I(limited_color_range);
  9443. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9444. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9445. PIPE_CONF_CHECK_I(has_infoframe);
  9446. PIPE_CONF_CHECK_I(ycbcr420);
  9447. PIPE_CONF_CHECK_I(has_audio);
  9448. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9449. DRM_MODE_FLAG_INTERLACE);
  9450. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9451. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9452. DRM_MODE_FLAG_PHSYNC);
  9453. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9454. DRM_MODE_FLAG_NHSYNC);
  9455. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9456. DRM_MODE_FLAG_PVSYNC);
  9457. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9458. DRM_MODE_FLAG_NVSYNC);
  9459. }
  9460. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9461. /* pfit ratios are autocomputed by the hw on gen4+ */
  9462. if (INTEL_GEN(dev_priv) < 4)
  9463. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9464. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9465. if (!adjust) {
  9466. PIPE_CONF_CHECK_I(pipe_src_w);
  9467. PIPE_CONF_CHECK_I(pipe_src_h);
  9468. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9469. if (current_config->pch_pfit.enabled) {
  9470. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9471. PIPE_CONF_CHECK_X(pch_pfit.size);
  9472. }
  9473. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9474. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9475. }
  9476. /* BDW+ don't expose a synchronous way to read the state */
  9477. if (IS_HASWELL(dev_priv))
  9478. PIPE_CONF_CHECK_I(ips_enabled);
  9479. PIPE_CONF_CHECK_I(double_wide);
  9480. PIPE_CONF_CHECK_P(shared_dpll);
  9481. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9482. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9483. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9484. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9485. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9486. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9487. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9488. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9489. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9490. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9491. PIPE_CONF_CHECK_X(dsi_pll.div);
  9492. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9493. PIPE_CONF_CHECK_I(pipe_bpp);
  9494. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9495. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9496. #undef PIPE_CONF_CHECK_X
  9497. #undef PIPE_CONF_CHECK_I
  9498. #undef PIPE_CONF_CHECK_P
  9499. #undef PIPE_CONF_CHECK_FLAGS
  9500. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9501. #undef PIPE_CONF_QUIRK
  9502. return ret;
  9503. }
  9504. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9505. const struct intel_crtc_state *pipe_config)
  9506. {
  9507. if (pipe_config->has_pch_encoder) {
  9508. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9509. &pipe_config->fdi_m_n);
  9510. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9511. /*
  9512. * FDI already provided one idea for the dotclock.
  9513. * Yell if the encoder disagrees.
  9514. */
  9515. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9516. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9517. fdi_dotclock, dotclock);
  9518. }
  9519. }
  9520. static void verify_wm_state(struct drm_crtc *crtc,
  9521. struct drm_crtc_state *new_state)
  9522. {
  9523. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9524. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9525. struct skl_pipe_wm hw_wm, *sw_wm;
  9526. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9527. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9529. const enum pipe pipe = intel_crtc->pipe;
  9530. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9531. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9532. return;
  9533. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9534. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9535. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9536. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9537. /* planes */
  9538. for_each_universal_plane(dev_priv, pipe, plane) {
  9539. hw_plane_wm = &hw_wm.planes[plane];
  9540. sw_plane_wm = &sw_wm->planes[plane];
  9541. /* Watermarks */
  9542. for (level = 0; level <= max_level; level++) {
  9543. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9544. &sw_plane_wm->wm[level]))
  9545. continue;
  9546. 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",
  9547. pipe_name(pipe), plane + 1, level,
  9548. sw_plane_wm->wm[level].plane_en,
  9549. sw_plane_wm->wm[level].plane_res_b,
  9550. sw_plane_wm->wm[level].plane_res_l,
  9551. hw_plane_wm->wm[level].plane_en,
  9552. hw_plane_wm->wm[level].plane_res_b,
  9553. hw_plane_wm->wm[level].plane_res_l);
  9554. }
  9555. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9556. &sw_plane_wm->trans_wm)) {
  9557. 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",
  9558. pipe_name(pipe), plane + 1,
  9559. sw_plane_wm->trans_wm.plane_en,
  9560. sw_plane_wm->trans_wm.plane_res_b,
  9561. sw_plane_wm->trans_wm.plane_res_l,
  9562. hw_plane_wm->trans_wm.plane_en,
  9563. hw_plane_wm->trans_wm.plane_res_b,
  9564. hw_plane_wm->trans_wm.plane_res_l);
  9565. }
  9566. /* DDB */
  9567. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9568. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9569. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9570. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9571. pipe_name(pipe), plane + 1,
  9572. sw_ddb_entry->start, sw_ddb_entry->end,
  9573. hw_ddb_entry->start, hw_ddb_entry->end);
  9574. }
  9575. }
  9576. /*
  9577. * cursor
  9578. * If the cursor plane isn't active, we may not have updated it's ddb
  9579. * allocation. In that case since the ddb allocation will be updated
  9580. * once the plane becomes visible, we can skip this check
  9581. */
  9582. if (1) {
  9583. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9584. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9585. /* Watermarks */
  9586. for (level = 0; level <= max_level; level++) {
  9587. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9588. &sw_plane_wm->wm[level]))
  9589. continue;
  9590. 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",
  9591. pipe_name(pipe), level,
  9592. sw_plane_wm->wm[level].plane_en,
  9593. sw_plane_wm->wm[level].plane_res_b,
  9594. sw_plane_wm->wm[level].plane_res_l,
  9595. hw_plane_wm->wm[level].plane_en,
  9596. hw_plane_wm->wm[level].plane_res_b,
  9597. hw_plane_wm->wm[level].plane_res_l);
  9598. }
  9599. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9600. &sw_plane_wm->trans_wm)) {
  9601. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9602. pipe_name(pipe),
  9603. sw_plane_wm->trans_wm.plane_en,
  9604. sw_plane_wm->trans_wm.plane_res_b,
  9605. sw_plane_wm->trans_wm.plane_res_l,
  9606. hw_plane_wm->trans_wm.plane_en,
  9607. hw_plane_wm->trans_wm.plane_res_b,
  9608. hw_plane_wm->trans_wm.plane_res_l);
  9609. }
  9610. /* DDB */
  9611. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9612. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9613. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9614. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9615. pipe_name(pipe),
  9616. sw_ddb_entry->start, sw_ddb_entry->end,
  9617. hw_ddb_entry->start, hw_ddb_entry->end);
  9618. }
  9619. }
  9620. }
  9621. static void
  9622. verify_connector_state(struct drm_device *dev,
  9623. struct drm_atomic_state *state,
  9624. struct drm_crtc *crtc)
  9625. {
  9626. struct drm_connector *connector;
  9627. struct drm_connector_state *new_conn_state;
  9628. int i;
  9629. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9630. struct drm_encoder *encoder = connector->encoder;
  9631. struct drm_crtc_state *crtc_state = NULL;
  9632. if (new_conn_state->crtc != crtc)
  9633. continue;
  9634. if (crtc)
  9635. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9636. intel_connector_verify_state(crtc_state, new_conn_state);
  9637. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9638. "connector's atomic encoder doesn't match legacy encoder\n");
  9639. }
  9640. }
  9641. static void
  9642. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9643. {
  9644. struct intel_encoder *encoder;
  9645. struct drm_connector *connector;
  9646. struct drm_connector_state *old_conn_state, *new_conn_state;
  9647. int i;
  9648. for_each_intel_encoder(dev, encoder) {
  9649. bool enabled = false, found = false;
  9650. enum pipe pipe;
  9651. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9652. encoder->base.base.id,
  9653. encoder->base.name);
  9654. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9655. new_conn_state, i) {
  9656. if (old_conn_state->best_encoder == &encoder->base)
  9657. found = true;
  9658. if (new_conn_state->best_encoder != &encoder->base)
  9659. continue;
  9660. found = enabled = true;
  9661. I915_STATE_WARN(new_conn_state->crtc !=
  9662. encoder->base.crtc,
  9663. "connector's crtc doesn't match encoder crtc\n");
  9664. }
  9665. if (!found)
  9666. continue;
  9667. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9668. "encoder's enabled state mismatch "
  9669. "(expected %i, found %i)\n",
  9670. !!encoder->base.crtc, enabled);
  9671. if (!encoder->base.crtc) {
  9672. bool active;
  9673. active = encoder->get_hw_state(encoder, &pipe);
  9674. I915_STATE_WARN(active,
  9675. "encoder detached but still enabled on pipe %c.\n",
  9676. pipe_name(pipe));
  9677. }
  9678. }
  9679. }
  9680. static void
  9681. verify_crtc_state(struct drm_crtc *crtc,
  9682. struct drm_crtc_state *old_crtc_state,
  9683. struct drm_crtc_state *new_crtc_state)
  9684. {
  9685. struct drm_device *dev = crtc->dev;
  9686. struct drm_i915_private *dev_priv = to_i915(dev);
  9687. struct intel_encoder *encoder;
  9688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9689. struct intel_crtc_state *pipe_config, *sw_config;
  9690. struct drm_atomic_state *old_state;
  9691. bool active;
  9692. old_state = old_crtc_state->state;
  9693. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9694. pipe_config = to_intel_crtc_state(old_crtc_state);
  9695. memset(pipe_config, 0, sizeof(*pipe_config));
  9696. pipe_config->base.crtc = crtc;
  9697. pipe_config->base.state = old_state;
  9698. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9699. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9700. /* we keep both pipes enabled on 830 */
  9701. if (IS_I830(dev_priv))
  9702. active = new_crtc_state->active;
  9703. I915_STATE_WARN(new_crtc_state->active != active,
  9704. "crtc active state doesn't match with hw state "
  9705. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9706. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9707. "transitional active state does not match atomic hw state "
  9708. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9709. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9710. enum pipe pipe;
  9711. active = encoder->get_hw_state(encoder, &pipe);
  9712. I915_STATE_WARN(active != new_crtc_state->active,
  9713. "[ENCODER:%i] active %i with crtc active %i\n",
  9714. encoder->base.base.id, active, new_crtc_state->active);
  9715. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9716. "Encoder connected to wrong pipe %c\n",
  9717. pipe_name(pipe));
  9718. if (active) {
  9719. pipe_config->output_types |= 1 << encoder->type;
  9720. encoder->get_config(encoder, pipe_config);
  9721. }
  9722. }
  9723. intel_crtc_compute_pixel_rate(pipe_config);
  9724. if (!new_crtc_state->active)
  9725. return;
  9726. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9727. sw_config = to_intel_crtc_state(new_crtc_state);
  9728. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9729. pipe_config, false)) {
  9730. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9731. intel_dump_pipe_config(intel_crtc, pipe_config,
  9732. "[hw state]");
  9733. intel_dump_pipe_config(intel_crtc, sw_config,
  9734. "[sw state]");
  9735. }
  9736. }
  9737. static void
  9738. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9739. struct intel_shared_dpll *pll,
  9740. struct drm_crtc *crtc,
  9741. struct drm_crtc_state *new_state)
  9742. {
  9743. struct intel_dpll_hw_state dpll_hw_state;
  9744. unsigned crtc_mask;
  9745. bool active;
  9746. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9747. DRM_DEBUG_KMS("%s\n", pll->name);
  9748. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9749. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9750. I915_STATE_WARN(!pll->on && pll->active_mask,
  9751. "pll in active use but not on in sw tracking\n");
  9752. I915_STATE_WARN(pll->on && !pll->active_mask,
  9753. "pll is on but not used by any active crtc\n");
  9754. I915_STATE_WARN(pll->on != active,
  9755. "pll on state mismatch (expected %i, found %i)\n",
  9756. pll->on, active);
  9757. }
  9758. if (!crtc) {
  9759. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9760. "more active pll users than references: %x vs %x\n",
  9761. pll->active_mask, pll->state.crtc_mask);
  9762. return;
  9763. }
  9764. crtc_mask = 1 << drm_crtc_index(crtc);
  9765. if (new_state->active)
  9766. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9767. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9768. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9769. else
  9770. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9771. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9772. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9773. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9774. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9775. crtc_mask, pll->state.crtc_mask);
  9776. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9777. &dpll_hw_state,
  9778. sizeof(dpll_hw_state)),
  9779. "pll hw state mismatch\n");
  9780. }
  9781. static void
  9782. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9783. struct drm_crtc_state *old_crtc_state,
  9784. struct drm_crtc_state *new_crtc_state)
  9785. {
  9786. struct drm_i915_private *dev_priv = to_i915(dev);
  9787. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9788. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9789. if (new_state->shared_dpll)
  9790. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9791. if (old_state->shared_dpll &&
  9792. old_state->shared_dpll != new_state->shared_dpll) {
  9793. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9794. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9795. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9796. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9797. pipe_name(drm_crtc_index(crtc)));
  9798. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9799. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9800. pipe_name(drm_crtc_index(crtc)));
  9801. }
  9802. }
  9803. static void
  9804. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9805. struct drm_atomic_state *state,
  9806. struct drm_crtc_state *old_state,
  9807. struct drm_crtc_state *new_state)
  9808. {
  9809. if (!needs_modeset(new_state) &&
  9810. !to_intel_crtc_state(new_state)->update_pipe)
  9811. return;
  9812. verify_wm_state(crtc, new_state);
  9813. verify_connector_state(crtc->dev, state, crtc);
  9814. verify_crtc_state(crtc, old_state, new_state);
  9815. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9816. }
  9817. static void
  9818. verify_disabled_dpll_state(struct drm_device *dev)
  9819. {
  9820. struct drm_i915_private *dev_priv = to_i915(dev);
  9821. int i;
  9822. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9823. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9824. }
  9825. static void
  9826. intel_modeset_verify_disabled(struct drm_device *dev,
  9827. struct drm_atomic_state *state)
  9828. {
  9829. verify_encoder_state(dev, state);
  9830. verify_connector_state(dev, state, NULL);
  9831. verify_disabled_dpll_state(dev);
  9832. }
  9833. static void update_scanline_offset(struct intel_crtc *crtc)
  9834. {
  9835. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9836. /*
  9837. * The scanline counter increments at the leading edge of hsync.
  9838. *
  9839. * On most platforms it starts counting from vtotal-1 on the
  9840. * first active line. That means the scanline counter value is
  9841. * always one less than what we would expect. Ie. just after
  9842. * start of vblank, which also occurs at start of hsync (on the
  9843. * last active line), the scanline counter will read vblank_start-1.
  9844. *
  9845. * On gen2 the scanline counter starts counting from 1 instead
  9846. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9847. * to keep the value positive), instead of adding one.
  9848. *
  9849. * On HSW+ the behaviour of the scanline counter depends on the output
  9850. * type. For DP ports it behaves like most other platforms, but on HDMI
  9851. * there's an extra 1 line difference. So we need to add two instead of
  9852. * one to the value.
  9853. *
  9854. * On VLV/CHV DSI the scanline counter would appear to increment
  9855. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9856. * that means we can't tell whether we're in vblank or not while
  9857. * we're on that particular line. We must still set scanline_offset
  9858. * to 1 so that the vblank timestamps come out correct when we query
  9859. * the scanline counter from within the vblank interrupt handler.
  9860. * However if queried just before the start of vblank we'll get an
  9861. * answer that's slightly in the future.
  9862. */
  9863. if (IS_GEN2(dev_priv)) {
  9864. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9865. int vtotal;
  9866. vtotal = adjusted_mode->crtc_vtotal;
  9867. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9868. vtotal /= 2;
  9869. crtc->scanline_offset = vtotal - 1;
  9870. } else if (HAS_DDI(dev_priv) &&
  9871. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9872. crtc->scanline_offset = 2;
  9873. } else
  9874. crtc->scanline_offset = 1;
  9875. }
  9876. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9877. {
  9878. struct drm_device *dev = state->dev;
  9879. struct drm_i915_private *dev_priv = to_i915(dev);
  9880. struct drm_crtc *crtc;
  9881. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9882. int i;
  9883. if (!dev_priv->display.crtc_compute_clock)
  9884. return;
  9885. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9886. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9887. struct intel_shared_dpll *old_dpll =
  9888. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9889. if (!needs_modeset(new_crtc_state))
  9890. continue;
  9891. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9892. if (!old_dpll)
  9893. continue;
  9894. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9895. }
  9896. }
  9897. /*
  9898. * This implements the workaround described in the "notes" section of the mode
  9899. * set sequence documentation. When going from no pipes or single pipe to
  9900. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9901. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9902. */
  9903. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9904. {
  9905. struct drm_crtc_state *crtc_state;
  9906. struct intel_crtc *intel_crtc;
  9907. struct drm_crtc *crtc;
  9908. struct intel_crtc_state *first_crtc_state = NULL;
  9909. struct intel_crtc_state *other_crtc_state = NULL;
  9910. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9911. int i;
  9912. /* look at all crtc's that are going to be enabled in during modeset */
  9913. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9914. intel_crtc = to_intel_crtc(crtc);
  9915. if (!crtc_state->active || !needs_modeset(crtc_state))
  9916. continue;
  9917. if (first_crtc_state) {
  9918. other_crtc_state = to_intel_crtc_state(crtc_state);
  9919. break;
  9920. } else {
  9921. first_crtc_state = to_intel_crtc_state(crtc_state);
  9922. first_pipe = intel_crtc->pipe;
  9923. }
  9924. }
  9925. /* No workaround needed? */
  9926. if (!first_crtc_state)
  9927. return 0;
  9928. /* w/a possibly needed, check how many crtc's are already enabled. */
  9929. for_each_intel_crtc(state->dev, intel_crtc) {
  9930. struct intel_crtc_state *pipe_config;
  9931. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9932. if (IS_ERR(pipe_config))
  9933. return PTR_ERR(pipe_config);
  9934. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9935. if (!pipe_config->base.active ||
  9936. needs_modeset(&pipe_config->base))
  9937. continue;
  9938. /* 2 or more enabled crtcs means no need for w/a */
  9939. if (enabled_pipe != INVALID_PIPE)
  9940. return 0;
  9941. enabled_pipe = intel_crtc->pipe;
  9942. }
  9943. if (enabled_pipe != INVALID_PIPE)
  9944. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9945. else if (other_crtc_state)
  9946. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9947. return 0;
  9948. }
  9949. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9950. {
  9951. struct drm_crtc *crtc;
  9952. /* Add all pipes to the state */
  9953. for_each_crtc(state->dev, crtc) {
  9954. struct drm_crtc_state *crtc_state;
  9955. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9956. if (IS_ERR(crtc_state))
  9957. return PTR_ERR(crtc_state);
  9958. }
  9959. return 0;
  9960. }
  9961. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9962. {
  9963. struct drm_crtc *crtc;
  9964. /*
  9965. * Add all pipes to the state, and force
  9966. * a modeset on all the active ones.
  9967. */
  9968. for_each_crtc(state->dev, crtc) {
  9969. struct drm_crtc_state *crtc_state;
  9970. int ret;
  9971. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9972. if (IS_ERR(crtc_state))
  9973. return PTR_ERR(crtc_state);
  9974. if (!crtc_state->active || needs_modeset(crtc_state))
  9975. continue;
  9976. crtc_state->mode_changed = true;
  9977. ret = drm_atomic_add_affected_connectors(state, crtc);
  9978. if (ret)
  9979. return ret;
  9980. ret = drm_atomic_add_affected_planes(state, crtc);
  9981. if (ret)
  9982. return ret;
  9983. }
  9984. return 0;
  9985. }
  9986. static int intel_modeset_checks(struct drm_atomic_state *state)
  9987. {
  9988. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9989. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9990. struct drm_crtc *crtc;
  9991. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9992. int ret = 0, i;
  9993. if (!check_digital_port_conflicts(state)) {
  9994. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9995. return -EINVAL;
  9996. }
  9997. intel_state->modeset = true;
  9998. intel_state->active_crtcs = dev_priv->active_crtcs;
  9999. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10000. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10001. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10002. if (new_crtc_state->active)
  10003. intel_state->active_crtcs |= 1 << i;
  10004. else
  10005. intel_state->active_crtcs &= ~(1 << i);
  10006. if (old_crtc_state->active != new_crtc_state->active)
  10007. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10008. }
  10009. /*
  10010. * See if the config requires any additional preparation, e.g.
  10011. * to adjust global state with pipes off. We need to do this
  10012. * here so we can get the modeset_pipe updated config for the new
  10013. * mode set on this crtc. For other crtcs we need to use the
  10014. * adjusted_mode bits in the crtc directly.
  10015. */
  10016. if (dev_priv->display.modeset_calc_cdclk) {
  10017. ret = dev_priv->display.modeset_calc_cdclk(state);
  10018. if (ret < 0)
  10019. return ret;
  10020. /*
  10021. * Writes to dev_priv->cdclk.logical must protected by
  10022. * holding all the crtc locks, even if we don't end up
  10023. * touching the hardware
  10024. */
  10025. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10026. &intel_state->cdclk.logical)) {
  10027. ret = intel_lock_all_pipes(state);
  10028. if (ret < 0)
  10029. return ret;
  10030. }
  10031. /* All pipes must be switched off while we change the cdclk. */
  10032. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10033. &intel_state->cdclk.actual)) {
  10034. ret = intel_modeset_all_pipes(state);
  10035. if (ret < 0)
  10036. return ret;
  10037. }
  10038. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10039. intel_state->cdclk.logical.cdclk,
  10040. intel_state->cdclk.actual.cdclk);
  10041. } else {
  10042. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10043. }
  10044. intel_modeset_clear_plls(state);
  10045. if (IS_HASWELL(dev_priv))
  10046. return haswell_mode_set_planes_workaround(state);
  10047. return 0;
  10048. }
  10049. /*
  10050. * Handle calculation of various watermark data at the end of the atomic check
  10051. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10052. * handlers to ensure that all derived state has been updated.
  10053. */
  10054. static int calc_watermark_data(struct drm_atomic_state *state)
  10055. {
  10056. struct drm_device *dev = state->dev;
  10057. struct drm_i915_private *dev_priv = to_i915(dev);
  10058. /* Is there platform-specific watermark information to calculate? */
  10059. if (dev_priv->display.compute_global_watermarks)
  10060. return dev_priv->display.compute_global_watermarks(state);
  10061. return 0;
  10062. }
  10063. /**
  10064. * intel_atomic_check - validate state object
  10065. * @dev: drm device
  10066. * @state: state to validate
  10067. */
  10068. static int intel_atomic_check(struct drm_device *dev,
  10069. struct drm_atomic_state *state)
  10070. {
  10071. struct drm_i915_private *dev_priv = to_i915(dev);
  10072. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10073. struct drm_crtc *crtc;
  10074. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10075. int ret, i;
  10076. bool any_ms = false;
  10077. ret = drm_atomic_helper_check_modeset(dev, state);
  10078. if (ret)
  10079. return ret;
  10080. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10081. struct intel_crtc_state *pipe_config =
  10082. to_intel_crtc_state(crtc_state);
  10083. /* Catch I915_MODE_FLAG_INHERITED */
  10084. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10085. crtc_state->mode_changed = true;
  10086. if (!needs_modeset(crtc_state))
  10087. continue;
  10088. if (!crtc_state->enable) {
  10089. any_ms = true;
  10090. continue;
  10091. }
  10092. /* FIXME: For only active_changed we shouldn't need to do any
  10093. * state recomputation at all. */
  10094. ret = drm_atomic_add_affected_connectors(state, crtc);
  10095. if (ret)
  10096. return ret;
  10097. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10098. if (ret) {
  10099. intel_dump_pipe_config(to_intel_crtc(crtc),
  10100. pipe_config, "[failed]");
  10101. return ret;
  10102. }
  10103. if (i915.fastboot &&
  10104. intel_pipe_config_compare(dev_priv,
  10105. to_intel_crtc_state(old_crtc_state),
  10106. pipe_config, true)) {
  10107. crtc_state->mode_changed = false;
  10108. pipe_config->update_pipe = true;
  10109. }
  10110. if (needs_modeset(crtc_state))
  10111. any_ms = true;
  10112. ret = drm_atomic_add_affected_planes(state, crtc);
  10113. if (ret)
  10114. return ret;
  10115. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10116. needs_modeset(crtc_state) ?
  10117. "[modeset]" : "[fastset]");
  10118. }
  10119. if (any_ms) {
  10120. ret = intel_modeset_checks(state);
  10121. if (ret)
  10122. return ret;
  10123. } else {
  10124. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10125. }
  10126. ret = drm_atomic_helper_check_planes(dev, state);
  10127. if (ret)
  10128. return ret;
  10129. intel_fbc_choose_crtc(dev_priv, state);
  10130. return calc_watermark_data(state);
  10131. }
  10132. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10133. struct drm_atomic_state *state)
  10134. {
  10135. return drm_atomic_helper_prepare_planes(dev, state);
  10136. }
  10137. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10138. {
  10139. struct drm_device *dev = crtc->base.dev;
  10140. if (!dev->max_vblank_count)
  10141. return drm_crtc_accurate_vblank_count(&crtc->base);
  10142. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10143. }
  10144. static void intel_update_crtc(struct drm_crtc *crtc,
  10145. struct drm_atomic_state *state,
  10146. struct drm_crtc_state *old_crtc_state,
  10147. struct drm_crtc_state *new_crtc_state)
  10148. {
  10149. struct drm_device *dev = crtc->dev;
  10150. struct drm_i915_private *dev_priv = to_i915(dev);
  10151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10152. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10153. bool modeset = needs_modeset(new_crtc_state);
  10154. if (modeset) {
  10155. update_scanline_offset(intel_crtc);
  10156. dev_priv->display.crtc_enable(pipe_config, state);
  10157. } else {
  10158. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10159. pipe_config);
  10160. }
  10161. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10162. intel_fbc_enable(
  10163. intel_crtc, pipe_config,
  10164. to_intel_plane_state(crtc->primary->state));
  10165. }
  10166. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10167. }
  10168. static void intel_update_crtcs(struct drm_atomic_state *state)
  10169. {
  10170. struct drm_crtc *crtc;
  10171. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10172. int i;
  10173. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10174. if (!new_crtc_state->active)
  10175. continue;
  10176. intel_update_crtc(crtc, state, old_crtc_state,
  10177. new_crtc_state);
  10178. }
  10179. }
  10180. static void skl_update_crtcs(struct drm_atomic_state *state)
  10181. {
  10182. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10183. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10184. struct drm_crtc *crtc;
  10185. struct intel_crtc *intel_crtc;
  10186. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10187. struct intel_crtc_state *cstate;
  10188. unsigned int updated = 0;
  10189. bool progress;
  10190. enum pipe pipe;
  10191. int i;
  10192. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10193. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10194. /* ignore allocations for crtc's that have been turned off. */
  10195. if (new_crtc_state->active)
  10196. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10197. /*
  10198. * Whenever the number of active pipes changes, we need to make sure we
  10199. * update the pipes in the right order so that their ddb allocations
  10200. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10201. * cause pipe underruns and other bad stuff.
  10202. */
  10203. do {
  10204. progress = false;
  10205. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10206. bool vbl_wait = false;
  10207. unsigned int cmask = drm_crtc_mask(crtc);
  10208. intel_crtc = to_intel_crtc(crtc);
  10209. cstate = to_intel_crtc_state(new_crtc_state);
  10210. pipe = intel_crtc->pipe;
  10211. if (updated & cmask || !cstate->base.active)
  10212. continue;
  10213. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10214. continue;
  10215. updated |= cmask;
  10216. entries[i] = &cstate->wm.skl.ddb;
  10217. /*
  10218. * If this is an already active pipe, it's DDB changed,
  10219. * and this isn't the last pipe that needs updating
  10220. * then we need to wait for a vblank to pass for the
  10221. * new ddb allocation to take effect.
  10222. */
  10223. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10224. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10225. !new_crtc_state->active_changed &&
  10226. intel_state->wm_results.dirty_pipes != updated)
  10227. vbl_wait = true;
  10228. intel_update_crtc(crtc, state, old_crtc_state,
  10229. new_crtc_state);
  10230. if (vbl_wait)
  10231. intel_wait_for_vblank(dev_priv, pipe);
  10232. progress = true;
  10233. }
  10234. } while (progress);
  10235. }
  10236. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10237. {
  10238. struct intel_atomic_state *state, *next;
  10239. struct llist_node *freed;
  10240. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10241. llist_for_each_entry_safe(state, next, freed, freed)
  10242. drm_atomic_state_put(&state->base);
  10243. }
  10244. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10245. {
  10246. struct drm_i915_private *dev_priv =
  10247. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10248. intel_atomic_helper_free_state(dev_priv);
  10249. }
  10250. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10251. {
  10252. struct wait_queue_entry wait_fence, wait_reset;
  10253. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10254. init_wait_entry(&wait_fence, 0);
  10255. init_wait_entry(&wait_reset, 0);
  10256. for (;;) {
  10257. prepare_to_wait(&intel_state->commit_ready.wait,
  10258. &wait_fence, TASK_UNINTERRUPTIBLE);
  10259. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10260. &wait_reset, TASK_UNINTERRUPTIBLE);
  10261. if (i915_sw_fence_done(&intel_state->commit_ready)
  10262. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10263. break;
  10264. schedule();
  10265. }
  10266. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10267. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10268. }
  10269. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10270. {
  10271. struct drm_device *dev = state->dev;
  10272. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10273. struct drm_i915_private *dev_priv = to_i915(dev);
  10274. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10275. struct drm_crtc *crtc;
  10276. struct intel_crtc_state *intel_cstate;
  10277. bool hw_check = intel_state->modeset;
  10278. u64 put_domains[I915_MAX_PIPES] = {};
  10279. int i;
  10280. intel_atomic_commit_fence_wait(intel_state);
  10281. drm_atomic_helper_wait_for_dependencies(state);
  10282. if (intel_state->modeset)
  10283. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10284. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10286. if (needs_modeset(new_crtc_state) ||
  10287. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10288. hw_check = true;
  10289. put_domains[to_intel_crtc(crtc)->pipe] =
  10290. modeset_get_crtc_power_domains(crtc,
  10291. to_intel_crtc_state(new_crtc_state));
  10292. }
  10293. if (!needs_modeset(new_crtc_state))
  10294. continue;
  10295. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10296. to_intel_crtc_state(new_crtc_state));
  10297. if (old_crtc_state->active) {
  10298. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10299. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10300. intel_crtc->active = false;
  10301. intel_fbc_disable(intel_crtc);
  10302. intel_disable_shared_dpll(intel_crtc);
  10303. /*
  10304. * Underruns don't always raise
  10305. * interrupts, so check manually.
  10306. */
  10307. intel_check_cpu_fifo_underruns(dev_priv);
  10308. intel_check_pch_fifo_underruns(dev_priv);
  10309. if (!new_crtc_state->active) {
  10310. /*
  10311. * Make sure we don't call initial_watermarks
  10312. * for ILK-style watermark updates.
  10313. *
  10314. * No clue what this is supposed to achieve.
  10315. */
  10316. if (INTEL_GEN(dev_priv) >= 9)
  10317. dev_priv->display.initial_watermarks(intel_state,
  10318. to_intel_crtc_state(new_crtc_state));
  10319. }
  10320. }
  10321. }
  10322. /* Only after disabling all output pipelines that will be changed can we
  10323. * update the the output configuration. */
  10324. intel_modeset_update_crtc_state(state);
  10325. if (intel_state->modeset) {
  10326. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10327. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10328. /*
  10329. * SKL workaround: bspec recommends we disable the SAGV when we
  10330. * have more then one pipe enabled
  10331. */
  10332. if (!intel_can_enable_sagv(state))
  10333. intel_disable_sagv(dev_priv);
  10334. intel_modeset_verify_disabled(dev, state);
  10335. }
  10336. /* Complete the events for pipes that have now been disabled */
  10337. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10338. bool modeset = needs_modeset(new_crtc_state);
  10339. /* Complete events for now disable pipes here. */
  10340. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10341. spin_lock_irq(&dev->event_lock);
  10342. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10343. spin_unlock_irq(&dev->event_lock);
  10344. new_crtc_state->event = NULL;
  10345. }
  10346. }
  10347. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10348. dev_priv->display.update_crtcs(state);
  10349. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10350. * already, but still need the state for the delayed optimization. To
  10351. * fix this:
  10352. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10353. * - schedule that vblank worker _before_ calling hw_done
  10354. * - at the start of commit_tail, cancel it _synchrously
  10355. * - switch over to the vblank wait helper in the core after that since
  10356. * we don't need out special handling any more.
  10357. */
  10358. drm_atomic_helper_wait_for_flip_done(dev, state);
  10359. /*
  10360. * Now that the vblank has passed, we can go ahead and program the
  10361. * optimal watermarks on platforms that need two-step watermark
  10362. * programming.
  10363. *
  10364. * TODO: Move this (and other cleanup) to an async worker eventually.
  10365. */
  10366. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10367. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10368. if (dev_priv->display.optimize_watermarks)
  10369. dev_priv->display.optimize_watermarks(intel_state,
  10370. intel_cstate);
  10371. }
  10372. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10373. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10374. if (put_domains[i])
  10375. modeset_put_power_domains(dev_priv, put_domains[i]);
  10376. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10377. }
  10378. if (intel_state->modeset && intel_can_enable_sagv(state))
  10379. intel_enable_sagv(dev_priv);
  10380. drm_atomic_helper_commit_hw_done(state);
  10381. if (intel_state->modeset) {
  10382. /* As one of the primary mmio accessors, KMS has a high
  10383. * likelihood of triggering bugs in unclaimed access. After we
  10384. * finish modesetting, see if an error has been flagged, and if
  10385. * so enable debugging for the next modeset - and hope we catch
  10386. * the culprit.
  10387. */
  10388. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10389. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10390. }
  10391. drm_atomic_helper_cleanup_planes(dev, state);
  10392. drm_atomic_helper_commit_cleanup_done(state);
  10393. drm_atomic_state_put(state);
  10394. intel_atomic_helper_free_state(dev_priv);
  10395. }
  10396. static void intel_atomic_commit_work(struct work_struct *work)
  10397. {
  10398. struct drm_atomic_state *state =
  10399. container_of(work, struct drm_atomic_state, commit_work);
  10400. intel_atomic_commit_tail(state);
  10401. }
  10402. static int __i915_sw_fence_call
  10403. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10404. enum i915_sw_fence_notify notify)
  10405. {
  10406. struct intel_atomic_state *state =
  10407. container_of(fence, struct intel_atomic_state, commit_ready);
  10408. switch (notify) {
  10409. case FENCE_COMPLETE:
  10410. /* we do blocking waits in the worker, nothing to do here */
  10411. break;
  10412. case FENCE_FREE:
  10413. {
  10414. struct intel_atomic_helper *helper =
  10415. &to_i915(state->base.dev)->atomic_helper;
  10416. if (llist_add(&state->freed, &helper->free_list))
  10417. schedule_work(&helper->free_work);
  10418. break;
  10419. }
  10420. }
  10421. return NOTIFY_DONE;
  10422. }
  10423. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10424. {
  10425. struct drm_plane_state *old_plane_state, *new_plane_state;
  10426. struct drm_plane *plane;
  10427. int i;
  10428. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10429. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10430. intel_fb_obj(new_plane_state->fb),
  10431. to_intel_plane(plane)->frontbuffer_bit);
  10432. }
  10433. /**
  10434. * intel_atomic_commit - commit validated state object
  10435. * @dev: DRM device
  10436. * @state: the top-level driver state object
  10437. * @nonblock: nonblocking commit
  10438. *
  10439. * This function commits a top-level state object that has been validated
  10440. * with drm_atomic_helper_check().
  10441. *
  10442. * RETURNS
  10443. * Zero for success or -errno.
  10444. */
  10445. static int intel_atomic_commit(struct drm_device *dev,
  10446. struct drm_atomic_state *state,
  10447. bool nonblock)
  10448. {
  10449. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10450. struct drm_i915_private *dev_priv = to_i915(dev);
  10451. int ret = 0;
  10452. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10453. if (ret)
  10454. return ret;
  10455. drm_atomic_state_get(state);
  10456. i915_sw_fence_init(&intel_state->commit_ready,
  10457. intel_atomic_commit_ready);
  10458. ret = intel_atomic_prepare_commit(dev, state);
  10459. if (ret) {
  10460. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10461. i915_sw_fence_commit(&intel_state->commit_ready);
  10462. return ret;
  10463. }
  10464. /*
  10465. * The intel_legacy_cursor_update() fast path takes care
  10466. * of avoiding the vblank waits for simple cursor
  10467. * movement and flips. For cursor on/off and size changes,
  10468. * we want to perform the vblank waits so that watermark
  10469. * updates happen during the correct frames. Gen9+ have
  10470. * double buffered watermarks and so shouldn't need this.
  10471. *
  10472. * Do this after drm_atomic_helper_setup_commit() and
  10473. * intel_atomic_prepare_commit() because we still want
  10474. * to skip the flip and fb cleanup waits. Although that
  10475. * does risk yanking the mapping from under the display
  10476. * engine.
  10477. *
  10478. * FIXME doing watermarks and fb cleanup from a vblank worker
  10479. * (assuming we had any) would solve these problems.
  10480. */
  10481. if (INTEL_GEN(dev_priv) < 9)
  10482. state->legacy_cursor_update = false;
  10483. ret = drm_atomic_helper_swap_state(state, true);
  10484. if (ret) {
  10485. i915_sw_fence_commit(&intel_state->commit_ready);
  10486. drm_atomic_helper_cleanup_planes(dev, state);
  10487. return ret;
  10488. }
  10489. dev_priv->wm.distrust_bios_wm = false;
  10490. intel_shared_dpll_swap_state(state);
  10491. intel_atomic_track_fbs(state);
  10492. if (intel_state->modeset) {
  10493. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10494. sizeof(intel_state->min_cdclk));
  10495. dev_priv->active_crtcs = intel_state->active_crtcs;
  10496. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10497. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10498. }
  10499. drm_atomic_state_get(state);
  10500. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10501. i915_sw_fence_commit(&intel_state->commit_ready);
  10502. if (nonblock)
  10503. queue_work(system_unbound_wq, &state->commit_work);
  10504. else
  10505. intel_atomic_commit_tail(state);
  10506. return 0;
  10507. }
  10508. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10509. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10510. .set_config = drm_atomic_helper_set_config,
  10511. .destroy = intel_crtc_destroy,
  10512. .page_flip = drm_atomic_helper_page_flip,
  10513. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10514. .atomic_destroy_state = intel_crtc_destroy_state,
  10515. .set_crc_source = intel_crtc_set_crc_source,
  10516. };
  10517. struct wait_rps_boost {
  10518. struct wait_queue_entry wait;
  10519. struct drm_crtc *crtc;
  10520. struct drm_i915_gem_request *request;
  10521. };
  10522. static int do_rps_boost(struct wait_queue_entry *_wait,
  10523. unsigned mode, int sync, void *key)
  10524. {
  10525. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10526. struct drm_i915_gem_request *rq = wait->request;
  10527. gen6_rps_boost(rq, NULL);
  10528. i915_gem_request_put(rq);
  10529. drm_crtc_vblank_put(wait->crtc);
  10530. list_del(&wait->wait.entry);
  10531. kfree(wait);
  10532. return 1;
  10533. }
  10534. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10535. struct dma_fence *fence)
  10536. {
  10537. struct wait_rps_boost *wait;
  10538. if (!dma_fence_is_i915(fence))
  10539. return;
  10540. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10541. return;
  10542. if (drm_crtc_vblank_get(crtc))
  10543. return;
  10544. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10545. if (!wait) {
  10546. drm_crtc_vblank_put(crtc);
  10547. return;
  10548. }
  10549. wait->request = to_request(dma_fence_get(fence));
  10550. wait->crtc = crtc;
  10551. wait->wait.func = do_rps_boost;
  10552. wait->wait.flags = 0;
  10553. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10554. }
  10555. /**
  10556. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10557. * @plane: drm plane to prepare for
  10558. * @fb: framebuffer to prepare for presentation
  10559. *
  10560. * Prepares a framebuffer for usage on a display plane. Generally this
  10561. * involves pinning the underlying object and updating the frontbuffer tracking
  10562. * bits. Some older platforms need special physical address handling for
  10563. * cursor planes.
  10564. *
  10565. * Must be called with struct_mutex held.
  10566. *
  10567. * Returns 0 on success, negative error code on failure.
  10568. */
  10569. int
  10570. intel_prepare_plane_fb(struct drm_plane *plane,
  10571. struct drm_plane_state *new_state)
  10572. {
  10573. struct intel_atomic_state *intel_state =
  10574. to_intel_atomic_state(new_state->state);
  10575. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10576. struct drm_framebuffer *fb = new_state->fb;
  10577. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10578. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10579. int ret;
  10580. if (old_obj) {
  10581. struct drm_crtc_state *crtc_state =
  10582. drm_atomic_get_existing_crtc_state(new_state->state,
  10583. plane->state->crtc);
  10584. /* Big Hammer, we also need to ensure that any pending
  10585. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10586. * current scanout is retired before unpinning the old
  10587. * framebuffer. Note that we rely on userspace rendering
  10588. * into the buffer attached to the pipe they are waiting
  10589. * on. If not, userspace generates a GPU hang with IPEHR
  10590. * point to the MI_WAIT_FOR_EVENT.
  10591. *
  10592. * This should only fail upon a hung GPU, in which case we
  10593. * can safely continue.
  10594. */
  10595. if (needs_modeset(crtc_state)) {
  10596. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10597. old_obj->resv, NULL,
  10598. false, 0,
  10599. GFP_KERNEL);
  10600. if (ret < 0)
  10601. return ret;
  10602. }
  10603. }
  10604. if (new_state->fence) { /* explicit fencing */
  10605. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10606. new_state->fence,
  10607. I915_FENCE_TIMEOUT,
  10608. GFP_KERNEL);
  10609. if (ret < 0)
  10610. return ret;
  10611. }
  10612. if (!obj)
  10613. return 0;
  10614. ret = i915_gem_object_pin_pages(obj);
  10615. if (ret)
  10616. return ret;
  10617. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10618. if (ret) {
  10619. i915_gem_object_unpin_pages(obj);
  10620. return ret;
  10621. }
  10622. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10623. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10624. const int align = intel_cursor_alignment(dev_priv);
  10625. ret = i915_gem_object_attach_phys(obj, align);
  10626. } else {
  10627. struct i915_vma *vma;
  10628. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10629. if (!IS_ERR(vma))
  10630. to_intel_plane_state(new_state)->vma = vma;
  10631. else
  10632. ret = PTR_ERR(vma);
  10633. }
  10634. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10635. mutex_unlock(&dev_priv->drm.struct_mutex);
  10636. i915_gem_object_unpin_pages(obj);
  10637. if (ret)
  10638. return ret;
  10639. if (!new_state->fence) { /* implicit fencing */
  10640. struct dma_fence *fence;
  10641. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10642. obj->resv, NULL,
  10643. false, I915_FENCE_TIMEOUT,
  10644. GFP_KERNEL);
  10645. if (ret < 0)
  10646. return ret;
  10647. fence = reservation_object_get_excl_rcu(obj->resv);
  10648. if (fence) {
  10649. add_rps_boost_after_vblank(new_state->crtc, fence);
  10650. dma_fence_put(fence);
  10651. }
  10652. } else {
  10653. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10654. }
  10655. return 0;
  10656. }
  10657. /**
  10658. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10659. * @plane: drm plane to clean up for
  10660. * @fb: old framebuffer that was on plane
  10661. *
  10662. * Cleans up a framebuffer that has just been removed from a plane.
  10663. *
  10664. * Must be called with struct_mutex held.
  10665. */
  10666. void
  10667. intel_cleanup_plane_fb(struct drm_plane *plane,
  10668. struct drm_plane_state *old_state)
  10669. {
  10670. struct i915_vma *vma;
  10671. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10672. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10673. if (vma) {
  10674. mutex_lock(&plane->dev->struct_mutex);
  10675. intel_unpin_fb_vma(vma);
  10676. mutex_unlock(&plane->dev->struct_mutex);
  10677. }
  10678. }
  10679. int
  10680. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10681. {
  10682. struct drm_i915_private *dev_priv;
  10683. int max_scale;
  10684. int crtc_clock, max_dotclk;
  10685. if (!intel_crtc || !crtc_state->base.enable)
  10686. return DRM_PLANE_HELPER_NO_SCALING;
  10687. dev_priv = to_i915(intel_crtc->base.dev);
  10688. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10689. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10690. if (IS_GEMINILAKE(dev_priv))
  10691. max_dotclk *= 2;
  10692. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10693. return DRM_PLANE_HELPER_NO_SCALING;
  10694. /*
  10695. * skl max scale is lower of:
  10696. * close to 3 but not 3, -1 is for that purpose
  10697. * or
  10698. * cdclk/crtc_clock
  10699. */
  10700. max_scale = min((1 << 16) * 3 - 1,
  10701. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10702. return max_scale;
  10703. }
  10704. static int
  10705. intel_check_primary_plane(struct intel_plane *plane,
  10706. struct intel_crtc_state *crtc_state,
  10707. struct intel_plane_state *state)
  10708. {
  10709. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10710. struct drm_crtc *crtc = state->base.crtc;
  10711. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10712. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10713. bool can_position = false;
  10714. int ret;
  10715. if (INTEL_GEN(dev_priv) >= 9) {
  10716. /* use scaler when colorkey is not required */
  10717. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10718. min_scale = 1;
  10719. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10720. }
  10721. can_position = true;
  10722. }
  10723. ret = drm_plane_helper_check_state(&state->base,
  10724. &state->clip,
  10725. min_scale, max_scale,
  10726. can_position, true);
  10727. if (ret)
  10728. return ret;
  10729. if (!state->base.fb)
  10730. return 0;
  10731. if (INTEL_GEN(dev_priv) >= 9) {
  10732. ret = skl_check_plane_surface(state);
  10733. if (ret)
  10734. return ret;
  10735. state->ctl = skl_plane_ctl(crtc_state, state);
  10736. } else {
  10737. ret = i9xx_check_plane_surface(state);
  10738. if (ret)
  10739. return ret;
  10740. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10741. }
  10742. return 0;
  10743. }
  10744. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10745. struct drm_crtc_state *old_crtc_state)
  10746. {
  10747. struct drm_device *dev = crtc->dev;
  10748. struct drm_i915_private *dev_priv = to_i915(dev);
  10749. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10750. struct intel_crtc_state *old_intel_cstate =
  10751. to_intel_crtc_state(old_crtc_state);
  10752. struct intel_atomic_state *old_intel_state =
  10753. to_intel_atomic_state(old_crtc_state->state);
  10754. struct intel_crtc_state *intel_cstate =
  10755. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10756. bool modeset = needs_modeset(&intel_cstate->base);
  10757. if (!modeset &&
  10758. (intel_cstate->base.color_mgmt_changed ||
  10759. intel_cstate->update_pipe)) {
  10760. intel_color_set_csc(&intel_cstate->base);
  10761. intel_color_load_luts(&intel_cstate->base);
  10762. }
  10763. /* Perform vblank evasion around commit operation */
  10764. intel_pipe_update_start(intel_cstate);
  10765. if (modeset)
  10766. goto out;
  10767. if (intel_cstate->update_pipe)
  10768. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10769. else if (INTEL_GEN(dev_priv) >= 9)
  10770. skl_detach_scalers(intel_crtc);
  10771. out:
  10772. if (dev_priv->display.atomic_update_watermarks)
  10773. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10774. intel_cstate);
  10775. }
  10776. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10777. struct drm_crtc_state *old_crtc_state)
  10778. {
  10779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10780. struct intel_atomic_state *old_intel_state =
  10781. to_intel_atomic_state(old_crtc_state->state);
  10782. struct intel_crtc_state *new_crtc_state =
  10783. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10784. intel_pipe_update_end(new_crtc_state);
  10785. }
  10786. /**
  10787. * intel_plane_destroy - destroy a plane
  10788. * @plane: plane to destroy
  10789. *
  10790. * Common destruction function for all types of planes (primary, cursor,
  10791. * sprite).
  10792. */
  10793. void intel_plane_destroy(struct drm_plane *plane)
  10794. {
  10795. drm_plane_cleanup(plane);
  10796. kfree(to_intel_plane(plane));
  10797. }
  10798. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10799. {
  10800. switch (format) {
  10801. case DRM_FORMAT_C8:
  10802. case DRM_FORMAT_RGB565:
  10803. case DRM_FORMAT_XRGB1555:
  10804. case DRM_FORMAT_XRGB8888:
  10805. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10806. modifier == I915_FORMAT_MOD_X_TILED;
  10807. default:
  10808. return false;
  10809. }
  10810. }
  10811. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10812. {
  10813. switch (format) {
  10814. case DRM_FORMAT_C8:
  10815. case DRM_FORMAT_RGB565:
  10816. case DRM_FORMAT_XRGB8888:
  10817. case DRM_FORMAT_XBGR8888:
  10818. case DRM_FORMAT_XRGB2101010:
  10819. case DRM_FORMAT_XBGR2101010:
  10820. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10821. modifier == I915_FORMAT_MOD_X_TILED;
  10822. default:
  10823. return false;
  10824. }
  10825. }
  10826. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10827. {
  10828. switch (format) {
  10829. case DRM_FORMAT_XRGB8888:
  10830. case DRM_FORMAT_XBGR8888:
  10831. case DRM_FORMAT_ARGB8888:
  10832. case DRM_FORMAT_ABGR8888:
  10833. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10834. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10835. return true;
  10836. /* fall through */
  10837. case DRM_FORMAT_RGB565:
  10838. case DRM_FORMAT_XRGB2101010:
  10839. case DRM_FORMAT_XBGR2101010:
  10840. case DRM_FORMAT_YUYV:
  10841. case DRM_FORMAT_YVYU:
  10842. case DRM_FORMAT_UYVY:
  10843. case DRM_FORMAT_VYUY:
  10844. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10845. return true;
  10846. /* fall through */
  10847. case DRM_FORMAT_C8:
  10848. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10849. modifier == I915_FORMAT_MOD_X_TILED ||
  10850. modifier == I915_FORMAT_MOD_Y_TILED)
  10851. return true;
  10852. /* fall through */
  10853. default:
  10854. return false;
  10855. }
  10856. }
  10857. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10858. uint32_t format,
  10859. uint64_t modifier)
  10860. {
  10861. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10862. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10863. return false;
  10864. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10865. modifier != DRM_FORMAT_MOD_LINEAR)
  10866. return false;
  10867. if (INTEL_GEN(dev_priv) >= 9)
  10868. return skl_mod_supported(format, modifier);
  10869. else if (INTEL_GEN(dev_priv) >= 4)
  10870. return i965_mod_supported(format, modifier);
  10871. else
  10872. return i8xx_mod_supported(format, modifier);
  10873. unreachable();
  10874. }
  10875. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10876. uint32_t format,
  10877. uint64_t modifier)
  10878. {
  10879. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10880. return false;
  10881. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10882. }
  10883. static struct drm_plane_funcs intel_plane_funcs = {
  10884. .update_plane = drm_atomic_helper_update_plane,
  10885. .disable_plane = drm_atomic_helper_disable_plane,
  10886. .destroy = intel_plane_destroy,
  10887. .atomic_get_property = intel_plane_atomic_get_property,
  10888. .atomic_set_property = intel_plane_atomic_set_property,
  10889. .atomic_duplicate_state = intel_plane_duplicate_state,
  10890. .atomic_destroy_state = intel_plane_destroy_state,
  10891. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10892. };
  10893. static int
  10894. intel_legacy_cursor_update(struct drm_plane *plane,
  10895. struct drm_crtc *crtc,
  10896. struct drm_framebuffer *fb,
  10897. int crtc_x, int crtc_y,
  10898. unsigned int crtc_w, unsigned int crtc_h,
  10899. uint32_t src_x, uint32_t src_y,
  10900. uint32_t src_w, uint32_t src_h,
  10901. struct drm_modeset_acquire_ctx *ctx)
  10902. {
  10903. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10904. int ret;
  10905. struct drm_plane_state *old_plane_state, *new_plane_state;
  10906. struct intel_plane *intel_plane = to_intel_plane(plane);
  10907. struct drm_framebuffer *old_fb;
  10908. struct drm_crtc_state *crtc_state = crtc->state;
  10909. struct i915_vma *old_vma, *vma;
  10910. /*
  10911. * When crtc is inactive or there is a modeset pending,
  10912. * wait for it to complete in the slowpath
  10913. */
  10914. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10915. to_intel_crtc_state(crtc_state)->update_pipe)
  10916. goto slow;
  10917. old_plane_state = plane->state;
  10918. /*
  10919. * Don't do an async update if there is an outstanding commit modifying
  10920. * the plane. This prevents our async update's changes from getting
  10921. * overridden by a previous synchronous update's state.
  10922. */
  10923. if (old_plane_state->commit &&
  10924. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  10925. goto slow;
  10926. /*
  10927. * If any parameters change that may affect watermarks,
  10928. * take the slowpath. Only changing fb or position should be
  10929. * in the fastpath.
  10930. */
  10931. if (old_plane_state->crtc != crtc ||
  10932. old_plane_state->src_w != src_w ||
  10933. old_plane_state->src_h != src_h ||
  10934. old_plane_state->crtc_w != crtc_w ||
  10935. old_plane_state->crtc_h != crtc_h ||
  10936. !old_plane_state->fb != !fb)
  10937. goto slow;
  10938. new_plane_state = intel_plane_duplicate_state(plane);
  10939. if (!new_plane_state)
  10940. return -ENOMEM;
  10941. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10942. new_plane_state->src_x = src_x;
  10943. new_plane_state->src_y = src_y;
  10944. new_plane_state->src_w = src_w;
  10945. new_plane_state->src_h = src_h;
  10946. new_plane_state->crtc_x = crtc_x;
  10947. new_plane_state->crtc_y = crtc_y;
  10948. new_plane_state->crtc_w = crtc_w;
  10949. new_plane_state->crtc_h = crtc_h;
  10950. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10951. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  10952. to_intel_plane_state(plane->state),
  10953. to_intel_plane_state(new_plane_state));
  10954. if (ret)
  10955. goto out_free;
  10956. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10957. if (ret)
  10958. goto out_free;
  10959. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10960. int align = intel_cursor_alignment(dev_priv);
  10961. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10962. if (ret) {
  10963. DRM_DEBUG_KMS("failed to attach phys object\n");
  10964. goto out_unlock;
  10965. }
  10966. } else {
  10967. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10968. if (IS_ERR(vma)) {
  10969. DRM_DEBUG_KMS("failed to pin object\n");
  10970. ret = PTR_ERR(vma);
  10971. goto out_unlock;
  10972. }
  10973. to_intel_plane_state(new_plane_state)->vma = vma;
  10974. }
  10975. old_fb = old_plane_state->fb;
  10976. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10977. intel_plane->frontbuffer_bit);
  10978. /* Swap plane state */
  10979. plane->state = new_plane_state;
  10980. if (plane->state->visible) {
  10981. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10982. intel_plane->update_plane(intel_plane,
  10983. to_intel_crtc_state(crtc->state),
  10984. to_intel_plane_state(plane->state));
  10985. } else {
  10986. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10987. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10988. }
  10989. old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
  10990. if (old_vma)
  10991. intel_unpin_fb_vma(old_vma);
  10992. out_unlock:
  10993. mutex_unlock(&dev_priv->drm.struct_mutex);
  10994. out_free:
  10995. if (ret)
  10996. intel_plane_destroy_state(plane, new_plane_state);
  10997. else
  10998. intel_plane_destroy_state(plane, old_plane_state);
  10999. return ret;
  11000. slow:
  11001. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11002. crtc_x, crtc_y, crtc_w, crtc_h,
  11003. src_x, src_y, src_w, src_h, ctx);
  11004. }
  11005. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11006. .update_plane = intel_legacy_cursor_update,
  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_cursor_plane_format_mod_supported,
  11014. };
  11015. static struct intel_plane *
  11016. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11017. {
  11018. struct intel_plane *primary = NULL;
  11019. struct intel_plane_state *state = NULL;
  11020. const uint32_t *intel_primary_formats;
  11021. unsigned int supported_rotations;
  11022. unsigned int num_formats;
  11023. const uint64_t *modifiers;
  11024. int ret;
  11025. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11026. if (!primary) {
  11027. ret = -ENOMEM;
  11028. goto fail;
  11029. }
  11030. state = intel_create_plane_state(&primary->base);
  11031. if (!state) {
  11032. ret = -ENOMEM;
  11033. goto fail;
  11034. }
  11035. primary->base.state = &state->base;
  11036. primary->can_scale = false;
  11037. primary->max_downscale = 1;
  11038. if (INTEL_GEN(dev_priv) >= 9) {
  11039. primary->can_scale = true;
  11040. state->scaler_id = -1;
  11041. }
  11042. primary->pipe = pipe;
  11043. /*
  11044. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11045. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11046. */
  11047. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11048. primary->plane = (enum plane) !pipe;
  11049. else
  11050. primary->plane = (enum plane) pipe;
  11051. primary->id = PLANE_PRIMARY;
  11052. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11053. primary->check_plane = intel_check_primary_plane;
  11054. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  11055. intel_primary_formats = skl_primary_formats;
  11056. num_formats = ARRAY_SIZE(skl_primary_formats);
  11057. modifiers = skl_format_modifiers_ccs;
  11058. primary->update_plane = skylake_update_primary_plane;
  11059. primary->disable_plane = skylake_disable_primary_plane;
  11060. } else if (INTEL_GEN(dev_priv) >= 9) {
  11061. intel_primary_formats = skl_primary_formats;
  11062. num_formats = ARRAY_SIZE(skl_primary_formats);
  11063. if (pipe < PIPE_C)
  11064. modifiers = skl_format_modifiers_ccs;
  11065. else
  11066. modifiers = skl_format_modifiers_noccs;
  11067. primary->update_plane = skylake_update_primary_plane;
  11068. primary->disable_plane = skylake_disable_primary_plane;
  11069. } else if (INTEL_GEN(dev_priv) >= 4) {
  11070. intel_primary_formats = i965_primary_formats;
  11071. num_formats = ARRAY_SIZE(i965_primary_formats);
  11072. modifiers = i9xx_format_modifiers;
  11073. primary->update_plane = i9xx_update_primary_plane;
  11074. primary->disable_plane = i9xx_disable_primary_plane;
  11075. } else {
  11076. intel_primary_formats = i8xx_primary_formats;
  11077. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11078. modifiers = i9xx_format_modifiers;
  11079. primary->update_plane = i9xx_update_primary_plane;
  11080. primary->disable_plane = i9xx_disable_primary_plane;
  11081. }
  11082. if (INTEL_GEN(dev_priv) >= 9)
  11083. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11084. 0, &intel_plane_funcs,
  11085. intel_primary_formats, num_formats,
  11086. modifiers,
  11087. DRM_PLANE_TYPE_PRIMARY,
  11088. "plane 1%c", pipe_name(pipe));
  11089. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11090. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11091. 0, &intel_plane_funcs,
  11092. intel_primary_formats, num_formats,
  11093. modifiers,
  11094. DRM_PLANE_TYPE_PRIMARY,
  11095. "primary %c", pipe_name(pipe));
  11096. else
  11097. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11098. 0, &intel_plane_funcs,
  11099. intel_primary_formats, num_formats,
  11100. modifiers,
  11101. DRM_PLANE_TYPE_PRIMARY,
  11102. "plane %c", plane_name(primary->plane));
  11103. if (ret)
  11104. goto fail;
  11105. if (INTEL_GEN(dev_priv) >= 9) {
  11106. supported_rotations =
  11107. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11108. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11109. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11110. supported_rotations =
  11111. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11112. DRM_MODE_REFLECT_X;
  11113. } else if (INTEL_GEN(dev_priv) >= 4) {
  11114. supported_rotations =
  11115. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11116. } else {
  11117. supported_rotations = DRM_MODE_ROTATE_0;
  11118. }
  11119. if (INTEL_GEN(dev_priv) >= 4)
  11120. drm_plane_create_rotation_property(&primary->base,
  11121. DRM_MODE_ROTATE_0,
  11122. supported_rotations);
  11123. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11124. return primary;
  11125. fail:
  11126. kfree(state);
  11127. kfree(primary);
  11128. return ERR_PTR(ret);
  11129. }
  11130. static struct intel_plane *
  11131. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11132. enum pipe pipe)
  11133. {
  11134. struct intel_plane *cursor = NULL;
  11135. struct intel_plane_state *state = NULL;
  11136. int ret;
  11137. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11138. if (!cursor) {
  11139. ret = -ENOMEM;
  11140. goto fail;
  11141. }
  11142. state = intel_create_plane_state(&cursor->base);
  11143. if (!state) {
  11144. ret = -ENOMEM;
  11145. goto fail;
  11146. }
  11147. cursor->base.state = &state->base;
  11148. cursor->can_scale = false;
  11149. cursor->max_downscale = 1;
  11150. cursor->pipe = pipe;
  11151. cursor->plane = pipe;
  11152. cursor->id = PLANE_CURSOR;
  11153. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11154. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11155. cursor->update_plane = i845_update_cursor;
  11156. cursor->disable_plane = i845_disable_cursor;
  11157. cursor->check_plane = i845_check_cursor;
  11158. } else {
  11159. cursor->update_plane = i9xx_update_cursor;
  11160. cursor->disable_plane = i9xx_disable_cursor;
  11161. cursor->check_plane = i9xx_check_cursor;
  11162. }
  11163. cursor->cursor.base = ~0;
  11164. cursor->cursor.cntl = ~0;
  11165. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11166. cursor->cursor.size = ~0;
  11167. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11168. 0, &intel_cursor_plane_funcs,
  11169. intel_cursor_formats,
  11170. ARRAY_SIZE(intel_cursor_formats),
  11171. cursor_format_modifiers,
  11172. DRM_PLANE_TYPE_CURSOR,
  11173. "cursor %c", pipe_name(pipe));
  11174. if (ret)
  11175. goto fail;
  11176. if (INTEL_GEN(dev_priv) >= 4)
  11177. drm_plane_create_rotation_property(&cursor->base,
  11178. DRM_MODE_ROTATE_0,
  11179. DRM_MODE_ROTATE_0 |
  11180. DRM_MODE_ROTATE_180);
  11181. if (INTEL_GEN(dev_priv) >= 9)
  11182. state->scaler_id = -1;
  11183. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11184. return cursor;
  11185. fail:
  11186. kfree(state);
  11187. kfree(cursor);
  11188. return ERR_PTR(ret);
  11189. }
  11190. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11191. struct intel_crtc_state *crtc_state)
  11192. {
  11193. struct intel_crtc_scaler_state *scaler_state =
  11194. &crtc_state->scaler_state;
  11195. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11196. int i;
  11197. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11198. if (!crtc->num_scalers)
  11199. return;
  11200. for (i = 0; i < crtc->num_scalers; i++) {
  11201. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11202. scaler->in_use = 0;
  11203. scaler->mode = PS_SCALER_MODE_DYN;
  11204. }
  11205. scaler_state->scaler_id = -1;
  11206. }
  11207. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11208. {
  11209. struct intel_crtc *intel_crtc;
  11210. struct intel_crtc_state *crtc_state = NULL;
  11211. struct intel_plane *primary = NULL;
  11212. struct intel_plane *cursor = NULL;
  11213. int sprite, ret;
  11214. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11215. if (!intel_crtc)
  11216. return -ENOMEM;
  11217. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11218. if (!crtc_state) {
  11219. ret = -ENOMEM;
  11220. goto fail;
  11221. }
  11222. intel_crtc->config = crtc_state;
  11223. intel_crtc->base.state = &crtc_state->base;
  11224. crtc_state->base.crtc = &intel_crtc->base;
  11225. primary = intel_primary_plane_create(dev_priv, pipe);
  11226. if (IS_ERR(primary)) {
  11227. ret = PTR_ERR(primary);
  11228. goto fail;
  11229. }
  11230. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11231. for_each_sprite(dev_priv, pipe, sprite) {
  11232. struct intel_plane *plane;
  11233. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11234. if (IS_ERR(plane)) {
  11235. ret = PTR_ERR(plane);
  11236. goto fail;
  11237. }
  11238. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11239. }
  11240. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11241. if (IS_ERR(cursor)) {
  11242. ret = PTR_ERR(cursor);
  11243. goto fail;
  11244. }
  11245. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11246. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11247. &primary->base, &cursor->base,
  11248. &intel_crtc_funcs,
  11249. "pipe %c", pipe_name(pipe));
  11250. if (ret)
  11251. goto fail;
  11252. intel_crtc->pipe = pipe;
  11253. intel_crtc->plane = primary->plane;
  11254. /* initialize shared scalers */
  11255. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11256. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11257. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11258. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11259. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11260. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11261. intel_color_init(&intel_crtc->base);
  11262. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11263. return 0;
  11264. fail:
  11265. /*
  11266. * drm_mode_config_cleanup() will free up any
  11267. * crtcs/planes already initialized.
  11268. */
  11269. kfree(crtc_state);
  11270. kfree(intel_crtc);
  11271. return ret;
  11272. }
  11273. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11274. {
  11275. struct drm_device *dev = connector->base.dev;
  11276. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11277. if (!connector->base.state->crtc)
  11278. return INVALID_PIPE;
  11279. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11280. }
  11281. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11282. struct drm_file *file)
  11283. {
  11284. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11285. struct drm_crtc *drmmode_crtc;
  11286. struct intel_crtc *crtc;
  11287. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11288. if (!drmmode_crtc)
  11289. return -ENOENT;
  11290. crtc = to_intel_crtc(drmmode_crtc);
  11291. pipe_from_crtc_id->pipe = crtc->pipe;
  11292. return 0;
  11293. }
  11294. static int intel_encoder_clones(struct intel_encoder *encoder)
  11295. {
  11296. struct drm_device *dev = encoder->base.dev;
  11297. struct intel_encoder *source_encoder;
  11298. int index_mask = 0;
  11299. int entry = 0;
  11300. for_each_intel_encoder(dev, source_encoder) {
  11301. if (encoders_cloneable(encoder, source_encoder))
  11302. index_mask |= (1 << entry);
  11303. entry++;
  11304. }
  11305. return index_mask;
  11306. }
  11307. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11308. {
  11309. if (!IS_MOBILE(dev_priv))
  11310. return false;
  11311. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11312. return false;
  11313. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11314. return false;
  11315. return true;
  11316. }
  11317. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11318. {
  11319. if (INTEL_GEN(dev_priv) >= 9)
  11320. return false;
  11321. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11322. return false;
  11323. if (IS_CHERRYVIEW(dev_priv))
  11324. return false;
  11325. if (HAS_PCH_LPT_H(dev_priv) &&
  11326. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11327. return false;
  11328. /* DDI E can't be used if DDI A requires 4 lanes */
  11329. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11330. return false;
  11331. if (!dev_priv->vbt.int_crt_support)
  11332. return false;
  11333. return true;
  11334. }
  11335. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11336. {
  11337. int pps_num;
  11338. int pps_idx;
  11339. if (HAS_DDI(dev_priv))
  11340. return;
  11341. /*
  11342. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11343. * everywhere where registers can be write protected.
  11344. */
  11345. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11346. pps_num = 2;
  11347. else
  11348. pps_num = 1;
  11349. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11350. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11351. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11352. I915_WRITE(PP_CONTROL(pps_idx), val);
  11353. }
  11354. }
  11355. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11356. {
  11357. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11358. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11359. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11360. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11361. else
  11362. dev_priv->pps_mmio_base = PPS_BASE;
  11363. intel_pps_unlock_regs_wa(dev_priv);
  11364. }
  11365. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11366. {
  11367. struct intel_encoder *encoder;
  11368. bool dpd_is_edp = false;
  11369. intel_pps_init(dev_priv);
  11370. /*
  11371. * intel_edp_init_connector() depends on this completing first, to
  11372. * prevent the registeration of both eDP and LVDS and the incorrect
  11373. * sharing of the PPS.
  11374. */
  11375. intel_lvds_init(dev_priv);
  11376. if (intel_crt_present(dev_priv))
  11377. intel_crt_init(dev_priv);
  11378. if (IS_GEN9_LP(dev_priv)) {
  11379. /*
  11380. * FIXME: Broxton doesn't support port detection via the
  11381. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11382. * detect the ports.
  11383. */
  11384. intel_ddi_init(dev_priv, PORT_A);
  11385. intel_ddi_init(dev_priv, PORT_B);
  11386. intel_ddi_init(dev_priv, PORT_C);
  11387. intel_dsi_init(dev_priv);
  11388. } else if (HAS_DDI(dev_priv)) {
  11389. int found;
  11390. /*
  11391. * Haswell uses DDI functions to detect digital outputs.
  11392. * On SKL pre-D0 the strap isn't connected, so we assume
  11393. * it's there.
  11394. */
  11395. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11396. /* WaIgnoreDDIAStrap: skl */
  11397. if (found || IS_GEN9_BC(dev_priv))
  11398. intel_ddi_init(dev_priv, PORT_A);
  11399. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11400. * register */
  11401. found = I915_READ(SFUSE_STRAP);
  11402. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11403. intel_ddi_init(dev_priv, PORT_B);
  11404. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11405. intel_ddi_init(dev_priv, PORT_C);
  11406. if (found & SFUSE_STRAP_DDID_DETECTED)
  11407. intel_ddi_init(dev_priv, PORT_D);
  11408. /*
  11409. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11410. */
  11411. if (IS_GEN9_BC(dev_priv) &&
  11412. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11413. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11414. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11415. intel_ddi_init(dev_priv, PORT_E);
  11416. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11417. int found;
  11418. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11419. if (has_edp_a(dev_priv))
  11420. intel_dp_init(dev_priv, DP_A, PORT_A);
  11421. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11422. /* PCH SDVOB multiplex with HDMIB */
  11423. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11424. if (!found)
  11425. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11426. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11427. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11428. }
  11429. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11430. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11431. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11432. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11433. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11434. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11435. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11436. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11437. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11438. bool has_edp, has_port;
  11439. /*
  11440. * The DP_DETECTED bit is the latched state of the DDC
  11441. * SDA pin at boot. However since eDP doesn't require DDC
  11442. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11443. * eDP ports may have been muxed to an alternate function.
  11444. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11445. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11446. * detect eDP ports.
  11447. *
  11448. * Sadly the straps seem to be missing sometimes even for HDMI
  11449. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11450. * and VBT for the presence of the port. Additionally we can't
  11451. * trust the port type the VBT declares as we've seen at least
  11452. * HDMI ports that the VBT claim are DP or eDP.
  11453. */
  11454. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11455. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11456. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11457. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11458. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11459. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11460. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11461. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11462. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11463. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11464. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11465. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11466. if (IS_CHERRYVIEW(dev_priv)) {
  11467. /*
  11468. * eDP not supported on port D,
  11469. * so no need to worry about it
  11470. */
  11471. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11472. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11473. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11474. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11475. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11476. }
  11477. intel_dsi_init(dev_priv);
  11478. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11479. bool found = false;
  11480. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11481. DRM_DEBUG_KMS("probing SDVOB\n");
  11482. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11483. if (!found && IS_G4X(dev_priv)) {
  11484. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11485. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11486. }
  11487. if (!found && IS_G4X(dev_priv))
  11488. intel_dp_init(dev_priv, DP_B, PORT_B);
  11489. }
  11490. /* Before G4X SDVOC doesn't have its own detect register */
  11491. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11492. DRM_DEBUG_KMS("probing SDVOC\n");
  11493. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11494. }
  11495. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11496. if (IS_G4X(dev_priv)) {
  11497. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11498. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11499. }
  11500. if (IS_G4X(dev_priv))
  11501. intel_dp_init(dev_priv, DP_C, PORT_C);
  11502. }
  11503. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11504. intel_dp_init(dev_priv, DP_D, PORT_D);
  11505. } else if (IS_GEN2(dev_priv))
  11506. intel_dvo_init(dev_priv);
  11507. if (SUPPORTS_TV(dev_priv))
  11508. intel_tv_init(dev_priv);
  11509. intel_psr_init(dev_priv);
  11510. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11511. encoder->base.possible_crtcs = encoder->crtc_mask;
  11512. encoder->base.possible_clones =
  11513. intel_encoder_clones(encoder);
  11514. }
  11515. intel_init_pch_refclk(dev_priv);
  11516. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11517. }
  11518. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11519. {
  11520. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11521. drm_framebuffer_cleanup(fb);
  11522. i915_gem_object_lock(intel_fb->obj);
  11523. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11524. i915_gem_object_unlock(intel_fb->obj);
  11525. i915_gem_object_put(intel_fb->obj);
  11526. kfree(intel_fb);
  11527. }
  11528. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11529. struct drm_file *file,
  11530. unsigned int *handle)
  11531. {
  11532. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11533. struct drm_i915_gem_object *obj = intel_fb->obj;
  11534. if (obj->userptr.mm) {
  11535. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11536. return -EINVAL;
  11537. }
  11538. return drm_gem_handle_create(file, &obj->base, handle);
  11539. }
  11540. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11541. struct drm_file *file,
  11542. unsigned flags, unsigned color,
  11543. struct drm_clip_rect *clips,
  11544. unsigned num_clips)
  11545. {
  11546. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11547. i915_gem_object_flush_if_display(obj);
  11548. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11549. return 0;
  11550. }
  11551. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11552. .destroy = intel_user_framebuffer_destroy,
  11553. .create_handle = intel_user_framebuffer_create_handle,
  11554. .dirty = intel_user_framebuffer_dirty,
  11555. };
  11556. static
  11557. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11558. uint64_t fb_modifier, uint32_t pixel_format)
  11559. {
  11560. u32 gen = INTEL_GEN(dev_priv);
  11561. if (gen >= 9) {
  11562. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11563. /* "The stride in bytes must not exceed the of the size of 8K
  11564. * pixels and 32K bytes."
  11565. */
  11566. return min(8192 * cpp, 32768);
  11567. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11568. return 32*1024;
  11569. } else if (gen >= 4) {
  11570. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11571. return 16*1024;
  11572. else
  11573. return 32*1024;
  11574. } else if (gen >= 3) {
  11575. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11576. return 8*1024;
  11577. else
  11578. return 16*1024;
  11579. } else {
  11580. /* XXX DSPC is limited to 4k tiled */
  11581. return 8*1024;
  11582. }
  11583. }
  11584. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11585. struct drm_i915_gem_object *obj,
  11586. struct drm_mode_fb_cmd2 *mode_cmd)
  11587. {
  11588. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11589. struct drm_framebuffer *fb = &intel_fb->base;
  11590. struct drm_format_name_buf format_name;
  11591. u32 pitch_limit;
  11592. unsigned int tiling, stride;
  11593. int ret = -EINVAL;
  11594. int i;
  11595. i915_gem_object_lock(obj);
  11596. obj->framebuffer_references++;
  11597. tiling = i915_gem_object_get_tiling(obj);
  11598. stride = i915_gem_object_get_stride(obj);
  11599. i915_gem_object_unlock(obj);
  11600. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11601. /*
  11602. * If there's a fence, enforce that
  11603. * the fb modifier and tiling mode match.
  11604. */
  11605. if (tiling != I915_TILING_NONE &&
  11606. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11607. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11608. goto err;
  11609. }
  11610. } else {
  11611. if (tiling == I915_TILING_X) {
  11612. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11613. } else if (tiling == I915_TILING_Y) {
  11614. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11615. goto err;
  11616. }
  11617. }
  11618. /* Passed in modifier sanity checking. */
  11619. switch (mode_cmd->modifier[0]) {
  11620. case I915_FORMAT_MOD_Y_TILED_CCS:
  11621. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11622. switch (mode_cmd->pixel_format) {
  11623. case DRM_FORMAT_XBGR8888:
  11624. case DRM_FORMAT_ABGR8888:
  11625. case DRM_FORMAT_XRGB8888:
  11626. case DRM_FORMAT_ARGB8888:
  11627. break;
  11628. default:
  11629. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11630. goto err;
  11631. }
  11632. /* fall through */
  11633. case I915_FORMAT_MOD_Y_TILED:
  11634. case I915_FORMAT_MOD_Yf_TILED:
  11635. if (INTEL_GEN(dev_priv) < 9) {
  11636. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11637. mode_cmd->modifier[0]);
  11638. goto err;
  11639. }
  11640. case DRM_FORMAT_MOD_LINEAR:
  11641. case I915_FORMAT_MOD_X_TILED:
  11642. break;
  11643. default:
  11644. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11645. mode_cmd->modifier[0]);
  11646. goto err;
  11647. }
  11648. /*
  11649. * gen2/3 display engine uses the fence if present,
  11650. * so the tiling mode must match the fb modifier exactly.
  11651. */
  11652. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11653. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11654. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11655. goto err;
  11656. }
  11657. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11658. mode_cmd->pixel_format);
  11659. if (mode_cmd->pitches[0] > pitch_limit) {
  11660. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11661. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11662. "tiled" : "linear",
  11663. mode_cmd->pitches[0], pitch_limit);
  11664. goto err;
  11665. }
  11666. /*
  11667. * If there's a fence, enforce that
  11668. * the fb pitch and fence stride match.
  11669. */
  11670. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11671. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11672. mode_cmd->pitches[0], stride);
  11673. goto err;
  11674. }
  11675. /* Reject formats not supported by any plane early. */
  11676. switch (mode_cmd->pixel_format) {
  11677. case DRM_FORMAT_C8:
  11678. case DRM_FORMAT_RGB565:
  11679. case DRM_FORMAT_XRGB8888:
  11680. case DRM_FORMAT_ARGB8888:
  11681. break;
  11682. case DRM_FORMAT_XRGB1555:
  11683. if (INTEL_GEN(dev_priv) > 3) {
  11684. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11685. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11686. goto err;
  11687. }
  11688. break;
  11689. case DRM_FORMAT_ABGR8888:
  11690. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11691. INTEL_GEN(dev_priv) < 9) {
  11692. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11693. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11694. goto err;
  11695. }
  11696. break;
  11697. case DRM_FORMAT_XBGR8888:
  11698. case DRM_FORMAT_XRGB2101010:
  11699. case DRM_FORMAT_XBGR2101010:
  11700. if (INTEL_GEN(dev_priv) < 4) {
  11701. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11702. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11703. goto err;
  11704. }
  11705. break;
  11706. case DRM_FORMAT_ABGR2101010:
  11707. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11708. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11709. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11710. goto err;
  11711. }
  11712. break;
  11713. case DRM_FORMAT_YUYV:
  11714. case DRM_FORMAT_UYVY:
  11715. case DRM_FORMAT_YVYU:
  11716. case DRM_FORMAT_VYUY:
  11717. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11718. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11719. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11720. goto err;
  11721. }
  11722. break;
  11723. default:
  11724. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11725. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11726. goto err;
  11727. }
  11728. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11729. if (mode_cmd->offsets[0] != 0)
  11730. goto err;
  11731. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11732. for (i = 0; i < fb->format->num_planes; i++) {
  11733. u32 stride_alignment;
  11734. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11735. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11736. goto err;
  11737. }
  11738. stride_alignment = intel_fb_stride_alignment(fb, i);
  11739. /*
  11740. * Display WA #0531: skl,bxt,kbl,glk
  11741. *
  11742. * Render decompression and plane width > 3840
  11743. * combined with horizontal panning requires the
  11744. * plane stride to be a multiple of 4. We'll just
  11745. * require the entire fb to accommodate that to avoid
  11746. * potential runtime errors at plane configuration time.
  11747. */
  11748. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11749. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11750. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11751. stride_alignment *= 4;
  11752. if (fb->pitches[i] & (stride_alignment - 1)) {
  11753. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11754. i, fb->pitches[i], stride_alignment);
  11755. goto err;
  11756. }
  11757. }
  11758. intel_fb->obj = obj;
  11759. ret = intel_fill_fb_info(dev_priv, fb);
  11760. if (ret)
  11761. goto err;
  11762. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11763. if (ret) {
  11764. DRM_ERROR("framebuffer init failed %d\n", ret);
  11765. goto err;
  11766. }
  11767. return 0;
  11768. err:
  11769. i915_gem_object_lock(obj);
  11770. obj->framebuffer_references--;
  11771. i915_gem_object_unlock(obj);
  11772. return ret;
  11773. }
  11774. static struct drm_framebuffer *
  11775. intel_user_framebuffer_create(struct drm_device *dev,
  11776. struct drm_file *filp,
  11777. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11778. {
  11779. struct drm_framebuffer *fb;
  11780. struct drm_i915_gem_object *obj;
  11781. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11782. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11783. if (!obj)
  11784. return ERR_PTR(-ENOENT);
  11785. fb = intel_framebuffer_create(obj, &mode_cmd);
  11786. if (IS_ERR(fb))
  11787. i915_gem_object_put(obj);
  11788. return fb;
  11789. }
  11790. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11791. {
  11792. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11793. drm_atomic_state_default_release(state);
  11794. i915_sw_fence_fini(&intel_state->commit_ready);
  11795. kfree(state);
  11796. }
  11797. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11798. .fb_create = intel_user_framebuffer_create,
  11799. .get_format_info = intel_get_format_info,
  11800. .output_poll_changed = intel_fbdev_output_poll_changed,
  11801. .atomic_check = intel_atomic_check,
  11802. .atomic_commit = intel_atomic_commit,
  11803. .atomic_state_alloc = intel_atomic_state_alloc,
  11804. .atomic_state_clear = intel_atomic_state_clear,
  11805. .atomic_state_free = intel_atomic_state_free,
  11806. };
  11807. /**
  11808. * intel_init_display_hooks - initialize the display modesetting hooks
  11809. * @dev_priv: device private
  11810. */
  11811. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11812. {
  11813. intel_init_cdclk_hooks(dev_priv);
  11814. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11815. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11816. dev_priv->display.get_initial_plane_config =
  11817. skylake_get_initial_plane_config;
  11818. dev_priv->display.crtc_compute_clock =
  11819. haswell_crtc_compute_clock;
  11820. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11821. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11822. } else if (HAS_DDI(dev_priv)) {
  11823. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11824. dev_priv->display.get_initial_plane_config =
  11825. ironlake_get_initial_plane_config;
  11826. dev_priv->display.crtc_compute_clock =
  11827. haswell_crtc_compute_clock;
  11828. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11829. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11830. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11831. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11832. dev_priv->display.get_initial_plane_config =
  11833. ironlake_get_initial_plane_config;
  11834. dev_priv->display.crtc_compute_clock =
  11835. ironlake_crtc_compute_clock;
  11836. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11837. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11838. } else if (IS_CHERRYVIEW(dev_priv)) {
  11839. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11840. dev_priv->display.get_initial_plane_config =
  11841. i9xx_get_initial_plane_config;
  11842. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11843. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11844. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11845. } else if (IS_VALLEYVIEW(dev_priv)) {
  11846. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11847. dev_priv->display.get_initial_plane_config =
  11848. i9xx_get_initial_plane_config;
  11849. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11850. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11851. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11852. } else if (IS_G4X(dev_priv)) {
  11853. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11854. dev_priv->display.get_initial_plane_config =
  11855. i9xx_get_initial_plane_config;
  11856. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11857. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11858. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11859. } else if (IS_PINEVIEW(dev_priv)) {
  11860. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11861. dev_priv->display.get_initial_plane_config =
  11862. i9xx_get_initial_plane_config;
  11863. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11864. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11865. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11866. } else if (!IS_GEN2(dev_priv)) {
  11867. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11868. dev_priv->display.get_initial_plane_config =
  11869. i9xx_get_initial_plane_config;
  11870. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11871. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11872. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11873. } else {
  11874. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11875. dev_priv->display.get_initial_plane_config =
  11876. i9xx_get_initial_plane_config;
  11877. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11878. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11879. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11880. }
  11881. if (IS_GEN5(dev_priv)) {
  11882. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11883. } else if (IS_GEN6(dev_priv)) {
  11884. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11885. } else if (IS_IVYBRIDGE(dev_priv)) {
  11886. /* FIXME: detect B0+ stepping and use auto training */
  11887. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11888. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11889. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11890. }
  11891. if (dev_priv->info.gen >= 9)
  11892. dev_priv->display.update_crtcs = skl_update_crtcs;
  11893. else
  11894. dev_priv->display.update_crtcs = intel_update_crtcs;
  11895. }
  11896. /*
  11897. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11898. */
  11899. static void quirk_ssc_force_disable(struct drm_device *dev)
  11900. {
  11901. struct drm_i915_private *dev_priv = to_i915(dev);
  11902. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11903. DRM_INFO("applying lvds SSC disable quirk\n");
  11904. }
  11905. /*
  11906. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11907. * brightness value
  11908. */
  11909. static void quirk_invert_brightness(struct drm_device *dev)
  11910. {
  11911. struct drm_i915_private *dev_priv = to_i915(dev);
  11912. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11913. DRM_INFO("applying inverted panel brightness quirk\n");
  11914. }
  11915. /* Some VBT's incorrectly indicate no backlight is present */
  11916. static void quirk_backlight_present(struct drm_device *dev)
  11917. {
  11918. struct drm_i915_private *dev_priv = to_i915(dev);
  11919. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11920. DRM_INFO("applying backlight present quirk\n");
  11921. }
  11922. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11923. * which is 300 ms greater than eDP spec T12 min.
  11924. */
  11925. static void quirk_increase_t12_delay(struct drm_device *dev)
  11926. {
  11927. struct drm_i915_private *dev_priv = to_i915(dev);
  11928. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11929. DRM_INFO("Applying T12 delay quirk\n");
  11930. }
  11931. struct intel_quirk {
  11932. int device;
  11933. int subsystem_vendor;
  11934. int subsystem_device;
  11935. void (*hook)(struct drm_device *dev);
  11936. };
  11937. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11938. struct intel_dmi_quirk {
  11939. void (*hook)(struct drm_device *dev);
  11940. const struct dmi_system_id (*dmi_id_list)[];
  11941. };
  11942. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11943. {
  11944. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11945. return 1;
  11946. }
  11947. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11948. {
  11949. .dmi_id_list = &(const struct dmi_system_id[]) {
  11950. {
  11951. .callback = intel_dmi_reverse_brightness,
  11952. .ident = "NCR Corporation",
  11953. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11954. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11955. },
  11956. },
  11957. { } /* terminating entry */
  11958. },
  11959. .hook = quirk_invert_brightness,
  11960. },
  11961. };
  11962. static struct intel_quirk intel_quirks[] = {
  11963. /* Lenovo U160 cannot use SSC on LVDS */
  11964. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11965. /* Sony Vaio Y cannot use SSC on LVDS */
  11966. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11967. /* Acer Aspire 5734Z must invert backlight brightness */
  11968. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11969. /* Acer/eMachines G725 */
  11970. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11971. /* Acer/eMachines e725 */
  11972. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11973. /* Acer/Packard Bell NCL20 */
  11974. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11975. /* Acer Aspire 4736Z */
  11976. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11977. /* Acer Aspire 5336 */
  11978. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11979. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11980. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11981. /* Acer C720 Chromebook (Core i3 4005U) */
  11982. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11983. /* Apple Macbook 2,1 (Core 2 T7400) */
  11984. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11985. /* Apple Macbook 4,1 */
  11986. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11987. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11988. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11989. /* HP Chromebook 14 (Celeron 2955U) */
  11990. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11991. /* Dell Chromebook 11 */
  11992. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11993. /* Dell Chromebook 11 (2015 version) */
  11994. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11995. /* Toshiba Satellite P50-C-18C */
  11996. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11997. };
  11998. static void intel_init_quirks(struct drm_device *dev)
  11999. {
  12000. struct pci_dev *d = dev->pdev;
  12001. int i;
  12002. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12003. struct intel_quirk *q = &intel_quirks[i];
  12004. if (d->device == q->device &&
  12005. (d->subsystem_vendor == q->subsystem_vendor ||
  12006. q->subsystem_vendor == PCI_ANY_ID) &&
  12007. (d->subsystem_device == q->subsystem_device ||
  12008. q->subsystem_device == PCI_ANY_ID))
  12009. q->hook(dev);
  12010. }
  12011. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12012. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12013. intel_dmi_quirks[i].hook(dev);
  12014. }
  12015. }
  12016. /* Disable the VGA plane that we never use */
  12017. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12018. {
  12019. struct pci_dev *pdev = dev_priv->drm.pdev;
  12020. u8 sr1;
  12021. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12022. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12023. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12024. outb(SR01, VGA_SR_INDEX);
  12025. sr1 = inb(VGA_SR_DATA);
  12026. outb(sr1 | 1<<5, VGA_SR_DATA);
  12027. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12028. udelay(300);
  12029. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12030. POSTING_READ(vga_reg);
  12031. }
  12032. void intel_modeset_init_hw(struct drm_device *dev)
  12033. {
  12034. struct drm_i915_private *dev_priv = to_i915(dev);
  12035. intel_update_cdclk(dev_priv);
  12036. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12037. intel_init_clock_gating(dev_priv);
  12038. }
  12039. /*
  12040. * Calculate what we think the watermarks should be for the state we've read
  12041. * out of the hardware and then immediately program those watermarks so that
  12042. * we ensure the hardware settings match our internal state.
  12043. *
  12044. * We can calculate what we think WM's should be by creating a duplicate of the
  12045. * current state (which was constructed during hardware readout) and running it
  12046. * through the atomic check code to calculate new watermark values in the
  12047. * state object.
  12048. */
  12049. static void sanitize_watermarks(struct drm_device *dev)
  12050. {
  12051. struct drm_i915_private *dev_priv = to_i915(dev);
  12052. struct drm_atomic_state *state;
  12053. struct intel_atomic_state *intel_state;
  12054. struct drm_crtc *crtc;
  12055. struct drm_crtc_state *cstate;
  12056. struct drm_modeset_acquire_ctx ctx;
  12057. int ret;
  12058. int i;
  12059. /* Only supported on platforms that use atomic watermark design */
  12060. if (!dev_priv->display.optimize_watermarks)
  12061. return;
  12062. /*
  12063. * We need to hold connection_mutex before calling duplicate_state so
  12064. * that the connector loop is protected.
  12065. */
  12066. drm_modeset_acquire_init(&ctx, 0);
  12067. retry:
  12068. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12069. if (ret == -EDEADLK) {
  12070. drm_modeset_backoff(&ctx);
  12071. goto retry;
  12072. } else if (WARN_ON(ret)) {
  12073. goto fail;
  12074. }
  12075. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12076. if (WARN_ON(IS_ERR(state)))
  12077. goto fail;
  12078. intel_state = to_intel_atomic_state(state);
  12079. /*
  12080. * Hardware readout is the only time we don't want to calculate
  12081. * intermediate watermarks (since we don't trust the current
  12082. * watermarks).
  12083. */
  12084. if (!HAS_GMCH_DISPLAY(dev_priv))
  12085. intel_state->skip_intermediate_wm = true;
  12086. ret = intel_atomic_check(dev, state);
  12087. if (ret) {
  12088. /*
  12089. * If we fail here, it means that the hardware appears to be
  12090. * programmed in a way that shouldn't be possible, given our
  12091. * understanding of watermark requirements. This might mean a
  12092. * mistake in the hardware readout code or a mistake in the
  12093. * watermark calculations for a given platform. Raise a WARN
  12094. * so that this is noticeable.
  12095. *
  12096. * If this actually happens, we'll have to just leave the
  12097. * BIOS-programmed watermarks untouched and hope for the best.
  12098. */
  12099. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12100. goto put_state;
  12101. }
  12102. /* Write calculated watermark values back */
  12103. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12104. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12105. cs->wm.need_postvbl_update = true;
  12106. dev_priv->display.optimize_watermarks(intel_state, cs);
  12107. }
  12108. put_state:
  12109. drm_atomic_state_put(state);
  12110. fail:
  12111. drm_modeset_drop_locks(&ctx);
  12112. drm_modeset_acquire_fini(&ctx);
  12113. }
  12114. int intel_modeset_init(struct drm_device *dev)
  12115. {
  12116. struct drm_i915_private *dev_priv = to_i915(dev);
  12117. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12118. enum pipe pipe;
  12119. struct intel_crtc *crtc;
  12120. drm_mode_config_init(dev);
  12121. dev->mode_config.min_width = 0;
  12122. dev->mode_config.min_height = 0;
  12123. dev->mode_config.preferred_depth = 24;
  12124. dev->mode_config.prefer_shadow = 1;
  12125. dev->mode_config.allow_fb_modifiers = true;
  12126. dev->mode_config.funcs = &intel_mode_funcs;
  12127. init_llist_head(&dev_priv->atomic_helper.free_list);
  12128. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12129. intel_atomic_helper_free_state_worker);
  12130. intel_init_quirks(dev);
  12131. intel_init_pm(dev_priv);
  12132. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12133. return 0;
  12134. /*
  12135. * There may be no VBT; and if the BIOS enabled SSC we can
  12136. * just keep using it to avoid unnecessary flicker. Whereas if the
  12137. * BIOS isn't using it, don't assume it will work even if the VBT
  12138. * indicates as much.
  12139. */
  12140. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12141. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12142. DREF_SSC1_ENABLE);
  12143. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12144. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12145. bios_lvds_use_ssc ? "en" : "dis",
  12146. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12147. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12148. }
  12149. }
  12150. if (IS_GEN2(dev_priv)) {
  12151. dev->mode_config.max_width = 2048;
  12152. dev->mode_config.max_height = 2048;
  12153. } else if (IS_GEN3(dev_priv)) {
  12154. dev->mode_config.max_width = 4096;
  12155. dev->mode_config.max_height = 4096;
  12156. } else {
  12157. dev->mode_config.max_width = 8192;
  12158. dev->mode_config.max_height = 8192;
  12159. }
  12160. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12161. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12162. dev->mode_config.cursor_height = 1023;
  12163. } else if (IS_GEN2(dev_priv)) {
  12164. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12165. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12166. } else {
  12167. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12168. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12169. }
  12170. dev->mode_config.fb_base = ggtt->mappable_base;
  12171. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12172. INTEL_INFO(dev_priv)->num_pipes,
  12173. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12174. for_each_pipe(dev_priv, pipe) {
  12175. int ret;
  12176. ret = intel_crtc_init(dev_priv, pipe);
  12177. if (ret) {
  12178. drm_mode_config_cleanup(dev);
  12179. return ret;
  12180. }
  12181. }
  12182. intel_shared_dpll_init(dev);
  12183. intel_update_czclk(dev_priv);
  12184. intel_modeset_init_hw(dev);
  12185. if (dev_priv->max_cdclk_freq == 0)
  12186. intel_update_max_cdclk(dev_priv);
  12187. /* Just disable it once at startup */
  12188. i915_disable_vga(dev_priv);
  12189. intel_setup_outputs(dev_priv);
  12190. drm_modeset_lock_all(dev);
  12191. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12192. drm_modeset_unlock_all(dev);
  12193. for_each_intel_crtc(dev, crtc) {
  12194. struct intel_initial_plane_config plane_config = {};
  12195. if (!crtc->active)
  12196. continue;
  12197. /*
  12198. * Note that reserving the BIOS fb up front prevents us
  12199. * from stuffing other stolen allocations like the ring
  12200. * on top. This prevents some ugliness at boot time, and
  12201. * can even allow for smooth boot transitions if the BIOS
  12202. * fb is large enough for the active pipe configuration.
  12203. */
  12204. dev_priv->display.get_initial_plane_config(crtc,
  12205. &plane_config);
  12206. /*
  12207. * If the fb is shared between multiple heads, we'll
  12208. * just get the first one.
  12209. */
  12210. intel_find_initial_plane_obj(crtc, &plane_config);
  12211. }
  12212. /*
  12213. * Make sure hardware watermarks really match the state we read out.
  12214. * Note that we need to do this after reconstructing the BIOS fb's
  12215. * since the watermark calculation done here will use pstate->fb.
  12216. */
  12217. if (!HAS_GMCH_DISPLAY(dev_priv))
  12218. sanitize_watermarks(dev);
  12219. return 0;
  12220. }
  12221. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12222. {
  12223. /* 640x480@60Hz, ~25175 kHz */
  12224. struct dpll clock = {
  12225. .m1 = 18,
  12226. .m2 = 7,
  12227. .p1 = 13,
  12228. .p2 = 4,
  12229. .n = 2,
  12230. };
  12231. u32 dpll, fp;
  12232. int i;
  12233. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12234. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12235. pipe_name(pipe), clock.vco, clock.dot);
  12236. fp = i9xx_dpll_compute_fp(&clock);
  12237. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12238. DPLL_VGA_MODE_DIS |
  12239. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12240. PLL_P2_DIVIDE_BY_4 |
  12241. PLL_REF_INPUT_DREFCLK |
  12242. DPLL_VCO_ENABLE;
  12243. I915_WRITE(FP0(pipe), fp);
  12244. I915_WRITE(FP1(pipe), fp);
  12245. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12246. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12247. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12248. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12249. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12250. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12251. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12252. /*
  12253. * Apparently we need to have VGA mode enabled prior to changing
  12254. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12255. * dividers, even though the register value does change.
  12256. */
  12257. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12258. I915_WRITE(DPLL(pipe), dpll);
  12259. /* Wait for the clocks to stabilize. */
  12260. POSTING_READ(DPLL(pipe));
  12261. udelay(150);
  12262. /* The pixel multiplier can only be updated once the
  12263. * DPLL is enabled and the clocks are stable.
  12264. *
  12265. * So write it again.
  12266. */
  12267. I915_WRITE(DPLL(pipe), dpll);
  12268. /* We do this three times for luck */
  12269. for (i = 0; i < 3 ; i++) {
  12270. I915_WRITE(DPLL(pipe), dpll);
  12271. POSTING_READ(DPLL(pipe));
  12272. udelay(150); /* wait for warmup */
  12273. }
  12274. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12275. POSTING_READ(PIPECONF(pipe));
  12276. }
  12277. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12278. {
  12279. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12280. pipe_name(pipe));
  12281. assert_plane_disabled(dev_priv, PLANE_A);
  12282. assert_plane_disabled(dev_priv, PLANE_B);
  12283. I915_WRITE(PIPECONF(pipe), 0);
  12284. POSTING_READ(PIPECONF(pipe));
  12285. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  12286. DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
  12287. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12288. POSTING_READ(DPLL(pipe));
  12289. }
  12290. static bool
  12291. intel_check_plane_mapping(struct intel_crtc *crtc)
  12292. {
  12293. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12294. u32 val;
  12295. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12296. return true;
  12297. val = I915_READ(DSPCNTR(!crtc->plane));
  12298. if ((val & DISPLAY_PLANE_ENABLE) &&
  12299. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12300. return false;
  12301. return true;
  12302. }
  12303. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12304. {
  12305. struct drm_device *dev = crtc->base.dev;
  12306. struct intel_encoder *encoder;
  12307. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12308. return true;
  12309. return false;
  12310. }
  12311. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12312. {
  12313. struct drm_device *dev = encoder->base.dev;
  12314. struct intel_connector *connector;
  12315. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12316. return connector;
  12317. return NULL;
  12318. }
  12319. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12320. enum transcoder pch_transcoder)
  12321. {
  12322. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12323. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12324. }
  12325. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12326. struct drm_modeset_acquire_ctx *ctx)
  12327. {
  12328. struct drm_device *dev = crtc->base.dev;
  12329. struct drm_i915_private *dev_priv = to_i915(dev);
  12330. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12331. /* Clear any frame start delays used for debugging left by the BIOS */
  12332. if (!transcoder_is_dsi(cpu_transcoder)) {
  12333. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12334. I915_WRITE(reg,
  12335. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12336. }
  12337. /* restore vblank interrupts to correct state */
  12338. drm_crtc_vblank_reset(&crtc->base);
  12339. if (crtc->active) {
  12340. struct intel_plane *plane;
  12341. drm_crtc_vblank_on(&crtc->base);
  12342. /* Disable everything but the primary plane */
  12343. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12344. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12345. continue;
  12346. trace_intel_disable_plane(&plane->base, crtc);
  12347. plane->disable_plane(plane, crtc);
  12348. }
  12349. }
  12350. /* We need to sanitize the plane -> pipe mapping first because this will
  12351. * disable the crtc (and hence change the state) if it is wrong. Note
  12352. * that gen4+ has a fixed plane -> pipe mapping. */
  12353. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12354. bool plane;
  12355. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12356. crtc->base.base.id, crtc->base.name);
  12357. /* Pipe has the wrong plane attached and the plane is active.
  12358. * Temporarily change the plane mapping and disable everything
  12359. * ... */
  12360. plane = crtc->plane;
  12361. crtc->base.primary->state->visible = true;
  12362. crtc->plane = !plane;
  12363. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12364. crtc->plane = plane;
  12365. }
  12366. /* Adjust the state of the output pipe according to whether we
  12367. * have active connectors/encoders. */
  12368. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12369. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12370. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12371. /*
  12372. * We start out with underrun reporting disabled to avoid races.
  12373. * For correct bookkeeping mark this on active crtcs.
  12374. *
  12375. * Also on gmch platforms we dont have any hardware bits to
  12376. * disable the underrun reporting. Which means we need to start
  12377. * out with underrun reporting disabled also on inactive pipes,
  12378. * since otherwise we'll complain about the garbage we read when
  12379. * e.g. coming up after runtime pm.
  12380. *
  12381. * No protection against concurrent access is required - at
  12382. * worst a fifo underrun happens which also sets this to false.
  12383. */
  12384. crtc->cpu_fifo_underrun_disabled = true;
  12385. /*
  12386. * We track the PCH trancoder underrun reporting state
  12387. * within the crtc. With crtc for pipe A housing the underrun
  12388. * reporting state for PCH transcoder A, crtc for pipe B housing
  12389. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12390. * and marking underrun reporting as disabled for the non-existing
  12391. * PCH transcoders B and C would prevent enabling the south
  12392. * error interrupt (see cpt_can_enable_serr_int()).
  12393. */
  12394. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12395. crtc->pch_fifo_underrun_disabled = true;
  12396. }
  12397. }
  12398. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12399. {
  12400. struct intel_connector *connector;
  12401. /* We need to check both for a crtc link (meaning that the
  12402. * encoder is active and trying to read from a pipe) and the
  12403. * pipe itself being active. */
  12404. bool has_active_crtc = encoder->base.crtc &&
  12405. to_intel_crtc(encoder->base.crtc)->active;
  12406. connector = intel_encoder_find_connector(encoder);
  12407. if (connector && !has_active_crtc) {
  12408. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12409. encoder->base.base.id,
  12410. encoder->base.name);
  12411. /* Connector is active, but has no active pipe. This is
  12412. * fallout from our resume register restoring. Disable
  12413. * the encoder manually again. */
  12414. if (encoder->base.crtc) {
  12415. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12416. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12417. encoder->base.base.id,
  12418. encoder->base.name);
  12419. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12420. if (encoder->post_disable)
  12421. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12422. }
  12423. encoder->base.crtc = NULL;
  12424. /* Inconsistent output/port/pipe state happens presumably due to
  12425. * a bug in one of the get_hw_state functions. Or someplace else
  12426. * in our code, like the register restore mess on resume. Clamp
  12427. * things to off as a safer default. */
  12428. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12429. connector->base.encoder = NULL;
  12430. }
  12431. /* Enabled encoders without active connectors will be fixed in
  12432. * the crtc fixup. */
  12433. }
  12434. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12435. {
  12436. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12437. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12438. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12439. i915_disable_vga(dev_priv);
  12440. }
  12441. }
  12442. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12443. {
  12444. /* This function can be called both from intel_modeset_setup_hw_state or
  12445. * at a very early point in our resume sequence, where the power well
  12446. * structures are not yet restored. Since this function is at a very
  12447. * paranoid "someone might have enabled VGA while we were not looking"
  12448. * level, just check if the power well is enabled instead of trying to
  12449. * follow the "don't touch the power well if we don't need it" policy
  12450. * the rest of the driver uses. */
  12451. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12452. return;
  12453. i915_redisable_vga_power_on(dev_priv);
  12454. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12455. }
  12456. static bool primary_get_hw_state(struct intel_plane *plane)
  12457. {
  12458. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12459. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12460. }
  12461. /* FIXME read out full plane state for all planes */
  12462. static void readout_plane_state(struct intel_crtc *crtc)
  12463. {
  12464. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12465. bool visible;
  12466. visible = crtc->active && primary_get_hw_state(primary);
  12467. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12468. to_intel_plane_state(primary->base.state),
  12469. visible);
  12470. }
  12471. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12472. {
  12473. struct drm_i915_private *dev_priv = to_i915(dev);
  12474. enum pipe pipe;
  12475. struct intel_crtc *crtc;
  12476. struct intel_encoder *encoder;
  12477. struct intel_connector *connector;
  12478. struct drm_connector_list_iter conn_iter;
  12479. int i;
  12480. dev_priv->active_crtcs = 0;
  12481. for_each_intel_crtc(dev, crtc) {
  12482. struct intel_crtc_state *crtc_state =
  12483. to_intel_crtc_state(crtc->base.state);
  12484. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12485. memset(crtc_state, 0, sizeof(*crtc_state));
  12486. crtc_state->base.crtc = &crtc->base;
  12487. crtc_state->base.active = crtc_state->base.enable =
  12488. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12489. crtc->base.enabled = crtc_state->base.enable;
  12490. crtc->active = crtc_state->base.active;
  12491. if (crtc_state->base.active)
  12492. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12493. readout_plane_state(crtc);
  12494. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12495. crtc->base.base.id, crtc->base.name,
  12496. enableddisabled(crtc_state->base.active));
  12497. }
  12498. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12499. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12500. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12501. &pll->state.hw_state);
  12502. pll->state.crtc_mask = 0;
  12503. for_each_intel_crtc(dev, crtc) {
  12504. struct intel_crtc_state *crtc_state =
  12505. to_intel_crtc_state(crtc->base.state);
  12506. if (crtc_state->base.active &&
  12507. crtc_state->shared_dpll == pll)
  12508. pll->state.crtc_mask |= 1 << crtc->pipe;
  12509. }
  12510. pll->active_mask = pll->state.crtc_mask;
  12511. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12512. pll->name, pll->state.crtc_mask, pll->on);
  12513. }
  12514. for_each_intel_encoder(dev, encoder) {
  12515. pipe = 0;
  12516. if (encoder->get_hw_state(encoder, &pipe)) {
  12517. struct intel_crtc_state *crtc_state;
  12518. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12519. crtc_state = to_intel_crtc_state(crtc->base.state);
  12520. encoder->base.crtc = &crtc->base;
  12521. crtc_state->output_types |= 1 << encoder->type;
  12522. encoder->get_config(encoder, crtc_state);
  12523. } else {
  12524. encoder->base.crtc = NULL;
  12525. }
  12526. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12527. encoder->base.base.id, encoder->base.name,
  12528. enableddisabled(encoder->base.crtc),
  12529. pipe_name(pipe));
  12530. }
  12531. drm_connector_list_iter_begin(dev, &conn_iter);
  12532. for_each_intel_connector_iter(connector, &conn_iter) {
  12533. if (connector->get_hw_state(connector)) {
  12534. connector->base.dpms = DRM_MODE_DPMS_ON;
  12535. encoder = connector->encoder;
  12536. connector->base.encoder = &encoder->base;
  12537. if (encoder->base.crtc &&
  12538. encoder->base.crtc->state->active) {
  12539. /*
  12540. * This has to be done during hardware readout
  12541. * because anything calling .crtc_disable may
  12542. * rely on the connector_mask being accurate.
  12543. */
  12544. encoder->base.crtc->state->connector_mask |=
  12545. 1 << drm_connector_index(&connector->base);
  12546. encoder->base.crtc->state->encoder_mask |=
  12547. 1 << drm_encoder_index(&encoder->base);
  12548. }
  12549. } else {
  12550. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12551. connector->base.encoder = NULL;
  12552. }
  12553. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12554. connector->base.base.id, connector->base.name,
  12555. enableddisabled(connector->base.encoder));
  12556. }
  12557. drm_connector_list_iter_end(&conn_iter);
  12558. for_each_intel_crtc(dev, crtc) {
  12559. struct intel_crtc_state *crtc_state =
  12560. to_intel_crtc_state(crtc->base.state);
  12561. int min_cdclk = 0;
  12562. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12563. if (crtc_state->base.active) {
  12564. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12565. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12566. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12567. /*
  12568. * The initial mode needs to be set in order to keep
  12569. * the atomic core happy. It wants a valid mode if the
  12570. * crtc's enabled, so we do the above call.
  12571. *
  12572. * But we don't set all the derived state fully, hence
  12573. * set a flag to indicate that a full recalculation is
  12574. * needed on the next commit.
  12575. */
  12576. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12577. intel_crtc_compute_pixel_rate(crtc_state);
  12578. if (dev_priv->display.modeset_calc_cdclk) {
  12579. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12580. if (WARN_ON(min_cdclk < 0))
  12581. min_cdclk = 0;
  12582. }
  12583. drm_calc_timestamping_constants(&crtc->base,
  12584. &crtc_state->base.adjusted_mode);
  12585. update_scanline_offset(crtc);
  12586. }
  12587. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12588. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12589. }
  12590. }
  12591. static void
  12592. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12593. {
  12594. struct intel_encoder *encoder;
  12595. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12596. u64 get_domains;
  12597. enum intel_display_power_domain domain;
  12598. if (!encoder->get_power_domains)
  12599. continue;
  12600. get_domains = encoder->get_power_domains(encoder);
  12601. for_each_power_domain(domain, get_domains)
  12602. intel_display_power_get(dev_priv, domain);
  12603. }
  12604. }
  12605. /* Scan out the current hw modeset state,
  12606. * and sanitizes it to the current state
  12607. */
  12608. static void
  12609. intel_modeset_setup_hw_state(struct drm_device *dev,
  12610. struct drm_modeset_acquire_ctx *ctx)
  12611. {
  12612. struct drm_i915_private *dev_priv = to_i915(dev);
  12613. enum pipe pipe;
  12614. struct intel_crtc *crtc;
  12615. struct intel_encoder *encoder;
  12616. int i;
  12617. intel_modeset_readout_hw_state(dev);
  12618. /* HW state is read out, now we need to sanitize this mess. */
  12619. get_encoder_power_domains(dev_priv);
  12620. for_each_intel_encoder(dev, encoder) {
  12621. intel_sanitize_encoder(encoder);
  12622. }
  12623. for_each_pipe(dev_priv, pipe) {
  12624. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12625. intel_sanitize_crtc(crtc, ctx);
  12626. intel_dump_pipe_config(crtc, crtc->config,
  12627. "[setup_hw_state]");
  12628. }
  12629. intel_modeset_update_connector_atomic_state(dev);
  12630. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12631. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12632. if (!pll->on || pll->active_mask)
  12633. continue;
  12634. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12635. pll->funcs.disable(dev_priv, pll);
  12636. pll->on = false;
  12637. }
  12638. if (IS_G4X(dev_priv)) {
  12639. g4x_wm_get_hw_state(dev);
  12640. g4x_wm_sanitize(dev_priv);
  12641. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12642. vlv_wm_get_hw_state(dev);
  12643. vlv_wm_sanitize(dev_priv);
  12644. } else if (INTEL_GEN(dev_priv) >= 9) {
  12645. skl_wm_get_hw_state(dev);
  12646. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12647. ilk_wm_get_hw_state(dev);
  12648. }
  12649. for_each_intel_crtc(dev, crtc) {
  12650. u64 put_domains;
  12651. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12652. if (WARN_ON(put_domains))
  12653. modeset_put_power_domains(dev_priv, put_domains);
  12654. }
  12655. intel_display_set_init_power(dev_priv, false);
  12656. intel_power_domains_verify_state(dev_priv);
  12657. intel_fbc_init_pipe_state(dev_priv);
  12658. }
  12659. void intel_display_resume(struct drm_device *dev)
  12660. {
  12661. struct drm_i915_private *dev_priv = to_i915(dev);
  12662. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12663. struct drm_modeset_acquire_ctx ctx;
  12664. int ret;
  12665. dev_priv->modeset_restore_state = NULL;
  12666. if (state)
  12667. state->acquire_ctx = &ctx;
  12668. drm_modeset_acquire_init(&ctx, 0);
  12669. while (1) {
  12670. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12671. if (ret != -EDEADLK)
  12672. break;
  12673. drm_modeset_backoff(&ctx);
  12674. }
  12675. if (!ret)
  12676. ret = __intel_display_resume(dev, state, &ctx);
  12677. drm_modeset_drop_locks(&ctx);
  12678. drm_modeset_acquire_fini(&ctx);
  12679. if (ret)
  12680. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12681. if (state)
  12682. drm_atomic_state_put(state);
  12683. }
  12684. void intel_modeset_gem_init(struct drm_device *dev)
  12685. {
  12686. struct drm_i915_private *dev_priv = to_i915(dev);
  12687. intel_init_gt_powersave(dev_priv);
  12688. intel_setup_overlay(dev_priv);
  12689. }
  12690. int intel_connector_register(struct drm_connector *connector)
  12691. {
  12692. struct intel_connector *intel_connector = to_intel_connector(connector);
  12693. int ret;
  12694. ret = intel_backlight_device_register(intel_connector);
  12695. if (ret)
  12696. goto err;
  12697. return 0;
  12698. err:
  12699. return ret;
  12700. }
  12701. void intel_connector_unregister(struct drm_connector *connector)
  12702. {
  12703. struct intel_connector *intel_connector = to_intel_connector(connector);
  12704. intel_backlight_device_unregister(intel_connector);
  12705. intel_panel_destroy_backlight(connector);
  12706. }
  12707. void intel_modeset_cleanup(struct drm_device *dev)
  12708. {
  12709. struct drm_i915_private *dev_priv = to_i915(dev);
  12710. flush_work(&dev_priv->atomic_helper.free_work);
  12711. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12712. intel_disable_gt_powersave(dev_priv);
  12713. /*
  12714. * Interrupts and polling as the first thing to avoid creating havoc.
  12715. * Too much stuff here (turning of connectors, ...) would
  12716. * experience fancy races otherwise.
  12717. */
  12718. intel_irq_uninstall(dev_priv);
  12719. /*
  12720. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12721. * poll handlers. Hence disable polling after hpd handling is shut down.
  12722. */
  12723. drm_kms_helper_poll_fini(dev);
  12724. /* poll work can call into fbdev, hence clean that up afterwards */
  12725. intel_fbdev_fini(dev_priv);
  12726. intel_unregister_dsm_handler();
  12727. intel_fbc_global_disable(dev_priv);
  12728. /* flush any delayed tasks or pending work */
  12729. flush_scheduled_work();
  12730. drm_mode_config_cleanup(dev);
  12731. intel_cleanup_overlay(dev_priv);
  12732. intel_cleanup_gt_powersave(dev_priv);
  12733. intel_teardown_gmbus(dev_priv);
  12734. }
  12735. void intel_connector_attach_encoder(struct intel_connector *connector,
  12736. struct intel_encoder *encoder)
  12737. {
  12738. connector->encoder = encoder;
  12739. drm_mode_connector_attach_encoder(&connector->base,
  12740. &encoder->base);
  12741. }
  12742. /*
  12743. * set vga decode state - true == enable VGA decode
  12744. */
  12745. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12746. {
  12747. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12748. u16 gmch_ctrl;
  12749. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12750. DRM_ERROR("failed to read control word\n");
  12751. return -EIO;
  12752. }
  12753. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12754. return 0;
  12755. if (state)
  12756. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12757. else
  12758. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12759. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12760. DRM_ERROR("failed to write control word\n");
  12761. return -EIO;
  12762. }
  12763. return 0;
  12764. }
  12765. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12766. struct intel_display_error_state {
  12767. u32 power_well_driver;
  12768. int num_transcoders;
  12769. struct intel_cursor_error_state {
  12770. u32 control;
  12771. u32 position;
  12772. u32 base;
  12773. u32 size;
  12774. } cursor[I915_MAX_PIPES];
  12775. struct intel_pipe_error_state {
  12776. bool power_domain_on;
  12777. u32 source;
  12778. u32 stat;
  12779. } pipe[I915_MAX_PIPES];
  12780. struct intel_plane_error_state {
  12781. u32 control;
  12782. u32 stride;
  12783. u32 size;
  12784. u32 pos;
  12785. u32 addr;
  12786. u32 surface;
  12787. u32 tile_offset;
  12788. } plane[I915_MAX_PIPES];
  12789. struct intel_transcoder_error_state {
  12790. bool power_domain_on;
  12791. enum transcoder cpu_transcoder;
  12792. u32 conf;
  12793. u32 htotal;
  12794. u32 hblank;
  12795. u32 hsync;
  12796. u32 vtotal;
  12797. u32 vblank;
  12798. u32 vsync;
  12799. } transcoder[4];
  12800. };
  12801. struct intel_display_error_state *
  12802. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12803. {
  12804. struct intel_display_error_state *error;
  12805. int transcoders[] = {
  12806. TRANSCODER_A,
  12807. TRANSCODER_B,
  12808. TRANSCODER_C,
  12809. TRANSCODER_EDP,
  12810. };
  12811. int i;
  12812. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12813. return NULL;
  12814. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12815. if (error == NULL)
  12816. return NULL;
  12817. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12818. error->power_well_driver =
  12819. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12820. for_each_pipe(dev_priv, i) {
  12821. error->pipe[i].power_domain_on =
  12822. __intel_display_power_is_enabled(dev_priv,
  12823. POWER_DOMAIN_PIPE(i));
  12824. if (!error->pipe[i].power_domain_on)
  12825. continue;
  12826. error->cursor[i].control = I915_READ(CURCNTR(i));
  12827. error->cursor[i].position = I915_READ(CURPOS(i));
  12828. error->cursor[i].base = I915_READ(CURBASE(i));
  12829. error->plane[i].control = I915_READ(DSPCNTR(i));
  12830. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12831. if (INTEL_GEN(dev_priv) <= 3) {
  12832. error->plane[i].size = I915_READ(DSPSIZE(i));
  12833. error->plane[i].pos = I915_READ(DSPPOS(i));
  12834. }
  12835. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12836. error->plane[i].addr = I915_READ(DSPADDR(i));
  12837. if (INTEL_GEN(dev_priv) >= 4) {
  12838. error->plane[i].surface = I915_READ(DSPSURF(i));
  12839. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12840. }
  12841. error->pipe[i].source = I915_READ(PIPESRC(i));
  12842. if (HAS_GMCH_DISPLAY(dev_priv))
  12843. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12844. }
  12845. /* Note: this does not include DSI transcoders. */
  12846. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12847. if (HAS_DDI(dev_priv))
  12848. error->num_transcoders++; /* Account for eDP. */
  12849. for (i = 0; i < error->num_transcoders; i++) {
  12850. enum transcoder cpu_transcoder = transcoders[i];
  12851. error->transcoder[i].power_domain_on =
  12852. __intel_display_power_is_enabled(dev_priv,
  12853. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12854. if (!error->transcoder[i].power_domain_on)
  12855. continue;
  12856. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12857. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12858. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12859. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12860. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12861. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12862. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12863. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12864. }
  12865. return error;
  12866. }
  12867. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12868. void
  12869. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12870. struct intel_display_error_state *error)
  12871. {
  12872. struct drm_i915_private *dev_priv = m->i915;
  12873. int i;
  12874. if (!error)
  12875. return;
  12876. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12877. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12878. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12879. error->power_well_driver);
  12880. for_each_pipe(dev_priv, i) {
  12881. err_printf(m, "Pipe [%d]:\n", i);
  12882. err_printf(m, " Power: %s\n",
  12883. onoff(error->pipe[i].power_domain_on));
  12884. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12885. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12886. err_printf(m, "Plane [%d]:\n", i);
  12887. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12888. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12889. if (INTEL_GEN(dev_priv) <= 3) {
  12890. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12891. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12892. }
  12893. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12894. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12895. if (INTEL_GEN(dev_priv) >= 4) {
  12896. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12897. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12898. }
  12899. err_printf(m, "Cursor [%d]:\n", i);
  12900. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12901. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12902. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12903. }
  12904. for (i = 0; i < error->num_transcoders; i++) {
  12905. err_printf(m, "CPU transcoder: %s\n",
  12906. transcoder_name(error->transcoder[i].cpu_transcoder));
  12907. err_printf(m, " Power: %s\n",
  12908. onoff(error->transcoder[i].power_domain_on));
  12909. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12910. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12911. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12912. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12913. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12914. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12915. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12916. }
  12917. }
  12918. #endif