intel_display.c 458 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint32_t skl_pri_planar_formats[] = {
  86. DRM_FORMAT_C8,
  87. DRM_FORMAT_RGB565,
  88. DRM_FORMAT_XRGB8888,
  89. DRM_FORMAT_XBGR8888,
  90. DRM_FORMAT_ARGB8888,
  91. DRM_FORMAT_ABGR8888,
  92. DRM_FORMAT_XRGB2101010,
  93. DRM_FORMAT_XBGR2101010,
  94. DRM_FORMAT_YUYV,
  95. DRM_FORMAT_YVYU,
  96. DRM_FORMAT_UYVY,
  97. DRM_FORMAT_VYUY,
  98. DRM_FORMAT_NV12,
  99. };
  100. static const uint64_t skl_format_modifiers_noccs[] = {
  101. I915_FORMAT_MOD_Yf_TILED,
  102. I915_FORMAT_MOD_Y_TILED,
  103. I915_FORMAT_MOD_X_TILED,
  104. DRM_FORMAT_MOD_LINEAR,
  105. DRM_FORMAT_MOD_INVALID
  106. };
  107. static const uint64_t skl_format_modifiers_ccs[] = {
  108. I915_FORMAT_MOD_Yf_TILED_CCS,
  109. I915_FORMAT_MOD_Y_TILED_CCS,
  110. I915_FORMAT_MOD_Yf_TILED,
  111. I915_FORMAT_MOD_Y_TILED,
  112. I915_FORMAT_MOD_X_TILED,
  113. DRM_FORMAT_MOD_LINEAR,
  114. DRM_FORMAT_MOD_INVALID
  115. };
  116. /* Cursor formats */
  117. static const uint32_t intel_cursor_formats[] = {
  118. DRM_FORMAT_ARGB8888,
  119. };
  120. static const uint64_t cursor_format_modifiers[] = {
  121. DRM_FORMAT_MOD_LINEAR,
  122. DRM_FORMAT_MOD_INVALID
  123. };
  124. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  125. struct intel_crtc_state *pipe_config);
  126. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  127. struct intel_crtc_state *pipe_config);
  128. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  129. struct drm_i915_gem_object *obj,
  130. struct drm_mode_fb_cmd2 *mode_cmd);
  131. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  132. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  133. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  134. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  135. struct intel_link_m_n *m_n,
  136. struct intel_link_m_n *m2_n2);
  137. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  138. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  139. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  140. static void vlv_prepare_pll(struct intel_crtc *crtc,
  141. const struct intel_crtc_state *pipe_config);
  142. static void chv_prepare_pll(struct intel_crtc *crtc,
  143. const struct intel_crtc_state *pipe_config);
  144. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  145. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  146. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  147. struct intel_crtc_state *crtc_state);
  148. static void skylake_pfit_enable(struct intel_crtc *crtc);
  149. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  150. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  151. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  152. struct drm_modeset_acquire_ctx *ctx);
  153. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  154. struct intel_limit {
  155. struct {
  156. int min, max;
  157. } dot, vco, n, m, m1, m2, p, p1;
  158. struct {
  159. int dot_limit;
  160. int p2_slow, p2_fast;
  161. } p2;
  162. };
  163. /* returns HPLL frequency in kHz */
  164. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  165. {
  166. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  167. /* Obtain SKU information */
  168. mutex_lock(&dev_priv->sb_lock);
  169. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  170. CCK_FUSE_HPLL_FREQ_MASK;
  171. mutex_unlock(&dev_priv->sb_lock);
  172. return vco_freq[hpll_freq] * 1000;
  173. }
  174. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  175. const char *name, u32 reg, int ref_freq)
  176. {
  177. u32 val;
  178. int divider;
  179. mutex_lock(&dev_priv->sb_lock);
  180. val = vlv_cck_read(dev_priv, reg);
  181. mutex_unlock(&dev_priv->sb_lock);
  182. divider = val & CCK_FREQUENCY_VALUES;
  183. WARN((val & CCK_FREQUENCY_STATUS) !=
  184. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  185. "%s change in progress\n", name);
  186. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  187. }
  188. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  189. const char *name, u32 reg)
  190. {
  191. if (dev_priv->hpll_freq == 0)
  192. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  193. return vlv_get_cck_clock(dev_priv, name, reg,
  194. dev_priv->hpll_freq);
  195. }
  196. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  197. {
  198. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  199. return;
  200. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  201. CCK_CZ_CLOCK_CONTROL);
  202. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  203. }
  204. static inline u32 /* units of 100MHz */
  205. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  206. const struct intel_crtc_state *pipe_config)
  207. {
  208. if (HAS_DDI(dev_priv))
  209. return pipe_config->port_clock; /* SPLL */
  210. else
  211. return dev_priv->fdi_pll_freq;
  212. }
  213. static const struct intel_limit intel_limits_i8xx_dac = {
  214. .dot = { .min = 25000, .max = 350000 },
  215. .vco = { .min = 908000, .max = 1512000 },
  216. .n = { .min = 2, .max = 16 },
  217. .m = { .min = 96, .max = 140 },
  218. .m1 = { .min = 18, .max = 26 },
  219. .m2 = { .min = 6, .max = 16 },
  220. .p = { .min = 4, .max = 128 },
  221. .p1 = { .min = 2, .max = 33 },
  222. .p2 = { .dot_limit = 165000,
  223. .p2_slow = 4, .p2_fast = 2 },
  224. };
  225. static const struct intel_limit intel_limits_i8xx_dvo = {
  226. .dot = { .min = 25000, .max = 350000 },
  227. .vco = { .min = 908000, .max = 1512000 },
  228. .n = { .min = 2, .max = 16 },
  229. .m = { .min = 96, .max = 140 },
  230. .m1 = { .min = 18, .max = 26 },
  231. .m2 = { .min = 6, .max = 16 },
  232. .p = { .min = 4, .max = 128 },
  233. .p1 = { .min = 2, .max = 33 },
  234. .p2 = { .dot_limit = 165000,
  235. .p2_slow = 4, .p2_fast = 4 },
  236. };
  237. static const struct intel_limit intel_limits_i8xx_lvds = {
  238. .dot = { .min = 25000, .max = 350000 },
  239. .vco = { .min = 908000, .max = 1512000 },
  240. .n = { .min = 2, .max = 16 },
  241. .m = { .min = 96, .max = 140 },
  242. .m1 = { .min = 18, .max = 26 },
  243. .m2 = { .min = 6, .max = 16 },
  244. .p = { .min = 4, .max = 128 },
  245. .p1 = { .min = 1, .max = 6 },
  246. .p2 = { .dot_limit = 165000,
  247. .p2_slow = 14, .p2_fast = 7 },
  248. };
  249. static const struct intel_limit intel_limits_i9xx_sdvo = {
  250. .dot = { .min = 20000, .max = 400000 },
  251. .vco = { .min = 1400000, .max = 2800000 },
  252. .n = { .min = 1, .max = 6 },
  253. .m = { .min = 70, .max = 120 },
  254. .m1 = { .min = 8, .max = 18 },
  255. .m2 = { .min = 3, .max = 7 },
  256. .p = { .min = 5, .max = 80 },
  257. .p1 = { .min = 1, .max = 8 },
  258. .p2 = { .dot_limit = 200000,
  259. .p2_slow = 10, .p2_fast = 5 },
  260. };
  261. static const struct intel_limit intel_limits_i9xx_lvds = {
  262. .dot = { .min = 20000, .max = 400000 },
  263. .vco = { .min = 1400000, .max = 2800000 },
  264. .n = { .min = 1, .max = 6 },
  265. .m = { .min = 70, .max = 120 },
  266. .m1 = { .min = 8, .max = 18 },
  267. .m2 = { .min = 3, .max = 7 },
  268. .p = { .min = 7, .max = 98 },
  269. .p1 = { .min = 1, .max = 8 },
  270. .p2 = { .dot_limit = 112000,
  271. .p2_slow = 14, .p2_fast = 7 },
  272. };
  273. static const struct intel_limit intel_limits_g4x_sdvo = {
  274. .dot = { .min = 25000, .max = 270000 },
  275. .vco = { .min = 1750000, .max = 3500000},
  276. .n = { .min = 1, .max = 4 },
  277. .m = { .min = 104, .max = 138 },
  278. .m1 = { .min = 17, .max = 23 },
  279. .m2 = { .min = 5, .max = 11 },
  280. .p = { .min = 10, .max = 30 },
  281. .p1 = { .min = 1, .max = 3},
  282. .p2 = { .dot_limit = 270000,
  283. .p2_slow = 10,
  284. .p2_fast = 10
  285. },
  286. };
  287. static const struct intel_limit intel_limits_g4x_hdmi = {
  288. .dot = { .min = 22000, .max = 400000 },
  289. .vco = { .min = 1750000, .max = 3500000},
  290. .n = { .min = 1, .max = 4 },
  291. .m = { .min = 104, .max = 138 },
  292. .m1 = { .min = 16, .max = 23 },
  293. .m2 = { .min = 5, .max = 11 },
  294. .p = { .min = 5, .max = 80 },
  295. .p1 = { .min = 1, .max = 8},
  296. .p2 = { .dot_limit = 165000,
  297. .p2_slow = 10, .p2_fast = 5 },
  298. };
  299. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  300. .dot = { .min = 20000, .max = 115000 },
  301. .vco = { .min = 1750000, .max = 3500000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 104, .max = 138 },
  304. .m1 = { .min = 17, .max = 23 },
  305. .m2 = { .min = 5, .max = 11 },
  306. .p = { .min = 28, .max = 112 },
  307. .p1 = { .min = 2, .max = 8 },
  308. .p2 = { .dot_limit = 0,
  309. .p2_slow = 14, .p2_fast = 14
  310. },
  311. };
  312. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  313. .dot = { .min = 80000, .max = 224000 },
  314. .vco = { .min = 1750000, .max = 3500000 },
  315. .n = { .min = 1, .max = 3 },
  316. .m = { .min = 104, .max = 138 },
  317. .m1 = { .min = 17, .max = 23 },
  318. .m2 = { .min = 5, .max = 11 },
  319. .p = { .min = 14, .max = 42 },
  320. .p1 = { .min = 2, .max = 6 },
  321. .p2 = { .dot_limit = 0,
  322. .p2_slow = 7, .p2_fast = 7
  323. },
  324. };
  325. static const struct intel_limit intel_limits_pineview_sdvo = {
  326. .dot = { .min = 20000, .max = 400000},
  327. .vco = { .min = 1700000, .max = 3500000 },
  328. /* Pineview's Ncounter is a ring counter */
  329. .n = { .min = 3, .max = 6 },
  330. .m = { .min = 2, .max = 256 },
  331. /* Pineview only has one combined m divider, which we treat as m2. */
  332. .m1 = { .min = 0, .max = 0 },
  333. .m2 = { .min = 0, .max = 254 },
  334. .p = { .min = 5, .max = 80 },
  335. .p1 = { .min = 1, .max = 8 },
  336. .p2 = { .dot_limit = 200000,
  337. .p2_slow = 10, .p2_fast = 5 },
  338. };
  339. static const struct intel_limit intel_limits_pineview_lvds = {
  340. .dot = { .min = 20000, .max = 400000 },
  341. .vco = { .min = 1700000, .max = 3500000 },
  342. .n = { .min = 3, .max = 6 },
  343. .m = { .min = 2, .max = 256 },
  344. .m1 = { .min = 0, .max = 0 },
  345. .m2 = { .min = 0, .max = 254 },
  346. .p = { .min = 7, .max = 112 },
  347. .p1 = { .min = 1, .max = 8 },
  348. .p2 = { .dot_limit = 112000,
  349. .p2_slow = 14, .p2_fast = 14 },
  350. };
  351. /* Ironlake / Sandybridge
  352. *
  353. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  354. * the range value for them is (actual_value - 2).
  355. */
  356. static const struct intel_limit intel_limits_ironlake_dac = {
  357. .dot = { .min = 25000, .max = 350000 },
  358. .vco = { .min = 1760000, .max = 3510000 },
  359. .n = { .min = 1, .max = 5 },
  360. .m = { .min = 79, .max = 127 },
  361. .m1 = { .min = 12, .max = 22 },
  362. .m2 = { .min = 5, .max = 9 },
  363. .p = { .min = 5, .max = 80 },
  364. .p1 = { .min = 1, .max = 8 },
  365. .p2 = { .dot_limit = 225000,
  366. .p2_slow = 10, .p2_fast = 5 },
  367. };
  368. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  369. .dot = { .min = 25000, .max = 350000 },
  370. .vco = { .min = 1760000, .max = 3510000 },
  371. .n = { .min = 1, .max = 3 },
  372. .m = { .min = 79, .max = 118 },
  373. .m1 = { .min = 12, .max = 22 },
  374. .m2 = { .min = 5, .max = 9 },
  375. .p = { .min = 28, .max = 112 },
  376. .p1 = { .min = 2, .max = 8 },
  377. .p2 = { .dot_limit = 225000,
  378. .p2_slow = 14, .p2_fast = 14 },
  379. };
  380. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  381. .dot = { .min = 25000, .max = 350000 },
  382. .vco = { .min = 1760000, .max = 3510000 },
  383. .n = { .min = 1, .max = 3 },
  384. .m = { .min = 79, .max = 127 },
  385. .m1 = { .min = 12, .max = 22 },
  386. .m2 = { .min = 5, .max = 9 },
  387. .p = { .min = 14, .max = 56 },
  388. .p1 = { .min = 2, .max = 8 },
  389. .p2 = { .dot_limit = 225000,
  390. .p2_slow = 7, .p2_fast = 7 },
  391. };
  392. /* LVDS 100mhz refclk limits. */
  393. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  394. .dot = { .min = 25000, .max = 350000 },
  395. .vco = { .min = 1760000, .max = 3510000 },
  396. .n = { .min = 1, .max = 2 },
  397. .m = { .min = 79, .max = 126 },
  398. .m1 = { .min = 12, .max = 22 },
  399. .m2 = { .min = 5, .max = 9 },
  400. .p = { .min = 28, .max = 112 },
  401. .p1 = { .min = 2, .max = 8 },
  402. .p2 = { .dot_limit = 225000,
  403. .p2_slow = 14, .p2_fast = 14 },
  404. };
  405. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  406. .dot = { .min = 25000, .max = 350000 },
  407. .vco = { .min = 1760000, .max = 3510000 },
  408. .n = { .min = 1, .max = 3 },
  409. .m = { .min = 79, .max = 126 },
  410. .m1 = { .min = 12, .max = 22 },
  411. .m2 = { .min = 5, .max = 9 },
  412. .p = { .min = 14, .max = 42 },
  413. .p1 = { .min = 2, .max = 6 },
  414. .p2 = { .dot_limit = 225000,
  415. .p2_slow = 7, .p2_fast = 7 },
  416. };
  417. static const struct intel_limit intel_limits_vlv = {
  418. /*
  419. * These are the data rate limits (measured in fast clocks)
  420. * since those are the strictest limits we have. The fast
  421. * clock and actual rate limits are more relaxed, so checking
  422. * them would make no difference.
  423. */
  424. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  425. .vco = { .min = 4000000, .max = 6000000 },
  426. .n = { .min = 1, .max = 7 },
  427. .m1 = { .min = 2, .max = 3 },
  428. .m2 = { .min = 11, .max = 156 },
  429. .p1 = { .min = 2, .max = 3 },
  430. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  431. };
  432. static const struct intel_limit intel_limits_chv = {
  433. /*
  434. * These are the data rate limits (measured in fast clocks)
  435. * since those are the strictest limits we have. The fast
  436. * clock and actual rate limits are more relaxed, so checking
  437. * them would make no difference.
  438. */
  439. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  440. .vco = { .min = 4800000, .max = 6480000 },
  441. .n = { .min = 1, .max = 1 },
  442. .m1 = { .min = 2, .max = 2 },
  443. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  444. .p1 = { .min = 2, .max = 4 },
  445. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  446. };
  447. static const struct intel_limit intel_limits_bxt = {
  448. /* FIXME: find real dot limits */
  449. .dot = { .min = 0, .max = INT_MAX },
  450. .vco = { .min = 4800000, .max = 6700000 },
  451. .n = { .min = 1, .max = 1 },
  452. .m1 = { .min = 2, .max = 2 },
  453. /* FIXME: find real m2 limits */
  454. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  455. .p1 = { .min = 2, .max = 4 },
  456. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  457. };
  458. static void
  459. skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
  460. {
  461. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  462. return;
  463. if (enable)
  464. I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
  465. else
  466. I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
  467. }
  468. static void
  469. skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
  470. {
  471. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  472. return;
  473. if (enable)
  474. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  475. DUPS1_GATING_DIS | DUPS2_GATING_DIS);
  476. else
  477. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  478. I915_READ(CLKGATE_DIS_PSL(pipe)) &
  479. ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
  480. }
  481. static bool
  482. needs_modeset(const struct drm_crtc_state *state)
  483. {
  484. return drm_atomic_crtc_needs_modeset(state);
  485. }
  486. /*
  487. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  488. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  489. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  490. * The helpers' return value is the rate of the clock that is fed to the
  491. * display engine's pipe which can be the above fast dot clock rate or a
  492. * divided-down version of it.
  493. */
  494. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  495. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  496. {
  497. clock->m = clock->m2 + 2;
  498. clock->p = clock->p1 * clock->p2;
  499. if (WARN_ON(clock->n == 0 || clock->p == 0))
  500. return 0;
  501. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  502. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  503. return clock->dot;
  504. }
  505. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  506. {
  507. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  508. }
  509. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  510. {
  511. clock->m = i9xx_dpll_compute_m(clock);
  512. clock->p = clock->p1 * clock->p2;
  513. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  514. return 0;
  515. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  516. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  517. return clock->dot;
  518. }
  519. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  520. {
  521. clock->m = clock->m1 * clock->m2;
  522. clock->p = clock->p1 * clock->p2;
  523. if (WARN_ON(clock->n == 0 || clock->p == 0))
  524. return 0;
  525. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  526. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  527. return clock->dot / 5;
  528. }
  529. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  530. {
  531. clock->m = clock->m1 * clock->m2;
  532. clock->p = clock->p1 * clock->p2;
  533. if (WARN_ON(clock->n == 0 || clock->p == 0))
  534. return 0;
  535. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  536. clock->n << 22);
  537. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  538. return clock->dot / 5;
  539. }
  540. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  541. /*
  542. * Returns whether the given set of divisors are valid for a given refclk with
  543. * the given connectors.
  544. */
  545. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  546. const struct intel_limit *limit,
  547. const struct dpll *clock)
  548. {
  549. if (clock->n < limit->n.min || limit->n.max < clock->n)
  550. INTELPllInvalid("n out of range\n");
  551. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  552. INTELPllInvalid("p1 out of range\n");
  553. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  554. INTELPllInvalid("m2 out of range\n");
  555. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  556. INTELPllInvalid("m1 out of range\n");
  557. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  558. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  559. if (clock->m1 <= clock->m2)
  560. INTELPllInvalid("m1 <= m2\n");
  561. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  562. !IS_GEN9_LP(dev_priv)) {
  563. if (clock->p < limit->p.min || limit->p.max < clock->p)
  564. INTELPllInvalid("p out of range\n");
  565. if (clock->m < limit->m.min || limit->m.max < clock->m)
  566. INTELPllInvalid("m out of range\n");
  567. }
  568. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  569. INTELPllInvalid("vco out of range\n");
  570. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  571. * connector, etc., rather than just a single range.
  572. */
  573. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  574. INTELPllInvalid("dot out of range\n");
  575. return true;
  576. }
  577. static int
  578. i9xx_select_p2_div(const struct intel_limit *limit,
  579. const struct intel_crtc_state *crtc_state,
  580. int target)
  581. {
  582. struct drm_device *dev = crtc_state->base.crtc->dev;
  583. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  584. /*
  585. * For LVDS just rely on its current settings for dual-channel.
  586. * We haven't figured out how to reliably set up different
  587. * single/dual channel state, if we even can.
  588. */
  589. if (intel_is_dual_link_lvds(dev))
  590. return limit->p2.p2_fast;
  591. else
  592. return limit->p2.p2_slow;
  593. } else {
  594. if (target < limit->p2.dot_limit)
  595. return limit->p2.p2_slow;
  596. else
  597. return limit->p2.p2_fast;
  598. }
  599. }
  600. /*
  601. * Returns a set of divisors for the desired target clock with the given
  602. * refclk, or FALSE. The returned values represent the clock equation:
  603. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  604. *
  605. * Target and reference clocks are specified in kHz.
  606. *
  607. * If match_clock is provided, then best_clock P divider must match the P
  608. * divider from @match_clock used for LVDS downclocking.
  609. */
  610. static bool
  611. i9xx_find_best_dpll(const struct intel_limit *limit,
  612. struct intel_crtc_state *crtc_state,
  613. int target, int refclk, struct dpll *match_clock,
  614. struct dpll *best_clock)
  615. {
  616. struct drm_device *dev = crtc_state->base.crtc->dev;
  617. struct dpll clock;
  618. int err = target;
  619. memset(best_clock, 0, sizeof(*best_clock));
  620. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  621. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  622. clock.m1++) {
  623. for (clock.m2 = limit->m2.min;
  624. clock.m2 <= limit->m2.max; clock.m2++) {
  625. if (clock.m2 >= clock.m1)
  626. break;
  627. for (clock.n = limit->n.min;
  628. clock.n <= limit->n.max; clock.n++) {
  629. for (clock.p1 = limit->p1.min;
  630. clock.p1 <= limit->p1.max; clock.p1++) {
  631. int this_err;
  632. i9xx_calc_dpll_params(refclk, &clock);
  633. if (!intel_PLL_is_valid(to_i915(dev),
  634. limit,
  635. &clock))
  636. continue;
  637. if (match_clock &&
  638. clock.p != match_clock->p)
  639. continue;
  640. this_err = abs(clock.dot - target);
  641. if (this_err < err) {
  642. *best_clock = clock;
  643. err = this_err;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. return (err != target);
  650. }
  651. /*
  652. * Returns a set of divisors for the desired target clock with the given
  653. * refclk, or FALSE. The returned values represent the clock equation:
  654. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  655. *
  656. * Target and reference clocks are specified in kHz.
  657. *
  658. * If match_clock is provided, then best_clock P divider must match the P
  659. * divider from @match_clock used for LVDS downclocking.
  660. */
  661. static bool
  662. pnv_find_best_dpll(const struct intel_limit *limit,
  663. struct intel_crtc_state *crtc_state,
  664. int target, int refclk, struct dpll *match_clock,
  665. struct dpll *best_clock)
  666. {
  667. struct drm_device *dev = crtc_state->base.crtc->dev;
  668. struct dpll clock;
  669. int err = target;
  670. memset(best_clock, 0, sizeof(*best_clock));
  671. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  672. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  673. clock.m1++) {
  674. for (clock.m2 = limit->m2.min;
  675. clock.m2 <= limit->m2.max; clock.m2++) {
  676. for (clock.n = limit->n.min;
  677. clock.n <= limit->n.max; clock.n++) {
  678. for (clock.p1 = limit->p1.min;
  679. clock.p1 <= limit->p1.max; clock.p1++) {
  680. int this_err;
  681. pnv_calc_dpll_params(refclk, &clock);
  682. if (!intel_PLL_is_valid(to_i915(dev),
  683. limit,
  684. &clock))
  685. continue;
  686. if (match_clock &&
  687. clock.p != match_clock->p)
  688. continue;
  689. this_err = abs(clock.dot - target);
  690. if (this_err < err) {
  691. *best_clock = clock;
  692. err = this_err;
  693. }
  694. }
  695. }
  696. }
  697. }
  698. return (err != target);
  699. }
  700. /*
  701. * Returns a set of divisors for the desired target clock with the given
  702. * refclk, or FALSE. The returned values represent the clock equation:
  703. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  704. *
  705. * Target and reference clocks are specified in kHz.
  706. *
  707. * If match_clock is provided, then best_clock P divider must match the P
  708. * divider from @match_clock used for LVDS downclocking.
  709. */
  710. static bool
  711. g4x_find_best_dpll(const struct intel_limit *limit,
  712. struct intel_crtc_state *crtc_state,
  713. int target, int refclk, struct dpll *match_clock,
  714. struct dpll *best_clock)
  715. {
  716. struct drm_device *dev = crtc_state->base.crtc->dev;
  717. struct dpll clock;
  718. int max_n;
  719. bool found = false;
  720. /* approximately equals target * 0.00585 */
  721. int err_most = (target >> 8) + (target >> 9);
  722. memset(best_clock, 0, sizeof(*best_clock));
  723. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  724. max_n = limit->n.max;
  725. /* based on hardware requirement, prefer smaller n to precision */
  726. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  727. /* based on hardware requirement, prefere larger m1,m2 */
  728. for (clock.m1 = limit->m1.max;
  729. clock.m1 >= limit->m1.min; clock.m1--) {
  730. for (clock.m2 = limit->m2.max;
  731. clock.m2 >= limit->m2.min; clock.m2--) {
  732. for (clock.p1 = limit->p1.max;
  733. clock.p1 >= limit->p1.min; clock.p1--) {
  734. int this_err;
  735. i9xx_calc_dpll_params(refclk, &clock);
  736. if (!intel_PLL_is_valid(to_i915(dev),
  737. limit,
  738. &clock))
  739. continue;
  740. this_err = abs(clock.dot - target);
  741. if (this_err < err_most) {
  742. *best_clock = clock;
  743. err_most = this_err;
  744. max_n = clock.n;
  745. found = true;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. return found;
  752. }
  753. /*
  754. * Check if the calculated PLL configuration is more optimal compared to the
  755. * best configuration and error found so far. Return the calculated error.
  756. */
  757. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  758. const struct dpll *calculated_clock,
  759. const struct dpll *best_clock,
  760. unsigned int best_error_ppm,
  761. unsigned int *error_ppm)
  762. {
  763. /*
  764. * For CHV ignore the error and consider only the P value.
  765. * Prefer a bigger P value based on HW requirements.
  766. */
  767. if (IS_CHERRYVIEW(to_i915(dev))) {
  768. *error_ppm = 0;
  769. return calculated_clock->p > best_clock->p;
  770. }
  771. if (WARN_ON_ONCE(!target_freq))
  772. return false;
  773. *error_ppm = div_u64(1000000ULL *
  774. abs(target_freq - calculated_clock->dot),
  775. target_freq);
  776. /*
  777. * Prefer a better P value over a better (smaller) error if the error
  778. * is small. Ensure this preference for future configurations too by
  779. * setting the error to 0.
  780. */
  781. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  782. *error_ppm = 0;
  783. return true;
  784. }
  785. return *error_ppm + 10 < best_error_ppm;
  786. }
  787. /*
  788. * Returns a set of divisors for the desired target clock with the given
  789. * refclk, or FALSE. The returned values represent the clock equation:
  790. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  791. */
  792. static bool
  793. vlv_find_best_dpll(const struct intel_limit *limit,
  794. struct intel_crtc_state *crtc_state,
  795. int target, int refclk, struct dpll *match_clock,
  796. struct dpll *best_clock)
  797. {
  798. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  799. struct drm_device *dev = crtc->base.dev;
  800. struct dpll clock;
  801. unsigned int bestppm = 1000000;
  802. /* min update 19.2 MHz */
  803. int max_n = min(limit->n.max, refclk / 19200);
  804. bool found = false;
  805. target *= 5; /* fast clock */
  806. memset(best_clock, 0, sizeof(*best_clock));
  807. /* based on hardware requirement, prefer smaller n to precision */
  808. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  809. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  810. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  811. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  812. clock.p = clock.p1 * clock.p2;
  813. /* based on hardware requirement, prefer bigger m1,m2 values */
  814. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  815. unsigned int ppm;
  816. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  817. refclk * clock.m1);
  818. vlv_calc_dpll_params(refclk, &clock);
  819. if (!intel_PLL_is_valid(to_i915(dev),
  820. limit,
  821. &clock))
  822. continue;
  823. if (!vlv_PLL_is_optimal(dev, target,
  824. &clock,
  825. best_clock,
  826. bestppm, &ppm))
  827. continue;
  828. *best_clock = clock;
  829. bestppm = ppm;
  830. found = true;
  831. }
  832. }
  833. }
  834. }
  835. return found;
  836. }
  837. /*
  838. * Returns a set of divisors for the desired target clock with the given
  839. * refclk, or FALSE. The returned values represent the clock equation:
  840. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  841. */
  842. static bool
  843. chv_find_best_dpll(const struct intel_limit *limit,
  844. struct intel_crtc_state *crtc_state,
  845. int target, int refclk, struct dpll *match_clock,
  846. struct dpll *best_clock)
  847. {
  848. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  849. struct drm_device *dev = crtc->base.dev;
  850. unsigned int best_error_ppm;
  851. struct dpll clock;
  852. uint64_t m2;
  853. int found = false;
  854. memset(best_clock, 0, sizeof(*best_clock));
  855. best_error_ppm = 1000000;
  856. /*
  857. * Based on hardware doc, the n always set to 1, and m1 always
  858. * set to 2. If requires to support 200Mhz refclk, we need to
  859. * revisit this because n may not 1 anymore.
  860. */
  861. clock.n = 1, clock.m1 = 2;
  862. target *= 5; /* fast clock */
  863. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  864. for (clock.p2 = limit->p2.p2_fast;
  865. clock.p2 >= limit->p2.p2_slow;
  866. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  867. unsigned int error_ppm;
  868. clock.p = clock.p1 * clock.p2;
  869. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  870. clock.n) << 22, refclk * clock.m1);
  871. if (m2 > INT_MAX/clock.m1)
  872. continue;
  873. clock.m2 = m2;
  874. chv_calc_dpll_params(refclk, &clock);
  875. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  876. continue;
  877. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  878. best_error_ppm, &error_ppm))
  879. continue;
  880. *best_clock = clock;
  881. best_error_ppm = error_ppm;
  882. found = true;
  883. }
  884. }
  885. return found;
  886. }
  887. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  888. struct dpll *best_clock)
  889. {
  890. int refclk = 100000;
  891. const struct intel_limit *limit = &intel_limits_bxt;
  892. return chv_find_best_dpll(limit, crtc_state,
  893. target_clock, refclk, NULL, best_clock);
  894. }
  895. bool intel_crtc_active(struct intel_crtc *crtc)
  896. {
  897. /* Be paranoid as we can arrive here with only partial
  898. * state retrieved from the hardware during setup.
  899. *
  900. * We can ditch the adjusted_mode.crtc_clock check as soon
  901. * as Haswell has gained clock readout/fastboot support.
  902. *
  903. * We can ditch the crtc->primary->state->fb check as soon as we can
  904. * properly reconstruct framebuffers.
  905. *
  906. * FIXME: The intel_crtc->active here should be switched to
  907. * crtc->state->active once we have proper CRTC states wired up
  908. * for atomic.
  909. */
  910. return crtc->active && crtc->base.primary->state->fb &&
  911. crtc->config->base.adjusted_mode.crtc_clock;
  912. }
  913. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  914. enum pipe pipe)
  915. {
  916. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  917. return crtc->config->cpu_transcoder;
  918. }
  919. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  920. enum pipe pipe)
  921. {
  922. i915_reg_t reg = PIPEDSL(pipe);
  923. u32 line1, line2;
  924. u32 line_mask;
  925. if (IS_GEN2(dev_priv))
  926. line_mask = DSL_LINEMASK_GEN2;
  927. else
  928. line_mask = DSL_LINEMASK_GEN3;
  929. line1 = I915_READ(reg) & line_mask;
  930. msleep(5);
  931. line2 = I915_READ(reg) & line_mask;
  932. return line1 != line2;
  933. }
  934. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  935. {
  936. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  937. enum pipe pipe = crtc->pipe;
  938. /* Wait for the display line to settle/start moving */
  939. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  940. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  941. pipe_name(pipe), onoff(state));
  942. }
  943. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  944. {
  945. wait_for_pipe_scanline_moving(crtc, false);
  946. }
  947. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  948. {
  949. wait_for_pipe_scanline_moving(crtc, true);
  950. }
  951. static void
  952. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  953. {
  954. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  955. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  956. if (INTEL_GEN(dev_priv) >= 4) {
  957. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  958. i915_reg_t reg = PIPECONF(cpu_transcoder);
  959. /* Wait for the Pipe State to go off */
  960. if (intel_wait_for_register(dev_priv,
  961. reg, I965_PIPECONF_ACTIVE, 0,
  962. 100))
  963. WARN(1, "pipe_off wait timed out\n");
  964. } else {
  965. intel_wait_for_pipe_scanline_stopped(crtc);
  966. }
  967. }
  968. /* Only for pre-ILK configs */
  969. void assert_pll(struct drm_i915_private *dev_priv,
  970. enum pipe pipe, bool state)
  971. {
  972. u32 val;
  973. bool cur_state;
  974. val = I915_READ(DPLL(pipe));
  975. cur_state = !!(val & DPLL_VCO_ENABLE);
  976. I915_STATE_WARN(cur_state != state,
  977. "PLL state assertion failure (expected %s, current %s)\n",
  978. onoff(state), onoff(cur_state));
  979. }
  980. /* XXX: the dsi pll is shared between MIPI DSI ports */
  981. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. mutex_lock(&dev_priv->sb_lock);
  986. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  987. mutex_unlock(&dev_priv->sb_lock);
  988. cur_state = val & DSI_PLL_VCO_EN;
  989. I915_STATE_WARN(cur_state != state,
  990. "DSI PLL state assertion failure (expected %s, current %s)\n",
  991. onoff(state), onoff(cur_state));
  992. }
  993. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  994. enum pipe pipe, bool state)
  995. {
  996. bool cur_state;
  997. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  998. pipe);
  999. if (HAS_DDI(dev_priv)) {
  1000. /* DDI does not have a specific FDI_TX register */
  1001. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1002. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1003. } else {
  1004. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1005. cur_state = !!(val & FDI_TX_ENABLE);
  1006. }
  1007. I915_STATE_WARN(cur_state != state,
  1008. "FDI TX state assertion failure (expected %s, current %s)\n",
  1009. onoff(state), onoff(cur_state));
  1010. }
  1011. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1012. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1013. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1014. enum pipe pipe, bool state)
  1015. {
  1016. u32 val;
  1017. bool cur_state;
  1018. val = I915_READ(FDI_RX_CTL(pipe));
  1019. cur_state = !!(val & FDI_RX_ENABLE);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "FDI RX state assertion failure (expected %s, current %s)\n",
  1022. onoff(state), onoff(cur_state));
  1023. }
  1024. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1025. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1026. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1027. enum pipe pipe)
  1028. {
  1029. u32 val;
  1030. /* ILK FDI PLL is always enabled */
  1031. if (IS_GEN5(dev_priv))
  1032. return;
  1033. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1034. if (HAS_DDI(dev_priv))
  1035. return;
  1036. val = I915_READ(FDI_TX_CTL(pipe));
  1037. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1038. }
  1039. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1040. enum pipe pipe, bool state)
  1041. {
  1042. u32 val;
  1043. bool cur_state;
  1044. val = I915_READ(FDI_RX_CTL(pipe));
  1045. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1046. I915_STATE_WARN(cur_state != state,
  1047. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1048. onoff(state), onoff(cur_state));
  1049. }
  1050. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1051. {
  1052. i915_reg_t pp_reg;
  1053. u32 val;
  1054. enum pipe panel_pipe = INVALID_PIPE;
  1055. bool locked = true;
  1056. if (WARN_ON(HAS_DDI(dev_priv)))
  1057. return;
  1058. if (HAS_PCH_SPLIT(dev_priv)) {
  1059. u32 port_sel;
  1060. pp_reg = PP_CONTROL(0);
  1061. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1062. switch (port_sel) {
  1063. case PANEL_PORT_SELECT_LVDS:
  1064. intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
  1065. break;
  1066. case PANEL_PORT_SELECT_DPA:
  1067. intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
  1068. break;
  1069. case PANEL_PORT_SELECT_DPC:
  1070. intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
  1071. break;
  1072. case PANEL_PORT_SELECT_DPD:
  1073. intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
  1074. break;
  1075. default:
  1076. MISSING_CASE(port_sel);
  1077. break;
  1078. }
  1079. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1080. /* presumably write lock depends on pipe, not port select */
  1081. pp_reg = PP_CONTROL(pipe);
  1082. panel_pipe = pipe;
  1083. } else {
  1084. u32 port_sel;
  1085. pp_reg = PP_CONTROL(0);
  1086. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1087. WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
  1088. intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
  1089. }
  1090. val = I915_READ(pp_reg);
  1091. if (!(val & PANEL_POWER_ON) ||
  1092. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1093. locked = false;
  1094. I915_STATE_WARN(panel_pipe == pipe && locked,
  1095. "panel assertion failure, pipe %c regs locked\n",
  1096. pipe_name(pipe));
  1097. }
  1098. void assert_pipe(struct drm_i915_private *dev_priv,
  1099. enum pipe pipe, bool state)
  1100. {
  1101. bool cur_state;
  1102. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1103. pipe);
  1104. enum intel_display_power_domain power_domain;
  1105. /* we keep both pipes enabled on 830 */
  1106. if (IS_I830(dev_priv))
  1107. state = true;
  1108. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1109. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1110. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1111. cur_state = !!(val & PIPECONF_ENABLE);
  1112. intel_display_power_put(dev_priv, power_domain);
  1113. } else {
  1114. cur_state = false;
  1115. }
  1116. I915_STATE_WARN(cur_state != state,
  1117. "pipe %c assertion failure (expected %s, current %s)\n",
  1118. pipe_name(pipe), onoff(state), onoff(cur_state));
  1119. }
  1120. static void assert_plane(struct intel_plane *plane, bool state)
  1121. {
  1122. enum pipe pipe;
  1123. bool cur_state;
  1124. cur_state = plane->get_hw_state(plane, &pipe);
  1125. I915_STATE_WARN(cur_state != state,
  1126. "%s assertion failure (expected %s, current %s)\n",
  1127. plane->base.name, onoff(state), onoff(cur_state));
  1128. }
  1129. #define assert_plane_enabled(p) assert_plane(p, true)
  1130. #define assert_plane_disabled(p) assert_plane(p, false)
  1131. static void assert_planes_disabled(struct intel_crtc *crtc)
  1132. {
  1133. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1134. struct intel_plane *plane;
  1135. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1136. assert_plane_disabled(plane);
  1137. }
  1138. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1139. {
  1140. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1141. drm_crtc_vblank_put(crtc);
  1142. }
  1143. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1144. enum pipe pipe)
  1145. {
  1146. u32 val;
  1147. bool enabled;
  1148. val = I915_READ(PCH_TRANSCONF(pipe));
  1149. enabled = !!(val & TRANS_ENABLE);
  1150. I915_STATE_WARN(enabled,
  1151. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1152. pipe_name(pipe));
  1153. }
  1154. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1155. enum pipe pipe, enum port port,
  1156. i915_reg_t dp_reg)
  1157. {
  1158. enum pipe port_pipe;
  1159. bool state;
  1160. state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
  1161. I915_STATE_WARN(state && port_pipe == pipe,
  1162. "PCH DP %c enabled on transcoder %c, should be disabled\n",
  1163. port_name(port), pipe_name(pipe));
  1164. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
  1165. "IBX PCH DP %c still using transcoder B\n",
  1166. port_name(port));
  1167. }
  1168. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1169. enum pipe pipe, enum port port,
  1170. i915_reg_t hdmi_reg)
  1171. {
  1172. enum pipe port_pipe;
  1173. bool state;
  1174. state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
  1175. I915_STATE_WARN(state && port_pipe == pipe,
  1176. "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
  1177. port_name(port), pipe_name(pipe));
  1178. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
  1179. "IBX PCH HDMI %c still using transcoder B\n",
  1180. port_name(port));
  1181. }
  1182. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1183. enum pipe pipe)
  1184. {
  1185. enum pipe port_pipe;
  1186. assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
  1187. assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
  1188. assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
  1189. I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
  1190. port_pipe == pipe,
  1191. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1192. pipe_name(pipe));
  1193. I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
  1194. port_pipe == pipe,
  1195. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1196. pipe_name(pipe));
  1197. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
  1198. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
  1199. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
  1200. }
  1201. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1202. const struct intel_crtc_state *pipe_config)
  1203. {
  1204. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1205. enum pipe pipe = crtc->pipe;
  1206. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1207. POSTING_READ(DPLL(pipe));
  1208. udelay(150);
  1209. if (intel_wait_for_register(dev_priv,
  1210. DPLL(pipe),
  1211. DPLL_LOCK_VLV,
  1212. DPLL_LOCK_VLV,
  1213. 1))
  1214. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1215. }
  1216. static void vlv_enable_pll(struct intel_crtc *crtc,
  1217. const struct intel_crtc_state *pipe_config)
  1218. {
  1219. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1220. enum pipe pipe = crtc->pipe;
  1221. assert_pipe_disabled(dev_priv, pipe);
  1222. /* PLL is protected by panel, make sure we can write it */
  1223. assert_panel_unlocked(dev_priv, pipe);
  1224. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1225. _vlv_enable_pll(crtc, pipe_config);
  1226. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1227. POSTING_READ(DPLL_MD(pipe));
  1228. }
  1229. static void _chv_enable_pll(struct intel_crtc *crtc,
  1230. const struct intel_crtc_state *pipe_config)
  1231. {
  1232. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1233. enum pipe pipe = crtc->pipe;
  1234. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1235. u32 tmp;
  1236. mutex_lock(&dev_priv->sb_lock);
  1237. /* Enable back the 10bit clock to display controller */
  1238. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1239. tmp |= DPIO_DCLKP_EN;
  1240. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1241. mutex_unlock(&dev_priv->sb_lock);
  1242. /*
  1243. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1244. */
  1245. udelay(1);
  1246. /* Enable PLL */
  1247. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1248. /* Check PLL is locked */
  1249. if (intel_wait_for_register(dev_priv,
  1250. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1251. 1))
  1252. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1253. }
  1254. static void chv_enable_pll(struct intel_crtc *crtc,
  1255. const struct intel_crtc_state *pipe_config)
  1256. {
  1257. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1258. enum pipe pipe = crtc->pipe;
  1259. assert_pipe_disabled(dev_priv, pipe);
  1260. /* PLL is protected by panel, make sure we can write it */
  1261. assert_panel_unlocked(dev_priv, pipe);
  1262. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1263. _chv_enable_pll(crtc, pipe_config);
  1264. if (pipe != PIPE_A) {
  1265. /*
  1266. * WaPixelRepeatModeFixForC0:chv
  1267. *
  1268. * DPLLCMD is AWOL. Use chicken bits to propagate
  1269. * the value from DPLLBMD to either pipe B or C.
  1270. */
  1271. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1272. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1273. I915_WRITE(CBR4_VLV, 0);
  1274. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1275. /*
  1276. * DPLLB VGA mode also seems to cause problems.
  1277. * We should always have it disabled.
  1278. */
  1279. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1280. } else {
  1281. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1282. POSTING_READ(DPLL_MD(pipe));
  1283. }
  1284. }
  1285. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1286. {
  1287. struct intel_crtc *crtc;
  1288. int count = 0;
  1289. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1290. count += crtc->base.state->active &&
  1291. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1292. }
  1293. return count;
  1294. }
  1295. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1296. const struct intel_crtc_state *crtc_state)
  1297. {
  1298. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1299. i915_reg_t reg = DPLL(crtc->pipe);
  1300. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1301. int i;
  1302. assert_pipe_disabled(dev_priv, crtc->pipe);
  1303. /* PLL is protected by panel, make sure we can write it */
  1304. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1305. assert_panel_unlocked(dev_priv, crtc->pipe);
  1306. /* Enable DVO 2x clock on both PLLs if necessary */
  1307. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1308. /*
  1309. * It appears to be important that we don't enable this
  1310. * for the current pipe before otherwise configuring the
  1311. * PLL. No idea how this should be handled if multiple
  1312. * DVO outputs are enabled simultaneosly.
  1313. */
  1314. dpll |= DPLL_DVO_2X_MODE;
  1315. I915_WRITE(DPLL(!crtc->pipe),
  1316. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1317. }
  1318. /*
  1319. * Apparently we need to have VGA mode enabled prior to changing
  1320. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1321. * dividers, even though the register value does change.
  1322. */
  1323. I915_WRITE(reg, 0);
  1324. I915_WRITE(reg, dpll);
  1325. /* Wait for the clocks to stabilize. */
  1326. POSTING_READ(reg);
  1327. udelay(150);
  1328. if (INTEL_GEN(dev_priv) >= 4) {
  1329. I915_WRITE(DPLL_MD(crtc->pipe),
  1330. crtc_state->dpll_hw_state.dpll_md);
  1331. } else {
  1332. /* The pixel multiplier can only be updated once the
  1333. * DPLL is enabled and the clocks are stable.
  1334. *
  1335. * So write it again.
  1336. */
  1337. I915_WRITE(reg, dpll);
  1338. }
  1339. /* We do this three times for luck */
  1340. for (i = 0; i < 3; i++) {
  1341. I915_WRITE(reg, dpll);
  1342. POSTING_READ(reg);
  1343. udelay(150); /* wait for warmup */
  1344. }
  1345. }
  1346. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1347. {
  1348. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1349. enum pipe pipe = crtc->pipe;
  1350. /* Disable DVO 2x clock on both PLLs if necessary */
  1351. if (IS_I830(dev_priv) &&
  1352. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1353. !intel_num_dvo_pipes(dev_priv)) {
  1354. I915_WRITE(DPLL(PIPE_B),
  1355. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1356. I915_WRITE(DPLL(PIPE_A),
  1357. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1358. }
  1359. /* Don't disable pipe or pipe PLLs if needed */
  1360. if (IS_I830(dev_priv))
  1361. return;
  1362. /* Make sure the pipe isn't still relying on us */
  1363. assert_pipe_disabled(dev_priv, pipe);
  1364. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1365. POSTING_READ(DPLL(pipe));
  1366. }
  1367. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1368. {
  1369. u32 val;
  1370. /* Make sure the pipe isn't still relying on us */
  1371. assert_pipe_disabled(dev_priv, pipe);
  1372. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1373. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1374. if (pipe != PIPE_A)
  1375. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1376. I915_WRITE(DPLL(pipe), val);
  1377. POSTING_READ(DPLL(pipe));
  1378. }
  1379. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1380. {
  1381. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1382. u32 val;
  1383. /* Make sure the pipe isn't still relying on us */
  1384. assert_pipe_disabled(dev_priv, pipe);
  1385. val = DPLL_SSC_REF_CLK_CHV |
  1386. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1387. if (pipe != PIPE_A)
  1388. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1389. I915_WRITE(DPLL(pipe), val);
  1390. POSTING_READ(DPLL(pipe));
  1391. mutex_lock(&dev_priv->sb_lock);
  1392. /* Disable 10bit clock to display controller */
  1393. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1394. val &= ~DPIO_DCLKP_EN;
  1395. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1396. mutex_unlock(&dev_priv->sb_lock);
  1397. }
  1398. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1399. struct intel_digital_port *dport,
  1400. unsigned int expected_mask)
  1401. {
  1402. u32 port_mask;
  1403. i915_reg_t dpll_reg;
  1404. switch (dport->base.port) {
  1405. case PORT_B:
  1406. port_mask = DPLL_PORTB_READY_MASK;
  1407. dpll_reg = DPLL(0);
  1408. break;
  1409. case PORT_C:
  1410. port_mask = DPLL_PORTC_READY_MASK;
  1411. dpll_reg = DPLL(0);
  1412. expected_mask <<= 4;
  1413. break;
  1414. case PORT_D:
  1415. port_mask = DPLL_PORTD_READY_MASK;
  1416. dpll_reg = DPIO_PHY_STATUS;
  1417. break;
  1418. default:
  1419. BUG();
  1420. }
  1421. if (intel_wait_for_register(dev_priv,
  1422. dpll_reg, port_mask, expected_mask,
  1423. 1000))
  1424. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1425. port_name(dport->base.port),
  1426. I915_READ(dpll_reg) & port_mask, expected_mask);
  1427. }
  1428. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1429. enum pipe pipe)
  1430. {
  1431. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1432. pipe);
  1433. i915_reg_t reg;
  1434. uint32_t val, pipeconf_val;
  1435. /* Make sure PCH DPLL is enabled */
  1436. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1437. /* FDI must be feeding us bits for PCH ports */
  1438. assert_fdi_tx_enabled(dev_priv, pipe);
  1439. assert_fdi_rx_enabled(dev_priv, pipe);
  1440. if (HAS_PCH_CPT(dev_priv)) {
  1441. /* Workaround: Set the timing override bit before enabling the
  1442. * pch transcoder. */
  1443. reg = TRANS_CHICKEN2(pipe);
  1444. val = I915_READ(reg);
  1445. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1446. I915_WRITE(reg, val);
  1447. }
  1448. reg = PCH_TRANSCONF(pipe);
  1449. val = I915_READ(reg);
  1450. pipeconf_val = I915_READ(PIPECONF(pipe));
  1451. if (HAS_PCH_IBX(dev_priv)) {
  1452. /*
  1453. * Make the BPC in transcoder be consistent with
  1454. * that in pipeconf reg. For HDMI we must use 8bpc
  1455. * here for both 8bpc and 12bpc.
  1456. */
  1457. val &= ~PIPECONF_BPC_MASK;
  1458. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1459. val |= PIPECONF_8BPC;
  1460. else
  1461. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1462. }
  1463. val &= ~TRANS_INTERLACE_MASK;
  1464. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1465. if (HAS_PCH_IBX(dev_priv) &&
  1466. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1467. val |= TRANS_LEGACY_INTERLACED_ILK;
  1468. else
  1469. val |= TRANS_INTERLACED;
  1470. else
  1471. val |= TRANS_PROGRESSIVE;
  1472. I915_WRITE(reg, val | TRANS_ENABLE);
  1473. if (intel_wait_for_register(dev_priv,
  1474. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1475. 100))
  1476. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1477. }
  1478. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1479. enum transcoder cpu_transcoder)
  1480. {
  1481. u32 val, pipeconf_val;
  1482. /* FDI must be feeding us bits for PCH ports */
  1483. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1484. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1485. /* Workaround: set timing override bit. */
  1486. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1487. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1488. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1489. val = TRANS_ENABLE;
  1490. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1491. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1492. PIPECONF_INTERLACED_ILK)
  1493. val |= TRANS_INTERLACED;
  1494. else
  1495. val |= TRANS_PROGRESSIVE;
  1496. I915_WRITE(LPT_TRANSCONF, val);
  1497. if (intel_wait_for_register(dev_priv,
  1498. LPT_TRANSCONF,
  1499. TRANS_STATE_ENABLE,
  1500. TRANS_STATE_ENABLE,
  1501. 100))
  1502. DRM_ERROR("Failed to enable PCH transcoder\n");
  1503. }
  1504. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1505. enum pipe pipe)
  1506. {
  1507. i915_reg_t reg;
  1508. uint32_t val;
  1509. /* FDI relies on the transcoder */
  1510. assert_fdi_tx_disabled(dev_priv, pipe);
  1511. assert_fdi_rx_disabled(dev_priv, pipe);
  1512. /* Ports must be off as well */
  1513. assert_pch_ports_disabled(dev_priv, pipe);
  1514. reg = PCH_TRANSCONF(pipe);
  1515. val = I915_READ(reg);
  1516. val &= ~TRANS_ENABLE;
  1517. I915_WRITE(reg, val);
  1518. /* wait for PCH transcoder off, transcoder state */
  1519. if (intel_wait_for_register(dev_priv,
  1520. reg, TRANS_STATE_ENABLE, 0,
  1521. 50))
  1522. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1523. if (HAS_PCH_CPT(dev_priv)) {
  1524. /* Workaround: Clear the timing override chicken bit again. */
  1525. reg = TRANS_CHICKEN2(pipe);
  1526. val = I915_READ(reg);
  1527. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1528. I915_WRITE(reg, val);
  1529. }
  1530. }
  1531. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1532. {
  1533. u32 val;
  1534. val = I915_READ(LPT_TRANSCONF);
  1535. val &= ~TRANS_ENABLE;
  1536. I915_WRITE(LPT_TRANSCONF, val);
  1537. /* wait for PCH transcoder off, transcoder state */
  1538. if (intel_wait_for_register(dev_priv,
  1539. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1540. 50))
  1541. DRM_ERROR("Failed to disable PCH transcoder\n");
  1542. /* Workaround: clear timing override bit. */
  1543. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1544. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1545. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1546. }
  1547. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1548. {
  1549. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1550. if (HAS_PCH_LPT(dev_priv))
  1551. return PIPE_A;
  1552. else
  1553. return crtc->pipe;
  1554. }
  1555. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1556. {
  1557. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1558. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1559. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1560. enum pipe pipe = crtc->pipe;
  1561. i915_reg_t reg;
  1562. u32 val;
  1563. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1564. assert_planes_disabled(crtc);
  1565. /*
  1566. * A pipe without a PLL won't actually be able to drive bits from
  1567. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1568. * need the check.
  1569. */
  1570. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1571. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1572. assert_dsi_pll_enabled(dev_priv);
  1573. else
  1574. assert_pll_enabled(dev_priv, pipe);
  1575. } else {
  1576. if (new_crtc_state->has_pch_encoder) {
  1577. /* if driving the PCH, we need FDI enabled */
  1578. assert_fdi_rx_pll_enabled(dev_priv,
  1579. intel_crtc_pch_transcoder(crtc));
  1580. assert_fdi_tx_pll_enabled(dev_priv,
  1581. (enum pipe) cpu_transcoder);
  1582. }
  1583. /* FIXME: assert CPU port conditions for SNB+ */
  1584. }
  1585. reg = PIPECONF(cpu_transcoder);
  1586. val = I915_READ(reg);
  1587. if (val & PIPECONF_ENABLE) {
  1588. /* we keep both pipes enabled on 830 */
  1589. WARN_ON(!IS_I830(dev_priv));
  1590. return;
  1591. }
  1592. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1593. POSTING_READ(reg);
  1594. /*
  1595. * Until the pipe starts PIPEDSL reads will return a stale value,
  1596. * which causes an apparent vblank timestamp jump when PIPEDSL
  1597. * resets to its proper value. That also messes up the frame count
  1598. * when it's derived from the timestamps. So let's wait for the
  1599. * pipe to start properly before we call drm_crtc_vblank_on()
  1600. */
  1601. if (dev_priv->drm.max_vblank_count == 0)
  1602. intel_wait_for_pipe_scanline_moving(crtc);
  1603. }
  1604. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1605. {
  1606. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1607. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1608. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1609. enum pipe pipe = crtc->pipe;
  1610. i915_reg_t reg;
  1611. u32 val;
  1612. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1613. /*
  1614. * Make sure planes won't keep trying to pump pixels to us,
  1615. * or we might hang the display.
  1616. */
  1617. assert_planes_disabled(crtc);
  1618. reg = PIPECONF(cpu_transcoder);
  1619. val = I915_READ(reg);
  1620. if ((val & PIPECONF_ENABLE) == 0)
  1621. return;
  1622. /*
  1623. * Double wide has implications for planes
  1624. * so best keep it disabled when not needed.
  1625. */
  1626. if (old_crtc_state->double_wide)
  1627. val &= ~PIPECONF_DOUBLE_WIDE;
  1628. /* Don't disable pipe or pipe PLLs if needed */
  1629. if (!IS_I830(dev_priv))
  1630. val &= ~PIPECONF_ENABLE;
  1631. I915_WRITE(reg, val);
  1632. if ((val & PIPECONF_ENABLE) == 0)
  1633. intel_wait_for_pipe_off(old_crtc_state);
  1634. }
  1635. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1636. {
  1637. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1638. }
  1639. static unsigned int
  1640. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1641. {
  1642. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1643. unsigned int cpp = fb->format->cpp[plane];
  1644. switch (fb->modifier) {
  1645. case DRM_FORMAT_MOD_LINEAR:
  1646. return cpp;
  1647. case I915_FORMAT_MOD_X_TILED:
  1648. if (IS_GEN2(dev_priv))
  1649. return 128;
  1650. else
  1651. return 512;
  1652. case I915_FORMAT_MOD_Y_TILED_CCS:
  1653. if (plane == 1)
  1654. return 128;
  1655. /* fall through */
  1656. case I915_FORMAT_MOD_Y_TILED:
  1657. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1658. return 128;
  1659. else
  1660. return 512;
  1661. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1662. if (plane == 1)
  1663. return 128;
  1664. /* fall through */
  1665. case I915_FORMAT_MOD_Yf_TILED:
  1666. switch (cpp) {
  1667. case 1:
  1668. return 64;
  1669. case 2:
  1670. case 4:
  1671. return 128;
  1672. case 8:
  1673. case 16:
  1674. return 256;
  1675. default:
  1676. MISSING_CASE(cpp);
  1677. return cpp;
  1678. }
  1679. break;
  1680. default:
  1681. MISSING_CASE(fb->modifier);
  1682. return cpp;
  1683. }
  1684. }
  1685. static unsigned int
  1686. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1687. {
  1688. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1689. return 1;
  1690. else
  1691. return intel_tile_size(to_i915(fb->dev)) /
  1692. intel_tile_width_bytes(fb, plane);
  1693. }
  1694. /* Return the tile dimensions in pixel units */
  1695. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1696. unsigned int *tile_width,
  1697. unsigned int *tile_height)
  1698. {
  1699. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1700. unsigned int cpp = fb->format->cpp[plane];
  1701. *tile_width = tile_width_bytes / cpp;
  1702. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1703. }
  1704. unsigned int
  1705. intel_fb_align_height(const struct drm_framebuffer *fb,
  1706. int plane, unsigned int height)
  1707. {
  1708. unsigned int tile_height = intel_tile_height(fb, plane);
  1709. return ALIGN(height, tile_height);
  1710. }
  1711. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1712. {
  1713. unsigned int size = 0;
  1714. int i;
  1715. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1716. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1717. return size;
  1718. }
  1719. static void
  1720. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1721. const struct drm_framebuffer *fb,
  1722. unsigned int rotation)
  1723. {
  1724. view->type = I915_GGTT_VIEW_NORMAL;
  1725. if (drm_rotation_90_or_270(rotation)) {
  1726. view->type = I915_GGTT_VIEW_ROTATED;
  1727. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1728. }
  1729. }
  1730. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1731. {
  1732. if (IS_I830(dev_priv))
  1733. return 16 * 1024;
  1734. else if (IS_I85X(dev_priv))
  1735. return 256;
  1736. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1737. return 32;
  1738. else
  1739. return 4 * 1024;
  1740. }
  1741. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1742. {
  1743. if (INTEL_GEN(dev_priv) >= 9)
  1744. return 256 * 1024;
  1745. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1746. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1747. return 128 * 1024;
  1748. else if (INTEL_GEN(dev_priv) >= 4)
  1749. return 4 * 1024;
  1750. else
  1751. return 0;
  1752. }
  1753. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1754. int plane)
  1755. {
  1756. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1757. /* AUX_DIST needs only 4K alignment */
  1758. if (plane == 1)
  1759. return 4096;
  1760. switch (fb->modifier) {
  1761. case DRM_FORMAT_MOD_LINEAR:
  1762. return intel_linear_alignment(dev_priv);
  1763. case I915_FORMAT_MOD_X_TILED:
  1764. if (INTEL_GEN(dev_priv) >= 9)
  1765. return 256 * 1024;
  1766. return 0;
  1767. case I915_FORMAT_MOD_Y_TILED_CCS:
  1768. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1769. case I915_FORMAT_MOD_Y_TILED:
  1770. case I915_FORMAT_MOD_Yf_TILED:
  1771. return 1 * 1024 * 1024;
  1772. default:
  1773. MISSING_CASE(fb->modifier);
  1774. return 0;
  1775. }
  1776. }
  1777. static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
  1778. {
  1779. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1780. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1781. return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
  1782. }
  1783. struct i915_vma *
  1784. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1785. unsigned int rotation,
  1786. bool uses_fence,
  1787. unsigned long *out_flags)
  1788. {
  1789. struct drm_device *dev = fb->dev;
  1790. struct drm_i915_private *dev_priv = to_i915(dev);
  1791. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1792. struct i915_ggtt_view view;
  1793. struct i915_vma *vma;
  1794. unsigned int pinctl;
  1795. u32 alignment;
  1796. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1797. alignment = intel_surf_alignment(fb, 0);
  1798. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1799. /* Note that the w/a also requires 64 PTE of padding following the
  1800. * bo. We currently fill all unused PTE with the shadow page and so
  1801. * we should always have valid PTE following the scanout preventing
  1802. * the VT-d warning.
  1803. */
  1804. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1805. alignment = 256 * 1024;
  1806. /*
  1807. * Global gtt pte registers are special registers which actually forward
  1808. * writes to a chunk of system memory. Which means that there is no risk
  1809. * that the register values disappear as soon as we call
  1810. * intel_runtime_pm_put(), so it is correct to wrap only the
  1811. * pin/unpin/fence and not more.
  1812. */
  1813. intel_runtime_pm_get(dev_priv);
  1814. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1815. pinctl = 0;
  1816. /* Valleyview is definitely limited to scanning out the first
  1817. * 512MiB. Lets presume this behaviour was inherited from the
  1818. * g4x display engine and that all earlier gen are similarly
  1819. * limited. Testing suggests that it is a little more
  1820. * complicated than this. For example, Cherryview appears quite
  1821. * happy to scanout from anywhere within its global aperture.
  1822. */
  1823. if (HAS_GMCH_DISPLAY(dev_priv))
  1824. pinctl |= PIN_MAPPABLE;
  1825. vma = i915_gem_object_pin_to_display_plane(obj,
  1826. alignment, &view, pinctl);
  1827. if (IS_ERR(vma))
  1828. goto err;
  1829. if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
  1830. int ret;
  1831. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1832. * fence, whereas 965+ only requires a fence if using
  1833. * framebuffer compression. For simplicity, we always, when
  1834. * possible, install a fence as the cost is not that onerous.
  1835. *
  1836. * If we fail to fence the tiled scanout, then either the
  1837. * modeset will reject the change (which is highly unlikely as
  1838. * the affected systems, all but one, do not have unmappable
  1839. * space) or we will not be able to enable full powersaving
  1840. * techniques (also likely not to apply due to various limits
  1841. * FBC and the like impose on the size of the buffer, which
  1842. * presumably we violated anyway with this unmappable buffer).
  1843. * Anyway, it is presumably better to stumble onwards with
  1844. * something and try to run the system in a "less than optimal"
  1845. * mode that matches the user configuration.
  1846. */
  1847. ret = i915_vma_pin_fence(vma);
  1848. if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
  1849. i915_gem_object_unpin_from_display_plane(vma);
  1850. vma = ERR_PTR(ret);
  1851. goto err;
  1852. }
  1853. if (ret == 0 && vma->fence)
  1854. *out_flags |= PLANE_HAS_FENCE;
  1855. }
  1856. i915_vma_get(vma);
  1857. err:
  1858. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1859. intel_runtime_pm_put(dev_priv);
  1860. return vma;
  1861. }
  1862. void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
  1863. {
  1864. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1865. if (flags & PLANE_HAS_FENCE)
  1866. i915_vma_unpin_fence(vma);
  1867. i915_gem_object_unpin_from_display_plane(vma);
  1868. i915_vma_put(vma);
  1869. }
  1870. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1871. unsigned int rotation)
  1872. {
  1873. if (drm_rotation_90_or_270(rotation))
  1874. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1875. else
  1876. return fb->pitches[plane];
  1877. }
  1878. /*
  1879. * Convert the x/y offsets into a linear offset.
  1880. * Only valid with 0/180 degree rotation, which is fine since linear
  1881. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1882. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1883. */
  1884. u32 intel_fb_xy_to_linear(int x, int y,
  1885. const struct intel_plane_state *state,
  1886. int plane)
  1887. {
  1888. const struct drm_framebuffer *fb = state->base.fb;
  1889. unsigned int cpp = fb->format->cpp[plane];
  1890. unsigned int pitch = fb->pitches[plane];
  1891. return y * pitch + x * cpp;
  1892. }
  1893. /*
  1894. * Add the x/y offsets derived from fb->offsets[] to the user
  1895. * specified plane src x/y offsets. The resulting x/y offsets
  1896. * specify the start of scanout from the beginning of the gtt mapping.
  1897. */
  1898. void intel_add_fb_offsets(int *x, int *y,
  1899. const struct intel_plane_state *state,
  1900. int plane)
  1901. {
  1902. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1903. unsigned int rotation = state->base.rotation;
  1904. if (drm_rotation_90_or_270(rotation)) {
  1905. *x += intel_fb->rotated[plane].x;
  1906. *y += intel_fb->rotated[plane].y;
  1907. } else {
  1908. *x += intel_fb->normal[plane].x;
  1909. *y += intel_fb->normal[plane].y;
  1910. }
  1911. }
  1912. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1913. unsigned int tile_width,
  1914. unsigned int tile_height,
  1915. unsigned int tile_size,
  1916. unsigned int pitch_tiles,
  1917. u32 old_offset,
  1918. u32 new_offset)
  1919. {
  1920. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1921. unsigned int tiles;
  1922. WARN_ON(old_offset & (tile_size - 1));
  1923. WARN_ON(new_offset & (tile_size - 1));
  1924. WARN_ON(new_offset > old_offset);
  1925. tiles = (old_offset - new_offset) / tile_size;
  1926. *y += tiles / pitch_tiles * tile_height;
  1927. *x += tiles % pitch_tiles * tile_width;
  1928. /* minimize x in case it got needlessly big */
  1929. *y += *x / pitch_pixels * tile_height;
  1930. *x %= pitch_pixels;
  1931. return new_offset;
  1932. }
  1933. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1934. const struct drm_framebuffer *fb, int plane,
  1935. unsigned int rotation,
  1936. u32 old_offset, u32 new_offset)
  1937. {
  1938. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1939. unsigned int cpp = fb->format->cpp[plane];
  1940. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1941. WARN_ON(new_offset > old_offset);
  1942. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1943. unsigned int tile_size, tile_width, tile_height;
  1944. unsigned int pitch_tiles;
  1945. tile_size = intel_tile_size(dev_priv);
  1946. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1947. if (drm_rotation_90_or_270(rotation)) {
  1948. pitch_tiles = pitch / tile_height;
  1949. swap(tile_width, tile_height);
  1950. } else {
  1951. pitch_tiles = pitch / (tile_width * cpp);
  1952. }
  1953. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1954. tile_size, pitch_tiles,
  1955. old_offset, new_offset);
  1956. } else {
  1957. old_offset += *y * pitch + *x * cpp;
  1958. *y = (old_offset - new_offset) / pitch;
  1959. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1960. }
  1961. return new_offset;
  1962. }
  1963. /*
  1964. * Adjust the tile offset by moving the difference into
  1965. * the x/y offsets.
  1966. */
  1967. static u32 intel_adjust_tile_offset(int *x, int *y,
  1968. const struct intel_plane_state *state, int plane,
  1969. u32 old_offset, u32 new_offset)
  1970. {
  1971. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  1972. state->base.rotation,
  1973. old_offset, new_offset);
  1974. }
  1975. /*
  1976. * Computes the linear offset to the base tile and adjusts
  1977. * x, y. bytes per pixel is assumed to be a power-of-two.
  1978. *
  1979. * In the 90/270 rotated case, x and y are assumed
  1980. * to be already rotated to match the rotated GTT view, and
  1981. * pitch is the tile_height aligned framebuffer height.
  1982. *
  1983. * This function is used when computing the derived information
  1984. * under intel_framebuffer, so using any of that information
  1985. * here is not allowed. Anything under drm_framebuffer can be
  1986. * used. This is why the user has to pass in the pitch since it
  1987. * is specified in the rotated orientation.
  1988. */
  1989. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  1990. int *x, int *y,
  1991. const struct drm_framebuffer *fb, int plane,
  1992. unsigned int pitch,
  1993. unsigned int rotation,
  1994. u32 alignment)
  1995. {
  1996. uint64_t fb_modifier = fb->modifier;
  1997. unsigned int cpp = fb->format->cpp[plane];
  1998. u32 offset, offset_aligned;
  1999. if (alignment)
  2000. alignment--;
  2001. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2002. unsigned int tile_size, tile_width, tile_height;
  2003. unsigned int tile_rows, tiles, pitch_tiles;
  2004. tile_size = intel_tile_size(dev_priv);
  2005. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2006. if (drm_rotation_90_or_270(rotation)) {
  2007. pitch_tiles = pitch / tile_height;
  2008. swap(tile_width, tile_height);
  2009. } else {
  2010. pitch_tiles = pitch / (tile_width * cpp);
  2011. }
  2012. tile_rows = *y / tile_height;
  2013. *y %= tile_height;
  2014. tiles = *x / tile_width;
  2015. *x %= tile_width;
  2016. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2017. offset_aligned = offset & ~alignment;
  2018. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2019. tile_size, pitch_tiles,
  2020. offset, offset_aligned);
  2021. } else {
  2022. offset = *y * pitch + *x * cpp;
  2023. offset_aligned = offset & ~alignment;
  2024. *y = (offset & alignment) / pitch;
  2025. *x = ((offset & alignment) - *y * pitch) / cpp;
  2026. }
  2027. return offset_aligned;
  2028. }
  2029. u32 intel_compute_tile_offset(int *x, int *y,
  2030. const struct intel_plane_state *state,
  2031. int plane)
  2032. {
  2033. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2034. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2035. const struct drm_framebuffer *fb = state->base.fb;
  2036. unsigned int rotation = state->base.rotation;
  2037. int pitch = intel_fb_pitch(fb, plane, rotation);
  2038. u32 alignment;
  2039. if (intel_plane->id == PLANE_CURSOR)
  2040. alignment = intel_cursor_alignment(dev_priv);
  2041. else
  2042. alignment = intel_surf_alignment(fb, plane);
  2043. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2044. rotation, alignment);
  2045. }
  2046. /* Convert the fb->offset[] into x/y offsets */
  2047. static int intel_fb_offset_to_xy(int *x, int *y,
  2048. const struct drm_framebuffer *fb, int plane)
  2049. {
  2050. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2051. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2052. fb->offsets[plane] % intel_tile_size(dev_priv))
  2053. return -EINVAL;
  2054. *x = 0;
  2055. *y = 0;
  2056. _intel_adjust_tile_offset(x, y,
  2057. fb, plane, DRM_MODE_ROTATE_0,
  2058. fb->offsets[plane], 0);
  2059. return 0;
  2060. }
  2061. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2062. {
  2063. switch (fb_modifier) {
  2064. case I915_FORMAT_MOD_X_TILED:
  2065. return I915_TILING_X;
  2066. case I915_FORMAT_MOD_Y_TILED:
  2067. case I915_FORMAT_MOD_Y_TILED_CCS:
  2068. return I915_TILING_Y;
  2069. default:
  2070. return I915_TILING_NONE;
  2071. }
  2072. }
  2073. /*
  2074. * From the Sky Lake PRM:
  2075. * "The Color Control Surface (CCS) contains the compression status of
  2076. * the cache-line pairs. The compression state of the cache-line pair
  2077. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2078. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2079. * cache-line-pairs. CCS is always Y tiled."
  2080. *
  2081. * Since cache line pairs refers to horizontally adjacent cache lines,
  2082. * each cache line in the CCS corresponds to an area of 32x16 cache
  2083. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2084. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2085. * main surface.
  2086. */
  2087. static const struct drm_format_info ccs_formats[] = {
  2088. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2089. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2090. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2091. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2092. };
  2093. static const struct drm_format_info *
  2094. lookup_format_info(const struct drm_format_info formats[],
  2095. int num_formats, u32 format)
  2096. {
  2097. int i;
  2098. for (i = 0; i < num_formats; i++) {
  2099. if (formats[i].format == format)
  2100. return &formats[i];
  2101. }
  2102. return NULL;
  2103. }
  2104. static const struct drm_format_info *
  2105. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2106. {
  2107. switch (cmd->modifier[0]) {
  2108. case I915_FORMAT_MOD_Y_TILED_CCS:
  2109. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2110. return lookup_format_info(ccs_formats,
  2111. ARRAY_SIZE(ccs_formats),
  2112. cmd->pixel_format);
  2113. default:
  2114. return NULL;
  2115. }
  2116. }
  2117. static int
  2118. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2119. struct drm_framebuffer *fb)
  2120. {
  2121. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2122. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2123. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2124. u32 gtt_offset_rotated = 0;
  2125. unsigned int max_size = 0;
  2126. int i, num_planes = fb->format->num_planes;
  2127. unsigned int tile_size = intel_tile_size(dev_priv);
  2128. for (i = 0; i < num_planes; i++) {
  2129. unsigned int width, height;
  2130. unsigned int cpp, size;
  2131. u32 offset;
  2132. int x, y;
  2133. int ret;
  2134. cpp = fb->format->cpp[i];
  2135. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2136. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2137. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2138. if (ret) {
  2139. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2140. i, fb->offsets[i]);
  2141. return ret;
  2142. }
  2143. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2144. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2145. int hsub = fb->format->hsub;
  2146. int vsub = fb->format->vsub;
  2147. int tile_width, tile_height;
  2148. int main_x, main_y;
  2149. int ccs_x, ccs_y;
  2150. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2151. tile_width *= hsub;
  2152. tile_height *= vsub;
  2153. ccs_x = (x * hsub) % tile_width;
  2154. ccs_y = (y * vsub) % tile_height;
  2155. main_x = intel_fb->normal[0].x % tile_width;
  2156. main_y = intel_fb->normal[0].y % tile_height;
  2157. /*
  2158. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2159. * x/y offsets must match between CCS and the main surface.
  2160. */
  2161. if (main_x != ccs_x || main_y != ccs_y) {
  2162. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2163. main_x, main_y,
  2164. ccs_x, ccs_y,
  2165. intel_fb->normal[0].x,
  2166. intel_fb->normal[0].y,
  2167. x, y);
  2168. return -EINVAL;
  2169. }
  2170. }
  2171. /*
  2172. * The fence (if used) is aligned to the start of the object
  2173. * so having the framebuffer wrap around across the edge of the
  2174. * fenced region doesn't really work. We have no API to configure
  2175. * the fence start offset within the object (nor could we probably
  2176. * on gen2/3). So it's just easier if we just require that the
  2177. * fb layout agrees with the fence layout. We already check that the
  2178. * fb stride matches the fence stride elsewhere.
  2179. */
  2180. if (i == 0 && i915_gem_object_is_tiled(obj) &&
  2181. (x + width) * cpp > fb->pitches[i]) {
  2182. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2183. i, fb->offsets[i]);
  2184. return -EINVAL;
  2185. }
  2186. /*
  2187. * First pixel of the framebuffer from
  2188. * the start of the normal gtt mapping.
  2189. */
  2190. intel_fb->normal[i].x = x;
  2191. intel_fb->normal[i].y = y;
  2192. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2193. fb, i, fb->pitches[i],
  2194. DRM_MODE_ROTATE_0, tile_size);
  2195. offset /= tile_size;
  2196. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2197. unsigned int tile_width, tile_height;
  2198. unsigned int pitch_tiles;
  2199. struct drm_rect r;
  2200. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2201. rot_info->plane[i].offset = offset;
  2202. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2203. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2204. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2205. intel_fb->rotated[i].pitch =
  2206. rot_info->plane[i].height * tile_height;
  2207. /* how many tiles does this plane need */
  2208. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2209. /*
  2210. * If the plane isn't horizontally tile aligned,
  2211. * we need one more tile.
  2212. */
  2213. if (x != 0)
  2214. size++;
  2215. /* rotate the x/y offsets to match the GTT view */
  2216. r.x1 = x;
  2217. r.y1 = y;
  2218. r.x2 = x + width;
  2219. r.y2 = y + height;
  2220. drm_rect_rotate(&r,
  2221. rot_info->plane[i].width * tile_width,
  2222. rot_info->plane[i].height * tile_height,
  2223. DRM_MODE_ROTATE_270);
  2224. x = r.x1;
  2225. y = r.y1;
  2226. /* rotate the tile dimensions to match the GTT view */
  2227. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2228. swap(tile_width, tile_height);
  2229. /*
  2230. * We only keep the x/y offsets, so push all of the
  2231. * gtt offset into the x/y offsets.
  2232. */
  2233. __intel_adjust_tile_offset(&x, &y,
  2234. tile_width, tile_height,
  2235. tile_size, pitch_tiles,
  2236. gtt_offset_rotated * tile_size, 0);
  2237. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2238. /*
  2239. * First pixel of the framebuffer from
  2240. * the start of the rotated gtt mapping.
  2241. */
  2242. intel_fb->rotated[i].x = x;
  2243. intel_fb->rotated[i].y = y;
  2244. } else {
  2245. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2246. x * cpp, tile_size);
  2247. }
  2248. /* how many tiles in total needed in the bo */
  2249. max_size = max(max_size, offset + size);
  2250. }
  2251. if (max_size * tile_size > obj->base.size) {
  2252. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2253. max_size * tile_size, obj->base.size);
  2254. return -EINVAL;
  2255. }
  2256. return 0;
  2257. }
  2258. static int i9xx_format_to_fourcc(int format)
  2259. {
  2260. switch (format) {
  2261. case DISPPLANE_8BPP:
  2262. return DRM_FORMAT_C8;
  2263. case DISPPLANE_BGRX555:
  2264. return DRM_FORMAT_XRGB1555;
  2265. case DISPPLANE_BGRX565:
  2266. return DRM_FORMAT_RGB565;
  2267. default:
  2268. case DISPPLANE_BGRX888:
  2269. return DRM_FORMAT_XRGB8888;
  2270. case DISPPLANE_RGBX888:
  2271. return DRM_FORMAT_XBGR8888;
  2272. case DISPPLANE_BGRX101010:
  2273. return DRM_FORMAT_XRGB2101010;
  2274. case DISPPLANE_RGBX101010:
  2275. return DRM_FORMAT_XBGR2101010;
  2276. }
  2277. }
  2278. int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2279. {
  2280. switch (format) {
  2281. case PLANE_CTL_FORMAT_RGB_565:
  2282. return DRM_FORMAT_RGB565;
  2283. case PLANE_CTL_FORMAT_NV12:
  2284. return DRM_FORMAT_NV12;
  2285. default:
  2286. case PLANE_CTL_FORMAT_XRGB_8888:
  2287. if (rgb_order) {
  2288. if (alpha)
  2289. return DRM_FORMAT_ABGR8888;
  2290. else
  2291. return DRM_FORMAT_XBGR8888;
  2292. } else {
  2293. if (alpha)
  2294. return DRM_FORMAT_ARGB8888;
  2295. else
  2296. return DRM_FORMAT_XRGB8888;
  2297. }
  2298. case PLANE_CTL_FORMAT_XRGB_2101010:
  2299. if (rgb_order)
  2300. return DRM_FORMAT_XBGR2101010;
  2301. else
  2302. return DRM_FORMAT_XRGB2101010;
  2303. }
  2304. }
  2305. static bool
  2306. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2307. struct intel_initial_plane_config *plane_config)
  2308. {
  2309. struct drm_device *dev = crtc->base.dev;
  2310. struct drm_i915_private *dev_priv = to_i915(dev);
  2311. struct drm_i915_gem_object *obj = NULL;
  2312. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2313. struct drm_framebuffer *fb = &plane_config->fb->base;
  2314. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2315. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2316. PAGE_SIZE);
  2317. size_aligned -= base_aligned;
  2318. if (plane_config->size == 0)
  2319. return false;
  2320. /* If the FB is too big, just don't use it since fbdev is not very
  2321. * important and we should probably use that space with FBC or other
  2322. * features. */
  2323. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2324. return false;
  2325. switch (fb->modifier) {
  2326. case DRM_FORMAT_MOD_LINEAR:
  2327. case I915_FORMAT_MOD_X_TILED:
  2328. case I915_FORMAT_MOD_Y_TILED:
  2329. break;
  2330. default:
  2331. DRM_DEBUG_DRIVER("Unsupported modifier for initial FB: 0x%llx\n",
  2332. fb->modifier);
  2333. return false;
  2334. }
  2335. mutex_lock(&dev->struct_mutex);
  2336. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2337. base_aligned,
  2338. base_aligned,
  2339. size_aligned);
  2340. mutex_unlock(&dev->struct_mutex);
  2341. if (!obj)
  2342. return false;
  2343. switch (plane_config->tiling) {
  2344. case I915_TILING_NONE:
  2345. break;
  2346. case I915_TILING_X:
  2347. case I915_TILING_Y:
  2348. obj->tiling_and_stride = fb->pitches[0] | plane_config->tiling;
  2349. break;
  2350. default:
  2351. MISSING_CASE(plane_config->tiling);
  2352. return false;
  2353. }
  2354. mode_cmd.pixel_format = fb->format->format;
  2355. mode_cmd.width = fb->width;
  2356. mode_cmd.height = fb->height;
  2357. mode_cmd.pitches[0] = fb->pitches[0];
  2358. mode_cmd.modifier[0] = fb->modifier;
  2359. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2360. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2361. DRM_DEBUG_KMS("intel fb init failed\n");
  2362. goto out_unref_obj;
  2363. }
  2364. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2365. return true;
  2366. out_unref_obj:
  2367. i915_gem_object_put(obj);
  2368. return false;
  2369. }
  2370. static void
  2371. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2372. struct intel_plane_state *plane_state,
  2373. bool visible)
  2374. {
  2375. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2376. plane_state->base.visible = visible;
  2377. if (visible)
  2378. crtc_state->base.plane_mask |= drm_plane_mask(&plane->base);
  2379. else
  2380. crtc_state->base.plane_mask &= ~drm_plane_mask(&plane->base);
  2381. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2382. crtc_state->base.crtc->name,
  2383. crtc_state->active_planes);
  2384. }
  2385. static void fixup_active_planes(struct intel_crtc_state *crtc_state)
  2386. {
  2387. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  2388. struct drm_plane *plane;
  2389. /*
  2390. * Active_planes aliases if multiple "primary" or cursor planes
  2391. * have been used on the same (or wrong) pipe. plane_mask uses
  2392. * unique ids, hence we can use that to reconstruct active_planes.
  2393. */
  2394. crtc_state->active_planes = 0;
  2395. drm_for_each_plane_mask(plane, &dev_priv->drm,
  2396. crtc_state->base.plane_mask)
  2397. crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
  2398. }
  2399. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2400. struct intel_plane *plane)
  2401. {
  2402. struct intel_crtc_state *crtc_state =
  2403. to_intel_crtc_state(crtc->base.state);
  2404. struct intel_plane_state *plane_state =
  2405. to_intel_plane_state(plane->base.state);
  2406. intel_set_plane_visible(crtc_state, plane_state, false);
  2407. fixup_active_planes(crtc_state);
  2408. if (plane->id == PLANE_PRIMARY)
  2409. intel_pre_disable_primary_noatomic(&crtc->base);
  2410. trace_intel_disable_plane(&plane->base, crtc);
  2411. plane->disable_plane(plane, crtc);
  2412. }
  2413. static void
  2414. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2415. struct intel_initial_plane_config *plane_config)
  2416. {
  2417. struct drm_device *dev = intel_crtc->base.dev;
  2418. struct drm_i915_private *dev_priv = to_i915(dev);
  2419. struct drm_crtc *c;
  2420. struct drm_i915_gem_object *obj;
  2421. struct drm_plane *primary = intel_crtc->base.primary;
  2422. struct drm_plane_state *plane_state = primary->state;
  2423. struct intel_plane *intel_plane = to_intel_plane(primary);
  2424. struct intel_plane_state *intel_state =
  2425. to_intel_plane_state(plane_state);
  2426. struct drm_framebuffer *fb;
  2427. if (!plane_config->fb)
  2428. return;
  2429. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2430. fb = &plane_config->fb->base;
  2431. goto valid_fb;
  2432. }
  2433. kfree(plane_config->fb);
  2434. /*
  2435. * Failed to alloc the obj, check to see if we should share
  2436. * an fb with another CRTC instead
  2437. */
  2438. for_each_crtc(dev, c) {
  2439. struct intel_plane_state *state;
  2440. if (c == &intel_crtc->base)
  2441. continue;
  2442. if (!to_intel_crtc(c)->active)
  2443. continue;
  2444. state = to_intel_plane_state(c->primary->state);
  2445. if (!state->vma)
  2446. continue;
  2447. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2448. fb = state->base.fb;
  2449. drm_framebuffer_get(fb);
  2450. goto valid_fb;
  2451. }
  2452. }
  2453. /*
  2454. * We've failed to reconstruct the BIOS FB. Current display state
  2455. * indicates that the primary plane is visible, but has a NULL FB,
  2456. * which will lead to problems later if we don't fix it up. The
  2457. * simplest solution is to just disable the primary plane now and
  2458. * pretend the BIOS never had it enabled.
  2459. */
  2460. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2461. return;
  2462. valid_fb:
  2463. mutex_lock(&dev->struct_mutex);
  2464. intel_state->vma =
  2465. intel_pin_and_fence_fb_obj(fb,
  2466. primary->state->rotation,
  2467. intel_plane_uses_fence(intel_state),
  2468. &intel_state->flags);
  2469. mutex_unlock(&dev->struct_mutex);
  2470. if (IS_ERR(intel_state->vma)) {
  2471. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2472. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2473. intel_state->vma = NULL;
  2474. drm_framebuffer_put(fb);
  2475. return;
  2476. }
  2477. obj = intel_fb_obj(fb);
  2478. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  2479. plane_state->src_x = 0;
  2480. plane_state->src_y = 0;
  2481. plane_state->src_w = fb->width << 16;
  2482. plane_state->src_h = fb->height << 16;
  2483. plane_state->crtc_x = 0;
  2484. plane_state->crtc_y = 0;
  2485. plane_state->crtc_w = fb->width;
  2486. plane_state->crtc_h = fb->height;
  2487. intel_state->base.src = drm_plane_state_src(plane_state);
  2488. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2489. if (i915_gem_object_is_tiled(obj))
  2490. dev_priv->preserve_bios_swizzle = true;
  2491. plane_state->fb = fb;
  2492. plane_state->crtc = &intel_crtc->base;
  2493. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2494. &obj->frontbuffer_bits);
  2495. }
  2496. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2497. unsigned int rotation)
  2498. {
  2499. int cpp = fb->format->cpp[plane];
  2500. switch (fb->modifier) {
  2501. case DRM_FORMAT_MOD_LINEAR:
  2502. case I915_FORMAT_MOD_X_TILED:
  2503. switch (cpp) {
  2504. case 8:
  2505. return 4096;
  2506. case 4:
  2507. case 2:
  2508. case 1:
  2509. return 8192;
  2510. default:
  2511. MISSING_CASE(cpp);
  2512. break;
  2513. }
  2514. break;
  2515. case I915_FORMAT_MOD_Y_TILED_CCS:
  2516. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2517. /* FIXME AUX plane? */
  2518. case I915_FORMAT_MOD_Y_TILED:
  2519. case I915_FORMAT_MOD_Yf_TILED:
  2520. switch (cpp) {
  2521. case 8:
  2522. return 2048;
  2523. case 4:
  2524. return 4096;
  2525. case 2:
  2526. case 1:
  2527. return 8192;
  2528. default:
  2529. MISSING_CASE(cpp);
  2530. break;
  2531. }
  2532. break;
  2533. default:
  2534. MISSING_CASE(fb->modifier);
  2535. }
  2536. return 2048;
  2537. }
  2538. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2539. int main_x, int main_y, u32 main_offset)
  2540. {
  2541. const struct drm_framebuffer *fb = plane_state->base.fb;
  2542. int hsub = fb->format->hsub;
  2543. int vsub = fb->format->vsub;
  2544. int aux_x = plane_state->aux.x;
  2545. int aux_y = plane_state->aux.y;
  2546. u32 aux_offset = plane_state->aux.offset;
  2547. u32 alignment = intel_surf_alignment(fb, 1);
  2548. while (aux_offset >= main_offset && aux_y <= main_y) {
  2549. int x, y;
  2550. if (aux_x == main_x && aux_y == main_y)
  2551. break;
  2552. if (aux_offset == 0)
  2553. break;
  2554. x = aux_x / hsub;
  2555. y = aux_y / vsub;
  2556. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2557. aux_offset, aux_offset - alignment);
  2558. aux_x = x * hsub + aux_x % hsub;
  2559. aux_y = y * vsub + aux_y % vsub;
  2560. }
  2561. if (aux_x != main_x || aux_y != main_y)
  2562. return false;
  2563. plane_state->aux.offset = aux_offset;
  2564. plane_state->aux.x = aux_x;
  2565. plane_state->aux.y = aux_y;
  2566. return true;
  2567. }
  2568. static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
  2569. struct intel_plane_state *plane_state)
  2570. {
  2571. struct drm_i915_private *dev_priv =
  2572. to_i915(plane_state->base.plane->dev);
  2573. const struct drm_framebuffer *fb = plane_state->base.fb;
  2574. unsigned int rotation = plane_state->base.rotation;
  2575. int x = plane_state->base.src.x1 >> 16;
  2576. int y = plane_state->base.src.y1 >> 16;
  2577. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2578. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2579. int dst_x = plane_state->base.dst.x1;
  2580. int dst_w = drm_rect_width(&plane_state->base.dst);
  2581. int pipe_src_w = crtc_state->pipe_src_w;
  2582. int max_width = skl_max_plane_width(fb, 0, rotation);
  2583. int max_height = 4096;
  2584. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2585. if (w > max_width || h > max_height) {
  2586. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2587. w, h, max_width, max_height);
  2588. return -EINVAL;
  2589. }
  2590. /*
  2591. * Display WA #1175: cnl,glk
  2592. * Planes other than the cursor may cause FIFO underflow and display
  2593. * corruption if starting less than 4 pixels from the right edge of
  2594. * the screen.
  2595. * Besides the above WA fix the similar problem, where planes other
  2596. * than the cursor ending less than 4 pixels from the left edge of the
  2597. * screen may cause FIFO underflow and display corruption.
  2598. */
  2599. if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  2600. (dst_x + dst_w < 4 || dst_x > pipe_src_w - 4)) {
  2601. DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
  2602. dst_x + dst_w < 4 ? "end" : "start",
  2603. dst_x + dst_w < 4 ? dst_x + dst_w : dst_x,
  2604. 4, pipe_src_w - 4);
  2605. return -ERANGE;
  2606. }
  2607. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2608. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2609. alignment = intel_surf_alignment(fb, 0);
  2610. /*
  2611. * AUX surface offset is specified as the distance from the
  2612. * main surface offset, and it must be non-negative. Make
  2613. * sure that is what we will get.
  2614. */
  2615. if (offset > aux_offset)
  2616. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2617. offset, aux_offset & ~(alignment - 1));
  2618. /*
  2619. * When using an X-tiled surface, the plane blows up
  2620. * if the x offset + width exceed the stride.
  2621. *
  2622. * TODO: linear and Y-tiled seem fine, Yf untested,
  2623. */
  2624. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2625. int cpp = fb->format->cpp[0];
  2626. while ((x + w) * cpp > fb->pitches[0]) {
  2627. if (offset == 0) {
  2628. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2629. return -EINVAL;
  2630. }
  2631. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2632. offset, offset - alignment);
  2633. }
  2634. }
  2635. /*
  2636. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2637. * they match with the main surface x/y offsets.
  2638. */
  2639. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2640. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2641. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2642. if (offset == 0)
  2643. break;
  2644. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2645. offset, offset - alignment);
  2646. }
  2647. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2648. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2649. return -EINVAL;
  2650. }
  2651. }
  2652. plane_state->main.offset = offset;
  2653. plane_state->main.x = x;
  2654. plane_state->main.y = y;
  2655. return 0;
  2656. }
  2657. static int
  2658. skl_check_nv12_surface(const struct intel_crtc_state *crtc_state,
  2659. struct intel_plane_state *plane_state)
  2660. {
  2661. /* Display WA #1106 */
  2662. if (plane_state->base.rotation !=
  2663. (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
  2664. plane_state->base.rotation != DRM_MODE_ROTATE_270)
  2665. return 0;
  2666. /*
  2667. * src coordinates are rotated here.
  2668. * We check height but report it as width
  2669. */
  2670. if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
  2671. DRM_DEBUG_KMS("src width must be multiple "
  2672. "of 4 for rotated NV12\n");
  2673. return -EINVAL;
  2674. }
  2675. return 0;
  2676. }
  2677. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2678. {
  2679. const struct drm_framebuffer *fb = plane_state->base.fb;
  2680. unsigned int rotation = plane_state->base.rotation;
  2681. int max_width = skl_max_plane_width(fb, 1, rotation);
  2682. int max_height = 4096;
  2683. int x = plane_state->base.src.x1 >> 17;
  2684. int y = plane_state->base.src.y1 >> 17;
  2685. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2686. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2687. u32 offset;
  2688. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2689. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2690. /* FIXME not quite sure how/if these apply to the chroma plane */
  2691. if (w > max_width || h > max_height) {
  2692. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2693. w, h, max_width, max_height);
  2694. return -EINVAL;
  2695. }
  2696. plane_state->aux.offset = offset;
  2697. plane_state->aux.x = x;
  2698. plane_state->aux.y = y;
  2699. return 0;
  2700. }
  2701. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2702. {
  2703. const struct drm_framebuffer *fb = plane_state->base.fb;
  2704. int src_x = plane_state->base.src.x1 >> 16;
  2705. int src_y = plane_state->base.src.y1 >> 16;
  2706. int hsub = fb->format->hsub;
  2707. int vsub = fb->format->vsub;
  2708. int x = src_x / hsub;
  2709. int y = src_y / vsub;
  2710. u32 offset;
  2711. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2712. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2713. plane_state->base.rotation);
  2714. return -EINVAL;
  2715. }
  2716. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2717. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2718. plane_state->aux.offset = offset;
  2719. plane_state->aux.x = x * hsub + src_x % hsub;
  2720. plane_state->aux.y = y * vsub + src_y % vsub;
  2721. return 0;
  2722. }
  2723. int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
  2724. struct intel_plane_state *plane_state)
  2725. {
  2726. const struct drm_framebuffer *fb = plane_state->base.fb;
  2727. unsigned int rotation = plane_state->base.rotation;
  2728. int ret;
  2729. if (rotation & DRM_MODE_REFLECT_X &&
  2730. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2731. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2732. return -EINVAL;
  2733. }
  2734. if (!plane_state->base.visible)
  2735. return 0;
  2736. /* Rotate src coordinates to match rotated GTT view */
  2737. if (drm_rotation_90_or_270(rotation))
  2738. drm_rect_rotate(&plane_state->base.src,
  2739. fb->width << 16, fb->height << 16,
  2740. DRM_MODE_ROTATE_270);
  2741. /*
  2742. * Handle the AUX surface first since
  2743. * the main surface setup depends on it.
  2744. */
  2745. if (fb->format->format == DRM_FORMAT_NV12) {
  2746. ret = skl_check_nv12_surface(crtc_state, plane_state);
  2747. if (ret)
  2748. return ret;
  2749. ret = skl_check_nv12_aux_surface(plane_state);
  2750. if (ret)
  2751. return ret;
  2752. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2753. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2754. ret = skl_check_ccs_aux_surface(plane_state);
  2755. if (ret)
  2756. return ret;
  2757. } else {
  2758. plane_state->aux.offset = ~0xfff;
  2759. plane_state->aux.x = 0;
  2760. plane_state->aux.y = 0;
  2761. }
  2762. ret = skl_check_main_surface(crtc_state, plane_state);
  2763. if (ret)
  2764. return ret;
  2765. return 0;
  2766. }
  2767. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2768. const struct intel_plane_state *plane_state)
  2769. {
  2770. struct drm_i915_private *dev_priv =
  2771. to_i915(plane_state->base.plane->dev);
  2772. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2773. const struct drm_framebuffer *fb = plane_state->base.fb;
  2774. unsigned int rotation = plane_state->base.rotation;
  2775. u32 dspcntr;
  2776. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2777. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2778. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2779. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2780. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2781. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2782. if (INTEL_GEN(dev_priv) < 5)
  2783. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2784. switch (fb->format->format) {
  2785. case DRM_FORMAT_C8:
  2786. dspcntr |= DISPPLANE_8BPP;
  2787. break;
  2788. case DRM_FORMAT_XRGB1555:
  2789. dspcntr |= DISPPLANE_BGRX555;
  2790. break;
  2791. case DRM_FORMAT_RGB565:
  2792. dspcntr |= DISPPLANE_BGRX565;
  2793. break;
  2794. case DRM_FORMAT_XRGB8888:
  2795. dspcntr |= DISPPLANE_BGRX888;
  2796. break;
  2797. case DRM_FORMAT_XBGR8888:
  2798. dspcntr |= DISPPLANE_RGBX888;
  2799. break;
  2800. case DRM_FORMAT_XRGB2101010:
  2801. dspcntr |= DISPPLANE_BGRX101010;
  2802. break;
  2803. case DRM_FORMAT_XBGR2101010:
  2804. dspcntr |= DISPPLANE_RGBX101010;
  2805. break;
  2806. default:
  2807. MISSING_CASE(fb->format->format);
  2808. return 0;
  2809. }
  2810. if (INTEL_GEN(dev_priv) >= 4 &&
  2811. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2812. dspcntr |= DISPPLANE_TILED;
  2813. if (rotation & DRM_MODE_ROTATE_180)
  2814. dspcntr |= DISPPLANE_ROTATE_180;
  2815. if (rotation & DRM_MODE_REFLECT_X)
  2816. dspcntr |= DISPPLANE_MIRROR;
  2817. return dspcntr;
  2818. }
  2819. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2820. {
  2821. struct drm_i915_private *dev_priv =
  2822. to_i915(plane_state->base.plane->dev);
  2823. int src_x = plane_state->base.src.x1 >> 16;
  2824. int src_y = plane_state->base.src.y1 >> 16;
  2825. u32 offset;
  2826. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2827. if (INTEL_GEN(dev_priv) >= 4)
  2828. offset = intel_compute_tile_offset(&src_x, &src_y,
  2829. plane_state, 0);
  2830. else
  2831. offset = 0;
  2832. /* HSW/BDW do this automagically in hardware */
  2833. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2834. unsigned int rotation = plane_state->base.rotation;
  2835. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2836. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2837. if (rotation & DRM_MODE_ROTATE_180) {
  2838. src_x += src_w - 1;
  2839. src_y += src_h - 1;
  2840. } else if (rotation & DRM_MODE_REFLECT_X) {
  2841. src_x += src_w - 1;
  2842. }
  2843. }
  2844. plane_state->main.offset = offset;
  2845. plane_state->main.x = src_x;
  2846. plane_state->main.y = src_y;
  2847. return 0;
  2848. }
  2849. static void i9xx_update_plane(struct intel_plane *plane,
  2850. const struct intel_crtc_state *crtc_state,
  2851. const struct intel_plane_state *plane_state)
  2852. {
  2853. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2854. const struct drm_framebuffer *fb = plane_state->base.fb;
  2855. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2856. u32 linear_offset;
  2857. u32 dspcntr = plane_state->ctl;
  2858. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2859. int x = plane_state->main.x;
  2860. int y = plane_state->main.y;
  2861. unsigned long irqflags;
  2862. u32 dspaddr_offset;
  2863. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2864. if (INTEL_GEN(dev_priv) >= 4)
  2865. dspaddr_offset = plane_state->main.offset;
  2866. else
  2867. dspaddr_offset = linear_offset;
  2868. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2869. if (INTEL_GEN(dev_priv) < 4) {
  2870. /* pipesrc and dspsize control the size that is scaled from,
  2871. * which should always be the user's requested size.
  2872. */
  2873. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2874. ((crtc_state->pipe_src_h - 1) << 16) |
  2875. (crtc_state->pipe_src_w - 1));
  2876. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2877. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2878. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2879. ((crtc_state->pipe_src_h - 1) << 16) |
  2880. (crtc_state->pipe_src_w - 1));
  2881. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2882. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2883. }
  2884. I915_WRITE_FW(reg, dspcntr);
  2885. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2886. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2887. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2888. intel_plane_ggtt_offset(plane_state) +
  2889. dspaddr_offset);
  2890. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2891. } else if (INTEL_GEN(dev_priv) >= 4) {
  2892. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2893. intel_plane_ggtt_offset(plane_state) +
  2894. dspaddr_offset);
  2895. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2896. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2897. } else {
  2898. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2899. intel_plane_ggtt_offset(plane_state) +
  2900. dspaddr_offset);
  2901. }
  2902. POSTING_READ_FW(reg);
  2903. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2904. }
  2905. static void i9xx_disable_plane(struct intel_plane *plane,
  2906. struct intel_crtc *crtc)
  2907. {
  2908. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2909. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2910. unsigned long irqflags;
  2911. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2912. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2913. if (INTEL_GEN(dev_priv) >= 4)
  2914. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2915. else
  2916. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2917. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2918. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2919. }
  2920. static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
  2921. enum pipe *pipe)
  2922. {
  2923. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2924. enum intel_display_power_domain power_domain;
  2925. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2926. bool ret;
  2927. u32 val;
  2928. /*
  2929. * Not 100% correct for planes that can move between pipes,
  2930. * but that's only the case for gen2-4 which don't have any
  2931. * display power wells.
  2932. */
  2933. power_domain = POWER_DOMAIN_PIPE(plane->pipe);
  2934. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2935. return false;
  2936. val = I915_READ(DSPCNTR(i9xx_plane));
  2937. ret = val & DISPLAY_PLANE_ENABLE;
  2938. if (INTEL_GEN(dev_priv) >= 5)
  2939. *pipe = plane->pipe;
  2940. else
  2941. *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  2942. DISPPLANE_SEL_PIPE_SHIFT;
  2943. intel_display_power_put(dev_priv, power_domain);
  2944. return ret;
  2945. }
  2946. static u32
  2947. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2948. {
  2949. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2950. return 64;
  2951. else
  2952. return intel_tile_width_bytes(fb, plane);
  2953. }
  2954. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2955. {
  2956. struct drm_device *dev = intel_crtc->base.dev;
  2957. struct drm_i915_private *dev_priv = to_i915(dev);
  2958. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2959. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2960. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2961. }
  2962. /*
  2963. * This function detaches (aka. unbinds) unused scalers in hardware
  2964. */
  2965. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2966. {
  2967. struct intel_crtc_scaler_state *scaler_state;
  2968. int i;
  2969. scaler_state = &intel_crtc->config->scaler_state;
  2970. /* loop through and disable scalers that aren't in use */
  2971. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2972. if (!scaler_state->scalers[i].in_use)
  2973. skl_detach_scaler(intel_crtc, i);
  2974. }
  2975. }
  2976. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2977. unsigned int rotation)
  2978. {
  2979. u32 stride;
  2980. if (plane >= fb->format->num_planes)
  2981. return 0;
  2982. stride = intel_fb_pitch(fb, plane, rotation);
  2983. /*
  2984. * The stride is either expressed as a multiple of 64 bytes chunks for
  2985. * linear buffers or in number of tiles for tiled buffers.
  2986. */
  2987. if (drm_rotation_90_or_270(rotation))
  2988. stride /= intel_tile_height(fb, plane);
  2989. else
  2990. stride /= intel_fb_stride_alignment(fb, plane);
  2991. return stride;
  2992. }
  2993. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2994. {
  2995. switch (pixel_format) {
  2996. case DRM_FORMAT_C8:
  2997. return PLANE_CTL_FORMAT_INDEXED;
  2998. case DRM_FORMAT_RGB565:
  2999. return PLANE_CTL_FORMAT_RGB_565;
  3000. case DRM_FORMAT_XBGR8888:
  3001. case DRM_FORMAT_ABGR8888:
  3002. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  3003. case DRM_FORMAT_XRGB8888:
  3004. case DRM_FORMAT_ARGB8888:
  3005. return PLANE_CTL_FORMAT_XRGB_8888;
  3006. case DRM_FORMAT_XRGB2101010:
  3007. return PLANE_CTL_FORMAT_XRGB_2101010;
  3008. case DRM_FORMAT_XBGR2101010:
  3009. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  3010. case DRM_FORMAT_YUYV:
  3011. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  3012. case DRM_FORMAT_YVYU:
  3013. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  3014. case DRM_FORMAT_UYVY:
  3015. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  3016. case DRM_FORMAT_VYUY:
  3017. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  3018. case DRM_FORMAT_NV12:
  3019. return PLANE_CTL_FORMAT_NV12;
  3020. default:
  3021. MISSING_CASE(pixel_format);
  3022. }
  3023. return 0;
  3024. }
  3025. /*
  3026. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  3027. * to be already pre-multiplied. We need to add a knob (or a different
  3028. * DRM_FORMAT) for user-space to configure that.
  3029. */
  3030. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  3031. {
  3032. switch (pixel_format) {
  3033. case DRM_FORMAT_ABGR8888:
  3034. case DRM_FORMAT_ARGB8888:
  3035. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  3036. default:
  3037. return PLANE_CTL_ALPHA_DISABLE;
  3038. }
  3039. }
  3040. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  3041. {
  3042. switch (pixel_format) {
  3043. case DRM_FORMAT_ABGR8888:
  3044. case DRM_FORMAT_ARGB8888:
  3045. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  3046. default:
  3047. return PLANE_COLOR_ALPHA_DISABLE;
  3048. }
  3049. }
  3050. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  3051. {
  3052. switch (fb_modifier) {
  3053. case DRM_FORMAT_MOD_LINEAR:
  3054. break;
  3055. case I915_FORMAT_MOD_X_TILED:
  3056. return PLANE_CTL_TILED_X;
  3057. case I915_FORMAT_MOD_Y_TILED:
  3058. return PLANE_CTL_TILED_Y;
  3059. case I915_FORMAT_MOD_Y_TILED_CCS:
  3060. return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
  3061. case I915_FORMAT_MOD_Yf_TILED:
  3062. return PLANE_CTL_TILED_YF;
  3063. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3064. return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
  3065. default:
  3066. MISSING_CASE(fb_modifier);
  3067. }
  3068. return 0;
  3069. }
  3070. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  3071. {
  3072. switch (rotate) {
  3073. case DRM_MODE_ROTATE_0:
  3074. break;
  3075. /*
  3076. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3077. * while i915 HW rotation is clockwise, thats why this swapping.
  3078. */
  3079. case DRM_MODE_ROTATE_90:
  3080. return PLANE_CTL_ROTATE_270;
  3081. case DRM_MODE_ROTATE_180:
  3082. return PLANE_CTL_ROTATE_180;
  3083. case DRM_MODE_ROTATE_270:
  3084. return PLANE_CTL_ROTATE_90;
  3085. default:
  3086. MISSING_CASE(rotate);
  3087. }
  3088. return 0;
  3089. }
  3090. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3091. {
  3092. switch (reflect) {
  3093. case 0:
  3094. break;
  3095. case DRM_MODE_REFLECT_X:
  3096. return PLANE_CTL_FLIP_HORIZONTAL;
  3097. case DRM_MODE_REFLECT_Y:
  3098. default:
  3099. MISSING_CASE(reflect);
  3100. }
  3101. return 0;
  3102. }
  3103. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3104. const struct intel_plane_state *plane_state)
  3105. {
  3106. struct drm_i915_private *dev_priv =
  3107. to_i915(plane_state->base.plane->dev);
  3108. const struct drm_framebuffer *fb = plane_state->base.fb;
  3109. unsigned int rotation = plane_state->base.rotation;
  3110. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3111. u32 plane_ctl;
  3112. plane_ctl = PLANE_CTL_ENABLE;
  3113. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3114. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3115. plane_ctl |=
  3116. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3117. PLANE_CTL_PIPE_CSC_ENABLE |
  3118. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3119. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3120. plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
  3121. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3122. plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
  3123. }
  3124. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3125. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3126. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3127. if (INTEL_GEN(dev_priv) >= 10)
  3128. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3129. DRM_MODE_REFLECT_MASK);
  3130. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3131. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3132. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3133. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3134. return plane_ctl;
  3135. }
  3136. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3137. const struct intel_plane_state *plane_state)
  3138. {
  3139. struct drm_i915_private *dev_priv =
  3140. to_i915(plane_state->base.plane->dev);
  3141. const struct drm_framebuffer *fb = plane_state->base.fb;
  3142. u32 plane_color_ctl = 0;
  3143. if (INTEL_GEN(dev_priv) < 11) {
  3144. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3145. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3146. }
  3147. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3148. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3149. if (fb->format->is_yuv) {
  3150. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3151. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3152. else
  3153. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
  3154. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3155. plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
  3156. }
  3157. return plane_color_ctl;
  3158. }
  3159. static int
  3160. __intel_display_resume(struct drm_device *dev,
  3161. struct drm_atomic_state *state,
  3162. struct drm_modeset_acquire_ctx *ctx)
  3163. {
  3164. struct drm_crtc_state *crtc_state;
  3165. struct drm_crtc *crtc;
  3166. int i, ret;
  3167. intel_modeset_setup_hw_state(dev, ctx);
  3168. i915_redisable_vga(to_i915(dev));
  3169. if (!state)
  3170. return 0;
  3171. /*
  3172. * We've duplicated the state, pointers to the old state are invalid.
  3173. *
  3174. * Don't attempt to use the old state until we commit the duplicated state.
  3175. */
  3176. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3177. /*
  3178. * Force recalculation even if we restore
  3179. * current state. With fast modeset this may not result
  3180. * in a modeset when the state is compatible.
  3181. */
  3182. crtc_state->mode_changed = true;
  3183. }
  3184. /* ignore any reset values/BIOS leftovers in the WM registers */
  3185. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3186. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3187. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3188. WARN_ON(ret == -EDEADLK);
  3189. return ret;
  3190. }
  3191. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3192. {
  3193. return intel_has_gpu_reset(dev_priv) &&
  3194. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3195. }
  3196. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3197. {
  3198. struct drm_device *dev = &dev_priv->drm;
  3199. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3200. struct drm_atomic_state *state;
  3201. int ret;
  3202. /* reset doesn't touch the display */
  3203. if (!i915_modparams.force_reset_modeset_test &&
  3204. !gpu_reset_clobbers_display(dev_priv))
  3205. return;
  3206. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3207. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3208. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3209. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3210. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3211. i915_gem_set_wedged(dev_priv);
  3212. }
  3213. /*
  3214. * Need mode_config.mutex so that we don't
  3215. * trample ongoing ->detect() and whatnot.
  3216. */
  3217. mutex_lock(&dev->mode_config.mutex);
  3218. drm_modeset_acquire_init(ctx, 0);
  3219. while (1) {
  3220. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3221. if (ret != -EDEADLK)
  3222. break;
  3223. drm_modeset_backoff(ctx);
  3224. }
  3225. /*
  3226. * Disabling the crtcs gracefully seems nicer. Also the
  3227. * g33 docs say we should at least disable all the planes.
  3228. */
  3229. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3230. if (IS_ERR(state)) {
  3231. ret = PTR_ERR(state);
  3232. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3233. return;
  3234. }
  3235. ret = drm_atomic_helper_disable_all(dev, ctx);
  3236. if (ret) {
  3237. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3238. drm_atomic_state_put(state);
  3239. return;
  3240. }
  3241. dev_priv->modeset_restore_state = state;
  3242. state->acquire_ctx = ctx;
  3243. }
  3244. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3245. {
  3246. struct drm_device *dev = &dev_priv->drm;
  3247. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3248. struct drm_atomic_state *state;
  3249. int ret;
  3250. /* reset doesn't touch the display */
  3251. if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  3252. return;
  3253. state = fetch_and_zero(&dev_priv->modeset_restore_state);
  3254. if (!state)
  3255. goto unlock;
  3256. /* reset doesn't touch the display */
  3257. if (!gpu_reset_clobbers_display(dev_priv)) {
  3258. /* for testing only restore the display */
  3259. ret = __intel_display_resume(dev, state, ctx);
  3260. if (ret)
  3261. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3262. } else {
  3263. /*
  3264. * The display has been reset as well,
  3265. * so need a full re-initialization.
  3266. */
  3267. intel_runtime_pm_disable_interrupts(dev_priv);
  3268. intel_runtime_pm_enable_interrupts(dev_priv);
  3269. intel_pps_unlock_regs_wa(dev_priv);
  3270. intel_modeset_init_hw(dev);
  3271. intel_init_clock_gating(dev_priv);
  3272. spin_lock_irq(&dev_priv->irq_lock);
  3273. if (dev_priv->display.hpd_irq_setup)
  3274. dev_priv->display.hpd_irq_setup(dev_priv);
  3275. spin_unlock_irq(&dev_priv->irq_lock);
  3276. ret = __intel_display_resume(dev, state, ctx);
  3277. if (ret)
  3278. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3279. intel_hpd_init(dev_priv);
  3280. }
  3281. drm_atomic_state_put(state);
  3282. unlock:
  3283. drm_modeset_drop_locks(ctx);
  3284. drm_modeset_acquire_fini(ctx);
  3285. mutex_unlock(&dev->mode_config.mutex);
  3286. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3287. }
  3288. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3289. const struct intel_crtc_state *new_crtc_state)
  3290. {
  3291. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3292. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3293. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3294. crtc->base.mode = new_crtc_state->base.mode;
  3295. /*
  3296. * Update pipe size and adjust fitter if needed: the reason for this is
  3297. * that in compute_mode_changes we check the native mode (not the pfit
  3298. * mode) to see if we can flip rather than do a full mode set. In the
  3299. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3300. * pfit state, we'll end up with a big fb scanned out into the wrong
  3301. * sized surface.
  3302. */
  3303. I915_WRITE(PIPESRC(crtc->pipe),
  3304. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3305. (new_crtc_state->pipe_src_h - 1));
  3306. /* on skylake this is done by detaching scalers */
  3307. if (INTEL_GEN(dev_priv) >= 9) {
  3308. skl_detach_scalers(crtc);
  3309. if (new_crtc_state->pch_pfit.enabled)
  3310. skylake_pfit_enable(crtc);
  3311. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3312. if (new_crtc_state->pch_pfit.enabled)
  3313. ironlake_pfit_enable(crtc);
  3314. else if (old_crtc_state->pch_pfit.enabled)
  3315. ironlake_pfit_disable(crtc, true);
  3316. }
  3317. }
  3318. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3319. {
  3320. struct drm_device *dev = crtc->base.dev;
  3321. struct drm_i915_private *dev_priv = to_i915(dev);
  3322. int pipe = crtc->pipe;
  3323. i915_reg_t reg;
  3324. u32 temp;
  3325. /* enable normal train */
  3326. reg = FDI_TX_CTL(pipe);
  3327. temp = I915_READ(reg);
  3328. if (IS_IVYBRIDGE(dev_priv)) {
  3329. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3330. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3331. } else {
  3332. temp &= ~FDI_LINK_TRAIN_NONE;
  3333. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3334. }
  3335. I915_WRITE(reg, temp);
  3336. reg = FDI_RX_CTL(pipe);
  3337. temp = I915_READ(reg);
  3338. if (HAS_PCH_CPT(dev_priv)) {
  3339. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3340. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3341. } else {
  3342. temp &= ~FDI_LINK_TRAIN_NONE;
  3343. temp |= FDI_LINK_TRAIN_NONE;
  3344. }
  3345. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3346. /* wait one idle pattern time */
  3347. POSTING_READ(reg);
  3348. udelay(1000);
  3349. /* IVB wants error correction enabled */
  3350. if (IS_IVYBRIDGE(dev_priv))
  3351. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3352. FDI_FE_ERRC_ENABLE);
  3353. }
  3354. /* The FDI link training functions for ILK/Ibexpeak. */
  3355. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3356. const struct intel_crtc_state *crtc_state)
  3357. {
  3358. struct drm_device *dev = crtc->base.dev;
  3359. struct drm_i915_private *dev_priv = to_i915(dev);
  3360. int pipe = crtc->pipe;
  3361. i915_reg_t reg;
  3362. u32 temp, tries;
  3363. /* FDI needs bits from pipe first */
  3364. assert_pipe_enabled(dev_priv, pipe);
  3365. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3366. for train result */
  3367. reg = FDI_RX_IMR(pipe);
  3368. temp = I915_READ(reg);
  3369. temp &= ~FDI_RX_SYMBOL_LOCK;
  3370. temp &= ~FDI_RX_BIT_LOCK;
  3371. I915_WRITE(reg, temp);
  3372. I915_READ(reg);
  3373. udelay(150);
  3374. /* enable CPU FDI TX and PCH FDI RX */
  3375. reg = FDI_TX_CTL(pipe);
  3376. temp = I915_READ(reg);
  3377. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3378. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3379. temp &= ~FDI_LINK_TRAIN_NONE;
  3380. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3381. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3382. reg = FDI_RX_CTL(pipe);
  3383. temp = I915_READ(reg);
  3384. temp &= ~FDI_LINK_TRAIN_NONE;
  3385. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3386. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3387. POSTING_READ(reg);
  3388. udelay(150);
  3389. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3390. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3391. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3392. FDI_RX_PHASE_SYNC_POINTER_EN);
  3393. reg = FDI_RX_IIR(pipe);
  3394. for (tries = 0; tries < 5; tries++) {
  3395. temp = I915_READ(reg);
  3396. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3397. if ((temp & FDI_RX_BIT_LOCK)) {
  3398. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3399. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3400. break;
  3401. }
  3402. }
  3403. if (tries == 5)
  3404. DRM_ERROR("FDI train 1 fail!\n");
  3405. /* Train 2 */
  3406. reg = FDI_TX_CTL(pipe);
  3407. temp = I915_READ(reg);
  3408. temp &= ~FDI_LINK_TRAIN_NONE;
  3409. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3410. I915_WRITE(reg, temp);
  3411. reg = FDI_RX_CTL(pipe);
  3412. temp = I915_READ(reg);
  3413. temp &= ~FDI_LINK_TRAIN_NONE;
  3414. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3415. I915_WRITE(reg, temp);
  3416. POSTING_READ(reg);
  3417. udelay(150);
  3418. reg = FDI_RX_IIR(pipe);
  3419. for (tries = 0; tries < 5; tries++) {
  3420. temp = I915_READ(reg);
  3421. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3422. if (temp & FDI_RX_SYMBOL_LOCK) {
  3423. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3424. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3425. break;
  3426. }
  3427. }
  3428. if (tries == 5)
  3429. DRM_ERROR("FDI train 2 fail!\n");
  3430. DRM_DEBUG_KMS("FDI train done\n");
  3431. }
  3432. static const int snb_b_fdi_train_param[] = {
  3433. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3434. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3435. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3436. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3437. };
  3438. /* The FDI link training functions for SNB/Cougarpoint. */
  3439. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3440. const struct intel_crtc_state *crtc_state)
  3441. {
  3442. struct drm_device *dev = crtc->base.dev;
  3443. struct drm_i915_private *dev_priv = to_i915(dev);
  3444. int pipe = crtc->pipe;
  3445. i915_reg_t reg;
  3446. u32 temp, i, retry;
  3447. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3448. for train result */
  3449. reg = FDI_RX_IMR(pipe);
  3450. temp = I915_READ(reg);
  3451. temp &= ~FDI_RX_SYMBOL_LOCK;
  3452. temp &= ~FDI_RX_BIT_LOCK;
  3453. I915_WRITE(reg, temp);
  3454. POSTING_READ(reg);
  3455. udelay(150);
  3456. /* enable CPU FDI TX and PCH FDI RX */
  3457. reg = FDI_TX_CTL(pipe);
  3458. temp = I915_READ(reg);
  3459. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3460. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3461. temp &= ~FDI_LINK_TRAIN_NONE;
  3462. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3463. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3464. /* SNB-B */
  3465. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3466. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3467. I915_WRITE(FDI_RX_MISC(pipe),
  3468. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3469. reg = FDI_RX_CTL(pipe);
  3470. temp = I915_READ(reg);
  3471. if (HAS_PCH_CPT(dev_priv)) {
  3472. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3473. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3474. } else {
  3475. temp &= ~FDI_LINK_TRAIN_NONE;
  3476. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3477. }
  3478. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3479. POSTING_READ(reg);
  3480. udelay(150);
  3481. for (i = 0; i < 4; i++) {
  3482. reg = FDI_TX_CTL(pipe);
  3483. temp = I915_READ(reg);
  3484. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3485. temp |= snb_b_fdi_train_param[i];
  3486. I915_WRITE(reg, temp);
  3487. POSTING_READ(reg);
  3488. udelay(500);
  3489. for (retry = 0; retry < 5; retry++) {
  3490. reg = FDI_RX_IIR(pipe);
  3491. temp = I915_READ(reg);
  3492. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3493. if (temp & FDI_RX_BIT_LOCK) {
  3494. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3495. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3496. break;
  3497. }
  3498. udelay(50);
  3499. }
  3500. if (retry < 5)
  3501. break;
  3502. }
  3503. if (i == 4)
  3504. DRM_ERROR("FDI train 1 fail!\n");
  3505. /* Train 2 */
  3506. reg = FDI_TX_CTL(pipe);
  3507. temp = I915_READ(reg);
  3508. temp &= ~FDI_LINK_TRAIN_NONE;
  3509. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3510. if (IS_GEN6(dev_priv)) {
  3511. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3512. /* SNB-B */
  3513. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3514. }
  3515. I915_WRITE(reg, temp);
  3516. reg = FDI_RX_CTL(pipe);
  3517. temp = I915_READ(reg);
  3518. if (HAS_PCH_CPT(dev_priv)) {
  3519. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3520. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3521. } else {
  3522. temp &= ~FDI_LINK_TRAIN_NONE;
  3523. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3524. }
  3525. I915_WRITE(reg, temp);
  3526. POSTING_READ(reg);
  3527. udelay(150);
  3528. for (i = 0; i < 4; i++) {
  3529. reg = FDI_TX_CTL(pipe);
  3530. temp = I915_READ(reg);
  3531. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3532. temp |= snb_b_fdi_train_param[i];
  3533. I915_WRITE(reg, temp);
  3534. POSTING_READ(reg);
  3535. udelay(500);
  3536. for (retry = 0; retry < 5; retry++) {
  3537. reg = FDI_RX_IIR(pipe);
  3538. temp = I915_READ(reg);
  3539. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3540. if (temp & FDI_RX_SYMBOL_LOCK) {
  3541. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3542. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3543. break;
  3544. }
  3545. udelay(50);
  3546. }
  3547. if (retry < 5)
  3548. break;
  3549. }
  3550. if (i == 4)
  3551. DRM_ERROR("FDI train 2 fail!\n");
  3552. DRM_DEBUG_KMS("FDI train done.\n");
  3553. }
  3554. /* Manual link training for Ivy Bridge A0 parts */
  3555. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3556. const struct intel_crtc_state *crtc_state)
  3557. {
  3558. struct drm_device *dev = crtc->base.dev;
  3559. struct drm_i915_private *dev_priv = to_i915(dev);
  3560. int pipe = crtc->pipe;
  3561. i915_reg_t reg;
  3562. u32 temp, i, j;
  3563. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3564. for train result */
  3565. reg = FDI_RX_IMR(pipe);
  3566. temp = I915_READ(reg);
  3567. temp &= ~FDI_RX_SYMBOL_LOCK;
  3568. temp &= ~FDI_RX_BIT_LOCK;
  3569. I915_WRITE(reg, temp);
  3570. POSTING_READ(reg);
  3571. udelay(150);
  3572. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3573. I915_READ(FDI_RX_IIR(pipe)));
  3574. /* Try each vswing and preemphasis setting twice before moving on */
  3575. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3576. /* disable first in case we need to retry */
  3577. reg = FDI_TX_CTL(pipe);
  3578. temp = I915_READ(reg);
  3579. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3580. temp &= ~FDI_TX_ENABLE;
  3581. I915_WRITE(reg, temp);
  3582. reg = FDI_RX_CTL(pipe);
  3583. temp = I915_READ(reg);
  3584. temp &= ~FDI_LINK_TRAIN_AUTO;
  3585. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3586. temp &= ~FDI_RX_ENABLE;
  3587. I915_WRITE(reg, temp);
  3588. /* enable CPU FDI TX and PCH FDI RX */
  3589. reg = FDI_TX_CTL(pipe);
  3590. temp = I915_READ(reg);
  3591. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3592. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3593. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3594. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3595. temp |= snb_b_fdi_train_param[j/2];
  3596. temp |= FDI_COMPOSITE_SYNC;
  3597. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3598. I915_WRITE(FDI_RX_MISC(pipe),
  3599. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3600. reg = FDI_RX_CTL(pipe);
  3601. temp = I915_READ(reg);
  3602. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3603. temp |= FDI_COMPOSITE_SYNC;
  3604. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3605. POSTING_READ(reg);
  3606. udelay(1); /* should be 0.5us */
  3607. for (i = 0; i < 4; i++) {
  3608. reg = FDI_RX_IIR(pipe);
  3609. temp = I915_READ(reg);
  3610. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3611. if (temp & FDI_RX_BIT_LOCK ||
  3612. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3613. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3614. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3615. i);
  3616. break;
  3617. }
  3618. udelay(1); /* should be 0.5us */
  3619. }
  3620. if (i == 4) {
  3621. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3622. continue;
  3623. }
  3624. /* Train 2 */
  3625. reg = FDI_TX_CTL(pipe);
  3626. temp = I915_READ(reg);
  3627. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3628. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3629. I915_WRITE(reg, temp);
  3630. reg = FDI_RX_CTL(pipe);
  3631. temp = I915_READ(reg);
  3632. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3633. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3634. I915_WRITE(reg, temp);
  3635. POSTING_READ(reg);
  3636. udelay(2); /* should be 1.5us */
  3637. for (i = 0; i < 4; i++) {
  3638. reg = FDI_RX_IIR(pipe);
  3639. temp = I915_READ(reg);
  3640. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3641. if (temp & FDI_RX_SYMBOL_LOCK ||
  3642. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3643. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3644. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3645. i);
  3646. goto train_done;
  3647. }
  3648. udelay(2); /* should be 1.5us */
  3649. }
  3650. if (i == 4)
  3651. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3652. }
  3653. train_done:
  3654. DRM_DEBUG_KMS("FDI train done.\n");
  3655. }
  3656. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3657. {
  3658. struct drm_device *dev = intel_crtc->base.dev;
  3659. struct drm_i915_private *dev_priv = to_i915(dev);
  3660. int pipe = intel_crtc->pipe;
  3661. i915_reg_t reg;
  3662. u32 temp;
  3663. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3664. reg = FDI_RX_CTL(pipe);
  3665. temp = I915_READ(reg);
  3666. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3667. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3668. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3669. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3670. POSTING_READ(reg);
  3671. udelay(200);
  3672. /* Switch from Rawclk to PCDclk */
  3673. temp = I915_READ(reg);
  3674. I915_WRITE(reg, temp | FDI_PCDCLK);
  3675. POSTING_READ(reg);
  3676. udelay(200);
  3677. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3678. reg = FDI_TX_CTL(pipe);
  3679. temp = I915_READ(reg);
  3680. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3681. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3682. POSTING_READ(reg);
  3683. udelay(100);
  3684. }
  3685. }
  3686. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3687. {
  3688. struct drm_device *dev = intel_crtc->base.dev;
  3689. struct drm_i915_private *dev_priv = to_i915(dev);
  3690. int pipe = intel_crtc->pipe;
  3691. i915_reg_t reg;
  3692. u32 temp;
  3693. /* Switch from PCDclk to Rawclk */
  3694. reg = FDI_RX_CTL(pipe);
  3695. temp = I915_READ(reg);
  3696. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3697. /* Disable CPU FDI TX PLL */
  3698. reg = FDI_TX_CTL(pipe);
  3699. temp = I915_READ(reg);
  3700. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3701. POSTING_READ(reg);
  3702. udelay(100);
  3703. reg = FDI_RX_CTL(pipe);
  3704. temp = I915_READ(reg);
  3705. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3706. /* Wait for the clocks to turn off. */
  3707. POSTING_READ(reg);
  3708. udelay(100);
  3709. }
  3710. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3711. {
  3712. struct drm_device *dev = crtc->dev;
  3713. struct drm_i915_private *dev_priv = to_i915(dev);
  3714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3715. int pipe = intel_crtc->pipe;
  3716. i915_reg_t reg;
  3717. u32 temp;
  3718. /* disable CPU FDI tx and PCH FDI rx */
  3719. reg = FDI_TX_CTL(pipe);
  3720. temp = I915_READ(reg);
  3721. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3722. POSTING_READ(reg);
  3723. reg = FDI_RX_CTL(pipe);
  3724. temp = I915_READ(reg);
  3725. temp &= ~(0x7 << 16);
  3726. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3727. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3728. POSTING_READ(reg);
  3729. udelay(100);
  3730. /* Ironlake workaround, disable clock pointer after downing FDI */
  3731. if (HAS_PCH_IBX(dev_priv))
  3732. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3733. /* still set train pattern 1 */
  3734. reg = FDI_TX_CTL(pipe);
  3735. temp = I915_READ(reg);
  3736. temp &= ~FDI_LINK_TRAIN_NONE;
  3737. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3738. I915_WRITE(reg, temp);
  3739. reg = FDI_RX_CTL(pipe);
  3740. temp = I915_READ(reg);
  3741. if (HAS_PCH_CPT(dev_priv)) {
  3742. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3743. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3744. } else {
  3745. temp &= ~FDI_LINK_TRAIN_NONE;
  3746. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3747. }
  3748. /* BPC in FDI rx is consistent with that in PIPECONF */
  3749. temp &= ~(0x07 << 16);
  3750. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3751. I915_WRITE(reg, temp);
  3752. POSTING_READ(reg);
  3753. udelay(100);
  3754. }
  3755. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3756. {
  3757. struct drm_crtc *crtc;
  3758. bool cleanup_done;
  3759. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3760. struct drm_crtc_commit *commit;
  3761. spin_lock(&crtc->commit_lock);
  3762. commit = list_first_entry_or_null(&crtc->commit_list,
  3763. struct drm_crtc_commit, commit_entry);
  3764. cleanup_done = commit ?
  3765. try_wait_for_completion(&commit->cleanup_done) : true;
  3766. spin_unlock(&crtc->commit_lock);
  3767. if (cleanup_done)
  3768. continue;
  3769. drm_crtc_wait_one_vblank(crtc);
  3770. return true;
  3771. }
  3772. return false;
  3773. }
  3774. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3775. {
  3776. u32 temp;
  3777. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3778. mutex_lock(&dev_priv->sb_lock);
  3779. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3780. temp |= SBI_SSCCTL_DISABLE;
  3781. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3782. mutex_unlock(&dev_priv->sb_lock);
  3783. }
  3784. /* Program iCLKIP clock to the desired frequency */
  3785. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3786. {
  3787. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3788. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3789. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3790. u32 temp;
  3791. lpt_disable_iclkip(dev_priv);
  3792. /* The iCLK virtual clock root frequency is in MHz,
  3793. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3794. * divisors, it is necessary to divide one by another, so we
  3795. * convert the virtual clock precision to KHz here for higher
  3796. * precision.
  3797. */
  3798. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3799. u32 iclk_virtual_root_freq = 172800 * 1000;
  3800. u32 iclk_pi_range = 64;
  3801. u32 desired_divisor;
  3802. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3803. clock << auxdiv);
  3804. divsel = (desired_divisor / iclk_pi_range) - 2;
  3805. phaseinc = desired_divisor % iclk_pi_range;
  3806. /*
  3807. * Near 20MHz is a corner case which is
  3808. * out of range for the 7-bit divisor
  3809. */
  3810. if (divsel <= 0x7f)
  3811. break;
  3812. }
  3813. /* This should not happen with any sane values */
  3814. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3815. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3816. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3817. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3818. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3819. clock,
  3820. auxdiv,
  3821. divsel,
  3822. phasedir,
  3823. phaseinc);
  3824. mutex_lock(&dev_priv->sb_lock);
  3825. /* Program SSCDIVINTPHASE6 */
  3826. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3827. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3828. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3829. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3830. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3831. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3832. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3833. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3834. /* Program SSCAUXDIV */
  3835. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3836. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3837. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3838. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3839. /* Enable modulator and associated divider */
  3840. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3841. temp &= ~SBI_SSCCTL_DISABLE;
  3842. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3843. mutex_unlock(&dev_priv->sb_lock);
  3844. /* Wait for initialization time */
  3845. udelay(24);
  3846. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3847. }
  3848. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3849. {
  3850. u32 divsel, phaseinc, auxdiv;
  3851. u32 iclk_virtual_root_freq = 172800 * 1000;
  3852. u32 iclk_pi_range = 64;
  3853. u32 desired_divisor;
  3854. u32 temp;
  3855. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3856. return 0;
  3857. mutex_lock(&dev_priv->sb_lock);
  3858. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3859. if (temp & SBI_SSCCTL_DISABLE) {
  3860. mutex_unlock(&dev_priv->sb_lock);
  3861. return 0;
  3862. }
  3863. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3864. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3865. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3866. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3867. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3868. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3869. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3870. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3871. mutex_unlock(&dev_priv->sb_lock);
  3872. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3873. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3874. desired_divisor << auxdiv);
  3875. }
  3876. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3877. enum pipe pch_transcoder)
  3878. {
  3879. struct drm_device *dev = crtc->base.dev;
  3880. struct drm_i915_private *dev_priv = to_i915(dev);
  3881. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3882. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3883. I915_READ(HTOTAL(cpu_transcoder)));
  3884. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3885. I915_READ(HBLANK(cpu_transcoder)));
  3886. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3887. I915_READ(HSYNC(cpu_transcoder)));
  3888. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3889. I915_READ(VTOTAL(cpu_transcoder)));
  3890. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3891. I915_READ(VBLANK(cpu_transcoder)));
  3892. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3893. I915_READ(VSYNC(cpu_transcoder)));
  3894. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3895. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3896. }
  3897. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3898. {
  3899. struct drm_i915_private *dev_priv = to_i915(dev);
  3900. uint32_t temp;
  3901. temp = I915_READ(SOUTH_CHICKEN1);
  3902. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3903. return;
  3904. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3905. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3906. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3907. if (enable)
  3908. temp |= FDI_BC_BIFURCATION_SELECT;
  3909. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3910. I915_WRITE(SOUTH_CHICKEN1, temp);
  3911. POSTING_READ(SOUTH_CHICKEN1);
  3912. }
  3913. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3914. {
  3915. struct drm_device *dev = intel_crtc->base.dev;
  3916. switch (intel_crtc->pipe) {
  3917. case PIPE_A:
  3918. break;
  3919. case PIPE_B:
  3920. if (intel_crtc->config->fdi_lanes > 2)
  3921. cpt_set_fdi_bc_bifurcation(dev, false);
  3922. else
  3923. cpt_set_fdi_bc_bifurcation(dev, true);
  3924. break;
  3925. case PIPE_C:
  3926. cpt_set_fdi_bc_bifurcation(dev, true);
  3927. break;
  3928. default:
  3929. BUG();
  3930. }
  3931. }
  3932. /*
  3933. * Finds the encoder associated with the given CRTC. This can only be
  3934. * used when we know that the CRTC isn't feeding multiple encoders!
  3935. */
  3936. static struct intel_encoder *
  3937. intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
  3938. const struct intel_crtc_state *crtc_state)
  3939. {
  3940. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3941. const struct drm_connector_state *connector_state;
  3942. const struct drm_connector *connector;
  3943. struct intel_encoder *encoder = NULL;
  3944. int num_encoders = 0;
  3945. int i;
  3946. for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
  3947. if (connector_state->crtc != &crtc->base)
  3948. continue;
  3949. encoder = to_intel_encoder(connector_state->best_encoder);
  3950. num_encoders++;
  3951. }
  3952. WARN(num_encoders != 1, "%d encoders for pipe %c\n",
  3953. num_encoders, pipe_name(crtc->pipe));
  3954. return encoder;
  3955. }
  3956. /*
  3957. * Enable PCH resources required for PCH ports:
  3958. * - PCH PLLs
  3959. * - FDI training & RX/TX
  3960. * - update transcoder timings
  3961. * - DP transcoding bits
  3962. * - transcoder
  3963. */
  3964. static void ironlake_pch_enable(const struct intel_atomic_state *state,
  3965. const struct intel_crtc_state *crtc_state)
  3966. {
  3967. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3968. struct drm_device *dev = crtc->base.dev;
  3969. struct drm_i915_private *dev_priv = to_i915(dev);
  3970. int pipe = crtc->pipe;
  3971. u32 temp;
  3972. assert_pch_transcoder_disabled(dev_priv, pipe);
  3973. if (IS_IVYBRIDGE(dev_priv))
  3974. ivybridge_update_fdi_bc_bifurcation(crtc);
  3975. /* Write the TU size bits before fdi link training, so that error
  3976. * detection works. */
  3977. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3978. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3979. /* For PCH output, training FDI link */
  3980. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3981. /* We need to program the right clock selection before writing the pixel
  3982. * mutliplier into the DPLL. */
  3983. if (HAS_PCH_CPT(dev_priv)) {
  3984. u32 sel;
  3985. temp = I915_READ(PCH_DPLL_SEL);
  3986. temp |= TRANS_DPLL_ENABLE(pipe);
  3987. sel = TRANS_DPLLB_SEL(pipe);
  3988. if (crtc_state->shared_dpll ==
  3989. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3990. temp |= sel;
  3991. else
  3992. temp &= ~sel;
  3993. I915_WRITE(PCH_DPLL_SEL, temp);
  3994. }
  3995. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3996. * transcoder, and we actually should do this to not upset any PCH
  3997. * transcoder that already use the clock when we share it.
  3998. *
  3999. * Note that enable_shared_dpll tries to do the right thing, but
  4000. * get_shared_dpll unconditionally resets the pll - we need that to have
  4001. * the right LVDS enable sequence. */
  4002. intel_enable_shared_dpll(crtc);
  4003. /* set transcoder timing, panel must allow it */
  4004. assert_panel_unlocked(dev_priv, pipe);
  4005. ironlake_pch_transcoder_set_timings(crtc, pipe);
  4006. intel_fdi_normal_train(crtc);
  4007. /* For PCH DP, enable TRANS_DP_CTL */
  4008. if (HAS_PCH_CPT(dev_priv) &&
  4009. intel_crtc_has_dp_encoder(crtc_state)) {
  4010. const struct drm_display_mode *adjusted_mode =
  4011. &crtc_state->base.adjusted_mode;
  4012. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  4013. i915_reg_t reg = TRANS_DP_CTL(pipe);
  4014. enum port port;
  4015. temp = I915_READ(reg);
  4016. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  4017. TRANS_DP_SYNC_MASK |
  4018. TRANS_DP_BPC_MASK);
  4019. temp |= TRANS_DP_OUTPUT_ENABLE;
  4020. temp |= bpc << 9; /* same format but at 11:9 */
  4021. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  4022. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  4023. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  4024. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  4025. port = intel_get_crtc_new_encoder(state, crtc_state)->port;
  4026. WARN_ON(port < PORT_B || port > PORT_D);
  4027. temp |= TRANS_DP_PORT_SEL(port);
  4028. I915_WRITE(reg, temp);
  4029. }
  4030. ironlake_enable_pch_transcoder(dev_priv, pipe);
  4031. }
  4032. static void lpt_pch_enable(const struct intel_atomic_state *state,
  4033. const struct intel_crtc_state *crtc_state)
  4034. {
  4035. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4036. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4037. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  4038. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4039. lpt_program_iclkip(crtc);
  4040. /* Set transcoder timing. */
  4041. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4042. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4043. }
  4044. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4045. {
  4046. struct drm_i915_private *dev_priv = to_i915(dev);
  4047. i915_reg_t dslreg = PIPEDSL(pipe);
  4048. u32 temp;
  4049. temp = I915_READ(dslreg);
  4050. udelay(500);
  4051. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4052. if (wait_for(I915_READ(dslreg) != temp, 5))
  4053. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4054. }
  4055. }
  4056. /*
  4057. * The hardware phase 0.0 refers to the center of the pixel.
  4058. * We want to start from the top/left edge which is phase
  4059. * -0.5. That matches how the hardware calculates the scaling
  4060. * factors (from top-left of the first pixel to bottom-right
  4061. * of the last pixel, as opposed to the pixel centers).
  4062. *
  4063. * For 4:2:0 subsampled chroma planes we obviously have to
  4064. * adjust that so that the chroma sample position lands in
  4065. * the right spot.
  4066. *
  4067. * Note that for packed YCbCr 4:2:2 formats there is no way to
  4068. * control chroma siting. The hardware simply replicates the
  4069. * chroma samples for both of the luma samples, and thus we don't
  4070. * actually get the expected MPEG2 chroma siting convention :(
  4071. * The same behaviour is observed on pre-SKL platforms as well.
  4072. */
  4073. u16 skl_scaler_calc_phase(int sub, bool chroma_cosited)
  4074. {
  4075. int phase = -0x8000;
  4076. u16 trip = 0;
  4077. if (chroma_cosited)
  4078. phase += (sub - 1) * 0x8000 / sub;
  4079. if (phase < 0)
  4080. phase = 0x10000 + phase;
  4081. else
  4082. trip = PS_PHASE_TRIP;
  4083. return ((phase >> 2) & PS_PHASE_MASK) | trip;
  4084. }
  4085. static int
  4086. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4087. unsigned int scaler_user, int *scaler_id,
  4088. int src_w, int src_h, int dst_w, int dst_h,
  4089. bool plane_scaler_check,
  4090. uint32_t pixel_format)
  4091. {
  4092. struct intel_crtc_scaler_state *scaler_state =
  4093. &crtc_state->scaler_state;
  4094. struct intel_crtc *intel_crtc =
  4095. to_intel_crtc(crtc_state->base.crtc);
  4096. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4097. const struct drm_display_mode *adjusted_mode =
  4098. &crtc_state->base.adjusted_mode;
  4099. int need_scaling;
  4100. /*
  4101. * Src coordinates are already rotated by 270 degrees for
  4102. * the 90/270 degree plane rotation cases (to match the
  4103. * GTT mapping), hence no need to account for rotation here.
  4104. */
  4105. need_scaling = src_w != dst_w || src_h != dst_h;
  4106. if (plane_scaler_check)
  4107. if (pixel_format == DRM_FORMAT_NV12)
  4108. need_scaling = true;
  4109. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4110. need_scaling = true;
  4111. /*
  4112. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4113. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4114. * Once NV12 is enabled, handle it here while allocating scaler
  4115. * for NV12.
  4116. */
  4117. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4118. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4119. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4120. return -EINVAL;
  4121. }
  4122. /*
  4123. * if plane is being disabled or scaler is no more required or force detach
  4124. * - free scaler binded to this plane/crtc
  4125. * - in order to do this, update crtc->scaler_usage
  4126. *
  4127. * Here scaler state in crtc_state is set free so that
  4128. * scaler can be assigned to other user. Actual register
  4129. * update to free the scaler is done in plane/panel-fit programming.
  4130. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4131. */
  4132. if (force_detach || !need_scaling) {
  4133. if (*scaler_id >= 0) {
  4134. scaler_state->scaler_users &= ~(1 << scaler_user);
  4135. scaler_state->scalers[*scaler_id].in_use = 0;
  4136. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4137. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4138. intel_crtc->pipe, scaler_user, *scaler_id,
  4139. scaler_state->scaler_users);
  4140. *scaler_id = -1;
  4141. }
  4142. return 0;
  4143. }
  4144. if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
  4145. (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
  4146. DRM_DEBUG_KMS("NV12: src dimensions not met\n");
  4147. return -EINVAL;
  4148. }
  4149. /* range checks */
  4150. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4151. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4152. (IS_GEN11(dev_priv) &&
  4153. (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
  4154. dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
  4155. (!IS_GEN11(dev_priv) &&
  4156. (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4157. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
  4158. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4159. "size is out of scaler range\n",
  4160. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4161. return -EINVAL;
  4162. }
  4163. /* mark this plane as a scaler user in crtc_state */
  4164. scaler_state->scaler_users |= (1 << scaler_user);
  4165. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4166. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4167. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4168. scaler_state->scaler_users);
  4169. return 0;
  4170. }
  4171. /**
  4172. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4173. *
  4174. * @state: crtc's scaler state
  4175. *
  4176. * Return
  4177. * 0 - scaler_usage updated successfully
  4178. * error - requested scaling cannot be supported or other error condition
  4179. */
  4180. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4181. {
  4182. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4183. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4184. &state->scaler_state.scaler_id,
  4185. state->pipe_src_w, state->pipe_src_h,
  4186. adjusted_mode->crtc_hdisplay,
  4187. adjusted_mode->crtc_vdisplay, false, 0);
  4188. }
  4189. /**
  4190. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4191. * @crtc_state: crtc's scaler state
  4192. * @plane_state: atomic plane state to update
  4193. *
  4194. * Return
  4195. * 0 - scaler_usage updated successfully
  4196. * error - requested scaling cannot be supported or other error condition
  4197. */
  4198. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4199. struct intel_plane_state *plane_state)
  4200. {
  4201. struct intel_plane *intel_plane =
  4202. to_intel_plane(plane_state->base.plane);
  4203. struct drm_framebuffer *fb = plane_state->base.fb;
  4204. int ret;
  4205. bool force_detach = !fb || !plane_state->base.visible;
  4206. ret = skl_update_scaler(crtc_state, force_detach,
  4207. drm_plane_index(&intel_plane->base),
  4208. &plane_state->scaler_id,
  4209. drm_rect_width(&plane_state->base.src) >> 16,
  4210. drm_rect_height(&plane_state->base.src) >> 16,
  4211. drm_rect_width(&plane_state->base.dst),
  4212. drm_rect_height(&plane_state->base.dst),
  4213. fb ? true : false, fb ? fb->format->format : 0);
  4214. if (ret || plane_state->scaler_id < 0)
  4215. return ret;
  4216. /* check colorkey */
  4217. if (plane_state->ckey.flags) {
  4218. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4219. intel_plane->base.base.id,
  4220. intel_plane->base.name);
  4221. return -EINVAL;
  4222. }
  4223. /* Check src format */
  4224. switch (fb->format->format) {
  4225. case DRM_FORMAT_RGB565:
  4226. case DRM_FORMAT_XBGR8888:
  4227. case DRM_FORMAT_XRGB8888:
  4228. case DRM_FORMAT_ABGR8888:
  4229. case DRM_FORMAT_ARGB8888:
  4230. case DRM_FORMAT_XRGB2101010:
  4231. case DRM_FORMAT_XBGR2101010:
  4232. case DRM_FORMAT_YUYV:
  4233. case DRM_FORMAT_YVYU:
  4234. case DRM_FORMAT_UYVY:
  4235. case DRM_FORMAT_VYUY:
  4236. case DRM_FORMAT_NV12:
  4237. break;
  4238. default:
  4239. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4240. intel_plane->base.base.id, intel_plane->base.name,
  4241. fb->base.id, fb->format->format);
  4242. return -EINVAL;
  4243. }
  4244. return 0;
  4245. }
  4246. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4247. {
  4248. int i;
  4249. for (i = 0; i < crtc->num_scalers; i++)
  4250. skl_detach_scaler(crtc, i);
  4251. }
  4252. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4253. {
  4254. struct drm_device *dev = crtc->base.dev;
  4255. struct drm_i915_private *dev_priv = to_i915(dev);
  4256. int pipe = crtc->pipe;
  4257. struct intel_crtc_scaler_state *scaler_state =
  4258. &crtc->config->scaler_state;
  4259. if (crtc->config->pch_pfit.enabled) {
  4260. u16 uv_rgb_hphase, uv_rgb_vphase;
  4261. int id;
  4262. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4263. return;
  4264. uv_rgb_hphase = skl_scaler_calc_phase(1, false);
  4265. uv_rgb_vphase = skl_scaler_calc_phase(1, false);
  4266. id = scaler_state->scaler_id;
  4267. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4268. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4269. I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
  4270. PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
  4271. I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
  4272. PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
  4273. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4274. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4275. }
  4276. }
  4277. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4278. {
  4279. struct drm_device *dev = crtc->base.dev;
  4280. struct drm_i915_private *dev_priv = to_i915(dev);
  4281. int pipe = crtc->pipe;
  4282. if (crtc->config->pch_pfit.enabled) {
  4283. /* Force use of hard-coded filter coefficients
  4284. * as some pre-programmed values are broken,
  4285. * e.g. x201.
  4286. */
  4287. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4288. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4289. PF_PIPE_SEL_IVB(pipe));
  4290. else
  4291. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4292. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4293. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4294. }
  4295. }
  4296. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4297. {
  4298. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4299. struct drm_device *dev = crtc->base.dev;
  4300. struct drm_i915_private *dev_priv = to_i915(dev);
  4301. if (!crtc_state->ips_enabled)
  4302. return;
  4303. /*
  4304. * We can only enable IPS after we enable a plane and wait for a vblank
  4305. * This function is called from post_plane_update, which is run after
  4306. * a vblank wait.
  4307. */
  4308. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4309. if (IS_BROADWELL(dev_priv)) {
  4310. mutex_lock(&dev_priv->pcu_lock);
  4311. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4312. IPS_ENABLE | IPS_PCODE_CONTROL));
  4313. mutex_unlock(&dev_priv->pcu_lock);
  4314. /* Quoting Art Runyan: "its not safe to expect any particular
  4315. * value in IPS_CTL bit 31 after enabling IPS through the
  4316. * mailbox." Moreover, the mailbox may return a bogus state,
  4317. * so we need to just enable it and continue on.
  4318. */
  4319. } else {
  4320. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4321. /* The bit only becomes 1 in the next vblank, so this wait here
  4322. * is essentially intel_wait_for_vblank. If we don't have this
  4323. * and don't wait for vblanks until the end of crtc_enable, then
  4324. * the HW state readout code will complain that the expected
  4325. * IPS_CTL value is not the one we read. */
  4326. if (intel_wait_for_register(dev_priv,
  4327. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4328. 50))
  4329. DRM_ERROR("Timed out waiting for IPS enable\n");
  4330. }
  4331. }
  4332. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4333. {
  4334. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4335. struct drm_device *dev = crtc->base.dev;
  4336. struct drm_i915_private *dev_priv = to_i915(dev);
  4337. if (!crtc_state->ips_enabled)
  4338. return;
  4339. if (IS_BROADWELL(dev_priv)) {
  4340. mutex_lock(&dev_priv->pcu_lock);
  4341. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4342. mutex_unlock(&dev_priv->pcu_lock);
  4343. /*
  4344. * Wait for PCODE to finish disabling IPS. The BSpec specified
  4345. * 42ms timeout value leads to occasional timeouts so use 100ms
  4346. * instead.
  4347. */
  4348. if (intel_wait_for_register(dev_priv,
  4349. IPS_CTL, IPS_ENABLE, 0,
  4350. 100))
  4351. DRM_ERROR("Timed out waiting for IPS disable\n");
  4352. } else {
  4353. I915_WRITE(IPS_CTL, 0);
  4354. POSTING_READ(IPS_CTL);
  4355. }
  4356. /* We need to wait for a vblank before we can disable the plane. */
  4357. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4358. }
  4359. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4360. {
  4361. if (intel_crtc->overlay) {
  4362. struct drm_device *dev = intel_crtc->base.dev;
  4363. mutex_lock(&dev->struct_mutex);
  4364. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4365. mutex_unlock(&dev->struct_mutex);
  4366. }
  4367. /* Let userspace switch the overlay on again. In most cases userspace
  4368. * has to recompute where to put it anyway.
  4369. */
  4370. }
  4371. /**
  4372. * intel_post_enable_primary - Perform operations after enabling primary plane
  4373. * @crtc: the CRTC whose primary plane was just enabled
  4374. * @new_crtc_state: the enabling state
  4375. *
  4376. * Performs potentially sleeping operations that must be done after the primary
  4377. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4378. * called due to an explicit primary plane update, or due to an implicit
  4379. * re-enable that is caused when a sprite plane is updated to no longer
  4380. * completely hide the primary plane.
  4381. */
  4382. static void
  4383. intel_post_enable_primary(struct drm_crtc *crtc,
  4384. const struct intel_crtc_state *new_crtc_state)
  4385. {
  4386. struct drm_device *dev = crtc->dev;
  4387. struct drm_i915_private *dev_priv = to_i915(dev);
  4388. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4389. int pipe = intel_crtc->pipe;
  4390. /*
  4391. * Gen2 reports pipe underruns whenever all planes are disabled.
  4392. * So don't enable underrun reporting before at least some planes
  4393. * are enabled.
  4394. * FIXME: Need to fix the logic to work when we turn off all planes
  4395. * but leave the pipe running.
  4396. */
  4397. if (IS_GEN2(dev_priv))
  4398. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4399. /* Underruns don't always raise interrupts, so check manually. */
  4400. intel_check_cpu_fifo_underruns(dev_priv);
  4401. intel_check_pch_fifo_underruns(dev_priv);
  4402. }
  4403. /* FIXME get rid of this and use pre_plane_update */
  4404. static void
  4405. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4406. {
  4407. struct drm_device *dev = crtc->dev;
  4408. struct drm_i915_private *dev_priv = to_i915(dev);
  4409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4410. int pipe = intel_crtc->pipe;
  4411. /*
  4412. * Gen2 reports pipe underruns whenever all planes are disabled.
  4413. * So disable underrun reporting before all the planes get disabled.
  4414. */
  4415. if (IS_GEN2(dev_priv))
  4416. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4417. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4418. /*
  4419. * Vblank time updates from the shadow to live plane control register
  4420. * are blocked if the memory self-refresh mode is active at that
  4421. * moment. So to make sure the plane gets truly disabled, disable
  4422. * first the self-refresh mode. The self-refresh enable bit in turn
  4423. * will be checked/applied by the HW only at the next frame start
  4424. * event which is after the vblank start event, so we need to have a
  4425. * wait-for-vblank between disabling the plane and the pipe.
  4426. */
  4427. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4428. intel_set_memory_cxsr(dev_priv, false))
  4429. intel_wait_for_vblank(dev_priv, pipe);
  4430. }
  4431. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4432. const struct intel_crtc_state *new_crtc_state)
  4433. {
  4434. if (!old_crtc_state->ips_enabled)
  4435. return false;
  4436. if (needs_modeset(&new_crtc_state->base))
  4437. return true;
  4438. return !new_crtc_state->ips_enabled;
  4439. }
  4440. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4441. const struct intel_crtc_state *new_crtc_state)
  4442. {
  4443. if (!new_crtc_state->ips_enabled)
  4444. return false;
  4445. if (needs_modeset(&new_crtc_state->base))
  4446. return true;
  4447. /*
  4448. * We can't read out IPS on broadwell, assume the worst and
  4449. * forcibly enable IPS on the first fastset.
  4450. */
  4451. if (new_crtc_state->update_pipe &&
  4452. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4453. return true;
  4454. return !old_crtc_state->ips_enabled;
  4455. }
  4456. static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
  4457. const struct intel_crtc_state *crtc_state)
  4458. {
  4459. if (!crtc_state->nv12_planes)
  4460. return false;
  4461. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  4462. return false;
  4463. if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
  4464. IS_CANNONLAKE(dev_priv))
  4465. return true;
  4466. return false;
  4467. }
  4468. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4469. {
  4470. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4471. struct drm_device *dev = crtc->base.dev;
  4472. struct drm_i915_private *dev_priv = to_i915(dev);
  4473. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4474. struct intel_crtc_state *pipe_config =
  4475. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4476. crtc);
  4477. struct drm_plane *primary = crtc->base.primary;
  4478. struct drm_plane_state *old_primary_state =
  4479. drm_atomic_get_old_plane_state(old_state, primary);
  4480. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4481. if (pipe_config->update_wm_post && pipe_config->base.active)
  4482. intel_update_watermarks(crtc);
  4483. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4484. hsw_enable_ips(pipe_config);
  4485. if (old_primary_state) {
  4486. struct drm_plane_state *new_primary_state =
  4487. drm_atomic_get_new_plane_state(old_state, primary);
  4488. intel_fbc_post_update(crtc);
  4489. if (new_primary_state->visible &&
  4490. (needs_modeset(&pipe_config->base) ||
  4491. !old_primary_state->visible))
  4492. intel_post_enable_primary(&crtc->base, pipe_config);
  4493. }
  4494. /* Display WA 827 */
  4495. if (needs_nv12_wa(dev_priv, old_crtc_state) &&
  4496. !needs_nv12_wa(dev_priv, pipe_config)) {
  4497. skl_wa_clkgate(dev_priv, crtc->pipe, false);
  4498. skl_wa_528(dev_priv, crtc->pipe, false);
  4499. }
  4500. }
  4501. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4502. struct intel_crtc_state *pipe_config)
  4503. {
  4504. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4505. struct drm_device *dev = crtc->base.dev;
  4506. struct drm_i915_private *dev_priv = to_i915(dev);
  4507. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4508. struct drm_plane *primary = crtc->base.primary;
  4509. struct drm_plane_state *old_primary_state =
  4510. drm_atomic_get_old_plane_state(old_state, primary);
  4511. bool modeset = needs_modeset(&pipe_config->base);
  4512. struct intel_atomic_state *old_intel_state =
  4513. to_intel_atomic_state(old_state);
  4514. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4515. hsw_disable_ips(old_crtc_state);
  4516. if (old_primary_state) {
  4517. struct intel_plane_state *new_primary_state =
  4518. intel_atomic_get_new_plane_state(old_intel_state,
  4519. to_intel_plane(primary));
  4520. intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
  4521. /*
  4522. * Gen2 reports pipe underruns whenever all planes are disabled.
  4523. * So disable underrun reporting before all the planes get disabled.
  4524. */
  4525. if (IS_GEN2(dev_priv) && old_primary_state->visible &&
  4526. (modeset || !new_primary_state->base.visible))
  4527. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4528. }
  4529. /* Display WA 827 */
  4530. if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
  4531. needs_nv12_wa(dev_priv, pipe_config)) {
  4532. skl_wa_clkgate(dev_priv, crtc->pipe, true);
  4533. skl_wa_528(dev_priv, crtc->pipe, true);
  4534. }
  4535. /*
  4536. * Vblank time updates from the shadow to live plane control register
  4537. * are blocked if the memory self-refresh mode is active at that
  4538. * moment. So to make sure the plane gets truly disabled, disable
  4539. * first the self-refresh mode. The self-refresh enable bit in turn
  4540. * will be checked/applied by the HW only at the next frame start
  4541. * event which is after the vblank start event, so we need to have a
  4542. * wait-for-vblank between disabling the plane and the pipe.
  4543. */
  4544. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4545. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4546. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4547. /*
  4548. * IVB workaround: must disable low power watermarks for at least
  4549. * one frame before enabling scaling. LP watermarks can be re-enabled
  4550. * when scaling is disabled.
  4551. *
  4552. * WaCxSRDisabledForSpriteScaling:ivb
  4553. */
  4554. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4555. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4556. /*
  4557. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4558. * watermark programming here.
  4559. */
  4560. if (needs_modeset(&pipe_config->base))
  4561. return;
  4562. /*
  4563. * For platforms that support atomic watermarks, program the
  4564. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4565. * will be the intermediate values that are safe for both pre- and
  4566. * post- vblank; when vblank happens, the 'active' values will be set
  4567. * to the final 'target' values and we'll do this again to get the
  4568. * optimal watermarks. For gen9+ platforms, the values we program here
  4569. * will be the final target values which will get automatically latched
  4570. * at vblank time; no further programming will be necessary.
  4571. *
  4572. * If a platform hasn't been transitioned to atomic watermarks yet,
  4573. * we'll continue to update watermarks the old way, if flags tell
  4574. * us to.
  4575. */
  4576. if (dev_priv->display.initial_watermarks != NULL)
  4577. dev_priv->display.initial_watermarks(old_intel_state,
  4578. pipe_config);
  4579. else if (pipe_config->update_wm_pre)
  4580. intel_update_watermarks(crtc);
  4581. }
  4582. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4583. {
  4584. struct drm_device *dev = crtc->dev;
  4585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4586. struct drm_plane *p;
  4587. int pipe = intel_crtc->pipe;
  4588. intel_crtc_dpms_overlay_disable(intel_crtc);
  4589. drm_for_each_plane_mask(p, dev, plane_mask)
  4590. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4591. /*
  4592. * FIXME: Once we grow proper nuclear flip support out of this we need
  4593. * to compute the mask of flip planes precisely. For the time being
  4594. * consider this a flip to a NULL plane.
  4595. */
  4596. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4597. }
  4598. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4599. struct intel_crtc_state *crtc_state,
  4600. struct drm_atomic_state *old_state)
  4601. {
  4602. struct drm_connector_state *conn_state;
  4603. struct drm_connector *conn;
  4604. int i;
  4605. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4606. struct intel_encoder *encoder =
  4607. to_intel_encoder(conn_state->best_encoder);
  4608. if (conn_state->crtc != crtc)
  4609. continue;
  4610. if (encoder->pre_pll_enable)
  4611. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4612. }
  4613. }
  4614. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4615. struct intel_crtc_state *crtc_state,
  4616. struct drm_atomic_state *old_state)
  4617. {
  4618. struct drm_connector_state *conn_state;
  4619. struct drm_connector *conn;
  4620. int i;
  4621. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4622. struct intel_encoder *encoder =
  4623. to_intel_encoder(conn_state->best_encoder);
  4624. if (conn_state->crtc != crtc)
  4625. continue;
  4626. if (encoder->pre_enable)
  4627. encoder->pre_enable(encoder, crtc_state, conn_state);
  4628. }
  4629. }
  4630. static void intel_encoders_enable(struct drm_crtc *crtc,
  4631. struct intel_crtc_state *crtc_state,
  4632. struct drm_atomic_state *old_state)
  4633. {
  4634. struct drm_connector_state *conn_state;
  4635. struct drm_connector *conn;
  4636. int i;
  4637. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4638. struct intel_encoder *encoder =
  4639. to_intel_encoder(conn_state->best_encoder);
  4640. if (conn_state->crtc != crtc)
  4641. continue;
  4642. encoder->enable(encoder, crtc_state, conn_state);
  4643. intel_opregion_notify_encoder(encoder, true);
  4644. }
  4645. }
  4646. static void intel_encoders_disable(struct drm_crtc *crtc,
  4647. struct intel_crtc_state *old_crtc_state,
  4648. struct drm_atomic_state *old_state)
  4649. {
  4650. struct drm_connector_state *old_conn_state;
  4651. struct drm_connector *conn;
  4652. int i;
  4653. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4654. struct intel_encoder *encoder =
  4655. to_intel_encoder(old_conn_state->best_encoder);
  4656. if (old_conn_state->crtc != crtc)
  4657. continue;
  4658. intel_opregion_notify_encoder(encoder, false);
  4659. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4660. }
  4661. }
  4662. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4663. struct intel_crtc_state *old_crtc_state,
  4664. struct drm_atomic_state *old_state)
  4665. {
  4666. struct drm_connector_state *old_conn_state;
  4667. struct drm_connector *conn;
  4668. int i;
  4669. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4670. struct intel_encoder *encoder =
  4671. to_intel_encoder(old_conn_state->best_encoder);
  4672. if (old_conn_state->crtc != crtc)
  4673. continue;
  4674. if (encoder->post_disable)
  4675. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4676. }
  4677. }
  4678. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4679. struct intel_crtc_state *old_crtc_state,
  4680. struct drm_atomic_state *old_state)
  4681. {
  4682. struct drm_connector_state *old_conn_state;
  4683. struct drm_connector *conn;
  4684. int i;
  4685. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4686. struct intel_encoder *encoder =
  4687. to_intel_encoder(old_conn_state->best_encoder);
  4688. if (old_conn_state->crtc != crtc)
  4689. continue;
  4690. if (encoder->post_pll_disable)
  4691. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4692. }
  4693. }
  4694. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4695. struct drm_atomic_state *old_state)
  4696. {
  4697. struct drm_crtc *crtc = pipe_config->base.crtc;
  4698. struct drm_device *dev = crtc->dev;
  4699. struct drm_i915_private *dev_priv = to_i915(dev);
  4700. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4701. int pipe = intel_crtc->pipe;
  4702. struct intel_atomic_state *old_intel_state =
  4703. to_intel_atomic_state(old_state);
  4704. if (WARN_ON(intel_crtc->active))
  4705. return;
  4706. /*
  4707. * Sometimes spurious CPU pipe underruns happen during FDI
  4708. * training, at least with VGA+HDMI cloning. Suppress them.
  4709. *
  4710. * On ILK we get an occasional spurious CPU pipe underruns
  4711. * between eDP port A enable and vdd enable. Also PCH port
  4712. * enable seems to result in the occasional CPU pipe underrun.
  4713. *
  4714. * Spurious PCH underruns also occur during PCH enabling.
  4715. */
  4716. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4717. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4718. if (intel_crtc->config->has_pch_encoder)
  4719. intel_prepare_shared_dpll(intel_crtc);
  4720. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4721. intel_dp_set_m_n(intel_crtc, M1_N1);
  4722. intel_set_pipe_timings(intel_crtc);
  4723. intel_set_pipe_src_size(intel_crtc);
  4724. if (intel_crtc->config->has_pch_encoder) {
  4725. intel_cpu_transcoder_set_m_n(intel_crtc,
  4726. &intel_crtc->config->fdi_m_n, NULL);
  4727. }
  4728. ironlake_set_pipeconf(crtc);
  4729. intel_crtc->active = true;
  4730. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4731. if (intel_crtc->config->has_pch_encoder) {
  4732. /* Note: FDI PLL enabling _must_ be done before we enable the
  4733. * cpu pipes, hence this is separate from all the other fdi/pch
  4734. * enabling. */
  4735. ironlake_fdi_pll_enable(intel_crtc);
  4736. } else {
  4737. assert_fdi_tx_disabled(dev_priv, pipe);
  4738. assert_fdi_rx_disabled(dev_priv, pipe);
  4739. }
  4740. ironlake_pfit_enable(intel_crtc);
  4741. /*
  4742. * On ILK+ LUT must be loaded before the pipe is running but with
  4743. * clocks enabled
  4744. */
  4745. intel_color_load_luts(&pipe_config->base);
  4746. if (dev_priv->display.initial_watermarks != NULL)
  4747. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4748. intel_enable_pipe(pipe_config);
  4749. if (intel_crtc->config->has_pch_encoder)
  4750. ironlake_pch_enable(old_intel_state, pipe_config);
  4751. assert_vblank_disabled(crtc);
  4752. drm_crtc_vblank_on(crtc);
  4753. intel_encoders_enable(crtc, pipe_config, old_state);
  4754. if (HAS_PCH_CPT(dev_priv))
  4755. cpt_verify_modeset(dev, intel_crtc->pipe);
  4756. /*
  4757. * Must wait for vblank to avoid spurious PCH FIFO underruns.
  4758. * And a second vblank wait is needed at least on ILK with
  4759. * some interlaced HDMI modes. Let's do the double wait always
  4760. * in case there are more corner cases we don't know about.
  4761. */
  4762. if (intel_crtc->config->has_pch_encoder) {
  4763. intel_wait_for_vblank(dev_priv, pipe);
  4764. intel_wait_for_vblank(dev_priv, pipe);
  4765. }
  4766. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4767. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4768. }
  4769. /* IPS only exists on ULT machines and is tied to pipe A. */
  4770. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4771. {
  4772. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4773. }
  4774. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4775. enum pipe pipe, bool apply)
  4776. {
  4777. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4778. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4779. if (apply)
  4780. val |= mask;
  4781. else
  4782. val &= ~mask;
  4783. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4784. }
  4785. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4786. {
  4787. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4788. enum pipe pipe = crtc->pipe;
  4789. uint32_t val;
  4790. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4791. /* Program B credit equally to all pipes */
  4792. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4793. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4794. }
  4795. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4796. struct drm_atomic_state *old_state)
  4797. {
  4798. struct drm_crtc *crtc = pipe_config->base.crtc;
  4799. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4801. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4802. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4803. struct intel_atomic_state *old_intel_state =
  4804. to_intel_atomic_state(old_state);
  4805. bool psl_clkgate_wa;
  4806. u32 pipe_chicken;
  4807. if (WARN_ON(intel_crtc->active))
  4808. return;
  4809. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4810. if (intel_crtc->config->shared_dpll)
  4811. intel_enable_shared_dpll(intel_crtc);
  4812. if (INTEL_GEN(dev_priv) >= 11)
  4813. icl_map_plls_to_ports(crtc, pipe_config, old_state);
  4814. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4815. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4816. intel_dp_set_m_n(intel_crtc, M1_N1);
  4817. if (!transcoder_is_dsi(cpu_transcoder))
  4818. intel_set_pipe_timings(intel_crtc);
  4819. intel_set_pipe_src_size(intel_crtc);
  4820. if (cpu_transcoder != TRANSCODER_EDP &&
  4821. !transcoder_is_dsi(cpu_transcoder)) {
  4822. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4823. intel_crtc->config->pixel_multiplier - 1);
  4824. }
  4825. if (intel_crtc->config->has_pch_encoder) {
  4826. intel_cpu_transcoder_set_m_n(intel_crtc,
  4827. &intel_crtc->config->fdi_m_n, NULL);
  4828. }
  4829. if (!transcoder_is_dsi(cpu_transcoder))
  4830. haswell_set_pipeconf(crtc);
  4831. haswell_set_pipemisc(crtc);
  4832. intel_color_set_csc(&pipe_config->base);
  4833. intel_crtc->active = true;
  4834. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4835. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4836. intel_crtc->config->pch_pfit.enabled;
  4837. if (psl_clkgate_wa)
  4838. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4839. if (INTEL_GEN(dev_priv) >= 9)
  4840. skylake_pfit_enable(intel_crtc);
  4841. else
  4842. ironlake_pfit_enable(intel_crtc);
  4843. /*
  4844. * On ILK+ LUT must be loaded before the pipe is running but with
  4845. * clocks enabled
  4846. */
  4847. intel_color_load_luts(&pipe_config->base);
  4848. /*
  4849. * Display WA #1153: enable hardware to bypass the alpha math
  4850. * and rounding for per-pixel values 00 and 0xff
  4851. */
  4852. if (INTEL_GEN(dev_priv) >= 11) {
  4853. pipe_chicken = I915_READ(PIPE_CHICKEN(pipe));
  4854. if (!(pipe_chicken & PER_PIXEL_ALPHA_BYPASS_EN))
  4855. I915_WRITE_FW(PIPE_CHICKEN(pipe),
  4856. pipe_chicken | PER_PIXEL_ALPHA_BYPASS_EN);
  4857. }
  4858. intel_ddi_set_pipe_settings(pipe_config);
  4859. if (!transcoder_is_dsi(cpu_transcoder))
  4860. intel_ddi_enable_transcoder_func(pipe_config);
  4861. if (dev_priv->display.initial_watermarks != NULL)
  4862. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4863. if (INTEL_GEN(dev_priv) >= 11)
  4864. icl_pipe_mbus_enable(intel_crtc);
  4865. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4866. if (!transcoder_is_dsi(cpu_transcoder))
  4867. intel_enable_pipe(pipe_config);
  4868. if (intel_crtc->config->has_pch_encoder)
  4869. lpt_pch_enable(old_intel_state, pipe_config);
  4870. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4871. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4872. assert_vblank_disabled(crtc);
  4873. drm_crtc_vblank_on(crtc);
  4874. intel_encoders_enable(crtc, pipe_config, old_state);
  4875. if (psl_clkgate_wa) {
  4876. intel_wait_for_vblank(dev_priv, pipe);
  4877. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4878. }
  4879. /* If we change the relative order between pipe/planes enabling, we need
  4880. * to change the workaround. */
  4881. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4882. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4883. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4884. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4885. }
  4886. }
  4887. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4888. {
  4889. struct drm_device *dev = crtc->base.dev;
  4890. struct drm_i915_private *dev_priv = to_i915(dev);
  4891. int pipe = crtc->pipe;
  4892. /* To avoid upsetting the power well on haswell only disable the pfit if
  4893. * it's in use. The hw state code will make sure we get this right. */
  4894. if (force || crtc->config->pch_pfit.enabled) {
  4895. I915_WRITE(PF_CTL(pipe), 0);
  4896. I915_WRITE(PF_WIN_POS(pipe), 0);
  4897. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4898. }
  4899. }
  4900. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4901. struct drm_atomic_state *old_state)
  4902. {
  4903. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4904. struct drm_device *dev = crtc->dev;
  4905. struct drm_i915_private *dev_priv = to_i915(dev);
  4906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4907. int pipe = intel_crtc->pipe;
  4908. /*
  4909. * Sometimes spurious CPU pipe underruns happen when the
  4910. * pipe is already disabled, but FDI RX/TX is still enabled.
  4911. * Happens at least with VGA+HDMI cloning. Suppress them.
  4912. */
  4913. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4914. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4915. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4916. drm_crtc_vblank_off(crtc);
  4917. assert_vblank_disabled(crtc);
  4918. intel_disable_pipe(old_crtc_state);
  4919. ironlake_pfit_disable(intel_crtc, false);
  4920. if (intel_crtc->config->has_pch_encoder)
  4921. ironlake_fdi_disable(crtc);
  4922. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4923. if (intel_crtc->config->has_pch_encoder) {
  4924. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4925. if (HAS_PCH_CPT(dev_priv)) {
  4926. i915_reg_t reg;
  4927. u32 temp;
  4928. /* disable TRANS_DP_CTL */
  4929. reg = TRANS_DP_CTL(pipe);
  4930. temp = I915_READ(reg);
  4931. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4932. TRANS_DP_PORT_SEL_MASK);
  4933. temp |= TRANS_DP_PORT_SEL_NONE;
  4934. I915_WRITE(reg, temp);
  4935. /* disable DPLL_SEL */
  4936. temp = I915_READ(PCH_DPLL_SEL);
  4937. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4938. I915_WRITE(PCH_DPLL_SEL, temp);
  4939. }
  4940. ironlake_fdi_pll_disable(intel_crtc);
  4941. }
  4942. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4943. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4944. }
  4945. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4946. struct drm_atomic_state *old_state)
  4947. {
  4948. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4949. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4951. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  4952. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4953. drm_crtc_vblank_off(crtc);
  4954. assert_vblank_disabled(crtc);
  4955. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4956. if (!transcoder_is_dsi(cpu_transcoder))
  4957. intel_disable_pipe(old_crtc_state);
  4958. if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
  4959. intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
  4960. if (!transcoder_is_dsi(cpu_transcoder))
  4961. intel_ddi_disable_transcoder_func(old_crtc_state);
  4962. if (INTEL_GEN(dev_priv) >= 9)
  4963. skylake_scaler_disable(intel_crtc);
  4964. else
  4965. ironlake_pfit_disable(intel_crtc, false);
  4966. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4967. if (INTEL_GEN(dev_priv) >= 11)
  4968. icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
  4969. }
  4970. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4971. {
  4972. struct drm_device *dev = crtc->base.dev;
  4973. struct drm_i915_private *dev_priv = to_i915(dev);
  4974. struct intel_crtc_state *pipe_config = crtc->config;
  4975. if (!pipe_config->gmch_pfit.control)
  4976. return;
  4977. /*
  4978. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4979. * according to register description and PRM.
  4980. */
  4981. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4982. assert_pipe_disabled(dev_priv, crtc->pipe);
  4983. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4984. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4985. /* Border color in case we don't scale up to the full screen. Black by
  4986. * default, change to something else for debugging. */
  4987. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4988. }
  4989. bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
  4990. {
  4991. if (IS_ICELAKE(dev_priv))
  4992. return port >= PORT_C && port <= PORT_F;
  4993. return false;
  4994. }
  4995. enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
  4996. {
  4997. if (!intel_port_is_tc(dev_priv, port))
  4998. return PORT_TC_NONE;
  4999. return port - PORT_C;
  5000. }
  5001. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  5002. {
  5003. switch (port) {
  5004. case PORT_A:
  5005. return POWER_DOMAIN_PORT_DDI_A_LANES;
  5006. case PORT_B:
  5007. return POWER_DOMAIN_PORT_DDI_B_LANES;
  5008. case PORT_C:
  5009. return POWER_DOMAIN_PORT_DDI_C_LANES;
  5010. case PORT_D:
  5011. return POWER_DOMAIN_PORT_DDI_D_LANES;
  5012. case PORT_E:
  5013. return POWER_DOMAIN_PORT_DDI_E_LANES;
  5014. case PORT_F:
  5015. return POWER_DOMAIN_PORT_DDI_F_LANES;
  5016. default:
  5017. MISSING_CASE(port);
  5018. return POWER_DOMAIN_PORT_OTHER;
  5019. }
  5020. }
  5021. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  5022. struct intel_crtc_state *crtc_state)
  5023. {
  5024. struct drm_device *dev = crtc->dev;
  5025. struct drm_i915_private *dev_priv = to_i915(dev);
  5026. struct drm_encoder *encoder;
  5027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5028. enum pipe pipe = intel_crtc->pipe;
  5029. u64 mask;
  5030. enum transcoder transcoder = crtc_state->cpu_transcoder;
  5031. if (!crtc_state->base.active)
  5032. return 0;
  5033. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  5034. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  5035. if (crtc_state->pch_pfit.enabled ||
  5036. crtc_state->pch_pfit.force_thru)
  5037. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  5038. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  5039. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5040. mask |= BIT_ULL(intel_encoder->power_domain);
  5041. }
  5042. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  5043. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  5044. if (crtc_state->shared_dpll)
  5045. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  5046. return mask;
  5047. }
  5048. static u64
  5049. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  5050. struct intel_crtc_state *crtc_state)
  5051. {
  5052. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5053. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5054. enum intel_display_power_domain domain;
  5055. u64 domains, new_domains, old_domains;
  5056. old_domains = intel_crtc->enabled_power_domains;
  5057. intel_crtc->enabled_power_domains = new_domains =
  5058. get_crtc_power_domains(crtc, crtc_state);
  5059. domains = new_domains & ~old_domains;
  5060. for_each_power_domain(domain, domains)
  5061. intel_display_power_get(dev_priv, domain);
  5062. return old_domains & ~new_domains;
  5063. }
  5064. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  5065. u64 domains)
  5066. {
  5067. enum intel_display_power_domain domain;
  5068. for_each_power_domain(domain, domains)
  5069. intel_display_power_put(dev_priv, domain);
  5070. }
  5071. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5072. struct drm_atomic_state *old_state)
  5073. {
  5074. struct intel_atomic_state *old_intel_state =
  5075. to_intel_atomic_state(old_state);
  5076. struct drm_crtc *crtc = pipe_config->base.crtc;
  5077. struct drm_device *dev = crtc->dev;
  5078. struct drm_i915_private *dev_priv = to_i915(dev);
  5079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5080. int pipe = intel_crtc->pipe;
  5081. if (WARN_ON(intel_crtc->active))
  5082. return;
  5083. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5084. intel_dp_set_m_n(intel_crtc, M1_N1);
  5085. intel_set_pipe_timings(intel_crtc);
  5086. intel_set_pipe_src_size(intel_crtc);
  5087. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5088. struct drm_i915_private *dev_priv = to_i915(dev);
  5089. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5090. I915_WRITE(CHV_CANVAS(pipe), 0);
  5091. }
  5092. i9xx_set_pipeconf(intel_crtc);
  5093. intel_crtc->active = true;
  5094. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5095. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5096. if (IS_CHERRYVIEW(dev_priv)) {
  5097. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5098. chv_enable_pll(intel_crtc, intel_crtc->config);
  5099. } else {
  5100. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5101. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5102. }
  5103. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5104. i9xx_pfit_enable(intel_crtc);
  5105. intel_color_load_luts(&pipe_config->base);
  5106. dev_priv->display.initial_watermarks(old_intel_state,
  5107. pipe_config);
  5108. intel_enable_pipe(pipe_config);
  5109. assert_vblank_disabled(crtc);
  5110. drm_crtc_vblank_on(crtc);
  5111. intel_encoders_enable(crtc, pipe_config, old_state);
  5112. }
  5113. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5114. {
  5115. struct drm_device *dev = crtc->base.dev;
  5116. struct drm_i915_private *dev_priv = to_i915(dev);
  5117. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5118. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5119. }
  5120. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5121. struct drm_atomic_state *old_state)
  5122. {
  5123. struct intel_atomic_state *old_intel_state =
  5124. to_intel_atomic_state(old_state);
  5125. struct drm_crtc *crtc = pipe_config->base.crtc;
  5126. struct drm_device *dev = crtc->dev;
  5127. struct drm_i915_private *dev_priv = to_i915(dev);
  5128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5129. enum pipe pipe = intel_crtc->pipe;
  5130. if (WARN_ON(intel_crtc->active))
  5131. return;
  5132. i9xx_set_pll_dividers(intel_crtc);
  5133. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5134. intel_dp_set_m_n(intel_crtc, M1_N1);
  5135. intel_set_pipe_timings(intel_crtc);
  5136. intel_set_pipe_src_size(intel_crtc);
  5137. i9xx_set_pipeconf(intel_crtc);
  5138. intel_crtc->active = true;
  5139. if (!IS_GEN2(dev_priv))
  5140. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5141. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5142. i9xx_enable_pll(intel_crtc, pipe_config);
  5143. i9xx_pfit_enable(intel_crtc);
  5144. intel_color_load_luts(&pipe_config->base);
  5145. if (dev_priv->display.initial_watermarks != NULL)
  5146. dev_priv->display.initial_watermarks(old_intel_state,
  5147. intel_crtc->config);
  5148. else
  5149. intel_update_watermarks(intel_crtc);
  5150. intel_enable_pipe(pipe_config);
  5151. assert_vblank_disabled(crtc);
  5152. drm_crtc_vblank_on(crtc);
  5153. intel_encoders_enable(crtc, pipe_config, old_state);
  5154. }
  5155. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5156. {
  5157. struct drm_device *dev = crtc->base.dev;
  5158. struct drm_i915_private *dev_priv = to_i915(dev);
  5159. if (!crtc->config->gmch_pfit.control)
  5160. return;
  5161. assert_pipe_disabled(dev_priv, crtc->pipe);
  5162. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5163. I915_READ(PFIT_CONTROL));
  5164. I915_WRITE(PFIT_CONTROL, 0);
  5165. }
  5166. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5167. struct drm_atomic_state *old_state)
  5168. {
  5169. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5170. struct drm_device *dev = crtc->dev;
  5171. struct drm_i915_private *dev_priv = to_i915(dev);
  5172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5173. int pipe = intel_crtc->pipe;
  5174. /*
  5175. * On gen2 planes are double buffered but the pipe isn't, so we must
  5176. * wait for planes to fully turn off before disabling the pipe.
  5177. */
  5178. if (IS_GEN2(dev_priv))
  5179. intel_wait_for_vblank(dev_priv, pipe);
  5180. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5181. drm_crtc_vblank_off(crtc);
  5182. assert_vblank_disabled(crtc);
  5183. intel_disable_pipe(old_crtc_state);
  5184. i9xx_pfit_disable(intel_crtc);
  5185. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5186. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5187. if (IS_CHERRYVIEW(dev_priv))
  5188. chv_disable_pll(dev_priv, pipe);
  5189. else if (IS_VALLEYVIEW(dev_priv))
  5190. vlv_disable_pll(dev_priv, pipe);
  5191. else
  5192. i9xx_disable_pll(intel_crtc);
  5193. }
  5194. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5195. if (!IS_GEN2(dev_priv))
  5196. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5197. if (!dev_priv->display.initial_watermarks)
  5198. intel_update_watermarks(intel_crtc);
  5199. /* clock the pipe down to 640x480@60 to potentially save power */
  5200. if (IS_I830(dev_priv))
  5201. i830_enable_pipe(dev_priv, pipe);
  5202. }
  5203. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5204. struct drm_modeset_acquire_ctx *ctx)
  5205. {
  5206. struct intel_encoder *encoder;
  5207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5208. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5209. enum intel_display_power_domain domain;
  5210. struct intel_plane *plane;
  5211. u64 domains;
  5212. struct drm_atomic_state *state;
  5213. struct intel_crtc_state *crtc_state;
  5214. int ret;
  5215. if (!intel_crtc->active)
  5216. return;
  5217. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5218. const struct intel_plane_state *plane_state =
  5219. to_intel_plane_state(plane->base.state);
  5220. if (plane_state->base.visible)
  5221. intel_plane_disable_noatomic(intel_crtc, plane);
  5222. }
  5223. state = drm_atomic_state_alloc(crtc->dev);
  5224. if (!state) {
  5225. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5226. crtc->base.id, crtc->name);
  5227. return;
  5228. }
  5229. state->acquire_ctx = ctx;
  5230. /* Everything's already locked, -EDEADLK can't happen. */
  5231. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5232. ret = drm_atomic_add_affected_connectors(state, crtc);
  5233. WARN_ON(IS_ERR(crtc_state) || ret);
  5234. dev_priv->display.crtc_disable(crtc_state, state);
  5235. drm_atomic_state_put(state);
  5236. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5237. crtc->base.id, crtc->name);
  5238. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5239. crtc->state->active = false;
  5240. intel_crtc->active = false;
  5241. crtc->enabled = false;
  5242. crtc->state->connector_mask = 0;
  5243. crtc->state->encoder_mask = 0;
  5244. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5245. encoder->base.crtc = NULL;
  5246. intel_fbc_disable(intel_crtc);
  5247. intel_update_watermarks(intel_crtc);
  5248. intel_disable_shared_dpll(intel_crtc);
  5249. domains = intel_crtc->enabled_power_domains;
  5250. for_each_power_domain(domain, domains)
  5251. intel_display_power_put(dev_priv, domain);
  5252. intel_crtc->enabled_power_domains = 0;
  5253. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5254. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5255. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5256. }
  5257. /*
  5258. * turn all crtc's off, but do not adjust state
  5259. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5260. */
  5261. int intel_display_suspend(struct drm_device *dev)
  5262. {
  5263. struct drm_i915_private *dev_priv = to_i915(dev);
  5264. struct drm_atomic_state *state;
  5265. int ret;
  5266. state = drm_atomic_helper_suspend(dev);
  5267. ret = PTR_ERR_OR_ZERO(state);
  5268. if (ret)
  5269. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5270. else
  5271. dev_priv->modeset_restore_state = state;
  5272. return ret;
  5273. }
  5274. void intel_encoder_destroy(struct drm_encoder *encoder)
  5275. {
  5276. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5277. drm_encoder_cleanup(encoder);
  5278. kfree(intel_encoder);
  5279. }
  5280. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5281. * internal consistency). */
  5282. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5283. struct drm_connector_state *conn_state)
  5284. {
  5285. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5286. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5287. connector->base.base.id,
  5288. connector->base.name);
  5289. if (connector->get_hw_state(connector)) {
  5290. struct intel_encoder *encoder = connector->encoder;
  5291. I915_STATE_WARN(!crtc_state,
  5292. "connector enabled without attached crtc\n");
  5293. if (!crtc_state)
  5294. return;
  5295. I915_STATE_WARN(!crtc_state->active,
  5296. "connector is active, but attached crtc isn't\n");
  5297. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5298. return;
  5299. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5300. "atomic encoder doesn't match attached encoder\n");
  5301. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5302. "attached encoder crtc differs from connector crtc\n");
  5303. } else {
  5304. I915_STATE_WARN(crtc_state && crtc_state->active,
  5305. "attached crtc is active, but connector isn't\n");
  5306. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5307. "best encoder set without crtc!\n");
  5308. }
  5309. }
  5310. int intel_connector_init(struct intel_connector *connector)
  5311. {
  5312. struct intel_digital_connector_state *conn_state;
  5313. /*
  5314. * Allocate enough memory to hold intel_digital_connector_state,
  5315. * This might be a few bytes too many, but for connectors that don't
  5316. * need it we'll free the state and allocate a smaller one on the first
  5317. * succesful commit anyway.
  5318. */
  5319. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5320. if (!conn_state)
  5321. return -ENOMEM;
  5322. __drm_atomic_helper_connector_reset(&connector->base,
  5323. &conn_state->base);
  5324. return 0;
  5325. }
  5326. struct intel_connector *intel_connector_alloc(void)
  5327. {
  5328. struct intel_connector *connector;
  5329. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5330. if (!connector)
  5331. return NULL;
  5332. if (intel_connector_init(connector) < 0) {
  5333. kfree(connector);
  5334. return NULL;
  5335. }
  5336. return connector;
  5337. }
  5338. /*
  5339. * Free the bits allocated by intel_connector_alloc.
  5340. * This should only be used after intel_connector_alloc has returned
  5341. * successfully, and before drm_connector_init returns successfully.
  5342. * Otherwise the destroy callbacks for the connector and the state should
  5343. * take care of proper cleanup/free
  5344. */
  5345. void intel_connector_free(struct intel_connector *connector)
  5346. {
  5347. kfree(to_intel_digital_connector_state(connector->base.state));
  5348. kfree(connector);
  5349. }
  5350. /* Simple connector->get_hw_state implementation for encoders that support only
  5351. * one connector and no cloning and hence the encoder state determines the state
  5352. * of the connector. */
  5353. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5354. {
  5355. enum pipe pipe = 0;
  5356. struct intel_encoder *encoder = connector->encoder;
  5357. return encoder->get_hw_state(encoder, &pipe);
  5358. }
  5359. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5360. {
  5361. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5362. return crtc_state->fdi_lanes;
  5363. return 0;
  5364. }
  5365. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5366. struct intel_crtc_state *pipe_config)
  5367. {
  5368. struct drm_i915_private *dev_priv = to_i915(dev);
  5369. struct drm_atomic_state *state = pipe_config->base.state;
  5370. struct intel_crtc *other_crtc;
  5371. struct intel_crtc_state *other_crtc_state;
  5372. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5373. pipe_name(pipe), pipe_config->fdi_lanes);
  5374. if (pipe_config->fdi_lanes > 4) {
  5375. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5376. pipe_name(pipe), pipe_config->fdi_lanes);
  5377. return -EINVAL;
  5378. }
  5379. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5380. if (pipe_config->fdi_lanes > 2) {
  5381. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5382. pipe_config->fdi_lanes);
  5383. return -EINVAL;
  5384. } else {
  5385. return 0;
  5386. }
  5387. }
  5388. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5389. return 0;
  5390. /* Ivybridge 3 pipe is really complicated */
  5391. switch (pipe) {
  5392. case PIPE_A:
  5393. return 0;
  5394. case PIPE_B:
  5395. if (pipe_config->fdi_lanes <= 2)
  5396. return 0;
  5397. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5398. other_crtc_state =
  5399. intel_atomic_get_crtc_state(state, other_crtc);
  5400. if (IS_ERR(other_crtc_state))
  5401. return PTR_ERR(other_crtc_state);
  5402. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5403. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5404. pipe_name(pipe), pipe_config->fdi_lanes);
  5405. return -EINVAL;
  5406. }
  5407. return 0;
  5408. case PIPE_C:
  5409. if (pipe_config->fdi_lanes > 2) {
  5410. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5411. pipe_name(pipe), pipe_config->fdi_lanes);
  5412. return -EINVAL;
  5413. }
  5414. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5415. other_crtc_state =
  5416. intel_atomic_get_crtc_state(state, other_crtc);
  5417. if (IS_ERR(other_crtc_state))
  5418. return PTR_ERR(other_crtc_state);
  5419. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5420. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5421. return -EINVAL;
  5422. }
  5423. return 0;
  5424. default:
  5425. BUG();
  5426. }
  5427. }
  5428. #define RETRY 1
  5429. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5430. struct intel_crtc_state *pipe_config)
  5431. {
  5432. struct drm_device *dev = intel_crtc->base.dev;
  5433. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5434. int lane, link_bw, fdi_dotclock, ret;
  5435. bool needs_recompute = false;
  5436. retry:
  5437. /* FDI is a binary signal running at ~2.7GHz, encoding
  5438. * each output octet as 10 bits. The actual frequency
  5439. * is stored as a divider into a 100MHz clock, and the
  5440. * mode pixel clock is stored in units of 1KHz.
  5441. * Hence the bw of each lane in terms of the mode signal
  5442. * is:
  5443. */
  5444. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5445. fdi_dotclock = adjusted_mode->crtc_clock;
  5446. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5447. pipe_config->pipe_bpp);
  5448. pipe_config->fdi_lanes = lane;
  5449. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5450. link_bw, &pipe_config->fdi_m_n, false);
  5451. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5452. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5453. pipe_config->pipe_bpp -= 2*3;
  5454. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5455. pipe_config->pipe_bpp);
  5456. needs_recompute = true;
  5457. pipe_config->bw_constrained = true;
  5458. goto retry;
  5459. }
  5460. if (needs_recompute)
  5461. return RETRY;
  5462. return ret;
  5463. }
  5464. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5465. {
  5466. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5467. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5468. /* IPS only exists on ULT machines and is tied to pipe A. */
  5469. if (!hsw_crtc_supports_ips(crtc))
  5470. return false;
  5471. if (!i915_modparams.enable_ips)
  5472. return false;
  5473. if (crtc_state->pipe_bpp > 24)
  5474. return false;
  5475. /*
  5476. * We compare against max which means we must take
  5477. * the increased cdclk requirement into account when
  5478. * calculating the new cdclk.
  5479. *
  5480. * Should measure whether using a lower cdclk w/o IPS
  5481. */
  5482. if (IS_BROADWELL(dev_priv) &&
  5483. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5484. return false;
  5485. return true;
  5486. }
  5487. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5488. {
  5489. struct drm_i915_private *dev_priv =
  5490. to_i915(crtc_state->base.crtc->dev);
  5491. struct intel_atomic_state *intel_state =
  5492. to_intel_atomic_state(crtc_state->base.state);
  5493. if (!hsw_crtc_state_ips_capable(crtc_state))
  5494. return false;
  5495. if (crtc_state->ips_force_disable)
  5496. return false;
  5497. /* IPS should be fine as long as at least one plane is enabled. */
  5498. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5499. return false;
  5500. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5501. if (IS_BROADWELL(dev_priv) &&
  5502. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5503. return false;
  5504. return true;
  5505. }
  5506. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5507. {
  5508. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5509. /* GDG double wide on either pipe, otherwise pipe A only */
  5510. return INTEL_GEN(dev_priv) < 4 &&
  5511. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5512. }
  5513. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5514. {
  5515. uint32_t pixel_rate;
  5516. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5517. /*
  5518. * We only use IF-ID interlacing. If we ever use
  5519. * PF-ID we'll need to adjust the pixel_rate here.
  5520. */
  5521. if (pipe_config->pch_pfit.enabled) {
  5522. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5523. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5524. pipe_w = pipe_config->pipe_src_w;
  5525. pipe_h = pipe_config->pipe_src_h;
  5526. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5527. pfit_h = pfit_size & 0xFFFF;
  5528. if (pipe_w < pfit_w)
  5529. pipe_w = pfit_w;
  5530. if (pipe_h < pfit_h)
  5531. pipe_h = pfit_h;
  5532. if (WARN_ON(!pfit_w || !pfit_h))
  5533. return pixel_rate;
  5534. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5535. pfit_w * pfit_h);
  5536. }
  5537. return pixel_rate;
  5538. }
  5539. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5540. {
  5541. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5542. if (HAS_GMCH_DISPLAY(dev_priv))
  5543. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5544. crtc_state->pixel_rate =
  5545. crtc_state->base.adjusted_mode.crtc_clock;
  5546. else
  5547. crtc_state->pixel_rate =
  5548. ilk_pipe_pixel_rate(crtc_state);
  5549. }
  5550. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5551. struct intel_crtc_state *pipe_config)
  5552. {
  5553. struct drm_device *dev = crtc->base.dev;
  5554. struct drm_i915_private *dev_priv = to_i915(dev);
  5555. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5556. int clock_limit = dev_priv->max_dotclk_freq;
  5557. if (INTEL_GEN(dev_priv) < 4) {
  5558. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5559. /*
  5560. * Enable double wide mode when the dot clock
  5561. * is > 90% of the (display) core speed.
  5562. */
  5563. if (intel_crtc_supports_double_wide(crtc) &&
  5564. adjusted_mode->crtc_clock > clock_limit) {
  5565. clock_limit = dev_priv->max_dotclk_freq;
  5566. pipe_config->double_wide = true;
  5567. }
  5568. }
  5569. if (adjusted_mode->crtc_clock > clock_limit) {
  5570. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5571. adjusted_mode->crtc_clock, clock_limit,
  5572. yesno(pipe_config->double_wide));
  5573. return -EINVAL;
  5574. }
  5575. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5576. /*
  5577. * There is only one pipe CSC unit per pipe, and we need that
  5578. * for output conversion from RGB->YCBCR. So if CTM is already
  5579. * applied we can't support YCBCR420 output.
  5580. */
  5581. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5582. return -EINVAL;
  5583. }
  5584. /*
  5585. * Pipe horizontal size must be even in:
  5586. * - DVO ganged mode
  5587. * - LVDS dual channel mode
  5588. * - Double wide pipe
  5589. */
  5590. if (pipe_config->pipe_src_w & 1) {
  5591. if (pipe_config->double_wide) {
  5592. DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
  5593. return -EINVAL;
  5594. }
  5595. if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5596. intel_is_dual_link_lvds(dev)) {
  5597. DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
  5598. return -EINVAL;
  5599. }
  5600. }
  5601. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5602. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5603. */
  5604. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5605. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5606. return -EINVAL;
  5607. intel_crtc_compute_pixel_rate(pipe_config);
  5608. if (pipe_config->has_pch_encoder)
  5609. return ironlake_fdi_compute_config(crtc, pipe_config);
  5610. return 0;
  5611. }
  5612. static void
  5613. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5614. {
  5615. while (*num > DATA_LINK_M_N_MASK ||
  5616. *den > DATA_LINK_M_N_MASK) {
  5617. *num >>= 1;
  5618. *den >>= 1;
  5619. }
  5620. }
  5621. static void compute_m_n(unsigned int m, unsigned int n,
  5622. uint32_t *ret_m, uint32_t *ret_n,
  5623. bool reduce_m_n)
  5624. {
  5625. /*
  5626. * Reduce M/N as much as possible without loss in precision. Several DP
  5627. * dongles in particular seem to be fussy about too large *link* M/N
  5628. * values. The passed in values are more likely to have the least
  5629. * significant bits zero than M after rounding below, so do this first.
  5630. */
  5631. if (reduce_m_n) {
  5632. while ((m & 1) == 0 && (n & 1) == 0) {
  5633. m >>= 1;
  5634. n >>= 1;
  5635. }
  5636. }
  5637. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5638. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5639. intel_reduce_m_n_ratio(ret_m, ret_n);
  5640. }
  5641. void
  5642. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5643. int pixel_clock, int link_clock,
  5644. struct intel_link_m_n *m_n,
  5645. bool reduce_m_n)
  5646. {
  5647. m_n->tu = 64;
  5648. compute_m_n(bits_per_pixel * pixel_clock,
  5649. link_clock * nlanes * 8,
  5650. &m_n->gmch_m, &m_n->gmch_n,
  5651. reduce_m_n);
  5652. compute_m_n(pixel_clock, link_clock,
  5653. &m_n->link_m, &m_n->link_n,
  5654. reduce_m_n);
  5655. }
  5656. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5657. {
  5658. if (i915_modparams.panel_use_ssc >= 0)
  5659. return i915_modparams.panel_use_ssc != 0;
  5660. return dev_priv->vbt.lvds_use_ssc
  5661. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5662. }
  5663. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5664. {
  5665. return (1 << dpll->n) << 16 | dpll->m2;
  5666. }
  5667. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5668. {
  5669. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5670. }
  5671. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5672. struct intel_crtc_state *crtc_state,
  5673. struct dpll *reduced_clock)
  5674. {
  5675. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5676. u32 fp, fp2 = 0;
  5677. if (IS_PINEVIEW(dev_priv)) {
  5678. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5679. if (reduced_clock)
  5680. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5681. } else {
  5682. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5683. if (reduced_clock)
  5684. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5685. }
  5686. crtc_state->dpll_hw_state.fp0 = fp;
  5687. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5688. reduced_clock) {
  5689. crtc_state->dpll_hw_state.fp1 = fp2;
  5690. } else {
  5691. crtc_state->dpll_hw_state.fp1 = fp;
  5692. }
  5693. }
  5694. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5695. pipe)
  5696. {
  5697. u32 reg_val;
  5698. /*
  5699. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5700. * and set it to a reasonable value instead.
  5701. */
  5702. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5703. reg_val &= 0xffffff00;
  5704. reg_val |= 0x00000030;
  5705. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5706. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5707. reg_val &= 0x00ffffff;
  5708. reg_val |= 0x8c000000;
  5709. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5710. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5711. reg_val &= 0xffffff00;
  5712. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5713. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5714. reg_val &= 0x00ffffff;
  5715. reg_val |= 0xb0000000;
  5716. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5717. }
  5718. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5719. struct intel_link_m_n *m_n)
  5720. {
  5721. struct drm_device *dev = crtc->base.dev;
  5722. struct drm_i915_private *dev_priv = to_i915(dev);
  5723. int pipe = crtc->pipe;
  5724. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5725. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5726. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5727. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5728. }
  5729. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5730. struct intel_link_m_n *m_n,
  5731. struct intel_link_m_n *m2_n2)
  5732. {
  5733. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5734. int pipe = crtc->pipe;
  5735. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5736. if (INTEL_GEN(dev_priv) >= 5) {
  5737. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5738. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5739. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5740. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5741. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5742. * for gen < 8) and if DRRS is supported (to make sure the
  5743. * registers are not unnecessarily accessed).
  5744. */
  5745. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5746. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5747. I915_WRITE(PIPE_DATA_M2(transcoder),
  5748. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5749. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5750. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5751. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5752. }
  5753. } else {
  5754. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5755. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5756. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5757. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5758. }
  5759. }
  5760. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5761. {
  5762. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5763. if (m_n == M1_N1) {
  5764. dp_m_n = &crtc->config->dp_m_n;
  5765. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5766. } else if (m_n == M2_N2) {
  5767. /*
  5768. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5769. * needs to be programmed into M1_N1.
  5770. */
  5771. dp_m_n = &crtc->config->dp_m2_n2;
  5772. } else {
  5773. DRM_ERROR("Unsupported divider value\n");
  5774. return;
  5775. }
  5776. if (crtc->config->has_pch_encoder)
  5777. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5778. else
  5779. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5780. }
  5781. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5782. struct intel_crtc_state *pipe_config)
  5783. {
  5784. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5785. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5786. if (crtc->pipe != PIPE_A)
  5787. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5788. /* DPLL not used with DSI, but still need the rest set up */
  5789. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5790. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5791. DPLL_EXT_BUFFER_ENABLE_VLV;
  5792. pipe_config->dpll_hw_state.dpll_md =
  5793. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5794. }
  5795. static void chv_compute_dpll(struct intel_crtc *crtc,
  5796. struct intel_crtc_state *pipe_config)
  5797. {
  5798. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5799. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5800. if (crtc->pipe != PIPE_A)
  5801. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5802. /* DPLL not used with DSI, but still need the rest set up */
  5803. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5804. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5805. pipe_config->dpll_hw_state.dpll_md =
  5806. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5807. }
  5808. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5809. const struct intel_crtc_state *pipe_config)
  5810. {
  5811. struct drm_device *dev = crtc->base.dev;
  5812. struct drm_i915_private *dev_priv = to_i915(dev);
  5813. enum pipe pipe = crtc->pipe;
  5814. u32 mdiv;
  5815. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5816. u32 coreclk, reg_val;
  5817. /* Enable Refclk */
  5818. I915_WRITE(DPLL(pipe),
  5819. pipe_config->dpll_hw_state.dpll &
  5820. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5821. /* No need to actually set up the DPLL with DSI */
  5822. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5823. return;
  5824. mutex_lock(&dev_priv->sb_lock);
  5825. bestn = pipe_config->dpll.n;
  5826. bestm1 = pipe_config->dpll.m1;
  5827. bestm2 = pipe_config->dpll.m2;
  5828. bestp1 = pipe_config->dpll.p1;
  5829. bestp2 = pipe_config->dpll.p2;
  5830. /* See eDP HDMI DPIO driver vbios notes doc */
  5831. /* PLL B needs special handling */
  5832. if (pipe == PIPE_B)
  5833. vlv_pllb_recal_opamp(dev_priv, pipe);
  5834. /* Set up Tx target for periodic Rcomp update */
  5835. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5836. /* Disable target IRef on PLL */
  5837. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5838. reg_val &= 0x00ffffff;
  5839. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5840. /* Disable fast lock */
  5841. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5842. /* Set idtafcrecal before PLL is enabled */
  5843. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5844. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5845. mdiv |= ((bestn << DPIO_N_SHIFT));
  5846. mdiv |= (1 << DPIO_K_SHIFT);
  5847. /*
  5848. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5849. * but we don't support that).
  5850. * Note: don't use the DAC post divider as it seems unstable.
  5851. */
  5852. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5853. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5854. mdiv |= DPIO_ENABLE_CALIBRATION;
  5855. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5856. /* Set HBR and RBR LPF coefficients */
  5857. if (pipe_config->port_clock == 162000 ||
  5858. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5859. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5860. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5861. 0x009f0003);
  5862. else
  5863. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5864. 0x00d0000f);
  5865. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5866. /* Use SSC source */
  5867. if (pipe == PIPE_A)
  5868. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5869. 0x0df40000);
  5870. else
  5871. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5872. 0x0df70000);
  5873. } else { /* HDMI or VGA */
  5874. /* Use bend source */
  5875. if (pipe == PIPE_A)
  5876. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5877. 0x0df70000);
  5878. else
  5879. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5880. 0x0df40000);
  5881. }
  5882. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5883. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5884. if (intel_crtc_has_dp_encoder(crtc->config))
  5885. coreclk |= 0x01000000;
  5886. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5887. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5888. mutex_unlock(&dev_priv->sb_lock);
  5889. }
  5890. static void chv_prepare_pll(struct intel_crtc *crtc,
  5891. const struct intel_crtc_state *pipe_config)
  5892. {
  5893. struct drm_device *dev = crtc->base.dev;
  5894. struct drm_i915_private *dev_priv = to_i915(dev);
  5895. enum pipe pipe = crtc->pipe;
  5896. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5897. u32 loopfilter, tribuf_calcntr;
  5898. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5899. u32 dpio_val;
  5900. int vco;
  5901. /* Enable Refclk and SSC */
  5902. I915_WRITE(DPLL(pipe),
  5903. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5904. /* No need to actually set up the DPLL with DSI */
  5905. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5906. return;
  5907. bestn = pipe_config->dpll.n;
  5908. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5909. bestm1 = pipe_config->dpll.m1;
  5910. bestm2 = pipe_config->dpll.m2 >> 22;
  5911. bestp1 = pipe_config->dpll.p1;
  5912. bestp2 = pipe_config->dpll.p2;
  5913. vco = pipe_config->dpll.vco;
  5914. dpio_val = 0;
  5915. loopfilter = 0;
  5916. mutex_lock(&dev_priv->sb_lock);
  5917. /* p1 and p2 divider */
  5918. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5919. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5920. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5921. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5922. 1 << DPIO_CHV_K_DIV_SHIFT);
  5923. /* Feedback post-divider - m2 */
  5924. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5925. /* Feedback refclk divider - n and m1 */
  5926. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5927. DPIO_CHV_M1_DIV_BY_2 |
  5928. 1 << DPIO_CHV_N_DIV_SHIFT);
  5929. /* M2 fraction division */
  5930. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5931. /* M2 fraction division enable */
  5932. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5933. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5934. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5935. if (bestm2_frac)
  5936. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5937. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5938. /* Program digital lock detect threshold */
  5939. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5940. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5941. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5942. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5943. if (!bestm2_frac)
  5944. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5945. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5946. /* Loop filter */
  5947. if (vco == 5400000) {
  5948. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5949. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5950. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5951. tribuf_calcntr = 0x9;
  5952. } else if (vco <= 6200000) {
  5953. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5954. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5955. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5956. tribuf_calcntr = 0x9;
  5957. } else if (vco <= 6480000) {
  5958. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5959. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5960. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5961. tribuf_calcntr = 0x8;
  5962. } else {
  5963. /* Not supported. Apply the same limits as in the max case */
  5964. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5965. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5966. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5967. tribuf_calcntr = 0;
  5968. }
  5969. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5970. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5971. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5972. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5973. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5974. /* AFC Recal */
  5975. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5976. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5977. DPIO_AFC_RECAL);
  5978. mutex_unlock(&dev_priv->sb_lock);
  5979. }
  5980. /**
  5981. * vlv_force_pll_on - forcibly enable just the PLL
  5982. * @dev_priv: i915 private structure
  5983. * @pipe: pipe PLL to enable
  5984. * @dpll: PLL configuration
  5985. *
  5986. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5987. * in cases where we need the PLL enabled even when @pipe is not going to
  5988. * be enabled.
  5989. */
  5990. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5991. const struct dpll *dpll)
  5992. {
  5993. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5994. struct intel_crtc_state *pipe_config;
  5995. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5996. if (!pipe_config)
  5997. return -ENOMEM;
  5998. pipe_config->base.crtc = &crtc->base;
  5999. pipe_config->pixel_multiplier = 1;
  6000. pipe_config->dpll = *dpll;
  6001. if (IS_CHERRYVIEW(dev_priv)) {
  6002. chv_compute_dpll(crtc, pipe_config);
  6003. chv_prepare_pll(crtc, pipe_config);
  6004. chv_enable_pll(crtc, pipe_config);
  6005. } else {
  6006. vlv_compute_dpll(crtc, pipe_config);
  6007. vlv_prepare_pll(crtc, pipe_config);
  6008. vlv_enable_pll(crtc, pipe_config);
  6009. }
  6010. kfree(pipe_config);
  6011. return 0;
  6012. }
  6013. /**
  6014. * vlv_force_pll_off - forcibly disable just the PLL
  6015. * @dev_priv: i915 private structure
  6016. * @pipe: pipe PLL to disable
  6017. *
  6018. * Disable the PLL for @pipe. To be used in cases where we need
  6019. * the PLL enabled even when @pipe is not going to be enabled.
  6020. */
  6021. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6022. {
  6023. if (IS_CHERRYVIEW(dev_priv))
  6024. chv_disable_pll(dev_priv, pipe);
  6025. else
  6026. vlv_disable_pll(dev_priv, pipe);
  6027. }
  6028. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6029. struct intel_crtc_state *crtc_state,
  6030. struct dpll *reduced_clock)
  6031. {
  6032. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6033. u32 dpll;
  6034. struct dpll *clock = &crtc_state->dpll;
  6035. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6036. dpll = DPLL_VGA_MODE_DIS;
  6037. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6038. dpll |= DPLLB_MODE_LVDS;
  6039. else
  6040. dpll |= DPLLB_MODE_DAC_SERIAL;
  6041. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6042. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6043. dpll |= (crtc_state->pixel_multiplier - 1)
  6044. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6045. }
  6046. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6047. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6048. dpll |= DPLL_SDVO_HIGH_SPEED;
  6049. if (intel_crtc_has_dp_encoder(crtc_state))
  6050. dpll |= DPLL_SDVO_HIGH_SPEED;
  6051. /* compute bitmask from p1 value */
  6052. if (IS_PINEVIEW(dev_priv))
  6053. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6054. else {
  6055. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6056. if (IS_G4X(dev_priv) && reduced_clock)
  6057. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6058. }
  6059. switch (clock->p2) {
  6060. case 5:
  6061. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6062. break;
  6063. case 7:
  6064. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6065. break;
  6066. case 10:
  6067. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6068. break;
  6069. case 14:
  6070. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6071. break;
  6072. }
  6073. if (INTEL_GEN(dev_priv) >= 4)
  6074. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6075. if (crtc_state->sdvo_tv_clock)
  6076. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6077. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6078. intel_panel_use_ssc(dev_priv))
  6079. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6080. else
  6081. dpll |= PLL_REF_INPUT_DREFCLK;
  6082. dpll |= DPLL_VCO_ENABLE;
  6083. crtc_state->dpll_hw_state.dpll = dpll;
  6084. if (INTEL_GEN(dev_priv) >= 4) {
  6085. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6086. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6087. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6088. }
  6089. }
  6090. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6091. struct intel_crtc_state *crtc_state,
  6092. struct dpll *reduced_clock)
  6093. {
  6094. struct drm_device *dev = crtc->base.dev;
  6095. struct drm_i915_private *dev_priv = to_i915(dev);
  6096. u32 dpll;
  6097. struct dpll *clock = &crtc_state->dpll;
  6098. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6099. dpll = DPLL_VGA_MODE_DIS;
  6100. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6101. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6102. } else {
  6103. if (clock->p1 == 2)
  6104. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6105. else
  6106. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6107. if (clock->p2 == 4)
  6108. dpll |= PLL_P2_DIVIDE_BY_4;
  6109. }
  6110. if (!IS_I830(dev_priv) &&
  6111. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6112. dpll |= DPLL_DVO_2X_MODE;
  6113. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6114. intel_panel_use_ssc(dev_priv))
  6115. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6116. else
  6117. dpll |= PLL_REF_INPUT_DREFCLK;
  6118. dpll |= DPLL_VCO_ENABLE;
  6119. crtc_state->dpll_hw_state.dpll = dpll;
  6120. }
  6121. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6122. {
  6123. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6124. enum pipe pipe = intel_crtc->pipe;
  6125. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6126. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6127. uint32_t crtc_vtotal, crtc_vblank_end;
  6128. int vsyncshift = 0;
  6129. /* We need to be careful not to changed the adjusted mode, for otherwise
  6130. * the hw state checker will get angry at the mismatch. */
  6131. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6132. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6133. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6134. /* the chip adds 2 halflines automatically */
  6135. crtc_vtotal -= 1;
  6136. crtc_vblank_end -= 1;
  6137. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6138. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6139. else
  6140. vsyncshift = adjusted_mode->crtc_hsync_start -
  6141. adjusted_mode->crtc_htotal / 2;
  6142. if (vsyncshift < 0)
  6143. vsyncshift += adjusted_mode->crtc_htotal;
  6144. }
  6145. if (INTEL_GEN(dev_priv) > 3)
  6146. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6147. I915_WRITE(HTOTAL(cpu_transcoder),
  6148. (adjusted_mode->crtc_hdisplay - 1) |
  6149. ((adjusted_mode->crtc_htotal - 1) << 16));
  6150. I915_WRITE(HBLANK(cpu_transcoder),
  6151. (adjusted_mode->crtc_hblank_start - 1) |
  6152. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6153. I915_WRITE(HSYNC(cpu_transcoder),
  6154. (adjusted_mode->crtc_hsync_start - 1) |
  6155. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6156. I915_WRITE(VTOTAL(cpu_transcoder),
  6157. (adjusted_mode->crtc_vdisplay - 1) |
  6158. ((crtc_vtotal - 1) << 16));
  6159. I915_WRITE(VBLANK(cpu_transcoder),
  6160. (adjusted_mode->crtc_vblank_start - 1) |
  6161. ((crtc_vblank_end - 1) << 16));
  6162. I915_WRITE(VSYNC(cpu_transcoder),
  6163. (adjusted_mode->crtc_vsync_start - 1) |
  6164. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6165. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6166. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6167. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6168. * bits. */
  6169. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6170. (pipe == PIPE_B || pipe == PIPE_C))
  6171. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6172. }
  6173. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6174. {
  6175. struct drm_device *dev = intel_crtc->base.dev;
  6176. struct drm_i915_private *dev_priv = to_i915(dev);
  6177. enum pipe pipe = intel_crtc->pipe;
  6178. /* pipesrc controls the size that is scaled from, which should
  6179. * always be the user's requested size.
  6180. */
  6181. I915_WRITE(PIPESRC(pipe),
  6182. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6183. (intel_crtc->config->pipe_src_h - 1));
  6184. }
  6185. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6186. struct intel_crtc_state *pipe_config)
  6187. {
  6188. struct drm_device *dev = crtc->base.dev;
  6189. struct drm_i915_private *dev_priv = to_i915(dev);
  6190. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6191. uint32_t tmp;
  6192. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6193. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6194. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6195. tmp = I915_READ(HBLANK(cpu_transcoder));
  6196. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6197. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6198. tmp = I915_READ(HSYNC(cpu_transcoder));
  6199. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6200. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6201. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6202. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6203. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6204. tmp = I915_READ(VBLANK(cpu_transcoder));
  6205. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6206. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6207. tmp = I915_READ(VSYNC(cpu_transcoder));
  6208. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6209. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6210. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6211. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6212. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6213. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6214. }
  6215. }
  6216. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6217. struct intel_crtc_state *pipe_config)
  6218. {
  6219. struct drm_device *dev = crtc->base.dev;
  6220. struct drm_i915_private *dev_priv = to_i915(dev);
  6221. u32 tmp;
  6222. tmp = I915_READ(PIPESRC(crtc->pipe));
  6223. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6224. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6225. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6226. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6227. }
  6228. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6229. struct intel_crtc_state *pipe_config)
  6230. {
  6231. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6232. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6233. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6234. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6235. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6236. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6237. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6238. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6239. mode->flags = pipe_config->base.adjusted_mode.flags;
  6240. mode->type = DRM_MODE_TYPE_DRIVER;
  6241. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6242. mode->hsync = drm_mode_hsync(mode);
  6243. mode->vrefresh = drm_mode_vrefresh(mode);
  6244. drm_mode_set_name(mode);
  6245. }
  6246. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6247. {
  6248. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6249. uint32_t pipeconf;
  6250. pipeconf = 0;
  6251. /* we keep both pipes enabled on 830 */
  6252. if (IS_I830(dev_priv))
  6253. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6254. if (intel_crtc->config->double_wide)
  6255. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6256. /* only g4x and later have fancy bpc/dither controls */
  6257. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6258. IS_CHERRYVIEW(dev_priv)) {
  6259. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6260. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6261. pipeconf |= PIPECONF_DITHER_EN |
  6262. PIPECONF_DITHER_TYPE_SP;
  6263. switch (intel_crtc->config->pipe_bpp) {
  6264. case 18:
  6265. pipeconf |= PIPECONF_6BPC;
  6266. break;
  6267. case 24:
  6268. pipeconf |= PIPECONF_8BPC;
  6269. break;
  6270. case 30:
  6271. pipeconf |= PIPECONF_10BPC;
  6272. break;
  6273. default:
  6274. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6275. BUG();
  6276. }
  6277. }
  6278. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6279. if (INTEL_GEN(dev_priv) < 4 ||
  6280. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6281. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6282. else
  6283. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6284. } else
  6285. pipeconf |= PIPECONF_PROGRESSIVE;
  6286. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6287. intel_crtc->config->limited_color_range)
  6288. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6289. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6290. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6291. }
  6292. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6293. struct intel_crtc_state *crtc_state)
  6294. {
  6295. struct drm_device *dev = crtc->base.dev;
  6296. struct drm_i915_private *dev_priv = to_i915(dev);
  6297. const struct intel_limit *limit;
  6298. int refclk = 48000;
  6299. memset(&crtc_state->dpll_hw_state, 0,
  6300. sizeof(crtc_state->dpll_hw_state));
  6301. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6302. if (intel_panel_use_ssc(dev_priv)) {
  6303. refclk = dev_priv->vbt.lvds_ssc_freq;
  6304. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6305. }
  6306. limit = &intel_limits_i8xx_lvds;
  6307. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6308. limit = &intel_limits_i8xx_dvo;
  6309. } else {
  6310. limit = &intel_limits_i8xx_dac;
  6311. }
  6312. if (!crtc_state->clock_set &&
  6313. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6314. refclk, NULL, &crtc_state->dpll)) {
  6315. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6316. return -EINVAL;
  6317. }
  6318. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6319. return 0;
  6320. }
  6321. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6322. struct intel_crtc_state *crtc_state)
  6323. {
  6324. struct drm_device *dev = crtc->base.dev;
  6325. struct drm_i915_private *dev_priv = to_i915(dev);
  6326. const struct intel_limit *limit;
  6327. int refclk = 96000;
  6328. memset(&crtc_state->dpll_hw_state, 0,
  6329. sizeof(crtc_state->dpll_hw_state));
  6330. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6331. if (intel_panel_use_ssc(dev_priv)) {
  6332. refclk = dev_priv->vbt.lvds_ssc_freq;
  6333. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6334. }
  6335. if (intel_is_dual_link_lvds(dev))
  6336. limit = &intel_limits_g4x_dual_channel_lvds;
  6337. else
  6338. limit = &intel_limits_g4x_single_channel_lvds;
  6339. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6340. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6341. limit = &intel_limits_g4x_hdmi;
  6342. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6343. limit = &intel_limits_g4x_sdvo;
  6344. } else {
  6345. /* The option is for other outputs */
  6346. limit = &intel_limits_i9xx_sdvo;
  6347. }
  6348. if (!crtc_state->clock_set &&
  6349. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6350. refclk, NULL, &crtc_state->dpll)) {
  6351. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6352. return -EINVAL;
  6353. }
  6354. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6355. return 0;
  6356. }
  6357. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6358. struct intel_crtc_state *crtc_state)
  6359. {
  6360. struct drm_device *dev = crtc->base.dev;
  6361. struct drm_i915_private *dev_priv = to_i915(dev);
  6362. const struct intel_limit *limit;
  6363. int refclk = 96000;
  6364. memset(&crtc_state->dpll_hw_state, 0,
  6365. sizeof(crtc_state->dpll_hw_state));
  6366. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6367. if (intel_panel_use_ssc(dev_priv)) {
  6368. refclk = dev_priv->vbt.lvds_ssc_freq;
  6369. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6370. }
  6371. limit = &intel_limits_pineview_lvds;
  6372. } else {
  6373. limit = &intel_limits_pineview_sdvo;
  6374. }
  6375. if (!crtc_state->clock_set &&
  6376. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6377. refclk, NULL, &crtc_state->dpll)) {
  6378. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6379. return -EINVAL;
  6380. }
  6381. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6382. return 0;
  6383. }
  6384. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6385. struct intel_crtc_state *crtc_state)
  6386. {
  6387. struct drm_device *dev = crtc->base.dev;
  6388. struct drm_i915_private *dev_priv = to_i915(dev);
  6389. const struct intel_limit *limit;
  6390. int refclk = 96000;
  6391. memset(&crtc_state->dpll_hw_state, 0,
  6392. sizeof(crtc_state->dpll_hw_state));
  6393. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6394. if (intel_panel_use_ssc(dev_priv)) {
  6395. refclk = dev_priv->vbt.lvds_ssc_freq;
  6396. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6397. }
  6398. limit = &intel_limits_i9xx_lvds;
  6399. } else {
  6400. limit = &intel_limits_i9xx_sdvo;
  6401. }
  6402. if (!crtc_state->clock_set &&
  6403. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6404. refclk, NULL, &crtc_state->dpll)) {
  6405. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6406. return -EINVAL;
  6407. }
  6408. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6409. return 0;
  6410. }
  6411. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6412. struct intel_crtc_state *crtc_state)
  6413. {
  6414. int refclk = 100000;
  6415. const struct intel_limit *limit = &intel_limits_chv;
  6416. memset(&crtc_state->dpll_hw_state, 0,
  6417. sizeof(crtc_state->dpll_hw_state));
  6418. if (!crtc_state->clock_set &&
  6419. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6420. refclk, NULL, &crtc_state->dpll)) {
  6421. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6422. return -EINVAL;
  6423. }
  6424. chv_compute_dpll(crtc, crtc_state);
  6425. return 0;
  6426. }
  6427. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6428. struct intel_crtc_state *crtc_state)
  6429. {
  6430. int refclk = 100000;
  6431. const struct intel_limit *limit = &intel_limits_vlv;
  6432. memset(&crtc_state->dpll_hw_state, 0,
  6433. sizeof(crtc_state->dpll_hw_state));
  6434. if (!crtc_state->clock_set &&
  6435. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6436. refclk, NULL, &crtc_state->dpll)) {
  6437. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6438. return -EINVAL;
  6439. }
  6440. vlv_compute_dpll(crtc, crtc_state);
  6441. return 0;
  6442. }
  6443. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6444. struct intel_crtc_state *pipe_config)
  6445. {
  6446. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6447. uint32_t tmp;
  6448. if (INTEL_GEN(dev_priv) <= 3 &&
  6449. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6450. return;
  6451. tmp = I915_READ(PFIT_CONTROL);
  6452. if (!(tmp & PFIT_ENABLE))
  6453. return;
  6454. /* Check whether the pfit is attached to our pipe. */
  6455. if (INTEL_GEN(dev_priv) < 4) {
  6456. if (crtc->pipe != PIPE_B)
  6457. return;
  6458. } else {
  6459. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6460. return;
  6461. }
  6462. pipe_config->gmch_pfit.control = tmp;
  6463. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6464. }
  6465. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6466. struct intel_crtc_state *pipe_config)
  6467. {
  6468. struct drm_device *dev = crtc->base.dev;
  6469. struct drm_i915_private *dev_priv = to_i915(dev);
  6470. int pipe = pipe_config->cpu_transcoder;
  6471. struct dpll clock;
  6472. u32 mdiv;
  6473. int refclk = 100000;
  6474. /* In case of DSI, DPLL will not be used */
  6475. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6476. return;
  6477. mutex_lock(&dev_priv->sb_lock);
  6478. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6479. mutex_unlock(&dev_priv->sb_lock);
  6480. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6481. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6482. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6483. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6484. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6485. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6486. }
  6487. static void
  6488. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6489. struct intel_initial_plane_config *plane_config)
  6490. {
  6491. struct drm_device *dev = crtc->base.dev;
  6492. struct drm_i915_private *dev_priv = to_i915(dev);
  6493. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6494. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6495. enum pipe pipe;
  6496. u32 val, base, offset;
  6497. int fourcc, pixel_format;
  6498. unsigned int aligned_height;
  6499. struct drm_framebuffer *fb;
  6500. struct intel_framebuffer *intel_fb;
  6501. if (!plane->get_hw_state(plane, &pipe))
  6502. return;
  6503. WARN_ON(pipe != crtc->pipe);
  6504. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6505. if (!intel_fb) {
  6506. DRM_DEBUG_KMS("failed to alloc fb\n");
  6507. return;
  6508. }
  6509. fb = &intel_fb->base;
  6510. fb->dev = dev;
  6511. val = I915_READ(DSPCNTR(i9xx_plane));
  6512. if (INTEL_GEN(dev_priv) >= 4) {
  6513. if (val & DISPPLANE_TILED) {
  6514. plane_config->tiling = I915_TILING_X;
  6515. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6516. }
  6517. }
  6518. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6519. fourcc = i9xx_format_to_fourcc(pixel_format);
  6520. fb->format = drm_format_info(fourcc);
  6521. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6522. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6523. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6524. } else if (INTEL_GEN(dev_priv) >= 4) {
  6525. if (plane_config->tiling)
  6526. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6527. else
  6528. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6529. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6530. } else {
  6531. base = I915_READ(DSPADDR(i9xx_plane));
  6532. }
  6533. plane_config->base = base;
  6534. val = I915_READ(PIPESRC(pipe));
  6535. fb->width = ((val >> 16) & 0xfff) + 1;
  6536. fb->height = ((val >> 0) & 0xfff) + 1;
  6537. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6538. fb->pitches[0] = val & 0xffffffc0;
  6539. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6540. plane_config->size = fb->pitches[0] * aligned_height;
  6541. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6542. crtc->base.name, plane->base.name, fb->width, fb->height,
  6543. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6544. plane_config->size);
  6545. plane_config->fb = intel_fb;
  6546. }
  6547. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6548. struct intel_crtc_state *pipe_config)
  6549. {
  6550. struct drm_device *dev = crtc->base.dev;
  6551. struct drm_i915_private *dev_priv = to_i915(dev);
  6552. int pipe = pipe_config->cpu_transcoder;
  6553. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6554. struct dpll clock;
  6555. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6556. int refclk = 100000;
  6557. /* In case of DSI, DPLL will not be used */
  6558. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6559. return;
  6560. mutex_lock(&dev_priv->sb_lock);
  6561. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6562. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6563. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6564. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6565. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6566. mutex_unlock(&dev_priv->sb_lock);
  6567. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6568. clock.m2 = (pll_dw0 & 0xff) << 22;
  6569. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6570. clock.m2 |= pll_dw2 & 0x3fffff;
  6571. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6572. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6573. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6574. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6575. }
  6576. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6577. struct intel_crtc_state *pipe_config)
  6578. {
  6579. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6580. enum intel_display_power_domain power_domain;
  6581. uint32_t tmp;
  6582. bool ret;
  6583. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6584. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6585. return false;
  6586. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6587. pipe_config->shared_dpll = NULL;
  6588. ret = false;
  6589. tmp = I915_READ(PIPECONF(crtc->pipe));
  6590. if (!(tmp & PIPECONF_ENABLE))
  6591. goto out;
  6592. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6593. IS_CHERRYVIEW(dev_priv)) {
  6594. switch (tmp & PIPECONF_BPC_MASK) {
  6595. case PIPECONF_6BPC:
  6596. pipe_config->pipe_bpp = 18;
  6597. break;
  6598. case PIPECONF_8BPC:
  6599. pipe_config->pipe_bpp = 24;
  6600. break;
  6601. case PIPECONF_10BPC:
  6602. pipe_config->pipe_bpp = 30;
  6603. break;
  6604. default:
  6605. break;
  6606. }
  6607. }
  6608. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6609. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6610. pipe_config->limited_color_range = true;
  6611. if (INTEL_GEN(dev_priv) < 4)
  6612. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6613. intel_get_pipe_timings(crtc, pipe_config);
  6614. intel_get_pipe_src_size(crtc, pipe_config);
  6615. i9xx_get_pfit_config(crtc, pipe_config);
  6616. if (INTEL_GEN(dev_priv) >= 4) {
  6617. /* No way to read it out on pipes B and C */
  6618. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6619. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6620. else
  6621. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6622. pipe_config->pixel_multiplier =
  6623. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6624. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6625. pipe_config->dpll_hw_state.dpll_md = tmp;
  6626. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6627. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6628. tmp = I915_READ(DPLL(crtc->pipe));
  6629. pipe_config->pixel_multiplier =
  6630. ((tmp & SDVO_MULTIPLIER_MASK)
  6631. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6632. } else {
  6633. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6634. * port and will be fixed up in the encoder->get_config
  6635. * function. */
  6636. pipe_config->pixel_multiplier = 1;
  6637. }
  6638. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6639. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6640. /*
  6641. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6642. * on 830. Filter it out here so that we don't
  6643. * report errors due to that.
  6644. */
  6645. if (IS_I830(dev_priv))
  6646. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6647. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6648. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6649. } else {
  6650. /* Mask out read-only status bits. */
  6651. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6652. DPLL_PORTC_READY_MASK |
  6653. DPLL_PORTB_READY_MASK);
  6654. }
  6655. if (IS_CHERRYVIEW(dev_priv))
  6656. chv_crtc_clock_get(crtc, pipe_config);
  6657. else if (IS_VALLEYVIEW(dev_priv))
  6658. vlv_crtc_clock_get(crtc, pipe_config);
  6659. else
  6660. i9xx_crtc_clock_get(crtc, pipe_config);
  6661. /*
  6662. * Normally the dotclock is filled in by the encoder .get_config()
  6663. * but in case the pipe is enabled w/o any ports we need a sane
  6664. * default.
  6665. */
  6666. pipe_config->base.adjusted_mode.crtc_clock =
  6667. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6668. ret = true;
  6669. out:
  6670. intel_display_power_put(dev_priv, power_domain);
  6671. return ret;
  6672. }
  6673. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6674. {
  6675. struct intel_encoder *encoder;
  6676. int i;
  6677. u32 val, final;
  6678. bool has_lvds = false;
  6679. bool has_cpu_edp = false;
  6680. bool has_panel = false;
  6681. bool has_ck505 = false;
  6682. bool can_ssc = false;
  6683. bool using_ssc_source = false;
  6684. /* We need to take the global config into account */
  6685. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6686. switch (encoder->type) {
  6687. case INTEL_OUTPUT_LVDS:
  6688. has_panel = true;
  6689. has_lvds = true;
  6690. break;
  6691. case INTEL_OUTPUT_EDP:
  6692. has_panel = true;
  6693. if (encoder->port == PORT_A)
  6694. has_cpu_edp = true;
  6695. break;
  6696. default:
  6697. break;
  6698. }
  6699. }
  6700. if (HAS_PCH_IBX(dev_priv)) {
  6701. has_ck505 = dev_priv->vbt.display_clock_mode;
  6702. can_ssc = has_ck505;
  6703. } else {
  6704. has_ck505 = false;
  6705. can_ssc = true;
  6706. }
  6707. /* Check if any DPLLs are using the SSC source */
  6708. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6709. u32 temp = I915_READ(PCH_DPLL(i));
  6710. if (!(temp & DPLL_VCO_ENABLE))
  6711. continue;
  6712. if ((temp & PLL_REF_INPUT_MASK) ==
  6713. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6714. using_ssc_source = true;
  6715. break;
  6716. }
  6717. }
  6718. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6719. has_panel, has_lvds, has_ck505, using_ssc_source);
  6720. /* Ironlake: try to setup display ref clock before DPLL
  6721. * enabling. This is only under driver's control after
  6722. * PCH B stepping, previous chipset stepping should be
  6723. * ignoring this setting.
  6724. */
  6725. val = I915_READ(PCH_DREF_CONTROL);
  6726. /* As we must carefully and slowly disable/enable each source in turn,
  6727. * compute the final state we want first and check if we need to
  6728. * make any changes at all.
  6729. */
  6730. final = val;
  6731. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6732. if (has_ck505)
  6733. final |= DREF_NONSPREAD_CK505_ENABLE;
  6734. else
  6735. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6736. final &= ~DREF_SSC_SOURCE_MASK;
  6737. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6738. final &= ~DREF_SSC1_ENABLE;
  6739. if (has_panel) {
  6740. final |= DREF_SSC_SOURCE_ENABLE;
  6741. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6742. final |= DREF_SSC1_ENABLE;
  6743. if (has_cpu_edp) {
  6744. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6745. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6746. else
  6747. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6748. } else
  6749. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6750. } else if (using_ssc_source) {
  6751. final |= DREF_SSC_SOURCE_ENABLE;
  6752. final |= DREF_SSC1_ENABLE;
  6753. }
  6754. if (final == val)
  6755. return;
  6756. /* Always enable nonspread source */
  6757. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6758. if (has_ck505)
  6759. val |= DREF_NONSPREAD_CK505_ENABLE;
  6760. else
  6761. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6762. if (has_panel) {
  6763. val &= ~DREF_SSC_SOURCE_MASK;
  6764. val |= DREF_SSC_SOURCE_ENABLE;
  6765. /* SSC must be turned on before enabling the CPU output */
  6766. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6767. DRM_DEBUG_KMS("Using SSC on panel\n");
  6768. val |= DREF_SSC1_ENABLE;
  6769. } else
  6770. val &= ~DREF_SSC1_ENABLE;
  6771. /* Get SSC going before enabling the outputs */
  6772. I915_WRITE(PCH_DREF_CONTROL, val);
  6773. POSTING_READ(PCH_DREF_CONTROL);
  6774. udelay(200);
  6775. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6776. /* Enable CPU source on CPU attached eDP */
  6777. if (has_cpu_edp) {
  6778. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6779. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6780. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6781. } else
  6782. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6783. } else
  6784. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6785. I915_WRITE(PCH_DREF_CONTROL, val);
  6786. POSTING_READ(PCH_DREF_CONTROL);
  6787. udelay(200);
  6788. } else {
  6789. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6790. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6791. /* Turn off CPU output */
  6792. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6793. I915_WRITE(PCH_DREF_CONTROL, val);
  6794. POSTING_READ(PCH_DREF_CONTROL);
  6795. udelay(200);
  6796. if (!using_ssc_source) {
  6797. DRM_DEBUG_KMS("Disabling SSC source\n");
  6798. /* Turn off the SSC source */
  6799. val &= ~DREF_SSC_SOURCE_MASK;
  6800. val |= DREF_SSC_SOURCE_DISABLE;
  6801. /* Turn off SSC1 */
  6802. val &= ~DREF_SSC1_ENABLE;
  6803. I915_WRITE(PCH_DREF_CONTROL, val);
  6804. POSTING_READ(PCH_DREF_CONTROL);
  6805. udelay(200);
  6806. }
  6807. }
  6808. BUG_ON(val != final);
  6809. }
  6810. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6811. {
  6812. uint32_t tmp;
  6813. tmp = I915_READ(SOUTH_CHICKEN2);
  6814. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6815. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6816. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6817. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6818. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6819. tmp = I915_READ(SOUTH_CHICKEN2);
  6820. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6821. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6822. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6823. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6824. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6825. }
  6826. /* WaMPhyProgramming:hsw */
  6827. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6828. {
  6829. uint32_t tmp;
  6830. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6831. tmp &= ~(0xFF << 24);
  6832. tmp |= (0x12 << 24);
  6833. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6834. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6835. tmp |= (1 << 11);
  6836. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6837. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6838. tmp |= (1 << 11);
  6839. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6840. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6841. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6842. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6843. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6844. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6845. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6846. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6847. tmp &= ~(7 << 13);
  6848. tmp |= (5 << 13);
  6849. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6850. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6851. tmp &= ~(7 << 13);
  6852. tmp |= (5 << 13);
  6853. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6854. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6855. tmp &= ~0xFF;
  6856. tmp |= 0x1C;
  6857. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6858. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6859. tmp &= ~0xFF;
  6860. tmp |= 0x1C;
  6861. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6862. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6863. tmp &= ~(0xFF << 16);
  6864. tmp |= (0x1C << 16);
  6865. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6866. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6867. tmp &= ~(0xFF << 16);
  6868. tmp |= (0x1C << 16);
  6869. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6870. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6871. tmp |= (1 << 27);
  6872. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6873. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6874. tmp |= (1 << 27);
  6875. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6876. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6877. tmp &= ~(0xF << 28);
  6878. tmp |= (4 << 28);
  6879. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6880. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6881. tmp &= ~(0xF << 28);
  6882. tmp |= (4 << 28);
  6883. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6884. }
  6885. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6886. * Programming" based on the parameters passed:
  6887. * - Sequence to enable CLKOUT_DP
  6888. * - Sequence to enable CLKOUT_DP without spread
  6889. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6890. */
  6891. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6892. bool with_spread, bool with_fdi)
  6893. {
  6894. uint32_t reg, tmp;
  6895. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6896. with_spread = true;
  6897. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6898. with_fdi, "LP PCH doesn't have FDI\n"))
  6899. with_fdi = false;
  6900. mutex_lock(&dev_priv->sb_lock);
  6901. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6902. tmp &= ~SBI_SSCCTL_DISABLE;
  6903. tmp |= SBI_SSCCTL_PATHALT;
  6904. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6905. udelay(24);
  6906. if (with_spread) {
  6907. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6908. tmp &= ~SBI_SSCCTL_PATHALT;
  6909. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6910. if (with_fdi) {
  6911. lpt_reset_fdi_mphy(dev_priv);
  6912. lpt_program_fdi_mphy(dev_priv);
  6913. }
  6914. }
  6915. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6916. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6917. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6918. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6919. mutex_unlock(&dev_priv->sb_lock);
  6920. }
  6921. /* Sequence to disable CLKOUT_DP */
  6922. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6923. {
  6924. uint32_t reg, tmp;
  6925. mutex_lock(&dev_priv->sb_lock);
  6926. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6927. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6928. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6929. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6930. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6931. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6932. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6933. tmp |= SBI_SSCCTL_PATHALT;
  6934. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6935. udelay(32);
  6936. }
  6937. tmp |= SBI_SSCCTL_DISABLE;
  6938. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6939. }
  6940. mutex_unlock(&dev_priv->sb_lock);
  6941. }
  6942. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6943. static const uint16_t sscdivintphase[] = {
  6944. [BEND_IDX( 50)] = 0x3B23,
  6945. [BEND_IDX( 45)] = 0x3B23,
  6946. [BEND_IDX( 40)] = 0x3C23,
  6947. [BEND_IDX( 35)] = 0x3C23,
  6948. [BEND_IDX( 30)] = 0x3D23,
  6949. [BEND_IDX( 25)] = 0x3D23,
  6950. [BEND_IDX( 20)] = 0x3E23,
  6951. [BEND_IDX( 15)] = 0x3E23,
  6952. [BEND_IDX( 10)] = 0x3F23,
  6953. [BEND_IDX( 5)] = 0x3F23,
  6954. [BEND_IDX( 0)] = 0x0025,
  6955. [BEND_IDX( -5)] = 0x0025,
  6956. [BEND_IDX(-10)] = 0x0125,
  6957. [BEND_IDX(-15)] = 0x0125,
  6958. [BEND_IDX(-20)] = 0x0225,
  6959. [BEND_IDX(-25)] = 0x0225,
  6960. [BEND_IDX(-30)] = 0x0325,
  6961. [BEND_IDX(-35)] = 0x0325,
  6962. [BEND_IDX(-40)] = 0x0425,
  6963. [BEND_IDX(-45)] = 0x0425,
  6964. [BEND_IDX(-50)] = 0x0525,
  6965. };
  6966. /*
  6967. * Bend CLKOUT_DP
  6968. * steps -50 to 50 inclusive, in steps of 5
  6969. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6970. * change in clock period = -(steps / 10) * 5.787 ps
  6971. */
  6972. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6973. {
  6974. uint32_t tmp;
  6975. int idx = BEND_IDX(steps);
  6976. if (WARN_ON(steps % 5 != 0))
  6977. return;
  6978. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6979. return;
  6980. mutex_lock(&dev_priv->sb_lock);
  6981. if (steps % 10 != 0)
  6982. tmp = 0xAAAAAAAB;
  6983. else
  6984. tmp = 0x00000000;
  6985. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6986. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6987. tmp &= 0xffff0000;
  6988. tmp |= sscdivintphase[idx];
  6989. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6990. mutex_unlock(&dev_priv->sb_lock);
  6991. }
  6992. #undef BEND_IDX
  6993. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6994. {
  6995. struct intel_encoder *encoder;
  6996. bool has_vga = false;
  6997. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6998. switch (encoder->type) {
  6999. case INTEL_OUTPUT_ANALOG:
  7000. has_vga = true;
  7001. break;
  7002. default:
  7003. break;
  7004. }
  7005. }
  7006. if (has_vga) {
  7007. lpt_bend_clkout_dp(dev_priv, 0);
  7008. lpt_enable_clkout_dp(dev_priv, true, true);
  7009. } else {
  7010. lpt_disable_clkout_dp(dev_priv);
  7011. }
  7012. }
  7013. /*
  7014. * Initialize reference clocks when the driver loads
  7015. */
  7016. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  7017. {
  7018. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7019. ironlake_init_pch_refclk(dev_priv);
  7020. else if (HAS_PCH_LPT(dev_priv))
  7021. lpt_init_pch_refclk(dev_priv);
  7022. }
  7023. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7024. {
  7025. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7026. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7027. int pipe = intel_crtc->pipe;
  7028. uint32_t val;
  7029. val = 0;
  7030. switch (intel_crtc->config->pipe_bpp) {
  7031. case 18:
  7032. val |= PIPECONF_6BPC;
  7033. break;
  7034. case 24:
  7035. val |= PIPECONF_8BPC;
  7036. break;
  7037. case 30:
  7038. val |= PIPECONF_10BPC;
  7039. break;
  7040. case 36:
  7041. val |= PIPECONF_12BPC;
  7042. break;
  7043. default:
  7044. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7045. BUG();
  7046. }
  7047. if (intel_crtc->config->dither)
  7048. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7049. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7050. val |= PIPECONF_INTERLACED_ILK;
  7051. else
  7052. val |= PIPECONF_PROGRESSIVE;
  7053. if (intel_crtc->config->limited_color_range)
  7054. val |= PIPECONF_COLOR_RANGE_SELECT;
  7055. I915_WRITE(PIPECONF(pipe), val);
  7056. POSTING_READ(PIPECONF(pipe));
  7057. }
  7058. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7059. {
  7060. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7061. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7062. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7063. u32 val = 0;
  7064. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7065. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7066. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7067. val |= PIPECONF_INTERLACED_ILK;
  7068. else
  7069. val |= PIPECONF_PROGRESSIVE;
  7070. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7071. POSTING_READ(PIPECONF(cpu_transcoder));
  7072. }
  7073. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7074. {
  7075. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7076. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7077. struct intel_crtc_state *config = intel_crtc->config;
  7078. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7079. u32 val = 0;
  7080. switch (intel_crtc->config->pipe_bpp) {
  7081. case 18:
  7082. val |= PIPEMISC_DITHER_6_BPC;
  7083. break;
  7084. case 24:
  7085. val |= PIPEMISC_DITHER_8_BPC;
  7086. break;
  7087. case 30:
  7088. val |= PIPEMISC_DITHER_10_BPC;
  7089. break;
  7090. case 36:
  7091. val |= PIPEMISC_DITHER_12_BPC;
  7092. break;
  7093. default:
  7094. /* Case prevented by pipe_config_set_bpp. */
  7095. BUG();
  7096. }
  7097. if (intel_crtc->config->dither)
  7098. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7099. if (config->ycbcr420) {
  7100. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  7101. PIPEMISC_YUV420_ENABLE |
  7102. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7103. }
  7104. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7105. }
  7106. }
  7107. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7108. {
  7109. /*
  7110. * Account for spread spectrum to avoid
  7111. * oversubscribing the link. Max center spread
  7112. * is 2.5%; use 5% for safety's sake.
  7113. */
  7114. u32 bps = target_clock * bpp * 21 / 20;
  7115. return DIV_ROUND_UP(bps, link_bw * 8);
  7116. }
  7117. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7118. {
  7119. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7120. }
  7121. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7122. struct intel_crtc_state *crtc_state,
  7123. struct dpll *reduced_clock)
  7124. {
  7125. struct drm_crtc *crtc = &intel_crtc->base;
  7126. struct drm_device *dev = crtc->dev;
  7127. struct drm_i915_private *dev_priv = to_i915(dev);
  7128. u32 dpll, fp, fp2;
  7129. int factor;
  7130. /* Enable autotuning of the PLL clock (if permissible) */
  7131. factor = 21;
  7132. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7133. if ((intel_panel_use_ssc(dev_priv) &&
  7134. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7135. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7136. factor = 25;
  7137. } else if (crtc_state->sdvo_tv_clock)
  7138. factor = 20;
  7139. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7140. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7141. fp |= FP_CB_TUNE;
  7142. if (reduced_clock) {
  7143. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7144. if (reduced_clock->m < factor * reduced_clock->n)
  7145. fp2 |= FP_CB_TUNE;
  7146. } else {
  7147. fp2 = fp;
  7148. }
  7149. dpll = 0;
  7150. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7151. dpll |= DPLLB_MODE_LVDS;
  7152. else
  7153. dpll |= DPLLB_MODE_DAC_SERIAL;
  7154. dpll |= (crtc_state->pixel_multiplier - 1)
  7155. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7156. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7157. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7158. dpll |= DPLL_SDVO_HIGH_SPEED;
  7159. if (intel_crtc_has_dp_encoder(crtc_state))
  7160. dpll |= DPLL_SDVO_HIGH_SPEED;
  7161. /*
  7162. * The high speed IO clock is only really required for
  7163. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7164. * possible to share the DPLL between CRT and HDMI. Enabling
  7165. * the clock needlessly does no real harm, except use up a
  7166. * bit of power potentially.
  7167. *
  7168. * We'll limit this to IVB with 3 pipes, since it has only two
  7169. * DPLLs and so DPLL sharing is the only way to get three pipes
  7170. * driving PCH ports at the same time. On SNB we could do this,
  7171. * and potentially avoid enabling the second DPLL, but it's not
  7172. * clear if it''s a win or loss power wise. No point in doing
  7173. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7174. */
  7175. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7176. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7177. dpll |= DPLL_SDVO_HIGH_SPEED;
  7178. /* compute bitmask from p1 value */
  7179. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7180. /* also FPA1 */
  7181. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7182. switch (crtc_state->dpll.p2) {
  7183. case 5:
  7184. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7185. break;
  7186. case 7:
  7187. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7188. break;
  7189. case 10:
  7190. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7191. break;
  7192. case 14:
  7193. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7194. break;
  7195. }
  7196. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7197. intel_panel_use_ssc(dev_priv))
  7198. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7199. else
  7200. dpll |= PLL_REF_INPUT_DREFCLK;
  7201. dpll |= DPLL_VCO_ENABLE;
  7202. crtc_state->dpll_hw_state.dpll = dpll;
  7203. crtc_state->dpll_hw_state.fp0 = fp;
  7204. crtc_state->dpll_hw_state.fp1 = fp2;
  7205. }
  7206. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7207. struct intel_crtc_state *crtc_state)
  7208. {
  7209. struct drm_device *dev = crtc->base.dev;
  7210. struct drm_i915_private *dev_priv = to_i915(dev);
  7211. const struct intel_limit *limit;
  7212. int refclk = 120000;
  7213. memset(&crtc_state->dpll_hw_state, 0,
  7214. sizeof(crtc_state->dpll_hw_state));
  7215. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7216. if (!crtc_state->has_pch_encoder)
  7217. return 0;
  7218. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7219. if (intel_panel_use_ssc(dev_priv)) {
  7220. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7221. dev_priv->vbt.lvds_ssc_freq);
  7222. refclk = dev_priv->vbt.lvds_ssc_freq;
  7223. }
  7224. if (intel_is_dual_link_lvds(dev)) {
  7225. if (refclk == 100000)
  7226. limit = &intel_limits_ironlake_dual_lvds_100m;
  7227. else
  7228. limit = &intel_limits_ironlake_dual_lvds;
  7229. } else {
  7230. if (refclk == 100000)
  7231. limit = &intel_limits_ironlake_single_lvds_100m;
  7232. else
  7233. limit = &intel_limits_ironlake_single_lvds;
  7234. }
  7235. } else {
  7236. limit = &intel_limits_ironlake_dac;
  7237. }
  7238. if (!crtc_state->clock_set &&
  7239. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7240. refclk, NULL, &crtc_state->dpll)) {
  7241. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7242. return -EINVAL;
  7243. }
  7244. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7245. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7246. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7247. pipe_name(crtc->pipe));
  7248. return -EINVAL;
  7249. }
  7250. return 0;
  7251. }
  7252. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7253. struct intel_link_m_n *m_n)
  7254. {
  7255. struct drm_device *dev = crtc->base.dev;
  7256. struct drm_i915_private *dev_priv = to_i915(dev);
  7257. enum pipe pipe = crtc->pipe;
  7258. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7259. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7260. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7261. & ~TU_SIZE_MASK;
  7262. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7263. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7264. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7265. }
  7266. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7267. enum transcoder transcoder,
  7268. struct intel_link_m_n *m_n,
  7269. struct intel_link_m_n *m2_n2)
  7270. {
  7271. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7272. enum pipe pipe = crtc->pipe;
  7273. if (INTEL_GEN(dev_priv) >= 5) {
  7274. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7275. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7276. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7277. & ~TU_SIZE_MASK;
  7278. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7279. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7280. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7281. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7282. * gen < 8) and if DRRS is supported (to make sure the
  7283. * registers are not unnecessarily read).
  7284. */
  7285. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7286. crtc->config->has_drrs) {
  7287. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7288. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7289. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7290. & ~TU_SIZE_MASK;
  7291. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7292. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7293. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7294. }
  7295. } else {
  7296. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7297. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7298. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7299. & ~TU_SIZE_MASK;
  7300. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7301. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7302. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7303. }
  7304. }
  7305. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7306. struct intel_crtc_state *pipe_config)
  7307. {
  7308. if (pipe_config->has_pch_encoder)
  7309. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7310. else
  7311. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7312. &pipe_config->dp_m_n,
  7313. &pipe_config->dp_m2_n2);
  7314. }
  7315. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7316. struct intel_crtc_state *pipe_config)
  7317. {
  7318. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7319. &pipe_config->fdi_m_n, NULL);
  7320. }
  7321. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7322. struct intel_crtc_state *pipe_config)
  7323. {
  7324. struct drm_device *dev = crtc->base.dev;
  7325. struct drm_i915_private *dev_priv = to_i915(dev);
  7326. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7327. uint32_t ps_ctrl = 0;
  7328. int id = -1;
  7329. int i;
  7330. /* find scaler attached to this pipe */
  7331. for (i = 0; i < crtc->num_scalers; i++) {
  7332. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7333. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7334. id = i;
  7335. pipe_config->pch_pfit.enabled = true;
  7336. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7337. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7338. break;
  7339. }
  7340. }
  7341. scaler_state->scaler_id = id;
  7342. if (id >= 0) {
  7343. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7344. } else {
  7345. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7346. }
  7347. }
  7348. static void
  7349. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7350. struct intel_initial_plane_config *plane_config)
  7351. {
  7352. struct drm_device *dev = crtc->base.dev;
  7353. struct drm_i915_private *dev_priv = to_i915(dev);
  7354. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7355. enum plane_id plane_id = plane->id;
  7356. enum pipe pipe;
  7357. u32 val, base, offset, stride_mult, tiling, alpha;
  7358. int fourcc, pixel_format;
  7359. unsigned int aligned_height;
  7360. struct drm_framebuffer *fb;
  7361. struct intel_framebuffer *intel_fb;
  7362. if (!plane->get_hw_state(plane, &pipe))
  7363. return;
  7364. WARN_ON(pipe != crtc->pipe);
  7365. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7366. if (!intel_fb) {
  7367. DRM_DEBUG_KMS("failed to alloc fb\n");
  7368. return;
  7369. }
  7370. fb = &intel_fb->base;
  7371. fb->dev = dev;
  7372. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7373. if (INTEL_GEN(dev_priv) >= 11)
  7374. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7375. else
  7376. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7377. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7378. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7379. alpha &= PLANE_COLOR_ALPHA_MASK;
  7380. } else {
  7381. alpha = val & PLANE_CTL_ALPHA_MASK;
  7382. }
  7383. fourcc = skl_format_to_fourcc(pixel_format,
  7384. val & PLANE_CTL_ORDER_RGBX, alpha);
  7385. fb->format = drm_format_info(fourcc);
  7386. tiling = val & PLANE_CTL_TILED_MASK;
  7387. switch (tiling) {
  7388. case PLANE_CTL_TILED_LINEAR:
  7389. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7390. break;
  7391. case PLANE_CTL_TILED_X:
  7392. plane_config->tiling = I915_TILING_X;
  7393. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7394. break;
  7395. case PLANE_CTL_TILED_Y:
  7396. plane_config->tiling = I915_TILING_Y;
  7397. if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
  7398. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7399. else
  7400. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7401. break;
  7402. case PLANE_CTL_TILED_YF:
  7403. if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
  7404. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7405. else
  7406. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7407. break;
  7408. default:
  7409. MISSING_CASE(tiling);
  7410. goto error;
  7411. }
  7412. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7413. plane_config->base = base;
  7414. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7415. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7416. fb->height = ((val >> 16) & 0xfff) + 1;
  7417. fb->width = ((val >> 0) & 0x1fff) + 1;
  7418. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7419. stride_mult = intel_fb_stride_alignment(fb, 0);
  7420. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7421. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7422. plane_config->size = fb->pitches[0] * aligned_height;
  7423. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7424. crtc->base.name, plane->base.name, fb->width, fb->height,
  7425. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7426. plane_config->size);
  7427. plane_config->fb = intel_fb;
  7428. return;
  7429. error:
  7430. kfree(intel_fb);
  7431. }
  7432. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7433. struct intel_crtc_state *pipe_config)
  7434. {
  7435. struct drm_device *dev = crtc->base.dev;
  7436. struct drm_i915_private *dev_priv = to_i915(dev);
  7437. uint32_t tmp;
  7438. tmp = I915_READ(PF_CTL(crtc->pipe));
  7439. if (tmp & PF_ENABLE) {
  7440. pipe_config->pch_pfit.enabled = true;
  7441. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7442. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7443. /* We currently do not free assignements of panel fitters on
  7444. * ivb/hsw (since we don't use the higher upscaling modes which
  7445. * differentiates them) so just WARN about this case for now. */
  7446. if (IS_GEN7(dev_priv)) {
  7447. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7448. PF_PIPE_SEL_IVB(crtc->pipe));
  7449. }
  7450. }
  7451. }
  7452. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7453. struct intel_crtc_state *pipe_config)
  7454. {
  7455. struct drm_device *dev = crtc->base.dev;
  7456. struct drm_i915_private *dev_priv = to_i915(dev);
  7457. enum intel_display_power_domain power_domain;
  7458. uint32_t tmp;
  7459. bool ret;
  7460. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7461. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7462. return false;
  7463. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7464. pipe_config->shared_dpll = NULL;
  7465. ret = false;
  7466. tmp = I915_READ(PIPECONF(crtc->pipe));
  7467. if (!(tmp & PIPECONF_ENABLE))
  7468. goto out;
  7469. switch (tmp & PIPECONF_BPC_MASK) {
  7470. case PIPECONF_6BPC:
  7471. pipe_config->pipe_bpp = 18;
  7472. break;
  7473. case PIPECONF_8BPC:
  7474. pipe_config->pipe_bpp = 24;
  7475. break;
  7476. case PIPECONF_10BPC:
  7477. pipe_config->pipe_bpp = 30;
  7478. break;
  7479. case PIPECONF_12BPC:
  7480. pipe_config->pipe_bpp = 36;
  7481. break;
  7482. default:
  7483. break;
  7484. }
  7485. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7486. pipe_config->limited_color_range = true;
  7487. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7488. struct intel_shared_dpll *pll;
  7489. enum intel_dpll_id pll_id;
  7490. pipe_config->has_pch_encoder = true;
  7491. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7492. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7493. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7494. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7495. if (HAS_PCH_IBX(dev_priv)) {
  7496. /*
  7497. * The pipe->pch transcoder and pch transcoder->pll
  7498. * mapping is fixed.
  7499. */
  7500. pll_id = (enum intel_dpll_id) crtc->pipe;
  7501. } else {
  7502. tmp = I915_READ(PCH_DPLL_SEL);
  7503. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7504. pll_id = DPLL_ID_PCH_PLL_B;
  7505. else
  7506. pll_id= DPLL_ID_PCH_PLL_A;
  7507. }
  7508. pipe_config->shared_dpll =
  7509. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7510. pll = pipe_config->shared_dpll;
  7511. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7512. &pipe_config->dpll_hw_state));
  7513. tmp = pipe_config->dpll_hw_state.dpll;
  7514. pipe_config->pixel_multiplier =
  7515. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7516. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7517. ironlake_pch_clock_get(crtc, pipe_config);
  7518. } else {
  7519. pipe_config->pixel_multiplier = 1;
  7520. }
  7521. intel_get_pipe_timings(crtc, pipe_config);
  7522. intel_get_pipe_src_size(crtc, pipe_config);
  7523. ironlake_get_pfit_config(crtc, pipe_config);
  7524. ret = true;
  7525. out:
  7526. intel_display_power_put(dev_priv, power_domain);
  7527. return ret;
  7528. }
  7529. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7530. {
  7531. struct drm_device *dev = &dev_priv->drm;
  7532. struct intel_crtc *crtc;
  7533. for_each_intel_crtc(dev, crtc)
  7534. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7535. pipe_name(crtc->pipe));
  7536. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7537. "Display power well on\n");
  7538. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7539. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7540. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7541. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7542. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7543. "CPU PWM1 enabled\n");
  7544. if (IS_HASWELL(dev_priv))
  7545. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7546. "CPU PWM2 enabled\n");
  7547. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7548. "PCH PWM1 enabled\n");
  7549. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7550. "Utility pin enabled\n");
  7551. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7552. /*
  7553. * In theory we can still leave IRQs enabled, as long as only the HPD
  7554. * interrupts remain enabled. We used to check for that, but since it's
  7555. * gen-specific and since we only disable LCPLL after we fully disable
  7556. * the interrupts, the check below should be enough.
  7557. */
  7558. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7559. }
  7560. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7561. {
  7562. if (IS_HASWELL(dev_priv))
  7563. return I915_READ(D_COMP_HSW);
  7564. else
  7565. return I915_READ(D_COMP_BDW);
  7566. }
  7567. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7568. {
  7569. if (IS_HASWELL(dev_priv)) {
  7570. mutex_lock(&dev_priv->pcu_lock);
  7571. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7572. val))
  7573. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7574. mutex_unlock(&dev_priv->pcu_lock);
  7575. } else {
  7576. I915_WRITE(D_COMP_BDW, val);
  7577. POSTING_READ(D_COMP_BDW);
  7578. }
  7579. }
  7580. /*
  7581. * This function implements pieces of two sequences from BSpec:
  7582. * - Sequence for display software to disable LCPLL
  7583. * - Sequence for display software to allow package C8+
  7584. * The steps implemented here are just the steps that actually touch the LCPLL
  7585. * register. Callers should take care of disabling all the display engine
  7586. * functions, doing the mode unset, fixing interrupts, etc.
  7587. */
  7588. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7589. bool switch_to_fclk, bool allow_power_down)
  7590. {
  7591. uint32_t val;
  7592. assert_can_disable_lcpll(dev_priv);
  7593. val = I915_READ(LCPLL_CTL);
  7594. if (switch_to_fclk) {
  7595. val |= LCPLL_CD_SOURCE_FCLK;
  7596. I915_WRITE(LCPLL_CTL, val);
  7597. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7598. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7599. DRM_ERROR("Switching to FCLK failed\n");
  7600. val = I915_READ(LCPLL_CTL);
  7601. }
  7602. val |= LCPLL_PLL_DISABLE;
  7603. I915_WRITE(LCPLL_CTL, val);
  7604. POSTING_READ(LCPLL_CTL);
  7605. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7606. DRM_ERROR("LCPLL still locked\n");
  7607. val = hsw_read_dcomp(dev_priv);
  7608. val |= D_COMP_COMP_DISABLE;
  7609. hsw_write_dcomp(dev_priv, val);
  7610. ndelay(100);
  7611. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7612. 1))
  7613. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7614. if (allow_power_down) {
  7615. val = I915_READ(LCPLL_CTL);
  7616. val |= LCPLL_POWER_DOWN_ALLOW;
  7617. I915_WRITE(LCPLL_CTL, val);
  7618. POSTING_READ(LCPLL_CTL);
  7619. }
  7620. }
  7621. /*
  7622. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7623. * source.
  7624. */
  7625. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7626. {
  7627. uint32_t val;
  7628. val = I915_READ(LCPLL_CTL);
  7629. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7630. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7631. return;
  7632. /*
  7633. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7634. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7635. */
  7636. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7637. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7638. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7639. I915_WRITE(LCPLL_CTL, val);
  7640. POSTING_READ(LCPLL_CTL);
  7641. }
  7642. val = hsw_read_dcomp(dev_priv);
  7643. val |= D_COMP_COMP_FORCE;
  7644. val &= ~D_COMP_COMP_DISABLE;
  7645. hsw_write_dcomp(dev_priv, val);
  7646. val = I915_READ(LCPLL_CTL);
  7647. val &= ~LCPLL_PLL_DISABLE;
  7648. I915_WRITE(LCPLL_CTL, val);
  7649. if (intel_wait_for_register(dev_priv,
  7650. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7651. 5))
  7652. DRM_ERROR("LCPLL not locked yet\n");
  7653. if (val & LCPLL_CD_SOURCE_FCLK) {
  7654. val = I915_READ(LCPLL_CTL);
  7655. val &= ~LCPLL_CD_SOURCE_FCLK;
  7656. I915_WRITE(LCPLL_CTL, val);
  7657. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7658. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7659. DRM_ERROR("Switching back to LCPLL failed\n");
  7660. }
  7661. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7662. intel_update_cdclk(dev_priv);
  7663. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7664. }
  7665. /*
  7666. * Package states C8 and deeper are really deep PC states that can only be
  7667. * reached when all the devices on the system allow it, so even if the graphics
  7668. * device allows PC8+, it doesn't mean the system will actually get to these
  7669. * states. Our driver only allows PC8+ when going into runtime PM.
  7670. *
  7671. * The requirements for PC8+ are that all the outputs are disabled, the power
  7672. * well is disabled and most interrupts are disabled, and these are also
  7673. * requirements for runtime PM. When these conditions are met, we manually do
  7674. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7675. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7676. * hang the machine.
  7677. *
  7678. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7679. * the state of some registers, so when we come back from PC8+ we need to
  7680. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7681. * need to take care of the registers kept by RC6. Notice that this happens even
  7682. * if we don't put the device in PCI D3 state (which is what currently happens
  7683. * because of the runtime PM support).
  7684. *
  7685. * For more, read "Display Sequences for Package C8" on the hardware
  7686. * documentation.
  7687. */
  7688. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7689. {
  7690. uint32_t val;
  7691. DRM_DEBUG_KMS("Enabling package C8+\n");
  7692. if (HAS_PCH_LPT_LP(dev_priv)) {
  7693. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7694. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7695. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7696. }
  7697. lpt_disable_clkout_dp(dev_priv);
  7698. hsw_disable_lcpll(dev_priv, true, true);
  7699. }
  7700. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7701. {
  7702. uint32_t val;
  7703. DRM_DEBUG_KMS("Disabling package C8+\n");
  7704. hsw_restore_lcpll(dev_priv);
  7705. lpt_init_pch_refclk(dev_priv);
  7706. if (HAS_PCH_LPT_LP(dev_priv)) {
  7707. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7708. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7709. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7710. }
  7711. }
  7712. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7713. struct intel_crtc_state *crtc_state)
  7714. {
  7715. struct intel_atomic_state *state =
  7716. to_intel_atomic_state(crtc_state->base.state);
  7717. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7718. struct intel_encoder *encoder =
  7719. intel_get_crtc_new_encoder(state, crtc_state);
  7720. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7721. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7722. pipe_name(crtc->pipe));
  7723. return -EINVAL;
  7724. }
  7725. }
  7726. return 0;
  7727. }
  7728. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7729. enum port port,
  7730. struct intel_crtc_state *pipe_config)
  7731. {
  7732. enum intel_dpll_id id;
  7733. u32 temp;
  7734. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7735. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7736. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7737. return;
  7738. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7739. }
  7740. static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7741. enum port port,
  7742. struct intel_crtc_state *pipe_config)
  7743. {
  7744. enum intel_dpll_id id;
  7745. u32 temp;
  7746. /* TODO: TBT pll not implemented. */
  7747. switch (port) {
  7748. case PORT_A:
  7749. case PORT_B:
  7750. temp = I915_READ(DPCLKA_CFGCR0_ICL) &
  7751. DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7752. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7753. if (WARN_ON(id != DPLL_ID_ICL_DPLL0 && id != DPLL_ID_ICL_DPLL1))
  7754. return;
  7755. break;
  7756. case PORT_C:
  7757. id = DPLL_ID_ICL_MGPLL1;
  7758. break;
  7759. case PORT_D:
  7760. id = DPLL_ID_ICL_MGPLL2;
  7761. break;
  7762. case PORT_E:
  7763. id = DPLL_ID_ICL_MGPLL3;
  7764. break;
  7765. case PORT_F:
  7766. id = DPLL_ID_ICL_MGPLL4;
  7767. break;
  7768. default:
  7769. MISSING_CASE(port);
  7770. return;
  7771. }
  7772. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7773. }
  7774. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7775. enum port port,
  7776. struct intel_crtc_state *pipe_config)
  7777. {
  7778. enum intel_dpll_id id;
  7779. switch (port) {
  7780. case PORT_A:
  7781. id = DPLL_ID_SKL_DPLL0;
  7782. break;
  7783. case PORT_B:
  7784. id = DPLL_ID_SKL_DPLL1;
  7785. break;
  7786. case PORT_C:
  7787. id = DPLL_ID_SKL_DPLL2;
  7788. break;
  7789. default:
  7790. DRM_ERROR("Incorrect port type\n");
  7791. return;
  7792. }
  7793. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7794. }
  7795. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7796. enum port port,
  7797. struct intel_crtc_state *pipe_config)
  7798. {
  7799. enum intel_dpll_id id;
  7800. u32 temp;
  7801. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7802. id = temp >> (port * 3 + 1);
  7803. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7804. return;
  7805. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7806. }
  7807. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7808. enum port port,
  7809. struct intel_crtc_state *pipe_config)
  7810. {
  7811. enum intel_dpll_id id;
  7812. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7813. switch (ddi_pll_sel) {
  7814. case PORT_CLK_SEL_WRPLL1:
  7815. id = DPLL_ID_WRPLL1;
  7816. break;
  7817. case PORT_CLK_SEL_WRPLL2:
  7818. id = DPLL_ID_WRPLL2;
  7819. break;
  7820. case PORT_CLK_SEL_SPLL:
  7821. id = DPLL_ID_SPLL;
  7822. break;
  7823. case PORT_CLK_SEL_LCPLL_810:
  7824. id = DPLL_ID_LCPLL_810;
  7825. break;
  7826. case PORT_CLK_SEL_LCPLL_1350:
  7827. id = DPLL_ID_LCPLL_1350;
  7828. break;
  7829. case PORT_CLK_SEL_LCPLL_2700:
  7830. id = DPLL_ID_LCPLL_2700;
  7831. break;
  7832. default:
  7833. MISSING_CASE(ddi_pll_sel);
  7834. /* fall through */
  7835. case PORT_CLK_SEL_NONE:
  7836. return;
  7837. }
  7838. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7839. }
  7840. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7841. struct intel_crtc_state *pipe_config,
  7842. u64 *power_domain_mask)
  7843. {
  7844. struct drm_device *dev = crtc->base.dev;
  7845. struct drm_i915_private *dev_priv = to_i915(dev);
  7846. enum intel_display_power_domain power_domain;
  7847. u32 tmp;
  7848. /*
  7849. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7850. * transcoder handled below.
  7851. */
  7852. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7853. /*
  7854. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7855. * consistency and less surprising code; it's in always on power).
  7856. */
  7857. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7858. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7859. enum pipe trans_edp_pipe;
  7860. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7861. default:
  7862. WARN(1, "unknown pipe linked to edp transcoder\n");
  7863. /* fall through */
  7864. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7865. case TRANS_DDI_EDP_INPUT_A_ON:
  7866. trans_edp_pipe = PIPE_A;
  7867. break;
  7868. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7869. trans_edp_pipe = PIPE_B;
  7870. break;
  7871. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7872. trans_edp_pipe = PIPE_C;
  7873. break;
  7874. }
  7875. if (trans_edp_pipe == crtc->pipe)
  7876. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7877. }
  7878. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7879. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7880. return false;
  7881. *power_domain_mask |= BIT_ULL(power_domain);
  7882. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7883. return tmp & PIPECONF_ENABLE;
  7884. }
  7885. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7886. struct intel_crtc_state *pipe_config,
  7887. u64 *power_domain_mask)
  7888. {
  7889. struct drm_device *dev = crtc->base.dev;
  7890. struct drm_i915_private *dev_priv = to_i915(dev);
  7891. enum intel_display_power_domain power_domain;
  7892. enum port port;
  7893. enum transcoder cpu_transcoder;
  7894. u32 tmp;
  7895. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7896. if (port == PORT_A)
  7897. cpu_transcoder = TRANSCODER_DSI_A;
  7898. else
  7899. cpu_transcoder = TRANSCODER_DSI_C;
  7900. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7901. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7902. continue;
  7903. *power_domain_mask |= BIT_ULL(power_domain);
  7904. /*
  7905. * The PLL needs to be enabled with a valid divider
  7906. * configuration, otherwise accessing DSI registers will hang
  7907. * the machine. See BSpec North Display Engine
  7908. * registers/MIPI[BXT]. We can break out here early, since we
  7909. * need the same DSI PLL to be enabled for both DSI ports.
  7910. */
  7911. if (!bxt_dsi_pll_is_enabled(dev_priv))
  7912. break;
  7913. /* XXX: this works for video mode only */
  7914. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7915. if (!(tmp & DPI_ENABLE))
  7916. continue;
  7917. tmp = I915_READ(MIPI_CTRL(port));
  7918. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7919. continue;
  7920. pipe_config->cpu_transcoder = cpu_transcoder;
  7921. break;
  7922. }
  7923. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7924. }
  7925. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7926. struct intel_crtc_state *pipe_config)
  7927. {
  7928. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7929. struct intel_shared_dpll *pll;
  7930. enum port port;
  7931. uint32_t tmp;
  7932. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7933. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7934. if (IS_ICELAKE(dev_priv))
  7935. icelake_get_ddi_pll(dev_priv, port, pipe_config);
  7936. else if (IS_CANNONLAKE(dev_priv))
  7937. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7938. else if (IS_GEN9_BC(dev_priv))
  7939. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7940. else if (IS_GEN9_LP(dev_priv))
  7941. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7942. else
  7943. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7944. pll = pipe_config->shared_dpll;
  7945. if (pll) {
  7946. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7947. &pipe_config->dpll_hw_state));
  7948. }
  7949. /*
  7950. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7951. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7952. * the PCH transcoder is on.
  7953. */
  7954. if (INTEL_GEN(dev_priv) < 9 &&
  7955. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7956. pipe_config->has_pch_encoder = true;
  7957. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7958. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7959. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7960. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7961. }
  7962. }
  7963. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7964. struct intel_crtc_state *pipe_config)
  7965. {
  7966. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7967. enum intel_display_power_domain power_domain;
  7968. u64 power_domain_mask;
  7969. bool active;
  7970. intel_crtc_init_scalers(crtc, pipe_config);
  7971. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7972. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7973. return false;
  7974. power_domain_mask = BIT_ULL(power_domain);
  7975. pipe_config->shared_dpll = NULL;
  7976. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7977. if (IS_GEN9_LP(dev_priv) &&
  7978. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7979. WARN_ON(active);
  7980. active = true;
  7981. }
  7982. if (!active)
  7983. goto out;
  7984. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7985. haswell_get_ddi_port_state(crtc, pipe_config);
  7986. intel_get_pipe_timings(crtc, pipe_config);
  7987. }
  7988. intel_get_pipe_src_size(crtc, pipe_config);
  7989. pipe_config->gamma_mode =
  7990. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7991. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7992. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7993. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7994. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7995. bool blend_mode_420 = tmp &
  7996. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7997. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7998. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7999. pipe_config->ycbcr420 != blend_mode_420)
  8000. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  8001. } else if (clrspace_yuv) {
  8002. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  8003. }
  8004. }
  8005. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8006. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8007. power_domain_mask |= BIT_ULL(power_domain);
  8008. if (INTEL_GEN(dev_priv) >= 9)
  8009. skylake_get_pfit_config(crtc, pipe_config);
  8010. else
  8011. ironlake_get_pfit_config(crtc, pipe_config);
  8012. }
  8013. if (hsw_crtc_supports_ips(crtc)) {
  8014. if (IS_HASWELL(dev_priv))
  8015. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  8016. else {
  8017. /*
  8018. * We cannot readout IPS state on broadwell, set to
  8019. * true so we can set it to a defined state on first
  8020. * commit.
  8021. */
  8022. pipe_config->ips_enabled = true;
  8023. }
  8024. }
  8025. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8026. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8027. pipe_config->pixel_multiplier =
  8028. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8029. } else {
  8030. pipe_config->pixel_multiplier = 1;
  8031. }
  8032. out:
  8033. for_each_power_domain(power_domain, power_domain_mask)
  8034. intel_display_power_put(dev_priv, power_domain);
  8035. return active;
  8036. }
  8037. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  8038. {
  8039. struct drm_i915_private *dev_priv =
  8040. to_i915(plane_state->base.plane->dev);
  8041. const struct drm_framebuffer *fb = plane_state->base.fb;
  8042. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8043. u32 base;
  8044. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  8045. base = obj->phys_handle->busaddr;
  8046. else
  8047. base = intel_plane_ggtt_offset(plane_state);
  8048. base += plane_state->main.offset;
  8049. /* ILK+ do this automagically */
  8050. if (HAS_GMCH_DISPLAY(dev_priv) &&
  8051. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8052. base += (plane_state->base.crtc_h *
  8053. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  8054. return base;
  8055. }
  8056. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  8057. {
  8058. int x = plane_state->base.crtc_x;
  8059. int y = plane_state->base.crtc_y;
  8060. u32 pos = 0;
  8061. if (x < 0) {
  8062. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8063. x = -x;
  8064. }
  8065. pos |= x << CURSOR_X_SHIFT;
  8066. if (y < 0) {
  8067. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8068. y = -y;
  8069. }
  8070. pos |= y << CURSOR_Y_SHIFT;
  8071. return pos;
  8072. }
  8073. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  8074. {
  8075. const struct drm_mode_config *config =
  8076. &plane_state->base.plane->dev->mode_config;
  8077. int width = plane_state->base.crtc_w;
  8078. int height = plane_state->base.crtc_h;
  8079. return width > 0 && width <= config->cursor_width &&
  8080. height > 0 && height <= config->cursor_height;
  8081. }
  8082. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  8083. struct intel_plane_state *plane_state)
  8084. {
  8085. const struct drm_framebuffer *fb = plane_state->base.fb;
  8086. int src_x, src_y;
  8087. u32 offset;
  8088. int ret;
  8089. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  8090. &crtc_state->base,
  8091. DRM_PLANE_HELPER_NO_SCALING,
  8092. DRM_PLANE_HELPER_NO_SCALING,
  8093. true, true);
  8094. if (ret)
  8095. return ret;
  8096. if (!fb)
  8097. return 0;
  8098. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  8099. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  8100. return -EINVAL;
  8101. }
  8102. src_x = plane_state->base.src_x >> 16;
  8103. src_y = plane_state->base.src_y >> 16;
  8104. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  8105. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  8106. if (src_x != 0 || src_y != 0) {
  8107. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  8108. return -EINVAL;
  8109. }
  8110. plane_state->main.offset = offset;
  8111. return 0;
  8112. }
  8113. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8114. const struct intel_plane_state *plane_state)
  8115. {
  8116. const struct drm_framebuffer *fb = plane_state->base.fb;
  8117. return CURSOR_ENABLE |
  8118. CURSOR_GAMMA_ENABLE |
  8119. CURSOR_FORMAT_ARGB |
  8120. CURSOR_STRIDE(fb->pitches[0]);
  8121. }
  8122. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  8123. {
  8124. int width = plane_state->base.crtc_w;
  8125. /*
  8126. * 845g/865g are only limited by the width of their cursors,
  8127. * the height is arbitrary up to the precision of the register.
  8128. */
  8129. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  8130. }
  8131. static int i845_check_cursor(struct intel_plane *plane,
  8132. struct intel_crtc_state *crtc_state,
  8133. struct intel_plane_state *plane_state)
  8134. {
  8135. const struct drm_framebuffer *fb = plane_state->base.fb;
  8136. int ret;
  8137. ret = intel_check_cursor(crtc_state, plane_state);
  8138. if (ret)
  8139. return ret;
  8140. /* if we want to turn off the cursor ignore width and height */
  8141. if (!fb)
  8142. return 0;
  8143. /* Check for which cursor types we support */
  8144. if (!i845_cursor_size_ok(plane_state)) {
  8145. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8146. plane_state->base.crtc_w,
  8147. plane_state->base.crtc_h);
  8148. return -EINVAL;
  8149. }
  8150. switch (fb->pitches[0]) {
  8151. case 256:
  8152. case 512:
  8153. case 1024:
  8154. case 2048:
  8155. break;
  8156. default:
  8157. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  8158. fb->pitches[0]);
  8159. return -EINVAL;
  8160. }
  8161. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  8162. return 0;
  8163. }
  8164. static void i845_update_cursor(struct intel_plane *plane,
  8165. const struct intel_crtc_state *crtc_state,
  8166. const struct intel_plane_state *plane_state)
  8167. {
  8168. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8169. u32 cntl = 0, base = 0, pos = 0, size = 0;
  8170. unsigned long irqflags;
  8171. if (plane_state && plane_state->base.visible) {
  8172. unsigned int width = plane_state->base.crtc_w;
  8173. unsigned int height = plane_state->base.crtc_h;
  8174. cntl = plane_state->ctl;
  8175. size = (height << 12) | width;
  8176. base = intel_cursor_base(plane_state);
  8177. pos = intel_cursor_position(plane_state);
  8178. }
  8179. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8180. /* On these chipsets we can only modify the base/size/stride
  8181. * whilst the cursor is disabled.
  8182. */
  8183. if (plane->cursor.base != base ||
  8184. plane->cursor.size != size ||
  8185. plane->cursor.cntl != cntl) {
  8186. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  8187. I915_WRITE_FW(CURBASE(PIPE_A), base);
  8188. I915_WRITE_FW(CURSIZE, size);
  8189. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8190. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  8191. plane->cursor.base = base;
  8192. plane->cursor.size = size;
  8193. plane->cursor.cntl = cntl;
  8194. } else {
  8195. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8196. }
  8197. POSTING_READ_FW(CURCNTR(PIPE_A));
  8198. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8199. }
  8200. static void i845_disable_cursor(struct intel_plane *plane,
  8201. struct intel_crtc *crtc)
  8202. {
  8203. i845_update_cursor(plane, NULL, NULL);
  8204. }
  8205. static bool i845_cursor_get_hw_state(struct intel_plane *plane,
  8206. enum pipe *pipe)
  8207. {
  8208. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8209. enum intel_display_power_domain power_domain;
  8210. bool ret;
  8211. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  8212. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8213. return false;
  8214. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  8215. *pipe = PIPE_A;
  8216. intel_display_power_put(dev_priv, power_domain);
  8217. return ret;
  8218. }
  8219. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8220. const struct intel_plane_state *plane_state)
  8221. {
  8222. struct drm_i915_private *dev_priv =
  8223. to_i915(plane_state->base.plane->dev);
  8224. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8225. u32 cntl = 0;
  8226. if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  8227. cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
  8228. if (INTEL_GEN(dev_priv) <= 10) {
  8229. cntl |= MCURSOR_GAMMA_ENABLE;
  8230. if (HAS_DDI(dev_priv))
  8231. cntl |= MCURSOR_PIPE_CSC_ENABLE;
  8232. }
  8233. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8234. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8235. switch (plane_state->base.crtc_w) {
  8236. case 64:
  8237. cntl |= MCURSOR_MODE_64_ARGB_AX;
  8238. break;
  8239. case 128:
  8240. cntl |= MCURSOR_MODE_128_ARGB_AX;
  8241. break;
  8242. case 256:
  8243. cntl |= MCURSOR_MODE_256_ARGB_AX;
  8244. break;
  8245. default:
  8246. MISSING_CASE(plane_state->base.crtc_w);
  8247. return 0;
  8248. }
  8249. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8250. cntl |= MCURSOR_ROTATE_180;
  8251. return cntl;
  8252. }
  8253. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8254. {
  8255. struct drm_i915_private *dev_priv =
  8256. to_i915(plane_state->base.plane->dev);
  8257. int width = plane_state->base.crtc_w;
  8258. int height = plane_state->base.crtc_h;
  8259. if (!intel_cursor_size_ok(plane_state))
  8260. return false;
  8261. /* Cursor width is limited to a few power-of-two sizes */
  8262. switch (width) {
  8263. case 256:
  8264. case 128:
  8265. case 64:
  8266. break;
  8267. default:
  8268. return false;
  8269. }
  8270. /*
  8271. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8272. * height from 8 lines up to the cursor width, when the
  8273. * cursor is not rotated. Everything else requires square
  8274. * cursors.
  8275. */
  8276. if (HAS_CUR_FBC(dev_priv) &&
  8277. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8278. if (height < 8 || height > width)
  8279. return false;
  8280. } else {
  8281. if (height != width)
  8282. return false;
  8283. }
  8284. return true;
  8285. }
  8286. static int i9xx_check_cursor(struct intel_plane *plane,
  8287. struct intel_crtc_state *crtc_state,
  8288. struct intel_plane_state *plane_state)
  8289. {
  8290. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8291. const struct drm_framebuffer *fb = plane_state->base.fb;
  8292. enum pipe pipe = plane->pipe;
  8293. int ret;
  8294. ret = intel_check_cursor(crtc_state, plane_state);
  8295. if (ret)
  8296. return ret;
  8297. /* if we want to turn off the cursor ignore width and height */
  8298. if (!fb)
  8299. return 0;
  8300. /* Check for which cursor types we support */
  8301. if (!i9xx_cursor_size_ok(plane_state)) {
  8302. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8303. plane_state->base.crtc_w,
  8304. plane_state->base.crtc_h);
  8305. return -EINVAL;
  8306. }
  8307. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8308. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8309. fb->pitches[0], plane_state->base.crtc_w);
  8310. return -EINVAL;
  8311. }
  8312. /*
  8313. * There's something wrong with the cursor on CHV pipe C.
  8314. * If it straddles the left edge of the screen then
  8315. * moving it away from the edge or disabling it often
  8316. * results in a pipe underrun, and often that can lead to
  8317. * dead pipe (constant underrun reported, and it scans
  8318. * out just a solid color). To recover from that, the
  8319. * display power well must be turned off and on again.
  8320. * Refuse the put the cursor into that compromised position.
  8321. */
  8322. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8323. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8324. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8325. return -EINVAL;
  8326. }
  8327. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8328. return 0;
  8329. }
  8330. static void i9xx_update_cursor(struct intel_plane *plane,
  8331. const struct intel_crtc_state *crtc_state,
  8332. const struct intel_plane_state *plane_state)
  8333. {
  8334. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8335. enum pipe pipe = plane->pipe;
  8336. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8337. unsigned long irqflags;
  8338. if (plane_state && plane_state->base.visible) {
  8339. cntl = plane_state->ctl;
  8340. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8341. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8342. base = intel_cursor_base(plane_state);
  8343. pos = intel_cursor_position(plane_state);
  8344. }
  8345. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8346. /*
  8347. * On some platforms writing CURCNTR first will also
  8348. * cause CURPOS to be armed by the CURBASE write.
  8349. * Without the CURCNTR write the CURPOS write would
  8350. * arm itself. Thus we always start the full update
  8351. * with a CURCNTR write.
  8352. *
  8353. * On other platforms CURPOS always requires the
  8354. * CURBASE write to arm the update. Additonally
  8355. * a write to any of the cursor register will cancel
  8356. * an already armed cursor update. Thus leaving out
  8357. * the CURBASE write after CURPOS could lead to a
  8358. * cursor that doesn't appear to move, or even change
  8359. * shape. Thus we always write CURBASE.
  8360. *
  8361. * CURCNTR and CUR_FBC_CTL are always
  8362. * armed by the CURBASE write only.
  8363. */
  8364. if (plane->cursor.base != base ||
  8365. plane->cursor.size != fbc_ctl ||
  8366. plane->cursor.cntl != cntl) {
  8367. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8368. if (HAS_CUR_FBC(dev_priv))
  8369. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8370. I915_WRITE_FW(CURPOS(pipe), pos);
  8371. I915_WRITE_FW(CURBASE(pipe), base);
  8372. plane->cursor.base = base;
  8373. plane->cursor.size = fbc_ctl;
  8374. plane->cursor.cntl = cntl;
  8375. } else {
  8376. I915_WRITE_FW(CURPOS(pipe), pos);
  8377. I915_WRITE_FW(CURBASE(pipe), base);
  8378. }
  8379. POSTING_READ_FW(CURBASE(pipe));
  8380. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8381. }
  8382. static void i9xx_disable_cursor(struct intel_plane *plane,
  8383. struct intel_crtc *crtc)
  8384. {
  8385. i9xx_update_cursor(plane, NULL, NULL);
  8386. }
  8387. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
  8388. enum pipe *pipe)
  8389. {
  8390. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8391. enum intel_display_power_domain power_domain;
  8392. bool ret;
  8393. u32 val;
  8394. /*
  8395. * Not 100% correct for planes that can move between pipes,
  8396. * but that's only the case for gen2-3 which don't have any
  8397. * display power wells.
  8398. */
  8399. power_domain = POWER_DOMAIN_PIPE(plane->pipe);
  8400. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8401. return false;
  8402. val = I915_READ(CURCNTR(plane->pipe));
  8403. ret = val & MCURSOR_MODE;
  8404. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8405. *pipe = plane->pipe;
  8406. else
  8407. *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
  8408. MCURSOR_PIPE_SELECT_SHIFT;
  8409. intel_display_power_put(dev_priv, power_domain);
  8410. return ret;
  8411. }
  8412. /* VESA 640x480x72Hz mode to set on the pipe */
  8413. static const struct drm_display_mode load_detect_mode = {
  8414. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8415. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8416. };
  8417. struct drm_framebuffer *
  8418. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8419. struct drm_mode_fb_cmd2 *mode_cmd)
  8420. {
  8421. struct intel_framebuffer *intel_fb;
  8422. int ret;
  8423. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8424. if (!intel_fb)
  8425. return ERR_PTR(-ENOMEM);
  8426. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8427. if (ret)
  8428. goto err;
  8429. return &intel_fb->base;
  8430. err:
  8431. kfree(intel_fb);
  8432. return ERR_PTR(ret);
  8433. }
  8434. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8435. struct drm_crtc *crtc)
  8436. {
  8437. struct drm_plane *plane;
  8438. struct drm_plane_state *plane_state;
  8439. int ret, i;
  8440. ret = drm_atomic_add_affected_planes(state, crtc);
  8441. if (ret)
  8442. return ret;
  8443. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8444. if (plane_state->crtc != crtc)
  8445. continue;
  8446. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8447. if (ret)
  8448. return ret;
  8449. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8450. }
  8451. return 0;
  8452. }
  8453. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8454. const struct drm_display_mode *mode,
  8455. struct intel_load_detect_pipe *old,
  8456. struct drm_modeset_acquire_ctx *ctx)
  8457. {
  8458. struct intel_crtc *intel_crtc;
  8459. struct intel_encoder *intel_encoder =
  8460. intel_attached_encoder(connector);
  8461. struct drm_crtc *possible_crtc;
  8462. struct drm_encoder *encoder = &intel_encoder->base;
  8463. struct drm_crtc *crtc = NULL;
  8464. struct drm_device *dev = encoder->dev;
  8465. struct drm_i915_private *dev_priv = to_i915(dev);
  8466. struct drm_mode_config *config = &dev->mode_config;
  8467. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8468. struct drm_connector_state *connector_state;
  8469. struct intel_crtc_state *crtc_state;
  8470. int ret, i = -1;
  8471. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8472. connector->base.id, connector->name,
  8473. encoder->base.id, encoder->name);
  8474. old->restore_state = NULL;
  8475. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8476. /*
  8477. * Algorithm gets a little messy:
  8478. *
  8479. * - if the connector already has an assigned crtc, use it (but make
  8480. * sure it's on first)
  8481. *
  8482. * - try to find the first unused crtc that can drive this connector,
  8483. * and use that if we find one
  8484. */
  8485. /* See if we already have a CRTC for this connector */
  8486. if (connector->state->crtc) {
  8487. crtc = connector->state->crtc;
  8488. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8489. if (ret)
  8490. goto fail;
  8491. /* Make sure the crtc and connector are running */
  8492. goto found;
  8493. }
  8494. /* Find an unused one (if possible) */
  8495. for_each_crtc(dev, possible_crtc) {
  8496. i++;
  8497. if (!(encoder->possible_crtcs & (1 << i)))
  8498. continue;
  8499. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8500. if (ret)
  8501. goto fail;
  8502. if (possible_crtc->state->enable) {
  8503. drm_modeset_unlock(&possible_crtc->mutex);
  8504. continue;
  8505. }
  8506. crtc = possible_crtc;
  8507. break;
  8508. }
  8509. /*
  8510. * If we didn't find an unused CRTC, don't use any.
  8511. */
  8512. if (!crtc) {
  8513. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8514. ret = -ENODEV;
  8515. goto fail;
  8516. }
  8517. found:
  8518. intel_crtc = to_intel_crtc(crtc);
  8519. state = drm_atomic_state_alloc(dev);
  8520. restore_state = drm_atomic_state_alloc(dev);
  8521. if (!state || !restore_state) {
  8522. ret = -ENOMEM;
  8523. goto fail;
  8524. }
  8525. state->acquire_ctx = ctx;
  8526. restore_state->acquire_ctx = ctx;
  8527. connector_state = drm_atomic_get_connector_state(state, connector);
  8528. if (IS_ERR(connector_state)) {
  8529. ret = PTR_ERR(connector_state);
  8530. goto fail;
  8531. }
  8532. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8533. if (ret)
  8534. goto fail;
  8535. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8536. if (IS_ERR(crtc_state)) {
  8537. ret = PTR_ERR(crtc_state);
  8538. goto fail;
  8539. }
  8540. crtc_state->base.active = crtc_state->base.enable = true;
  8541. if (!mode)
  8542. mode = &load_detect_mode;
  8543. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8544. if (ret)
  8545. goto fail;
  8546. ret = intel_modeset_disable_planes(state, crtc);
  8547. if (ret)
  8548. goto fail;
  8549. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8550. if (!ret)
  8551. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8552. if (!ret)
  8553. ret = drm_atomic_add_affected_planes(restore_state, crtc);
  8554. if (ret) {
  8555. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8556. goto fail;
  8557. }
  8558. ret = drm_atomic_commit(state);
  8559. if (ret) {
  8560. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8561. goto fail;
  8562. }
  8563. old->restore_state = restore_state;
  8564. drm_atomic_state_put(state);
  8565. /* let the connector get through one full cycle before testing */
  8566. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8567. return true;
  8568. fail:
  8569. if (state) {
  8570. drm_atomic_state_put(state);
  8571. state = NULL;
  8572. }
  8573. if (restore_state) {
  8574. drm_atomic_state_put(restore_state);
  8575. restore_state = NULL;
  8576. }
  8577. if (ret == -EDEADLK)
  8578. return ret;
  8579. return false;
  8580. }
  8581. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8582. struct intel_load_detect_pipe *old,
  8583. struct drm_modeset_acquire_ctx *ctx)
  8584. {
  8585. struct intel_encoder *intel_encoder =
  8586. intel_attached_encoder(connector);
  8587. struct drm_encoder *encoder = &intel_encoder->base;
  8588. struct drm_atomic_state *state = old->restore_state;
  8589. int ret;
  8590. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8591. connector->base.id, connector->name,
  8592. encoder->base.id, encoder->name);
  8593. if (!state)
  8594. return;
  8595. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8596. if (ret)
  8597. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8598. drm_atomic_state_put(state);
  8599. }
  8600. static int i9xx_pll_refclk(struct drm_device *dev,
  8601. const struct intel_crtc_state *pipe_config)
  8602. {
  8603. struct drm_i915_private *dev_priv = to_i915(dev);
  8604. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8605. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8606. return dev_priv->vbt.lvds_ssc_freq;
  8607. else if (HAS_PCH_SPLIT(dev_priv))
  8608. return 120000;
  8609. else if (!IS_GEN2(dev_priv))
  8610. return 96000;
  8611. else
  8612. return 48000;
  8613. }
  8614. /* Returns the clock of the currently programmed mode of the given pipe. */
  8615. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8616. struct intel_crtc_state *pipe_config)
  8617. {
  8618. struct drm_device *dev = crtc->base.dev;
  8619. struct drm_i915_private *dev_priv = to_i915(dev);
  8620. int pipe = pipe_config->cpu_transcoder;
  8621. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8622. u32 fp;
  8623. struct dpll clock;
  8624. int port_clock;
  8625. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8626. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8627. fp = pipe_config->dpll_hw_state.fp0;
  8628. else
  8629. fp = pipe_config->dpll_hw_state.fp1;
  8630. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8631. if (IS_PINEVIEW(dev_priv)) {
  8632. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8633. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8634. } else {
  8635. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8636. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8637. }
  8638. if (!IS_GEN2(dev_priv)) {
  8639. if (IS_PINEVIEW(dev_priv))
  8640. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8641. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8642. else
  8643. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8644. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8645. switch (dpll & DPLL_MODE_MASK) {
  8646. case DPLLB_MODE_DAC_SERIAL:
  8647. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8648. 5 : 10;
  8649. break;
  8650. case DPLLB_MODE_LVDS:
  8651. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8652. 7 : 14;
  8653. break;
  8654. default:
  8655. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8656. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8657. return;
  8658. }
  8659. if (IS_PINEVIEW(dev_priv))
  8660. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8661. else
  8662. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8663. } else {
  8664. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8665. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8666. if (is_lvds) {
  8667. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8668. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8669. if (lvds & LVDS_CLKB_POWER_UP)
  8670. clock.p2 = 7;
  8671. else
  8672. clock.p2 = 14;
  8673. } else {
  8674. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8675. clock.p1 = 2;
  8676. else {
  8677. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8678. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8679. }
  8680. if (dpll & PLL_P2_DIVIDE_BY_4)
  8681. clock.p2 = 4;
  8682. else
  8683. clock.p2 = 2;
  8684. }
  8685. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8686. }
  8687. /*
  8688. * This value includes pixel_multiplier. We will use
  8689. * port_clock to compute adjusted_mode.crtc_clock in the
  8690. * encoder's get_config() function.
  8691. */
  8692. pipe_config->port_clock = port_clock;
  8693. }
  8694. int intel_dotclock_calculate(int link_freq,
  8695. const struct intel_link_m_n *m_n)
  8696. {
  8697. /*
  8698. * The calculation for the data clock is:
  8699. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8700. * But we want to avoid losing precison if possible, so:
  8701. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8702. *
  8703. * and the link clock is simpler:
  8704. * link_clock = (m * link_clock) / n
  8705. */
  8706. if (!m_n->link_n)
  8707. return 0;
  8708. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8709. }
  8710. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8711. struct intel_crtc_state *pipe_config)
  8712. {
  8713. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8714. /* read out port_clock from the DPLL */
  8715. i9xx_crtc_clock_get(crtc, pipe_config);
  8716. /*
  8717. * In case there is an active pipe without active ports,
  8718. * we may need some idea for the dotclock anyway.
  8719. * Calculate one based on the FDI configuration.
  8720. */
  8721. pipe_config->base.adjusted_mode.crtc_clock =
  8722. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8723. &pipe_config->fdi_m_n);
  8724. }
  8725. /* Returns the currently programmed mode of the given encoder. */
  8726. struct drm_display_mode *
  8727. intel_encoder_current_mode(struct intel_encoder *encoder)
  8728. {
  8729. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8730. struct intel_crtc_state *crtc_state;
  8731. struct drm_display_mode *mode;
  8732. struct intel_crtc *crtc;
  8733. enum pipe pipe;
  8734. if (!encoder->get_hw_state(encoder, &pipe))
  8735. return NULL;
  8736. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8737. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8738. if (!mode)
  8739. return NULL;
  8740. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8741. if (!crtc_state) {
  8742. kfree(mode);
  8743. return NULL;
  8744. }
  8745. crtc_state->base.crtc = &crtc->base;
  8746. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8747. kfree(crtc_state);
  8748. kfree(mode);
  8749. return NULL;
  8750. }
  8751. encoder->get_config(encoder, crtc_state);
  8752. intel_mode_from_pipe_config(mode, crtc_state);
  8753. kfree(crtc_state);
  8754. return mode;
  8755. }
  8756. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8757. {
  8758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8759. drm_crtc_cleanup(crtc);
  8760. kfree(intel_crtc);
  8761. }
  8762. /**
  8763. * intel_wm_need_update - Check whether watermarks need updating
  8764. * @plane: drm plane
  8765. * @state: new plane state
  8766. *
  8767. * Check current plane state versus the new one to determine whether
  8768. * watermarks need to be recalculated.
  8769. *
  8770. * Returns true or false.
  8771. */
  8772. static bool intel_wm_need_update(struct drm_plane *plane,
  8773. struct drm_plane_state *state)
  8774. {
  8775. struct intel_plane_state *new = to_intel_plane_state(state);
  8776. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8777. /* Update watermarks on tiling or size changes. */
  8778. if (new->base.visible != cur->base.visible)
  8779. return true;
  8780. if (!cur->base.fb || !new->base.fb)
  8781. return false;
  8782. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8783. cur->base.rotation != new->base.rotation ||
  8784. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8785. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8786. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8787. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8788. return true;
  8789. return false;
  8790. }
  8791. static bool needs_scaling(const struct intel_plane_state *state)
  8792. {
  8793. int src_w = drm_rect_width(&state->base.src) >> 16;
  8794. int src_h = drm_rect_height(&state->base.src) >> 16;
  8795. int dst_w = drm_rect_width(&state->base.dst);
  8796. int dst_h = drm_rect_height(&state->base.dst);
  8797. return (src_w != dst_w || src_h != dst_h);
  8798. }
  8799. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8800. struct drm_crtc_state *crtc_state,
  8801. const struct intel_plane_state *old_plane_state,
  8802. struct drm_plane_state *plane_state)
  8803. {
  8804. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8805. struct drm_crtc *crtc = crtc_state->crtc;
  8806. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8807. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8808. struct drm_device *dev = crtc->dev;
  8809. struct drm_i915_private *dev_priv = to_i915(dev);
  8810. bool mode_changed = needs_modeset(crtc_state);
  8811. bool was_crtc_enabled = old_crtc_state->base.active;
  8812. bool is_crtc_enabled = crtc_state->active;
  8813. bool turn_off, turn_on, visible, was_visible;
  8814. struct drm_framebuffer *fb = plane_state->fb;
  8815. int ret;
  8816. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8817. ret = skl_update_scaler_plane(
  8818. to_intel_crtc_state(crtc_state),
  8819. to_intel_plane_state(plane_state));
  8820. if (ret)
  8821. return ret;
  8822. }
  8823. was_visible = old_plane_state->base.visible;
  8824. visible = plane_state->visible;
  8825. if (!was_crtc_enabled && WARN_ON(was_visible))
  8826. was_visible = false;
  8827. /*
  8828. * Visibility is calculated as if the crtc was on, but
  8829. * after scaler setup everything depends on it being off
  8830. * when the crtc isn't active.
  8831. *
  8832. * FIXME this is wrong for watermarks. Watermarks should also
  8833. * be computed as if the pipe would be active. Perhaps move
  8834. * per-plane wm computation to the .check_plane() hook, and
  8835. * only combine the results from all planes in the current place?
  8836. */
  8837. if (!is_crtc_enabled) {
  8838. plane_state->visible = visible = false;
  8839. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8840. }
  8841. if (!was_visible && !visible)
  8842. return 0;
  8843. if (fb != old_plane_state->base.fb)
  8844. pipe_config->fb_changed = true;
  8845. turn_off = was_visible && (!visible || mode_changed);
  8846. turn_on = visible && (!was_visible || mode_changed);
  8847. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8848. intel_crtc->base.base.id, intel_crtc->base.name,
  8849. plane->base.base.id, plane->base.name,
  8850. fb ? fb->base.id : -1);
  8851. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8852. plane->base.base.id, plane->base.name,
  8853. was_visible, visible,
  8854. turn_off, turn_on, mode_changed);
  8855. if (turn_on) {
  8856. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8857. pipe_config->update_wm_pre = true;
  8858. /* must disable cxsr around plane enable/disable */
  8859. if (plane->id != PLANE_CURSOR)
  8860. pipe_config->disable_cxsr = true;
  8861. } else if (turn_off) {
  8862. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8863. pipe_config->update_wm_post = true;
  8864. /* must disable cxsr around plane enable/disable */
  8865. if (plane->id != PLANE_CURSOR)
  8866. pipe_config->disable_cxsr = true;
  8867. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8868. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8869. /* FIXME bollocks */
  8870. pipe_config->update_wm_pre = true;
  8871. pipe_config->update_wm_post = true;
  8872. }
  8873. }
  8874. if (visible || was_visible)
  8875. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8876. /*
  8877. * WaCxSRDisabledForSpriteScaling:ivb
  8878. *
  8879. * cstate->update_wm was already set above, so this flag will
  8880. * take effect when we commit and program watermarks.
  8881. */
  8882. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8883. needs_scaling(to_intel_plane_state(plane_state)) &&
  8884. !needs_scaling(old_plane_state))
  8885. pipe_config->disable_lp_wm = true;
  8886. return 0;
  8887. }
  8888. static bool encoders_cloneable(const struct intel_encoder *a,
  8889. const struct intel_encoder *b)
  8890. {
  8891. /* masks could be asymmetric, so check both ways */
  8892. return a == b || (a->cloneable & (1 << b->type) &&
  8893. b->cloneable & (1 << a->type));
  8894. }
  8895. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8896. struct intel_crtc *crtc,
  8897. struct intel_encoder *encoder)
  8898. {
  8899. struct intel_encoder *source_encoder;
  8900. struct drm_connector *connector;
  8901. struct drm_connector_state *connector_state;
  8902. int i;
  8903. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8904. if (connector_state->crtc != &crtc->base)
  8905. continue;
  8906. source_encoder =
  8907. to_intel_encoder(connector_state->best_encoder);
  8908. if (!encoders_cloneable(encoder, source_encoder))
  8909. return false;
  8910. }
  8911. return true;
  8912. }
  8913. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8914. struct drm_crtc_state *crtc_state)
  8915. {
  8916. struct drm_device *dev = crtc->dev;
  8917. struct drm_i915_private *dev_priv = to_i915(dev);
  8918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8919. struct intel_crtc_state *pipe_config =
  8920. to_intel_crtc_state(crtc_state);
  8921. struct drm_atomic_state *state = crtc_state->state;
  8922. int ret;
  8923. bool mode_changed = needs_modeset(crtc_state);
  8924. if (mode_changed && !crtc_state->active)
  8925. pipe_config->update_wm_post = true;
  8926. if (mode_changed && crtc_state->enable &&
  8927. dev_priv->display.crtc_compute_clock &&
  8928. !WARN_ON(pipe_config->shared_dpll)) {
  8929. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8930. pipe_config);
  8931. if (ret)
  8932. return ret;
  8933. }
  8934. if (crtc_state->color_mgmt_changed) {
  8935. ret = intel_color_check(crtc, crtc_state);
  8936. if (ret)
  8937. return ret;
  8938. /*
  8939. * Changing color management on Intel hardware is
  8940. * handled as part of planes update.
  8941. */
  8942. crtc_state->planes_changed = true;
  8943. }
  8944. ret = 0;
  8945. if (dev_priv->display.compute_pipe_wm) {
  8946. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8947. if (ret) {
  8948. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8949. return ret;
  8950. }
  8951. }
  8952. if (dev_priv->display.compute_intermediate_wm &&
  8953. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8954. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8955. return 0;
  8956. /*
  8957. * Calculate 'intermediate' watermarks that satisfy both the
  8958. * old state and the new state. We can program these
  8959. * immediately.
  8960. */
  8961. ret = dev_priv->display.compute_intermediate_wm(dev,
  8962. intel_crtc,
  8963. pipe_config);
  8964. if (ret) {
  8965. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8966. return ret;
  8967. }
  8968. } else if (dev_priv->display.compute_intermediate_wm) {
  8969. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8970. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8971. }
  8972. if (INTEL_GEN(dev_priv) >= 9) {
  8973. if (mode_changed)
  8974. ret = skl_update_scaler_crtc(pipe_config);
  8975. if (!ret)
  8976. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8977. pipe_config);
  8978. if (!ret)
  8979. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8980. pipe_config);
  8981. }
  8982. if (HAS_IPS(dev_priv))
  8983. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8984. return ret;
  8985. }
  8986. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8987. .atomic_begin = intel_begin_crtc_commit,
  8988. .atomic_flush = intel_finish_crtc_commit,
  8989. .atomic_check = intel_crtc_atomic_check,
  8990. };
  8991. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8992. {
  8993. struct intel_connector *connector;
  8994. struct drm_connector_list_iter conn_iter;
  8995. drm_connector_list_iter_begin(dev, &conn_iter);
  8996. for_each_intel_connector_iter(connector, &conn_iter) {
  8997. if (connector->base.state->crtc)
  8998. drm_connector_put(&connector->base);
  8999. if (connector->base.encoder) {
  9000. connector->base.state->best_encoder =
  9001. connector->base.encoder;
  9002. connector->base.state->crtc =
  9003. connector->base.encoder->crtc;
  9004. drm_connector_get(&connector->base);
  9005. } else {
  9006. connector->base.state->best_encoder = NULL;
  9007. connector->base.state->crtc = NULL;
  9008. }
  9009. }
  9010. drm_connector_list_iter_end(&conn_iter);
  9011. }
  9012. static void
  9013. connected_sink_compute_bpp(struct intel_connector *connector,
  9014. struct intel_crtc_state *pipe_config)
  9015. {
  9016. const struct drm_display_info *info = &connector->base.display_info;
  9017. int bpp = pipe_config->pipe_bpp;
  9018. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9019. connector->base.base.id,
  9020. connector->base.name);
  9021. /* Don't use an invalid EDID bpc value */
  9022. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9023. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9024. bpp, info->bpc * 3);
  9025. pipe_config->pipe_bpp = info->bpc * 3;
  9026. }
  9027. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9028. if (info->bpc == 0 && bpp > 24) {
  9029. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9030. bpp);
  9031. pipe_config->pipe_bpp = 24;
  9032. }
  9033. }
  9034. static int
  9035. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9036. struct intel_crtc_state *pipe_config)
  9037. {
  9038. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9039. struct drm_atomic_state *state;
  9040. struct drm_connector *connector;
  9041. struct drm_connector_state *connector_state;
  9042. int bpp, i;
  9043. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9044. IS_CHERRYVIEW(dev_priv)))
  9045. bpp = 10*3;
  9046. else if (INTEL_GEN(dev_priv) >= 5)
  9047. bpp = 12*3;
  9048. else
  9049. bpp = 8*3;
  9050. pipe_config->pipe_bpp = bpp;
  9051. state = pipe_config->base.state;
  9052. /* Clamp display bpp to EDID value */
  9053. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9054. if (connector_state->crtc != &crtc->base)
  9055. continue;
  9056. connected_sink_compute_bpp(to_intel_connector(connector),
  9057. pipe_config);
  9058. }
  9059. return bpp;
  9060. }
  9061. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9062. {
  9063. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9064. "type: 0x%x flags: 0x%x\n",
  9065. mode->crtc_clock,
  9066. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9067. mode->crtc_hsync_end, mode->crtc_htotal,
  9068. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9069. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9070. }
  9071. static inline void
  9072. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9073. unsigned int lane_count, struct intel_link_m_n *m_n)
  9074. {
  9075. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9076. id, lane_count,
  9077. m_n->gmch_m, m_n->gmch_n,
  9078. m_n->link_m, m_n->link_n, m_n->tu);
  9079. }
  9080. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  9081. static const char * const output_type_str[] = {
  9082. OUTPUT_TYPE(UNUSED),
  9083. OUTPUT_TYPE(ANALOG),
  9084. OUTPUT_TYPE(DVO),
  9085. OUTPUT_TYPE(SDVO),
  9086. OUTPUT_TYPE(LVDS),
  9087. OUTPUT_TYPE(TVOUT),
  9088. OUTPUT_TYPE(HDMI),
  9089. OUTPUT_TYPE(DP),
  9090. OUTPUT_TYPE(EDP),
  9091. OUTPUT_TYPE(DSI),
  9092. OUTPUT_TYPE(DDI),
  9093. OUTPUT_TYPE(DP_MST),
  9094. };
  9095. #undef OUTPUT_TYPE
  9096. static void snprintf_output_types(char *buf, size_t len,
  9097. unsigned int output_types)
  9098. {
  9099. char *str = buf;
  9100. int i;
  9101. str[0] = '\0';
  9102. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  9103. int r;
  9104. if ((output_types & BIT(i)) == 0)
  9105. continue;
  9106. r = snprintf(str, len, "%s%s",
  9107. str != buf ? "," : "", output_type_str[i]);
  9108. if (r >= len)
  9109. break;
  9110. str += r;
  9111. len -= r;
  9112. output_types &= ~BIT(i);
  9113. }
  9114. WARN_ON_ONCE(output_types != 0);
  9115. }
  9116. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9117. struct intel_crtc_state *pipe_config,
  9118. const char *context)
  9119. {
  9120. struct drm_device *dev = crtc->base.dev;
  9121. struct drm_i915_private *dev_priv = to_i915(dev);
  9122. struct drm_plane *plane;
  9123. struct intel_plane *intel_plane;
  9124. struct intel_plane_state *state;
  9125. struct drm_framebuffer *fb;
  9126. char buf[64];
  9127. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9128. crtc->base.base.id, crtc->base.name, context);
  9129. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  9130. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  9131. buf, pipe_config->output_types);
  9132. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9133. transcoder_name(pipe_config->cpu_transcoder),
  9134. pipe_config->pipe_bpp, pipe_config->dither);
  9135. if (pipe_config->has_pch_encoder)
  9136. intel_dump_m_n_config(pipe_config, "fdi",
  9137. pipe_config->fdi_lanes,
  9138. &pipe_config->fdi_m_n);
  9139. if (pipe_config->ycbcr420)
  9140. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  9141. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9142. intel_dump_m_n_config(pipe_config, "dp m_n",
  9143. pipe_config->lane_count, &pipe_config->dp_m_n);
  9144. if (pipe_config->has_drrs)
  9145. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9146. pipe_config->lane_count,
  9147. &pipe_config->dp_m2_n2);
  9148. }
  9149. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9150. pipe_config->has_audio, pipe_config->has_infoframe);
  9151. DRM_DEBUG_KMS("requested mode:\n");
  9152. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9153. DRM_DEBUG_KMS("adjusted mode:\n");
  9154. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9155. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9156. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9157. pipe_config->port_clock,
  9158. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9159. pipe_config->pixel_rate);
  9160. if (INTEL_GEN(dev_priv) >= 9)
  9161. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9162. crtc->num_scalers,
  9163. pipe_config->scaler_state.scaler_users,
  9164. pipe_config->scaler_state.scaler_id);
  9165. if (HAS_GMCH_DISPLAY(dev_priv))
  9166. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9167. pipe_config->gmch_pfit.control,
  9168. pipe_config->gmch_pfit.pgm_ratios,
  9169. pipe_config->gmch_pfit.lvds_border_bits);
  9170. else
  9171. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9172. pipe_config->pch_pfit.pos,
  9173. pipe_config->pch_pfit.size,
  9174. enableddisabled(pipe_config->pch_pfit.enabled));
  9175. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9176. pipe_config->ips_enabled, pipe_config->double_wide);
  9177. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9178. DRM_DEBUG_KMS("planes on this crtc\n");
  9179. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9180. struct drm_format_name_buf format_name;
  9181. intel_plane = to_intel_plane(plane);
  9182. if (intel_plane->pipe != crtc->pipe)
  9183. continue;
  9184. state = to_intel_plane_state(plane->state);
  9185. fb = state->base.fb;
  9186. if (!fb) {
  9187. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9188. plane->base.id, plane->name, state->scaler_id);
  9189. continue;
  9190. }
  9191. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9192. plane->base.id, plane->name,
  9193. fb->base.id, fb->width, fb->height,
  9194. drm_get_format_name(fb->format->format, &format_name));
  9195. if (INTEL_GEN(dev_priv) >= 9)
  9196. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9197. state->scaler_id,
  9198. state->base.src.x1 >> 16,
  9199. state->base.src.y1 >> 16,
  9200. drm_rect_width(&state->base.src) >> 16,
  9201. drm_rect_height(&state->base.src) >> 16,
  9202. state->base.dst.x1, state->base.dst.y1,
  9203. drm_rect_width(&state->base.dst),
  9204. drm_rect_height(&state->base.dst));
  9205. }
  9206. }
  9207. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9208. {
  9209. struct drm_device *dev = state->dev;
  9210. struct drm_connector *connector;
  9211. struct drm_connector_list_iter conn_iter;
  9212. unsigned int used_ports = 0;
  9213. unsigned int used_mst_ports = 0;
  9214. bool ret = true;
  9215. /*
  9216. * Walk the connector list instead of the encoder
  9217. * list to detect the problem on ddi platforms
  9218. * where there's just one encoder per digital port.
  9219. */
  9220. drm_connector_list_iter_begin(dev, &conn_iter);
  9221. drm_for_each_connector_iter(connector, &conn_iter) {
  9222. struct drm_connector_state *connector_state;
  9223. struct intel_encoder *encoder;
  9224. connector_state = drm_atomic_get_new_connector_state(state, connector);
  9225. if (!connector_state)
  9226. connector_state = connector->state;
  9227. if (!connector_state->best_encoder)
  9228. continue;
  9229. encoder = to_intel_encoder(connector_state->best_encoder);
  9230. WARN_ON(!connector_state->crtc);
  9231. switch (encoder->type) {
  9232. unsigned int port_mask;
  9233. case INTEL_OUTPUT_DDI:
  9234. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9235. break;
  9236. /* else: fall through */
  9237. case INTEL_OUTPUT_DP:
  9238. case INTEL_OUTPUT_HDMI:
  9239. case INTEL_OUTPUT_EDP:
  9240. port_mask = 1 << encoder->port;
  9241. /* the same port mustn't appear more than once */
  9242. if (used_ports & port_mask)
  9243. ret = false;
  9244. used_ports |= port_mask;
  9245. break;
  9246. case INTEL_OUTPUT_DP_MST:
  9247. used_mst_ports |=
  9248. 1 << encoder->port;
  9249. break;
  9250. default:
  9251. break;
  9252. }
  9253. }
  9254. drm_connector_list_iter_end(&conn_iter);
  9255. /* can't mix MST and SST/HDMI on the same port */
  9256. if (used_ports & used_mst_ports)
  9257. return false;
  9258. return ret;
  9259. }
  9260. static void
  9261. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9262. {
  9263. struct drm_i915_private *dev_priv =
  9264. to_i915(crtc_state->base.crtc->dev);
  9265. struct intel_crtc_scaler_state scaler_state;
  9266. struct intel_dpll_hw_state dpll_hw_state;
  9267. struct intel_shared_dpll *shared_dpll;
  9268. struct intel_crtc_wm_state wm_state;
  9269. bool force_thru, ips_force_disable;
  9270. /* FIXME: before the switch to atomic started, a new pipe_config was
  9271. * kzalloc'd. Code that depends on any field being zero should be
  9272. * fixed, so that the crtc_state can be safely duplicated. For now,
  9273. * only fields that are know to not cause problems are preserved. */
  9274. scaler_state = crtc_state->scaler_state;
  9275. shared_dpll = crtc_state->shared_dpll;
  9276. dpll_hw_state = crtc_state->dpll_hw_state;
  9277. force_thru = crtc_state->pch_pfit.force_thru;
  9278. ips_force_disable = crtc_state->ips_force_disable;
  9279. if (IS_G4X(dev_priv) ||
  9280. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9281. wm_state = crtc_state->wm;
  9282. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9283. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9284. memset(&crtc_state->base + 1, 0,
  9285. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9286. crtc_state->scaler_state = scaler_state;
  9287. crtc_state->shared_dpll = shared_dpll;
  9288. crtc_state->dpll_hw_state = dpll_hw_state;
  9289. crtc_state->pch_pfit.force_thru = force_thru;
  9290. crtc_state->ips_force_disable = ips_force_disable;
  9291. if (IS_G4X(dev_priv) ||
  9292. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9293. crtc_state->wm = wm_state;
  9294. }
  9295. static int
  9296. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9297. struct intel_crtc_state *pipe_config)
  9298. {
  9299. struct drm_atomic_state *state = pipe_config->base.state;
  9300. struct intel_encoder *encoder;
  9301. struct drm_connector *connector;
  9302. struct drm_connector_state *connector_state;
  9303. int base_bpp, ret = -EINVAL;
  9304. int i;
  9305. bool retry = true;
  9306. clear_intel_crtc_state(pipe_config);
  9307. pipe_config->cpu_transcoder =
  9308. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9309. /*
  9310. * Sanitize sync polarity flags based on requested ones. If neither
  9311. * positive or negative polarity is requested, treat this as meaning
  9312. * negative polarity.
  9313. */
  9314. if (!(pipe_config->base.adjusted_mode.flags &
  9315. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9316. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9317. if (!(pipe_config->base.adjusted_mode.flags &
  9318. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9319. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9320. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9321. pipe_config);
  9322. if (base_bpp < 0)
  9323. goto fail;
  9324. /*
  9325. * Determine the real pipe dimensions. Note that stereo modes can
  9326. * increase the actual pipe size due to the frame doubling and
  9327. * insertion of additional space for blanks between the frame. This
  9328. * is stored in the crtc timings. We use the requested mode to do this
  9329. * computation to clearly distinguish it from the adjusted mode, which
  9330. * can be changed by the connectors in the below retry loop.
  9331. */
  9332. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9333. &pipe_config->pipe_src_w,
  9334. &pipe_config->pipe_src_h);
  9335. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9336. if (connector_state->crtc != crtc)
  9337. continue;
  9338. encoder = to_intel_encoder(connector_state->best_encoder);
  9339. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9340. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9341. goto fail;
  9342. }
  9343. /*
  9344. * Determine output_types before calling the .compute_config()
  9345. * hooks so that the hooks can use this information safely.
  9346. */
  9347. if (encoder->compute_output_type)
  9348. pipe_config->output_types |=
  9349. BIT(encoder->compute_output_type(encoder, pipe_config,
  9350. connector_state));
  9351. else
  9352. pipe_config->output_types |= BIT(encoder->type);
  9353. }
  9354. encoder_retry:
  9355. /* Ensure the port clock defaults are reset when retrying. */
  9356. pipe_config->port_clock = 0;
  9357. pipe_config->pixel_multiplier = 1;
  9358. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9359. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9360. CRTC_STEREO_DOUBLE);
  9361. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9362. * adjust it according to limitations or connector properties, and also
  9363. * a chance to reject the mode entirely.
  9364. */
  9365. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9366. if (connector_state->crtc != crtc)
  9367. continue;
  9368. encoder = to_intel_encoder(connector_state->best_encoder);
  9369. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9370. DRM_DEBUG_KMS("Encoder config failure\n");
  9371. goto fail;
  9372. }
  9373. }
  9374. /* Set default port clock if not overwritten by the encoder. Needs to be
  9375. * done afterwards in case the encoder adjusts the mode. */
  9376. if (!pipe_config->port_clock)
  9377. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9378. * pipe_config->pixel_multiplier;
  9379. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9380. if (ret < 0) {
  9381. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9382. goto fail;
  9383. }
  9384. if (ret == RETRY) {
  9385. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9386. ret = -EINVAL;
  9387. goto fail;
  9388. }
  9389. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9390. retry = false;
  9391. goto encoder_retry;
  9392. }
  9393. /* Dithering seems to not pass-through bits correctly when it should, so
  9394. * only enable it on 6bpc panels and when its not a compliance
  9395. * test requesting 6bpc video pattern.
  9396. */
  9397. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9398. !pipe_config->dither_force_disable;
  9399. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9400. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9401. fail:
  9402. return ret;
  9403. }
  9404. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9405. {
  9406. int diff;
  9407. if (clock1 == clock2)
  9408. return true;
  9409. if (!clock1 || !clock2)
  9410. return false;
  9411. diff = abs(clock1 - clock2);
  9412. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9413. return true;
  9414. return false;
  9415. }
  9416. static bool
  9417. intel_compare_m_n(unsigned int m, unsigned int n,
  9418. unsigned int m2, unsigned int n2,
  9419. bool exact)
  9420. {
  9421. if (m == m2 && n == n2)
  9422. return true;
  9423. if (exact || !m || !n || !m2 || !n2)
  9424. return false;
  9425. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9426. if (n > n2) {
  9427. while (n > n2) {
  9428. m2 <<= 1;
  9429. n2 <<= 1;
  9430. }
  9431. } else if (n < n2) {
  9432. while (n < n2) {
  9433. m <<= 1;
  9434. n <<= 1;
  9435. }
  9436. }
  9437. if (n != n2)
  9438. return false;
  9439. return intel_fuzzy_clock_check(m, m2);
  9440. }
  9441. static bool
  9442. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9443. struct intel_link_m_n *m2_n2,
  9444. bool adjust)
  9445. {
  9446. if (m_n->tu == m2_n2->tu &&
  9447. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9448. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9449. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9450. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9451. if (adjust)
  9452. *m2_n2 = *m_n;
  9453. return true;
  9454. }
  9455. return false;
  9456. }
  9457. static void __printf(3, 4)
  9458. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9459. {
  9460. struct va_format vaf;
  9461. va_list args;
  9462. va_start(args, format);
  9463. vaf.fmt = format;
  9464. vaf.va = &args;
  9465. if (adjust)
  9466. drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
  9467. else
  9468. drm_err("mismatch in %s %pV", name, &vaf);
  9469. va_end(args);
  9470. }
  9471. static bool
  9472. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9473. struct intel_crtc_state *current_config,
  9474. struct intel_crtc_state *pipe_config,
  9475. bool adjust)
  9476. {
  9477. bool ret = true;
  9478. bool fixup_inherited = adjust &&
  9479. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9480. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9481. #define PIPE_CONF_CHECK_X(name) do { \
  9482. if (current_config->name != pipe_config->name) { \
  9483. pipe_config_err(adjust, __stringify(name), \
  9484. "(expected 0x%08x, found 0x%08x)\n", \
  9485. current_config->name, \
  9486. pipe_config->name); \
  9487. ret = false; \
  9488. } \
  9489. } while (0)
  9490. #define PIPE_CONF_CHECK_I(name) do { \
  9491. if (current_config->name != pipe_config->name) { \
  9492. pipe_config_err(adjust, __stringify(name), \
  9493. "(expected %i, found %i)\n", \
  9494. current_config->name, \
  9495. pipe_config->name); \
  9496. ret = false; \
  9497. } \
  9498. } while (0)
  9499. #define PIPE_CONF_CHECK_BOOL(name) do { \
  9500. if (current_config->name != pipe_config->name) { \
  9501. pipe_config_err(adjust, __stringify(name), \
  9502. "(expected %s, found %s)\n", \
  9503. yesno(current_config->name), \
  9504. yesno(pipe_config->name)); \
  9505. ret = false; \
  9506. } \
  9507. } while (0)
  9508. /*
  9509. * Checks state where we only read out the enabling, but not the entire
  9510. * state itself (like full infoframes or ELD for audio). These states
  9511. * require a full modeset on bootup to fix up.
  9512. */
  9513. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
  9514. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9515. PIPE_CONF_CHECK_BOOL(name); \
  9516. } else { \
  9517. pipe_config_err(adjust, __stringify(name), \
  9518. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9519. yesno(current_config->name), \
  9520. yesno(pipe_config->name)); \
  9521. ret = false; \
  9522. } \
  9523. } while (0)
  9524. #define PIPE_CONF_CHECK_P(name) do { \
  9525. if (current_config->name != pipe_config->name) { \
  9526. pipe_config_err(adjust, __stringify(name), \
  9527. "(expected %p, found %p)\n", \
  9528. current_config->name, \
  9529. pipe_config->name); \
  9530. ret = false; \
  9531. } \
  9532. } while (0)
  9533. #define PIPE_CONF_CHECK_M_N(name) do { \
  9534. if (!intel_compare_link_m_n(&current_config->name, \
  9535. &pipe_config->name,\
  9536. adjust)) { \
  9537. pipe_config_err(adjust, __stringify(name), \
  9538. "(expected tu %i gmch %i/%i link %i/%i, " \
  9539. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9540. current_config->name.tu, \
  9541. current_config->name.gmch_m, \
  9542. current_config->name.gmch_n, \
  9543. current_config->name.link_m, \
  9544. current_config->name.link_n, \
  9545. pipe_config->name.tu, \
  9546. pipe_config->name.gmch_m, \
  9547. pipe_config->name.gmch_n, \
  9548. pipe_config->name.link_m, \
  9549. pipe_config->name.link_n); \
  9550. ret = false; \
  9551. } \
  9552. } while (0)
  9553. /* This is required for BDW+ where there is only one set of registers for
  9554. * switching between high and low RR.
  9555. * This macro can be used whenever a comparison has to be made between one
  9556. * hw state and multiple sw state variables.
  9557. */
  9558. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
  9559. if (!intel_compare_link_m_n(&current_config->name, \
  9560. &pipe_config->name, adjust) && \
  9561. !intel_compare_link_m_n(&current_config->alt_name, \
  9562. &pipe_config->name, adjust)) { \
  9563. pipe_config_err(adjust, __stringify(name), \
  9564. "(expected tu %i gmch %i/%i link %i/%i, " \
  9565. "or tu %i gmch %i/%i link %i/%i, " \
  9566. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9567. current_config->name.tu, \
  9568. current_config->name.gmch_m, \
  9569. current_config->name.gmch_n, \
  9570. current_config->name.link_m, \
  9571. current_config->name.link_n, \
  9572. current_config->alt_name.tu, \
  9573. current_config->alt_name.gmch_m, \
  9574. current_config->alt_name.gmch_n, \
  9575. current_config->alt_name.link_m, \
  9576. current_config->alt_name.link_n, \
  9577. pipe_config->name.tu, \
  9578. pipe_config->name.gmch_m, \
  9579. pipe_config->name.gmch_n, \
  9580. pipe_config->name.link_m, \
  9581. pipe_config->name.link_n); \
  9582. ret = false; \
  9583. } \
  9584. } while (0)
  9585. #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
  9586. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9587. pipe_config_err(adjust, __stringify(name), \
  9588. "(%x) (expected %i, found %i)\n", \
  9589. (mask), \
  9590. current_config->name & (mask), \
  9591. pipe_config->name & (mask)); \
  9592. ret = false; \
  9593. } \
  9594. } while (0)
  9595. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
  9596. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9597. pipe_config_err(adjust, __stringify(name), \
  9598. "(expected %i, found %i)\n", \
  9599. current_config->name, \
  9600. pipe_config->name); \
  9601. ret = false; \
  9602. } \
  9603. } while (0)
  9604. #define PIPE_CONF_QUIRK(quirk) \
  9605. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9606. PIPE_CONF_CHECK_I(cpu_transcoder);
  9607. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9608. PIPE_CONF_CHECK_I(fdi_lanes);
  9609. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9610. PIPE_CONF_CHECK_I(lane_count);
  9611. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9612. if (INTEL_GEN(dev_priv) < 8) {
  9613. PIPE_CONF_CHECK_M_N(dp_m_n);
  9614. if (current_config->has_drrs)
  9615. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9616. } else
  9617. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9618. PIPE_CONF_CHECK_X(output_types);
  9619. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9620. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9621. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9622. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9623. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9624. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9625. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9626. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9627. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9628. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9629. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9630. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9631. PIPE_CONF_CHECK_I(pixel_multiplier);
  9632. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9633. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9634. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9635. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9636. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9637. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9638. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9639. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9640. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9641. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9642. DRM_MODE_FLAG_INTERLACE);
  9643. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9644. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9645. DRM_MODE_FLAG_PHSYNC);
  9646. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9647. DRM_MODE_FLAG_NHSYNC);
  9648. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9649. DRM_MODE_FLAG_PVSYNC);
  9650. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9651. DRM_MODE_FLAG_NVSYNC);
  9652. }
  9653. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9654. /* pfit ratios are autocomputed by the hw on gen4+ */
  9655. if (INTEL_GEN(dev_priv) < 4)
  9656. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9657. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9658. if (!adjust) {
  9659. PIPE_CONF_CHECK_I(pipe_src_w);
  9660. PIPE_CONF_CHECK_I(pipe_src_h);
  9661. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9662. if (current_config->pch_pfit.enabled) {
  9663. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9664. PIPE_CONF_CHECK_X(pch_pfit.size);
  9665. }
  9666. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9667. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9668. }
  9669. PIPE_CONF_CHECK_BOOL(double_wide);
  9670. PIPE_CONF_CHECK_P(shared_dpll);
  9671. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9672. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9673. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9674. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9675. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9676. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9677. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9678. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9679. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9680. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9681. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9682. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9683. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9684. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9685. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9686. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9687. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9688. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9689. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9690. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9691. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9692. PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
  9693. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
  9694. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
  9695. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
  9696. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
  9697. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
  9698. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
  9699. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
  9700. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
  9701. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
  9702. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9703. PIPE_CONF_CHECK_X(dsi_pll.div);
  9704. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9705. PIPE_CONF_CHECK_I(pipe_bpp);
  9706. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9707. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9708. PIPE_CONF_CHECK_I(min_voltage_level);
  9709. #undef PIPE_CONF_CHECK_X
  9710. #undef PIPE_CONF_CHECK_I
  9711. #undef PIPE_CONF_CHECK_BOOL
  9712. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9713. #undef PIPE_CONF_CHECK_P
  9714. #undef PIPE_CONF_CHECK_FLAGS
  9715. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9716. #undef PIPE_CONF_QUIRK
  9717. return ret;
  9718. }
  9719. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9720. const struct intel_crtc_state *pipe_config)
  9721. {
  9722. if (pipe_config->has_pch_encoder) {
  9723. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9724. &pipe_config->fdi_m_n);
  9725. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9726. /*
  9727. * FDI already provided one idea for the dotclock.
  9728. * Yell if the encoder disagrees.
  9729. */
  9730. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9731. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9732. fdi_dotclock, dotclock);
  9733. }
  9734. }
  9735. static void verify_wm_state(struct drm_crtc *crtc,
  9736. struct drm_crtc_state *new_state)
  9737. {
  9738. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9739. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9740. struct skl_pipe_wm hw_wm, *sw_wm;
  9741. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9742. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9743. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9744. const enum pipe pipe = intel_crtc->pipe;
  9745. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9746. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9747. return;
  9748. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9749. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9750. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9751. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9752. if (INTEL_GEN(dev_priv) >= 11)
  9753. if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
  9754. DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
  9755. sw_ddb->enabled_slices,
  9756. hw_ddb.enabled_slices);
  9757. /* planes */
  9758. for_each_universal_plane(dev_priv, pipe, plane) {
  9759. hw_plane_wm = &hw_wm.planes[plane];
  9760. sw_plane_wm = &sw_wm->planes[plane];
  9761. /* Watermarks */
  9762. for (level = 0; level <= max_level; level++) {
  9763. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9764. &sw_plane_wm->wm[level]))
  9765. continue;
  9766. 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",
  9767. pipe_name(pipe), plane + 1, level,
  9768. sw_plane_wm->wm[level].plane_en,
  9769. sw_plane_wm->wm[level].plane_res_b,
  9770. sw_plane_wm->wm[level].plane_res_l,
  9771. hw_plane_wm->wm[level].plane_en,
  9772. hw_plane_wm->wm[level].plane_res_b,
  9773. hw_plane_wm->wm[level].plane_res_l);
  9774. }
  9775. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9776. &sw_plane_wm->trans_wm)) {
  9777. 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",
  9778. pipe_name(pipe), plane + 1,
  9779. sw_plane_wm->trans_wm.plane_en,
  9780. sw_plane_wm->trans_wm.plane_res_b,
  9781. sw_plane_wm->trans_wm.plane_res_l,
  9782. hw_plane_wm->trans_wm.plane_en,
  9783. hw_plane_wm->trans_wm.plane_res_b,
  9784. hw_plane_wm->trans_wm.plane_res_l);
  9785. }
  9786. /* DDB */
  9787. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9788. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9789. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9790. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9791. pipe_name(pipe), plane + 1,
  9792. sw_ddb_entry->start, sw_ddb_entry->end,
  9793. hw_ddb_entry->start, hw_ddb_entry->end);
  9794. }
  9795. }
  9796. /*
  9797. * cursor
  9798. * If the cursor plane isn't active, we may not have updated it's ddb
  9799. * allocation. In that case since the ddb allocation will be updated
  9800. * once the plane becomes visible, we can skip this check
  9801. */
  9802. if (1) {
  9803. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9804. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9805. /* Watermarks */
  9806. for (level = 0; level <= max_level; level++) {
  9807. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9808. &sw_plane_wm->wm[level]))
  9809. continue;
  9810. 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",
  9811. pipe_name(pipe), level,
  9812. sw_plane_wm->wm[level].plane_en,
  9813. sw_plane_wm->wm[level].plane_res_b,
  9814. sw_plane_wm->wm[level].plane_res_l,
  9815. hw_plane_wm->wm[level].plane_en,
  9816. hw_plane_wm->wm[level].plane_res_b,
  9817. hw_plane_wm->wm[level].plane_res_l);
  9818. }
  9819. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9820. &sw_plane_wm->trans_wm)) {
  9821. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9822. pipe_name(pipe),
  9823. sw_plane_wm->trans_wm.plane_en,
  9824. sw_plane_wm->trans_wm.plane_res_b,
  9825. sw_plane_wm->trans_wm.plane_res_l,
  9826. hw_plane_wm->trans_wm.plane_en,
  9827. hw_plane_wm->trans_wm.plane_res_b,
  9828. hw_plane_wm->trans_wm.plane_res_l);
  9829. }
  9830. /* DDB */
  9831. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9832. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9833. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9834. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9835. pipe_name(pipe),
  9836. sw_ddb_entry->start, sw_ddb_entry->end,
  9837. hw_ddb_entry->start, hw_ddb_entry->end);
  9838. }
  9839. }
  9840. }
  9841. static void
  9842. verify_connector_state(struct drm_device *dev,
  9843. struct drm_atomic_state *state,
  9844. struct drm_crtc *crtc)
  9845. {
  9846. struct drm_connector *connector;
  9847. struct drm_connector_state *new_conn_state;
  9848. int i;
  9849. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9850. struct drm_encoder *encoder = connector->encoder;
  9851. struct drm_crtc_state *crtc_state = NULL;
  9852. if (new_conn_state->crtc != crtc)
  9853. continue;
  9854. if (crtc)
  9855. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9856. intel_connector_verify_state(crtc_state, new_conn_state);
  9857. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9858. "connector's atomic encoder doesn't match legacy encoder\n");
  9859. }
  9860. }
  9861. static void
  9862. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9863. {
  9864. struct intel_encoder *encoder;
  9865. struct drm_connector *connector;
  9866. struct drm_connector_state *old_conn_state, *new_conn_state;
  9867. int i;
  9868. for_each_intel_encoder(dev, encoder) {
  9869. bool enabled = false, found = false;
  9870. enum pipe pipe;
  9871. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9872. encoder->base.base.id,
  9873. encoder->base.name);
  9874. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9875. new_conn_state, i) {
  9876. if (old_conn_state->best_encoder == &encoder->base)
  9877. found = true;
  9878. if (new_conn_state->best_encoder != &encoder->base)
  9879. continue;
  9880. found = enabled = true;
  9881. I915_STATE_WARN(new_conn_state->crtc !=
  9882. encoder->base.crtc,
  9883. "connector's crtc doesn't match encoder crtc\n");
  9884. }
  9885. if (!found)
  9886. continue;
  9887. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9888. "encoder's enabled state mismatch "
  9889. "(expected %i, found %i)\n",
  9890. !!encoder->base.crtc, enabled);
  9891. if (!encoder->base.crtc) {
  9892. bool active;
  9893. active = encoder->get_hw_state(encoder, &pipe);
  9894. I915_STATE_WARN(active,
  9895. "encoder detached but still enabled on pipe %c.\n",
  9896. pipe_name(pipe));
  9897. }
  9898. }
  9899. }
  9900. static void
  9901. verify_crtc_state(struct drm_crtc *crtc,
  9902. struct drm_crtc_state *old_crtc_state,
  9903. struct drm_crtc_state *new_crtc_state)
  9904. {
  9905. struct drm_device *dev = crtc->dev;
  9906. struct drm_i915_private *dev_priv = to_i915(dev);
  9907. struct intel_encoder *encoder;
  9908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9909. struct intel_crtc_state *pipe_config, *sw_config;
  9910. struct drm_atomic_state *old_state;
  9911. bool active;
  9912. old_state = old_crtc_state->state;
  9913. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9914. pipe_config = to_intel_crtc_state(old_crtc_state);
  9915. memset(pipe_config, 0, sizeof(*pipe_config));
  9916. pipe_config->base.crtc = crtc;
  9917. pipe_config->base.state = old_state;
  9918. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9919. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9920. /* we keep both pipes enabled on 830 */
  9921. if (IS_I830(dev_priv))
  9922. active = new_crtc_state->active;
  9923. I915_STATE_WARN(new_crtc_state->active != active,
  9924. "crtc active state doesn't match with hw state "
  9925. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9926. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9927. "transitional active state does not match atomic hw state "
  9928. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9929. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9930. enum pipe pipe;
  9931. active = encoder->get_hw_state(encoder, &pipe);
  9932. I915_STATE_WARN(active != new_crtc_state->active,
  9933. "[ENCODER:%i] active %i with crtc active %i\n",
  9934. encoder->base.base.id, active, new_crtc_state->active);
  9935. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9936. "Encoder connected to wrong pipe %c\n",
  9937. pipe_name(pipe));
  9938. if (active)
  9939. encoder->get_config(encoder, pipe_config);
  9940. }
  9941. intel_crtc_compute_pixel_rate(pipe_config);
  9942. if (!new_crtc_state->active)
  9943. return;
  9944. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9945. sw_config = to_intel_crtc_state(new_crtc_state);
  9946. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9947. pipe_config, false)) {
  9948. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9949. intel_dump_pipe_config(intel_crtc, pipe_config,
  9950. "[hw state]");
  9951. intel_dump_pipe_config(intel_crtc, sw_config,
  9952. "[sw state]");
  9953. }
  9954. }
  9955. static void
  9956. intel_verify_planes(struct intel_atomic_state *state)
  9957. {
  9958. struct intel_plane *plane;
  9959. const struct intel_plane_state *plane_state;
  9960. int i;
  9961. for_each_new_intel_plane_in_state(state, plane,
  9962. plane_state, i)
  9963. assert_plane(plane, plane_state->base.visible);
  9964. }
  9965. static void
  9966. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9967. struct intel_shared_dpll *pll,
  9968. struct drm_crtc *crtc,
  9969. struct drm_crtc_state *new_state)
  9970. {
  9971. struct intel_dpll_hw_state dpll_hw_state;
  9972. unsigned int crtc_mask;
  9973. bool active;
  9974. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9975. DRM_DEBUG_KMS("%s\n", pll->info->name);
  9976. active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9977. if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
  9978. I915_STATE_WARN(!pll->on && pll->active_mask,
  9979. "pll in active use but not on in sw tracking\n");
  9980. I915_STATE_WARN(pll->on && !pll->active_mask,
  9981. "pll is on but not used by any active crtc\n");
  9982. I915_STATE_WARN(pll->on != active,
  9983. "pll on state mismatch (expected %i, found %i)\n",
  9984. pll->on, active);
  9985. }
  9986. if (!crtc) {
  9987. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9988. "more active pll users than references: %x vs %x\n",
  9989. pll->active_mask, pll->state.crtc_mask);
  9990. return;
  9991. }
  9992. crtc_mask = drm_crtc_mask(crtc);
  9993. if (new_state->active)
  9994. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9995. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9996. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9997. else
  9998. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9999. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10000. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10001. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10002. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10003. crtc_mask, pll->state.crtc_mask);
  10004. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10005. &dpll_hw_state,
  10006. sizeof(dpll_hw_state)),
  10007. "pll hw state mismatch\n");
  10008. }
  10009. static void
  10010. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10011. struct drm_crtc_state *old_crtc_state,
  10012. struct drm_crtc_state *new_crtc_state)
  10013. {
  10014. struct drm_i915_private *dev_priv = to_i915(dev);
  10015. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10016. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10017. if (new_state->shared_dpll)
  10018. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10019. if (old_state->shared_dpll &&
  10020. old_state->shared_dpll != new_state->shared_dpll) {
  10021. unsigned int crtc_mask = drm_crtc_mask(crtc);
  10022. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10023. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10024. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10025. pipe_name(drm_crtc_index(crtc)));
  10026. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10027. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10028. pipe_name(drm_crtc_index(crtc)));
  10029. }
  10030. }
  10031. static void
  10032. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10033. struct drm_atomic_state *state,
  10034. struct drm_crtc_state *old_state,
  10035. struct drm_crtc_state *new_state)
  10036. {
  10037. if (!needs_modeset(new_state) &&
  10038. !to_intel_crtc_state(new_state)->update_pipe)
  10039. return;
  10040. verify_wm_state(crtc, new_state);
  10041. verify_connector_state(crtc->dev, state, crtc);
  10042. verify_crtc_state(crtc, old_state, new_state);
  10043. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10044. }
  10045. static void
  10046. verify_disabled_dpll_state(struct drm_device *dev)
  10047. {
  10048. struct drm_i915_private *dev_priv = to_i915(dev);
  10049. int i;
  10050. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10051. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10052. }
  10053. static void
  10054. intel_modeset_verify_disabled(struct drm_device *dev,
  10055. struct drm_atomic_state *state)
  10056. {
  10057. verify_encoder_state(dev, state);
  10058. verify_connector_state(dev, state, NULL);
  10059. verify_disabled_dpll_state(dev);
  10060. }
  10061. static void update_scanline_offset(struct intel_crtc *crtc)
  10062. {
  10063. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10064. /*
  10065. * The scanline counter increments at the leading edge of hsync.
  10066. *
  10067. * On most platforms it starts counting from vtotal-1 on the
  10068. * first active line. That means the scanline counter value is
  10069. * always one less than what we would expect. Ie. just after
  10070. * start of vblank, which also occurs at start of hsync (on the
  10071. * last active line), the scanline counter will read vblank_start-1.
  10072. *
  10073. * On gen2 the scanline counter starts counting from 1 instead
  10074. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10075. * to keep the value positive), instead of adding one.
  10076. *
  10077. * On HSW+ the behaviour of the scanline counter depends on the output
  10078. * type. For DP ports it behaves like most other platforms, but on HDMI
  10079. * there's an extra 1 line difference. So we need to add two instead of
  10080. * one to the value.
  10081. *
  10082. * On VLV/CHV DSI the scanline counter would appear to increment
  10083. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  10084. * that means we can't tell whether we're in vblank or not while
  10085. * we're on that particular line. We must still set scanline_offset
  10086. * to 1 so that the vblank timestamps come out correct when we query
  10087. * the scanline counter from within the vblank interrupt handler.
  10088. * However if queried just before the start of vblank we'll get an
  10089. * answer that's slightly in the future.
  10090. */
  10091. if (IS_GEN2(dev_priv)) {
  10092. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10093. int vtotal;
  10094. vtotal = adjusted_mode->crtc_vtotal;
  10095. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10096. vtotal /= 2;
  10097. crtc->scanline_offset = vtotal - 1;
  10098. } else if (HAS_DDI(dev_priv) &&
  10099. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10100. crtc->scanline_offset = 2;
  10101. } else
  10102. crtc->scanline_offset = 1;
  10103. }
  10104. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10105. {
  10106. struct drm_device *dev = state->dev;
  10107. struct drm_i915_private *dev_priv = to_i915(dev);
  10108. struct drm_crtc *crtc;
  10109. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10110. int i;
  10111. if (!dev_priv->display.crtc_compute_clock)
  10112. return;
  10113. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10115. struct intel_shared_dpll *old_dpll =
  10116. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10117. if (!needs_modeset(new_crtc_state))
  10118. continue;
  10119. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10120. if (!old_dpll)
  10121. continue;
  10122. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10123. }
  10124. }
  10125. /*
  10126. * This implements the workaround described in the "notes" section of the mode
  10127. * set sequence documentation. When going from no pipes or single pipe to
  10128. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10129. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10130. */
  10131. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10132. {
  10133. struct drm_crtc_state *crtc_state;
  10134. struct intel_crtc *intel_crtc;
  10135. struct drm_crtc *crtc;
  10136. struct intel_crtc_state *first_crtc_state = NULL;
  10137. struct intel_crtc_state *other_crtc_state = NULL;
  10138. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10139. int i;
  10140. /* look at all crtc's that are going to be enabled in during modeset */
  10141. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10142. intel_crtc = to_intel_crtc(crtc);
  10143. if (!crtc_state->active || !needs_modeset(crtc_state))
  10144. continue;
  10145. if (first_crtc_state) {
  10146. other_crtc_state = to_intel_crtc_state(crtc_state);
  10147. break;
  10148. } else {
  10149. first_crtc_state = to_intel_crtc_state(crtc_state);
  10150. first_pipe = intel_crtc->pipe;
  10151. }
  10152. }
  10153. /* No workaround needed? */
  10154. if (!first_crtc_state)
  10155. return 0;
  10156. /* w/a possibly needed, check how many crtc's are already enabled. */
  10157. for_each_intel_crtc(state->dev, intel_crtc) {
  10158. struct intel_crtc_state *pipe_config;
  10159. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10160. if (IS_ERR(pipe_config))
  10161. return PTR_ERR(pipe_config);
  10162. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10163. if (!pipe_config->base.active ||
  10164. needs_modeset(&pipe_config->base))
  10165. continue;
  10166. /* 2 or more enabled crtcs means no need for w/a */
  10167. if (enabled_pipe != INVALID_PIPE)
  10168. return 0;
  10169. enabled_pipe = intel_crtc->pipe;
  10170. }
  10171. if (enabled_pipe != INVALID_PIPE)
  10172. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10173. else if (other_crtc_state)
  10174. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10175. return 0;
  10176. }
  10177. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10178. {
  10179. struct drm_crtc *crtc;
  10180. /* Add all pipes to the state */
  10181. for_each_crtc(state->dev, crtc) {
  10182. struct drm_crtc_state *crtc_state;
  10183. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10184. if (IS_ERR(crtc_state))
  10185. return PTR_ERR(crtc_state);
  10186. }
  10187. return 0;
  10188. }
  10189. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10190. {
  10191. struct drm_crtc *crtc;
  10192. /*
  10193. * Add all pipes to the state, and force
  10194. * a modeset on all the active ones.
  10195. */
  10196. for_each_crtc(state->dev, crtc) {
  10197. struct drm_crtc_state *crtc_state;
  10198. int ret;
  10199. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10200. if (IS_ERR(crtc_state))
  10201. return PTR_ERR(crtc_state);
  10202. if (!crtc_state->active || needs_modeset(crtc_state))
  10203. continue;
  10204. crtc_state->mode_changed = true;
  10205. ret = drm_atomic_add_affected_connectors(state, crtc);
  10206. if (ret)
  10207. return ret;
  10208. ret = drm_atomic_add_affected_planes(state, crtc);
  10209. if (ret)
  10210. return ret;
  10211. }
  10212. return 0;
  10213. }
  10214. static int intel_modeset_checks(struct drm_atomic_state *state)
  10215. {
  10216. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10217. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10218. struct drm_crtc *crtc;
  10219. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10220. int ret = 0, i;
  10221. if (!check_digital_port_conflicts(state)) {
  10222. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10223. return -EINVAL;
  10224. }
  10225. intel_state->modeset = true;
  10226. intel_state->active_crtcs = dev_priv->active_crtcs;
  10227. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10228. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10229. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10230. if (new_crtc_state->active)
  10231. intel_state->active_crtcs |= 1 << i;
  10232. else
  10233. intel_state->active_crtcs &= ~(1 << i);
  10234. if (old_crtc_state->active != new_crtc_state->active)
  10235. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10236. }
  10237. /*
  10238. * See if the config requires any additional preparation, e.g.
  10239. * to adjust global state with pipes off. We need to do this
  10240. * here so we can get the modeset_pipe updated config for the new
  10241. * mode set on this crtc. For other crtcs we need to use the
  10242. * adjusted_mode bits in the crtc directly.
  10243. */
  10244. if (dev_priv->display.modeset_calc_cdclk) {
  10245. ret = dev_priv->display.modeset_calc_cdclk(state);
  10246. if (ret < 0)
  10247. return ret;
  10248. /*
  10249. * Writes to dev_priv->cdclk.logical must protected by
  10250. * holding all the crtc locks, even if we don't end up
  10251. * touching the hardware
  10252. */
  10253. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10254. &intel_state->cdclk.logical)) {
  10255. ret = intel_lock_all_pipes(state);
  10256. if (ret < 0)
  10257. return ret;
  10258. }
  10259. /* All pipes must be switched off while we change the cdclk. */
  10260. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10261. &intel_state->cdclk.actual)) {
  10262. ret = intel_modeset_all_pipes(state);
  10263. if (ret < 0)
  10264. return ret;
  10265. }
  10266. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10267. intel_state->cdclk.logical.cdclk,
  10268. intel_state->cdclk.actual.cdclk);
  10269. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10270. intel_state->cdclk.logical.voltage_level,
  10271. intel_state->cdclk.actual.voltage_level);
  10272. } else {
  10273. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10274. }
  10275. intel_modeset_clear_plls(state);
  10276. if (IS_HASWELL(dev_priv))
  10277. return haswell_mode_set_planes_workaround(state);
  10278. return 0;
  10279. }
  10280. /*
  10281. * Handle calculation of various watermark data at the end of the atomic check
  10282. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10283. * handlers to ensure that all derived state has been updated.
  10284. */
  10285. static int calc_watermark_data(struct drm_atomic_state *state)
  10286. {
  10287. struct drm_device *dev = state->dev;
  10288. struct drm_i915_private *dev_priv = to_i915(dev);
  10289. /* Is there platform-specific watermark information to calculate? */
  10290. if (dev_priv->display.compute_global_watermarks)
  10291. return dev_priv->display.compute_global_watermarks(state);
  10292. return 0;
  10293. }
  10294. /**
  10295. * intel_atomic_check - validate state object
  10296. * @dev: drm device
  10297. * @state: state to validate
  10298. */
  10299. static int intel_atomic_check(struct drm_device *dev,
  10300. struct drm_atomic_state *state)
  10301. {
  10302. struct drm_i915_private *dev_priv = to_i915(dev);
  10303. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10304. struct drm_crtc *crtc;
  10305. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10306. int ret, i;
  10307. bool any_ms = false;
  10308. /* Catch I915_MODE_FLAG_INHERITED */
  10309. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  10310. crtc_state, i) {
  10311. if (crtc_state->mode.private_flags !=
  10312. old_crtc_state->mode.private_flags)
  10313. crtc_state->mode_changed = true;
  10314. }
  10315. ret = drm_atomic_helper_check_modeset(dev, state);
  10316. if (ret)
  10317. return ret;
  10318. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10319. struct intel_crtc_state *pipe_config =
  10320. to_intel_crtc_state(crtc_state);
  10321. if (!needs_modeset(crtc_state))
  10322. continue;
  10323. if (!crtc_state->enable) {
  10324. any_ms = true;
  10325. continue;
  10326. }
  10327. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10328. if (ret) {
  10329. intel_dump_pipe_config(to_intel_crtc(crtc),
  10330. pipe_config, "[failed]");
  10331. return ret;
  10332. }
  10333. if (i915_modparams.fastboot &&
  10334. intel_pipe_config_compare(dev_priv,
  10335. to_intel_crtc_state(old_crtc_state),
  10336. pipe_config, true)) {
  10337. crtc_state->mode_changed = false;
  10338. pipe_config->update_pipe = true;
  10339. }
  10340. if (needs_modeset(crtc_state))
  10341. any_ms = true;
  10342. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10343. needs_modeset(crtc_state) ?
  10344. "[modeset]" : "[fastset]");
  10345. }
  10346. if (any_ms) {
  10347. ret = intel_modeset_checks(state);
  10348. if (ret)
  10349. return ret;
  10350. } else {
  10351. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10352. }
  10353. ret = drm_atomic_helper_check_planes(dev, state);
  10354. if (ret)
  10355. return ret;
  10356. intel_fbc_choose_crtc(dev_priv, intel_state);
  10357. return calc_watermark_data(state);
  10358. }
  10359. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10360. struct drm_atomic_state *state)
  10361. {
  10362. return drm_atomic_helper_prepare_planes(dev, state);
  10363. }
  10364. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10365. {
  10366. struct drm_device *dev = crtc->base.dev;
  10367. if (!dev->max_vblank_count)
  10368. return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
  10369. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10370. }
  10371. static void intel_update_crtc(struct drm_crtc *crtc,
  10372. struct drm_atomic_state *state,
  10373. struct drm_crtc_state *old_crtc_state,
  10374. struct drm_crtc_state *new_crtc_state)
  10375. {
  10376. struct drm_device *dev = crtc->dev;
  10377. struct drm_i915_private *dev_priv = to_i915(dev);
  10378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10379. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10380. bool modeset = needs_modeset(new_crtc_state);
  10381. struct intel_plane_state *new_plane_state =
  10382. intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
  10383. to_intel_plane(crtc->primary));
  10384. if (modeset) {
  10385. update_scanline_offset(intel_crtc);
  10386. dev_priv->display.crtc_enable(pipe_config, state);
  10387. /* vblanks work again, re-enable pipe CRC. */
  10388. intel_crtc_enable_pipe_crc(intel_crtc);
  10389. } else {
  10390. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10391. pipe_config);
  10392. }
  10393. if (new_plane_state)
  10394. intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
  10395. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10396. }
  10397. static void intel_update_crtcs(struct drm_atomic_state *state)
  10398. {
  10399. struct drm_crtc *crtc;
  10400. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10401. int i;
  10402. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10403. if (!new_crtc_state->active)
  10404. continue;
  10405. intel_update_crtc(crtc, state, old_crtc_state,
  10406. new_crtc_state);
  10407. }
  10408. }
  10409. static void skl_update_crtcs(struct drm_atomic_state *state)
  10410. {
  10411. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10412. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10413. struct drm_crtc *crtc;
  10414. struct intel_crtc *intel_crtc;
  10415. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10416. struct intel_crtc_state *cstate;
  10417. unsigned int updated = 0;
  10418. bool progress;
  10419. enum pipe pipe;
  10420. int i;
  10421. u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
  10422. u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
  10423. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10424. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10425. /* ignore allocations for crtc's that have been turned off. */
  10426. if (new_crtc_state->active)
  10427. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10428. /* If 2nd DBuf slice required, enable it here */
  10429. if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
  10430. icl_dbuf_slices_update(dev_priv, required_slices);
  10431. /*
  10432. * Whenever the number of active pipes changes, we need to make sure we
  10433. * update the pipes in the right order so that their ddb allocations
  10434. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10435. * cause pipe underruns and other bad stuff.
  10436. */
  10437. do {
  10438. progress = false;
  10439. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10440. bool vbl_wait = false;
  10441. unsigned int cmask = drm_crtc_mask(crtc);
  10442. intel_crtc = to_intel_crtc(crtc);
  10443. cstate = to_intel_crtc_state(new_crtc_state);
  10444. pipe = intel_crtc->pipe;
  10445. if (updated & cmask || !cstate->base.active)
  10446. continue;
  10447. if (skl_ddb_allocation_overlaps(dev_priv,
  10448. entries,
  10449. &cstate->wm.skl.ddb,
  10450. i))
  10451. continue;
  10452. updated |= cmask;
  10453. entries[i] = &cstate->wm.skl.ddb;
  10454. /*
  10455. * If this is an already active pipe, it's DDB changed,
  10456. * and this isn't the last pipe that needs updating
  10457. * then we need to wait for a vblank to pass for the
  10458. * new ddb allocation to take effect.
  10459. */
  10460. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10461. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10462. !new_crtc_state->active_changed &&
  10463. intel_state->wm_results.dirty_pipes != updated)
  10464. vbl_wait = true;
  10465. intel_update_crtc(crtc, state, old_crtc_state,
  10466. new_crtc_state);
  10467. if (vbl_wait)
  10468. intel_wait_for_vblank(dev_priv, pipe);
  10469. progress = true;
  10470. }
  10471. } while (progress);
  10472. /* If 2nd DBuf slice is no more required disable it */
  10473. if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
  10474. icl_dbuf_slices_update(dev_priv, required_slices);
  10475. }
  10476. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10477. {
  10478. struct intel_atomic_state *state, *next;
  10479. struct llist_node *freed;
  10480. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10481. llist_for_each_entry_safe(state, next, freed, freed)
  10482. drm_atomic_state_put(&state->base);
  10483. }
  10484. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10485. {
  10486. struct drm_i915_private *dev_priv =
  10487. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10488. intel_atomic_helper_free_state(dev_priv);
  10489. }
  10490. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10491. {
  10492. struct wait_queue_entry wait_fence, wait_reset;
  10493. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10494. init_wait_entry(&wait_fence, 0);
  10495. init_wait_entry(&wait_reset, 0);
  10496. for (;;) {
  10497. prepare_to_wait(&intel_state->commit_ready.wait,
  10498. &wait_fence, TASK_UNINTERRUPTIBLE);
  10499. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10500. &wait_reset, TASK_UNINTERRUPTIBLE);
  10501. if (i915_sw_fence_done(&intel_state->commit_ready)
  10502. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10503. break;
  10504. schedule();
  10505. }
  10506. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10507. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10508. }
  10509. static void intel_atomic_cleanup_work(struct work_struct *work)
  10510. {
  10511. struct drm_atomic_state *state =
  10512. container_of(work, struct drm_atomic_state, commit_work);
  10513. struct drm_i915_private *i915 = to_i915(state->dev);
  10514. drm_atomic_helper_cleanup_planes(&i915->drm, state);
  10515. drm_atomic_helper_commit_cleanup_done(state);
  10516. drm_atomic_state_put(state);
  10517. intel_atomic_helper_free_state(i915);
  10518. }
  10519. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10520. {
  10521. struct drm_device *dev = state->dev;
  10522. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10523. struct drm_i915_private *dev_priv = to_i915(dev);
  10524. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10525. struct drm_crtc *crtc;
  10526. struct intel_crtc_state *intel_cstate;
  10527. u64 put_domains[I915_MAX_PIPES] = {};
  10528. int i;
  10529. intel_atomic_commit_fence_wait(intel_state);
  10530. drm_atomic_helper_wait_for_dependencies(state);
  10531. if (intel_state->modeset)
  10532. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10533. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10534. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10535. if (needs_modeset(new_crtc_state) ||
  10536. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10537. put_domains[to_intel_crtc(crtc)->pipe] =
  10538. modeset_get_crtc_power_domains(crtc,
  10539. to_intel_crtc_state(new_crtc_state));
  10540. }
  10541. if (!needs_modeset(new_crtc_state))
  10542. continue;
  10543. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10544. to_intel_crtc_state(new_crtc_state));
  10545. if (old_crtc_state->active) {
  10546. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10547. /*
  10548. * We need to disable pipe CRC before disabling the pipe,
  10549. * or we race against vblank off.
  10550. */
  10551. intel_crtc_disable_pipe_crc(intel_crtc);
  10552. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10553. intel_crtc->active = false;
  10554. intel_fbc_disable(intel_crtc);
  10555. intel_disable_shared_dpll(intel_crtc);
  10556. /*
  10557. * Underruns don't always raise
  10558. * interrupts, so check manually.
  10559. */
  10560. intel_check_cpu_fifo_underruns(dev_priv);
  10561. intel_check_pch_fifo_underruns(dev_priv);
  10562. /* FIXME unify this for all platforms */
  10563. if (!new_crtc_state->active &&
  10564. !HAS_GMCH_DISPLAY(dev_priv) &&
  10565. dev_priv->display.initial_watermarks)
  10566. dev_priv->display.initial_watermarks(intel_state,
  10567. to_intel_crtc_state(new_crtc_state));
  10568. }
  10569. }
  10570. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10571. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10572. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10573. if (intel_state->modeset) {
  10574. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10575. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10576. /*
  10577. * SKL workaround: bspec recommends we disable the SAGV when we
  10578. * have more then one pipe enabled
  10579. */
  10580. if (!intel_can_enable_sagv(state))
  10581. intel_disable_sagv(dev_priv);
  10582. intel_modeset_verify_disabled(dev, state);
  10583. }
  10584. /* Complete the events for pipes that have now been disabled */
  10585. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10586. bool modeset = needs_modeset(new_crtc_state);
  10587. /* Complete events for now disable pipes here. */
  10588. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10589. spin_lock_irq(&dev->event_lock);
  10590. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10591. spin_unlock_irq(&dev->event_lock);
  10592. new_crtc_state->event = NULL;
  10593. }
  10594. }
  10595. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10596. dev_priv->display.update_crtcs(state);
  10597. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10598. * already, but still need the state for the delayed optimization. To
  10599. * fix this:
  10600. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10601. * - schedule that vblank worker _before_ calling hw_done
  10602. * - at the start of commit_tail, cancel it _synchrously
  10603. * - switch over to the vblank wait helper in the core after that since
  10604. * we don't need out special handling any more.
  10605. */
  10606. drm_atomic_helper_wait_for_flip_done(dev, state);
  10607. /*
  10608. * Now that the vblank has passed, we can go ahead and program the
  10609. * optimal watermarks on platforms that need two-step watermark
  10610. * programming.
  10611. *
  10612. * TODO: Move this (and other cleanup) to an async worker eventually.
  10613. */
  10614. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10615. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10616. if (dev_priv->display.optimize_watermarks)
  10617. dev_priv->display.optimize_watermarks(intel_state,
  10618. intel_cstate);
  10619. }
  10620. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10621. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10622. if (put_domains[i])
  10623. modeset_put_power_domains(dev_priv, put_domains[i]);
  10624. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10625. }
  10626. if (intel_state->modeset)
  10627. intel_verify_planes(intel_state);
  10628. if (intel_state->modeset && intel_can_enable_sagv(state))
  10629. intel_enable_sagv(dev_priv);
  10630. drm_atomic_helper_commit_hw_done(state);
  10631. if (intel_state->modeset) {
  10632. /* As one of the primary mmio accessors, KMS has a high
  10633. * likelihood of triggering bugs in unclaimed access. After we
  10634. * finish modesetting, see if an error has been flagged, and if
  10635. * so enable debugging for the next modeset - and hope we catch
  10636. * the culprit.
  10637. */
  10638. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10639. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10640. }
  10641. /*
  10642. * Defer the cleanup of the old state to a separate worker to not
  10643. * impede the current task (userspace for blocking modesets) that
  10644. * are executed inline. For out-of-line asynchronous modesets/flips,
  10645. * deferring to a new worker seems overkill, but we would place a
  10646. * schedule point (cond_resched()) here anyway to keep latencies
  10647. * down.
  10648. */
  10649. INIT_WORK(&state->commit_work, intel_atomic_cleanup_work);
  10650. schedule_work(&state->commit_work);
  10651. }
  10652. static void intel_atomic_commit_work(struct work_struct *work)
  10653. {
  10654. struct drm_atomic_state *state =
  10655. container_of(work, struct drm_atomic_state, commit_work);
  10656. intel_atomic_commit_tail(state);
  10657. }
  10658. static int __i915_sw_fence_call
  10659. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10660. enum i915_sw_fence_notify notify)
  10661. {
  10662. struct intel_atomic_state *state =
  10663. container_of(fence, struct intel_atomic_state, commit_ready);
  10664. switch (notify) {
  10665. case FENCE_COMPLETE:
  10666. /* we do blocking waits in the worker, nothing to do here */
  10667. break;
  10668. case FENCE_FREE:
  10669. {
  10670. struct intel_atomic_helper *helper =
  10671. &to_i915(state->base.dev)->atomic_helper;
  10672. if (llist_add(&state->freed, &helper->free_list))
  10673. schedule_work(&helper->free_work);
  10674. break;
  10675. }
  10676. }
  10677. return NOTIFY_DONE;
  10678. }
  10679. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10680. {
  10681. struct drm_plane_state *old_plane_state, *new_plane_state;
  10682. struct drm_plane *plane;
  10683. int i;
  10684. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10685. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10686. intel_fb_obj(new_plane_state->fb),
  10687. to_intel_plane(plane)->frontbuffer_bit);
  10688. }
  10689. /**
  10690. * intel_atomic_commit - commit validated state object
  10691. * @dev: DRM device
  10692. * @state: the top-level driver state object
  10693. * @nonblock: nonblocking commit
  10694. *
  10695. * This function commits a top-level state object that has been validated
  10696. * with drm_atomic_helper_check().
  10697. *
  10698. * RETURNS
  10699. * Zero for success or -errno.
  10700. */
  10701. static int intel_atomic_commit(struct drm_device *dev,
  10702. struct drm_atomic_state *state,
  10703. bool nonblock)
  10704. {
  10705. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10706. struct drm_i915_private *dev_priv = to_i915(dev);
  10707. int ret = 0;
  10708. drm_atomic_state_get(state);
  10709. i915_sw_fence_init(&intel_state->commit_ready,
  10710. intel_atomic_commit_ready);
  10711. /*
  10712. * The intel_legacy_cursor_update() fast path takes care
  10713. * of avoiding the vblank waits for simple cursor
  10714. * movement and flips. For cursor on/off and size changes,
  10715. * we want to perform the vblank waits so that watermark
  10716. * updates happen during the correct frames. Gen9+ have
  10717. * double buffered watermarks and so shouldn't need this.
  10718. *
  10719. * Unset state->legacy_cursor_update before the call to
  10720. * drm_atomic_helper_setup_commit() because otherwise
  10721. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10722. * we get FIFO underruns because we didn't wait
  10723. * for vblank.
  10724. *
  10725. * FIXME doing watermarks and fb cleanup from a vblank worker
  10726. * (assuming we had any) would solve these problems.
  10727. */
  10728. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10729. struct intel_crtc_state *new_crtc_state;
  10730. struct intel_crtc *crtc;
  10731. int i;
  10732. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10733. if (new_crtc_state->wm.need_postvbl_update ||
  10734. new_crtc_state->update_wm_post)
  10735. state->legacy_cursor_update = false;
  10736. }
  10737. ret = intel_atomic_prepare_commit(dev, state);
  10738. if (ret) {
  10739. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10740. i915_sw_fence_commit(&intel_state->commit_ready);
  10741. return ret;
  10742. }
  10743. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10744. if (!ret)
  10745. ret = drm_atomic_helper_swap_state(state, true);
  10746. if (ret) {
  10747. i915_sw_fence_commit(&intel_state->commit_ready);
  10748. drm_atomic_helper_cleanup_planes(dev, state);
  10749. return ret;
  10750. }
  10751. dev_priv->wm.distrust_bios_wm = false;
  10752. intel_shared_dpll_swap_state(state);
  10753. intel_atomic_track_fbs(state);
  10754. if (intel_state->modeset) {
  10755. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10756. sizeof(intel_state->min_cdclk));
  10757. memcpy(dev_priv->min_voltage_level,
  10758. intel_state->min_voltage_level,
  10759. sizeof(intel_state->min_voltage_level));
  10760. dev_priv->active_crtcs = intel_state->active_crtcs;
  10761. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10762. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10763. }
  10764. drm_atomic_state_get(state);
  10765. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10766. i915_sw_fence_commit(&intel_state->commit_ready);
  10767. if (nonblock && intel_state->modeset) {
  10768. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10769. } else if (nonblock) {
  10770. queue_work(system_unbound_wq, &state->commit_work);
  10771. } else {
  10772. if (intel_state->modeset)
  10773. flush_workqueue(dev_priv->modeset_wq);
  10774. intel_atomic_commit_tail(state);
  10775. }
  10776. return 0;
  10777. }
  10778. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10779. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10780. .set_config = drm_atomic_helper_set_config,
  10781. .destroy = intel_crtc_destroy,
  10782. .page_flip = drm_atomic_helper_page_flip,
  10783. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10784. .atomic_destroy_state = intel_crtc_destroy_state,
  10785. .set_crc_source = intel_crtc_set_crc_source,
  10786. };
  10787. struct wait_rps_boost {
  10788. struct wait_queue_entry wait;
  10789. struct drm_crtc *crtc;
  10790. struct i915_request *request;
  10791. };
  10792. static int do_rps_boost(struct wait_queue_entry *_wait,
  10793. unsigned mode, int sync, void *key)
  10794. {
  10795. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10796. struct i915_request *rq = wait->request;
  10797. /*
  10798. * If we missed the vblank, but the request is already running it
  10799. * is reasonable to assume that it will complete before the next
  10800. * vblank without our intervention, so leave RPS alone.
  10801. */
  10802. if (!i915_request_started(rq))
  10803. gen6_rps_boost(rq, NULL);
  10804. i915_request_put(rq);
  10805. drm_crtc_vblank_put(wait->crtc);
  10806. list_del(&wait->wait.entry);
  10807. kfree(wait);
  10808. return 1;
  10809. }
  10810. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10811. struct dma_fence *fence)
  10812. {
  10813. struct wait_rps_boost *wait;
  10814. if (!dma_fence_is_i915(fence))
  10815. return;
  10816. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10817. return;
  10818. if (drm_crtc_vblank_get(crtc))
  10819. return;
  10820. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10821. if (!wait) {
  10822. drm_crtc_vblank_put(crtc);
  10823. return;
  10824. }
  10825. wait->request = to_request(dma_fence_get(fence));
  10826. wait->crtc = crtc;
  10827. wait->wait.func = do_rps_boost;
  10828. wait->wait.flags = 0;
  10829. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10830. }
  10831. static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
  10832. {
  10833. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  10834. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10835. struct drm_framebuffer *fb = plane_state->base.fb;
  10836. struct i915_vma *vma;
  10837. if (plane->id == PLANE_CURSOR &&
  10838. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10839. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10840. const int align = intel_cursor_alignment(dev_priv);
  10841. return i915_gem_object_attach_phys(obj, align);
  10842. }
  10843. vma = intel_pin_and_fence_fb_obj(fb,
  10844. plane_state->base.rotation,
  10845. intel_plane_uses_fence(plane_state),
  10846. &plane_state->flags);
  10847. if (IS_ERR(vma))
  10848. return PTR_ERR(vma);
  10849. plane_state->vma = vma;
  10850. return 0;
  10851. }
  10852. static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
  10853. {
  10854. struct i915_vma *vma;
  10855. vma = fetch_and_zero(&old_plane_state->vma);
  10856. if (vma)
  10857. intel_unpin_fb_vma(vma, old_plane_state->flags);
  10858. }
  10859. static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
  10860. {
  10861. struct i915_sched_attr attr = {
  10862. .priority = I915_PRIORITY_DISPLAY,
  10863. };
  10864. i915_gem_object_wait_priority(obj, 0, &attr);
  10865. }
  10866. /**
  10867. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10868. * @plane: drm plane to prepare for
  10869. * @new_state: the plane state being prepared
  10870. *
  10871. * Prepares a framebuffer for usage on a display plane. Generally this
  10872. * involves pinning the underlying object and updating the frontbuffer tracking
  10873. * bits. Some older platforms need special physical address handling for
  10874. * cursor planes.
  10875. *
  10876. * Must be called with struct_mutex held.
  10877. *
  10878. * Returns 0 on success, negative error code on failure.
  10879. */
  10880. int
  10881. intel_prepare_plane_fb(struct drm_plane *plane,
  10882. struct drm_plane_state *new_state)
  10883. {
  10884. struct intel_atomic_state *intel_state =
  10885. to_intel_atomic_state(new_state->state);
  10886. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10887. struct drm_framebuffer *fb = new_state->fb;
  10888. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10889. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10890. int ret;
  10891. if (old_obj) {
  10892. struct drm_crtc_state *crtc_state =
  10893. drm_atomic_get_new_crtc_state(new_state->state,
  10894. plane->state->crtc);
  10895. /* Big Hammer, we also need to ensure that any pending
  10896. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10897. * current scanout is retired before unpinning the old
  10898. * framebuffer. Note that we rely on userspace rendering
  10899. * into the buffer attached to the pipe they are waiting
  10900. * on. If not, userspace generates a GPU hang with IPEHR
  10901. * point to the MI_WAIT_FOR_EVENT.
  10902. *
  10903. * This should only fail upon a hung GPU, in which case we
  10904. * can safely continue.
  10905. */
  10906. if (needs_modeset(crtc_state)) {
  10907. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10908. old_obj->resv, NULL,
  10909. false, 0,
  10910. GFP_KERNEL);
  10911. if (ret < 0)
  10912. return ret;
  10913. }
  10914. }
  10915. if (new_state->fence) { /* explicit fencing */
  10916. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10917. new_state->fence,
  10918. I915_FENCE_TIMEOUT,
  10919. GFP_KERNEL);
  10920. if (ret < 0)
  10921. return ret;
  10922. }
  10923. if (!obj)
  10924. return 0;
  10925. ret = i915_gem_object_pin_pages(obj);
  10926. if (ret)
  10927. return ret;
  10928. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10929. if (ret) {
  10930. i915_gem_object_unpin_pages(obj);
  10931. return ret;
  10932. }
  10933. ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
  10934. fb_obj_bump_render_priority(obj);
  10935. mutex_unlock(&dev_priv->drm.struct_mutex);
  10936. i915_gem_object_unpin_pages(obj);
  10937. if (ret)
  10938. return ret;
  10939. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  10940. if (!new_state->fence) { /* implicit fencing */
  10941. struct dma_fence *fence;
  10942. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10943. obj->resv, NULL,
  10944. false, I915_FENCE_TIMEOUT,
  10945. GFP_KERNEL);
  10946. if (ret < 0)
  10947. return ret;
  10948. fence = reservation_object_get_excl_rcu(obj->resv);
  10949. if (fence) {
  10950. add_rps_boost_after_vblank(new_state->crtc, fence);
  10951. dma_fence_put(fence);
  10952. }
  10953. } else {
  10954. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10955. }
  10956. /*
  10957. * We declare pageflips to be interactive and so merit a small bias
  10958. * towards upclocking to deliver the frame on time. By only changing
  10959. * the RPS thresholds to sample more regularly and aim for higher
  10960. * clocks we can hopefully deliver low power workloads (like kodi)
  10961. * that are not quite steady state without resorting to forcing
  10962. * maximum clocks following a vblank miss (see do_rps_boost()).
  10963. */
  10964. if (!intel_state->rps_interactive) {
  10965. intel_rps_mark_interactive(dev_priv, true);
  10966. intel_state->rps_interactive = true;
  10967. }
  10968. return 0;
  10969. }
  10970. /**
  10971. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10972. * @plane: drm plane to clean up for
  10973. * @old_state: the state from the previous modeset
  10974. *
  10975. * Cleans up a framebuffer that has just been removed from a plane.
  10976. *
  10977. * Must be called with struct_mutex held.
  10978. */
  10979. void
  10980. intel_cleanup_plane_fb(struct drm_plane *plane,
  10981. struct drm_plane_state *old_state)
  10982. {
  10983. struct intel_atomic_state *intel_state =
  10984. to_intel_atomic_state(old_state->state);
  10985. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10986. if (intel_state->rps_interactive) {
  10987. intel_rps_mark_interactive(dev_priv, false);
  10988. intel_state->rps_interactive = false;
  10989. }
  10990. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10991. mutex_lock(&dev_priv->drm.struct_mutex);
  10992. intel_plane_unpin_fb(to_intel_plane_state(old_state));
  10993. mutex_unlock(&dev_priv->drm.struct_mutex);
  10994. }
  10995. int
  10996. skl_max_scale(struct intel_crtc *intel_crtc,
  10997. struct intel_crtc_state *crtc_state,
  10998. uint32_t pixel_format)
  10999. {
  11000. struct drm_i915_private *dev_priv;
  11001. int max_scale, mult;
  11002. int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
  11003. if (!intel_crtc || !crtc_state->base.enable)
  11004. return DRM_PLANE_HELPER_NO_SCALING;
  11005. dev_priv = to_i915(intel_crtc->base.dev);
  11006. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11007. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11008. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  11009. max_dotclk *= 2;
  11010. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11011. return DRM_PLANE_HELPER_NO_SCALING;
  11012. /*
  11013. * skl max scale is lower of:
  11014. * close to 3 but not 3, -1 is for that purpose
  11015. * or
  11016. * cdclk/crtc_clock
  11017. */
  11018. mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
  11019. tmpclk1 = (1 << 16) * mult - 1;
  11020. tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
  11021. max_scale = min(tmpclk1, tmpclk2);
  11022. return max_scale;
  11023. }
  11024. static int
  11025. intel_check_primary_plane(struct intel_plane *plane,
  11026. struct intel_crtc_state *crtc_state,
  11027. struct intel_plane_state *state)
  11028. {
  11029. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  11030. struct drm_crtc *crtc = state->base.crtc;
  11031. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11032. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11033. bool can_position = false;
  11034. int ret;
  11035. uint32_t pixel_format = 0;
  11036. if (INTEL_GEN(dev_priv) >= 9) {
  11037. /* use scaler when colorkey is not required */
  11038. if (!state->ckey.flags) {
  11039. min_scale = 1;
  11040. if (state->base.fb)
  11041. pixel_format = state->base.fb->format->format;
  11042. max_scale = skl_max_scale(to_intel_crtc(crtc),
  11043. crtc_state, pixel_format);
  11044. }
  11045. can_position = true;
  11046. }
  11047. ret = drm_atomic_helper_check_plane_state(&state->base,
  11048. &crtc_state->base,
  11049. min_scale, max_scale,
  11050. can_position, true);
  11051. if (ret)
  11052. return ret;
  11053. if (!state->base.fb)
  11054. return 0;
  11055. if (INTEL_GEN(dev_priv) >= 9) {
  11056. ret = skl_check_plane_surface(crtc_state, state);
  11057. if (ret)
  11058. return ret;
  11059. state->ctl = skl_plane_ctl(crtc_state, state);
  11060. } else {
  11061. ret = i9xx_check_plane_surface(state);
  11062. if (ret)
  11063. return ret;
  11064. state->ctl = i9xx_plane_ctl(crtc_state, state);
  11065. }
  11066. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  11067. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  11068. return 0;
  11069. }
  11070. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11071. struct drm_crtc_state *old_crtc_state)
  11072. {
  11073. struct drm_device *dev = crtc->dev;
  11074. struct drm_i915_private *dev_priv = to_i915(dev);
  11075. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11076. struct intel_crtc_state *old_intel_cstate =
  11077. to_intel_crtc_state(old_crtc_state);
  11078. struct intel_atomic_state *old_intel_state =
  11079. to_intel_atomic_state(old_crtc_state->state);
  11080. struct intel_crtc_state *intel_cstate =
  11081. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  11082. bool modeset = needs_modeset(&intel_cstate->base);
  11083. if (!modeset &&
  11084. (intel_cstate->base.color_mgmt_changed ||
  11085. intel_cstate->update_pipe)) {
  11086. intel_color_set_csc(&intel_cstate->base);
  11087. intel_color_load_luts(&intel_cstate->base);
  11088. }
  11089. /* Perform vblank evasion around commit operation */
  11090. intel_pipe_update_start(intel_cstate);
  11091. if (modeset)
  11092. goto out;
  11093. if (intel_cstate->update_pipe)
  11094. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  11095. else if (INTEL_GEN(dev_priv) >= 9)
  11096. skl_detach_scalers(intel_crtc);
  11097. out:
  11098. if (dev_priv->display.atomic_update_watermarks)
  11099. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11100. intel_cstate);
  11101. }
  11102. void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
  11103. struct intel_crtc_state *crtc_state)
  11104. {
  11105. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11106. if (!IS_GEN2(dev_priv))
  11107. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
  11108. if (crtc_state->has_pch_encoder) {
  11109. enum pipe pch_transcoder =
  11110. intel_crtc_pch_transcoder(crtc);
  11111. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  11112. }
  11113. }
  11114. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11115. struct drm_crtc_state *old_crtc_state)
  11116. {
  11117. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11118. struct intel_atomic_state *old_intel_state =
  11119. to_intel_atomic_state(old_crtc_state->state);
  11120. struct intel_crtc_state *new_crtc_state =
  11121. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  11122. intel_pipe_update_end(new_crtc_state);
  11123. if (new_crtc_state->update_pipe &&
  11124. !needs_modeset(&new_crtc_state->base) &&
  11125. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
  11126. intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
  11127. }
  11128. /**
  11129. * intel_plane_destroy - destroy a plane
  11130. * @plane: plane to destroy
  11131. *
  11132. * Common destruction function for all types of planes (primary, cursor,
  11133. * sprite).
  11134. */
  11135. void intel_plane_destroy(struct drm_plane *plane)
  11136. {
  11137. drm_plane_cleanup(plane);
  11138. kfree(to_intel_plane(plane));
  11139. }
  11140. static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
  11141. u32 format, u64 modifier)
  11142. {
  11143. switch (modifier) {
  11144. case DRM_FORMAT_MOD_LINEAR:
  11145. case I915_FORMAT_MOD_X_TILED:
  11146. break;
  11147. default:
  11148. return false;
  11149. }
  11150. switch (format) {
  11151. case DRM_FORMAT_C8:
  11152. case DRM_FORMAT_RGB565:
  11153. case DRM_FORMAT_XRGB1555:
  11154. case DRM_FORMAT_XRGB8888:
  11155. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11156. modifier == I915_FORMAT_MOD_X_TILED;
  11157. default:
  11158. return false;
  11159. }
  11160. }
  11161. static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
  11162. u32 format, u64 modifier)
  11163. {
  11164. switch (modifier) {
  11165. case DRM_FORMAT_MOD_LINEAR:
  11166. case I915_FORMAT_MOD_X_TILED:
  11167. break;
  11168. default:
  11169. return false;
  11170. }
  11171. switch (format) {
  11172. case DRM_FORMAT_C8:
  11173. case DRM_FORMAT_RGB565:
  11174. case DRM_FORMAT_XRGB8888:
  11175. case DRM_FORMAT_XBGR8888:
  11176. case DRM_FORMAT_XRGB2101010:
  11177. case DRM_FORMAT_XBGR2101010:
  11178. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11179. modifier == I915_FORMAT_MOD_X_TILED;
  11180. default:
  11181. return false;
  11182. }
  11183. }
  11184. static bool skl_plane_format_mod_supported(struct drm_plane *_plane,
  11185. u32 format, u64 modifier)
  11186. {
  11187. struct intel_plane *plane = to_intel_plane(_plane);
  11188. switch (modifier) {
  11189. case DRM_FORMAT_MOD_LINEAR:
  11190. case I915_FORMAT_MOD_X_TILED:
  11191. case I915_FORMAT_MOD_Y_TILED:
  11192. case I915_FORMAT_MOD_Yf_TILED:
  11193. break;
  11194. case I915_FORMAT_MOD_Y_TILED_CCS:
  11195. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11196. if (!plane->has_ccs)
  11197. return false;
  11198. break;
  11199. default:
  11200. return false;
  11201. }
  11202. switch (format) {
  11203. case DRM_FORMAT_XRGB8888:
  11204. case DRM_FORMAT_XBGR8888:
  11205. case DRM_FORMAT_ARGB8888:
  11206. case DRM_FORMAT_ABGR8888:
  11207. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  11208. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  11209. return true;
  11210. /* fall through */
  11211. case DRM_FORMAT_RGB565:
  11212. case DRM_FORMAT_XRGB2101010:
  11213. case DRM_FORMAT_XBGR2101010:
  11214. case DRM_FORMAT_YUYV:
  11215. case DRM_FORMAT_YVYU:
  11216. case DRM_FORMAT_UYVY:
  11217. case DRM_FORMAT_VYUY:
  11218. case DRM_FORMAT_NV12:
  11219. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  11220. return true;
  11221. /* fall through */
  11222. case DRM_FORMAT_C8:
  11223. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  11224. modifier == I915_FORMAT_MOD_X_TILED ||
  11225. modifier == I915_FORMAT_MOD_Y_TILED)
  11226. return true;
  11227. /* fall through */
  11228. default:
  11229. return false;
  11230. }
  11231. }
  11232. static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
  11233. u32 format, u64 modifier)
  11234. {
  11235. return modifier == DRM_FORMAT_MOD_LINEAR &&
  11236. format == DRM_FORMAT_ARGB8888;
  11237. }
  11238. static struct drm_plane_funcs skl_plane_funcs = {
  11239. .update_plane = drm_atomic_helper_update_plane,
  11240. .disable_plane = drm_atomic_helper_disable_plane,
  11241. .destroy = intel_plane_destroy,
  11242. .atomic_get_property = intel_plane_atomic_get_property,
  11243. .atomic_set_property = intel_plane_atomic_set_property,
  11244. .atomic_duplicate_state = intel_plane_duplicate_state,
  11245. .atomic_destroy_state = intel_plane_destroy_state,
  11246. .format_mod_supported = skl_plane_format_mod_supported,
  11247. };
  11248. static struct drm_plane_funcs i965_plane_funcs = {
  11249. .update_plane = drm_atomic_helper_update_plane,
  11250. .disable_plane = drm_atomic_helper_disable_plane,
  11251. .destroy = intel_plane_destroy,
  11252. .atomic_get_property = intel_plane_atomic_get_property,
  11253. .atomic_set_property = intel_plane_atomic_set_property,
  11254. .atomic_duplicate_state = intel_plane_duplicate_state,
  11255. .atomic_destroy_state = intel_plane_destroy_state,
  11256. .format_mod_supported = i965_plane_format_mod_supported,
  11257. };
  11258. static struct drm_plane_funcs i8xx_plane_funcs = {
  11259. .update_plane = drm_atomic_helper_update_plane,
  11260. .disable_plane = drm_atomic_helper_disable_plane,
  11261. .destroy = intel_plane_destroy,
  11262. .atomic_get_property = intel_plane_atomic_get_property,
  11263. .atomic_set_property = intel_plane_atomic_set_property,
  11264. .atomic_duplicate_state = intel_plane_duplicate_state,
  11265. .atomic_destroy_state = intel_plane_destroy_state,
  11266. .format_mod_supported = i8xx_plane_format_mod_supported,
  11267. };
  11268. static int
  11269. intel_legacy_cursor_update(struct drm_plane *plane,
  11270. struct drm_crtc *crtc,
  11271. struct drm_framebuffer *fb,
  11272. int crtc_x, int crtc_y,
  11273. unsigned int crtc_w, unsigned int crtc_h,
  11274. uint32_t src_x, uint32_t src_y,
  11275. uint32_t src_w, uint32_t src_h,
  11276. struct drm_modeset_acquire_ctx *ctx)
  11277. {
  11278. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11279. int ret;
  11280. struct drm_plane_state *old_plane_state, *new_plane_state;
  11281. struct intel_plane *intel_plane = to_intel_plane(plane);
  11282. struct drm_framebuffer *old_fb;
  11283. struct drm_crtc_state *crtc_state = crtc->state;
  11284. /*
  11285. * When crtc is inactive or there is a modeset pending,
  11286. * wait for it to complete in the slowpath
  11287. */
  11288. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11289. to_intel_crtc_state(crtc_state)->update_pipe)
  11290. goto slow;
  11291. old_plane_state = plane->state;
  11292. /*
  11293. * Don't do an async update if there is an outstanding commit modifying
  11294. * the plane. This prevents our async update's changes from getting
  11295. * overridden by a previous synchronous update's state.
  11296. */
  11297. if (old_plane_state->commit &&
  11298. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  11299. goto slow;
  11300. /*
  11301. * If any parameters change that may affect watermarks,
  11302. * take the slowpath. Only changing fb or position should be
  11303. * in the fastpath.
  11304. */
  11305. if (old_plane_state->crtc != crtc ||
  11306. old_plane_state->src_w != src_w ||
  11307. old_plane_state->src_h != src_h ||
  11308. old_plane_state->crtc_w != crtc_w ||
  11309. old_plane_state->crtc_h != crtc_h ||
  11310. !old_plane_state->fb != !fb)
  11311. goto slow;
  11312. new_plane_state = intel_plane_duplicate_state(plane);
  11313. if (!new_plane_state)
  11314. return -ENOMEM;
  11315. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11316. new_plane_state->src_x = src_x;
  11317. new_plane_state->src_y = src_y;
  11318. new_plane_state->src_w = src_w;
  11319. new_plane_state->src_h = src_h;
  11320. new_plane_state->crtc_x = crtc_x;
  11321. new_plane_state->crtc_y = crtc_y;
  11322. new_plane_state->crtc_w = crtc_w;
  11323. new_plane_state->crtc_h = crtc_h;
  11324. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11325. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  11326. to_intel_plane_state(plane->state),
  11327. to_intel_plane_state(new_plane_state));
  11328. if (ret)
  11329. goto out_free;
  11330. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11331. if (ret)
  11332. goto out_free;
  11333. ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
  11334. if (ret)
  11335. goto out_unlock;
  11336. intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
  11337. old_fb = old_plane_state->fb;
  11338. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11339. intel_plane->frontbuffer_bit);
  11340. /* Swap plane state */
  11341. plane->state = new_plane_state;
  11342. if (plane->state->visible) {
  11343. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11344. intel_plane->update_plane(intel_plane,
  11345. to_intel_crtc_state(crtc->state),
  11346. to_intel_plane_state(plane->state));
  11347. } else {
  11348. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11349. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11350. }
  11351. intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
  11352. out_unlock:
  11353. mutex_unlock(&dev_priv->drm.struct_mutex);
  11354. out_free:
  11355. if (ret)
  11356. intel_plane_destroy_state(plane, new_plane_state);
  11357. else
  11358. intel_plane_destroy_state(plane, old_plane_state);
  11359. return ret;
  11360. slow:
  11361. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11362. crtc_x, crtc_y, crtc_w, crtc_h,
  11363. src_x, src_y, src_w, src_h, ctx);
  11364. }
  11365. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11366. .update_plane = intel_legacy_cursor_update,
  11367. .disable_plane = drm_atomic_helper_disable_plane,
  11368. .destroy = intel_plane_destroy,
  11369. .atomic_get_property = intel_plane_atomic_get_property,
  11370. .atomic_set_property = intel_plane_atomic_set_property,
  11371. .atomic_duplicate_state = intel_plane_duplicate_state,
  11372. .atomic_destroy_state = intel_plane_destroy_state,
  11373. .format_mod_supported = intel_cursor_format_mod_supported,
  11374. };
  11375. static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
  11376. enum i9xx_plane_id i9xx_plane)
  11377. {
  11378. if (!HAS_FBC(dev_priv))
  11379. return false;
  11380. if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  11381. return i9xx_plane == PLANE_A; /* tied to pipe A */
  11382. else if (IS_IVYBRIDGE(dev_priv))
  11383. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
  11384. i9xx_plane == PLANE_C;
  11385. else if (INTEL_GEN(dev_priv) >= 4)
  11386. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
  11387. else
  11388. return i9xx_plane == PLANE_A;
  11389. }
  11390. static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
  11391. enum pipe pipe, enum plane_id plane_id)
  11392. {
  11393. if (!HAS_FBC(dev_priv))
  11394. return false;
  11395. return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
  11396. }
  11397. bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
  11398. enum pipe pipe, enum plane_id plane_id)
  11399. {
  11400. if (plane_id == PLANE_PRIMARY) {
  11401. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11402. return false;
  11403. else if ((INTEL_GEN(dev_priv) == 9 && pipe == PIPE_C) &&
  11404. !IS_GEMINILAKE(dev_priv))
  11405. return false;
  11406. } else if (plane_id >= PLANE_SPRITE0) {
  11407. if (plane_id == PLANE_CURSOR)
  11408. return false;
  11409. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) == 10) {
  11410. if (plane_id != PLANE_SPRITE0)
  11411. return false;
  11412. } else {
  11413. if (plane_id != PLANE_SPRITE0 || pipe == PIPE_C ||
  11414. IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11415. return false;
  11416. }
  11417. }
  11418. return true;
  11419. }
  11420. static struct intel_plane *
  11421. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11422. {
  11423. struct intel_plane *primary = NULL;
  11424. struct intel_plane_state *state = NULL;
  11425. const struct drm_plane_funcs *plane_funcs;
  11426. const uint32_t *intel_primary_formats;
  11427. unsigned int supported_rotations;
  11428. unsigned int num_formats;
  11429. const uint64_t *modifiers;
  11430. int ret;
  11431. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11432. if (!primary) {
  11433. ret = -ENOMEM;
  11434. goto fail;
  11435. }
  11436. state = intel_create_plane_state(&primary->base);
  11437. if (!state) {
  11438. ret = -ENOMEM;
  11439. goto fail;
  11440. }
  11441. primary->base.state = &state->base;
  11442. primary->can_scale = false;
  11443. primary->max_downscale = 1;
  11444. if (INTEL_GEN(dev_priv) >= 9) {
  11445. primary->can_scale = true;
  11446. state->scaler_id = -1;
  11447. }
  11448. primary->pipe = pipe;
  11449. /*
  11450. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11451. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11452. */
  11453. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11454. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11455. else
  11456. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11457. primary->id = PLANE_PRIMARY;
  11458. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11459. if (INTEL_GEN(dev_priv) >= 9)
  11460. primary->has_fbc = skl_plane_has_fbc(dev_priv,
  11461. primary->pipe,
  11462. primary->id);
  11463. else
  11464. primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
  11465. primary->i9xx_plane);
  11466. if (primary->has_fbc) {
  11467. struct intel_fbc *fbc = &dev_priv->fbc;
  11468. fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
  11469. }
  11470. primary->check_plane = intel_check_primary_plane;
  11471. if (INTEL_GEN(dev_priv) >= 9) {
  11472. primary->has_ccs = skl_plane_has_ccs(dev_priv, pipe,
  11473. PLANE_PRIMARY);
  11474. if (skl_plane_has_planar(dev_priv, pipe, PLANE_PRIMARY)) {
  11475. intel_primary_formats = skl_pri_planar_formats;
  11476. num_formats = ARRAY_SIZE(skl_pri_planar_formats);
  11477. } else {
  11478. intel_primary_formats = skl_primary_formats;
  11479. num_formats = ARRAY_SIZE(skl_primary_formats);
  11480. }
  11481. if (primary->has_ccs)
  11482. modifiers = skl_format_modifiers_ccs;
  11483. else
  11484. modifiers = skl_format_modifiers_noccs;
  11485. primary->update_plane = skl_update_plane;
  11486. primary->disable_plane = skl_disable_plane;
  11487. primary->get_hw_state = skl_plane_get_hw_state;
  11488. plane_funcs = &skl_plane_funcs;
  11489. } else if (INTEL_GEN(dev_priv) >= 4) {
  11490. intel_primary_formats = i965_primary_formats;
  11491. num_formats = ARRAY_SIZE(i965_primary_formats);
  11492. modifiers = i9xx_format_modifiers;
  11493. primary->update_plane = i9xx_update_plane;
  11494. primary->disable_plane = i9xx_disable_plane;
  11495. primary->get_hw_state = i9xx_plane_get_hw_state;
  11496. plane_funcs = &i965_plane_funcs;
  11497. } else {
  11498. intel_primary_formats = i8xx_primary_formats;
  11499. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11500. modifiers = i9xx_format_modifiers;
  11501. primary->update_plane = i9xx_update_plane;
  11502. primary->disable_plane = i9xx_disable_plane;
  11503. primary->get_hw_state = i9xx_plane_get_hw_state;
  11504. plane_funcs = &i8xx_plane_funcs;
  11505. }
  11506. if (INTEL_GEN(dev_priv) >= 9)
  11507. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11508. 0, plane_funcs,
  11509. intel_primary_formats, num_formats,
  11510. modifiers,
  11511. DRM_PLANE_TYPE_PRIMARY,
  11512. "plane 1%c", pipe_name(pipe));
  11513. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11514. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11515. 0, plane_funcs,
  11516. intel_primary_formats, num_formats,
  11517. modifiers,
  11518. DRM_PLANE_TYPE_PRIMARY,
  11519. "primary %c", pipe_name(pipe));
  11520. else
  11521. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11522. 0, plane_funcs,
  11523. intel_primary_formats, num_formats,
  11524. modifiers,
  11525. DRM_PLANE_TYPE_PRIMARY,
  11526. "plane %c",
  11527. plane_name(primary->i9xx_plane));
  11528. if (ret)
  11529. goto fail;
  11530. if (INTEL_GEN(dev_priv) >= 10) {
  11531. supported_rotations =
  11532. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11533. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11534. DRM_MODE_REFLECT_X;
  11535. } else if (INTEL_GEN(dev_priv) >= 9) {
  11536. supported_rotations =
  11537. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11538. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11539. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11540. supported_rotations =
  11541. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11542. DRM_MODE_REFLECT_X;
  11543. } else if (INTEL_GEN(dev_priv) >= 4) {
  11544. supported_rotations =
  11545. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11546. } else {
  11547. supported_rotations = DRM_MODE_ROTATE_0;
  11548. }
  11549. if (INTEL_GEN(dev_priv) >= 4)
  11550. drm_plane_create_rotation_property(&primary->base,
  11551. DRM_MODE_ROTATE_0,
  11552. supported_rotations);
  11553. if (INTEL_GEN(dev_priv) >= 9)
  11554. drm_plane_create_color_properties(&primary->base,
  11555. BIT(DRM_COLOR_YCBCR_BT601) |
  11556. BIT(DRM_COLOR_YCBCR_BT709),
  11557. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  11558. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  11559. DRM_COLOR_YCBCR_BT709,
  11560. DRM_COLOR_YCBCR_LIMITED_RANGE);
  11561. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11562. return primary;
  11563. fail:
  11564. kfree(state);
  11565. kfree(primary);
  11566. return ERR_PTR(ret);
  11567. }
  11568. static struct intel_plane *
  11569. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11570. enum pipe pipe)
  11571. {
  11572. struct intel_plane *cursor = NULL;
  11573. struct intel_plane_state *state = NULL;
  11574. int ret;
  11575. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11576. if (!cursor) {
  11577. ret = -ENOMEM;
  11578. goto fail;
  11579. }
  11580. state = intel_create_plane_state(&cursor->base);
  11581. if (!state) {
  11582. ret = -ENOMEM;
  11583. goto fail;
  11584. }
  11585. cursor->base.state = &state->base;
  11586. cursor->can_scale = false;
  11587. cursor->max_downscale = 1;
  11588. cursor->pipe = pipe;
  11589. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11590. cursor->id = PLANE_CURSOR;
  11591. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11592. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11593. cursor->update_plane = i845_update_cursor;
  11594. cursor->disable_plane = i845_disable_cursor;
  11595. cursor->get_hw_state = i845_cursor_get_hw_state;
  11596. cursor->check_plane = i845_check_cursor;
  11597. } else {
  11598. cursor->update_plane = i9xx_update_cursor;
  11599. cursor->disable_plane = i9xx_disable_cursor;
  11600. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11601. cursor->check_plane = i9xx_check_cursor;
  11602. }
  11603. cursor->cursor.base = ~0;
  11604. cursor->cursor.cntl = ~0;
  11605. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11606. cursor->cursor.size = ~0;
  11607. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11608. 0, &intel_cursor_plane_funcs,
  11609. intel_cursor_formats,
  11610. ARRAY_SIZE(intel_cursor_formats),
  11611. cursor_format_modifiers,
  11612. DRM_PLANE_TYPE_CURSOR,
  11613. "cursor %c", pipe_name(pipe));
  11614. if (ret)
  11615. goto fail;
  11616. if (INTEL_GEN(dev_priv) >= 4)
  11617. drm_plane_create_rotation_property(&cursor->base,
  11618. DRM_MODE_ROTATE_0,
  11619. DRM_MODE_ROTATE_0 |
  11620. DRM_MODE_ROTATE_180);
  11621. if (INTEL_GEN(dev_priv) >= 9)
  11622. state->scaler_id = -1;
  11623. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11624. return cursor;
  11625. fail:
  11626. kfree(state);
  11627. kfree(cursor);
  11628. return ERR_PTR(ret);
  11629. }
  11630. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11631. struct intel_crtc_state *crtc_state)
  11632. {
  11633. struct intel_crtc_scaler_state *scaler_state =
  11634. &crtc_state->scaler_state;
  11635. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11636. int i;
  11637. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11638. if (!crtc->num_scalers)
  11639. return;
  11640. for (i = 0; i < crtc->num_scalers; i++) {
  11641. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11642. scaler->in_use = 0;
  11643. scaler->mode = PS_SCALER_MODE_DYN;
  11644. }
  11645. scaler_state->scaler_id = -1;
  11646. }
  11647. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11648. {
  11649. struct intel_crtc *intel_crtc;
  11650. struct intel_crtc_state *crtc_state = NULL;
  11651. struct intel_plane *primary = NULL;
  11652. struct intel_plane *cursor = NULL;
  11653. int sprite, ret;
  11654. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11655. if (!intel_crtc)
  11656. return -ENOMEM;
  11657. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11658. if (!crtc_state) {
  11659. ret = -ENOMEM;
  11660. goto fail;
  11661. }
  11662. intel_crtc->config = crtc_state;
  11663. intel_crtc->base.state = &crtc_state->base;
  11664. crtc_state->base.crtc = &intel_crtc->base;
  11665. primary = intel_primary_plane_create(dev_priv, pipe);
  11666. if (IS_ERR(primary)) {
  11667. ret = PTR_ERR(primary);
  11668. goto fail;
  11669. }
  11670. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11671. for_each_sprite(dev_priv, pipe, sprite) {
  11672. struct intel_plane *plane;
  11673. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11674. if (IS_ERR(plane)) {
  11675. ret = PTR_ERR(plane);
  11676. goto fail;
  11677. }
  11678. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11679. }
  11680. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11681. if (IS_ERR(cursor)) {
  11682. ret = PTR_ERR(cursor);
  11683. goto fail;
  11684. }
  11685. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11686. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11687. &primary->base, &cursor->base,
  11688. &intel_crtc_funcs,
  11689. "pipe %c", pipe_name(pipe));
  11690. if (ret)
  11691. goto fail;
  11692. intel_crtc->pipe = pipe;
  11693. /* initialize shared scalers */
  11694. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11695. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
  11696. dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
  11697. dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
  11698. if (INTEL_GEN(dev_priv) < 9) {
  11699. enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
  11700. BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11701. dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
  11702. dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
  11703. }
  11704. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11705. intel_color_init(&intel_crtc->base);
  11706. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11707. return 0;
  11708. fail:
  11709. /*
  11710. * drm_mode_config_cleanup() will free up any
  11711. * crtcs/planes already initialized.
  11712. */
  11713. kfree(crtc_state);
  11714. kfree(intel_crtc);
  11715. return ret;
  11716. }
  11717. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11718. {
  11719. struct drm_device *dev = connector->base.dev;
  11720. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11721. if (!connector->base.state->crtc)
  11722. return INVALID_PIPE;
  11723. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11724. }
  11725. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11726. struct drm_file *file)
  11727. {
  11728. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11729. struct drm_crtc *drmmode_crtc;
  11730. struct intel_crtc *crtc;
  11731. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11732. if (!drmmode_crtc)
  11733. return -ENOENT;
  11734. crtc = to_intel_crtc(drmmode_crtc);
  11735. pipe_from_crtc_id->pipe = crtc->pipe;
  11736. return 0;
  11737. }
  11738. static int intel_encoder_clones(struct intel_encoder *encoder)
  11739. {
  11740. struct drm_device *dev = encoder->base.dev;
  11741. struct intel_encoder *source_encoder;
  11742. int index_mask = 0;
  11743. int entry = 0;
  11744. for_each_intel_encoder(dev, source_encoder) {
  11745. if (encoders_cloneable(encoder, source_encoder))
  11746. index_mask |= (1 << entry);
  11747. entry++;
  11748. }
  11749. return index_mask;
  11750. }
  11751. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11752. {
  11753. if (!IS_MOBILE(dev_priv))
  11754. return false;
  11755. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11756. return false;
  11757. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11758. return false;
  11759. return true;
  11760. }
  11761. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11762. {
  11763. if (INTEL_GEN(dev_priv) >= 9)
  11764. return false;
  11765. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11766. return false;
  11767. if (IS_CHERRYVIEW(dev_priv))
  11768. return false;
  11769. if (HAS_PCH_LPT_H(dev_priv) &&
  11770. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11771. return false;
  11772. /* DDI E can't be used if DDI A requires 4 lanes */
  11773. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11774. return false;
  11775. if (!dev_priv->vbt.int_crt_support)
  11776. return false;
  11777. return true;
  11778. }
  11779. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11780. {
  11781. int pps_num;
  11782. int pps_idx;
  11783. if (HAS_DDI(dev_priv))
  11784. return;
  11785. /*
  11786. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11787. * everywhere where registers can be write protected.
  11788. */
  11789. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11790. pps_num = 2;
  11791. else
  11792. pps_num = 1;
  11793. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11794. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11795. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11796. I915_WRITE(PP_CONTROL(pps_idx), val);
  11797. }
  11798. }
  11799. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11800. {
  11801. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11802. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11803. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11804. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11805. else
  11806. dev_priv->pps_mmio_base = PPS_BASE;
  11807. intel_pps_unlock_regs_wa(dev_priv);
  11808. }
  11809. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11810. {
  11811. struct intel_encoder *encoder;
  11812. bool dpd_is_edp = false;
  11813. intel_pps_init(dev_priv);
  11814. /*
  11815. * intel_edp_init_connector() depends on this completing first, to
  11816. * prevent the registeration of both eDP and LVDS and the incorrect
  11817. * sharing of the PPS.
  11818. */
  11819. intel_lvds_init(dev_priv);
  11820. if (intel_crt_present(dev_priv))
  11821. intel_crt_init(dev_priv);
  11822. if (IS_ICELAKE(dev_priv)) {
  11823. intel_ddi_init(dev_priv, PORT_A);
  11824. intel_ddi_init(dev_priv, PORT_B);
  11825. intel_ddi_init(dev_priv, PORT_C);
  11826. intel_ddi_init(dev_priv, PORT_D);
  11827. intel_ddi_init(dev_priv, PORT_E);
  11828. intel_ddi_init(dev_priv, PORT_F);
  11829. } else if (IS_GEN9_LP(dev_priv)) {
  11830. /*
  11831. * FIXME: Broxton doesn't support port detection via the
  11832. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11833. * detect the ports.
  11834. */
  11835. intel_ddi_init(dev_priv, PORT_A);
  11836. intel_ddi_init(dev_priv, PORT_B);
  11837. intel_ddi_init(dev_priv, PORT_C);
  11838. vlv_dsi_init(dev_priv);
  11839. } else if (HAS_DDI(dev_priv)) {
  11840. int found;
  11841. /*
  11842. * Haswell uses DDI functions to detect digital outputs.
  11843. * On SKL pre-D0 the strap isn't connected, so we assume
  11844. * it's there.
  11845. */
  11846. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11847. /* WaIgnoreDDIAStrap: skl */
  11848. if (found || IS_GEN9_BC(dev_priv))
  11849. intel_ddi_init(dev_priv, PORT_A);
  11850. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11851. * register */
  11852. found = I915_READ(SFUSE_STRAP);
  11853. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11854. intel_ddi_init(dev_priv, PORT_B);
  11855. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11856. intel_ddi_init(dev_priv, PORT_C);
  11857. if (found & SFUSE_STRAP_DDID_DETECTED)
  11858. intel_ddi_init(dev_priv, PORT_D);
  11859. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11860. intel_ddi_init(dev_priv, PORT_F);
  11861. /*
  11862. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11863. */
  11864. if (IS_GEN9_BC(dev_priv) &&
  11865. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11866. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11867. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11868. intel_ddi_init(dev_priv, PORT_E);
  11869. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11870. int found;
  11871. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11872. if (has_edp_a(dev_priv))
  11873. intel_dp_init(dev_priv, DP_A, PORT_A);
  11874. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11875. /* PCH SDVOB multiplex with HDMIB */
  11876. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11877. if (!found)
  11878. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11879. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11880. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11881. }
  11882. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11883. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11884. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11885. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11886. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11887. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11888. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11889. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11890. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11891. bool has_edp, has_port;
  11892. /*
  11893. * The DP_DETECTED bit is the latched state of the DDC
  11894. * SDA pin at boot. However since eDP doesn't require DDC
  11895. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11896. * eDP ports may have been muxed to an alternate function.
  11897. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11898. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11899. * detect eDP ports.
  11900. *
  11901. * Sadly the straps seem to be missing sometimes even for HDMI
  11902. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11903. * and VBT for the presence of the port. Additionally we can't
  11904. * trust the port type the VBT declares as we've seen at least
  11905. * HDMI ports that the VBT claim are DP or eDP.
  11906. */
  11907. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11908. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11909. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11910. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11911. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11912. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11913. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11914. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11915. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11916. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11917. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11918. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11919. if (IS_CHERRYVIEW(dev_priv)) {
  11920. /*
  11921. * eDP not supported on port D,
  11922. * so no need to worry about it
  11923. */
  11924. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11925. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11926. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11927. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11928. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11929. }
  11930. vlv_dsi_init(dev_priv);
  11931. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11932. bool found = false;
  11933. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11934. DRM_DEBUG_KMS("probing SDVOB\n");
  11935. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11936. if (!found && IS_G4X(dev_priv)) {
  11937. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11938. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11939. }
  11940. if (!found && IS_G4X(dev_priv))
  11941. intel_dp_init(dev_priv, DP_B, PORT_B);
  11942. }
  11943. /* Before G4X SDVOC doesn't have its own detect register */
  11944. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11945. DRM_DEBUG_KMS("probing SDVOC\n");
  11946. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11947. }
  11948. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11949. if (IS_G4X(dev_priv)) {
  11950. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11951. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11952. }
  11953. if (IS_G4X(dev_priv))
  11954. intel_dp_init(dev_priv, DP_C, PORT_C);
  11955. }
  11956. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11957. intel_dp_init(dev_priv, DP_D, PORT_D);
  11958. } else if (IS_GEN2(dev_priv))
  11959. intel_dvo_init(dev_priv);
  11960. if (SUPPORTS_TV(dev_priv))
  11961. intel_tv_init(dev_priv);
  11962. intel_psr_init(dev_priv);
  11963. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11964. encoder->base.possible_crtcs = encoder->crtc_mask;
  11965. encoder->base.possible_clones =
  11966. intel_encoder_clones(encoder);
  11967. }
  11968. intel_init_pch_refclk(dev_priv);
  11969. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11970. }
  11971. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11972. {
  11973. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11974. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11975. drm_framebuffer_cleanup(fb);
  11976. i915_gem_object_lock(obj);
  11977. WARN_ON(!obj->framebuffer_references--);
  11978. i915_gem_object_unlock(obj);
  11979. i915_gem_object_put(obj);
  11980. kfree(intel_fb);
  11981. }
  11982. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11983. struct drm_file *file,
  11984. unsigned int *handle)
  11985. {
  11986. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11987. if (obj->userptr.mm) {
  11988. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11989. return -EINVAL;
  11990. }
  11991. return drm_gem_handle_create(file, &obj->base, handle);
  11992. }
  11993. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11994. struct drm_file *file,
  11995. unsigned flags, unsigned color,
  11996. struct drm_clip_rect *clips,
  11997. unsigned num_clips)
  11998. {
  11999. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12000. i915_gem_object_flush_if_display(obj);
  12001. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  12002. return 0;
  12003. }
  12004. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12005. .destroy = intel_user_framebuffer_destroy,
  12006. .create_handle = intel_user_framebuffer_create_handle,
  12007. .dirty = intel_user_framebuffer_dirty,
  12008. };
  12009. static
  12010. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  12011. uint64_t fb_modifier, uint32_t pixel_format)
  12012. {
  12013. u32 gen = INTEL_GEN(dev_priv);
  12014. if (gen >= 9) {
  12015. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12016. /* "The stride in bytes must not exceed the of the size of 8K
  12017. * pixels and 32K bytes."
  12018. */
  12019. return min(8192 * cpp, 32768);
  12020. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  12021. return 32*1024;
  12022. } else if (gen >= 4) {
  12023. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12024. return 16*1024;
  12025. else
  12026. return 32*1024;
  12027. } else if (gen >= 3) {
  12028. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12029. return 8*1024;
  12030. else
  12031. return 16*1024;
  12032. } else {
  12033. /* XXX DSPC is limited to 4k tiled */
  12034. return 8*1024;
  12035. }
  12036. }
  12037. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  12038. struct drm_i915_gem_object *obj,
  12039. struct drm_mode_fb_cmd2 *mode_cmd)
  12040. {
  12041. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  12042. struct drm_framebuffer *fb = &intel_fb->base;
  12043. struct drm_format_name_buf format_name;
  12044. u32 pitch_limit;
  12045. unsigned int tiling, stride;
  12046. int ret = -EINVAL;
  12047. int i;
  12048. i915_gem_object_lock(obj);
  12049. obj->framebuffer_references++;
  12050. tiling = i915_gem_object_get_tiling(obj);
  12051. stride = i915_gem_object_get_stride(obj);
  12052. i915_gem_object_unlock(obj);
  12053. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12054. /*
  12055. * If there's a fence, enforce that
  12056. * the fb modifier and tiling mode match.
  12057. */
  12058. if (tiling != I915_TILING_NONE &&
  12059. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12060. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  12061. goto err;
  12062. }
  12063. } else {
  12064. if (tiling == I915_TILING_X) {
  12065. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12066. } else if (tiling == I915_TILING_Y) {
  12067. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  12068. goto err;
  12069. }
  12070. }
  12071. /* Passed in modifier sanity checking. */
  12072. switch (mode_cmd->modifier[0]) {
  12073. case I915_FORMAT_MOD_Y_TILED_CCS:
  12074. case I915_FORMAT_MOD_Yf_TILED_CCS:
  12075. switch (mode_cmd->pixel_format) {
  12076. case DRM_FORMAT_XBGR8888:
  12077. case DRM_FORMAT_ABGR8888:
  12078. case DRM_FORMAT_XRGB8888:
  12079. case DRM_FORMAT_ARGB8888:
  12080. break;
  12081. default:
  12082. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  12083. goto err;
  12084. }
  12085. /* fall through */
  12086. case I915_FORMAT_MOD_Y_TILED:
  12087. case I915_FORMAT_MOD_Yf_TILED:
  12088. if (INTEL_GEN(dev_priv) < 9) {
  12089. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  12090. mode_cmd->modifier[0]);
  12091. goto err;
  12092. }
  12093. case DRM_FORMAT_MOD_LINEAR:
  12094. case I915_FORMAT_MOD_X_TILED:
  12095. break;
  12096. default:
  12097. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  12098. mode_cmd->modifier[0]);
  12099. goto err;
  12100. }
  12101. /*
  12102. * gen2/3 display engine uses the fence if present,
  12103. * so the tiling mode must match the fb modifier exactly.
  12104. */
  12105. if (INTEL_GEN(dev_priv) < 4 &&
  12106. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12107. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  12108. goto err;
  12109. }
  12110. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  12111. mode_cmd->pixel_format);
  12112. if (mode_cmd->pitches[0] > pitch_limit) {
  12113. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  12114. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  12115. "tiled" : "linear",
  12116. mode_cmd->pitches[0], pitch_limit);
  12117. goto err;
  12118. }
  12119. /*
  12120. * If there's a fence, enforce that
  12121. * the fb pitch and fence stride match.
  12122. */
  12123. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  12124. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  12125. mode_cmd->pitches[0], stride);
  12126. goto err;
  12127. }
  12128. /* Reject formats not supported by any plane early. */
  12129. switch (mode_cmd->pixel_format) {
  12130. case DRM_FORMAT_C8:
  12131. case DRM_FORMAT_RGB565:
  12132. case DRM_FORMAT_XRGB8888:
  12133. case DRM_FORMAT_ARGB8888:
  12134. break;
  12135. case DRM_FORMAT_XRGB1555:
  12136. if (INTEL_GEN(dev_priv) > 3) {
  12137. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12138. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12139. goto err;
  12140. }
  12141. break;
  12142. case DRM_FORMAT_ABGR8888:
  12143. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12144. INTEL_GEN(dev_priv) < 9) {
  12145. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12146. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12147. goto err;
  12148. }
  12149. break;
  12150. case DRM_FORMAT_XBGR8888:
  12151. case DRM_FORMAT_XRGB2101010:
  12152. case DRM_FORMAT_XBGR2101010:
  12153. if (INTEL_GEN(dev_priv) < 4) {
  12154. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12155. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12156. goto err;
  12157. }
  12158. break;
  12159. case DRM_FORMAT_ABGR2101010:
  12160. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12161. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12162. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12163. goto err;
  12164. }
  12165. break;
  12166. case DRM_FORMAT_YUYV:
  12167. case DRM_FORMAT_UYVY:
  12168. case DRM_FORMAT_YVYU:
  12169. case DRM_FORMAT_VYUY:
  12170. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  12171. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12172. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12173. goto err;
  12174. }
  12175. break;
  12176. case DRM_FORMAT_NV12:
  12177. if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
  12178. IS_BROXTON(dev_priv)) {
  12179. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12180. drm_get_format_name(mode_cmd->pixel_format,
  12181. &format_name));
  12182. goto err;
  12183. }
  12184. break;
  12185. default:
  12186. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12187. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12188. goto err;
  12189. }
  12190. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12191. if (mode_cmd->offsets[0] != 0)
  12192. goto err;
  12193. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  12194. if (fb->format->format == DRM_FORMAT_NV12 &&
  12195. (fb->width < SKL_MIN_YUV_420_SRC_W ||
  12196. fb->height < SKL_MIN_YUV_420_SRC_H ||
  12197. (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
  12198. DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
  12199. goto err;
  12200. }
  12201. for (i = 0; i < fb->format->num_planes; i++) {
  12202. u32 stride_alignment;
  12203. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  12204. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  12205. goto err;
  12206. }
  12207. stride_alignment = intel_fb_stride_alignment(fb, i);
  12208. /*
  12209. * Display WA #0531: skl,bxt,kbl,glk
  12210. *
  12211. * Render decompression and plane width > 3840
  12212. * combined with horizontal panning requires the
  12213. * plane stride to be a multiple of 4. We'll just
  12214. * require the entire fb to accommodate that to avoid
  12215. * potential runtime errors at plane configuration time.
  12216. */
  12217. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  12218. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  12219. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  12220. stride_alignment *= 4;
  12221. if (fb->pitches[i] & (stride_alignment - 1)) {
  12222. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  12223. i, fb->pitches[i], stride_alignment);
  12224. goto err;
  12225. }
  12226. fb->obj[i] = &obj->base;
  12227. }
  12228. ret = intel_fill_fb_info(dev_priv, fb);
  12229. if (ret)
  12230. goto err;
  12231. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  12232. if (ret) {
  12233. DRM_ERROR("framebuffer init failed %d\n", ret);
  12234. goto err;
  12235. }
  12236. return 0;
  12237. err:
  12238. i915_gem_object_lock(obj);
  12239. obj->framebuffer_references--;
  12240. i915_gem_object_unlock(obj);
  12241. return ret;
  12242. }
  12243. static struct drm_framebuffer *
  12244. intel_user_framebuffer_create(struct drm_device *dev,
  12245. struct drm_file *filp,
  12246. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12247. {
  12248. struct drm_framebuffer *fb;
  12249. struct drm_i915_gem_object *obj;
  12250. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12251. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12252. if (!obj)
  12253. return ERR_PTR(-ENOENT);
  12254. fb = intel_framebuffer_create(obj, &mode_cmd);
  12255. if (IS_ERR(fb))
  12256. i915_gem_object_put(obj);
  12257. return fb;
  12258. }
  12259. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12260. {
  12261. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12262. drm_atomic_state_default_release(state);
  12263. i915_sw_fence_fini(&intel_state->commit_ready);
  12264. kfree(state);
  12265. }
  12266. static enum drm_mode_status
  12267. intel_mode_valid(struct drm_device *dev,
  12268. const struct drm_display_mode *mode)
  12269. {
  12270. struct drm_i915_private *dev_priv = to_i915(dev);
  12271. int hdisplay_max, htotal_max;
  12272. int vdisplay_max, vtotal_max;
  12273. /*
  12274. * Can't reject DBLSCAN here because Xorg ddxen can add piles
  12275. * of DBLSCAN modes to the output's mode list when they detect
  12276. * the scaling mode property on the connector. And they don't
  12277. * ask the kernel to validate those modes in any way until
  12278. * modeset time at which point the client gets a protocol error.
  12279. * So in order to not upset those clients we silently ignore the
  12280. * DBLSCAN flag on such connectors. For other connectors we will
  12281. * reject modes with the DBLSCAN flag in encoder->compute_config().
  12282. * And we always reject DBLSCAN modes in connector->mode_valid()
  12283. * as we never want such modes on the connector's mode list.
  12284. */
  12285. if (mode->vscan > 1)
  12286. return MODE_NO_VSCAN;
  12287. if (mode->flags & DRM_MODE_FLAG_HSKEW)
  12288. return MODE_H_ILLEGAL;
  12289. if (mode->flags & (DRM_MODE_FLAG_CSYNC |
  12290. DRM_MODE_FLAG_NCSYNC |
  12291. DRM_MODE_FLAG_PCSYNC))
  12292. return MODE_HSYNC;
  12293. if (mode->flags & (DRM_MODE_FLAG_BCAST |
  12294. DRM_MODE_FLAG_PIXMUX |
  12295. DRM_MODE_FLAG_CLKDIV2))
  12296. return MODE_BAD;
  12297. if (INTEL_GEN(dev_priv) >= 9 ||
  12298. IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
  12299. hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
  12300. vdisplay_max = 4096;
  12301. htotal_max = 8192;
  12302. vtotal_max = 8192;
  12303. } else if (INTEL_GEN(dev_priv) >= 3) {
  12304. hdisplay_max = 4096;
  12305. vdisplay_max = 4096;
  12306. htotal_max = 8192;
  12307. vtotal_max = 8192;
  12308. } else {
  12309. hdisplay_max = 2048;
  12310. vdisplay_max = 2048;
  12311. htotal_max = 4096;
  12312. vtotal_max = 4096;
  12313. }
  12314. if (mode->hdisplay > hdisplay_max ||
  12315. mode->hsync_start > htotal_max ||
  12316. mode->hsync_end > htotal_max ||
  12317. mode->htotal > htotal_max)
  12318. return MODE_H_ILLEGAL;
  12319. if (mode->vdisplay > vdisplay_max ||
  12320. mode->vsync_start > vtotal_max ||
  12321. mode->vsync_end > vtotal_max ||
  12322. mode->vtotal > vtotal_max)
  12323. return MODE_V_ILLEGAL;
  12324. return MODE_OK;
  12325. }
  12326. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12327. .fb_create = intel_user_framebuffer_create,
  12328. .get_format_info = intel_get_format_info,
  12329. .output_poll_changed = intel_fbdev_output_poll_changed,
  12330. .mode_valid = intel_mode_valid,
  12331. .atomic_check = intel_atomic_check,
  12332. .atomic_commit = intel_atomic_commit,
  12333. .atomic_state_alloc = intel_atomic_state_alloc,
  12334. .atomic_state_clear = intel_atomic_state_clear,
  12335. .atomic_state_free = intel_atomic_state_free,
  12336. };
  12337. /**
  12338. * intel_init_display_hooks - initialize the display modesetting hooks
  12339. * @dev_priv: device private
  12340. */
  12341. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12342. {
  12343. intel_init_cdclk_hooks(dev_priv);
  12344. if (INTEL_GEN(dev_priv) >= 9) {
  12345. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12346. dev_priv->display.get_initial_plane_config =
  12347. skylake_get_initial_plane_config;
  12348. dev_priv->display.crtc_compute_clock =
  12349. haswell_crtc_compute_clock;
  12350. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12351. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12352. } else if (HAS_DDI(dev_priv)) {
  12353. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12354. dev_priv->display.get_initial_plane_config =
  12355. i9xx_get_initial_plane_config;
  12356. dev_priv->display.crtc_compute_clock =
  12357. haswell_crtc_compute_clock;
  12358. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12359. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12360. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12361. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12362. dev_priv->display.get_initial_plane_config =
  12363. i9xx_get_initial_plane_config;
  12364. dev_priv->display.crtc_compute_clock =
  12365. ironlake_crtc_compute_clock;
  12366. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12367. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12368. } else if (IS_CHERRYVIEW(dev_priv)) {
  12369. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12370. dev_priv->display.get_initial_plane_config =
  12371. i9xx_get_initial_plane_config;
  12372. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12373. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12374. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12375. } else if (IS_VALLEYVIEW(dev_priv)) {
  12376. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12377. dev_priv->display.get_initial_plane_config =
  12378. i9xx_get_initial_plane_config;
  12379. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12380. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12381. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12382. } else if (IS_G4X(dev_priv)) {
  12383. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12384. dev_priv->display.get_initial_plane_config =
  12385. i9xx_get_initial_plane_config;
  12386. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12387. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12388. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12389. } else if (IS_PINEVIEW(dev_priv)) {
  12390. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12391. dev_priv->display.get_initial_plane_config =
  12392. i9xx_get_initial_plane_config;
  12393. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12394. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12395. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12396. } else if (!IS_GEN2(dev_priv)) {
  12397. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12398. dev_priv->display.get_initial_plane_config =
  12399. i9xx_get_initial_plane_config;
  12400. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12401. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12402. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12403. } else {
  12404. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12405. dev_priv->display.get_initial_plane_config =
  12406. i9xx_get_initial_plane_config;
  12407. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12408. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12409. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12410. }
  12411. if (IS_GEN5(dev_priv)) {
  12412. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12413. } else if (IS_GEN6(dev_priv)) {
  12414. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12415. } else if (IS_IVYBRIDGE(dev_priv)) {
  12416. /* FIXME: detect B0+ stepping and use auto training */
  12417. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12418. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12419. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12420. }
  12421. if (INTEL_GEN(dev_priv) >= 9)
  12422. dev_priv->display.update_crtcs = skl_update_crtcs;
  12423. else
  12424. dev_priv->display.update_crtcs = intel_update_crtcs;
  12425. }
  12426. /*
  12427. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12428. */
  12429. static void quirk_ssc_force_disable(struct drm_device *dev)
  12430. {
  12431. struct drm_i915_private *dev_priv = to_i915(dev);
  12432. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12433. DRM_INFO("applying lvds SSC disable quirk\n");
  12434. }
  12435. /*
  12436. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12437. * brightness value
  12438. */
  12439. static void quirk_invert_brightness(struct drm_device *dev)
  12440. {
  12441. struct drm_i915_private *dev_priv = to_i915(dev);
  12442. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12443. DRM_INFO("applying inverted panel brightness quirk\n");
  12444. }
  12445. /* Some VBT's incorrectly indicate no backlight is present */
  12446. static void quirk_backlight_present(struct drm_device *dev)
  12447. {
  12448. struct drm_i915_private *dev_priv = to_i915(dev);
  12449. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12450. DRM_INFO("applying backlight present quirk\n");
  12451. }
  12452. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  12453. * which is 300 ms greater than eDP spec T12 min.
  12454. */
  12455. static void quirk_increase_t12_delay(struct drm_device *dev)
  12456. {
  12457. struct drm_i915_private *dev_priv = to_i915(dev);
  12458. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  12459. DRM_INFO("Applying T12 delay quirk\n");
  12460. }
  12461. /*
  12462. * GeminiLake NUC HDMI outputs require additional off time
  12463. * this allows the onboard retimer to correctly sync to signal
  12464. */
  12465. static void quirk_increase_ddi_disabled_time(struct drm_device *dev)
  12466. {
  12467. struct drm_i915_private *dev_priv = to_i915(dev);
  12468. dev_priv->quirks |= QUIRK_INCREASE_DDI_DISABLED_TIME;
  12469. DRM_INFO("Applying Increase DDI Disabled quirk\n");
  12470. }
  12471. struct intel_quirk {
  12472. int device;
  12473. int subsystem_vendor;
  12474. int subsystem_device;
  12475. void (*hook)(struct drm_device *dev);
  12476. };
  12477. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12478. struct intel_dmi_quirk {
  12479. void (*hook)(struct drm_device *dev);
  12480. const struct dmi_system_id (*dmi_id_list)[];
  12481. };
  12482. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12483. {
  12484. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12485. return 1;
  12486. }
  12487. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12488. {
  12489. .dmi_id_list = &(const struct dmi_system_id[]) {
  12490. {
  12491. .callback = intel_dmi_reverse_brightness,
  12492. .ident = "NCR Corporation",
  12493. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12494. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12495. },
  12496. },
  12497. { } /* terminating entry */
  12498. },
  12499. .hook = quirk_invert_brightness,
  12500. },
  12501. };
  12502. static struct intel_quirk intel_quirks[] = {
  12503. /* Lenovo U160 cannot use SSC on LVDS */
  12504. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12505. /* Sony Vaio Y cannot use SSC on LVDS */
  12506. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12507. /* Acer Aspire 5734Z must invert backlight brightness */
  12508. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12509. /* Acer/eMachines G725 */
  12510. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12511. /* Acer/eMachines e725 */
  12512. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12513. /* Acer/Packard Bell NCL20 */
  12514. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12515. /* Acer Aspire 4736Z */
  12516. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12517. /* Acer Aspire 5336 */
  12518. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12519. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12520. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12521. /* Acer C720 Chromebook (Core i3 4005U) */
  12522. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12523. /* Apple Macbook 2,1 (Core 2 T7400) */
  12524. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12525. /* Apple Macbook 4,1 */
  12526. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12527. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12528. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12529. /* HP Chromebook 14 (Celeron 2955U) */
  12530. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12531. /* Dell Chromebook 11 */
  12532. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12533. /* Dell Chromebook 11 (2015 version) */
  12534. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12535. /* Toshiba Satellite P50-C-18C */
  12536. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12537. /* GeminiLake NUC */
  12538. { 0x3185, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
  12539. { 0x3184, 0x8086, 0x2072, quirk_increase_ddi_disabled_time },
  12540. /* ASRock ITX*/
  12541. { 0x3185, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
  12542. { 0x3184, 0x1849, 0x2212, quirk_increase_ddi_disabled_time },
  12543. };
  12544. static void intel_init_quirks(struct drm_device *dev)
  12545. {
  12546. struct pci_dev *d = dev->pdev;
  12547. int i;
  12548. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12549. struct intel_quirk *q = &intel_quirks[i];
  12550. if (d->device == q->device &&
  12551. (d->subsystem_vendor == q->subsystem_vendor ||
  12552. q->subsystem_vendor == PCI_ANY_ID) &&
  12553. (d->subsystem_device == q->subsystem_device ||
  12554. q->subsystem_device == PCI_ANY_ID))
  12555. q->hook(dev);
  12556. }
  12557. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12558. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12559. intel_dmi_quirks[i].hook(dev);
  12560. }
  12561. }
  12562. /* Disable the VGA plane that we never use */
  12563. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12564. {
  12565. struct pci_dev *pdev = dev_priv->drm.pdev;
  12566. u8 sr1;
  12567. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12568. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12569. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12570. outb(SR01, VGA_SR_INDEX);
  12571. sr1 = inb(VGA_SR_DATA);
  12572. outb(sr1 | 1<<5, VGA_SR_DATA);
  12573. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12574. udelay(300);
  12575. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12576. POSTING_READ(vga_reg);
  12577. }
  12578. void intel_modeset_init_hw(struct drm_device *dev)
  12579. {
  12580. struct drm_i915_private *dev_priv = to_i915(dev);
  12581. intel_update_cdclk(dev_priv);
  12582. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12583. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12584. }
  12585. /*
  12586. * Calculate what we think the watermarks should be for the state we've read
  12587. * out of the hardware and then immediately program those watermarks so that
  12588. * we ensure the hardware settings match our internal state.
  12589. *
  12590. * We can calculate what we think WM's should be by creating a duplicate of the
  12591. * current state (which was constructed during hardware readout) and running it
  12592. * through the atomic check code to calculate new watermark values in the
  12593. * state object.
  12594. */
  12595. static void sanitize_watermarks(struct drm_device *dev)
  12596. {
  12597. struct drm_i915_private *dev_priv = to_i915(dev);
  12598. struct drm_atomic_state *state;
  12599. struct intel_atomic_state *intel_state;
  12600. struct drm_crtc *crtc;
  12601. struct drm_crtc_state *cstate;
  12602. struct drm_modeset_acquire_ctx ctx;
  12603. int ret;
  12604. int i;
  12605. /* Only supported on platforms that use atomic watermark design */
  12606. if (!dev_priv->display.optimize_watermarks)
  12607. return;
  12608. /*
  12609. * We need to hold connection_mutex before calling duplicate_state so
  12610. * that the connector loop is protected.
  12611. */
  12612. drm_modeset_acquire_init(&ctx, 0);
  12613. retry:
  12614. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12615. if (ret == -EDEADLK) {
  12616. drm_modeset_backoff(&ctx);
  12617. goto retry;
  12618. } else if (WARN_ON(ret)) {
  12619. goto fail;
  12620. }
  12621. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12622. if (WARN_ON(IS_ERR(state)))
  12623. goto fail;
  12624. intel_state = to_intel_atomic_state(state);
  12625. /*
  12626. * Hardware readout is the only time we don't want to calculate
  12627. * intermediate watermarks (since we don't trust the current
  12628. * watermarks).
  12629. */
  12630. if (!HAS_GMCH_DISPLAY(dev_priv))
  12631. intel_state->skip_intermediate_wm = true;
  12632. ret = intel_atomic_check(dev, state);
  12633. if (ret) {
  12634. /*
  12635. * If we fail here, it means that the hardware appears to be
  12636. * programmed in a way that shouldn't be possible, given our
  12637. * understanding of watermark requirements. This might mean a
  12638. * mistake in the hardware readout code or a mistake in the
  12639. * watermark calculations for a given platform. Raise a WARN
  12640. * so that this is noticeable.
  12641. *
  12642. * If this actually happens, we'll have to just leave the
  12643. * BIOS-programmed watermarks untouched and hope for the best.
  12644. */
  12645. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12646. goto put_state;
  12647. }
  12648. /* Write calculated watermark values back */
  12649. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12650. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12651. cs->wm.need_postvbl_update = true;
  12652. dev_priv->display.optimize_watermarks(intel_state, cs);
  12653. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12654. }
  12655. put_state:
  12656. drm_atomic_state_put(state);
  12657. fail:
  12658. drm_modeset_drop_locks(&ctx);
  12659. drm_modeset_acquire_fini(&ctx);
  12660. }
  12661. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12662. {
  12663. if (IS_GEN5(dev_priv)) {
  12664. u32 fdi_pll_clk =
  12665. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12666. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12667. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12668. dev_priv->fdi_pll_freq = 270000;
  12669. } else {
  12670. return;
  12671. }
  12672. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12673. }
  12674. int intel_modeset_init(struct drm_device *dev)
  12675. {
  12676. struct drm_i915_private *dev_priv = to_i915(dev);
  12677. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12678. enum pipe pipe;
  12679. struct intel_crtc *crtc;
  12680. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12681. drm_mode_config_init(dev);
  12682. dev->mode_config.min_width = 0;
  12683. dev->mode_config.min_height = 0;
  12684. dev->mode_config.preferred_depth = 24;
  12685. dev->mode_config.prefer_shadow = 1;
  12686. dev->mode_config.allow_fb_modifiers = true;
  12687. dev->mode_config.funcs = &intel_mode_funcs;
  12688. init_llist_head(&dev_priv->atomic_helper.free_list);
  12689. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12690. intel_atomic_helper_free_state_worker);
  12691. intel_init_quirks(dev);
  12692. intel_init_pm(dev_priv);
  12693. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12694. return 0;
  12695. /*
  12696. * There may be no VBT; and if the BIOS enabled SSC we can
  12697. * just keep using it to avoid unnecessary flicker. Whereas if the
  12698. * BIOS isn't using it, don't assume it will work even if the VBT
  12699. * indicates as much.
  12700. */
  12701. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12702. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12703. DREF_SSC1_ENABLE);
  12704. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12705. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12706. bios_lvds_use_ssc ? "en" : "dis",
  12707. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12708. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12709. }
  12710. }
  12711. /* maximum framebuffer dimensions */
  12712. if (IS_GEN2(dev_priv)) {
  12713. dev->mode_config.max_width = 2048;
  12714. dev->mode_config.max_height = 2048;
  12715. } else if (IS_GEN3(dev_priv)) {
  12716. dev->mode_config.max_width = 4096;
  12717. dev->mode_config.max_height = 4096;
  12718. } else {
  12719. dev->mode_config.max_width = 8192;
  12720. dev->mode_config.max_height = 8192;
  12721. }
  12722. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12723. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12724. dev->mode_config.cursor_height = 1023;
  12725. } else if (IS_GEN2(dev_priv)) {
  12726. dev->mode_config.cursor_width = 64;
  12727. dev->mode_config.cursor_height = 64;
  12728. } else {
  12729. dev->mode_config.cursor_width = 256;
  12730. dev->mode_config.cursor_height = 256;
  12731. }
  12732. dev->mode_config.fb_base = ggtt->gmadr.start;
  12733. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12734. INTEL_INFO(dev_priv)->num_pipes,
  12735. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12736. for_each_pipe(dev_priv, pipe) {
  12737. int ret;
  12738. ret = intel_crtc_init(dev_priv, pipe);
  12739. if (ret) {
  12740. drm_mode_config_cleanup(dev);
  12741. return ret;
  12742. }
  12743. }
  12744. intel_shared_dpll_init(dev);
  12745. intel_update_fdi_pll_freq(dev_priv);
  12746. intel_update_czclk(dev_priv);
  12747. intel_modeset_init_hw(dev);
  12748. if (dev_priv->max_cdclk_freq == 0)
  12749. intel_update_max_cdclk(dev_priv);
  12750. /* Just disable it once at startup */
  12751. i915_disable_vga(dev_priv);
  12752. intel_setup_outputs(dev_priv);
  12753. drm_modeset_lock_all(dev);
  12754. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12755. drm_modeset_unlock_all(dev);
  12756. for_each_intel_crtc(dev, crtc) {
  12757. struct intel_initial_plane_config plane_config = {};
  12758. if (!crtc->active)
  12759. continue;
  12760. /*
  12761. * Note that reserving the BIOS fb up front prevents us
  12762. * from stuffing other stolen allocations like the ring
  12763. * on top. This prevents some ugliness at boot time, and
  12764. * can even allow for smooth boot transitions if the BIOS
  12765. * fb is large enough for the active pipe configuration.
  12766. */
  12767. dev_priv->display.get_initial_plane_config(crtc,
  12768. &plane_config);
  12769. /*
  12770. * If the fb is shared between multiple heads, we'll
  12771. * just get the first one.
  12772. */
  12773. intel_find_initial_plane_obj(crtc, &plane_config);
  12774. }
  12775. /*
  12776. * Make sure hardware watermarks really match the state we read out.
  12777. * Note that we need to do this after reconstructing the BIOS fb's
  12778. * since the watermark calculation done here will use pstate->fb.
  12779. */
  12780. if (!HAS_GMCH_DISPLAY(dev_priv))
  12781. sanitize_watermarks(dev);
  12782. return 0;
  12783. }
  12784. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12785. {
  12786. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12787. /* 640x480@60Hz, ~25175 kHz */
  12788. struct dpll clock = {
  12789. .m1 = 18,
  12790. .m2 = 7,
  12791. .p1 = 13,
  12792. .p2 = 4,
  12793. .n = 2,
  12794. };
  12795. u32 dpll, fp;
  12796. int i;
  12797. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12798. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12799. pipe_name(pipe), clock.vco, clock.dot);
  12800. fp = i9xx_dpll_compute_fp(&clock);
  12801. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12802. DPLL_VGA_MODE_DIS |
  12803. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12804. PLL_P2_DIVIDE_BY_4 |
  12805. PLL_REF_INPUT_DREFCLK |
  12806. DPLL_VCO_ENABLE;
  12807. I915_WRITE(FP0(pipe), fp);
  12808. I915_WRITE(FP1(pipe), fp);
  12809. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12810. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12811. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12812. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12813. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12814. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12815. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12816. /*
  12817. * Apparently we need to have VGA mode enabled prior to changing
  12818. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12819. * dividers, even though the register value does change.
  12820. */
  12821. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12822. I915_WRITE(DPLL(pipe), dpll);
  12823. /* Wait for the clocks to stabilize. */
  12824. POSTING_READ(DPLL(pipe));
  12825. udelay(150);
  12826. /* The pixel multiplier can only be updated once the
  12827. * DPLL is enabled and the clocks are stable.
  12828. *
  12829. * So write it again.
  12830. */
  12831. I915_WRITE(DPLL(pipe), dpll);
  12832. /* We do this three times for luck */
  12833. for (i = 0; i < 3 ; i++) {
  12834. I915_WRITE(DPLL(pipe), dpll);
  12835. POSTING_READ(DPLL(pipe));
  12836. udelay(150); /* wait for warmup */
  12837. }
  12838. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12839. POSTING_READ(PIPECONF(pipe));
  12840. intel_wait_for_pipe_scanline_moving(crtc);
  12841. }
  12842. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12843. {
  12844. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12845. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12846. pipe_name(pipe));
  12847. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12848. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12849. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12850. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
  12851. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
  12852. I915_WRITE(PIPECONF(pipe), 0);
  12853. POSTING_READ(PIPECONF(pipe));
  12854. intel_wait_for_pipe_scanline_stopped(crtc);
  12855. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12856. POSTING_READ(DPLL(pipe));
  12857. }
  12858. static void
  12859. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12860. {
  12861. struct intel_crtc *crtc;
  12862. if (INTEL_GEN(dev_priv) >= 4)
  12863. return;
  12864. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12865. struct intel_plane *plane =
  12866. to_intel_plane(crtc->base.primary);
  12867. struct intel_crtc *plane_crtc;
  12868. enum pipe pipe;
  12869. if (!plane->get_hw_state(plane, &pipe))
  12870. continue;
  12871. if (pipe == crtc->pipe)
  12872. continue;
  12873. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12874. plane->base.name);
  12875. plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12876. intel_plane_disable_noatomic(plane_crtc, plane);
  12877. }
  12878. }
  12879. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12880. {
  12881. struct drm_device *dev = crtc->base.dev;
  12882. struct intel_encoder *encoder;
  12883. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12884. return true;
  12885. return false;
  12886. }
  12887. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12888. {
  12889. struct drm_device *dev = encoder->base.dev;
  12890. struct intel_connector *connector;
  12891. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12892. return connector;
  12893. return NULL;
  12894. }
  12895. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12896. enum pipe pch_transcoder)
  12897. {
  12898. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12899. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12900. }
  12901. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12902. struct drm_modeset_acquire_ctx *ctx)
  12903. {
  12904. struct drm_device *dev = crtc->base.dev;
  12905. struct drm_i915_private *dev_priv = to_i915(dev);
  12906. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12907. /* Clear any frame start delays used for debugging left by the BIOS */
  12908. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12909. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12910. I915_WRITE(reg,
  12911. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12912. }
  12913. if (crtc->active) {
  12914. struct intel_plane *plane;
  12915. /* Disable everything but the primary plane */
  12916. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12917. const struct intel_plane_state *plane_state =
  12918. to_intel_plane_state(plane->base.state);
  12919. if (plane_state->base.visible &&
  12920. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12921. intel_plane_disable_noatomic(crtc, plane);
  12922. }
  12923. }
  12924. /* Adjust the state of the output pipe according to whether we
  12925. * have active connectors/encoders. */
  12926. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12927. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12928. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12929. /*
  12930. * We start out with underrun reporting disabled to avoid races.
  12931. * For correct bookkeeping mark this on active crtcs.
  12932. *
  12933. * Also on gmch platforms we dont have any hardware bits to
  12934. * disable the underrun reporting. Which means we need to start
  12935. * out with underrun reporting disabled also on inactive pipes,
  12936. * since otherwise we'll complain about the garbage we read when
  12937. * e.g. coming up after runtime pm.
  12938. *
  12939. * No protection against concurrent access is required - at
  12940. * worst a fifo underrun happens which also sets this to false.
  12941. */
  12942. crtc->cpu_fifo_underrun_disabled = true;
  12943. /*
  12944. * We track the PCH trancoder underrun reporting state
  12945. * within the crtc. With crtc for pipe A housing the underrun
  12946. * reporting state for PCH transcoder A, crtc for pipe B housing
  12947. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12948. * and marking underrun reporting as disabled for the non-existing
  12949. * PCH transcoders B and C would prevent enabling the south
  12950. * error interrupt (see cpt_can_enable_serr_int()).
  12951. */
  12952. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12953. crtc->pch_fifo_underrun_disabled = true;
  12954. }
  12955. }
  12956. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12957. {
  12958. struct intel_connector *connector;
  12959. /* We need to check both for a crtc link (meaning that the
  12960. * encoder is active and trying to read from a pipe) and the
  12961. * pipe itself being active. */
  12962. bool has_active_crtc = encoder->base.crtc &&
  12963. to_intel_crtc(encoder->base.crtc)->active;
  12964. connector = intel_encoder_find_connector(encoder);
  12965. if (connector && !has_active_crtc) {
  12966. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12967. encoder->base.base.id,
  12968. encoder->base.name);
  12969. /* Connector is active, but has no active pipe. This is
  12970. * fallout from our resume register restoring. Disable
  12971. * the encoder manually again. */
  12972. if (encoder->base.crtc) {
  12973. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12974. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12975. encoder->base.base.id,
  12976. encoder->base.name);
  12977. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12978. if (encoder->post_disable)
  12979. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12980. }
  12981. encoder->base.crtc = NULL;
  12982. /* Inconsistent output/port/pipe state happens presumably due to
  12983. * a bug in one of the get_hw_state functions. Or someplace else
  12984. * in our code, like the register restore mess on resume. Clamp
  12985. * things to off as a safer default. */
  12986. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12987. connector->base.encoder = NULL;
  12988. }
  12989. /* notify opregion of the sanitized encoder state */
  12990. intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
  12991. }
  12992. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12993. {
  12994. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12995. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12996. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12997. i915_disable_vga(dev_priv);
  12998. }
  12999. }
  13000. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  13001. {
  13002. /* This function can be called both from intel_modeset_setup_hw_state or
  13003. * at a very early point in our resume sequence, where the power well
  13004. * structures are not yet restored. Since this function is at a very
  13005. * paranoid "someone might have enabled VGA while we were not looking"
  13006. * level, just check if the power well is enabled instead of trying to
  13007. * follow the "don't touch the power well if we don't need it" policy
  13008. * the rest of the driver uses. */
  13009. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13010. return;
  13011. i915_redisable_vga_power_on(dev_priv);
  13012. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13013. }
  13014. /* FIXME read out full plane state for all planes */
  13015. static void readout_plane_state(struct drm_i915_private *dev_priv)
  13016. {
  13017. struct intel_plane *plane;
  13018. struct intel_crtc *crtc;
  13019. for_each_intel_plane(&dev_priv->drm, plane) {
  13020. struct intel_plane_state *plane_state =
  13021. to_intel_plane_state(plane->base.state);
  13022. struct intel_crtc_state *crtc_state;
  13023. enum pipe pipe = PIPE_A;
  13024. bool visible;
  13025. visible = plane->get_hw_state(plane, &pipe);
  13026. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13027. crtc_state = to_intel_crtc_state(crtc->base.state);
  13028. intel_set_plane_visible(crtc_state, plane_state, visible);
  13029. }
  13030. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13031. struct intel_crtc_state *crtc_state =
  13032. to_intel_crtc_state(crtc->base.state);
  13033. fixup_active_planes(crtc_state);
  13034. }
  13035. }
  13036. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13037. {
  13038. struct drm_i915_private *dev_priv = to_i915(dev);
  13039. enum pipe pipe;
  13040. struct intel_crtc *crtc;
  13041. struct intel_encoder *encoder;
  13042. struct intel_connector *connector;
  13043. struct drm_connector_list_iter conn_iter;
  13044. int i;
  13045. dev_priv->active_crtcs = 0;
  13046. for_each_intel_crtc(dev, crtc) {
  13047. struct intel_crtc_state *crtc_state =
  13048. to_intel_crtc_state(crtc->base.state);
  13049. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13050. memset(crtc_state, 0, sizeof(*crtc_state));
  13051. crtc_state->base.crtc = &crtc->base;
  13052. crtc_state->base.active = crtc_state->base.enable =
  13053. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13054. crtc->base.enabled = crtc_state->base.enable;
  13055. crtc->active = crtc_state->base.active;
  13056. if (crtc_state->base.active)
  13057. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13058. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13059. crtc->base.base.id, crtc->base.name,
  13060. enableddisabled(crtc_state->base.active));
  13061. }
  13062. readout_plane_state(dev_priv);
  13063. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13064. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13065. pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
  13066. &pll->state.hw_state);
  13067. pll->state.crtc_mask = 0;
  13068. for_each_intel_crtc(dev, crtc) {
  13069. struct intel_crtc_state *crtc_state =
  13070. to_intel_crtc_state(crtc->base.state);
  13071. if (crtc_state->base.active &&
  13072. crtc_state->shared_dpll == pll)
  13073. pll->state.crtc_mask |= 1 << crtc->pipe;
  13074. }
  13075. pll->active_mask = pll->state.crtc_mask;
  13076. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13077. pll->info->name, pll->state.crtc_mask, pll->on);
  13078. }
  13079. for_each_intel_encoder(dev, encoder) {
  13080. pipe = 0;
  13081. if (encoder->get_hw_state(encoder, &pipe)) {
  13082. struct intel_crtc_state *crtc_state;
  13083. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13084. crtc_state = to_intel_crtc_state(crtc->base.state);
  13085. encoder->base.crtc = &crtc->base;
  13086. encoder->get_config(encoder, crtc_state);
  13087. } else {
  13088. encoder->base.crtc = NULL;
  13089. }
  13090. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13091. encoder->base.base.id, encoder->base.name,
  13092. enableddisabled(encoder->base.crtc),
  13093. pipe_name(pipe));
  13094. }
  13095. drm_connector_list_iter_begin(dev, &conn_iter);
  13096. for_each_intel_connector_iter(connector, &conn_iter) {
  13097. if (connector->get_hw_state(connector)) {
  13098. connector->base.dpms = DRM_MODE_DPMS_ON;
  13099. encoder = connector->encoder;
  13100. connector->base.encoder = &encoder->base;
  13101. if (encoder->base.crtc &&
  13102. encoder->base.crtc->state->active) {
  13103. /*
  13104. * This has to be done during hardware readout
  13105. * because anything calling .crtc_disable may
  13106. * rely on the connector_mask being accurate.
  13107. */
  13108. encoder->base.crtc->state->connector_mask |=
  13109. drm_connector_mask(&connector->base);
  13110. encoder->base.crtc->state->encoder_mask |=
  13111. drm_encoder_mask(&encoder->base);
  13112. }
  13113. } else {
  13114. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13115. connector->base.encoder = NULL;
  13116. }
  13117. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13118. connector->base.base.id, connector->base.name,
  13119. enableddisabled(connector->base.encoder));
  13120. }
  13121. drm_connector_list_iter_end(&conn_iter);
  13122. for_each_intel_crtc(dev, crtc) {
  13123. struct intel_crtc_state *crtc_state =
  13124. to_intel_crtc_state(crtc->base.state);
  13125. int min_cdclk = 0;
  13126. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13127. if (crtc_state->base.active) {
  13128. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  13129. crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
  13130. crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
  13131. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  13132. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13133. /*
  13134. * The initial mode needs to be set in order to keep
  13135. * the atomic core happy. It wants a valid mode if the
  13136. * crtc's enabled, so we do the above call.
  13137. *
  13138. * But we don't set all the derived state fully, hence
  13139. * set a flag to indicate that a full recalculation is
  13140. * needed on the next commit.
  13141. */
  13142. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  13143. intel_crtc_compute_pixel_rate(crtc_state);
  13144. if (dev_priv->display.modeset_calc_cdclk) {
  13145. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  13146. if (WARN_ON(min_cdclk < 0))
  13147. min_cdclk = 0;
  13148. }
  13149. drm_calc_timestamping_constants(&crtc->base,
  13150. &crtc_state->base.adjusted_mode);
  13151. update_scanline_offset(crtc);
  13152. }
  13153. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  13154. dev_priv->min_voltage_level[crtc->pipe] =
  13155. crtc_state->min_voltage_level;
  13156. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  13157. }
  13158. }
  13159. static void
  13160. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  13161. {
  13162. struct intel_encoder *encoder;
  13163. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13164. u64 get_domains;
  13165. enum intel_display_power_domain domain;
  13166. struct intel_crtc_state *crtc_state;
  13167. if (!encoder->get_power_domains)
  13168. continue;
  13169. /*
  13170. * MST-primary and inactive encoders don't have a crtc state
  13171. * and neither of these require any power domain references.
  13172. */
  13173. if (!encoder->base.crtc)
  13174. continue;
  13175. crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
  13176. get_domains = encoder->get_power_domains(encoder, crtc_state);
  13177. for_each_power_domain(domain, get_domains)
  13178. intel_display_power_get(dev_priv, domain);
  13179. }
  13180. }
  13181. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  13182. {
  13183. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  13184. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  13185. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  13186. DARBF_GATING_DIS);
  13187. if (IS_HASWELL(dev_priv)) {
  13188. /*
  13189. * WaRsPkgCStateDisplayPMReq:hsw
  13190. * System hang if this isn't done before disabling all planes!
  13191. */
  13192. I915_WRITE(CHICKEN_PAR1_1,
  13193. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  13194. }
  13195. }
  13196. /* Scan out the current hw modeset state,
  13197. * and sanitizes it to the current state
  13198. */
  13199. static void
  13200. intel_modeset_setup_hw_state(struct drm_device *dev,
  13201. struct drm_modeset_acquire_ctx *ctx)
  13202. {
  13203. struct drm_i915_private *dev_priv = to_i915(dev);
  13204. struct intel_crtc *crtc;
  13205. struct intel_encoder *encoder;
  13206. int i;
  13207. intel_early_display_was(dev_priv);
  13208. intel_modeset_readout_hw_state(dev);
  13209. /* HW state is read out, now we need to sanitize this mess. */
  13210. get_encoder_power_domains(dev_priv);
  13211. /*
  13212. * intel_sanitize_plane_mapping() may need to do vblank
  13213. * waits, so we need vblank interrupts restored beforehand.
  13214. */
  13215. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13216. drm_crtc_vblank_reset(&crtc->base);
  13217. if (crtc->active)
  13218. drm_crtc_vblank_on(&crtc->base);
  13219. }
  13220. intel_sanitize_plane_mapping(dev_priv);
  13221. for_each_intel_encoder(dev, encoder)
  13222. intel_sanitize_encoder(encoder);
  13223. for_each_intel_crtc(&dev_priv->drm, crtc) {
  13224. intel_sanitize_crtc(crtc, ctx);
  13225. intel_dump_pipe_config(crtc, crtc->config,
  13226. "[setup_hw_state]");
  13227. }
  13228. intel_modeset_update_connector_atomic_state(dev);
  13229. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13230. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13231. if (!pll->on || pll->active_mask)
  13232. continue;
  13233. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
  13234. pll->info->name);
  13235. pll->info->funcs->disable(dev_priv, pll);
  13236. pll->on = false;
  13237. }
  13238. if (IS_G4X(dev_priv)) {
  13239. g4x_wm_get_hw_state(dev);
  13240. g4x_wm_sanitize(dev_priv);
  13241. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13242. vlv_wm_get_hw_state(dev);
  13243. vlv_wm_sanitize(dev_priv);
  13244. } else if (INTEL_GEN(dev_priv) >= 9) {
  13245. skl_wm_get_hw_state(dev);
  13246. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13247. ilk_wm_get_hw_state(dev);
  13248. }
  13249. for_each_intel_crtc(dev, crtc) {
  13250. u64 put_domains;
  13251. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13252. if (WARN_ON(put_domains))
  13253. modeset_put_power_domains(dev_priv, put_domains);
  13254. }
  13255. intel_display_set_init_power(dev_priv, false);
  13256. intel_power_domains_verify_state(dev_priv);
  13257. intel_fbc_init_pipe_state(dev_priv);
  13258. }
  13259. void intel_display_resume(struct drm_device *dev)
  13260. {
  13261. struct drm_i915_private *dev_priv = to_i915(dev);
  13262. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13263. struct drm_modeset_acquire_ctx ctx;
  13264. int ret;
  13265. dev_priv->modeset_restore_state = NULL;
  13266. if (state)
  13267. state->acquire_ctx = &ctx;
  13268. drm_modeset_acquire_init(&ctx, 0);
  13269. while (1) {
  13270. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13271. if (ret != -EDEADLK)
  13272. break;
  13273. drm_modeset_backoff(&ctx);
  13274. }
  13275. if (!ret)
  13276. ret = __intel_display_resume(dev, state, &ctx);
  13277. intel_enable_ipc(dev_priv);
  13278. drm_modeset_drop_locks(&ctx);
  13279. drm_modeset_acquire_fini(&ctx);
  13280. if (ret)
  13281. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13282. if (state)
  13283. drm_atomic_state_put(state);
  13284. }
  13285. int intel_connector_register(struct drm_connector *connector)
  13286. {
  13287. struct intel_connector *intel_connector = to_intel_connector(connector);
  13288. int ret;
  13289. ret = intel_backlight_device_register(intel_connector);
  13290. if (ret)
  13291. goto err;
  13292. return 0;
  13293. err:
  13294. return ret;
  13295. }
  13296. void intel_connector_unregister(struct drm_connector *connector)
  13297. {
  13298. struct intel_connector *intel_connector = to_intel_connector(connector);
  13299. intel_backlight_device_unregister(intel_connector);
  13300. intel_panel_destroy_backlight(connector);
  13301. }
  13302. static void intel_hpd_poll_fini(struct drm_device *dev)
  13303. {
  13304. struct intel_connector *connector;
  13305. struct drm_connector_list_iter conn_iter;
  13306. /* Kill all the work that may have been queued by hpd. */
  13307. drm_connector_list_iter_begin(dev, &conn_iter);
  13308. for_each_intel_connector_iter(connector, &conn_iter) {
  13309. if (connector->modeset_retry_work.func)
  13310. cancel_work_sync(&connector->modeset_retry_work);
  13311. if (connector->hdcp_shim) {
  13312. cancel_delayed_work_sync(&connector->hdcp_check_work);
  13313. cancel_work_sync(&connector->hdcp_prop_work);
  13314. }
  13315. }
  13316. drm_connector_list_iter_end(&conn_iter);
  13317. }
  13318. void intel_modeset_cleanup(struct drm_device *dev)
  13319. {
  13320. struct drm_i915_private *dev_priv = to_i915(dev);
  13321. flush_workqueue(dev_priv->modeset_wq);
  13322. flush_work(&dev_priv->atomic_helper.free_work);
  13323. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13324. /*
  13325. * Interrupts and polling as the first thing to avoid creating havoc.
  13326. * Too much stuff here (turning of connectors, ...) would
  13327. * experience fancy races otherwise.
  13328. */
  13329. intel_irq_uninstall(dev_priv);
  13330. /*
  13331. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13332. * poll handlers. Hence disable polling after hpd handling is shut down.
  13333. */
  13334. intel_hpd_poll_fini(dev);
  13335. /* poll work can call into fbdev, hence clean that up afterwards */
  13336. intel_fbdev_fini(dev_priv);
  13337. intel_unregister_dsm_handler();
  13338. intel_fbc_global_disable(dev_priv);
  13339. /* flush any delayed tasks or pending work */
  13340. flush_scheduled_work();
  13341. drm_mode_config_cleanup(dev);
  13342. intel_cleanup_overlay(dev_priv);
  13343. intel_teardown_gmbus(dev_priv);
  13344. destroy_workqueue(dev_priv->modeset_wq);
  13345. }
  13346. void intel_connector_attach_encoder(struct intel_connector *connector,
  13347. struct intel_encoder *encoder)
  13348. {
  13349. connector->encoder = encoder;
  13350. drm_connector_attach_encoder(&connector->base, &encoder->base);
  13351. }
  13352. /*
  13353. * set vga decode state - true == enable VGA decode
  13354. */
  13355. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13356. {
  13357. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13358. u16 gmch_ctrl;
  13359. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13360. DRM_ERROR("failed to read control word\n");
  13361. return -EIO;
  13362. }
  13363. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13364. return 0;
  13365. if (state)
  13366. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13367. else
  13368. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13369. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13370. DRM_ERROR("failed to write control word\n");
  13371. return -EIO;
  13372. }
  13373. return 0;
  13374. }
  13375. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13376. struct intel_display_error_state {
  13377. u32 power_well_driver;
  13378. int num_transcoders;
  13379. struct intel_cursor_error_state {
  13380. u32 control;
  13381. u32 position;
  13382. u32 base;
  13383. u32 size;
  13384. } cursor[I915_MAX_PIPES];
  13385. struct intel_pipe_error_state {
  13386. bool power_domain_on;
  13387. u32 source;
  13388. u32 stat;
  13389. } pipe[I915_MAX_PIPES];
  13390. struct intel_plane_error_state {
  13391. u32 control;
  13392. u32 stride;
  13393. u32 size;
  13394. u32 pos;
  13395. u32 addr;
  13396. u32 surface;
  13397. u32 tile_offset;
  13398. } plane[I915_MAX_PIPES];
  13399. struct intel_transcoder_error_state {
  13400. bool power_domain_on;
  13401. enum transcoder cpu_transcoder;
  13402. u32 conf;
  13403. u32 htotal;
  13404. u32 hblank;
  13405. u32 hsync;
  13406. u32 vtotal;
  13407. u32 vblank;
  13408. u32 vsync;
  13409. } transcoder[4];
  13410. };
  13411. struct intel_display_error_state *
  13412. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13413. {
  13414. struct intel_display_error_state *error;
  13415. int transcoders[] = {
  13416. TRANSCODER_A,
  13417. TRANSCODER_B,
  13418. TRANSCODER_C,
  13419. TRANSCODER_EDP,
  13420. };
  13421. int i;
  13422. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13423. return NULL;
  13424. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13425. if (error == NULL)
  13426. return NULL;
  13427. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13428. error->power_well_driver =
  13429. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  13430. for_each_pipe(dev_priv, i) {
  13431. error->pipe[i].power_domain_on =
  13432. __intel_display_power_is_enabled(dev_priv,
  13433. POWER_DOMAIN_PIPE(i));
  13434. if (!error->pipe[i].power_domain_on)
  13435. continue;
  13436. error->cursor[i].control = I915_READ(CURCNTR(i));
  13437. error->cursor[i].position = I915_READ(CURPOS(i));
  13438. error->cursor[i].base = I915_READ(CURBASE(i));
  13439. error->plane[i].control = I915_READ(DSPCNTR(i));
  13440. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13441. if (INTEL_GEN(dev_priv) <= 3) {
  13442. error->plane[i].size = I915_READ(DSPSIZE(i));
  13443. error->plane[i].pos = I915_READ(DSPPOS(i));
  13444. }
  13445. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13446. error->plane[i].addr = I915_READ(DSPADDR(i));
  13447. if (INTEL_GEN(dev_priv) >= 4) {
  13448. error->plane[i].surface = I915_READ(DSPSURF(i));
  13449. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13450. }
  13451. error->pipe[i].source = I915_READ(PIPESRC(i));
  13452. if (HAS_GMCH_DISPLAY(dev_priv))
  13453. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13454. }
  13455. /* Note: this does not include DSI transcoders. */
  13456. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13457. if (HAS_DDI(dev_priv))
  13458. error->num_transcoders++; /* Account for eDP. */
  13459. for (i = 0; i < error->num_transcoders; i++) {
  13460. enum transcoder cpu_transcoder = transcoders[i];
  13461. error->transcoder[i].power_domain_on =
  13462. __intel_display_power_is_enabled(dev_priv,
  13463. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13464. if (!error->transcoder[i].power_domain_on)
  13465. continue;
  13466. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13467. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13468. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13469. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13470. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13471. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13472. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13473. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13474. }
  13475. return error;
  13476. }
  13477. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13478. void
  13479. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13480. struct intel_display_error_state *error)
  13481. {
  13482. struct drm_i915_private *dev_priv = m->i915;
  13483. int i;
  13484. if (!error)
  13485. return;
  13486. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13487. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13488. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13489. error->power_well_driver);
  13490. for_each_pipe(dev_priv, i) {
  13491. err_printf(m, "Pipe [%d]:\n", i);
  13492. err_printf(m, " Power: %s\n",
  13493. onoff(error->pipe[i].power_domain_on));
  13494. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13495. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13496. err_printf(m, "Plane [%d]:\n", i);
  13497. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13498. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13499. if (INTEL_GEN(dev_priv) <= 3) {
  13500. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13501. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13502. }
  13503. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13504. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13505. if (INTEL_GEN(dev_priv) >= 4) {
  13506. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13507. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13508. }
  13509. err_printf(m, "Cursor [%d]:\n", i);
  13510. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13511. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13512. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13513. }
  13514. for (i = 0; i < error->num_transcoders; i++) {
  13515. err_printf(m, "CPU transcoder: %s\n",
  13516. transcoder_name(error->transcoder[i].cpu_transcoder));
  13517. err_printf(m, " Power: %s\n",
  13518. onoff(error->transcoder[i].power_domain_on));
  13519. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13520. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13521. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13522. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13523. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13524. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13525. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13526. }
  13527. }
  13528. #endif