intel_display.c 436 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint64_t skl_format_modifiers_noccs[] = {
  86. I915_FORMAT_MOD_Yf_TILED,
  87. I915_FORMAT_MOD_Y_TILED,
  88. I915_FORMAT_MOD_X_TILED,
  89. DRM_FORMAT_MOD_LINEAR,
  90. DRM_FORMAT_MOD_INVALID
  91. };
  92. static const uint64_t skl_format_modifiers_ccs[] = {
  93. I915_FORMAT_MOD_Yf_TILED_CCS,
  94. I915_FORMAT_MOD_Y_TILED_CCS,
  95. I915_FORMAT_MOD_Yf_TILED,
  96. I915_FORMAT_MOD_Y_TILED,
  97. I915_FORMAT_MOD_X_TILED,
  98. DRM_FORMAT_MOD_LINEAR,
  99. DRM_FORMAT_MOD_INVALID
  100. };
  101. /* Cursor formats */
  102. static const uint32_t intel_cursor_formats[] = {
  103. DRM_FORMAT_ARGB8888,
  104. };
  105. static const uint64_t cursor_format_modifiers[] = {
  106. DRM_FORMAT_MOD_LINEAR,
  107. DRM_FORMAT_MOD_INVALID
  108. };
  109. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  110. struct intel_crtc_state *pipe_config);
  111. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  112. struct intel_crtc_state *pipe_config);
  113. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  114. struct drm_i915_gem_object *obj,
  115. struct drm_mode_fb_cmd2 *mode_cmd);
  116. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  117. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  118. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  119. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  120. struct intel_link_m_n *m_n,
  121. struct intel_link_m_n *m2_n2);
  122. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  123. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  124. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  125. static void vlv_prepare_pll(struct intel_crtc *crtc,
  126. const struct intel_crtc_state *pipe_config);
  127. static void chv_prepare_pll(struct intel_crtc *crtc,
  128. const struct intel_crtc_state *pipe_config);
  129. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  130. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  131. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  132. struct intel_crtc_state *crtc_state);
  133. static void skylake_pfit_enable(struct intel_crtc *crtc);
  134. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  135. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  136. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  137. struct drm_modeset_acquire_ctx *ctx);
  138. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  139. struct intel_limit {
  140. struct {
  141. int min, max;
  142. } dot, vco, n, m, m1, m2, p, p1;
  143. struct {
  144. int dot_limit;
  145. int p2_slow, p2_fast;
  146. } p2;
  147. };
  148. /* returns HPLL frequency in kHz */
  149. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  150. {
  151. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  152. /* Obtain SKU information */
  153. mutex_lock(&dev_priv->sb_lock);
  154. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  155. CCK_FUSE_HPLL_FREQ_MASK;
  156. mutex_unlock(&dev_priv->sb_lock);
  157. return vco_freq[hpll_freq] * 1000;
  158. }
  159. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  160. const char *name, u32 reg, int ref_freq)
  161. {
  162. u32 val;
  163. int divider;
  164. mutex_lock(&dev_priv->sb_lock);
  165. val = vlv_cck_read(dev_priv, reg);
  166. mutex_unlock(&dev_priv->sb_lock);
  167. divider = val & CCK_FREQUENCY_VALUES;
  168. WARN((val & CCK_FREQUENCY_STATUS) !=
  169. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  170. "%s change in progress\n", name);
  171. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  172. }
  173. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  174. const char *name, u32 reg)
  175. {
  176. if (dev_priv->hpll_freq == 0)
  177. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  178. return vlv_get_cck_clock(dev_priv, name, reg,
  179. dev_priv->hpll_freq);
  180. }
  181. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  182. {
  183. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  184. return;
  185. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  186. CCK_CZ_CLOCK_CONTROL);
  187. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  188. }
  189. static inline u32 /* units of 100MHz */
  190. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  191. const struct intel_crtc_state *pipe_config)
  192. {
  193. if (HAS_DDI(dev_priv))
  194. return pipe_config->port_clock; /* SPLL */
  195. else
  196. return dev_priv->fdi_pll_freq;
  197. }
  198. static const struct intel_limit intel_limits_i8xx_dac = {
  199. .dot = { .min = 25000, .max = 350000 },
  200. .vco = { .min = 908000, .max = 1512000 },
  201. .n = { .min = 2, .max = 16 },
  202. .m = { .min = 96, .max = 140 },
  203. .m1 = { .min = 18, .max = 26 },
  204. .m2 = { .min = 6, .max = 16 },
  205. .p = { .min = 4, .max = 128 },
  206. .p1 = { .min = 2, .max = 33 },
  207. .p2 = { .dot_limit = 165000,
  208. .p2_slow = 4, .p2_fast = 2 },
  209. };
  210. static const struct intel_limit intel_limits_i8xx_dvo = {
  211. .dot = { .min = 25000, .max = 350000 },
  212. .vco = { .min = 908000, .max = 1512000 },
  213. .n = { .min = 2, .max = 16 },
  214. .m = { .min = 96, .max = 140 },
  215. .m1 = { .min = 18, .max = 26 },
  216. .m2 = { .min = 6, .max = 16 },
  217. .p = { .min = 4, .max = 128 },
  218. .p1 = { .min = 2, .max = 33 },
  219. .p2 = { .dot_limit = 165000,
  220. .p2_slow = 4, .p2_fast = 4 },
  221. };
  222. static const struct intel_limit intel_limits_i8xx_lvds = {
  223. .dot = { .min = 25000, .max = 350000 },
  224. .vco = { .min = 908000, .max = 1512000 },
  225. .n = { .min = 2, .max = 16 },
  226. .m = { .min = 96, .max = 140 },
  227. .m1 = { .min = 18, .max = 26 },
  228. .m2 = { .min = 6, .max = 16 },
  229. .p = { .min = 4, .max = 128 },
  230. .p1 = { .min = 1, .max = 6 },
  231. .p2 = { .dot_limit = 165000,
  232. .p2_slow = 14, .p2_fast = 7 },
  233. };
  234. static const struct intel_limit intel_limits_i9xx_sdvo = {
  235. .dot = { .min = 20000, .max = 400000 },
  236. .vco = { .min = 1400000, .max = 2800000 },
  237. .n = { .min = 1, .max = 6 },
  238. .m = { .min = 70, .max = 120 },
  239. .m1 = { .min = 8, .max = 18 },
  240. .m2 = { .min = 3, .max = 7 },
  241. .p = { .min = 5, .max = 80 },
  242. .p1 = { .min = 1, .max = 8 },
  243. .p2 = { .dot_limit = 200000,
  244. .p2_slow = 10, .p2_fast = 5 },
  245. };
  246. static const struct intel_limit intel_limits_i9xx_lvds = {
  247. .dot = { .min = 20000, .max = 400000 },
  248. .vco = { .min = 1400000, .max = 2800000 },
  249. .n = { .min = 1, .max = 6 },
  250. .m = { .min = 70, .max = 120 },
  251. .m1 = { .min = 8, .max = 18 },
  252. .m2 = { .min = 3, .max = 7 },
  253. .p = { .min = 7, .max = 98 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 112000,
  256. .p2_slow = 14, .p2_fast = 7 },
  257. };
  258. static const struct intel_limit intel_limits_g4x_sdvo = {
  259. .dot = { .min = 25000, .max = 270000 },
  260. .vco = { .min = 1750000, .max = 3500000},
  261. .n = { .min = 1, .max = 4 },
  262. .m = { .min = 104, .max = 138 },
  263. .m1 = { .min = 17, .max = 23 },
  264. .m2 = { .min = 5, .max = 11 },
  265. .p = { .min = 10, .max = 30 },
  266. .p1 = { .min = 1, .max = 3},
  267. .p2 = { .dot_limit = 270000,
  268. .p2_slow = 10,
  269. .p2_fast = 10
  270. },
  271. };
  272. static const struct intel_limit intel_limits_g4x_hdmi = {
  273. .dot = { .min = 22000, .max = 400000 },
  274. .vco = { .min = 1750000, .max = 3500000},
  275. .n = { .min = 1, .max = 4 },
  276. .m = { .min = 104, .max = 138 },
  277. .m1 = { .min = 16, .max = 23 },
  278. .m2 = { .min = 5, .max = 11 },
  279. .p = { .min = 5, .max = 80 },
  280. .p1 = { .min = 1, .max = 8},
  281. .p2 = { .dot_limit = 165000,
  282. .p2_slow = 10, .p2_fast = 5 },
  283. };
  284. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  285. .dot = { .min = 20000, .max = 115000 },
  286. .vco = { .min = 1750000, .max = 3500000 },
  287. .n = { .min = 1, .max = 3 },
  288. .m = { .min = 104, .max = 138 },
  289. .m1 = { .min = 17, .max = 23 },
  290. .m2 = { .min = 5, .max = 11 },
  291. .p = { .min = 28, .max = 112 },
  292. .p1 = { .min = 2, .max = 8 },
  293. .p2 = { .dot_limit = 0,
  294. .p2_slow = 14, .p2_fast = 14
  295. },
  296. };
  297. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  298. .dot = { .min = 80000, .max = 224000 },
  299. .vco = { .min = 1750000, .max = 3500000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 104, .max = 138 },
  302. .m1 = { .min = 17, .max = 23 },
  303. .m2 = { .min = 5, .max = 11 },
  304. .p = { .min = 14, .max = 42 },
  305. .p1 = { .min = 2, .max = 6 },
  306. .p2 = { .dot_limit = 0,
  307. .p2_slow = 7, .p2_fast = 7
  308. },
  309. };
  310. static const struct intel_limit intel_limits_pineview_sdvo = {
  311. .dot = { .min = 20000, .max = 400000},
  312. .vco = { .min = 1700000, .max = 3500000 },
  313. /* Pineview's Ncounter is a ring counter */
  314. .n = { .min = 3, .max = 6 },
  315. .m = { .min = 2, .max = 256 },
  316. /* Pineview only has one combined m divider, which we treat as m2. */
  317. .m1 = { .min = 0, .max = 0 },
  318. .m2 = { .min = 0, .max = 254 },
  319. .p = { .min = 5, .max = 80 },
  320. .p1 = { .min = 1, .max = 8 },
  321. .p2 = { .dot_limit = 200000,
  322. .p2_slow = 10, .p2_fast = 5 },
  323. };
  324. static const struct intel_limit intel_limits_pineview_lvds = {
  325. .dot = { .min = 20000, .max = 400000 },
  326. .vco = { .min = 1700000, .max = 3500000 },
  327. .n = { .min = 3, .max = 6 },
  328. .m = { .min = 2, .max = 256 },
  329. .m1 = { .min = 0, .max = 0 },
  330. .m2 = { .min = 0, .max = 254 },
  331. .p = { .min = 7, .max = 112 },
  332. .p1 = { .min = 1, .max = 8 },
  333. .p2 = { .dot_limit = 112000,
  334. .p2_slow = 14, .p2_fast = 14 },
  335. };
  336. /* Ironlake / Sandybridge
  337. *
  338. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  339. * the range value for them is (actual_value - 2).
  340. */
  341. static const struct intel_limit intel_limits_ironlake_dac = {
  342. .dot = { .min = 25000, .max = 350000 },
  343. .vco = { .min = 1760000, .max = 3510000 },
  344. .n = { .min = 1, .max = 5 },
  345. .m = { .min = 79, .max = 127 },
  346. .m1 = { .min = 12, .max = 22 },
  347. .m2 = { .min = 5, .max = 9 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 225000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  354. .dot = { .min = 25000, .max = 350000 },
  355. .vco = { .min = 1760000, .max = 3510000 },
  356. .n = { .min = 1, .max = 3 },
  357. .m = { .min = 79, .max = 118 },
  358. .m1 = { .min = 12, .max = 22 },
  359. .m2 = { .min = 5, .max = 9 },
  360. .p = { .min = 28, .max = 112 },
  361. .p1 = { .min = 2, .max = 8 },
  362. .p2 = { .dot_limit = 225000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  366. .dot = { .min = 25000, .max = 350000 },
  367. .vco = { .min = 1760000, .max = 3510000 },
  368. .n = { .min = 1, .max = 3 },
  369. .m = { .min = 79, .max = 127 },
  370. .m1 = { .min = 12, .max = 22 },
  371. .m2 = { .min = 5, .max = 9 },
  372. .p = { .min = 14, .max = 56 },
  373. .p1 = { .min = 2, .max = 8 },
  374. .p2 = { .dot_limit = 225000,
  375. .p2_slow = 7, .p2_fast = 7 },
  376. };
  377. /* LVDS 100mhz refclk limits. */
  378. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  379. .dot = { .min = 25000, .max = 350000 },
  380. .vco = { .min = 1760000, .max = 3510000 },
  381. .n = { .min = 1, .max = 2 },
  382. .m = { .min = 79, .max = 126 },
  383. .m1 = { .min = 12, .max = 22 },
  384. .m2 = { .min = 5, .max = 9 },
  385. .p = { .min = 28, .max = 112 },
  386. .p1 = { .min = 2, .max = 8 },
  387. .p2 = { .dot_limit = 225000,
  388. .p2_slow = 14, .p2_fast = 14 },
  389. };
  390. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  391. .dot = { .min = 25000, .max = 350000 },
  392. .vco = { .min = 1760000, .max = 3510000 },
  393. .n = { .min = 1, .max = 3 },
  394. .m = { .min = 79, .max = 126 },
  395. .m1 = { .min = 12, .max = 22 },
  396. .m2 = { .min = 5, .max = 9 },
  397. .p = { .min = 14, .max = 42 },
  398. .p1 = { .min = 2, .max = 6 },
  399. .p2 = { .dot_limit = 225000,
  400. .p2_slow = 7, .p2_fast = 7 },
  401. };
  402. static const struct intel_limit intel_limits_vlv = {
  403. /*
  404. * These are the data rate limits (measured in fast clocks)
  405. * since those are the strictest limits we have. The fast
  406. * clock and actual rate limits are more relaxed, so checking
  407. * them would make no difference.
  408. */
  409. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  410. .vco = { .min = 4000000, .max = 6000000 },
  411. .n = { .min = 1, .max = 7 },
  412. .m1 = { .min = 2, .max = 3 },
  413. .m2 = { .min = 11, .max = 156 },
  414. .p1 = { .min = 2, .max = 3 },
  415. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  416. };
  417. static const struct intel_limit intel_limits_chv = {
  418. /*
  419. * These are the data rate limits (measured in fast clocks)
  420. * since those are the strictest limits we have. The fast
  421. * clock and actual rate limits are more relaxed, so checking
  422. * them would make no difference.
  423. */
  424. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  425. .vco = { .min = 4800000, .max = 6480000 },
  426. .n = { .min = 1, .max = 1 },
  427. .m1 = { .min = 2, .max = 2 },
  428. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  429. .p1 = { .min = 2, .max = 4 },
  430. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  431. };
  432. static const struct intel_limit intel_limits_bxt = {
  433. /* FIXME: find real dot limits */
  434. .dot = { .min = 0, .max = INT_MAX },
  435. .vco = { .min = 4800000, .max = 6700000 },
  436. .n = { .min = 1, .max = 1 },
  437. .m1 = { .min = 2, .max = 2 },
  438. /* FIXME: find real m2 limits */
  439. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  440. .p1 = { .min = 2, .max = 4 },
  441. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  442. };
  443. static bool
  444. needs_modeset(const struct drm_crtc_state *state)
  445. {
  446. return drm_atomic_crtc_needs_modeset(state);
  447. }
  448. /*
  449. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  450. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  451. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  452. * The helpers' return value is the rate of the clock that is fed to the
  453. * display engine's pipe which can be the above fast dot clock rate or a
  454. * divided-down version of it.
  455. */
  456. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  457. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  458. {
  459. clock->m = clock->m2 + 2;
  460. clock->p = clock->p1 * clock->p2;
  461. if (WARN_ON(clock->n == 0 || clock->p == 0))
  462. return 0;
  463. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  464. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  465. return clock->dot;
  466. }
  467. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  468. {
  469. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  470. }
  471. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = i9xx_dpll_compute_m(clock);
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  478. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  479. return clock->dot;
  480. }
  481. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  482. {
  483. clock->m = clock->m1 * clock->m2;
  484. clock->p = clock->p1 * clock->p2;
  485. if (WARN_ON(clock->n == 0 || clock->p == 0))
  486. return 0;
  487. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  488. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  489. return clock->dot / 5;
  490. }
  491. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m1 * clock->m2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  498. clock->n << 22);
  499. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  500. return clock->dot / 5;
  501. }
  502. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  503. /**
  504. * Returns whether the given set of divisors are valid for a given refclk with
  505. * the given connectors.
  506. */
  507. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  508. const struct intel_limit *limit,
  509. const struct dpll *clock)
  510. {
  511. if (clock->n < limit->n.min || limit->n.max < clock->n)
  512. INTELPllInvalid("n out of range\n");
  513. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  514. INTELPllInvalid("p1 out of range\n");
  515. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  516. INTELPllInvalid("m2 out of range\n");
  517. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  518. INTELPllInvalid("m1 out of range\n");
  519. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  520. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  521. if (clock->m1 <= clock->m2)
  522. INTELPllInvalid("m1 <= m2\n");
  523. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  524. !IS_GEN9_LP(dev_priv)) {
  525. if (clock->p < limit->p.min || limit->p.max < clock->p)
  526. INTELPllInvalid("p out of range\n");
  527. if (clock->m < limit->m.min || limit->m.max < clock->m)
  528. INTELPllInvalid("m out of range\n");
  529. }
  530. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  531. INTELPllInvalid("vco out of range\n");
  532. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  533. * connector, etc., rather than just a single range.
  534. */
  535. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  536. INTELPllInvalid("dot out of range\n");
  537. return true;
  538. }
  539. static int
  540. i9xx_select_p2_div(const struct intel_limit *limit,
  541. const struct intel_crtc_state *crtc_state,
  542. int target)
  543. {
  544. struct drm_device *dev = crtc_state->base.crtc->dev;
  545. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  546. /*
  547. * For LVDS just rely on its current settings for dual-channel.
  548. * We haven't figured out how to reliably set up different
  549. * single/dual channel state, if we even can.
  550. */
  551. if (intel_is_dual_link_lvds(dev))
  552. return limit->p2.p2_fast;
  553. else
  554. return limit->p2.p2_slow;
  555. } else {
  556. if (target < limit->p2.dot_limit)
  557. return limit->p2.p2_slow;
  558. else
  559. return limit->p2.p2_fast;
  560. }
  561. }
  562. /*
  563. * Returns a set of divisors for the desired target clock with the given
  564. * refclk, or FALSE. The returned values represent the clock equation:
  565. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  566. *
  567. * Target and reference clocks are specified in kHz.
  568. *
  569. * If match_clock is provided, then best_clock P divider must match the P
  570. * divider from @match_clock used for LVDS downclocking.
  571. */
  572. static bool
  573. i9xx_find_best_dpll(const struct intel_limit *limit,
  574. struct intel_crtc_state *crtc_state,
  575. int target, int refclk, struct dpll *match_clock,
  576. struct dpll *best_clock)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. struct dpll clock;
  580. int err = target;
  581. memset(best_clock, 0, sizeof(*best_clock));
  582. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  583. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  584. clock.m1++) {
  585. for (clock.m2 = limit->m2.min;
  586. clock.m2 <= limit->m2.max; clock.m2++) {
  587. if (clock.m2 >= clock.m1)
  588. break;
  589. for (clock.n = limit->n.min;
  590. clock.n <= limit->n.max; clock.n++) {
  591. for (clock.p1 = limit->p1.min;
  592. clock.p1 <= limit->p1.max; clock.p1++) {
  593. int this_err;
  594. i9xx_calc_dpll_params(refclk, &clock);
  595. if (!intel_PLL_is_valid(to_i915(dev),
  596. limit,
  597. &clock))
  598. continue;
  599. if (match_clock &&
  600. clock.p != match_clock->p)
  601. continue;
  602. this_err = abs(clock.dot - target);
  603. if (this_err < err) {
  604. *best_clock = clock;
  605. err = this_err;
  606. }
  607. }
  608. }
  609. }
  610. }
  611. return (err != target);
  612. }
  613. /*
  614. * Returns a set of divisors for the desired target clock with the given
  615. * refclk, or FALSE. The returned values represent the clock equation:
  616. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  617. *
  618. * Target and reference clocks are specified in kHz.
  619. *
  620. * If match_clock is provided, then best_clock P divider must match the P
  621. * divider from @match_clock used for LVDS downclocking.
  622. */
  623. static bool
  624. pnv_find_best_dpll(const struct intel_limit *limit,
  625. struct intel_crtc_state *crtc_state,
  626. int target, int refclk, struct dpll *match_clock,
  627. struct dpll *best_clock)
  628. {
  629. struct drm_device *dev = crtc_state->base.crtc->dev;
  630. struct dpll clock;
  631. int err = target;
  632. memset(best_clock, 0, sizeof(*best_clock));
  633. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  634. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  635. clock.m1++) {
  636. for (clock.m2 = limit->m2.min;
  637. clock.m2 <= limit->m2.max; clock.m2++) {
  638. for (clock.n = limit->n.min;
  639. clock.n <= limit->n.max; clock.n++) {
  640. for (clock.p1 = limit->p1.min;
  641. clock.p1 <= limit->p1.max; clock.p1++) {
  642. int this_err;
  643. pnv_calc_dpll_params(refclk, &clock);
  644. if (!intel_PLL_is_valid(to_i915(dev),
  645. limit,
  646. &clock))
  647. continue;
  648. if (match_clock &&
  649. clock.p != match_clock->p)
  650. continue;
  651. this_err = abs(clock.dot - target);
  652. if (this_err < err) {
  653. *best_clock = clock;
  654. err = this_err;
  655. }
  656. }
  657. }
  658. }
  659. }
  660. return (err != target);
  661. }
  662. /*
  663. * Returns a set of divisors for the desired target clock with the given
  664. * refclk, or FALSE. The returned values represent the clock equation:
  665. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  666. *
  667. * Target and reference clocks are specified in kHz.
  668. *
  669. * If match_clock is provided, then best_clock P divider must match the P
  670. * divider from @match_clock used for LVDS downclocking.
  671. */
  672. static bool
  673. g4x_find_best_dpll(const struct intel_limit *limit,
  674. struct intel_crtc_state *crtc_state,
  675. int target, int refclk, struct dpll *match_clock,
  676. struct dpll *best_clock)
  677. {
  678. struct drm_device *dev = crtc_state->base.crtc->dev;
  679. struct dpll clock;
  680. int max_n;
  681. bool found = false;
  682. /* approximately equals target * 0.00585 */
  683. int err_most = (target >> 8) + (target >> 9);
  684. memset(best_clock, 0, sizeof(*best_clock));
  685. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  686. max_n = limit->n.max;
  687. /* based on hardware requirement, prefer smaller n to precision */
  688. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  689. /* based on hardware requirement, prefere larger m1,m2 */
  690. for (clock.m1 = limit->m1.max;
  691. clock.m1 >= limit->m1.min; clock.m1--) {
  692. for (clock.m2 = limit->m2.max;
  693. clock.m2 >= limit->m2.min; clock.m2--) {
  694. for (clock.p1 = limit->p1.max;
  695. clock.p1 >= limit->p1.min; clock.p1--) {
  696. int this_err;
  697. i9xx_calc_dpll_params(refclk, &clock);
  698. if (!intel_PLL_is_valid(to_i915(dev),
  699. limit,
  700. &clock))
  701. continue;
  702. this_err = abs(clock.dot - target);
  703. if (this_err < err_most) {
  704. *best_clock = clock;
  705. err_most = this_err;
  706. max_n = clock.n;
  707. found = true;
  708. }
  709. }
  710. }
  711. }
  712. }
  713. return found;
  714. }
  715. /*
  716. * Check if the calculated PLL configuration is more optimal compared to the
  717. * best configuration and error found so far. Return the calculated error.
  718. */
  719. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  720. const struct dpll *calculated_clock,
  721. const struct dpll *best_clock,
  722. unsigned int best_error_ppm,
  723. unsigned int *error_ppm)
  724. {
  725. /*
  726. * For CHV ignore the error and consider only the P value.
  727. * Prefer a bigger P value based on HW requirements.
  728. */
  729. if (IS_CHERRYVIEW(to_i915(dev))) {
  730. *error_ppm = 0;
  731. return calculated_clock->p > best_clock->p;
  732. }
  733. if (WARN_ON_ONCE(!target_freq))
  734. return false;
  735. *error_ppm = div_u64(1000000ULL *
  736. abs(target_freq - calculated_clock->dot),
  737. target_freq);
  738. /*
  739. * Prefer a better P value over a better (smaller) error if the error
  740. * is small. Ensure this preference for future configurations too by
  741. * setting the error to 0.
  742. */
  743. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  744. *error_ppm = 0;
  745. return true;
  746. }
  747. return *error_ppm + 10 < best_error_ppm;
  748. }
  749. /*
  750. * Returns a set of divisors for the desired target clock with the given
  751. * refclk, or FALSE. The returned values represent the clock equation:
  752. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  753. */
  754. static bool
  755. vlv_find_best_dpll(const struct intel_limit *limit,
  756. struct intel_crtc_state *crtc_state,
  757. int target, int refclk, struct dpll *match_clock,
  758. struct dpll *best_clock)
  759. {
  760. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  761. struct drm_device *dev = crtc->base.dev;
  762. struct dpll clock;
  763. unsigned int bestppm = 1000000;
  764. /* min update 19.2 MHz */
  765. int max_n = min(limit->n.max, refclk / 19200);
  766. bool found = false;
  767. target *= 5; /* fast clock */
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. /* based on hardware requirement, prefer smaller n to precision */
  770. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  771. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  772. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  773. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  774. clock.p = clock.p1 * clock.p2;
  775. /* based on hardware requirement, prefer bigger m1,m2 values */
  776. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  777. unsigned int ppm;
  778. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  779. refclk * clock.m1);
  780. vlv_calc_dpll_params(refclk, &clock);
  781. if (!intel_PLL_is_valid(to_i915(dev),
  782. limit,
  783. &clock))
  784. continue;
  785. if (!vlv_PLL_is_optimal(dev, target,
  786. &clock,
  787. best_clock,
  788. bestppm, &ppm))
  789. continue;
  790. *best_clock = clock;
  791. bestppm = ppm;
  792. found = true;
  793. }
  794. }
  795. }
  796. }
  797. return found;
  798. }
  799. /*
  800. * Returns a set of divisors for the desired target clock with the given
  801. * refclk, or FALSE. The returned values represent the clock equation:
  802. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  803. */
  804. static bool
  805. chv_find_best_dpll(const struct intel_limit *limit,
  806. struct intel_crtc_state *crtc_state,
  807. int target, int refclk, struct dpll *match_clock,
  808. struct dpll *best_clock)
  809. {
  810. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  811. struct drm_device *dev = crtc->base.dev;
  812. unsigned int best_error_ppm;
  813. struct dpll clock;
  814. uint64_t m2;
  815. int found = false;
  816. memset(best_clock, 0, sizeof(*best_clock));
  817. best_error_ppm = 1000000;
  818. /*
  819. * Based on hardware doc, the n always set to 1, and m1 always
  820. * set to 2. If requires to support 200Mhz refclk, we need to
  821. * revisit this because n may not 1 anymore.
  822. */
  823. clock.n = 1, clock.m1 = 2;
  824. target *= 5; /* fast clock */
  825. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  826. for (clock.p2 = limit->p2.p2_fast;
  827. clock.p2 >= limit->p2.p2_slow;
  828. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  829. unsigned int error_ppm;
  830. clock.p = clock.p1 * clock.p2;
  831. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  832. clock.n) << 22, refclk * clock.m1);
  833. if (m2 > INT_MAX/clock.m1)
  834. continue;
  835. clock.m2 = m2;
  836. chv_calc_dpll_params(refclk, &clock);
  837. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  838. continue;
  839. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  840. best_error_ppm, &error_ppm))
  841. continue;
  842. *best_clock = clock;
  843. best_error_ppm = error_ppm;
  844. found = true;
  845. }
  846. }
  847. return found;
  848. }
  849. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  850. struct dpll *best_clock)
  851. {
  852. int refclk = 100000;
  853. const struct intel_limit *limit = &intel_limits_bxt;
  854. return chv_find_best_dpll(limit, crtc_state,
  855. target_clock, refclk, NULL, best_clock);
  856. }
  857. bool intel_crtc_active(struct intel_crtc *crtc)
  858. {
  859. /* Be paranoid as we can arrive here with only partial
  860. * state retrieved from the hardware during setup.
  861. *
  862. * We can ditch the adjusted_mode.crtc_clock check as soon
  863. * as Haswell has gained clock readout/fastboot support.
  864. *
  865. * We can ditch the crtc->primary->fb check as soon as we can
  866. * properly reconstruct framebuffers.
  867. *
  868. * FIXME: The intel_crtc->active here should be switched to
  869. * crtc->state->active once we have proper CRTC states wired up
  870. * for atomic.
  871. */
  872. return crtc->active && crtc->base.primary->state->fb &&
  873. crtc->config->base.adjusted_mode.crtc_clock;
  874. }
  875. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  876. enum pipe pipe)
  877. {
  878. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  879. return crtc->config->cpu_transcoder;
  880. }
  881. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  882. enum pipe pipe)
  883. {
  884. i915_reg_t reg = PIPEDSL(pipe);
  885. u32 line1, line2;
  886. u32 line_mask;
  887. if (IS_GEN2(dev_priv))
  888. line_mask = DSL_LINEMASK_GEN2;
  889. else
  890. line_mask = DSL_LINEMASK_GEN3;
  891. line1 = I915_READ(reg) & line_mask;
  892. msleep(5);
  893. line2 = I915_READ(reg) & line_mask;
  894. return line1 != line2;
  895. }
  896. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  897. {
  898. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  899. enum pipe pipe = crtc->pipe;
  900. /* Wait for the display line to settle/start moving */
  901. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  902. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  903. pipe_name(pipe), onoff(state));
  904. }
  905. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  906. {
  907. wait_for_pipe_scanline_moving(crtc, false);
  908. }
  909. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  910. {
  911. wait_for_pipe_scanline_moving(crtc, true);
  912. }
  913. static void
  914. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  915. {
  916. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  917. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  918. if (INTEL_GEN(dev_priv) >= 4) {
  919. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  920. i915_reg_t reg = PIPECONF(cpu_transcoder);
  921. /* Wait for the Pipe State to go off */
  922. if (intel_wait_for_register(dev_priv,
  923. reg, I965_PIPECONF_ACTIVE, 0,
  924. 100))
  925. WARN(1, "pipe_off wait timed out\n");
  926. } else {
  927. intel_wait_for_pipe_scanline_stopped(crtc);
  928. }
  929. }
  930. /* Only for pre-ILK configs */
  931. void assert_pll(struct drm_i915_private *dev_priv,
  932. enum pipe pipe, bool state)
  933. {
  934. u32 val;
  935. bool cur_state;
  936. val = I915_READ(DPLL(pipe));
  937. cur_state = !!(val & DPLL_VCO_ENABLE);
  938. I915_STATE_WARN(cur_state != state,
  939. "PLL state assertion failure (expected %s, current %s)\n",
  940. onoff(state), onoff(cur_state));
  941. }
  942. /* XXX: the dsi pll is shared between MIPI DSI ports */
  943. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  944. {
  945. u32 val;
  946. bool cur_state;
  947. mutex_lock(&dev_priv->sb_lock);
  948. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  949. mutex_unlock(&dev_priv->sb_lock);
  950. cur_state = val & DSI_PLL_VCO_EN;
  951. I915_STATE_WARN(cur_state != state,
  952. "DSI PLL state assertion failure (expected %s, current %s)\n",
  953. onoff(state), onoff(cur_state));
  954. }
  955. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  956. enum pipe pipe, bool state)
  957. {
  958. bool cur_state;
  959. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  960. pipe);
  961. if (HAS_DDI(dev_priv)) {
  962. /* DDI does not have a specific FDI_TX register */
  963. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  964. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  965. } else {
  966. u32 val = I915_READ(FDI_TX_CTL(pipe));
  967. cur_state = !!(val & FDI_TX_ENABLE);
  968. }
  969. I915_STATE_WARN(cur_state != state,
  970. "FDI TX state assertion failure (expected %s, current %s)\n",
  971. onoff(state), onoff(cur_state));
  972. }
  973. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  974. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  975. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  976. enum pipe pipe, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. val = I915_READ(FDI_RX_CTL(pipe));
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. I915_STATE_WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. onoff(state), onoff(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. u32 val;
  992. /* ILK FDI PLL is always enabled */
  993. if (IS_GEN5(dev_priv))
  994. return;
  995. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  996. if (HAS_DDI(dev_priv))
  997. return;
  998. val = I915_READ(FDI_TX_CTL(pipe));
  999. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1000. }
  1001. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1002. enum pipe pipe, bool state)
  1003. {
  1004. u32 val;
  1005. bool cur_state;
  1006. val = I915_READ(FDI_RX_CTL(pipe));
  1007. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1008. I915_STATE_WARN(cur_state != state,
  1009. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1010. onoff(state), onoff(cur_state));
  1011. }
  1012. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1013. {
  1014. i915_reg_t pp_reg;
  1015. u32 val;
  1016. enum pipe panel_pipe = PIPE_A;
  1017. bool locked = true;
  1018. if (WARN_ON(HAS_DDI(dev_priv)))
  1019. return;
  1020. if (HAS_PCH_SPLIT(dev_priv)) {
  1021. u32 port_sel;
  1022. pp_reg = PP_CONTROL(0);
  1023. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1024. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1025. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1026. panel_pipe = PIPE_B;
  1027. /* XXX: else fix for eDP */
  1028. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1029. /* presumably write lock depends on pipe, not port select */
  1030. pp_reg = PP_CONTROL(pipe);
  1031. panel_pipe = pipe;
  1032. } else {
  1033. pp_reg = PP_CONTROL(0);
  1034. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. }
  1037. val = I915_READ(pp_reg);
  1038. if (!(val & PANEL_POWER_ON) ||
  1039. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1040. locked = false;
  1041. I915_STATE_WARN(panel_pipe == pipe && locked,
  1042. "panel assertion failure, pipe %c regs locked\n",
  1043. pipe_name(pipe));
  1044. }
  1045. void assert_pipe(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, bool state)
  1047. {
  1048. bool cur_state;
  1049. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1050. pipe);
  1051. enum intel_display_power_domain power_domain;
  1052. /* we keep both pipes enabled on 830 */
  1053. if (IS_I830(dev_priv))
  1054. state = true;
  1055. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1056. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1057. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1058. cur_state = !!(val & PIPECONF_ENABLE);
  1059. intel_display_power_put(dev_priv, power_domain);
  1060. } else {
  1061. cur_state = false;
  1062. }
  1063. I915_STATE_WARN(cur_state != state,
  1064. "pipe %c assertion failure (expected %s, current %s)\n",
  1065. pipe_name(pipe), onoff(state), onoff(cur_state));
  1066. }
  1067. static void assert_plane(struct intel_plane *plane, bool state)
  1068. {
  1069. bool cur_state = plane->get_hw_state(plane);
  1070. I915_STATE_WARN(cur_state != state,
  1071. "%s assertion failure (expected %s, current %s)\n",
  1072. plane->base.name, onoff(state), onoff(cur_state));
  1073. }
  1074. #define assert_plane_enabled(p) assert_plane(p, true)
  1075. #define assert_plane_disabled(p) assert_plane(p, false)
  1076. static void assert_planes_disabled(struct intel_crtc *crtc)
  1077. {
  1078. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1079. struct intel_plane *plane;
  1080. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1081. assert_plane_disabled(plane);
  1082. }
  1083. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1084. {
  1085. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1086. drm_crtc_vblank_put(crtc);
  1087. }
  1088. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. u32 val;
  1092. bool enabled;
  1093. val = I915_READ(PCH_TRANSCONF(pipe));
  1094. enabled = !!(val & TRANS_ENABLE);
  1095. I915_STATE_WARN(enabled,
  1096. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1097. pipe_name(pipe));
  1098. }
  1099. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1100. enum pipe pipe, u32 port_sel, u32 val)
  1101. {
  1102. if ((val & DP_PORT_EN) == 0)
  1103. return false;
  1104. if (HAS_PCH_CPT(dev_priv)) {
  1105. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1106. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1107. return false;
  1108. } else if (IS_CHERRYVIEW(dev_priv)) {
  1109. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1110. return false;
  1111. } else {
  1112. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1113. return false;
  1114. }
  1115. return true;
  1116. }
  1117. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe, u32 val)
  1119. {
  1120. if ((val & SDVO_ENABLE) == 0)
  1121. return false;
  1122. if (HAS_PCH_CPT(dev_priv)) {
  1123. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1124. return false;
  1125. } else if (IS_CHERRYVIEW(dev_priv)) {
  1126. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1127. return false;
  1128. } else {
  1129. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1130. return false;
  1131. }
  1132. return true;
  1133. }
  1134. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1135. enum pipe pipe, u32 val)
  1136. {
  1137. if ((val & LVDS_PORT_EN) == 0)
  1138. return false;
  1139. if (HAS_PCH_CPT(dev_priv)) {
  1140. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1141. return false;
  1142. } else {
  1143. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1144. return false;
  1145. }
  1146. return true;
  1147. }
  1148. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1149. enum pipe pipe, u32 val)
  1150. {
  1151. if ((val & ADPA_DAC_ENABLE) == 0)
  1152. return false;
  1153. if (HAS_PCH_CPT(dev_priv)) {
  1154. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1155. return false;
  1156. } else {
  1157. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1158. return false;
  1159. }
  1160. return true;
  1161. }
  1162. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe, i915_reg_t reg,
  1164. u32 port_sel)
  1165. {
  1166. u32 val = I915_READ(reg);
  1167. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1168. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1169. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1170. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1171. && (val & DP_PIPEB_SELECT),
  1172. "IBX PCH dp port still using transcoder B\n");
  1173. }
  1174. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1175. enum pipe pipe, i915_reg_t reg)
  1176. {
  1177. u32 val = I915_READ(reg);
  1178. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1179. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1180. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1181. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1182. && (val & SDVO_PIPE_B_SELECT),
  1183. "IBX PCH hdmi port still using transcoder B\n");
  1184. }
  1185. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1190. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1191. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1192. val = I915_READ(PCH_ADPA);
  1193. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1194. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1195. pipe_name(pipe));
  1196. val = I915_READ(PCH_LVDS);
  1197. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1198. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1199. pipe_name(pipe));
  1200. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1201. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1202. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1203. }
  1204. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1205. const struct intel_crtc_state *pipe_config)
  1206. {
  1207. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1208. enum pipe pipe = crtc->pipe;
  1209. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1210. POSTING_READ(DPLL(pipe));
  1211. udelay(150);
  1212. if (intel_wait_for_register(dev_priv,
  1213. DPLL(pipe),
  1214. DPLL_LOCK_VLV,
  1215. DPLL_LOCK_VLV,
  1216. 1))
  1217. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1218. }
  1219. static void vlv_enable_pll(struct intel_crtc *crtc,
  1220. const struct intel_crtc_state *pipe_config)
  1221. {
  1222. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1223. enum pipe pipe = crtc->pipe;
  1224. assert_pipe_disabled(dev_priv, pipe);
  1225. /* PLL is protected by panel, make sure we can write it */
  1226. assert_panel_unlocked(dev_priv, pipe);
  1227. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1228. _vlv_enable_pll(crtc, pipe_config);
  1229. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1230. POSTING_READ(DPLL_MD(pipe));
  1231. }
  1232. static void _chv_enable_pll(struct intel_crtc *crtc,
  1233. const struct intel_crtc_state *pipe_config)
  1234. {
  1235. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1236. enum pipe pipe = crtc->pipe;
  1237. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1238. u32 tmp;
  1239. mutex_lock(&dev_priv->sb_lock);
  1240. /* Enable back the 10bit clock to display controller */
  1241. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1242. tmp |= DPIO_DCLKP_EN;
  1243. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1244. mutex_unlock(&dev_priv->sb_lock);
  1245. /*
  1246. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1247. */
  1248. udelay(1);
  1249. /* Enable PLL */
  1250. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1251. /* Check PLL is locked */
  1252. if (intel_wait_for_register(dev_priv,
  1253. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1254. 1))
  1255. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1256. }
  1257. static void chv_enable_pll(struct intel_crtc *crtc,
  1258. const struct intel_crtc_state *pipe_config)
  1259. {
  1260. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1261. enum pipe pipe = crtc->pipe;
  1262. assert_pipe_disabled(dev_priv, pipe);
  1263. /* PLL is protected by panel, make sure we can write it */
  1264. assert_panel_unlocked(dev_priv, pipe);
  1265. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1266. _chv_enable_pll(crtc, pipe_config);
  1267. if (pipe != PIPE_A) {
  1268. /*
  1269. * WaPixelRepeatModeFixForC0:chv
  1270. *
  1271. * DPLLCMD is AWOL. Use chicken bits to propagate
  1272. * the value from DPLLBMD to either pipe B or C.
  1273. */
  1274. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1275. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1276. I915_WRITE(CBR4_VLV, 0);
  1277. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1278. /*
  1279. * DPLLB VGA mode also seems to cause problems.
  1280. * We should always have it disabled.
  1281. */
  1282. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1283. } else {
  1284. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1285. POSTING_READ(DPLL_MD(pipe));
  1286. }
  1287. }
  1288. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1289. {
  1290. struct intel_crtc *crtc;
  1291. int count = 0;
  1292. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1293. count += crtc->base.state->active &&
  1294. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1295. }
  1296. return count;
  1297. }
  1298. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1299. const struct intel_crtc_state *crtc_state)
  1300. {
  1301. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1302. i915_reg_t reg = DPLL(crtc->pipe);
  1303. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1304. int i;
  1305. assert_pipe_disabled(dev_priv, crtc->pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1308. assert_panel_unlocked(dev_priv, crtc->pipe);
  1309. /* Enable DVO 2x clock on both PLLs if necessary */
  1310. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1311. /*
  1312. * It appears to be important that we don't enable this
  1313. * for the current pipe before otherwise configuring the
  1314. * PLL. No idea how this should be handled if multiple
  1315. * DVO outputs are enabled simultaneosly.
  1316. */
  1317. dpll |= DPLL_DVO_2X_MODE;
  1318. I915_WRITE(DPLL(!crtc->pipe),
  1319. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1320. }
  1321. /*
  1322. * Apparently we need to have VGA mode enabled prior to changing
  1323. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1324. * dividers, even though the register value does change.
  1325. */
  1326. I915_WRITE(reg, 0);
  1327. I915_WRITE(reg, dpll);
  1328. /* Wait for the clocks to stabilize. */
  1329. POSTING_READ(reg);
  1330. udelay(150);
  1331. if (INTEL_GEN(dev_priv) >= 4) {
  1332. I915_WRITE(DPLL_MD(crtc->pipe),
  1333. crtc_state->dpll_hw_state.dpll_md);
  1334. } else {
  1335. /* The pixel multiplier can only be updated once the
  1336. * DPLL is enabled and the clocks are stable.
  1337. *
  1338. * So write it again.
  1339. */
  1340. I915_WRITE(reg, dpll);
  1341. }
  1342. /* We do this three times for luck */
  1343. for (i = 0; i < 3; i++) {
  1344. I915_WRITE(reg, dpll);
  1345. POSTING_READ(reg);
  1346. udelay(150); /* wait for warmup */
  1347. }
  1348. }
  1349. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1350. {
  1351. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1352. enum pipe pipe = crtc->pipe;
  1353. /* Disable DVO 2x clock on both PLLs if necessary */
  1354. if (IS_I830(dev_priv) &&
  1355. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1356. !intel_num_dvo_pipes(dev_priv)) {
  1357. I915_WRITE(DPLL(PIPE_B),
  1358. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1359. I915_WRITE(DPLL(PIPE_A),
  1360. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1361. }
  1362. /* Don't disable pipe or pipe PLLs if needed */
  1363. if (IS_I830(dev_priv))
  1364. return;
  1365. /* Make sure the pipe isn't still relying on us */
  1366. assert_pipe_disabled(dev_priv, pipe);
  1367. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1368. POSTING_READ(DPLL(pipe));
  1369. }
  1370. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1371. {
  1372. u32 val;
  1373. /* Make sure the pipe isn't still relying on us */
  1374. assert_pipe_disabled(dev_priv, pipe);
  1375. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1376. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1377. if (pipe != PIPE_A)
  1378. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1379. I915_WRITE(DPLL(pipe), val);
  1380. POSTING_READ(DPLL(pipe));
  1381. }
  1382. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1383. {
  1384. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1385. u32 val;
  1386. /* Make sure the pipe isn't still relying on us */
  1387. assert_pipe_disabled(dev_priv, pipe);
  1388. val = DPLL_SSC_REF_CLK_CHV |
  1389. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1390. if (pipe != PIPE_A)
  1391. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1392. I915_WRITE(DPLL(pipe), val);
  1393. POSTING_READ(DPLL(pipe));
  1394. mutex_lock(&dev_priv->sb_lock);
  1395. /* Disable 10bit clock to display controller */
  1396. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1397. val &= ~DPIO_DCLKP_EN;
  1398. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1399. mutex_unlock(&dev_priv->sb_lock);
  1400. }
  1401. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1402. struct intel_digital_port *dport,
  1403. unsigned int expected_mask)
  1404. {
  1405. u32 port_mask;
  1406. i915_reg_t dpll_reg;
  1407. switch (dport->base.port) {
  1408. case PORT_B:
  1409. port_mask = DPLL_PORTB_READY_MASK;
  1410. dpll_reg = DPLL(0);
  1411. break;
  1412. case PORT_C:
  1413. port_mask = DPLL_PORTC_READY_MASK;
  1414. dpll_reg = DPLL(0);
  1415. expected_mask <<= 4;
  1416. break;
  1417. case PORT_D:
  1418. port_mask = DPLL_PORTD_READY_MASK;
  1419. dpll_reg = DPIO_PHY_STATUS;
  1420. break;
  1421. default:
  1422. BUG();
  1423. }
  1424. if (intel_wait_for_register(dev_priv,
  1425. dpll_reg, port_mask, expected_mask,
  1426. 1000))
  1427. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1428. port_name(dport->base.port),
  1429. I915_READ(dpll_reg) & port_mask, expected_mask);
  1430. }
  1431. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1432. enum pipe pipe)
  1433. {
  1434. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1435. pipe);
  1436. i915_reg_t reg;
  1437. uint32_t val, pipeconf_val;
  1438. /* Make sure PCH DPLL is enabled */
  1439. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1440. /* FDI must be feeding us bits for PCH ports */
  1441. assert_fdi_tx_enabled(dev_priv, pipe);
  1442. assert_fdi_rx_enabled(dev_priv, pipe);
  1443. if (HAS_PCH_CPT(dev_priv)) {
  1444. /* Workaround: Set the timing override bit before enabling the
  1445. * pch transcoder. */
  1446. reg = TRANS_CHICKEN2(pipe);
  1447. val = I915_READ(reg);
  1448. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1449. I915_WRITE(reg, val);
  1450. }
  1451. reg = PCH_TRANSCONF(pipe);
  1452. val = I915_READ(reg);
  1453. pipeconf_val = I915_READ(PIPECONF(pipe));
  1454. if (HAS_PCH_IBX(dev_priv)) {
  1455. /*
  1456. * Make the BPC in transcoder be consistent with
  1457. * that in pipeconf reg. For HDMI we must use 8bpc
  1458. * here for both 8bpc and 12bpc.
  1459. */
  1460. val &= ~PIPECONF_BPC_MASK;
  1461. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1462. val |= PIPECONF_8BPC;
  1463. else
  1464. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1465. }
  1466. val &= ~TRANS_INTERLACE_MASK;
  1467. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1468. if (HAS_PCH_IBX(dev_priv) &&
  1469. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1470. val |= TRANS_LEGACY_INTERLACED_ILK;
  1471. else
  1472. val |= TRANS_INTERLACED;
  1473. else
  1474. val |= TRANS_PROGRESSIVE;
  1475. I915_WRITE(reg, val | TRANS_ENABLE);
  1476. if (intel_wait_for_register(dev_priv,
  1477. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1478. 100))
  1479. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1480. }
  1481. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1482. enum transcoder cpu_transcoder)
  1483. {
  1484. u32 val, pipeconf_val;
  1485. /* FDI must be feeding us bits for PCH ports */
  1486. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1487. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1488. /* Workaround: set timing override bit. */
  1489. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1490. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1491. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1492. val = TRANS_ENABLE;
  1493. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1494. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1495. PIPECONF_INTERLACED_ILK)
  1496. val |= TRANS_INTERLACED;
  1497. else
  1498. val |= TRANS_PROGRESSIVE;
  1499. I915_WRITE(LPT_TRANSCONF, val);
  1500. if (intel_wait_for_register(dev_priv,
  1501. LPT_TRANSCONF,
  1502. TRANS_STATE_ENABLE,
  1503. TRANS_STATE_ENABLE,
  1504. 100))
  1505. DRM_ERROR("Failed to enable PCH transcoder\n");
  1506. }
  1507. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1508. enum pipe pipe)
  1509. {
  1510. i915_reg_t reg;
  1511. uint32_t val;
  1512. /* FDI relies on the transcoder */
  1513. assert_fdi_tx_disabled(dev_priv, pipe);
  1514. assert_fdi_rx_disabled(dev_priv, pipe);
  1515. /* Ports must be off as well */
  1516. assert_pch_ports_disabled(dev_priv, pipe);
  1517. reg = PCH_TRANSCONF(pipe);
  1518. val = I915_READ(reg);
  1519. val &= ~TRANS_ENABLE;
  1520. I915_WRITE(reg, val);
  1521. /* wait for PCH transcoder off, transcoder state */
  1522. if (intel_wait_for_register(dev_priv,
  1523. reg, TRANS_STATE_ENABLE, 0,
  1524. 50))
  1525. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1526. if (HAS_PCH_CPT(dev_priv)) {
  1527. /* Workaround: Clear the timing override chicken bit again. */
  1528. reg = TRANS_CHICKEN2(pipe);
  1529. val = I915_READ(reg);
  1530. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1531. I915_WRITE(reg, val);
  1532. }
  1533. }
  1534. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1535. {
  1536. u32 val;
  1537. val = I915_READ(LPT_TRANSCONF);
  1538. val &= ~TRANS_ENABLE;
  1539. I915_WRITE(LPT_TRANSCONF, val);
  1540. /* wait for PCH transcoder off, transcoder state */
  1541. if (intel_wait_for_register(dev_priv,
  1542. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1543. 50))
  1544. DRM_ERROR("Failed to disable PCH transcoder\n");
  1545. /* Workaround: clear timing override bit. */
  1546. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1547. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1548. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1549. }
  1550. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1551. {
  1552. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1553. if (HAS_PCH_LPT(dev_priv))
  1554. return PIPE_A;
  1555. else
  1556. return crtc->pipe;
  1557. }
  1558. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1559. {
  1560. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1562. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1563. enum pipe pipe = crtc->pipe;
  1564. i915_reg_t reg;
  1565. u32 val;
  1566. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1567. assert_planes_disabled(crtc);
  1568. /*
  1569. * A pipe without a PLL won't actually be able to drive bits from
  1570. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1571. * need the check.
  1572. */
  1573. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1574. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1575. assert_dsi_pll_enabled(dev_priv);
  1576. else
  1577. assert_pll_enabled(dev_priv, pipe);
  1578. } else {
  1579. if (new_crtc_state->has_pch_encoder) {
  1580. /* if driving the PCH, we need FDI enabled */
  1581. assert_fdi_rx_pll_enabled(dev_priv,
  1582. intel_crtc_pch_transcoder(crtc));
  1583. assert_fdi_tx_pll_enabled(dev_priv,
  1584. (enum pipe) cpu_transcoder);
  1585. }
  1586. /* FIXME: assert CPU port conditions for SNB+ */
  1587. }
  1588. reg = PIPECONF(cpu_transcoder);
  1589. val = I915_READ(reg);
  1590. if (val & PIPECONF_ENABLE) {
  1591. /* we keep both pipes enabled on 830 */
  1592. WARN_ON(!IS_I830(dev_priv));
  1593. return;
  1594. }
  1595. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1596. POSTING_READ(reg);
  1597. /*
  1598. * Until the pipe starts PIPEDSL reads will return a stale value,
  1599. * which causes an apparent vblank timestamp jump when PIPEDSL
  1600. * resets to its proper value. That also messes up the frame count
  1601. * when it's derived from the timestamps. So let's wait for the
  1602. * pipe to start properly before we call drm_crtc_vblank_on()
  1603. */
  1604. if (dev_priv->drm.max_vblank_count == 0)
  1605. intel_wait_for_pipe_scanline_moving(crtc);
  1606. }
  1607. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1608. {
  1609. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1610. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1611. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1612. enum pipe pipe = crtc->pipe;
  1613. i915_reg_t reg;
  1614. u32 val;
  1615. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1616. /*
  1617. * Make sure planes won't keep trying to pump pixels to us,
  1618. * or we might hang the display.
  1619. */
  1620. assert_planes_disabled(crtc);
  1621. reg = PIPECONF(cpu_transcoder);
  1622. val = I915_READ(reg);
  1623. if ((val & PIPECONF_ENABLE) == 0)
  1624. return;
  1625. /*
  1626. * Double wide has implications for planes
  1627. * so best keep it disabled when not needed.
  1628. */
  1629. if (old_crtc_state->double_wide)
  1630. val &= ~PIPECONF_DOUBLE_WIDE;
  1631. /* Don't disable pipe or pipe PLLs if needed */
  1632. if (!IS_I830(dev_priv))
  1633. val &= ~PIPECONF_ENABLE;
  1634. I915_WRITE(reg, val);
  1635. if ((val & PIPECONF_ENABLE) == 0)
  1636. intel_wait_for_pipe_off(old_crtc_state);
  1637. }
  1638. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1639. {
  1640. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1641. }
  1642. static unsigned int
  1643. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1644. {
  1645. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1646. unsigned int cpp = fb->format->cpp[plane];
  1647. switch (fb->modifier) {
  1648. case DRM_FORMAT_MOD_LINEAR:
  1649. return cpp;
  1650. case I915_FORMAT_MOD_X_TILED:
  1651. if (IS_GEN2(dev_priv))
  1652. return 128;
  1653. else
  1654. return 512;
  1655. case I915_FORMAT_MOD_Y_TILED_CCS:
  1656. if (plane == 1)
  1657. return 128;
  1658. /* fall through */
  1659. case I915_FORMAT_MOD_Y_TILED:
  1660. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1661. return 128;
  1662. else
  1663. return 512;
  1664. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1665. if (plane == 1)
  1666. return 128;
  1667. /* fall through */
  1668. case I915_FORMAT_MOD_Yf_TILED:
  1669. switch (cpp) {
  1670. case 1:
  1671. return 64;
  1672. case 2:
  1673. case 4:
  1674. return 128;
  1675. case 8:
  1676. case 16:
  1677. return 256;
  1678. default:
  1679. MISSING_CASE(cpp);
  1680. return cpp;
  1681. }
  1682. break;
  1683. default:
  1684. MISSING_CASE(fb->modifier);
  1685. return cpp;
  1686. }
  1687. }
  1688. static unsigned int
  1689. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1690. {
  1691. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1692. return 1;
  1693. else
  1694. return intel_tile_size(to_i915(fb->dev)) /
  1695. intel_tile_width_bytes(fb, plane);
  1696. }
  1697. /* Return the tile dimensions in pixel units */
  1698. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1699. unsigned int *tile_width,
  1700. unsigned int *tile_height)
  1701. {
  1702. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1703. unsigned int cpp = fb->format->cpp[plane];
  1704. *tile_width = tile_width_bytes / cpp;
  1705. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1706. }
  1707. unsigned int
  1708. intel_fb_align_height(const struct drm_framebuffer *fb,
  1709. int plane, unsigned int height)
  1710. {
  1711. unsigned int tile_height = intel_tile_height(fb, plane);
  1712. return ALIGN(height, tile_height);
  1713. }
  1714. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1715. {
  1716. unsigned int size = 0;
  1717. int i;
  1718. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1719. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1720. return size;
  1721. }
  1722. static void
  1723. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1724. const struct drm_framebuffer *fb,
  1725. unsigned int rotation)
  1726. {
  1727. view->type = I915_GGTT_VIEW_NORMAL;
  1728. if (drm_rotation_90_or_270(rotation)) {
  1729. view->type = I915_GGTT_VIEW_ROTATED;
  1730. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1731. }
  1732. }
  1733. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1734. {
  1735. if (IS_I830(dev_priv))
  1736. return 16 * 1024;
  1737. else if (IS_I85X(dev_priv))
  1738. return 256;
  1739. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1740. return 32;
  1741. else
  1742. return 4 * 1024;
  1743. }
  1744. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1745. {
  1746. if (INTEL_INFO(dev_priv)->gen >= 9)
  1747. return 256 * 1024;
  1748. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1749. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1750. return 128 * 1024;
  1751. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1752. return 4 * 1024;
  1753. else
  1754. return 0;
  1755. }
  1756. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1757. int plane)
  1758. {
  1759. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1760. /* AUX_DIST needs only 4K alignment */
  1761. if (plane == 1)
  1762. return 4096;
  1763. switch (fb->modifier) {
  1764. case DRM_FORMAT_MOD_LINEAR:
  1765. return intel_linear_alignment(dev_priv);
  1766. case I915_FORMAT_MOD_X_TILED:
  1767. if (INTEL_GEN(dev_priv) >= 9)
  1768. return 256 * 1024;
  1769. return 0;
  1770. case I915_FORMAT_MOD_Y_TILED_CCS:
  1771. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1772. case I915_FORMAT_MOD_Y_TILED:
  1773. case I915_FORMAT_MOD_Yf_TILED:
  1774. return 1 * 1024 * 1024;
  1775. default:
  1776. MISSING_CASE(fb->modifier);
  1777. return 0;
  1778. }
  1779. }
  1780. struct i915_vma *
  1781. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1782. {
  1783. struct drm_device *dev = fb->dev;
  1784. struct drm_i915_private *dev_priv = to_i915(dev);
  1785. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1786. struct i915_ggtt_view view;
  1787. struct i915_vma *vma;
  1788. u32 alignment;
  1789. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1790. alignment = intel_surf_alignment(fb, 0);
  1791. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1792. /* Note that the w/a also requires 64 PTE of padding following the
  1793. * bo. We currently fill all unused PTE with the shadow page and so
  1794. * we should always have valid PTE following the scanout preventing
  1795. * the VT-d warning.
  1796. */
  1797. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1798. alignment = 256 * 1024;
  1799. /*
  1800. * Global gtt pte registers are special registers which actually forward
  1801. * writes to a chunk of system memory. Which means that there is no risk
  1802. * that the register values disappear as soon as we call
  1803. * intel_runtime_pm_put(), so it is correct to wrap only the
  1804. * pin/unpin/fence and not more.
  1805. */
  1806. intel_runtime_pm_get(dev_priv);
  1807. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1808. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1809. if (IS_ERR(vma))
  1810. goto err;
  1811. if (i915_vma_is_map_and_fenceable(vma)) {
  1812. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1813. * fence, whereas 965+ only requires a fence if using
  1814. * framebuffer compression. For simplicity, we always, when
  1815. * possible, install a fence as the cost is not that onerous.
  1816. *
  1817. * If we fail to fence the tiled scanout, then either the
  1818. * modeset will reject the change (which is highly unlikely as
  1819. * the affected systems, all but one, do not have unmappable
  1820. * space) or we will not be able to enable full powersaving
  1821. * techniques (also likely not to apply due to various limits
  1822. * FBC and the like impose on the size of the buffer, which
  1823. * presumably we violated anyway with this unmappable buffer).
  1824. * Anyway, it is presumably better to stumble onwards with
  1825. * something and try to run the system in a "less than optimal"
  1826. * mode that matches the user configuration.
  1827. */
  1828. i915_vma_pin_fence(vma);
  1829. }
  1830. i915_vma_get(vma);
  1831. err:
  1832. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1833. intel_runtime_pm_put(dev_priv);
  1834. return vma;
  1835. }
  1836. void intel_unpin_fb_vma(struct i915_vma *vma)
  1837. {
  1838. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1839. i915_vma_unpin_fence(vma);
  1840. i915_gem_object_unpin_from_display_plane(vma);
  1841. i915_vma_put(vma);
  1842. }
  1843. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1844. unsigned int rotation)
  1845. {
  1846. if (drm_rotation_90_or_270(rotation))
  1847. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1848. else
  1849. return fb->pitches[plane];
  1850. }
  1851. /*
  1852. * Convert the x/y offsets into a linear offset.
  1853. * Only valid with 0/180 degree rotation, which is fine since linear
  1854. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1855. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1856. */
  1857. u32 intel_fb_xy_to_linear(int x, int y,
  1858. const struct intel_plane_state *state,
  1859. int plane)
  1860. {
  1861. const struct drm_framebuffer *fb = state->base.fb;
  1862. unsigned int cpp = fb->format->cpp[plane];
  1863. unsigned int pitch = fb->pitches[plane];
  1864. return y * pitch + x * cpp;
  1865. }
  1866. /*
  1867. * Add the x/y offsets derived from fb->offsets[] to the user
  1868. * specified plane src x/y offsets. The resulting x/y offsets
  1869. * specify the start of scanout from the beginning of the gtt mapping.
  1870. */
  1871. void intel_add_fb_offsets(int *x, int *y,
  1872. const struct intel_plane_state *state,
  1873. int plane)
  1874. {
  1875. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1876. unsigned int rotation = state->base.rotation;
  1877. if (drm_rotation_90_or_270(rotation)) {
  1878. *x += intel_fb->rotated[plane].x;
  1879. *y += intel_fb->rotated[plane].y;
  1880. } else {
  1881. *x += intel_fb->normal[plane].x;
  1882. *y += intel_fb->normal[plane].y;
  1883. }
  1884. }
  1885. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1886. unsigned int tile_width,
  1887. unsigned int tile_height,
  1888. unsigned int tile_size,
  1889. unsigned int pitch_tiles,
  1890. u32 old_offset,
  1891. u32 new_offset)
  1892. {
  1893. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1894. unsigned int tiles;
  1895. WARN_ON(old_offset & (tile_size - 1));
  1896. WARN_ON(new_offset & (tile_size - 1));
  1897. WARN_ON(new_offset > old_offset);
  1898. tiles = (old_offset - new_offset) / tile_size;
  1899. *y += tiles / pitch_tiles * tile_height;
  1900. *x += tiles % pitch_tiles * tile_width;
  1901. /* minimize x in case it got needlessly big */
  1902. *y += *x / pitch_pixels * tile_height;
  1903. *x %= pitch_pixels;
  1904. return new_offset;
  1905. }
  1906. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1907. const struct drm_framebuffer *fb, int plane,
  1908. unsigned int rotation,
  1909. u32 old_offset, u32 new_offset)
  1910. {
  1911. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1912. unsigned int cpp = fb->format->cpp[plane];
  1913. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1914. WARN_ON(new_offset > old_offset);
  1915. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1916. unsigned int tile_size, tile_width, tile_height;
  1917. unsigned int pitch_tiles;
  1918. tile_size = intel_tile_size(dev_priv);
  1919. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1920. if (drm_rotation_90_or_270(rotation)) {
  1921. pitch_tiles = pitch / tile_height;
  1922. swap(tile_width, tile_height);
  1923. } else {
  1924. pitch_tiles = pitch / (tile_width * cpp);
  1925. }
  1926. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1927. tile_size, pitch_tiles,
  1928. old_offset, new_offset);
  1929. } else {
  1930. old_offset += *y * pitch + *x * cpp;
  1931. *y = (old_offset - new_offset) / pitch;
  1932. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1933. }
  1934. return new_offset;
  1935. }
  1936. /*
  1937. * Adjust the tile offset by moving the difference into
  1938. * the x/y offsets.
  1939. */
  1940. static u32 intel_adjust_tile_offset(int *x, int *y,
  1941. const struct intel_plane_state *state, int plane,
  1942. u32 old_offset, u32 new_offset)
  1943. {
  1944. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  1945. state->base.rotation,
  1946. old_offset, new_offset);
  1947. }
  1948. /*
  1949. * Computes the linear offset to the base tile and adjusts
  1950. * x, y. bytes per pixel is assumed to be a power-of-two.
  1951. *
  1952. * In the 90/270 rotated case, x and y are assumed
  1953. * to be already rotated to match the rotated GTT view, and
  1954. * pitch is the tile_height aligned framebuffer height.
  1955. *
  1956. * This function is used when computing the derived information
  1957. * under intel_framebuffer, so using any of that information
  1958. * here is not allowed. Anything under drm_framebuffer can be
  1959. * used. This is why the user has to pass in the pitch since it
  1960. * is specified in the rotated orientation.
  1961. */
  1962. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  1963. int *x, int *y,
  1964. const struct drm_framebuffer *fb, int plane,
  1965. unsigned int pitch,
  1966. unsigned int rotation,
  1967. u32 alignment)
  1968. {
  1969. uint64_t fb_modifier = fb->modifier;
  1970. unsigned int cpp = fb->format->cpp[plane];
  1971. u32 offset, offset_aligned;
  1972. if (alignment)
  1973. alignment--;
  1974. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  1975. unsigned int tile_size, tile_width, tile_height;
  1976. unsigned int tile_rows, tiles, pitch_tiles;
  1977. tile_size = intel_tile_size(dev_priv);
  1978. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1979. if (drm_rotation_90_or_270(rotation)) {
  1980. pitch_tiles = pitch / tile_height;
  1981. swap(tile_width, tile_height);
  1982. } else {
  1983. pitch_tiles = pitch / (tile_width * cpp);
  1984. }
  1985. tile_rows = *y / tile_height;
  1986. *y %= tile_height;
  1987. tiles = *x / tile_width;
  1988. *x %= tile_width;
  1989. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  1990. offset_aligned = offset & ~alignment;
  1991. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1992. tile_size, pitch_tiles,
  1993. offset, offset_aligned);
  1994. } else {
  1995. offset = *y * pitch + *x * cpp;
  1996. offset_aligned = offset & ~alignment;
  1997. *y = (offset & alignment) / pitch;
  1998. *x = ((offset & alignment) - *y * pitch) / cpp;
  1999. }
  2000. return offset_aligned;
  2001. }
  2002. u32 intel_compute_tile_offset(int *x, int *y,
  2003. const struct intel_plane_state *state,
  2004. int plane)
  2005. {
  2006. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2007. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2008. const struct drm_framebuffer *fb = state->base.fb;
  2009. unsigned int rotation = state->base.rotation;
  2010. int pitch = intel_fb_pitch(fb, plane, rotation);
  2011. u32 alignment;
  2012. if (intel_plane->id == PLANE_CURSOR)
  2013. alignment = intel_cursor_alignment(dev_priv);
  2014. else
  2015. alignment = intel_surf_alignment(fb, plane);
  2016. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2017. rotation, alignment);
  2018. }
  2019. /* Convert the fb->offset[] into x/y offsets */
  2020. static int intel_fb_offset_to_xy(int *x, int *y,
  2021. const struct drm_framebuffer *fb, int plane)
  2022. {
  2023. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2024. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2025. fb->offsets[plane] % intel_tile_size(dev_priv))
  2026. return -EINVAL;
  2027. *x = 0;
  2028. *y = 0;
  2029. _intel_adjust_tile_offset(x, y,
  2030. fb, plane, DRM_MODE_ROTATE_0,
  2031. fb->offsets[plane], 0);
  2032. return 0;
  2033. }
  2034. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2035. {
  2036. switch (fb_modifier) {
  2037. case I915_FORMAT_MOD_X_TILED:
  2038. return I915_TILING_X;
  2039. case I915_FORMAT_MOD_Y_TILED:
  2040. case I915_FORMAT_MOD_Y_TILED_CCS:
  2041. return I915_TILING_Y;
  2042. default:
  2043. return I915_TILING_NONE;
  2044. }
  2045. }
  2046. static const struct drm_format_info ccs_formats[] = {
  2047. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2048. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2049. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2050. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2051. };
  2052. static const struct drm_format_info *
  2053. lookup_format_info(const struct drm_format_info formats[],
  2054. int num_formats, u32 format)
  2055. {
  2056. int i;
  2057. for (i = 0; i < num_formats; i++) {
  2058. if (formats[i].format == format)
  2059. return &formats[i];
  2060. }
  2061. return NULL;
  2062. }
  2063. static const struct drm_format_info *
  2064. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2065. {
  2066. switch (cmd->modifier[0]) {
  2067. case I915_FORMAT_MOD_Y_TILED_CCS:
  2068. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2069. return lookup_format_info(ccs_formats,
  2070. ARRAY_SIZE(ccs_formats),
  2071. cmd->pixel_format);
  2072. default:
  2073. return NULL;
  2074. }
  2075. }
  2076. static int
  2077. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2078. struct drm_framebuffer *fb)
  2079. {
  2080. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2081. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2082. u32 gtt_offset_rotated = 0;
  2083. unsigned int max_size = 0;
  2084. int i, num_planes = fb->format->num_planes;
  2085. unsigned int tile_size = intel_tile_size(dev_priv);
  2086. for (i = 0; i < num_planes; i++) {
  2087. unsigned int width, height;
  2088. unsigned int cpp, size;
  2089. u32 offset;
  2090. int x, y;
  2091. int ret;
  2092. cpp = fb->format->cpp[i];
  2093. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2094. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2095. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2096. if (ret) {
  2097. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2098. i, fb->offsets[i]);
  2099. return ret;
  2100. }
  2101. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2102. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2103. int hsub = fb->format->hsub;
  2104. int vsub = fb->format->vsub;
  2105. int tile_width, tile_height;
  2106. int main_x, main_y;
  2107. int ccs_x, ccs_y;
  2108. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2109. tile_width *= hsub;
  2110. tile_height *= vsub;
  2111. ccs_x = (x * hsub) % tile_width;
  2112. ccs_y = (y * vsub) % tile_height;
  2113. main_x = intel_fb->normal[0].x % tile_width;
  2114. main_y = intel_fb->normal[0].y % tile_height;
  2115. /*
  2116. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2117. * x/y offsets must match between CCS and the main surface.
  2118. */
  2119. if (main_x != ccs_x || main_y != ccs_y) {
  2120. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2121. main_x, main_y,
  2122. ccs_x, ccs_y,
  2123. intel_fb->normal[0].x,
  2124. intel_fb->normal[0].y,
  2125. x, y);
  2126. return -EINVAL;
  2127. }
  2128. }
  2129. /*
  2130. * The fence (if used) is aligned to the start of the object
  2131. * so having the framebuffer wrap around across the edge of the
  2132. * fenced region doesn't really work. We have no API to configure
  2133. * the fence start offset within the object (nor could we probably
  2134. * on gen2/3). So it's just easier if we just require that the
  2135. * fb layout agrees with the fence layout. We already check that the
  2136. * fb stride matches the fence stride elsewhere.
  2137. */
  2138. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2139. (x + width) * cpp > fb->pitches[i]) {
  2140. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2141. i, fb->offsets[i]);
  2142. return -EINVAL;
  2143. }
  2144. /*
  2145. * First pixel of the framebuffer from
  2146. * the start of the normal gtt mapping.
  2147. */
  2148. intel_fb->normal[i].x = x;
  2149. intel_fb->normal[i].y = y;
  2150. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2151. fb, i, fb->pitches[i],
  2152. DRM_MODE_ROTATE_0, tile_size);
  2153. offset /= tile_size;
  2154. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2155. unsigned int tile_width, tile_height;
  2156. unsigned int pitch_tiles;
  2157. struct drm_rect r;
  2158. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2159. rot_info->plane[i].offset = offset;
  2160. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2161. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2162. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2163. intel_fb->rotated[i].pitch =
  2164. rot_info->plane[i].height * tile_height;
  2165. /* how many tiles does this plane need */
  2166. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2167. /*
  2168. * If the plane isn't horizontally tile aligned,
  2169. * we need one more tile.
  2170. */
  2171. if (x != 0)
  2172. size++;
  2173. /* rotate the x/y offsets to match the GTT view */
  2174. r.x1 = x;
  2175. r.y1 = y;
  2176. r.x2 = x + width;
  2177. r.y2 = y + height;
  2178. drm_rect_rotate(&r,
  2179. rot_info->plane[i].width * tile_width,
  2180. rot_info->plane[i].height * tile_height,
  2181. DRM_MODE_ROTATE_270);
  2182. x = r.x1;
  2183. y = r.y1;
  2184. /* rotate the tile dimensions to match the GTT view */
  2185. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2186. swap(tile_width, tile_height);
  2187. /*
  2188. * We only keep the x/y offsets, so push all of the
  2189. * gtt offset into the x/y offsets.
  2190. */
  2191. __intel_adjust_tile_offset(&x, &y,
  2192. tile_width, tile_height,
  2193. tile_size, pitch_tiles,
  2194. gtt_offset_rotated * tile_size, 0);
  2195. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2196. /*
  2197. * First pixel of the framebuffer from
  2198. * the start of the rotated gtt mapping.
  2199. */
  2200. intel_fb->rotated[i].x = x;
  2201. intel_fb->rotated[i].y = y;
  2202. } else {
  2203. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2204. x * cpp, tile_size);
  2205. }
  2206. /* how many tiles in total needed in the bo */
  2207. max_size = max(max_size, offset + size);
  2208. }
  2209. if (max_size * tile_size > intel_fb->obj->base.size) {
  2210. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2211. max_size * tile_size, intel_fb->obj->base.size);
  2212. return -EINVAL;
  2213. }
  2214. return 0;
  2215. }
  2216. static int i9xx_format_to_fourcc(int format)
  2217. {
  2218. switch (format) {
  2219. case DISPPLANE_8BPP:
  2220. return DRM_FORMAT_C8;
  2221. case DISPPLANE_BGRX555:
  2222. return DRM_FORMAT_XRGB1555;
  2223. case DISPPLANE_BGRX565:
  2224. return DRM_FORMAT_RGB565;
  2225. default:
  2226. case DISPPLANE_BGRX888:
  2227. return DRM_FORMAT_XRGB8888;
  2228. case DISPPLANE_RGBX888:
  2229. return DRM_FORMAT_XBGR8888;
  2230. case DISPPLANE_BGRX101010:
  2231. return DRM_FORMAT_XRGB2101010;
  2232. case DISPPLANE_RGBX101010:
  2233. return DRM_FORMAT_XBGR2101010;
  2234. }
  2235. }
  2236. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2237. {
  2238. switch (format) {
  2239. case PLANE_CTL_FORMAT_RGB_565:
  2240. return DRM_FORMAT_RGB565;
  2241. default:
  2242. case PLANE_CTL_FORMAT_XRGB_8888:
  2243. if (rgb_order) {
  2244. if (alpha)
  2245. return DRM_FORMAT_ABGR8888;
  2246. else
  2247. return DRM_FORMAT_XBGR8888;
  2248. } else {
  2249. if (alpha)
  2250. return DRM_FORMAT_ARGB8888;
  2251. else
  2252. return DRM_FORMAT_XRGB8888;
  2253. }
  2254. case PLANE_CTL_FORMAT_XRGB_2101010:
  2255. if (rgb_order)
  2256. return DRM_FORMAT_XBGR2101010;
  2257. else
  2258. return DRM_FORMAT_XRGB2101010;
  2259. }
  2260. }
  2261. static bool
  2262. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2263. struct intel_initial_plane_config *plane_config)
  2264. {
  2265. struct drm_device *dev = crtc->base.dev;
  2266. struct drm_i915_private *dev_priv = to_i915(dev);
  2267. struct drm_i915_gem_object *obj = NULL;
  2268. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2269. struct drm_framebuffer *fb = &plane_config->fb->base;
  2270. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2271. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2272. PAGE_SIZE);
  2273. size_aligned -= base_aligned;
  2274. if (plane_config->size == 0)
  2275. return false;
  2276. /* If the FB is too big, just don't use it since fbdev is not very
  2277. * important and we should probably use that space with FBC or other
  2278. * features. */
  2279. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2280. return false;
  2281. mutex_lock(&dev->struct_mutex);
  2282. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2283. base_aligned,
  2284. base_aligned,
  2285. size_aligned);
  2286. mutex_unlock(&dev->struct_mutex);
  2287. if (!obj)
  2288. return false;
  2289. if (plane_config->tiling == I915_TILING_X)
  2290. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2291. mode_cmd.pixel_format = fb->format->format;
  2292. mode_cmd.width = fb->width;
  2293. mode_cmd.height = fb->height;
  2294. mode_cmd.pitches[0] = fb->pitches[0];
  2295. mode_cmd.modifier[0] = fb->modifier;
  2296. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2297. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2298. DRM_DEBUG_KMS("intel fb init failed\n");
  2299. goto out_unref_obj;
  2300. }
  2301. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2302. return true;
  2303. out_unref_obj:
  2304. i915_gem_object_put(obj);
  2305. return false;
  2306. }
  2307. static void
  2308. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2309. struct intel_plane_state *plane_state,
  2310. bool visible)
  2311. {
  2312. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2313. plane_state->base.visible = visible;
  2314. /* FIXME pre-g4x don't work like this */
  2315. if (visible) {
  2316. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2317. crtc_state->active_planes |= BIT(plane->id);
  2318. } else {
  2319. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2320. crtc_state->active_planes &= ~BIT(plane->id);
  2321. }
  2322. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2323. crtc_state->base.crtc->name,
  2324. crtc_state->active_planes);
  2325. }
  2326. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2327. struct intel_plane *plane)
  2328. {
  2329. struct intel_crtc_state *crtc_state =
  2330. to_intel_crtc_state(crtc->base.state);
  2331. struct intel_plane_state *plane_state =
  2332. to_intel_plane_state(plane->base.state);
  2333. intel_set_plane_visible(crtc_state, plane_state, false);
  2334. if (plane->id == PLANE_PRIMARY)
  2335. intel_pre_disable_primary_noatomic(&crtc->base);
  2336. trace_intel_disable_plane(&plane->base, crtc);
  2337. plane->disable_plane(plane, crtc);
  2338. }
  2339. static void
  2340. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2341. struct intel_initial_plane_config *plane_config)
  2342. {
  2343. struct drm_device *dev = intel_crtc->base.dev;
  2344. struct drm_i915_private *dev_priv = to_i915(dev);
  2345. struct drm_crtc *c;
  2346. struct drm_i915_gem_object *obj;
  2347. struct drm_plane *primary = intel_crtc->base.primary;
  2348. struct drm_plane_state *plane_state = primary->state;
  2349. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2350. struct intel_plane *intel_plane = to_intel_plane(primary);
  2351. struct intel_plane_state *intel_state =
  2352. to_intel_plane_state(plane_state);
  2353. struct drm_framebuffer *fb;
  2354. if (!plane_config->fb)
  2355. return;
  2356. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2357. fb = &plane_config->fb->base;
  2358. goto valid_fb;
  2359. }
  2360. kfree(plane_config->fb);
  2361. /*
  2362. * Failed to alloc the obj, check to see if we should share
  2363. * an fb with another CRTC instead
  2364. */
  2365. for_each_crtc(dev, c) {
  2366. struct intel_plane_state *state;
  2367. if (c == &intel_crtc->base)
  2368. continue;
  2369. if (!to_intel_crtc(c)->active)
  2370. continue;
  2371. state = to_intel_plane_state(c->primary->state);
  2372. if (!state->vma)
  2373. continue;
  2374. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2375. fb = c->primary->fb;
  2376. drm_framebuffer_get(fb);
  2377. goto valid_fb;
  2378. }
  2379. }
  2380. /*
  2381. * We've failed to reconstruct the BIOS FB. Current display state
  2382. * indicates that the primary plane is visible, but has a NULL FB,
  2383. * which will lead to problems later if we don't fix it up. The
  2384. * simplest solution is to just disable the primary plane now and
  2385. * pretend the BIOS never had it enabled.
  2386. */
  2387. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2388. return;
  2389. valid_fb:
  2390. mutex_lock(&dev->struct_mutex);
  2391. intel_state->vma =
  2392. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2393. mutex_unlock(&dev->struct_mutex);
  2394. if (IS_ERR(intel_state->vma)) {
  2395. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2396. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2397. intel_state->vma = NULL;
  2398. drm_framebuffer_put(fb);
  2399. return;
  2400. }
  2401. plane_state->src_x = 0;
  2402. plane_state->src_y = 0;
  2403. plane_state->src_w = fb->width << 16;
  2404. plane_state->src_h = fb->height << 16;
  2405. plane_state->crtc_x = 0;
  2406. plane_state->crtc_y = 0;
  2407. plane_state->crtc_w = fb->width;
  2408. plane_state->crtc_h = fb->height;
  2409. intel_state->base.src = drm_plane_state_src(plane_state);
  2410. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2411. obj = intel_fb_obj(fb);
  2412. if (i915_gem_object_is_tiled(obj))
  2413. dev_priv->preserve_bios_swizzle = true;
  2414. drm_framebuffer_get(fb);
  2415. primary->fb = primary->state->fb = fb;
  2416. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2417. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2418. to_intel_plane_state(plane_state),
  2419. true);
  2420. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2421. &obj->frontbuffer_bits);
  2422. }
  2423. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2424. unsigned int rotation)
  2425. {
  2426. int cpp = fb->format->cpp[plane];
  2427. switch (fb->modifier) {
  2428. case DRM_FORMAT_MOD_LINEAR:
  2429. case I915_FORMAT_MOD_X_TILED:
  2430. switch (cpp) {
  2431. case 8:
  2432. return 4096;
  2433. case 4:
  2434. case 2:
  2435. case 1:
  2436. return 8192;
  2437. default:
  2438. MISSING_CASE(cpp);
  2439. break;
  2440. }
  2441. break;
  2442. case I915_FORMAT_MOD_Y_TILED_CCS:
  2443. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2444. /* FIXME AUX plane? */
  2445. case I915_FORMAT_MOD_Y_TILED:
  2446. case I915_FORMAT_MOD_Yf_TILED:
  2447. switch (cpp) {
  2448. case 8:
  2449. return 2048;
  2450. case 4:
  2451. return 4096;
  2452. case 2:
  2453. case 1:
  2454. return 8192;
  2455. default:
  2456. MISSING_CASE(cpp);
  2457. break;
  2458. }
  2459. break;
  2460. default:
  2461. MISSING_CASE(fb->modifier);
  2462. }
  2463. return 2048;
  2464. }
  2465. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2466. int main_x, int main_y, u32 main_offset)
  2467. {
  2468. const struct drm_framebuffer *fb = plane_state->base.fb;
  2469. int hsub = fb->format->hsub;
  2470. int vsub = fb->format->vsub;
  2471. int aux_x = plane_state->aux.x;
  2472. int aux_y = plane_state->aux.y;
  2473. u32 aux_offset = plane_state->aux.offset;
  2474. u32 alignment = intel_surf_alignment(fb, 1);
  2475. while (aux_offset >= main_offset && aux_y <= main_y) {
  2476. int x, y;
  2477. if (aux_x == main_x && aux_y == main_y)
  2478. break;
  2479. if (aux_offset == 0)
  2480. break;
  2481. x = aux_x / hsub;
  2482. y = aux_y / vsub;
  2483. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2484. aux_offset, aux_offset - alignment);
  2485. aux_x = x * hsub + aux_x % hsub;
  2486. aux_y = y * vsub + aux_y % vsub;
  2487. }
  2488. if (aux_x != main_x || aux_y != main_y)
  2489. return false;
  2490. plane_state->aux.offset = aux_offset;
  2491. plane_state->aux.x = aux_x;
  2492. plane_state->aux.y = aux_y;
  2493. return true;
  2494. }
  2495. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2496. {
  2497. const struct drm_framebuffer *fb = plane_state->base.fb;
  2498. unsigned int rotation = plane_state->base.rotation;
  2499. int x = plane_state->base.src.x1 >> 16;
  2500. int y = plane_state->base.src.y1 >> 16;
  2501. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2502. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2503. int max_width = skl_max_plane_width(fb, 0, rotation);
  2504. int max_height = 4096;
  2505. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2506. if (w > max_width || h > max_height) {
  2507. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2508. w, h, max_width, max_height);
  2509. return -EINVAL;
  2510. }
  2511. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2512. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2513. alignment = intel_surf_alignment(fb, 0);
  2514. /*
  2515. * AUX surface offset is specified as the distance from the
  2516. * main surface offset, and it must be non-negative. Make
  2517. * sure that is what we will get.
  2518. */
  2519. if (offset > aux_offset)
  2520. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2521. offset, aux_offset & ~(alignment - 1));
  2522. /*
  2523. * When using an X-tiled surface, the plane blows up
  2524. * if the x offset + width exceed the stride.
  2525. *
  2526. * TODO: linear and Y-tiled seem fine, Yf untested,
  2527. */
  2528. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2529. int cpp = fb->format->cpp[0];
  2530. while ((x + w) * cpp > fb->pitches[0]) {
  2531. if (offset == 0) {
  2532. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2533. return -EINVAL;
  2534. }
  2535. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2536. offset, offset - alignment);
  2537. }
  2538. }
  2539. /*
  2540. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2541. * they match with the main surface x/y offsets.
  2542. */
  2543. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2544. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2545. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2546. if (offset == 0)
  2547. break;
  2548. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2549. offset, offset - alignment);
  2550. }
  2551. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2552. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2553. return -EINVAL;
  2554. }
  2555. }
  2556. plane_state->main.offset = offset;
  2557. plane_state->main.x = x;
  2558. plane_state->main.y = y;
  2559. return 0;
  2560. }
  2561. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2562. {
  2563. const struct drm_framebuffer *fb = plane_state->base.fb;
  2564. unsigned int rotation = plane_state->base.rotation;
  2565. int max_width = skl_max_plane_width(fb, 1, rotation);
  2566. int max_height = 4096;
  2567. int x = plane_state->base.src.x1 >> 17;
  2568. int y = plane_state->base.src.y1 >> 17;
  2569. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2570. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2571. u32 offset;
  2572. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2573. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2574. /* FIXME not quite sure how/if these apply to the chroma plane */
  2575. if (w > max_width || h > max_height) {
  2576. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2577. w, h, max_width, max_height);
  2578. return -EINVAL;
  2579. }
  2580. plane_state->aux.offset = offset;
  2581. plane_state->aux.x = x;
  2582. plane_state->aux.y = y;
  2583. return 0;
  2584. }
  2585. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2586. {
  2587. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2588. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2589. const struct drm_framebuffer *fb = plane_state->base.fb;
  2590. int src_x = plane_state->base.src.x1 >> 16;
  2591. int src_y = plane_state->base.src.y1 >> 16;
  2592. int hsub = fb->format->hsub;
  2593. int vsub = fb->format->vsub;
  2594. int x = src_x / hsub;
  2595. int y = src_y / vsub;
  2596. u32 offset;
  2597. switch (plane->id) {
  2598. case PLANE_PRIMARY:
  2599. case PLANE_SPRITE0:
  2600. break;
  2601. default:
  2602. DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
  2603. return -EINVAL;
  2604. }
  2605. if (crtc->pipe == PIPE_C) {
  2606. DRM_DEBUG_KMS("No RC support on pipe C\n");
  2607. return -EINVAL;
  2608. }
  2609. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2610. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2611. plane_state->base.rotation);
  2612. return -EINVAL;
  2613. }
  2614. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2615. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2616. plane_state->aux.offset = offset;
  2617. plane_state->aux.x = x * hsub + src_x % hsub;
  2618. plane_state->aux.y = y * vsub + src_y % vsub;
  2619. return 0;
  2620. }
  2621. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2622. {
  2623. const struct drm_framebuffer *fb = plane_state->base.fb;
  2624. unsigned int rotation = plane_state->base.rotation;
  2625. int ret;
  2626. if (rotation & DRM_MODE_REFLECT_X &&
  2627. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2628. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2629. return -EINVAL;
  2630. }
  2631. if (!plane_state->base.visible)
  2632. return 0;
  2633. /* Rotate src coordinates to match rotated GTT view */
  2634. if (drm_rotation_90_or_270(rotation))
  2635. drm_rect_rotate(&plane_state->base.src,
  2636. fb->width << 16, fb->height << 16,
  2637. DRM_MODE_ROTATE_270);
  2638. /*
  2639. * Handle the AUX surface first since
  2640. * the main surface setup depends on it.
  2641. */
  2642. if (fb->format->format == DRM_FORMAT_NV12) {
  2643. ret = skl_check_nv12_aux_surface(plane_state);
  2644. if (ret)
  2645. return ret;
  2646. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2647. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2648. ret = skl_check_ccs_aux_surface(plane_state);
  2649. if (ret)
  2650. return ret;
  2651. } else {
  2652. plane_state->aux.offset = ~0xfff;
  2653. plane_state->aux.x = 0;
  2654. plane_state->aux.y = 0;
  2655. }
  2656. ret = skl_check_main_surface(plane_state);
  2657. if (ret)
  2658. return ret;
  2659. return 0;
  2660. }
  2661. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2662. const struct intel_plane_state *plane_state)
  2663. {
  2664. struct drm_i915_private *dev_priv =
  2665. to_i915(plane_state->base.plane->dev);
  2666. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2667. const struct drm_framebuffer *fb = plane_state->base.fb;
  2668. unsigned int rotation = plane_state->base.rotation;
  2669. u32 dspcntr;
  2670. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2671. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2672. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2673. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2674. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2675. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2676. if (INTEL_GEN(dev_priv) < 4)
  2677. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2678. switch (fb->format->format) {
  2679. case DRM_FORMAT_C8:
  2680. dspcntr |= DISPPLANE_8BPP;
  2681. break;
  2682. case DRM_FORMAT_XRGB1555:
  2683. dspcntr |= DISPPLANE_BGRX555;
  2684. break;
  2685. case DRM_FORMAT_RGB565:
  2686. dspcntr |= DISPPLANE_BGRX565;
  2687. break;
  2688. case DRM_FORMAT_XRGB8888:
  2689. dspcntr |= DISPPLANE_BGRX888;
  2690. break;
  2691. case DRM_FORMAT_XBGR8888:
  2692. dspcntr |= DISPPLANE_RGBX888;
  2693. break;
  2694. case DRM_FORMAT_XRGB2101010:
  2695. dspcntr |= DISPPLANE_BGRX101010;
  2696. break;
  2697. case DRM_FORMAT_XBGR2101010:
  2698. dspcntr |= DISPPLANE_RGBX101010;
  2699. break;
  2700. default:
  2701. MISSING_CASE(fb->format->format);
  2702. return 0;
  2703. }
  2704. if (INTEL_GEN(dev_priv) >= 4 &&
  2705. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2706. dspcntr |= DISPPLANE_TILED;
  2707. if (rotation & DRM_MODE_ROTATE_180)
  2708. dspcntr |= DISPPLANE_ROTATE_180;
  2709. if (rotation & DRM_MODE_REFLECT_X)
  2710. dspcntr |= DISPPLANE_MIRROR;
  2711. return dspcntr;
  2712. }
  2713. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2714. {
  2715. struct drm_i915_private *dev_priv =
  2716. to_i915(plane_state->base.plane->dev);
  2717. int src_x = plane_state->base.src.x1 >> 16;
  2718. int src_y = plane_state->base.src.y1 >> 16;
  2719. u32 offset;
  2720. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2721. if (INTEL_GEN(dev_priv) >= 4)
  2722. offset = intel_compute_tile_offset(&src_x, &src_y,
  2723. plane_state, 0);
  2724. else
  2725. offset = 0;
  2726. /* HSW/BDW do this automagically in hardware */
  2727. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2728. unsigned int rotation = plane_state->base.rotation;
  2729. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2730. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2731. if (rotation & DRM_MODE_ROTATE_180) {
  2732. src_x += src_w - 1;
  2733. src_y += src_h - 1;
  2734. } else if (rotation & DRM_MODE_REFLECT_X) {
  2735. src_x += src_w - 1;
  2736. }
  2737. }
  2738. plane_state->main.offset = offset;
  2739. plane_state->main.x = src_x;
  2740. plane_state->main.y = src_y;
  2741. return 0;
  2742. }
  2743. static void i9xx_update_plane(struct intel_plane *plane,
  2744. const struct intel_crtc_state *crtc_state,
  2745. const struct intel_plane_state *plane_state)
  2746. {
  2747. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2748. const struct drm_framebuffer *fb = plane_state->base.fb;
  2749. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2750. u32 linear_offset;
  2751. u32 dspcntr = plane_state->ctl;
  2752. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2753. int x = plane_state->main.x;
  2754. int y = plane_state->main.y;
  2755. unsigned long irqflags;
  2756. u32 dspaddr_offset;
  2757. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2758. if (INTEL_GEN(dev_priv) >= 4)
  2759. dspaddr_offset = plane_state->main.offset;
  2760. else
  2761. dspaddr_offset = linear_offset;
  2762. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2763. if (INTEL_GEN(dev_priv) < 4) {
  2764. /* pipesrc and dspsize control the size that is scaled from,
  2765. * which should always be the user's requested size.
  2766. */
  2767. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2768. ((crtc_state->pipe_src_h - 1) << 16) |
  2769. (crtc_state->pipe_src_w - 1));
  2770. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2771. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2772. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2773. ((crtc_state->pipe_src_h - 1) << 16) |
  2774. (crtc_state->pipe_src_w - 1));
  2775. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2776. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2777. }
  2778. I915_WRITE_FW(reg, dspcntr);
  2779. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2780. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2781. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2782. intel_plane_ggtt_offset(plane_state) +
  2783. dspaddr_offset);
  2784. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2785. } else if (INTEL_GEN(dev_priv) >= 4) {
  2786. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2787. intel_plane_ggtt_offset(plane_state) +
  2788. dspaddr_offset);
  2789. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2790. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2791. } else {
  2792. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2793. intel_plane_ggtt_offset(plane_state) +
  2794. dspaddr_offset);
  2795. }
  2796. POSTING_READ_FW(reg);
  2797. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2798. }
  2799. static void i9xx_disable_plane(struct intel_plane *plane,
  2800. struct intel_crtc *crtc)
  2801. {
  2802. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2803. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2804. unsigned long irqflags;
  2805. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2806. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2807. if (INTEL_GEN(dev_priv) >= 4)
  2808. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2809. else
  2810. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2811. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2812. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2813. }
  2814. static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
  2815. {
  2816. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2817. enum intel_display_power_domain power_domain;
  2818. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2819. enum pipe pipe = plane->pipe;
  2820. bool ret;
  2821. /*
  2822. * Not 100% correct for planes that can move between pipes,
  2823. * but that's only the case for gen2-4 which don't have any
  2824. * display power wells.
  2825. */
  2826. power_domain = POWER_DOMAIN_PIPE(pipe);
  2827. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2828. return false;
  2829. ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
  2830. intel_display_power_put(dev_priv, power_domain);
  2831. return ret;
  2832. }
  2833. static u32
  2834. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2835. {
  2836. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2837. return 64;
  2838. else
  2839. return intel_tile_width_bytes(fb, plane);
  2840. }
  2841. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2842. {
  2843. struct drm_device *dev = intel_crtc->base.dev;
  2844. struct drm_i915_private *dev_priv = to_i915(dev);
  2845. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2846. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2847. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2848. }
  2849. /*
  2850. * This function detaches (aka. unbinds) unused scalers in hardware
  2851. */
  2852. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2853. {
  2854. struct intel_crtc_scaler_state *scaler_state;
  2855. int i;
  2856. scaler_state = &intel_crtc->config->scaler_state;
  2857. /* loop through and disable scalers that aren't in use */
  2858. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2859. if (!scaler_state->scalers[i].in_use)
  2860. skl_detach_scaler(intel_crtc, i);
  2861. }
  2862. }
  2863. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2864. unsigned int rotation)
  2865. {
  2866. u32 stride;
  2867. if (plane >= fb->format->num_planes)
  2868. return 0;
  2869. stride = intel_fb_pitch(fb, plane, rotation);
  2870. /*
  2871. * The stride is either expressed as a multiple of 64 bytes chunks for
  2872. * linear buffers or in number of tiles for tiled buffers.
  2873. */
  2874. if (drm_rotation_90_or_270(rotation))
  2875. stride /= intel_tile_height(fb, plane);
  2876. else
  2877. stride /= intel_fb_stride_alignment(fb, plane);
  2878. return stride;
  2879. }
  2880. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2881. {
  2882. switch (pixel_format) {
  2883. case DRM_FORMAT_C8:
  2884. return PLANE_CTL_FORMAT_INDEXED;
  2885. case DRM_FORMAT_RGB565:
  2886. return PLANE_CTL_FORMAT_RGB_565;
  2887. case DRM_FORMAT_XBGR8888:
  2888. case DRM_FORMAT_ABGR8888:
  2889. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2890. case DRM_FORMAT_XRGB8888:
  2891. case DRM_FORMAT_ARGB8888:
  2892. return PLANE_CTL_FORMAT_XRGB_8888;
  2893. case DRM_FORMAT_XRGB2101010:
  2894. return PLANE_CTL_FORMAT_XRGB_2101010;
  2895. case DRM_FORMAT_XBGR2101010:
  2896. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2897. case DRM_FORMAT_YUYV:
  2898. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2899. case DRM_FORMAT_YVYU:
  2900. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2901. case DRM_FORMAT_UYVY:
  2902. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2903. case DRM_FORMAT_VYUY:
  2904. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2905. default:
  2906. MISSING_CASE(pixel_format);
  2907. }
  2908. return 0;
  2909. }
  2910. /*
  2911. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2912. * to be already pre-multiplied. We need to add a knob (or a different
  2913. * DRM_FORMAT) for user-space to configure that.
  2914. */
  2915. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  2916. {
  2917. switch (pixel_format) {
  2918. case DRM_FORMAT_ABGR8888:
  2919. case DRM_FORMAT_ARGB8888:
  2920. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2921. default:
  2922. return PLANE_CTL_ALPHA_DISABLE;
  2923. }
  2924. }
  2925. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  2926. {
  2927. switch (pixel_format) {
  2928. case DRM_FORMAT_ABGR8888:
  2929. case DRM_FORMAT_ARGB8888:
  2930. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  2931. default:
  2932. return PLANE_COLOR_ALPHA_DISABLE;
  2933. }
  2934. }
  2935. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2936. {
  2937. switch (fb_modifier) {
  2938. case DRM_FORMAT_MOD_LINEAR:
  2939. break;
  2940. case I915_FORMAT_MOD_X_TILED:
  2941. return PLANE_CTL_TILED_X;
  2942. case I915_FORMAT_MOD_Y_TILED:
  2943. return PLANE_CTL_TILED_Y;
  2944. case I915_FORMAT_MOD_Y_TILED_CCS:
  2945. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2946. case I915_FORMAT_MOD_Yf_TILED:
  2947. return PLANE_CTL_TILED_YF;
  2948. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2949. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  2950. default:
  2951. MISSING_CASE(fb_modifier);
  2952. }
  2953. return 0;
  2954. }
  2955. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  2956. {
  2957. switch (rotate) {
  2958. case DRM_MODE_ROTATE_0:
  2959. break;
  2960. /*
  2961. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2962. * while i915 HW rotation is clockwise, thats why this swapping.
  2963. */
  2964. case DRM_MODE_ROTATE_90:
  2965. return PLANE_CTL_ROTATE_270;
  2966. case DRM_MODE_ROTATE_180:
  2967. return PLANE_CTL_ROTATE_180;
  2968. case DRM_MODE_ROTATE_270:
  2969. return PLANE_CTL_ROTATE_90;
  2970. default:
  2971. MISSING_CASE(rotate);
  2972. }
  2973. return 0;
  2974. }
  2975. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  2976. {
  2977. switch (reflect) {
  2978. case 0:
  2979. break;
  2980. case DRM_MODE_REFLECT_X:
  2981. return PLANE_CTL_FLIP_HORIZONTAL;
  2982. case DRM_MODE_REFLECT_Y:
  2983. default:
  2984. MISSING_CASE(reflect);
  2985. }
  2986. return 0;
  2987. }
  2988. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  2989. const struct intel_plane_state *plane_state)
  2990. {
  2991. struct drm_i915_private *dev_priv =
  2992. to_i915(plane_state->base.plane->dev);
  2993. const struct drm_framebuffer *fb = plane_state->base.fb;
  2994. unsigned int rotation = plane_state->base.rotation;
  2995. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  2996. u32 plane_ctl;
  2997. plane_ctl = PLANE_CTL_ENABLE;
  2998. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  2999. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3000. plane_ctl |=
  3001. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3002. PLANE_CTL_PIPE_CSC_ENABLE |
  3003. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3004. }
  3005. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3006. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3007. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3008. if (INTEL_GEN(dev_priv) >= 10)
  3009. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3010. DRM_MODE_REFLECT_MASK);
  3011. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3012. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3013. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3014. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3015. return plane_ctl;
  3016. }
  3017. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3018. const struct intel_plane_state *plane_state)
  3019. {
  3020. const struct drm_framebuffer *fb = plane_state->base.fb;
  3021. u32 plane_color_ctl = 0;
  3022. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3023. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3024. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3025. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3026. return plane_color_ctl;
  3027. }
  3028. static int
  3029. __intel_display_resume(struct drm_device *dev,
  3030. struct drm_atomic_state *state,
  3031. struct drm_modeset_acquire_ctx *ctx)
  3032. {
  3033. struct drm_crtc_state *crtc_state;
  3034. struct drm_crtc *crtc;
  3035. int i, ret;
  3036. intel_modeset_setup_hw_state(dev, ctx);
  3037. i915_redisable_vga(to_i915(dev));
  3038. if (!state)
  3039. return 0;
  3040. /*
  3041. * We've duplicated the state, pointers to the old state are invalid.
  3042. *
  3043. * Don't attempt to use the old state until we commit the duplicated state.
  3044. */
  3045. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3046. /*
  3047. * Force recalculation even if we restore
  3048. * current state. With fast modeset this may not result
  3049. * in a modeset when the state is compatible.
  3050. */
  3051. crtc_state->mode_changed = true;
  3052. }
  3053. /* ignore any reset values/BIOS leftovers in the WM registers */
  3054. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3055. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3056. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3057. WARN_ON(ret == -EDEADLK);
  3058. return ret;
  3059. }
  3060. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3061. {
  3062. return intel_has_gpu_reset(dev_priv) &&
  3063. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3064. }
  3065. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3066. {
  3067. struct drm_device *dev = &dev_priv->drm;
  3068. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3069. struct drm_atomic_state *state;
  3070. int ret;
  3071. /* reset doesn't touch the display */
  3072. if (!i915_modparams.force_reset_modeset_test &&
  3073. !gpu_reset_clobbers_display(dev_priv))
  3074. return;
  3075. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3076. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3077. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3078. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3079. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3080. i915_gem_set_wedged(dev_priv);
  3081. }
  3082. /*
  3083. * Need mode_config.mutex so that we don't
  3084. * trample ongoing ->detect() and whatnot.
  3085. */
  3086. mutex_lock(&dev->mode_config.mutex);
  3087. drm_modeset_acquire_init(ctx, 0);
  3088. while (1) {
  3089. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3090. if (ret != -EDEADLK)
  3091. break;
  3092. drm_modeset_backoff(ctx);
  3093. }
  3094. /*
  3095. * Disabling the crtcs gracefully seems nicer. Also the
  3096. * g33 docs say we should at least disable all the planes.
  3097. */
  3098. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3099. if (IS_ERR(state)) {
  3100. ret = PTR_ERR(state);
  3101. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3102. return;
  3103. }
  3104. ret = drm_atomic_helper_disable_all(dev, ctx);
  3105. if (ret) {
  3106. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3107. drm_atomic_state_put(state);
  3108. return;
  3109. }
  3110. dev_priv->modeset_restore_state = state;
  3111. state->acquire_ctx = ctx;
  3112. }
  3113. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3114. {
  3115. struct drm_device *dev = &dev_priv->drm;
  3116. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3117. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3118. int ret;
  3119. /* reset doesn't touch the display */
  3120. if (!i915_modparams.force_reset_modeset_test &&
  3121. !gpu_reset_clobbers_display(dev_priv))
  3122. return;
  3123. if (!state)
  3124. goto unlock;
  3125. dev_priv->modeset_restore_state = NULL;
  3126. /* reset doesn't touch the display */
  3127. if (!gpu_reset_clobbers_display(dev_priv)) {
  3128. /* for testing only restore the display */
  3129. ret = __intel_display_resume(dev, state, ctx);
  3130. if (ret)
  3131. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3132. } else {
  3133. /*
  3134. * The display has been reset as well,
  3135. * so need a full re-initialization.
  3136. */
  3137. intel_runtime_pm_disable_interrupts(dev_priv);
  3138. intel_runtime_pm_enable_interrupts(dev_priv);
  3139. intel_pps_unlock_regs_wa(dev_priv);
  3140. intel_modeset_init_hw(dev);
  3141. intel_init_clock_gating(dev_priv);
  3142. spin_lock_irq(&dev_priv->irq_lock);
  3143. if (dev_priv->display.hpd_irq_setup)
  3144. dev_priv->display.hpd_irq_setup(dev_priv);
  3145. spin_unlock_irq(&dev_priv->irq_lock);
  3146. ret = __intel_display_resume(dev, state, ctx);
  3147. if (ret)
  3148. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3149. intel_hpd_init(dev_priv);
  3150. }
  3151. drm_atomic_state_put(state);
  3152. unlock:
  3153. drm_modeset_drop_locks(ctx);
  3154. drm_modeset_acquire_fini(ctx);
  3155. mutex_unlock(&dev->mode_config.mutex);
  3156. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3157. }
  3158. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3159. const struct intel_crtc_state *new_crtc_state)
  3160. {
  3161. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3162. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3163. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3164. crtc->base.mode = new_crtc_state->base.mode;
  3165. /*
  3166. * Update pipe size and adjust fitter if needed: the reason for this is
  3167. * that in compute_mode_changes we check the native mode (not the pfit
  3168. * mode) to see if we can flip rather than do a full mode set. In the
  3169. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3170. * pfit state, we'll end up with a big fb scanned out into the wrong
  3171. * sized surface.
  3172. */
  3173. I915_WRITE(PIPESRC(crtc->pipe),
  3174. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3175. (new_crtc_state->pipe_src_h - 1));
  3176. /* on skylake this is done by detaching scalers */
  3177. if (INTEL_GEN(dev_priv) >= 9) {
  3178. skl_detach_scalers(crtc);
  3179. if (new_crtc_state->pch_pfit.enabled)
  3180. skylake_pfit_enable(crtc);
  3181. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3182. if (new_crtc_state->pch_pfit.enabled)
  3183. ironlake_pfit_enable(crtc);
  3184. else if (old_crtc_state->pch_pfit.enabled)
  3185. ironlake_pfit_disable(crtc, true);
  3186. }
  3187. }
  3188. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3189. {
  3190. struct drm_device *dev = crtc->base.dev;
  3191. struct drm_i915_private *dev_priv = to_i915(dev);
  3192. int pipe = crtc->pipe;
  3193. i915_reg_t reg;
  3194. u32 temp;
  3195. /* enable normal train */
  3196. reg = FDI_TX_CTL(pipe);
  3197. temp = I915_READ(reg);
  3198. if (IS_IVYBRIDGE(dev_priv)) {
  3199. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3200. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3201. } else {
  3202. temp &= ~FDI_LINK_TRAIN_NONE;
  3203. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3204. }
  3205. I915_WRITE(reg, temp);
  3206. reg = FDI_RX_CTL(pipe);
  3207. temp = I915_READ(reg);
  3208. if (HAS_PCH_CPT(dev_priv)) {
  3209. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3210. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3211. } else {
  3212. temp &= ~FDI_LINK_TRAIN_NONE;
  3213. temp |= FDI_LINK_TRAIN_NONE;
  3214. }
  3215. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3216. /* wait one idle pattern time */
  3217. POSTING_READ(reg);
  3218. udelay(1000);
  3219. /* IVB wants error correction enabled */
  3220. if (IS_IVYBRIDGE(dev_priv))
  3221. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3222. FDI_FE_ERRC_ENABLE);
  3223. }
  3224. /* The FDI link training functions for ILK/Ibexpeak. */
  3225. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3226. const struct intel_crtc_state *crtc_state)
  3227. {
  3228. struct drm_device *dev = crtc->base.dev;
  3229. struct drm_i915_private *dev_priv = to_i915(dev);
  3230. int pipe = crtc->pipe;
  3231. i915_reg_t reg;
  3232. u32 temp, tries;
  3233. /* FDI needs bits from pipe first */
  3234. assert_pipe_enabled(dev_priv, pipe);
  3235. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3236. for train result */
  3237. reg = FDI_RX_IMR(pipe);
  3238. temp = I915_READ(reg);
  3239. temp &= ~FDI_RX_SYMBOL_LOCK;
  3240. temp &= ~FDI_RX_BIT_LOCK;
  3241. I915_WRITE(reg, temp);
  3242. I915_READ(reg);
  3243. udelay(150);
  3244. /* enable CPU FDI TX and PCH FDI RX */
  3245. reg = FDI_TX_CTL(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3248. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3249. temp &= ~FDI_LINK_TRAIN_NONE;
  3250. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3251. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3252. reg = FDI_RX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. temp &= ~FDI_LINK_TRAIN_NONE;
  3255. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3256. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3257. POSTING_READ(reg);
  3258. udelay(150);
  3259. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3260. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3261. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3262. FDI_RX_PHASE_SYNC_POINTER_EN);
  3263. reg = FDI_RX_IIR(pipe);
  3264. for (tries = 0; tries < 5; tries++) {
  3265. temp = I915_READ(reg);
  3266. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3267. if ((temp & FDI_RX_BIT_LOCK)) {
  3268. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3269. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3270. break;
  3271. }
  3272. }
  3273. if (tries == 5)
  3274. DRM_ERROR("FDI train 1 fail!\n");
  3275. /* Train 2 */
  3276. reg = FDI_TX_CTL(pipe);
  3277. temp = I915_READ(reg);
  3278. temp &= ~FDI_LINK_TRAIN_NONE;
  3279. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3280. I915_WRITE(reg, temp);
  3281. reg = FDI_RX_CTL(pipe);
  3282. temp = I915_READ(reg);
  3283. temp &= ~FDI_LINK_TRAIN_NONE;
  3284. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3285. I915_WRITE(reg, temp);
  3286. POSTING_READ(reg);
  3287. udelay(150);
  3288. reg = FDI_RX_IIR(pipe);
  3289. for (tries = 0; tries < 5; tries++) {
  3290. temp = I915_READ(reg);
  3291. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3292. if (temp & FDI_RX_SYMBOL_LOCK) {
  3293. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3294. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3295. break;
  3296. }
  3297. }
  3298. if (tries == 5)
  3299. DRM_ERROR("FDI train 2 fail!\n");
  3300. DRM_DEBUG_KMS("FDI train done\n");
  3301. }
  3302. static const int snb_b_fdi_train_param[] = {
  3303. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3304. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3305. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3306. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3307. };
  3308. /* The FDI link training functions for SNB/Cougarpoint. */
  3309. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3310. const struct intel_crtc_state *crtc_state)
  3311. {
  3312. struct drm_device *dev = crtc->base.dev;
  3313. struct drm_i915_private *dev_priv = to_i915(dev);
  3314. int pipe = crtc->pipe;
  3315. i915_reg_t reg;
  3316. u32 temp, i, retry;
  3317. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3318. for train result */
  3319. reg = FDI_RX_IMR(pipe);
  3320. temp = I915_READ(reg);
  3321. temp &= ~FDI_RX_SYMBOL_LOCK;
  3322. temp &= ~FDI_RX_BIT_LOCK;
  3323. I915_WRITE(reg, temp);
  3324. POSTING_READ(reg);
  3325. udelay(150);
  3326. /* enable CPU FDI TX and PCH FDI RX */
  3327. reg = FDI_TX_CTL(pipe);
  3328. temp = I915_READ(reg);
  3329. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3330. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3331. temp &= ~FDI_LINK_TRAIN_NONE;
  3332. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3333. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3334. /* SNB-B */
  3335. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3336. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3337. I915_WRITE(FDI_RX_MISC(pipe),
  3338. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3339. reg = FDI_RX_CTL(pipe);
  3340. temp = I915_READ(reg);
  3341. if (HAS_PCH_CPT(dev_priv)) {
  3342. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3343. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3344. } else {
  3345. temp &= ~FDI_LINK_TRAIN_NONE;
  3346. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3347. }
  3348. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3349. POSTING_READ(reg);
  3350. udelay(150);
  3351. for (i = 0; i < 4; i++) {
  3352. reg = FDI_TX_CTL(pipe);
  3353. temp = I915_READ(reg);
  3354. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3355. temp |= snb_b_fdi_train_param[i];
  3356. I915_WRITE(reg, temp);
  3357. POSTING_READ(reg);
  3358. udelay(500);
  3359. for (retry = 0; retry < 5; retry++) {
  3360. reg = FDI_RX_IIR(pipe);
  3361. temp = I915_READ(reg);
  3362. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3363. if (temp & FDI_RX_BIT_LOCK) {
  3364. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3365. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3366. break;
  3367. }
  3368. udelay(50);
  3369. }
  3370. if (retry < 5)
  3371. break;
  3372. }
  3373. if (i == 4)
  3374. DRM_ERROR("FDI train 1 fail!\n");
  3375. /* Train 2 */
  3376. reg = FDI_TX_CTL(pipe);
  3377. temp = I915_READ(reg);
  3378. temp &= ~FDI_LINK_TRAIN_NONE;
  3379. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3380. if (IS_GEN6(dev_priv)) {
  3381. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3382. /* SNB-B */
  3383. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3384. }
  3385. I915_WRITE(reg, temp);
  3386. reg = FDI_RX_CTL(pipe);
  3387. temp = I915_READ(reg);
  3388. if (HAS_PCH_CPT(dev_priv)) {
  3389. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3390. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3391. } else {
  3392. temp &= ~FDI_LINK_TRAIN_NONE;
  3393. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3394. }
  3395. I915_WRITE(reg, temp);
  3396. POSTING_READ(reg);
  3397. udelay(150);
  3398. for (i = 0; i < 4; i++) {
  3399. reg = FDI_TX_CTL(pipe);
  3400. temp = I915_READ(reg);
  3401. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3402. temp |= snb_b_fdi_train_param[i];
  3403. I915_WRITE(reg, temp);
  3404. POSTING_READ(reg);
  3405. udelay(500);
  3406. for (retry = 0; retry < 5; retry++) {
  3407. reg = FDI_RX_IIR(pipe);
  3408. temp = I915_READ(reg);
  3409. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3410. if (temp & FDI_RX_SYMBOL_LOCK) {
  3411. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3412. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3413. break;
  3414. }
  3415. udelay(50);
  3416. }
  3417. if (retry < 5)
  3418. break;
  3419. }
  3420. if (i == 4)
  3421. DRM_ERROR("FDI train 2 fail!\n");
  3422. DRM_DEBUG_KMS("FDI train done.\n");
  3423. }
  3424. /* Manual link training for Ivy Bridge A0 parts */
  3425. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3426. const struct intel_crtc_state *crtc_state)
  3427. {
  3428. struct drm_device *dev = crtc->base.dev;
  3429. struct drm_i915_private *dev_priv = to_i915(dev);
  3430. int pipe = crtc->pipe;
  3431. i915_reg_t reg;
  3432. u32 temp, i, j;
  3433. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3434. for train result */
  3435. reg = FDI_RX_IMR(pipe);
  3436. temp = I915_READ(reg);
  3437. temp &= ~FDI_RX_SYMBOL_LOCK;
  3438. temp &= ~FDI_RX_BIT_LOCK;
  3439. I915_WRITE(reg, temp);
  3440. POSTING_READ(reg);
  3441. udelay(150);
  3442. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3443. I915_READ(FDI_RX_IIR(pipe)));
  3444. /* Try each vswing and preemphasis setting twice before moving on */
  3445. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3446. /* disable first in case we need to retry */
  3447. reg = FDI_TX_CTL(pipe);
  3448. temp = I915_READ(reg);
  3449. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3450. temp &= ~FDI_TX_ENABLE;
  3451. I915_WRITE(reg, temp);
  3452. reg = FDI_RX_CTL(pipe);
  3453. temp = I915_READ(reg);
  3454. temp &= ~FDI_LINK_TRAIN_AUTO;
  3455. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3456. temp &= ~FDI_RX_ENABLE;
  3457. I915_WRITE(reg, temp);
  3458. /* enable CPU FDI TX and PCH FDI RX */
  3459. reg = FDI_TX_CTL(pipe);
  3460. temp = I915_READ(reg);
  3461. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3462. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3463. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3464. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3465. temp |= snb_b_fdi_train_param[j/2];
  3466. temp |= FDI_COMPOSITE_SYNC;
  3467. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3468. I915_WRITE(FDI_RX_MISC(pipe),
  3469. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3470. reg = FDI_RX_CTL(pipe);
  3471. temp = I915_READ(reg);
  3472. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3473. temp |= FDI_COMPOSITE_SYNC;
  3474. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3475. POSTING_READ(reg);
  3476. udelay(1); /* should be 0.5us */
  3477. for (i = 0; i < 4; i++) {
  3478. reg = FDI_RX_IIR(pipe);
  3479. temp = I915_READ(reg);
  3480. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3481. if (temp & FDI_RX_BIT_LOCK ||
  3482. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3483. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3484. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3485. i);
  3486. break;
  3487. }
  3488. udelay(1); /* should be 0.5us */
  3489. }
  3490. if (i == 4) {
  3491. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3492. continue;
  3493. }
  3494. /* Train 2 */
  3495. reg = FDI_TX_CTL(pipe);
  3496. temp = I915_READ(reg);
  3497. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3498. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3499. I915_WRITE(reg, temp);
  3500. reg = FDI_RX_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3503. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3504. I915_WRITE(reg, temp);
  3505. POSTING_READ(reg);
  3506. udelay(2); /* should be 1.5us */
  3507. for (i = 0; i < 4; i++) {
  3508. reg = FDI_RX_IIR(pipe);
  3509. temp = I915_READ(reg);
  3510. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3511. if (temp & FDI_RX_SYMBOL_LOCK ||
  3512. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3513. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3514. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3515. i);
  3516. goto train_done;
  3517. }
  3518. udelay(2); /* should be 1.5us */
  3519. }
  3520. if (i == 4)
  3521. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3522. }
  3523. train_done:
  3524. DRM_DEBUG_KMS("FDI train done.\n");
  3525. }
  3526. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3527. {
  3528. struct drm_device *dev = intel_crtc->base.dev;
  3529. struct drm_i915_private *dev_priv = to_i915(dev);
  3530. int pipe = intel_crtc->pipe;
  3531. i915_reg_t reg;
  3532. u32 temp;
  3533. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3534. reg = FDI_RX_CTL(pipe);
  3535. temp = I915_READ(reg);
  3536. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3537. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3538. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3539. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3540. POSTING_READ(reg);
  3541. udelay(200);
  3542. /* Switch from Rawclk to PCDclk */
  3543. temp = I915_READ(reg);
  3544. I915_WRITE(reg, temp | FDI_PCDCLK);
  3545. POSTING_READ(reg);
  3546. udelay(200);
  3547. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3548. reg = FDI_TX_CTL(pipe);
  3549. temp = I915_READ(reg);
  3550. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3551. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3552. POSTING_READ(reg);
  3553. udelay(100);
  3554. }
  3555. }
  3556. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3557. {
  3558. struct drm_device *dev = intel_crtc->base.dev;
  3559. struct drm_i915_private *dev_priv = to_i915(dev);
  3560. int pipe = intel_crtc->pipe;
  3561. i915_reg_t reg;
  3562. u32 temp;
  3563. /* Switch from PCDclk to Rawclk */
  3564. reg = FDI_RX_CTL(pipe);
  3565. temp = I915_READ(reg);
  3566. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3567. /* Disable CPU FDI TX PLL */
  3568. reg = FDI_TX_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3571. POSTING_READ(reg);
  3572. udelay(100);
  3573. reg = FDI_RX_CTL(pipe);
  3574. temp = I915_READ(reg);
  3575. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3576. /* Wait for the clocks to turn off. */
  3577. POSTING_READ(reg);
  3578. udelay(100);
  3579. }
  3580. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3581. {
  3582. struct drm_device *dev = crtc->dev;
  3583. struct drm_i915_private *dev_priv = to_i915(dev);
  3584. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3585. int pipe = intel_crtc->pipe;
  3586. i915_reg_t reg;
  3587. u32 temp;
  3588. /* disable CPU FDI tx and PCH FDI rx */
  3589. reg = FDI_TX_CTL(pipe);
  3590. temp = I915_READ(reg);
  3591. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3592. POSTING_READ(reg);
  3593. reg = FDI_RX_CTL(pipe);
  3594. temp = I915_READ(reg);
  3595. temp &= ~(0x7 << 16);
  3596. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3597. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3598. POSTING_READ(reg);
  3599. udelay(100);
  3600. /* Ironlake workaround, disable clock pointer after downing FDI */
  3601. if (HAS_PCH_IBX(dev_priv))
  3602. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3603. /* still set train pattern 1 */
  3604. reg = FDI_TX_CTL(pipe);
  3605. temp = I915_READ(reg);
  3606. temp &= ~FDI_LINK_TRAIN_NONE;
  3607. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3608. I915_WRITE(reg, temp);
  3609. reg = FDI_RX_CTL(pipe);
  3610. temp = I915_READ(reg);
  3611. if (HAS_PCH_CPT(dev_priv)) {
  3612. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3613. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3614. } else {
  3615. temp &= ~FDI_LINK_TRAIN_NONE;
  3616. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3617. }
  3618. /* BPC in FDI rx is consistent with that in PIPECONF */
  3619. temp &= ~(0x07 << 16);
  3620. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3621. I915_WRITE(reg, temp);
  3622. POSTING_READ(reg);
  3623. udelay(100);
  3624. }
  3625. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3626. {
  3627. struct drm_crtc *crtc;
  3628. bool cleanup_done;
  3629. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3630. struct drm_crtc_commit *commit;
  3631. spin_lock(&crtc->commit_lock);
  3632. commit = list_first_entry_or_null(&crtc->commit_list,
  3633. struct drm_crtc_commit, commit_entry);
  3634. cleanup_done = commit ?
  3635. try_wait_for_completion(&commit->cleanup_done) : true;
  3636. spin_unlock(&crtc->commit_lock);
  3637. if (cleanup_done)
  3638. continue;
  3639. drm_crtc_wait_one_vblank(crtc);
  3640. return true;
  3641. }
  3642. return false;
  3643. }
  3644. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3645. {
  3646. u32 temp;
  3647. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3648. mutex_lock(&dev_priv->sb_lock);
  3649. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3650. temp |= SBI_SSCCTL_DISABLE;
  3651. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3652. mutex_unlock(&dev_priv->sb_lock);
  3653. }
  3654. /* Program iCLKIP clock to the desired frequency */
  3655. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3656. {
  3657. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3658. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3659. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3660. u32 temp;
  3661. lpt_disable_iclkip(dev_priv);
  3662. /* The iCLK virtual clock root frequency is in MHz,
  3663. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3664. * divisors, it is necessary to divide one by another, so we
  3665. * convert the virtual clock precision to KHz here for higher
  3666. * precision.
  3667. */
  3668. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3669. u32 iclk_virtual_root_freq = 172800 * 1000;
  3670. u32 iclk_pi_range = 64;
  3671. u32 desired_divisor;
  3672. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3673. clock << auxdiv);
  3674. divsel = (desired_divisor / iclk_pi_range) - 2;
  3675. phaseinc = desired_divisor % iclk_pi_range;
  3676. /*
  3677. * Near 20MHz is a corner case which is
  3678. * out of range for the 7-bit divisor
  3679. */
  3680. if (divsel <= 0x7f)
  3681. break;
  3682. }
  3683. /* This should not happen with any sane values */
  3684. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3685. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3686. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3687. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3688. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3689. clock,
  3690. auxdiv,
  3691. divsel,
  3692. phasedir,
  3693. phaseinc);
  3694. mutex_lock(&dev_priv->sb_lock);
  3695. /* Program SSCDIVINTPHASE6 */
  3696. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3697. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3698. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3699. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3700. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3701. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3702. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3703. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3704. /* Program SSCAUXDIV */
  3705. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3706. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3707. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3708. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3709. /* Enable modulator and associated divider */
  3710. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3711. temp &= ~SBI_SSCCTL_DISABLE;
  3712. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3713. mutex_unlock(&dev_priv->sb_lock);
  3714. /* Wait for initialization time */
  3715. udelay(24);
  3716. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3717. }
  3718. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3719. {
  3720. u32 divsel, phaseinc, auxdiv;
  3721. u32 iclk_virtual_root_freq = 172800 * 1000;
  3722. u32 iclk_pi_range = 64;
  3723. u32 desired_divisor;
  3724. u32 temp;
  3725. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3726. return 0;
  3727. mutex_lock(&dev_priv->sb_lock);
  3728. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3729. if (temp & SBI_SSCCTL_DISABLE) {
  3730. mutex_unlock(&dev_priv->sb_lock);
  3731. return 0;
  3732. }
  3733. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3734. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3735. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3736. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3737. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3738. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3739. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3740. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3741. mutex_unlock(&dev_priv->sb_lock);
  3742. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3743. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3744. desired_divisor << auxdiv);
  3745. }
  3746. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3747. enum pipe pch_transcoder)
  3748. {
  3749. struct drm_device *dev = crtc->base.dev;
  3750. struct drm_i915_private *dev_priv = to_i915(dev);
  3751. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3752. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3753. I915_READ(HTOTAL(cpu_transcoder)));
  3754. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3755. I915_READ(HBLANK(cpu_transcoder)));
  3756. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3757. I915_READ(HSYNC(cpu_transcoder)));
  3758. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3759. I915_READ(VTOTAL(cpu_transcoder)));
  3760. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3761. I915_READ(VBLANK(cpu_transcoder)));
  3762. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3763. I915_READ(VSYNC(cpu_transcoder)));
  3764. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3765. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3766. }
  3767. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3768. {
  3769. struct drm_i915_private *dev_priv = to_i915(dev);
  3770. uint32_t temp;
  3771. temp = I915_READ(SOUTH_CHICKEN1);
  3772. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3773. return;
  3774. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3775. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3776. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3777. if (enable)
  3778. temp |= FDI_BC_BIFURCATION_SELECT;
  3779. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3780. I915_WRITE(SOUTH_CHICKEN1, temp);
  3781. POSTING_READ(SOUTH_CHICKEN1);
  3782. }
  3783. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3784. {
  3785. struct drm_device *dev = intel_crtc->base.dev;
  3786. switch (intel_crtc->pipe) {
  3787. case PIPE_A:
  3788. break;
  3789. case PIPE_B:
  3790. if (intel_crtc->config->fdi_lanes > 2)
  3791. cpt_set_fdi_bc_bifurcation(dev, false);
  3792. else
  3793. cpt_set_fdi_bc_bifurcation(dev, true);
  3794. break;
  3795. case PIPE_C:
  3796. cpt_set_fdi_bc_bifurcation(dev, true);
  3797. break;
  3798. default:
  3799. BUG();
  3800. }
  3801. }
  3802. /* Return which DP Port should be selected for Transcoder DP control */
  3803. static enum port
  3804. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3805. {
  3806. struct drm_device *dev = crtc->base.dev;
  3807. struct intel_encoder *encoder;
  3808. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3809. if (encoder->type == INTEL_OUTPUT_DP ||
  3810. encoder->type == INTEL_OUTPUT_EDP)
  3811. return encoder->port;
  3812. }
  3813. return -1;
  3814. }
  3815. /*
  3816. * Enable PCH resources required for PCH ports:
  3817. * - PCH PLLs
  3818. * - FDI training & RX/TX
  3819. * - update transcoder timings
  3820. * - DP transcoding bits
  3821. * - transcoder
  3822. */
  3823. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3824. {
  3825. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3826. struct drm_device *dev = crtc->base.dev;
  3827. struct drm_i915_private *dev_priv = to_i915(dev);
  3828. int pipe = crtc->pipe;
  3829. u32 temp;
  3830. assert_pch_transcoder_disabled(dev_priv, pipe);
  3831. if (IS_IVYBRIDGE(dev_priv))
  3832. ivybridge_update_fdi_bc_bifurcation(crtc);
  3833. /* Write the TU size bits before fdi link training, so that error
  3834. * detection works. */
  3835. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3836. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3837. /* For PCH output, training FDI link */
  3838. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3839. /* We need to program the right clock selection before writing the pixel
  3840. * mutliplier into the DPLL. */
  3841. if (HAS_PCH_CPT(dev_priv)) {
  3842. u32 sel;
  3843. temp = I915_READ(PCH_DPLL_SEL);
  3844. temp |= TRANS_DPLL_ENABLE(pipe);
  3845. sel = TRANS_DPLLB_SEL(pipe);
  3846. if (crtc_state->shared_dpll ==
  3847. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3848. temp |= sel;
  3849. else
  3850. temp &= ~sel;
  3851. I915_WRITE(PCH_DPLL_SEL, temp);
  3852. }
  3853. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3854. * transcoder, and we actually should do this to not upset any PCH
  3855. * transcoder that already use the clock when we share it.
  3856. *
  3857. * Note that enable_shared_dpll tries to do the right thing, but
  3858. * get_shared_dpll unconditionally resets the pll - we need that to have
  3859. * the right LVDS enable sequence. */
  3860. intel_enable_shared_dpll(crtc);
  3861. /* set transcoder timing, panel must allow it */
  3862. assert_panel_unlocked(dev_priv, pipe);
  3863. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3864. intel_fdi_normal_train(crtc);
  3865. /* For PCH DP, enable TRANS_DP_CTL */
  3866. if (HAS_PCH_CPT(dev_priv) &&
  3867. intel_crtc_has_dp_encoder(crtc_state)) {
  3868. const struct drm_display_mode *adjusted_mode =
  3869. &crtc_state->base.adjusted_mode;
  3870. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3871. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3872. temp = I915_READ(reg);
  3873. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3874. TRANS_DP_SYNC_MASK |
  3875. TRANS_DP_BPC_MASK);
  3876. temp |= TRANS_DP_OUTPUT_ENABLE;
  3877. temp |= bpc << 9; /* same format but at 11:9 */
  3878. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3879. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3880. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3881. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3882. switch (intel_trans_dp_port_sel(crtc)) {
  3883. case PORT_B:
  3884. temp |= TRANS_DP_PORT_SEL_B;
  3885. break;
  3886. case PORT_C:
  3887. temp |= TRANS_DP_PORT_SEL_C;
  3888. break;
  3889. case PORT_D:
  3890. temp |= TRANS_DP_PORT_SEL_D;
  3891. break;
  3892. default:
  3893. BUG();
  3894. }
  3895. I915_WRITE(reg, temp);
  3896. }
  3897. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3898. }
  3899. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3900. {
  3901. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3902. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3903. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3904. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3905. lpt_program_iclkip(crtc);
  3906. /* Set transcoder timing. */
  3907. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3908. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3909. }
  3910. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3911. {
  3912. struct drm_i915_private *dev_priv = to_i915(dev);
  3913. i915_reg_t dslreg = PIPEDSL(pipe);
  3914. u32 temp;
  3915. temp = I915_READ(dslreg);
  3916. udelay(500);
  3917. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3918. if (wait_for(I915_READ(dslreg) != temp, 5))
  3919. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3920. }
  3921. }
  3922. static int
  3923. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3924. unsigned int scaler_user, int *scaler_id,
  3925. int src_w, int src_h, int dst_w, int dst_h)
  3926. {
  3927. struct intel_crtc_scaler_state *scaler_state =
  3928. &crtc_state->scaler_state;
  3929. struct intel_crtc *intel_crtc =
  3930. to_intel_crtc(crtc_state->base.crtc);
  3931. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3932. const struct drm_display_mode *adjusted_mode =
  3933. &crtc_state->base.adjusted_mode;
  3934. int need_scaling;
  3935. /*
  3936. * Src coordinates are already rotated by 270 degrees for
  3937. * the 90/270 degree plane rotation cases (to match the
  3938. * GTT mapping), hence no need to account for rotation here.
  3939. */
  3940. need_scaling = src_w != dst_w || src_h != dst_h;
  3941. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  3942. need_scaling = true;
  3943. /*
  3944. * Scaling/fitting not supported in IF-ID mode in GEN9+
  3945. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  3946. * Once NV12 is enabled, handle it here while allocating scaler
  3947. * for NV12.
  3948. */
  3949. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  3950. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3951. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  3952. return -EINVAL;
  3953. }
  3954. /*
  3955. * if plane is being disabled or scaler is no more required or force detach
  3956. * - free scaler binded to this plane/crtc
  3957. * - in order to do this, update crtc->scaler_usage
  3958. *
  3959. * Here scaler state in crtc_state is set free so that
  3960. * scaler can be assigned to other user. Actual register
  3961. * update to free the scaler is done in plane/panel-fit programming.
  3962. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3963. */
  3964. if (force_detach || !need_scaling) {
  3965. if (*scaler_id >= 0) {
  3966. scaler_state->scaler_users &= ~(1 << scaler_user);
  3967. scaler_state->scalers[*scaler_id].in_use = 0;
  3968. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3969. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3970. intel_crtc->pipe, scaler_user, *scaler_id,
  3971. scaler_state->scaler_users);
  3972. *scaler_id = -1;
  3973. }
  3974. return 0;
  3975. }
  3976. /* range checks */
  3977. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3978. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3979. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3980. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3981. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3982. "size is out of scaler range\n",
  3983. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3984. return -EINVAL;
  3985. }
  3986. /* mark this plane as a scaler user in crtc_state */
  3987. scaler_state->scaler_users |= (1 << scaler_user);
  3988. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3989. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3990. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3991. scaler_state->scaler_users);
  3992. return 0;
  3993. }
  3994. /**
  3995. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3996. *
  3997. * @state: crtc's scaler state
  3998. *
  3999. * Return
  4000. * 0 - scaler_usage updated successfully
  4001. * error - requested scaling cannot be supported or other error condition
  4002. */
  4003. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4004. {
  4005. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4006. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4007. &state->scaler_state.scaler_id,
  4008. state->pipe_src_w, state->pipe_src_h,
  4009. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4010. }
  4011. /**
  4012. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4013. *
  4014. * @state: crtc's scaler state
  4015. * @plane_state: atomic plane state to update
  4016. *
  4017. * Return
  4018. * 0 - scaler_usage updated successfully
  4019. * error - requested scaling cannot be supported or other error condition
  4020. */
  4021. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4022. struct intel_plane_state *plane_state)
  4023. {
  4024. struct intel_plane *intel_plane =
  4025. to_intel_plane(plane_state->base.plane);
  4026. struct drm_framebuffer *fb = plane_state->base.fb;
  4027. int ret;
  4028. bool force_detach = !fb || !plane_state->base.visible;
  4029. ret = skl_update_scaler(crtc_state, force_detach,
  4030. drm_plane_index(&intel_plane->base),
  4031. &plane_state->scaler_id,
  4032. drm_rect_width(&plane_state->base.src) >> 16,
  4033. drm_rect_height(&plane_state->base.src) >> 16,
  4034. drm_rect_width(&plane_state->base.dst),
  4035. drm_rect_height(&plane_state->base.dst));
  4036. if (ret || plane_state->scaler_id < 0)
  4037. return ret;
  4038. /* check colorkey */
  4039. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4040. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4041. intel_plane->base.base.id,
  4042. intel_plane->base.name);
  4043. return -EINVAL;
  4044. }
  4045. /* Check src format */
  4046. switch (fb->format->format) {
  4047. case DRM_FORMAT_RGB565:
  4048. case DRM_FORMAT_XBGR8888:
  4049. case DRM_FORMAT_XRGB8888:
  4050. case DRM_FORMAT_ABGR8888:
  4051. case DRM_FORMAT_ARGB8888:
  4052. case DRM_FORMAT_XRGB2101010:
  4053. case DRM_FORMAT_XBGR2101010:
  4054. case DRM_FORMAT_YUYV:
  4055. case DRM_FORMAT_YVYU:
  4056. case DRM_FORMAT_UYVY:
  4057. case DRM_FORMAT_VYUY:
  4058. break;
  4059. default:
  4060. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4061. intel_plane->base.base.id, intel_plane->base.name,
  4062. fb->base.id, fb->format->format);
  4063. return -EINVAL;
  4064. }
  4065. return 0;
  4066. }
  4067. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4068. {
  4069. int i;
  4070. for (i = 0; i < crtc->num_scalers; i++)
  4071. skl_detach_scaler(crtc, i);
  4072. }
  4073. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4074. {
  4075. struct drm_device *dev = crtc->base.dev;
  4076. struct drm_i915_private *dev_priv = to_i915(dev);
  4077. int pipe = crtc->pipe;
  4078. struct intel_crtc_scaler_state *scaler_state =
  4079. &crtc->config->scaler_state;
  4080. if (crtc->config->pch_pfit.enabled) {
  4081. int id;
  4082. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4083. return;
  4084. id = scaler_state->scaler_id;
  4085. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4086. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4087. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4088. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4089. }
  4090. }
  4091. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4092. {
  4093. struct drm_device *dev = crtc->base.dev;
  4094. struct drm_i915_private *dev_priv = to_i915(dev);
  4095. int pipe = crtc->pipe;
  4096. if (crtc->config->pch_pfit.enabled) {
  4097. /* Force use of hard-coded filter coefficients
  4098. * as some pre-programmed values are broken,
  4099. * e.g. x201.
  4100. */
  4101. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4102. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4103. PF_PIPE_SEL_IVB(pipe));
  4104. else
  4105. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4106. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4107. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4108. }
  4109. }
  4110. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4111. {
  4112. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4113. struct drm_device *dev = crtc->base.dev;
  4114. struct drm_i915_private *dev_priv = to_i915(dev);
  4115. if (!crtc_state->ips_enabled)
  4116. return;
  4117. /*
  4118. * We can only enable IPS after we enable a plane and wait for a vblank
  4119. * This function is called from post_plane_update, which is run after
  4120. * a vblank wait.
  4121. */
  4122. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4123. if (IS_BROADWELL(dev_priv)) {
  4124. mutex_lock(&dev_priv->pcu_lock);
  4125. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4126. IPS_ENABLE | IPS_PCODE_CONTROL));
  4127. mutex_unlock(&dev_priv->pcu_lock);
  4128. /* Quoting Art Runyan: "its not safe to expect any particular
  4129. * value in IPS_CTL bit 31 after enabling IPS through the
  4130. * mailbox." Moreover, the mailbox may return a bogus state,
  4131. * so we need to just enable it and continue on.
  4132. */
  4133. } else {
  4134. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4135. /* The bit only becomes 1 in the next vblank, so this wait here
  4136. * is essentially intel_wait_for_vblank. If we don't have this
  4137. * and don't wait for vblanks until the end of crtc_enable, then
  4138. * the HW state readout code will complain that the expected
  4139. * IPS_CTL value is not the one we read. */
  4140. if (intel_wait_for_register(dev_priv,
  4141. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4142. 50))
  4143. DRM_ERROR("Timed out waiting for IPS enable\n");
  4144. }
  4145. }
  4146. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4147. {
  4148. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4149. struct drm_device *dev = crtc->base.dev;
  4150. struct drm_i915_private *dev_priv = to_i915(dev);
  4151. if (!crtc_state->ips_enabled)
  4152. return;
  4153. if (IS_BROADWELL(dev_priv)) {
  4154. mutex_lock(&dev_priv->pcu_lock);
  4155. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4156. mutex_unlock(&dev_priv->pcu_lock);
  4157. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4158. if (intel_wait_for_register(dev_priv,
  4159. IPS_CTL, IPS_ENABLE, 0,
  4160. 42))
  4161. DRM_ERROR("Timed out waiting for IPS disable\n");
  4162. } else {
  4163. I915_WRITE(IPS_CTL, 0);
  4164. POSTING_READ(IPS_CTL);
  4165. }
  4166. /* We need to wait for a vblank before we can disable the plane. */
  4167. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4168. }
  4169. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4170. {
  4171. if (intel_crtc->overlay) {
  4172. struct drm_device *dev = intel_crtc->base.dev;
  4173. mutex_lock(&dev->struct_mutex);
  4174. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4175. mutex_unlock(&dev->struct_mutex);
  4176. }
  4177. /* Let userspace switch the overlay on again. In most cases userspace
  4178. * has to recompute where to put it anyway.
  4179. */
  4180. }
  4181. /**
  4182. * intel_post_enable_primary - Perform operations after enabling primary plane
  4183. * @crtc: the CRTC whose primary plane was just enabled
  4184. *
  4185. * Performs potentially sleeping operations that must be done after the primary
  4186. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4187. * called due to an explicit primary plane update, or due to an implicit
  4188. * re-enable that is caused when a sprite plane is updated to no longer
  4189. * completely hide the primary plane.
  4190. */
  4191. static void
  4192. intel_post_enable_primary(struct drm_crtc *crtc,
  4193. const struct intel_crtc_state *new_crtc_state)
  4194. {
  4195. struct drm_device *dev = crtc->dev;
  4196. struct drm_i915_private *dev_priv = to_i915(dev);
  4197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4198. int pipe = intel_crtc->pipe;
  4199. /*
  4200. * Gen2 reports pipe underruns whenever all planes are disabled.
  4201. * So don't enable underrun reporting before at least some planes
  4202. * are enabled.
  4203. * FIXME: Need to fix the logic to work when we turn off all planes
  4204. * but leave the pipe running.
  4205. */
  4206. if (IS_GEN2(dev_priv))
  4207. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4208. /* Underruns don't always raise interrupts, so check manually. */
  4209. intel_check_cpu_fifo_underruns(dev_priv);
  4210. intel_check_pch_fifo_underruns(dev_priv);
  4211. }
  4212. /* FIXME get rid of this and use pre_plane_update */
  4213. static void
  4214. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4215. {
  4216. struct drm_device *dev = crtc->dev;
  4217. struct drm_i915_private *dev_priv = to_i915(dev);
  4218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4219. int pipe = intel_crtc->pipe;
  4220. /*
  4221. * Gen2 reports pipe underruns whenever all planes are disabled.
  4222. * So disable underrun reporting before all the planes get disabled.
  4223. */
  4224. if (IS_GEN2(dev_priv))
  4225. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4226. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4227. /*
  4228. * Vblank time updates from the shadow to live plane control register
  4229. * are blocked if the memory self-refresh mode is active at that
  4230. * moment. So to make sure the plane gets truly disabled, disable
  4231. * first the self-refresh mode. The self-refresh enable bit in turn
  4232. * will be checked/applied by the HW only at the next frame start
  4233. * event which is after the vblank start event, so we need to have a
  4234. * wait-for-vblank between disabling the plane and the pipe.
  4235. */
  4236. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4237. intel_set_memory_cxsr(dev_priv, false))
  4238. intel_wait_for_vblank(dev_priv, pipe);
  4239. }
  4240. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4241. const struct intel_crtc_state *new_crtc_state)
  4242. {
  4243. if (!old_crtc_state->ips_enabled)
  4244. return false;
  4245. if (needs_modeset(&new_crtc_state->base))
  4246. return true;
  4247. return !new_crtc_state->ips_enabled;
  4248. }
  4249. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4250. const struct intel_crtc_state *new_crtc_state)
  4251. {
  4252. if (!new_crtc_state->ips_enabled)
  4253. return false;
  4254. if (needs_modeset(&new_crtc_state->base))
  4255. return true;
  4256. /*
  4257. * We can't read out IPS on broadwell, assume the worst and
  4258. * forcibly enable IPS on the first fastset.
  4259. */
  4260. if (new_crtc_state->update_pipe &&
  4261. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4262. return true;
  4263. return !old_crtc_state->ips_enabled;
  4264. }
  4265. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4266. {
  4267. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4268. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4269. struct intel_crtc_state *pipe_config =
  4270. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4271. crtc);
  4272. struct drm_plane *primary = crtc->base.primary;
  4273. struct drm_plane_state *old_pri_state =
  4274. drm_atomic_get_existing_plane_state(old_state, primary);
  4275. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4276. if (pipe_config->update_wm_post && pipe_config->base.active)
  4277. intel_update_watermarks(crtc);
  4278. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4279. hsw_enable_ips(pipe_config);
  4280. if (old_pri_state) {
  4281. struct intel_plane_state *primary_state =
  4282. intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
  4283. to_intel_plane(primary));
  4284. struct intel_plane_state *old_primary_state =
  4285. to_intel_plane_state(old_pri_state);
  4286. intel_fbc_post_update(crtc);
  4287. if (primary_state->base.visible &&
  4288. (needs_modeset(&pipe_config->base) ||
  4289. !old_primary_state->base.visible))
  4290. intel_post_enable_primary(&crtc->base, pipe_config);
  4291. }
  4292. }
  4293. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4294. struct intel_crtc_state *pipe_config)
  4295. {
  4296. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4297. struct drm_device *dev = crtc->base.dev;
  4298. struct drm_i915_private *dev_priv = to_i915(dev);
  4299. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4300. struct drm_plane *primary = crtc->base.primary;
  4301. struct drm_plane_state *old_pri_state =
  4302. drm_atomic_get_existing_plane_state(old_state, primary);
  4303. bool modeset = needs_modeset(&pipe_config->base);
  4304. struct intel_atomic_state *old_intel_state =
  4305. to_intel_atomic_state(old_state);
  4306. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4307. hsw_disable_ips(old_crtc_state);
  4308. if (old_pri_state) {
  4309. struct intel_plane_state *primary_state =
  4310. intel_atomic_get_new_plane_state(old_intel_state,
  4311. to_intel_plane(primary));
  4312. struct intel_plane_state *old_primary_state =
  4313. to_intel_plane_state(old_pri_state);
  4314. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4315. /*
  4316. * Gen2 reports pipe underruns whenever all planes are disabled.
  4317. * So disable underrun reporting before all the planes get disabled.
  4318. */
  4319. if (IS_GEN2(dev_priv) && old_primary_state->base.visible &&
  4320. (modeset || !primary_state->base.visible))
  4321. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4322. }
  4323. /*
  4324. * Vblank time updates from the shadow to live plane control register
  4325. * are blocked if the memory self-refresh mode is active at that
  4326. * moment. So to make sure the plane gets truly disabled, disable
  4327. * first the self-refresh mode. The self-refresh enable bit in turn
  4328. * will be checked/applied by the HW only at the next frame start
  4329. * event which is after the vblank start event, so we need to have a
  4330. * wait-for-vblank between disabling the plane and the pipe.
  4331. */
  4332. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4333. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4334. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4335. /*
  4336. * IVB workaround: must disable low power watermarks for at least
  4337. * one frame before enabling scaling. LP watermarks can be re-enabled
  4338. * when scaling is disabled.
  4339. *
  4340. * WaCxSRDisabledForSpriteScaling:ivb
  4341. */
  4342. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4343. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4344. /*
  4345. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4346. * watermark programming here.
  4347. */
  4348. if (needs_modeset(&pipe_config->base))
  4349. return;
  4350. /*
  4351. * For platforms that support atomic watermarks, program the
  4352. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4353. * will be the intermediate values that are safe for both pre- and
  4354. * post- vblank; when vblank happens, the 'active' values will be set
  4355. * to the final 'target' values and we'll do this again to get the
  4356. * optimal watermarks. For gen9+ platforms, the values we program here
  4357. * will be the final target values which will get automatically latched
  4358. * at vblank time; no further programming will be necessary.
  4359. *
  4360. * If a platform hasn't been transitioned to atomic watermarks yet,
  4361. * we'll continue to update watermarks the old way, if flags tell
  4362. * us to.
  4363. */
  4364. if (dev_priv->display.initial_watermarks != NULL)
  4365. dev_priv->display.initial_watermarks(old_intel_state,
  4366. pipe_config);
  4367. else if (pipe_config->update_wm_pre)
  4368. intel_update_watermarks(crtc);
  4369. }
  4370. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4371. {
  4372. struct drm_device *dev = crtc->dev;
  4373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4374. struct drm_plane *p;
  4375. int pipe = intel_crtc->pipe;
  4376. intel_crtc_dpms_overlay_disable(intel_crtc);
  4377. drm_for_each_plane_mask(p, dev, plane_mask)
  4378. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4379. /*
  4380. * FIXME: Once we grow proper nuclear flip support out of this we need
  4381. * to compute the mask of flip planes precisely. For the time being
  4382. * consider this a flip to a NULL plane.
  4383. */
  4384. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4385. }
  4386. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4387. struct intel_crtc_state *crtc_state,
  4388. struct drm_atomic_state *old_state)
  4389. {
  4390. struct drm_connector_state *conn_state;
  4391. struct drm_connector *conn;
  4392. int i;
  4393. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4394. struct intel_encoder *encoder =
  4395. to_intel_encoder(conn_state->best_encoder);
  4396. if (conn_state->crtc != crtc)
  4397. continue;
  4398. if (encoder->pre_pll_enable)
  4399. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4400. }
  4401. }
  4402. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4403. struct intel_crtc_state *crtc_state,
  4404. struct drm_atomic_state *old_state)
  4405. {
  4406. struct drm_connector_state *conn_state;
  4407. struct drm_connector *conn;
  4408. int i;
  4409. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4410. struct intel_encoder *encoder =
  4411. to_intel_encoder(conn_state->best_encoder);
  4412. if (conn_state->crtc != crtc)
  4413. continue;
  4414. if (encoder->pre_enable)
  4415. encoder->pre_enable(encoder, crtc_state, conn_state);
  4416. }
  4417. }
  4418. static void intel_encoders_enable(struct drm_crtc *crtc,
  4419. struct intel_crtc_state *crtc_state,
  4420. struct drm_atomic_state *old_state)
  4421. {
  4422. struct drm_connector_state *conn_state;
  4423. struct drm_connector *conn;
  4424. int i;
  4425. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4426. struct intel_encoder *encoder =
  4427. to_intel_encoder(conn_state->best_encoder);
  4428. if (conn_state->crtc != crtc)
  4429. continue;
  4430. encoder->enable(encoder, crtc_state, conn_state);
  4431. intel_opregion_notify_encoder(encoder, true);
  4432. }
  4433. }
  4434. static void intel_encoders_disable(struct drm_crtc *crtc,
  4435. struct intel_crtc_state *old_crtc_state,
  4436. struct drm_atomic_state *old_state)
  4437. {
  4438. struct drm_connector_state *old_conn_state;
  4439. struct drm_connector *conn;
  4440. int i;
  4441. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4442. struct intel_encoder *encoder =
  4443. to_intel_encoder(old_conn_state->best_encoder);
  4444. if (old_conn_state->crtc != crtc)
  4445. continue;
  4446. intel_opregion_notify_encoder(encoder, false);
  4447. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4448. }
  4449. }
  4450. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4451. struct intel_crtc_state *old_crtc_state,
  4452. struct drm_atomic_state *old_state)
  4453. {
  4454. struct drm_connector_state *old_conn_state;
  4455. struct drm_connector *conn;
  4456. int i;
  4457. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4458. struct intel_encoder *encoder =
  4459. to_intel_encoder(old_conn_state->best_encoder);
  4460. if (old_conn_state->crtc != crtc)
  4461. continue;
  4462. if (encoder->post_disable)
  4463. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4464. }
  4465. }
  4466. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4467. struct intel_crtc_state *old_crtc_state,
  4468. struct drm_atomic_state *old_state)
  4469. {
  4470. struct drm_connector_state *old_conn_state;
  4471. struct drm_connector *conn;
  4472. int i;
  4473. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4474. struct intel_encoder *encoder =
  4475. to_intel_encoder(old_conn_state->best_encoder);
  4476. if (old_conn_state->crtc != crtc)
  4477. continue;
  4478. if (encoder->post_pll_disable)
  4479. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4480. }
  4481. }
  4482. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4483. struct drm_atomic_state *old_state)
  4484. {
  4485. struct drm_crtc *crtc = pipe_config->base.crtc;
  4486. struct drm_device *dev = crtc->dev;
  4487. struct drm_i915_private *dev_priv = to_i915(dev);
  4488. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4489. int pipe = intel_crtc->pipe;
  4490. struct intel_atomic_state *old_intel_state =
  4491. to_intel_atomic_state(old_state);
  4492. if (WARN_ON(intel_crtc->active))
  4493. return;
  4494. /*
  4495. * Sometimes spurious CPU pipe underruns happen during FDI
  4496. * training, at least with VGA+HDMI cloning. Suppress them.
  4497. *
  4498. * On ILK we get an occasional spurious CPU pipe underruns
  4499. * between eDP port A enable and vdd enable. Also PCH port
  4500. * enable seems to result in the occasional CPU pipe underrun.
  4501. *
  4502. * Spurious PCH underruns also occur during PCH enabling.
  4503. */
  4504. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4505. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4506. if (intel_crtc->config->has_pch_encoder)
  4507. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4508. if (intel_crtc->config->has_pch_encoder)
  4509. intel_prepare_shared_dpll(intel_crtc);
  4510. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4511. intel_dp_set_m_n(intel_crtc, M1_N1);
  4512. intel_set_pipe_timings(intel_crtc);
  4513. intel_set_pipe_src_size(intel_crtc);
  4514. if (intel_crtc->config->has_pch_encoder) {
  4515. intel_cpu_transcoder_set_m_n(intel_crtc,
  4516. &intel_crtc->config->fdi_m_n, NULL);
  4517. }
  4518. ironlake_set_pipeconf(crtc);
  4519. intel_crtc->active = true;
  4520. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4521. if (intel_crtc->config->has_pch_encoder) {
  4522. /* Note: FDI PLL enabling _must_ be done before we enable the
  4523. * cpu pipes, hence this is separate from all the other fdi/pch
  4524. * enabling. */
  4525. ironlake_fdi_pll_enable(intel_crtc);
  4526. } else {
  4527. assert_fdi_tx_disabled(dev_priv, pipe);
  4528. assert_fdi_rx_disabled(dev_priv, pipe);
  4529. }
  4530. ironlake_pfit_enable(intel_crtc);
  4531. /*
  4532. * On ILK+ LUT must be loaded before the pipe is running but with
  4533. * clocks enabled
  4534. */
  4535. intel_color_load_luts(&pipe_config->base);
  4536. if (dev_priv->display.initial_watermarks != NULL)
  4537. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4538. intel_enable_pipe(pipe_config);
  4539. if (intel_crtc->config->has_pch_encoder)
  4540. ironlake_pch_enable(pipe_config);
  4541. assert_vblank_disabled(crtc);
  4542. drm_crtc_vblank_on(crtc);
  4543. intel_encoders_enable(crtc, pipe_config, old_state);
  4544. if (HAS_PCH_CPT(dev_priv))
  4545. cpt_verify_modeset(dev, intel_crtc->pipe);
  4546. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4547. if (intel_crtc->config->has_pch_encoder)
  4548. intel_wait_for_vblank(dev_priv, pipe);
  4549. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4550. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4551. }
  4552. /* IPS only exists on ULT machines and is tied to pipe A. */
  4553. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4554. {
  4555. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4556. }
  4557. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4558. enum pipe pipe, bool apply)
  4559. {
  4560. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4561. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4562. if (apply)
  4563. val |= mask;
  4564. else
  4565. val &= ~mask;
  4566. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4567. }
  4568. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4569. struct drm_atomic_state *old_state)
  4570. {
  4571. struct drm_crtc *crtc = pipe_config->base.crtc;
  4572. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4574. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4575. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4576. struct intel_atomic_state *old_intel_state =
  4577. to_intel_atomic_state(old_state);
  4578. bool psl_clkgate_wa;
  4579. if (WARN_ON(intel_crtc->active))
  4580. return;
  4581. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4582. if (intel_crtc->config->shared_dpll)
  4583. intel_enable_shared_dpll(intel_crtc);
  4584. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4585. intel_dp_set_m_n(intel_crtc, M1_N1);
  4586. if (!transcoder_is_dsi(cpu_transcoder))
  4587. intel_set_pipe_timings(intel_crtc);
  4588. intel_set_pipe_src_size(intel_crtc);
  4589. if (cpu_transcoder != TRANSCODER_EDP &&
  4590. !transcoder_is_dsi(cpu_transcoder)) {
  4591. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4592. intel_crtc->config->pixel_multiplier - 1);
  4593. }
  4594. if (intel_crtc->config->has_pch_encoder) {
  4595. intel_cpu_transcoder_set_m_n(intel_crtc,
  4596. &intel_crtc->config->fdi_m_n, NULL);
  4597. }
  4598. if (!transcoder_is_dsi(cpu_transcoder))
  4599. haswell_set_pipeconf(crtc);
  4600. haswell_set_pipemisc(crtc);
  4601. intel_color_set_csc(&pipe_config->base);
  4602. intel_crtc->active = true;
  4603. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4604. if (!transcoder_is_dsi(cpu_transcoder))
  4605. intel_ddi_enable_pipe_clock(pipe_config);
  4606. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4607. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4608. intel_crtc->config->pch_pfit.enabled;
  4609. if (psl_clkgate_wa)
  4610. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4611. if (INTEL_GEN(dev_priv) >= 9)
  4612. skylake_pfit_enable(intel_crtc);
  4613. else
  4614. ironlake_pfit_enable(intel_crtc);
  4615. /*
  4616. * On ILK+ LUT must be loaded before the pipe is running but with
  4617. * clocks enabled
  4618. */
  4619. intel_color_load_luts(&pipe_config->base);
  4620. intel_ddi_set_pipe_settings(pipe_config);
  4621. if (!transcoder_is_dsi(cpu_transcoder))
  4622. intel_ddi_enable_transcoder_func(pipe_config);
  4623. if (dev_priv->display.initial_watermarks != NULL)
  4624. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4625. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4626. if (!transcoder_is_dsi(cpu_transcoder))
  4627. intel_enable_pipe(pipe_config);
  4628. if (intel_crtc->config->has_pch_encoder)
  4629. lpt_pch_enable(pipe_config);
  4630. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4631. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4632. assert_vblank_disabled(crtc);
  4633. drm_crtc_vblank_on(crtc);
  4634. intel_encoders_enable(crtc, pipe_config, old_state);
  4635. if (psl_clkgate_wa) {
  4636. intel_wait_for_vblank(dev_priv, pipe);
  4637. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4638. }
  4639. /* If we change the relative order between pipe/planes enabling, we need
  4640. * to change the workaround. */
  4641. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4642. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4643. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4644. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4645. }
  4646. }
  4647. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4648. {
  4649. struct drm_device *dev = crtc->base.dev;
  4650. struct drm_i915_private *dev_priv = to_i915(dev);
  4651. int pipe = crtc->pipe;
  4652. /* To avoid upsetting the power well on haswell only disable the pfit if
  4653. * it's in use. The hw state code will make sure we get this right. */
  4654. if (force || crtc->config->pch_pfit.enabled) {
  4655. I915_WRITE(PF_CTL(pipe), 0);
  4656. I915_WRITE(PF_WIN_POS(pipe), 0);
  4657. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4658. }
  4659. }
  4660. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4661. struct drm_atomic_state *old_state)
  4662. {
  4663. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4664. struct drm_device *dev = crtc->dev;
  4665. struct drm_i915_private *dev_priv = to_i915(dev);
  4666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4667. int pipe = intel_crtc->pipe;
  4668. /*
  4669. * Sometimes spurious CPU pipe underruns happen when the
  4670. * pipe is already disabled, but FDI RX/TX is still enabled.
  4671. * Happens at least with VGA+HDMI cloning. Suppress them.
  4672. */
  4673. if (intel_crtc->config->has_pch_encoder) {
  4674. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4675. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4676. }
  4677. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4678. drm_crtc_vblank_off(crtc);
  4679. assert_vblank_disabled(crtc);
  4680. intel_disable_pipe(old_crtc_state);
  4681. ironlake_pfit_disable(intel_crtc, false);
  4682. if (intel_crtc->config->has_pch_encoder)
  4683. ironlake_fdi_disable(crtc);
  4684. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4685. if (intel_crtc->config->has_pch_encoder) {
  4686. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4687. if (HAS_PCH_CPT(dev_priv)) {
  4688. i915_reg_t reg;
  4689. u32 temp;
  4690. /* disable TRANS_DP_CTL */
  4691. reg = TRANS_DP_CTL(pipe);
  4692. temp = I915_READ(reg);
  4693. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4694. TRANS_DP_PORT_SEL_MASK);
  4695. temp |= TRANS_DP_PORT_SEL_NONE;
  4696. I915_WRITE(reg, temp);
  4697. /* disable DPLL_SEL */
  4698. temp = I915_READ(PCH_DPLL_SEL);
  4699. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4700. I915_WRITE(PCH_DPLL_SEL, temp);
  4701. }
  4702. ironlake_fdi_pll_disable(intel_crtc);
  4703. }
  4704. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4705. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4706. }
  4707. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4708. struct drm_atomic_state *old_state)
  4709. {
  4710. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4711. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4713. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4714. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4715. drm_crtc_vblank_off(crtc);
  4716. assert_vblank_disabled(crtc);
  4717. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4718. if (!transcoder_is_dsi(cpu_transcoder))
  4719. intel_disable_pipe(old_crtc_state);
  4720. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4721. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4722. if (!transcoder_is_dsi(cpu_transcoder))
  4723. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4724. if (INTEL_GEN(dev_priv) >= 9)
  4725. skylake_scaler_disable(intel_crtc);
  4726. else
  4727. ironlake_pfit_disable(intel_crtc, false);
  4728. if (!transcoder_is_dsi(cpu_transcoder))
  4729. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4730. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4731. }
  4732. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4733. {
  4734. struct drm_device *dev = crtc->base.dev;
  4735. struct drm_i915_private *dev_priv = to_i915(dev);
  4736. struct intel_crtc_state *pipe_config = crtc->config;
  4737. if (!pipe_config->gmch_pfit.control)
  4738. return;
  4739. /*
  4740. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4741. * according to register description and PRM.
  4742. */
  4743. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4744. assert_pipe_disabled(dev_priv, crtc->pipe);
  4745. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4746. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4747. /* Border color in case we don't scale up to the full screen. Black by
  4748. * default, change to something else for debugging. */
  4749. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4750. }
  4751. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4752. {
  4753. switch (port) {
  4754. case PORT_A:
  4755. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4756. case PORT_B:
  4757. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4758. case PORT_C:
  4759. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4760. case PORT_D:
  4761. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4762. case PORT_E:
  4763. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4764. default:
  4765. MISSING_CASE(port);
  4766. return POWER_DOMAIN_PORT_OTHER;
  4767. }
  4768. }
  4769. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4770. struct intel_crtc_state *crtc_state)
  4771. {
  4772. struct drm_device *dev = crtc->dev;
  4773. struct drm_i915_private *dev_priv = to_i915(dev);
  4774. struct drm_encoder *encoder;
  4775. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4776. enum pipe pipe = intel_crtc->pipe;
  4777. u64 mask;
  4778. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4779. if (!crtc_state->base.active)
  4780. return 0;
  4781. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4782. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4783. if (crtc_state->pch_pfit.enabled ||
  4784. crtc_state->pch_pfit.force_thru)
  4785. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4786. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4787. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4788. mask |= BIT_ULL(intel_encoder->power_domain);
  4789. }
  4790. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4791. mask |= BIT(POWER_DOMAIN_AUDIO);
  4792. if (crtc_state->shared_dpll)
  4793. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4794. return mask;
  4795. }
  4796. static u64
  4797. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4798. struct intel_crtc_state *crtc_state)
  4799. {
  4800. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4801. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4802. enum intel_display_power_domain domain;
  4803. u64 domains, new_domains, old_domains;
  4804. old_domains = intel_crtc->enabled_power_domains;
  4805. intel_crtc->enabled_power_domains = new_domains =
  4806. get_crtc_power_domains(crtc, crtc_state);
  4807. domains = new_domains & ~old_domains;
  4808. for_each_power_domain(domain, domains)
  4809. intel_display_power_get(dev_priv, domain);
  4810. return old_domains & ~new_domains;
  4811. }
  4812. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4813. u64 domains)
  4814. {
  4815. enum intel_display_power_domain domain;
  4816. for_each_power_domain(domain, domains)
  4817. intel_display_power_put(dev_priv, domain);
  4818. }
  4819. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4820. struct drm_atomic_state *old_state)
  4821. {
  4822. struct intel_atomic_state *old_intel_state =
  4823. to_intel_atomic_state(old_state);
  4824. struct drm_crtc *crtc = pipe_config->base.crtc;
  4825. struct drm_device *dev = crtc->dev;
  4826. struct drm_i915_private *dev_priv = to_i915(dev);
  4827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4828. int pipe = intel_crtc->pipe;
  4829. if (WARN_ON(intel_crtc->active))
  4830. return;
  4831. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4832. intel_dp_set_m_n(intel_crtc, M1_N1);
  4833. intel_set_pipe_timings(intel_crtc);
  4834. intel_set_pipe_src_size(intel_crtc);
  4835. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4836. struct drm_i915_private *dev_priv = to_i915(dev);
  4837. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4838. I915_WRITE(CHV_CANVAS(pipe), 0);
  4839. }
  4840. i9xx_set_pipeconf(intel_crtc);
  4841. intel_crtc->active = true;
  4842. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4843. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4844. if (IS_CHERRYVIEW(dev_priv)) {
  4845. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4846. chv_enable_pll(intel_crtc, intel_crtc->config);
  4847. } else {
  4848. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4849. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4850. }
  4851. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4852. i9xx_pfit_enable(intel_crtc);
  4853. intel_color_load_luts(&pipe_config->base);
  4854. dev_priv->display.initial_watermarks(old_intel_state,
  4855. pipe_config);
  4856. intel_enable_pipe(pipe_config);
  4857. assert_vblank_disabled(crtc);
  4858. drm_crtc_vblank_on(crtc);
  4859. intel_encoders_enable(crtc, pipe_config, old_state);
  4860. }
  4861. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4862. {
  4863. struct drm_device *dev = crtc->base.dev;
  4864. struct drm_i915_private *dev_priv = to_i915(dev);
  4865. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4866. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4867. }
  4868. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4869. struct drm_atomic_state *old_state)
  4870. {
  4871. struct intel_atomic_state *old_intel_state =
  4872. to_intel_atomic_state(old_state);
  4873. struct drm_crtc *crtc = pipe_config->base.crtc;
  4874. struct drm_device *dev = crtc->dev;
  4875. struct drm_i915_private *dev_priv = to_i915(dev);
  4876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4877. enum pipe pipe = intel_crtc->pipe;
  4878. if (WARN_ON(intel_crtc->active))
  4879. return;
  4880. i9xx_set_pll_dividers(intel_crtc);
  4881. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4882. intel_dp_set_m_n(intel_crtc, M1_N1);
  4883. intel_set_pipe_timings(intel_crtc);
  4884. intel_set_pipe_src_size(intel_crtc);
  4885. i9xx_set_pipeconf(intel_crtc);
  4886. intel_crtc->active = true;
  4887. if (!IS_GEN2(dev_priv))
  4888. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4889. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4890. i9xx_enable_pll(intel_crtc, pipe_config);
  4891. i9xx_pfit_enable(intel_crtc);
  4892. intel_color_load_luts(&pipe_config->base);
  4893. if (dev_priv->display.initial_watermarks != NULL)
  4894. dev_priv->display.initial_watermarks(old_intel_state,
  4895. intel_crtc->config);
  4896. else
  4897. intel_update_watermarks(intel_crtc);
  4898. intel_enable_pipe(pipe_config);
  4899. assert_vblank_disabled(crtc);
  4900. drm_crtc_vblank_on(crtc);
  4901. intel_encoders_enable(crtc, pipe_config, old_state);
  4902. }
  4903. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4904. {
  4905. struct drm_device *dev = crtc->base.dev;
  4906. struct drm_i915_private *dev_priv = to_i915(dev);
  4907. if (!crtc->config->gmch_pfit.control)
  4908. return;
  4909. assert_pipe_disabled(dev_priv, crtc->pipe);
  4910. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4911. I915_READ(PFIT_CONTROL));
  4912. I915_WRITE(PFIT_CONTROL, 0);
  4913. }
  4914. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4915. struct drm_atomic_state *old_state)
  4916. {
  4917. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4918. struct drm_device *dev = crtc->dev;
  4919. struct drm_i915_private *dev_priv = to_i915(dev);
  4920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4921. int pipe = intel_crtc->pipe;
  4922. /*
  4923. * On gen2 planes are double buffered but the pipe isn't, so we must
  4924. * wait for planes to fully turn off before disabling the pipe.
  4925. */
  4926. if (IS_GEN2(dev_priv))
  4927. intel_wait_for_vblank(dev_priv, pipe);
  4928. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4929. drm_crtc_vblank_off(crtc);
  4930. assert_vblank_disabled(crtc);
  4931. intel_disable_pipe(old_crtc_state);
  4932. i9xx_pfit_disable(intel_crtc);
  4933. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4934. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4935. if (IS_CHERRYVIEW(dev_priv))
  4936. chv_disable_pll(dev_priv, pipe);
  4937. else if (IS_VALLEYVIEW(dev_priv))
  4938. vlv_disable_pll(dev_priv, pipe);
  4939. else
  4940. i9xx_disable_pll(intel_crtc);
  4941. }
  4942. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4943. if (!IS_GEN2(dev_priv))
  4944. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4945. if (!dev_priv->display.initial_watermarks)
  4946. intel_update_watermarks(intel_crtc);
  4947. /* clock the pipe down to 640x480@60 to potentially save power */
  4948. if (IS_I830(dev_priv))
  4949. i830_enable_pipe(dev_priv, pipe);
  4950. }
  4951. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  4952. struct drm_modeset_acquire_ctx *ctx)
  4953. {
  4954. struct intel_encoder *encoder;
  4955. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4956. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4957. enum intel_display_power_domain domain;
  4958. struct intel_plane *plane;
  4959. u64 domains;
  4960. struct drm_atomic_state *state;
  4961. struct intel_crtc_state *crtc_state;
  4962. int ret;
  4963. if (!intel_crtc->active)
  4964. return;
  4965. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  4966. const struct intel_plane_state *plane_state =
  4967. to_intel_plane_state(plane->base.state);
  4968. if (plane_state->base.visible)
  4969. intel_plane_disable_noatomic(intel_crtc, plane);
  4970. }
  4971. state = drm_atomic_state_alloc(crtc->dev);
  4972. if (!state) {
  4973. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4974. crtc->base.id, crtc->name);
  4975. return;
  4976. }
  4977. state->acquire_ctx = ctx;
  4978. /* Everything's already locked, -EDEADLK can't happen. */
  4979. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4980. ret = drm_atomic_add_affected_connectors(state, crtc);
  4981. WARN_ON(IS_ERR(crtc_state) || ret);
  4982. dev_priv->display.crtc_disable(crtc_state, state);
  4983. drm_atomic_state_put(state);
  4984. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4985. crtc->base.id, crtc->name);
  4986. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4987. crtc->state->active = false;
  4988. intel_crtc->active = false;
  4989. crtc->enabled = false;
  4990. crtc->state->connector_mask = 0;
  4991. crtc->state->encoder_mask = 0;
  4992. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4993. encoder->base.crtc = NULL;
  4994. intel_fbc_disable(intel_crtc);
  4995. intel_update_watermarks(intel_crtc);
  4996. intel_disable_shared_dpll(intel_crtc);
  4997. domains = intel_crtc->enabled_power_domains;
  4998. for_each_power_domain(domain, domains)
  4999. intel_display_power_put(dev_priv, domain);
  5000. intel_crtc->enabled_power_domains = 0;
  5001. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5002. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5003. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5004. }
  5005. /*
  5006. * turn all crtc's off, but do not adjust state
  5007. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5008. */
  5009. int intel_display_suspend(struct drm_device *dev)
  5010. {
  5011. struct drm_i915_private *dev_priv = to_i915(dev);
  5012. struct drm_atomic_state *state;
  5013. int ret;
  5014. state = drm_atomic_helper_suspend(dev);
  5015. ret = PTR_ERR_OR_ZERO(state);
  5016. if (ret)
  5017. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5018. else
  5019. dev_priv->modeset_restore_state = state;
  5020. return ret;
  5021. }
  5022. void intel_encoder_destroy(struct drm_encoder *encoder)
  5023. {
  5024. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5025. drm_encoder_cleanup(encoder);
  5026. kfree(intel_encoder);
  5027. }
  5028. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5029. * internal consistency). */
  5030. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5031. struct drm_connector_state *conn_state)
  5032. {
  5033. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5034. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5035. connector->base.base.id,
  5036. connector->base.name);
  5037. if (connector->get_hw_state(connector)) {
  5038. struct intel_encoder *encoder = connector->encoder;
  5039. I915_STATE_WARN(!crtc_state,
  5040. "connector enabled without attached crtc\n");
  5041. if (!crtc_state)
  5042. return;
  5043. I915_STATE_WARN(!crtc_state->active,
  5044. "connector is active, but attached crtc isn't\n");
  5045. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5046. return;
  5047. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5048. "atomic encoder doesn't match attached encoder\n");
  5049. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5050. "attached encoder crtc differs from connector crtc\n");
  5051. } else {
  5052. I915_STATE_WARN(crtc_state && crtc_state->active,
  5053. "attached crtc is active, but connector isn't\n");
  5054. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5055. "best encoder set without crtc!\n");
  5056. }
  5057. }
  5058. int intel_connector_init(struct intel_connector *connector)
  5059. {
  5060. struct intel_digital_connector_state *conn_state;
  5061. /*
  5062. * Allocate enough memory to hold intel_digital_connector_state,
  5063. * This might be a few bytes too many, but for connectors that don't
  5064. * need it we'll free the state and allocate a smaller one on the first
  5065. * succesful commit anyway.
  5066. */
  5067. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5068. if (!conn_state)
  5069. return -ENOMEM;
  5070. __drm_atomic_helper_connector_reset(&connector->base,
  5071. &conn_state->base);
  5072. return 0;
  5073. }
  5074. struct intel_connector *intel_connector_alloc(void)
  5075. {
  5076. struct intel_connector *connector;
  5077. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5078. if (!connector)
  5079. return NULL;
  5080. if (intel_connector_init(connector) < 0) {
  5081. kfree(connector);
  5082. return NULL;
  5083. }
  5084. return connector;
  5085. }
  5086. /*
  5087. * Free the bits allocated by intel_connector_alloc.
  5088. * This should only be used after intel_connector_alloc has returned
  5089. * successfully, and before drm_connector_init returns successfully.
  5090. * Otherwise the destroy callbacks for the connector and the state should
  5091. * take care of proper cleanup/free
  5092. */
  5093. void intel_connector_free(struct intel_connector *connector)
  5094. {
  5095. kfree(to_intel_digital_connector_state(connector->base.state));
  5096. kfree(connector);
  5097. }
  5098. /* Simple connector->get_hw_state implementation for encoders that support only
  5099. * one connector and no cloning and hence the encoder state determines the state
  5100. * of the connector. */
  5101. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5102. {
  5103. enum pipe pipe = 0;
  5104. struct intel_encoder *encoder = connector->encoder;
  5105. return encoder->get_hw_state(encoder, &pipe);
  5106. }
  5107. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5108. {
  5109. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5110. return crtc_state->fdi_lanes;
  5111. return 0;
  5112. }
  5113. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5114. struct intel_crtc_state *pipe_config)
  5115. {
  5116. struct drm_i915_private *dev_priv = to_i915(dev);
  5117. struct drm_atomic_state *state = pipe_config->base.state;
  5118. struct intel_crtc *other_crtc;
  5119. struct intel_crtc_state *other_crtc_state;
  5120. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5121. pipe_name(pipe), pipe_config->fdi_lanes);
  5122. if (pipe_config->fdi_lanes > 4) {
  5123. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5124. pipe_name(pipe), pipe_config->fdi_lanes);
  5125. return -EINVAL;
  5126. }
  5127. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5128. if (pipe_config->fdi_lanes > 2) {
  5129. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5130. pipe_config->fdi_lanes);
  5131. return -EINVAL;
  5132. } else {
  5133. return 0;
  5134. }
  5135. }
  5136. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5137. return 0;
  5138. /* Ivybridge 3 pipe is really complicated */
  5139. switch (pipe) {
  5140. case PIPE_A:
  5141. return 0;
  5142. case PIPE_B:
  5143. if (pipe_config->fdi_lanes <= 2)
  5144. return 0;
  5145. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5146. other_crtc_state =
  5147. intel_atomic_get_crtc_state(state, other_crtc);
  5148. if (IS_ERR(other_crtc_state))
  5149. return PTR_ERR(other_crtc_state);
  5150. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5151. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5152. pipe_name(pipe), pipe_config->fdi_lanes);
  5153. return -EINVAL;
  5154. }
  5155. return 0;
  5156. case PIPE_C:
  5157. if (pipe_config->fdi_lanes > 2) {
  5158. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5159. pipe_name(pipe), pipe_config->fdi_lanes);
  5160. return -EINVAL;
  5161. }
  5162. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5163. other_crtc_state =
  5164. intel_atomic_get_crtc_state(state, other_crtc);
  5165. if (IS_ERR(other_crtc_state))
  5166. return PTR_ERR(other_crtc_state);
  5167. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5168. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5169. return -EINVAL;
  5170. }
  5171. return 0;
  5172. default:
  5173. BUG();
  5174. }
  5175. }
  5176. #define RETRY 1
  5177. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5178. struct intel_crtc_state *pipe_config)
  5179. {
  5180. struct drm_device *dev = intel_crtc->base.dev;
  5181. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5182. int lane, link_bw, fdi_dotclock, ret;
  5183. bool needs_recompute = false;
  5184. retry:
  5185. /* FDI is a binary signal running at ~2.7GHz, encoding
  5186. * each output octet as 10 bits. The actual frequency
  5187. * is stored as a divider into a 100MHz clock, and the
  5188. * mode pixel clock is stored in units of 1KHz.
  5189. * Hence the bw of each lane in terms of the mode signal
  5190. * is:
  5191. */
  5192. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5193. fdi_dotclock = adjusted_mode->crtc_clock;
  5194. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5195. pipe_config->pipe_bpp);
  5196. pipe_config->fdi_lanes = lane;
  5197. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5198. link_bw, &pipe_config->fdi_m_n, false);
  5199. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5200. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5201. pipe_config->pipe_bpp -= 2*3;
  5202. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5203. pipe_config->pipe_bpp);
  5204. needs_recompute = true;
  5205. pipe_config->bw_constrained = true;
  5206. goto retry;
  5207. }
  5208. if (needs_recompute)
  5209. return RETRY;
  5210. return ret;
  5211. }
  5212. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5213. {
  5214. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5215. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5216. /* IPS only exists on ULT machines and is tied to pipe A. */
  5217. if (!hsw_crtc_supports_ips(crtc))
  5218. return false;
  5219. if (!i915_modparams.enable_ips)
  5220. return false;
  5221. if (crtc_state->pipe_bpp > 24)
  5222. return false;
  5223. /*
  5224. * We compare against max which means we must take
  5225. * the increased cdclk requirement into account when
  5226. * calculating the new cdclk.
  5227. *
  5228. * Should measure whether using a lower cdclk w/o IPS
  5229. */
  5230. if (IS_BROADWELL(dev_priv) &&
  5231. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5232. return false;
  5233. return true;
  5234. }
  5235. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5236. {
  5237. struct drm_i915_private *dev_priv =
  5238. to_i915(crtc_state->base.crtc->dev);
  5239. struct intel_atomic_state *intel_state =
  5240. to_intel_atomic_state(crtc_state->base.state);
  5241. if (!hsw_crtc_state_ips_capable(crtc_state))
  5242. return false;
  5243. if (crtc_state->ips_force_disable)
  5244. return false;
  5245. /* IPS should be fine as long as at least one plane is enabled. */
  5246. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5247. return false;
  5248. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5249. if (IS_BROADWELL(dev_priv) &&
  5250. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5251. return false;
  5252. return true;
  5253. }
  5254. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5255. {
  5256. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5257. /* GDG double wide on either pipe, otherwise pipe A only */
  5258. return INTEL_INFO(dev_priv)->gen < 4 &&
  5259. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5260. }
  5261. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5262. {
  5263. uint32_t pixel_rate;
  5264. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5265. /*
  5266. * We only use IF-ID interlacing. If we ever use
  5267. * PF-ID we'll need to adjust the pixel_rate here.
  5268. */
  5269. if (pipe_config->pch_pfit.enabled) {
  5270. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5271. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5272. pipe_w = pipe_config->pipe_src_w;
  5273. pipe_h = pipe_config->pipe_src_h;
  5274. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5275. pfit_h = pfit_size & 0xFFFF;
  5276. if (pipe_w < pfit_w)
  5277. pipe_w = pfit_w;
  5278. if (pipe_h < pfit_h)
  5279. pipe_h = pfit_h;
  5280. if (WARN_ON(!pfit_w || !pfit_h))
  5281. return pixel_rate;
  5282. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5283. pfit_w * pfit_h);
  5284. }
  5285. return pixel_rate;
  5286. }
  5287. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5288. {
  5289. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5290. if (HAS_GMCH_DISPLAY(dev_priv))
  5291. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5292. crtc_state->pixel_rate =
  5293. crtc_state->base.adjusted_mode.crtc_clock;
  5294. else
  5295. crtc_state->pixel_rate =
  5296. ilk_pipe_pixel_rate(crtc_state);
  5297. }
  5298. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5299. struct intel_crtc_state *pipe_config)
  5300. {
  5301. struct drm_device *dev = crtc->base.dev;
  5302. struct drm_i915_private *dev_priv = to_i915(dev);
  5303. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5304. int clock_limit = dev_priv->max_dotclk_freq;
  5305. if (INTEL_GEN(dev_priv) < 4) {
  5306. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5307. /*
  5308. * Enable double wide mode when the dot clock
  5309. * is > 90% of the (display) core speed.
  5310. */
  5311. if (intel_crtc_supports_double_wide(crtc) &&
  5312. adjusted_mode->crtc_clock > clock_limit) {
  5313. clock_limit = dev_priv->max_dotclk_freq;
  5314. pipe_config->double_wide = true;
  5315. }
  5316. }
  5317. if (adjusted_mode->crtc_clock > clock_limit) {
  5318. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5319. adjusted_mode->crtc_clock, clock_limit,
  5320. yesno(pipe_config->double_wide));
  5321. return -EINVAL;
  5322. }
  5323. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5324. /*
  5325. * There is only one pipe CSC unit per pipe, and we need that
  5326. * for output conversion from RGB->YCBCR. So if CTM is already
  5327. * applied we can't support YCBCR420 output.
  5328. */
  5329. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5330. return -EINVAL;
  5331. }
  5332. /*
  5333. * Pipe horizontal size must be even in:
  5334. * - DVO ganged mode
  5335. * - LVDS dual channel mode
  5336. * - Double wide pipe
  5337. */
  5338. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5339. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5340. pipe_config->pipe_src_w &= ~1;
  5341. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5342. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5343. */
  5344. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5345. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5346. return -EINVAL;
  5347. intel_crtc_compute_pixel_rate(pipe_config);
  5348. if (pipe_config->has_pch_encoder)
  5349. return ironlake_fdi_compute_config(crtc, pipe_config);
  5350. return 0;
  5351. }
  5352. static void
  5353. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5354. {
  5355. while (*num > DATA_LINK_M_N_MASK ||
  5356. *den > DATA_LINK_M_N_MASK) {
  5357. *num >>= 1;
  5358. *den >>= 1;
  5359. }
  5360. }
  5361. static void compute_m_n(unsigned int m, unsigned int n,
  5362. uint32_t *ret_m, uint32_t *ret_n,
  5363. bool reduce_m_n)
  5364. {
  5365. /*
  5366. * Reduce M/N as much as possible without loss in precision. Several DP
  5367. * dongles in particular seem to be fussy about too large *link* M/N
  5368. * values. The passed in values are more likely to have the least
  5369. * significant bits zero than M after rounding below, so do this first.
  5370. */
  5371. if (reduce_m_n) {
  5372. while ((m & 1) == 0 && (n & 1) == 0) {
  5373. m >>= 1;
  5374. n >>= 1;
  5375. }
  5376. }
  5377. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5378. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5379. intel_reduce_m_n_ratio(ret_m, ret_n);
  5380. }
  5381. void
  5382. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5383. int pixel_clock, int link_clock,
  5384. struct intel_link_m_n *m_n,
  5385. bool reduce_m_n)
  5386. {
  5387. m_n->tu = 64;
  5388. compute_m_n(bits_per_pixel * pixel_clock,
  5389. link_clock * nlanes * 8,
  5390. &m_n->gmch_m, &m_n->gmch_n,
  5391. reduce_m_n);
  5392. compute_m_n(pixel_clock, link_clock,
  5393. &m_n->link_m, &m_n->link_n,
  5394. reduce_m_n);
  5395. }
  5396. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5397. {
  5398. if (i915_modparams.panel_use_ssc >= 0)
  5399. return i915_modparams.panel_use_ssc != 0;
  5400. return dev_priv->vbt.lvds_use_ssc
  5401. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5402. }
  5403. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5404. {
  5405. return (1 << dpll->n) << 16 | dpll->m2;
  5406. }
  5407. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5408. {
  5409. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5410. }
  5411. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5412. struct intel_crtc_state *crtc_state,
  5413. struct dpll *reduced_clock)
  5414. {
  5415. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5416. u32 fp, fp2 = 0;
  5417. if (IS_PINEVIEW(dev_priv)) {
  5418. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5419. if (reduced_clock)
  5420. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5421. } else {
  5422. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5423. if (reduced_clock)
  5424. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5425. }
  5426. crtc_state->dpll_hw_state.fp0 = fp;
  5427. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5428. reduced_clock) {
  5429. crtc_state->dpll_hw_state.fp1 = fp2;
  5430. } else {
  5431. crtc_state->dpll_hw_state.fp1 = fp;
  5432. }
  5433. }
  5434. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5435. pipe)
  5436. {
  5437. u32 reg_val;
  5438. /*
  5439. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5440. * and set it to a reasonable value instead.
  5441. */
  5442. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5443. reg_val &= 0xffffff00;
  5444. reg_val |= 0x00000030;
  5445. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5446. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5447. reg_val &= 0x00ffffff;
  5448. reg_val |= 0x8c000000;
  5449. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5450. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5451. reg_val &= 0xffffff00;
  5452. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5453. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5454. reg_val &= 0x00ffffff;
  5455. reg_val |= 0xb0000000;
  5456. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5457. }
  5458. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5459. struct intel_link_m_n *m_n)
  5460. {
  5461. struct drm_device *dev = crtc->base.dev;
  5462. struct drm_i915_private *dev_priv = to_i915(dev);
  5463. int pipe = crtc->pipe;
  5464. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5465. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5466. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5467. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5468. }
  5469. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5470. struct intel_link_m_n *m_n,
  5471. struct intel_link_m_n *m2_n2)
  5472. {
  5473. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5474. int pipe = crtc->pipe;
  5475. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5476. if (INTEL_GEN(dev_priv) >= 5) {
  5477. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5478. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5479. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5480. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5481. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5482. * for gen < 8) and if DRRS is supported (to make sure the
  5483. * registers are not unnecessarily accessed).
  5484. */
  5485. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5486. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5487. I915_WRITE(PIPE_DATA_M2(transcoder),
  5488. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5489. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5490. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5491. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5492. }
  5493. } else {
  5494. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5495. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5496. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5497. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5498. }
  5499. }
  5500. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5501. {
  5502. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5503. if (m_n == M1_N1) {
  5504. dp_m_n = &crtc->config->dp_m_n;
  5505. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5506. } else if (m_n == M2_N2) {
  5507. /*
  5508. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5509. * needs to be programmed into M1_N1.
  5510. */
  5511. dp_m_n = &crtc->config->dp_m2_n2;
  5512. } else {
  5513. DRM_ERROR("Unsupported divider value\n");
  5514. return;
  5515. }
  5516. if (crtc->config->has_pch_encoder)
  5517. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5518. else
  5519. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5520. }
  5521. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5522. struct intel_crtc_state *pipe_config)
  5523. {
  5524. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5525. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5526. if (crtc->pipe != PIPE_A)
  5527. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5528. /* DPLL not used with DSI, but still need the rest set up */
  5529. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5530. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5531. DPLL_EXT_BUFFER_ENABLE_VLV;
  5532. pipe_config->dpll_hw_state.dpll_md =
  5533. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5534. }
  5535. static void chv_compute_dpll(struct intel_crtc *crtc,
  5536. struct intel_crtc_state *pipe_config)
  5537. {
  5538. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5539. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5540. if (crtc->pipe != PIPE_A)
  5541. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5542. /* DPLL not used with DSI, but still need the rest set up */
  5543. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5544. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5545. pipe_config->dpll_hw_state.dpll_md =
  5546. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5547. }
  5548. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5549. const struct intel_crtc_state *pipe_config)
  5550. {
  5551. struct drm_device *dev = crtc->base.dev;
  5552. struct drm_i915_private *dev_priv = to_i915(dev);
  5553. enum pipe pipe = crtc->pipe;
  5554. u32 mdiv;
  5555. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5556. u32 coreclk, reg_val;
  5557. /* Enable Refclk */
  5558. I915_WRITE(DPLL(pipe),
  5559. pipe_config->dpll_hw_state.dpll &
  5560. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5561. /* No need to actually set up the DPLL with DSI */
  5562. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5563. return;
  5564. mutex_lock(&dev_priv->sb_lock);
  5565. bestn = pipe_config->dpll.n;
  5566. bestm1 = pipe_config->dpll.m1;
  5567. bestm2 = pipe_config->dpll.m2;
  5568. bestp1 = pipe_config->dpll.p1;
  5569. bestp2 = pipe_config->dpll.p2;
  5570. /* See eDP HDMI DPIO driver vbios notes doc */
  5571. /* PLL B needs special handling */
  5572. if (pipe == PIPE_B)
  5573. vlv_pllb_recal_opamp(dev_priv, pipe);
  5574. /* Set up Tx target for periodic Rcomp update */
  5575. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5576. /* Disable target IRef on PLL */
  5577. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5578. reg_val &= 0x00ffffff;
  5579. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5580. /* Disable fast lock */
  5581. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5582. /* Set idtafcrecal before PLL is enabled */
  5583. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5584. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5585. mdiv |= ((bestn << DPIO_N_SHIFT));
  5586. mdiv |= (1 << DPIO_K_SHIFT);
  5587. /*
  5588. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5589. * but we don't support that).
  5590. * Note: don't use the DAC post divider as it seems unstable.
  5591. */
  5592. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5593. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5594. mdiv |= DPIO_ENABLE_CALIBRATION;
  5595. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5596. /* Set HBR and RBR LPF coefficients */
  5597. if (pipe_config->port_clock == 162000 ||
  5598. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5599. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5600. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5601. 0x009f0003);
  5602. else
  5603. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5604. 0x00d0000f);
  5605. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5606. /* Use SSC source */
  5607. if (pipe == PIPE_A)
  5608. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5609. 0x0df40000);
  5610. else
  5611. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5612. 0x0df70000);
  5613. } else { /* HDMI or VGA */
  5614. /* Use bend source */
  5615. if (pipe == PIPE_A)
  5616. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5617. 0x0df70000);
  5618. else
  5619. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5620. 0x0df40000);
  5621. }
  5622. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5623. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5624. if (intel_crtc_has_dp_encoder(crtc->config))
  5625. coreclk |= 0x01000000;
  5626. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5627. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5628. mutex_unlock(&dev_priv->sb_lock);
  5629. }
  5630. static void chv_prepare_pll(struct intel_crtc *crtc,
  5631. const struct intel_crtc_state *pipe_config)
  5632. {
  5633. struct drm_device *dev = crtc->base.dev;
  5634. struct drm_i915_private *dev_priv = to_i915(dev);
  5635. enum pipe pipe = crtc->pipe;
  5636. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5637. u32 loopfilter, tribuf_calcntr;
  5638. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5639. u32 dpio_val;
  5640. int vco;
  5641. /* Enable Refclk and SSC */
  5642. I915_WRITE(DPLL(pipe),
  5643. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5644. /* No need to actually set up the DPLL with DSI */
  5645. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5646. return;
  5647. bestn = pipe_config->dpll.n;
  5648. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5649. bestm1 = pipe_config->dpll.m1;
  5650. bestm2 = pipe_config->dpll.m2 >> 22;
  5651. bestp1 = pipe_config->dpll.p1;
  5652. bestp2 = pipe_config->dpll.p2;
  5653. vco = pipe_config->dpll.vco;
  5654. dpio_val = 0;
  5655. loopfilter = 0;
  5656. mutex_lock(&dev_priv->sb_lock);
  5657. /* p1 and p2 divider */
  5658. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5659. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5660. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5661. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5662. 1 << DPIO_CHV_K_DIV_SHIFT);
  5663. /* Feedback post-divider - m2 */
  5664. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5665. /* Feedback refclk divider - n and m1 */
  5666. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5667. DPIO_CHV_M1_DIV_BY_2 |
  5668. 1 << DPIO_CHV_N_DIV_SHIFT);
  5669. /* M2 fraction division */
  5670. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5671. /* M2 fraction division enable */
  5672. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5673. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5674. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5675. if (bestm2_frac)
  5676. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5677. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5678. /* Program digital lock detect threshold */
  5679. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5680. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5681. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5682. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5683. if (!bestm2_frac)
  5684. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5685. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5686. /* Loop filter */
  5687. if (vco == 5400000) {
  5688. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5689. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5690. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5691. tribuf_calcntr = 0x9;
  5692. } else if (vco <= 6200000) {
  5693. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5694. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5695. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5696. tribuf_calcntr = 0x9;
  5697. } else if (vco <= 6480000) {
  5698. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5699. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5700. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5701. tribuf_calcntr = 0x8;
  5702. } else {
  5703. /* Not supported. Apply the same limits as in the max case */
  5704. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5705. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5706. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5707. tribuf_calcntr = 0;
  5708. }
  5709. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5710. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5711. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5712. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5713. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5714. /* AFC Recal */
  5715. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5716. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5717. DPIO_AFC_RECAL);
  5718. mutex_unlock(&dev_priv->sb_lock);
  5719. }
  5720. /**
  5721. * vlv_force_pll_on - forcibly enable just the PLL
  5722. * @dev_priv: i915 private structure
  5723. * @pipe: pipe PLL to enable
  5724. * @dpll: PLL configuration
  5725. *
  5726. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5727. * in cases where we need the PLL enabled even when @pipe is not going to
  5728. * be enabled.
  5729. */
  5730. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5731. const struct dpll *dpll)
  5732. {
  5733. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5734. struct intel_crtc_state *pipe_config;
  5735. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5736. if (!pipe_config)
  5737. return -ENOMEM;
  5738. pipe_config->base.crtc = &crtc->base;
  5739. pipe_config->pixel_multiplier = 1;
  5740. pipe_config->dpll = *dpll;
  5741. if (IS_CHERRYVIEW(dev_priv)) {
  5742. chv_compute_dpll(crtc, pipe_config);
  5743. chv_prepare_pll(crtc, pipe_config);
  5744. chv_enable_pll(crtc, pipe_config);
  5745. } else {
  5746. vlv_compute_dpll(crtc, pipe_config);
  5747. vlv_prepare_pll(crtc, pipe_config);
  5748. vlv_enable_pll(crtc, pipe_config);
  5749. }
  5750. kfree(pipe_config);
  5751. return 0;
  5752. }
  5753. /**
  5754. * vlv_force_pll_off - forcibly disable just the PLL
  5755. * @dev_priv: i915 private structure
  5756. * @pipe: pipe PLL to disable
  5757. *
  5758. * Disable the PLL for @pipe. To be used in cases where we need
  5759. * the PLL enabled even when @pipe is not going to be enabled.
  5760. */
  5761. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5762. {
  5763. if (IS_CHERRYVIEW(dev_priv))
  5764. chv_disable_pll(dev_priv, pipe);
  5765. else
  5766. vlv_disable_pll(dev_priv, pipe);
  5767. }
  5768. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5769. struct intel_crtc_state *crtc_state,
  5770. struct dpll *reduced_clock)
  5771. {
  5772. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5773. u32 dpll;
  5774. struct dpll *clock = &crtc_state->dpll;
  5775. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5776. dpll = DPLL_VGA_MODE_DIS;
  5777. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5778. dpll |= DPLLB_MODE_LVDS;
  5779. else
  5780. dpll |= DPLLB_MODE_DAC_SERIAL;
  5781. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5782. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5783. dpll |= (crtc_state->pixel_multiplier - 1)
  5784. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5785. }
  5786. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5787. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5788. dpll |= DPLL_SDVO_HIGH_SPEED;
  5789. if (intel_crtc_has_dp_encoder(crtc_state))
  5790. dpll |= DPLL_SDVO_HIGH_SPEED;
  5791. /* compute bitmask from p1 value */
  5792. if (IS_PINEVIEW(dev_priv))
  5793. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5794. else {
  5795. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5796. if (IS_G4X(dev_priv) && reduced_clock)
  5797. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5798. }
  5799. switch (clock->p2) {
  5800. case 5:
  5801. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5802. break;
  5803. case 7:
  5804. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5805. break;
  5806. case 10:
  5807. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5808. break;
  5809. case 14:
  5810. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5811. break;
  5812. }
  5813. if (INTEL_GEN(dev_priv) >= 4)
  5814. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5815. if (crtc_state->sdvo_tv_clock)
  5816. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5817. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5818. intel_panel_use_ssc(dev_priv))
  5819. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5820. else
  5821. dpll |= PLL_REF_INPUT_DREFCLK;
  5822. dpll |= DPLL_VCO_ENABLE;
  5823. crtc_state->dpll_hw_state.dpll = dpll;
  5824. if (INTEL_GEN(dev_priv) >= 4) {
  5825. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5826. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5827. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5828. }
  5829. }
  5830. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5831. struct intel_crtc_state *crtc_state,
  5832. struct dpll *reduced_clock)
  5833. {
  5834. struct drm_device *dev = crtc->base.dev;
  5835. struct drm_i915_private *dev_priv = to_i915(dev);
  5836. u32 dpll;
  5837. struct dpll *clock = &crtc_state->dpll;
  5838. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5839. dpll = DPLL_VGA_MODE_DIS;
  5840. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5841. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5842. } else {
  5843. if (clock->p1 == 2)
  5844. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5845. else
  5846. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5847. if (clock->p2 == 4)
  5848. dpll |= PLL_P2_DIVIDE_BY_4;
  5849. }
  5850. if (!IS_I830(dev_priv) &&
  5851. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5852. dpll |= DPLL_DVO_2X_MODE;
  5853. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5854. intel_panel_use_ssc(dev_priv))
  5855. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5856. else
  5857. dpll |= PLL_REF_INPUT_DREFCLK;
  5858. dpll |= DPLL_VCO_ENABLE;
  5859. crtc_state->dpll_hw_state.dpll = dpll;
  5860. }
  5861. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5862. {
  5863. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5864. enum pipe pipe = intel_crtc->pipe;
  5865. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5866. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5867. uint32_t crtc_vtotal, crtc_vblank_end;
  5868. int vsyncshift = 0;
  5869. /* We need to be careful not to changed the adjusted mode, for otherwise
  5870. * the hw state checker will get angry at the mismatch. */
  5871. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5872. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5873. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5874. /* the chip adds 2 halflines automatically */
  5875. crtc_vtotal -= 1;
  5876. crtc_vblank_end -= 1;
  5877. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5878. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5879. else
  5880. vsyncshift = adjusted_mode->crtc_hsync_start -
  5881. adjusted_mode->crtc_htotal / 2;
  5882. if (vsyncshift < 0)
  5883. vsyncshift += adjusted_mode->crtc_htotal;
  5884. }
  5885. if (INTEL_GEN(dev_priv) > 3)
  5886. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5887. I915_WRITE(HTOTAL(cpu_transcoder),
  5888. (adjusted_mode->crtc_hdisplay - 1) |
  5889. ((adjusted_mode->crtc_htotal - 1) << 16));
  5890. I915_WRITE(HBLANK(cpu_transcoder),
  5891. (adjusted_mode->crtc_hblank_start - 1) |
  5892. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5893. I915_WRITE(HSYNC(cpu_transcoder),
  5894. (adjusted_mode->crtc_hsync_start - 1) |
  5895. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5896. I915_WRITE(VTOTAL(cpu_transcoder),
  5897. (adjusted_mode->crtc_vdisplay - 1) |
  5898. ((crtc_vtotal - 1) << 16));
  5899. I915_WRITE(VBLANK(cpu_transcoder),
  5900. (adjusted_mode->crtc_vblank_start - 1) |
  5901. ((crtc_vblank_end - 1) << 16));
  5902. I915_WRITE(VSYNC(cpu_transcoder),
  5903. (adjusted_mode->crtc_vsync_start - 1) |
  5904. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5905. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5906. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5907. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5908. * bits. */
  5909. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5910. (pipe == PIPE_B || pipe == PIPE_C))
  5911. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5912. }
  5913. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5914. {
  5915. struct drm_device *dev = intel_crtc->base.dev;
  5916. struct drm_i915_private *dev_priv = to_i915(dev);
  5917. enum pipe pipe = intel_crtc->pipe;
  5918. /* pipesrc controls the size that is scaled from, which should
  5919. * always be the user's requested size.
  5920. */
  5921. I915_WRITE(PIPESRC(pipe),
  5922. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5923. (intel_crtc->config->pipe_src_h - 1));
  5924. }
  5925. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5926. struct intel_crtc_state *pipe_config)
  5927. {
  5928. struct drm_device *dev = crtc->base.dev;
  5929. struct drm_i915_private *dev_priv = to_i915(dev);
  5930. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5931. uint32_t tmp;
  5932. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5933. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5934. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5935. tmp = I915_READ(HBLANK(cpu_transcoder));
  5936. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5937. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5938. tmp = I915_READ(HSYNC(cpu_transcoder));
  5939. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5940. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5941. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5942. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5943. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5944. tmp = I915_READ(VBLANK(cpu_transcoder));
  5945. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5946. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5947. tmp = I915_READ(VSYNC(cpu_transcoder));
  5948. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5949. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5950. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5951. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5952. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5953. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5954. }
  5955. }
  5956. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5957. struct intel_crtc_state *pipe_config)
  5958. {
  5959. struct drm_device *dev = crtc->base.dev;
  5960. struct drm_i915_private *dev_priv = to_i915(dev);
  5961. u32 tmp;
  5962. tmp = I915_READ(PIPESRC(crtc->pipe));
  5963. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5964. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5965. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5966. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5967. }
  5968. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5969. struct intel_crtc_state *pipe_config)
  5970. {
  5971. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5972. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5973. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5974. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5975. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5976. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5977. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5978. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5979. mode->flags = pipe_config->base.adjusted_mode.flags;
  5980. mode->type = DRM_MODE_TYPE_DRIVER;
  5981. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5982. mode->hsync = drm_mode_hsync(mode);
  5983. mode->vrefresh = drm_mode_vrefresh(mode);
  5984. drm_mode_set_name(mode);
  5985. }
  5986. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5987. {
  5988. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5989. uint32_t pipeconf;
  5990. pipeconf = 0;
  5991. /* we keep both pipes enabled on 830 */
  5992. if (IS_I830(dev_priv))
  5993. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5994. if (intel_crtc->config->double_wide)
  5995. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5996. /* only g4x and later have fancy bpc/dither controls */
  5997. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5998. IS_CHERRYVIEW(dev_priv)) {
  5999. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6000. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6001. pipeconf |= PIPECONF_DITHER_EN |
  6002. PIPECONF_DITHER_TYPE_SP;
  6003. switch (intel_crtc->config->pipe_bpp) {
  6004. case 18:
  6005. pipeconf |= PIPECONF_6BPC;
  6006. break;
  6007. case 24:
  6008. pipeconf |= PIPECONF_8BPC;
  6009. break;
  6010. case 30:
  6011. pipeconf |= PIPECONF_10BPC;
  6012. break;
  6013. default:
  6014. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6015. BUG();
  6016. }
  6017. }
  6018. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6019. if (INTEL_GEN(dev_priv) < 4 ||
  6020. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6021. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6022. else
  6023. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6024. } else
  6025. pipeconf |= PIPECONF_PROGRESSIVE;
  6026. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6027. intel_crtc->config->limited_color_range)
  6028. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6029. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6030. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6031. }
  6032. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6033. struct intel_crtc_state *crtc_state)
  6034. {
  6035. struct drm_device *dev = crtc->base.dev;
  6036. struct drm_i915_private *dev_priv = to_i915(dev);
  6037. const struct intel_limit *limit;
  6038. int refclk = 48000;
  6039. memset(&crtc_state->dpll_hw_state, 0,
  6040. sizeof(crtc_state->dpll_hw_state));
  6041. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6042. if (intel_panel_use_ssc(dev_priv)) {
  6043. refclk = dev_priv->vbt.lvds_ssc_freq;
  6044. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6045. }
  6046. limit = &intel_limits_i8xx_lvds;
  6047. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6048. limit = &intel_limits_i8xx_dvo;
  6049. } else {
  6050. limit = &intel_limits_i8xx_dac;
  6051. }
  6052. if (!crtc_state->clock_set &&
  6053. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6054. refclk, NULL, &crtc_state->dpll)) {
  6055. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6056. return -EINVAL;
  6057. }
  6058. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6059. return 0;
  6060. }
  6061. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6062. struct intel_crtc_state *crtc_state)
  6063. {
  6064. struct drm_device *dev = crtc->base.dev;
  6065. struct drm_i915_private *dev_priv = to_i915(dev);
  6066. const struct intel_limit *limit;
  6067. int refclk = 96000;
  6068. memset(&crtc_state->dpll_hw_state, 0,
  6069. sizeof(crtc_state->dpll_hw_state));
  6070. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6071. if (intel_panel_use_ssc(dev_priv)) {
  6072. refclk = dev_priv->vbt.lvds_ssc_freq;
  6073. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6074. }
  6075. if (intel_is_dual_link_lvds(dev))
  6076. limit = &intel_limits_g4x_dual_channel_lvds;
  6077. else
  6078. limit = &intel_limits_g4x_single_channel_lvds;
  6079. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6080. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6081. limit = &intel_limits_g4x_hdmi;
  6082. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6083. limit = &intel_limits_g4x_sdvo;
  6084. } else {
  6085. /* The option is for other outputs */
  6086. limit = &intel_limits_i9xx_sdvo;
  6087. }
  6088. if (!crtc_state->clock_set &&
  6089. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6090. refclk, NULL, &crtc_state->dpll)) {
  6091. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6092. return -EINVAL;
  6093. }
  6094. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6095. return 0;
  6096. }
  6097. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6098. struct intel_crtc_state *crtc_state)
  6099. {
  6100. struct drm_device *dev = crtc->base.dev;
  6101. struct drm_i915_private *dev_priv = to_i915(dev);
  6102. const struct intel_limit *limit;
  6103. int refclk = 96000;
  6104. memset(&crtc_state->dpll_hw_state, 0,
  6105. sizeof(crtc_state->dpll_hw_state));
  6106. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6107. if (intel_panel_use_ssc(dev_priv)) {
  6108. refclk = dev_priv->vbt.lvds_ssc_freq;
  6109. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6110. }
  6111. limit = &intel_limits_pineview_lvds;
  6112. } else {
  6113. limit = &intel_limits_pineview_sdvo;
  6114. }
  6115. if (!crtc_state->clock_set &&
  6116. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6117. refclk, NULL, &crtc_state->dpll)) {
  6118. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6119. return -EINVAL;
  6120. }
  6121. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6122. return 0;
  6123. }
  6124. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6125. struct intel_crtc_state *crtc_state)
  6126. {
  6127. struct drm_device *dev = crtc->base.dev;
  6128. struct drm_i915_private *dev_priv = to_i915(dev);
  6129. const struct intel_limit *limit;
  6130. int refclk = 96000;
  6131. memset(&crtc_state->dpll_hw_state, 0,
  6132. sizeof(crtc_state->dpll_hw_state));
  6133. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6134. if (intel_panel_use_ssc(dev_priv)) {
  6135. refclk = dev_priv->vbt.lvds_ssc_freq;
  6136. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6137. }
  6138. limit = &intel_limits_i9xx_lvds;
  6139. } else {
  6140. limit = &intel_limits_i9xx_sdvo;
  6141. }
  6142. if (!crtc_state->clock_set &&
  6143. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6144. refclk, NULL, &crtc_state->dpll)) {
  6145. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6146. return -EINVAL;
  6147. }
  6148. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6149. return 0;
  6150. }
  6151. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6152. struct intel_crtc_state *crtc_state)
  6153. {
  6154. int refclk = 100000;
  6155. const struct intel_limit *limit = &intel_limits_chv;
  6156. memset(&crtc_state->dpll_hw_state, 0,
  6157. sizeof(crtc_state->dpll_hw_state));
  6158. if (!crtc_state->clock_set &&
  6159. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6160. refclk, NULL, &crtc_state->dpll)) {
  6161. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6162. return -EINVAL;
  6163. }
  6164. chv_compute_dpll(crtc, crtc_state);
  6165. return 0;
  6166. }
  6167. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6168. struct intel_crtc_state *crtc_state)
  6169. {
  6170. int refclk = 100000;
  6171. const struct intel_limit *limit = &intel_limits_vlv;
  6172. memset(&crtc_state->dpll_hw_state, 0,
  6173. sizeof(crtc_state->dpll_hw_state));
  6174. if (!crtc_state->clock_set &&
  6175. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6176. refclk, NULL, &crtc_state->dpll)) {
  6177. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6178. return -EINVAL;
  6179. }
  6180. vlv_compute_dpll(crtc, crtc_state);
  6181. return 0;
  6182. }
  6183. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6184. struct intel_crtc_state *pipe_config)
  6185. {
  6186. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6187. uint32_t tmp;
  6188. if (INTEL_GEN(dev_priv) <= 3 &&
  6189. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6190. return;
  6191. tmp = I915_READ(PFIT_CONTROL);
  6192. if (!(tmp & PFIT_ENABLE))
  6193. return;
  6194. /* Check whether the pfit is attached to our pipe. */
  6195. if (INTEL_GEN(dev_priv) < 4) {
  6196. if (crtc->pipe != PIPE_B)
  6197. return;
  6198. } else {
  6199. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6200. return;
  6201. }
  6202. pipe_config->gmch_pfit.control = tmp;
  6203. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6204. }
  6205. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6206. struct intel_crtc_state *pipe_config)
  6207. {
  6208. struct drm_device *dev = crtc->base.dev;
  6209. struct drm_i915_private *dev_priv = to_i915(dev);
  6210. int pipe = pipe_config->cpu_transcoder;
  6211. struct dpll clock;
  6212. u32 mdiv;
  6213. int refclk = 100000;
  6214. /* In case of DSI, DPLL will not be used */
  6215. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6216. return;
  6217. mutex_lock(&dev_priv->sb_lock);
  6218. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6219. mutex_unlock(&dev_priv->sb_lock);
  6220. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6221. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6222. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6223. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6224. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6225. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6226. }
  6227. static void
  6228. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6229. struct intel_initial_plane_config *plane_config)
  6230. {
  6231. struct drm_device *dev = crtc->base.dev;
  6232. struct drm_i915_private *dev_priv = to_i915(dev);
  6233. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6234. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6235. enum pipe pipe = crtc->pipe;
  6236. u32 val, base, offset;
  6237. int fourcc, pixel_format;
  6238. unsigned int aligned_height;
  6239. struct drm_framebuffer *fb;
  6240. struct intel_framebuffer *intel_fb;
  6241. if (!plane->get_hw_state(plane))
  6242. return;
  6243. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6244. if (!intel_fb) {
  6245. DRM_DEBUG_KMS("failed to alloc fb\n");
  6246. return;
  6247. }
  6248. fb = &intel_fb->base;
  6249. fb->dev = dev;
  6250. val = I915_READ(DSPCNTR(i9xx_plane));
  6251. if (INTEL_GEN(dev_priv) >= 4) {
  6252. if (val & DISPPLANE_TILED) {
  6253. plane_config->tiling = I915_TILING_X;
  6254. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6255. }
  6256. }
  6257. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6258. fourcc = i9xx_format_to_fourcc(pixel_format);
  6259. fb->format = drm_format_info(fourcc);
  6260. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6261. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6262. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6263. } else if (INTEL_GEN(dev_priv) >= 4) {
  6264. if (plane_config->tiling)
  6265. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6266. else
  6267. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6268. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6269. } else {
  6270. base = I915_READ(DSPADDR(i9xx_plane));
  6271. }
  6272. plane_config->base = base;
  6273. val = I915_READ(PIPESRC(pipe));
  6274. fb->width = ((val >> 16) & 0xfff) + 1;
  6275. fb->height = ((val >> 0) & 0xfff) + 1;
  6276. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6277. fb->pitches[0] = val & 0xffffffc0;
  6278. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6279. plane_config->size = fb->pitches[0] * aligned_height;
  6280. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6281. crtc->base.name, plane->base.name, fb->width, fb->height,
  6282. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6283. plane_config->size);
  6284. plane_config->fb = intel_fb;
  6285. }
  6286. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6287. struct intel_crtc_state *pipe_config)
  6288. {
  6289. struct drm_device *dev = crtc->base.dev;
  6290. struct drm_i915_private *dev_priv = to_i915(dev);
  6291. int pipe = pipe_config->cpu_transcoder;
  6292. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6293. struct dpll clock;
  6294. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6295. int refclk = 100000;
  6296. /* In case of DSI, DPLL will not be used */
  6297. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6298. return;
  6299. mutex_lock(&dev_priv->sb_lock);
  6300. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6301. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6302. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6303. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6304. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6305. mutex_unlock(&dev_priv->sb_lock);
  6306. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6307. clock.m2 = (pll_dw0 & 0xff) << 22;
  6308. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6309. clock.m2 |= pll_dw2 & 0x3fffff;
  6310. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6311. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6312. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6313. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6314. }
  6315. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6316. struct intel_crtc_state *pipe_config)
  6317. {
  6318. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6319. enum intel_display_power_domain power_domain;
  6320. uint32_t tmp;
  6321. bool ret;
  6322. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6323. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6324. return false;
  6325. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6326. pipe_config->shared_dpll = NULL;
  6327. ret = false;
  6328. tmp = I915_READ(PIPECONF(crtc->pipe));
  6329. if (!(tmp & PIPECONF_ENABLE))
  6330. goto out;
  6331. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6332. IS_CHERRYVIEW(dev_priv)) {
  6333. switch (tmp & PIPECONF_BPC_MASK) {
  6334. case PIPECONF_6BPC:
  6335. pipe_config->pipe_bpp = 18;
  6336. break;
  6337. case PIPECONF_8BPC:
  6338. pipe_config->pipe_bpp = 24;
  6339. break;
  6340. case PIPECONF_10BPC:
  6341. pipe_config->pipe_bpp = 30;
  6342. break;
  6343. default:
  6344. break;
  6345. }
  6346. }
  6347. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6348. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6349. pipe_config->limited_color_range = true;
  6350. if (INTEL_GEN(dev_priv) < 4)
  6351. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6352. intel_get_pipe_timings(crtc, pipe_config);
  6353. intel_get_pipe_src_size(crtc, pipe_config);
  6354. i9xx_get_pfit_config(crtc, pipe_config);
  6355. if (INTEL_GEN(dev_priv) >= 4) {
  6356. /* No way to read it out on pipes B and C */
  6357. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6358. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6359. else
  6360. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6361. pipe_config->pixel_multiplier =
  6362. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6363. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6364. pipe_config->dpll_hw_state.dpll_md = tmp;
  6365. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6366. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6367. tmp = I915_READ(DPLL(crtc->pipe));
  6368. pipe_config->pixel_multiplier =
  6369. ((tmp & SDVO_MULTIPLIER_MASK)
  6370. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6371. } else {
  6372. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6373. * port and will be fixed up in the encoder->get_config
  6374. * function. */
  6375. pipe_config->pixel_multiplier = 1;
  6376. }
  6377. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6378. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6379. /*
  6380. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6381. * on 830. Filter it out here so that we don't
  6382. * report errors due to that.
  6383. */
  6384. if (IS_I830(dev_priv))
  6385. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6386. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6387. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6388. } else {
  6389. /* Mask out read-only status bits. */
  6390. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6391. DPLL_PORTC_READY_MASK |
  6392. DPLL_PORTB_READY_MASK);
  6393. }
  6394. if (IS_CHERRYVIEW(dev_priv))
  6395. chv_crtc_clock_get(crtc, pipe_config);
  6396. else if (IS_VALLEYVIEW(dev_priv))
  6397. vlv_crtc_clock_get(crtc, pipe_config);
  6398. else
  6399. i9xx_crtc_clock_get(crtc, pipe_config);
  6400. /*
  6401. * Normally the dotclock is filled in by the encoder .get_config()
  6402. * but in case the pipe is enabled w/o any ports we need a sane
  6403. * default.
  6404. */
  6405. pipe_config->base.adjusted_mode.crtc_clock =
  6406. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6407. ret = true;
  6408. out:
  6409. intel_display_power_put(dev_priv, power_domain);
  6410. return ret;
  6411. }
  6412. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6413. {
  6414. struct intel_encoder *encoder;
  6415. int i;
  6416. u32 val, final;
  6417. bool has_lvds = false;
  6418. bool has_cpu_edp = false;
  6419. bool has_panel = false;
  6420. bool has_ck505 = false;
  6421. bool can_ssc = false;
  6422. bool using_ssc_source = false;
  6423. /* We need to take the global config into account */
  6424. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6425. switch (encoder->type) {
  6426. case INTEL_OUTPUT_LVDS:
  6427. has_panel = true;
  6428. has_lvds = true;
  6429. break;
  6430. case INTEL_OUTPUT_EDP:
  6431. has_panel = true;
  6432. if (encoder->port == PORT_A)
  6433. has_cpu_edp = true;
  6434. break;
  6435. default:
  6436. break;
  6437. }
  6438. }
  6439. if (HAS_PCH_IBX(dev_priv)) {
  6440. has_ck505 = dev_priv->vbt.display_clock_mode;
  6441. can_ssc = has_ck505;
  6442. } else {
  6443. has_ck505 = false;
  6444. can_ssc = true;
  6445. }
  6446. /* Check if any DPLLs are using the SSC source */
  6447. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6448. u32 temp = I915_READ(PCH_DPLL(i));
  6449. if (!(temp & DPLL_VCO_ENABLE))
  6450. continue;
  6451. if ((temp & PLL_REF_INPUT_MASK) ==
  6452. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6453. using_ssc_source = true;
  6454. break;
  6455. }
  6456. }
  6457. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6458. has_panel, has_lvds, has_ck505, using_ssc_source);
  6459. /* Ironlake: try to setup display ref clock before DPLL
  6460. * enabling. This is only under driver's control after
  6461. * PCH B stepping, previous chipset stepping should be
  6462. * ignoring this setting.
  6463. */
  6464. val = I915_READ(PCH_DREF_CONTROL);
  6465. /* As we must carefully and slowly disable/enable each source in turn,
  6466. * compute the final state we want first and check if we need to
  6467. * make any changes at all.
  6468. */
  6469. final = val;
  6470. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6471. if (has_ck505)
  6472. final |= DREF_NONSPREAD_CK505_ENABLE;
  6473. else
  6474. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6475. final &= ~DREF_SSC_SOURCE_MASK;
  6476. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6477. final &= ~DREF_SSC1_ENABLE;
  6478. if (has_panel) {
  6479. final |= DREF_SSC_SOURCE_ENABLE;
  6480. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6481. final |= DREF_SSC1_ENABLE;
  6482. if (has_cpu_edp) {
  6483. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6484. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6485. else
  6486. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6487. } else
  6488. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6489. } else if (using_ssc_source) {
  6490. final |= DREF_SSC_SOURCE_ENABLE;
  6491. final |= DREF_SSC1_ENABLE;
  6492. }
  6493. if (final == val)
  6494. return;
  6495. /* Always enable nonspread source */
  6496. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6497. if (has_ck505)
  6498. val |= DREF_NONSPREAD_CK505_ENABLE;
  6499. else
  6500. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6501. if (has_panel) {
  6502. val &= ~DREF_SSC_SOURCE_MASK;
  6503. val |= DREF_SSC_SOURCE_ENABLE;
  6504. /* SSC must be turned on before enabling the CPU output */
  6505. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6506. DRM_DEBUG_KMS("Using SSC on panel\n");
  6507. val |= DREF_SSC1_ENABLE;
  6508. } else
  6509. val &= ~DREF_SSC1_ENABLE;
  6510. /* Get SSC going before enabling the outputs */
  6511. I915_WRITE(PCH_DREF_CONTROL, val);
  6512. POSTING_READ(PCH_DREF_CONTROL);
  6513. udelay(200);
  6514. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6515. /* Enable CPU source on CPU attached eDP */
  6516. if (has_cpu_edp) {
  6517. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6518. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6519. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6520. } else
  6521. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6522. } else
  6523. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6524. I915_WRITE(PCH_DREF_CONTROL, val);
  6525. POSTING_READ(PCH_DREF_CONTROL);
  6526. udelay(200);
  6527. } else {
  6528. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6529. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6530. /* Turn off CPU output */
  6531. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6532. I915_WRITE(PCH_DREF_CONTROL, val);
  6533. POSTING_READ(PCH_DREF_CONTROL);
  6534. udelay(200);
  6535. if (!using_ssc_source) {
  6536. DRM_DEBUG_KMS("Disabling SSC source\n");
  6537. /* Turn off the SSC source */
  6538. val &= ~DREF_SSC_SOURCE_MASK;
  6539. val |= DREF_SSC_SOURCE_DISABLE;
  6540. /* Turn off SSC1 */
  6541. val &= ~DREF_SSC1_ENABLE;
  6542. I915_WRITE(PCH_DREF_CONTROL, val);
  6543. POSTING_READ(PCH_DREF_CONTROL);
  6544. udelay(200);
  6545. }
  6546. }
  6547. BUG_ON(val != final);
  6548. }
  6549. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6550. {
  6551. uint32_t tmp;
  6552. tmp = I915_READ(SOUTH_CHICKEN2);
  6553. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6554. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6555. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6556. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6557. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6558. tmp = I915_READ(SOUTH_CHICKEN2);
  6559. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6560. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6561. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6562. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6563. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6564. }
  6565. /* WaMPhyProgramming:hsw */
  6566. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6567. {
  6568. uint32_t tmp;
  6569. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6570. tmp &= ~(0xFF << 24);
  6571. tmp |= (0x12 << 24);
  6572. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6573. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6574. tmp |= (1 << 11);
  6575. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6576. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6577. tmp |= (1 << 11);
  6578. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6579. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6580. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6581. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6582. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6583. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6584. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6585. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6586. tmp &= ~(7 << 13);
  6587. tmp |= (5 << 13);
  6588. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6589. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6590. tmp &= ~(7 << 13);
  6591. tmp |= (5 << 13);
  6592. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6593. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6594. tmp &= ~0xFF;
  6595. tmp |= 0x1C;
  6596. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6597. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6598. tmp &= ~0xFF;
  6599. tmp |= 0x1C;
  6600. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6601. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6602. tmp &= ~(0xFF << 16);
  6603. tmp |= (0x1C << 16);
  6604. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6605. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6606. tmp &= ~(0xFF << 16);
  6607. tmp |= (0x1C << 16);
  6608. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6609. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6610. tmp |= (1 << 27);
  6611. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6612. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6613. tmp |= (1 << 27);
  6614. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6615. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6616. tmp &= ~(0xF << 28);
  6617. tmp |= (4 << 28);
  6618. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6619. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6620. tmp &= ~(0xF << 28);
  6621. tmp |= (4 << 28);
  6622. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6623. }
  6624. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6625. * Programming" based on the parameters passed:
  6626. * - Sequence to enable CLKOUT_DP
  6627. * - Sequence to enable CLKOUT_DP without spread
  6628. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6629. */
  6630. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6631. bool with_spread, bool with_fdi)
  6632. {
  6633. uint32_t reg, tmp;
  6634. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6635. with_spread = true;
  6636. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6637. with_fdi, "LP PCH doesn't have FDI\n"))
  6638. with_fdi = false;
  6639. mutex_lock(&dev_priv->sb_lock);
  6640. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6641. tmp &= ~SBI_SSCCTL_DISABLE;
  6642. tmp |= SBI_SSCCTL_PATHALT;
  6643. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6644. udelay(24);
  6645. if (with_spread) {
  6646. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6647. tmp &= ~SBI_SSCCTL_PATHALT;
  6648. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6649. if (with_fdi) {
  6650. lpt_reset_fdi_mphy(dev_priv);
  6651. lpt_program_fdi_mphy(dev_priv);
  6652. }
  6653. }
  6654. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6655. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6656. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6657. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6658. mutex_unlock(&dev_priv->sb_lock);
  6659. }
  6660. /* Sequence to disable CLKOUT_DP */
  6661. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6662. {
  6663. uint32_t reg, tmp;
  6664. mutex_lock(&dev_priv->sb_lock);
  6665. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6666. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6667. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6668. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6669. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6670. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6671. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6672. tmp |= SBI_SSCCTL_PATHALT;
  6673. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6674. udelay(32);
  6675. }
  6676. tmp |= SBI_SSCCTL_DISABLE;
  6677. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6678. }
  6679. mutex_unlock(&dev_priv->sb_lock);
  6680. }
  6681. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6682. static const uint16_t sscdivintphase[] = {
  6683. [BEND_IDX( 50)] = 0x3B23,
  6684. [BEND_IDX( 45)] = 0x3B23,
  6685. [BEND_IDX( 40)] = 0x3C23,
  6686. [BEND_IDX( 35)] = 0x3C23,
  6687. [BEND_IDX( 30)] = 0x3D23,
  6688. [BEND_IDX( 25)] = 0x3D23,
  6689. [BEND_IDX( 20)] = 0x3E23,
  6690. [BEND_IDX( 15)] = 0x3E23,
  6691. [BEND_IDX( 10)] = 0x3F23,
  6692. [BEND_IDX( 5)] = 0x3F23,
  6693. [BEND_IDX( 0)] = 0x0025,
  6694. [BEND_IDX( -5)] = 0x0025,
  6695. [BEND_IDX(-10)] = 0x0125,
  6696. [BEND_IDX(-15)] = 0x0125,
  6697. [BEND_IDX(-20)] = 0x0225,
  6698. [BEND_IDX(-25)] = 0x0225,
  6699. [BEND_IDX(-30)] = 0x0325,
  6700. [BEND_IDX(-35)] = 0x0325,
  6701. [BEND_IDX(-40)] = 0x0425,
  6702. [BEND_IDX(-45)] = 0x0425,
  6703. [BEND_IDX(-50)] = 0x0525,
  6704. };
  6705. /*
  6706. * Bend CLKOUT_DP
  6707. * steps -50 to 50 inclusive, in steps of 5
  6708. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6709. * change in clock period = -(steps / 10) * 5.787 ps
  6710. */
  6711. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6712. {
  6713. uint32_t tmp;
  6714. int idx = BEND_IDX(steps);
  6715. if (WARN_ON(steps % 5 != 0))
  6716. return;
  6717. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6718. return;
  6719. mutex_lock(&dev_priv->sb_lock);
  6720. if (steps % 10 != 0)
  6721. tmp = 0xAAAAAAAB;
  6722. else
  6723. tmp = 0x00000000;
  6724. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6725. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6726. tmp &= 0xffff0000;
  6727. tmp |= sscdivintphase[idx];
  6728. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6729. mutex_unlock(&dev_priv->sb_lock);
  6730. }
  6731. #undef BEND_IDX
  6732. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6733. {
  6734. struct intel_encoder *encoder;
  6735. bool has_vga = false;
  6736. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6737. switch (encoder->type) {
  6738. case INTEL_OUTPUT_ANALOG:
  6739. has_vga = true;
  6740. break;
  6741. default:
  6742. break;
  6743. }
  6744. }
  6745. if (has_vga) {
  6746. lpt_bend_clkout_dp(dev_priv, 0);
  6747. lpt_enable_clkout_dp(dev_priv, true, true);
  6748. } else {
  6749. lpt_disable_clkout_dp(dev_priv);
  6750. }
  6751. }
  6752. /*
  6753. * Initialize reference clocks when the driver loads
  6754. */
  6755. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6756. {
  6757. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6758. ironlake_init_pch_refclk(dev_priv);
  6759. else if (HAS_PCH_LPT(dev_priv))
  6760. lpt_init_pch_refclk(dev_priv);
  6761. }
  6762. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6763. {
  6764. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6766. int pipe = intel_crtc->pipe;
  6767. uint32_t val;
  6768. val = 0;
  6769. switch (intel_crtc->config->pipe_bpp) {
  6770. case 18:
  6771. val |= PIPECONF_6BPC;
  6772. break;
  6773. case 24:
  6774. val |= PIPECONF_8BPC;
  6775. break;
  6776. case 30:
  6777. val |= PIPECONF_10BPC;
  6778. break;
  6779. case 36:
  6780. val |= PIPECONF_12BPC;
  6781. break;
  6782. default:
  6783. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6784. BUG();
  6785. }
  6786. if (intel_crtc->config->dither)
  6787. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6788. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6789. val |= PIPECONF_INTERLACED_ILK;
  6790. else
  6791. val |= PIPECONF_PROGRESSIVE;
  6792. if (intel_crtc->config->limited_color_range)
  6793. val |= PIPECONF_COLOR_RANGE_SELECT;
  6794. I915_WRITE(PIPECONF(pipe), val);
  6795. POSTING_READ(PIPECONF(pipe));
  6796. }
  6797. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6798. {
  6799. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6801. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6802. u32 val = 0;
  6803. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6804. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6805. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6806. val |= PIPECONF_INTERLACED_ILK;
  6807. else
  6808. val |= PIPECONF_PROGRESSIVE;
  6809. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6810. POSTING_READ(PIPECONF(cpu_transcoder));
  6811. }
  6812. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6813. {
  6814. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6816. struct intel_crtc_state *config = intel_crtc->config;
  6817. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6818. u32 val = 0;
  6819. switch (intel_crtc->config->pipe_bpp) {
  6820. case 18:
  6821. val |= PIPEMISC_DITHER_6_BPC;
  6822. break;
  6823. case 24:
  6824. val |= PIPEMISC_DITHER_8_BPC;
  6825. break;
  6826. case 30:
  6827. val |= PIPEMISC_DITHER_10_BPC;
  6828. break;
  6829. case 36:
  6830. val |= PIPEMISC_DITHER_12_BPC;
  6831. break;
  6832. default:
  6833. /* Case prevented by pipe_config_set_bpp. */
  6834. BUG();
  6835. }
  6836. if (intel_crtc->config->dither)
  6837. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6838. if (config->ycbcr420) {
  6839. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6840. PIPEMISC_YUV420_ENABLE |
  6841. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6842. }
  6843. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6844. }
  6845. }
  6846. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6847. {
  6848. /*
  6849. * Account for spread spectrum to avoid
  6850. * oversubscribing the link. Max center spread
  6851. * is 2.5%; use 5% for safety's sake.
  6852. */
  6853. u32 bps = target_clock * bpp * 21 / 20;
  6854. return DIV_ROUND_UP(bps, link_bw * 8);
  6855. }
  6856. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6857. {
  6858. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6859. }
  6860. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6861. struct intel_crtc_state *crtc_state,
  6862. struct dpll *reduced_clock)
  6863. {
  6864. struct drm_crtc *crtc = &intel_crtc->base;
  6865. struct drm_device *dev = crtc->dev;
  6866. struct drm_i915_private *dev_priv = to_i915(dev);
  6867. u32 dpll, fp, fp2;
  6868. int factor;
  6869. /* Enable autotuning of the PLL clock (if permissible) */
  6870. factor = 21;
  6871. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6872. if ((intel_panel_use_ssc(dev_priv) &&
  6873. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6874. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6875. factor = 25;
  6876. } else if (crtc_state->sdvo_tv_clock)
  6877. factor = 20;
  6878. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6879. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6880. fp |= FP_CB_TUNE;
  6881. if (reduced_clock) {
  6882. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6883. if (reduced_clock->m < factor * reduced_clock->n)
  6884. fp2 |= FP_CB_TUNE;
  6885. } else {
  6886. fp2 = fp;
  6887. }
  6888. dpll = 0;
  6889. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6890. dpll |= DPLLB_MODE_LVDS;
  6891. else
  6892. dpll |= DPLLB_MODE_DAC_SERIAL;
  6893. dpll |= (crtc_state->pixel_multiplier - 1)
  6894. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6895. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6896. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6897. dpll |= DPLL_SDVO_HIGH_SPEED;
  6898. if (intel_crtc_has_dp_encoder(crtc_state))
  6899. dpll |= DPLL_SDVO_HIGH_SPEED;
  6900. /*
  6901. * The high speed IO clock is only really required for
  6902. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6903. * possible to share the DPLL between CRT and HDMI. Enabling
  6904. * the clock needlessly does no real harm, except use up a
  6905. * bit of power potentially.
  6906. *
  6907. * We'll limit this to IVB with 3 pipes, since it has only two
  6908. * DPLLs and so DPLL sharing is the only way to get three pipes
  6909. * driving PCH ports at the same time. On SNB we could do this,
  6910. * and potentially avoid enabling the second DPLL, but it's not
  6911. * clear if it''s a win or loss power wise. No point in doing
  6912. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6913. */
  6914. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6915. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6916. dpll |= DPLL_SDVO_HIGH_SPEED;
  6917. /* compute bitmask from p1 value */
  6918. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6919. /* also FPA1 */
  6920. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6921. switch (crtc_state->dpll.p2) {
  6922. case 5:
  6923. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6924. break;
  6925. case 7:
  6926. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6927. break;
  6928. case 10:
  6929. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6930. break;
  6931. case 14:
  6932. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6933. break;
  6934. }
  6935. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6936. intel_panel_use_ssc(dev_priv))
  6937. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6938. else
  6939. dpll |= PLL_REF_INPUT_DREFCLK;
  6940. dpll |= DPLL_VCO_ENABLE;
  6941. crtc_state->dpll_hw_state.dpll = dpll;
  6942. crtc_state->dpll_hw_state.fp0 = fp;
  6943. crtc_state->dpll_hw_state.fp1 = fp2;
  6944. }
  6945. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6946. struct intel_crtc_state *crtc_state)
  6947. {
  6948. struct drm_device *dev = crtc->base.dev;
  6949. struct drm_i915_private *dev_priv = to_i915(dev);
  6950. const struct intel_limit *limit;
  6951. int refclk = 120000;
  6952. memset(&crtc_state->dpll_hw_state, 0,
  6953. sizeof(crtc_state->dpll_hw_state));
  6954. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6955. if (!crtc_state->has_pch_encoder)
  6956. return 0;
  6957. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6958. if (intel_panel_use_ssc(dev_priv)) {
  6959. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6960. dev_priv->vbt.lvds_ssc_freq);
  6961. refclk = dev_priv->vbt.lvds_ssc_freq;
  6962. }
  6963. if (intel_is_dual_link_lvds(dev)) {
  6964. if (refclk == 100000)
  6965. limit = &intel_limits_ironlake_dual_lvds_100m;
  6966. else
  6967. limit = &intel_limits_ironlake_dual_lvds;
  6968. } else {
  6969. if (refclk == 100000)
  6970. limit = &intel_limits_ironlake_single_lvds_100m;
  6971. else
  6972. limit = &intel_limits_ironlake_single_lvds;
  6973. }
  6974. } else {
  6975. limit = &intel_limits_ironlake_dac;
  6976. }
  6977. if (!crtc_state->clock_set &&
  6978. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6979. refclk, NULL, &crtc_state->dpll)) {
  6980. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6981. return -EINVAL;
  6982. }
  6983. ironlake_compute_dpll(crtc, crtc_state, NULL);
  6984. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  6985. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6986. pipe_name(crtc->pipe));
  6987. return -EINVAL;
  6988. }
  6989. return 0;
  6990. }
  6991. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6992. struct intel_link_m_n *m_n)
  6993. {
  6994. struct drm_device *dev = crtc->base.dev;
  6995. struct drm_i915_private *dev_priv = to_i915(dev);
  6996. enum pipe pipe = crtc->pipe;
  6997. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6998. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6999. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7000. & ~TU_SIZE_MASK;
  7001. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7002. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7003. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7004. }
  7005. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7006. enum transcoder transcoder,
  7007. struct intel_link_m_n *m_n,
  7008. struct intel_link_m_n *m2_n2)
  7009. {
  7010. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7011. enum pipe pipe = crtc->pipe;
  7012. if (INTEL_GEN(dev_priv) >= 5) {
  7013. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7014. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7015. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7016. & ~TU_SIZE_MASK;
  7017. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7018. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7019. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7020. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7021. * gen < 8) and if DRRS is supported (to make sure the
  7022. * registers are not unnecessarily read).
  7023. */
  7024. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7025. crtc->config->has_drrs) {
  7026. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7027. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7028. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7029. & ~TU_SIZE_MASK;
  7030. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7031. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7032. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7033. }
  7034. } else {
  7035. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7036. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7037. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7038. & ~TU_SIZE_MASK;
  7039. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7040. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7041. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7042. }
  7043. }
  7044. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7045. struct intel_crtc_state *pipe_config)
  7046. {
  7047. if (pipe_config->has_pch_encoder)
  7048. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7049. else
  7050. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7051. &pipe_config->dp_m_n,
  7052. &pipe_config->dp_m2_n2);
  7053. }
  7054. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7055. struct intel_crtc_state *pipe_config)
  7056. {
  7057. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7058. &pipe_config->fdi_m_n, NULL);
  7059. }
  7060. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7061. struct intel_crtc_state *pipe_config)
  7062. {
  7063. struct drm_device *dev = crtc->base.dev;
  7064. struct drm_i915_private *dev_priv = to_i915(dev);
  7065. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7066. uint32_t ps_ctrl = 0;
  7067. int id = -1;
  7068. int i;
  7069. /* find scaler attached to this pipe */
  7070. for (i = 0; i < crtc->num_scalers; i++) {
  7071. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7072. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7073. id = i;
  7074. pipe_config->pch_pfit.enabled = true;
  7075. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7076. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7077. break;
  7078. }
  7079. }
  7080. scaler_state->scaler_id = id;
  7081. if (id >= 0) {
  7082. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7083. } else {
  7084. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7085. }
  7086. }
  7087. static void
  7088. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7089. struct intel_initial_plane_config *plane_config)
  7090. {
  7091. struct drm_device *dev = crtc->base.dev;
  7092. struct drm_i915_private *dev_priv = to_i915(dev);
  7093. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7094. enum plane_id plane_id = plane->id;
  7095. enum pipe pipe = crtc->pipe;
  7096. u32 val, base, offset, stride_mult, tiling, alpha;
  7097. int fourcc, pixel_format;
  7098. unsigned int aligned_height;
  7099. struct drm_framebuffer *fb;
  7100. struct intel_framebuffer *intel_fb;
  7101. if (!plane->get_hw_state(plane))
  7102. return;
  7103. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7104. if (!intel_fb) {
  7105. DRM_DEBUG_KMS("failed to alloc fb\n");
  7106. return;
  7107. }
  7108. fb = &intel_fb->base;
  7109. fb->dev = dev;
  7110. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7111. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7112. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7113. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7114. alpha &= PLANE_COLOR_ALPHA_MASK;
  7115. } else {
  7116. alpha = val & PLANE_CTL_ALPHA_MASK;
  7117. }
  7118. fourcc = skl_format_to_fourcc(pixel_format,
  7119. val & PLANE_CTL_ORDER_RGBX, alpha);
  7120. fb->format = drm_format_info(fourcc);
  7121. tiling = val & PLANE_CTL_TILED_MASK;
  7122. switch (tiling) {
  7123. case PLANE_CTL_TILED_LINEAR:
  7124. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7125. break;
  7126. case PLANE_CTL_TILED_X:
  7127. plane_config->tiling = I915_TILING_X;
  7128. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7129. break;
  7130. case PLANE_CTL_TILED_Y:
  7131. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7132. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7133. else
  7134. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7135. break;
  7136. case PLANE_CTL_TILED_YF:
  7137. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7138. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7139. else
  7140. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7141. break;
  7142. default:
  7143. MISSING_CASE(tiling);
  7144. goto error;
  7145. }
  7146. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7147. plane_config->base = base;
  7148. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7149. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7150. fb->height = ((val >> 16) & 0xfff) + 1;
  7151. fb->width = ((val >> 0) & 0x1fff) + 1;
  7152. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7153. stride_mult = intel_fb_stride_alignment(fb, 0);
  7154. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7155. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7156. plane_config->size = fb->pitches[0] * aligned_height;
  7157. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7158. crtc->base.name, plane->base.name, fb->width, fb->height,
  7159. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7160. plane_config->size);
  7161. plane_config->fb = intel_fb;
  7162. return;
  7163. error:
  7164. kfree(intel_fb);
  7165. }
  7166. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7167. struct intel_crtc_state *pipe_config)
  7168. {
  7169. struct drm_device *dev = crtc->base.dev;
  7170. struct drm_i915_private *dev_priv = to_i915(dev);
  7171. uint32_t tmp;
  7172. tmp = I915_READ(PF_CTL(crtc->pipe));
  7173. if (tmp & PF_ENABLE) {
  7174. pipe_config->pch_pfit.enabled = true;
  7175. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7176. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7177. /* We currently do not free assignements of panel fitters on
  7178. * ivb/hsw (since we don't use the higher upscaling modes which
  7179. * differentiates them) so just WARN about this case for now. */
  7180. if (IS_GEN7(dev_priv)) {
  7181. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7182. PF_PIPE_SEL_IVB(crtc->pipe));
  7183. }
  7184. }
  7185. }
  7186. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7187. struct intel_crtc_state *pipe_config)
  7188. {
  7189. struct drm_device *dev = crtc->base.dev;
  7190. struct drm_i915_private *dev_priv = to_i915(dev);
  7191. enum intel_display_power_domain power_domain;
  7192. uint32_t tmp;
  7193. bool ret;
  7194. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7195. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7196. return false;
  7197. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7198. pipe_config->shared_dpll = NULL;
  7199. ret = false;
  7200. tmp = I915_READ(PIPECONF(crtc->pipe));
  7201. if (!(tmp & PIPECONF_ENABLE))
  7202. goto out;
  7203. switch (tmp & PIPECONF_BPC_MASK) {
  7204. case PIPECONF_6BPC:
  7205. pipe_config->pipe_bpp = 18;
  7206. break;
  7207. case PIPECONF_8BPC:
  7208. pipe_config->pipe_bpp = 24;
  7209. break;
  7210. case PIPECONF_10BPC:
  7211. pipe_config->pipe_bpp = 30;
  7212. break;
  7213. case PIPECONF_12BPC:
  7214. pipe_config->pipe_bpp = 36;
  7215. break;
  7216. default:
  7217. break;
  7218. }
  7219. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7220. pipe_config->limited_color_range = true;
  7221. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7222. struct intel_shared_dpll *pll;
  7223. enum intel_dpll_id pll_id;
  7224. pipe_config->has_pch_encoder = true;
  7225. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7226. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7227. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7228. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7229. if (HAS_PCH_IBX(dev_priv)) {
  7230. /*
  7231. * The pipe->pch transcoder and pch transcoder->pll
  7232. * mapping is fixed.
  7233. */
  7234. pll_id = (enum intel_dpll_id) crtc->pipe;
  7235. } else {
  7236. tmp = I915_READ(PCH_DPLL_SEL);
  7237. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7238. pll_id = DPLL_ID_PCH_PLL_B;
  7239. else
  7240. pll_id= DPLL_ID_PCH_PLL_A;
  7241. }
  7242. pipe_config->shared_dpll =
  7243. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7244. pll = pipe_config->shared_dpll;
  7245. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7246. &pipe_config->dpll_hw_state));
  7247. tmp = pipe_config->dpll_hw_state.dpll;
  7248. pipe_config->pixel_multiplier =
  7249. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7250. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7251. ironlake_pch_clock_get(crtc, pipe_config);
  7252. } else {
  7253. pipe_config->pixel_multiplier = 1;
  7254. }
  7255. intel_get_pipe_timings(crtc, pipe_config);
  7256. intel_get_pipe_src_size(crtc, pipe_config);
  7257. ironlake_get_pfit_config(crtc, pipe_config);
  7258. ret = true;
  7259. out:
  7260. intel_display_power_put(dev_priv, power_domain);
  7261. return ret;
  7262. }
  7263. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7264. {
  7265. struct drm_device *dev = &dev_priv->drm;
  7266. struct intel_crtc *crtc;
  7267. for_each_intel_crtc(dev, crtc)
  7268. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7269. pipe_name(crtc->pipe));
  7270. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7271. "Display power well on\n");
  7272. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7273. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7274. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7275. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7276. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7277. "CPU PWM1 enabled\n");
  7278. if (IS_HASWELL(dev_priv))
  7279. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7280. "CPU PWM2 enabled\n");
  7281. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7282. "PCH PWM1 enabled\n");
  7283. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7284. "Utility pin enabled\n");
  7285. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7286. /*
  7287. * In theory we can still leave IRQs enabled, as long as only the HPD
  7288. * interrupts remain enabled. We used to check for that, but since it's
  7289. * gen-specific and since we only disable LCPLL after we fully disable
  7290. * the interrupts, the check below should be enough.
  7291. */
  7292. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7293. }
  7294. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7295. {
  7296. if (IS_HASWELL(dev_priv))
  7297. return I915_READ(D_COMP_HSW);
  7298. else
  7299. return I915_READ(D_COMP_BDW);
  7300. }
  7301. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7302. {
  7303. if (IS_HASWELL(dev_priv)) {
  7304. mutex_lock(&dev_priv->pcu_lock);
  7305. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7306. val))
  7307. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7308. mutex_unlock(&dev_priv->pcu_lock);
  7309. } else {
  7310. I915_WRITE(D_COMP_BDW, val);
  7311. POSTING_READ(D_COMP_BDW);
  7312. }
  7313. }
  7314. /*
  7315. * This function implements pieces of two sequences from BSpec:
  7316. * - Sequence for display software to disable LCPLL
  7317. * - Sequence for display software to allow package C8+
  7318. * The steps implemented here are just the steps that actually touch the LCPLL
  7319. * register. Callers should take care of disabling all the display engine
  7320. * functions, doing the mode unset, fixing interrupts, etc.
  7321. */
  7322. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7323. bool switch_to_fclk, bool allow_power_down)
  7324. {
  7325. uint32_t val;
  7326. assert_can_disable_lcpll(dev_priv);
  7327. val = I915_READ(LCPLL_CTL);
  7328. if (switch_to_fclk) {
  7329. val |= LCPLL_CD_SOURCE_FCLK;
  7330. I915_WRITE(LCPLL_CTL, val);
  7331. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7332. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7333. DRM_ERROR("Switching to FCLK failed\n");
  7334. val = I915_READ(LCPLL_CTL);
  7335. }
  7336. val |= LCPLL_PLL_DISABLE;
  7337. I915_WRITE(LCPLL_CTL, val);
  7338. POSTING_READ(LCPLL_CTL);
  7339. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7340. DRM_ERROR("LCPLL still locked\n");
  7341. val = hsw_read_dcomp(dev_priv);
  7342. val |= D_COMP_COMP_DISABLE;
  7343. hsw_write_dcomp(dev_priv, val);
  7344. ndelay(100);
  7345. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7346. 1))
  7347. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7348. if (allow_power_down) {
  7349. val = I915_READ(LCPLL_CTL);
  7350. val |= LCPLL_POWER_DOWN_ALLOW;
  7351. I915_WRITE(LCPLL_CTL, val);
  7352. POSTING_READ(LCPLL_CTL);
  7353. }
  7354. }
  7355. /*
  7356. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7357. * source.
  7358. */
  7359. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7360. {
  7361. uint32_t val;
  7362. val = I915_READ(LCPLL_CTL);
  7363. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7364. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7365. return;
  7366. /*
  7367. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7368. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7369. */
  7370. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7371. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7372. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7373. I915_WRITE(LCPLL_CTL, val);
  7374. POSTING_READ(LCPLL_CTL);
  7375. }
  7376. val = hsw_read_dcomp(dev_priv);
  7377. val |= D_COMP_COMP_FORCE;
  7378. val &= ~D_COMP_COMP_DISABLE;
  7379. hsw_write_dcomp(dev_priv, val);
  7380. val = I915_READ(LCPLL_CTL);
  7381. val &= ~LCPLL_PLL_DISABLE;
  7382. I915_WRITE(LCPLL_CTL, val);
  7383. if (intel_wait_for_register(dev_priv,
  7384. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7385. 5))
  7386. DRM_ERROR("LCPLL not locked yet\n");
  7387. if (val & LCPLL_CD_SOURCE_FCLK) {
  7388. val = I915_READ(LCPLL_CTL);
  7389. val &= ~LCPLL_CD_SOURCE_FCLK;
  7390. I915_WRITE(LCPLL_CTL, val);
  7391. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7392. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7393. DRM_ERROR("Switching back to LCPLL failed\n");
  7394. }
  7395. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7396. intel_update_cdclk(dev_priv);
  7397. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7398. }
  7399. /*
  7400. * Package states C8 and deeper are really deep PC states that can only be
  7401. * reached when all the devices on the system allow it, so even if the graphics
  7402. * device allows PC8+, it doesn't mean the system will actually get to these
  7403. * states. Our driver only allows PC8+ when going into runtime PM.
  7404. *
  7405. * The requirements for PC8+ are that all the outputs are disabled, the power
  7406. * well is disabled and most interrupts are disabled, and these are also
  7407. * requirements for runtime PM. When these conditions are met, we manually do
  7408. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7409. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7410. * hang the machine.
  7411. *
  7412. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7413. * the state of some registers, so when we come back from PC8+ we need to
  7414. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7415. * need to take care of the registers kept by RC6. Notice that this happens even
  7416. * if we don't put the device in PCI D3 state (which is what currently happens
  7417. * because of the runtime PM support).
  7418. *
  7419. * For more, read "Display Sequences for Package C8" on the hardware
  7420. * documentation.
  7421. */
  7422. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7423. {
  7424. uint32_t val;
  7425. DRM_DEBUG_KMS("Enabling package C8+\n");
  7426. if (HAS_PCH_LPT_LP(dev_priv)) {
  7427. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7428. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7429. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7430. }
  7431. lpt_disable_clkout_dp(dev_priv);
  7432. hsw_disable_lcpll(dev_priv, true, true);
  7433. }
  7434. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7435. {
  7436. uint32_t val;
  7437. DRM_DEBUG_KMS("Disabling package C8+\n");
  7438. hsw_restore_lcpll(dev_priv);
  7439. lpt_init_pch_refclk(dev_priv);
  7440. if (HAS_PCH_LPT_LP(dev_priv)) {
  7441. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7442. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7443. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7444. }
  7445. }
  7446. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7447. struct intel_crtc_state *crtc_state)
  7448. {
  7449. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7450. struct intel_encoder *encoder =
  7451. intel_ddi_get_crtc_new_encoder(crtc_state);
  7452. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7453. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7454. pipe_name(crtc->pipe));
  7455. return -EINVAL;
  7456. }
  7457. }
  7458. return 0;
  7459. }
  7460. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7461. enum port port,
  7462. struct intel_crtc_state *pipe_config)
  7463. {
  7464. enum intel_dpll_id id;
  7465. u32 temp;
  7466. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7467. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7468. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7469. return;
  7470. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7471. }
  7472. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7473. enum port port,
  7474. struct intel_crtc_state *pipe_config)
  7475. {
  7476. enum intel_dpll_id id;
  7477. switch (port) {
  7478. case PORT_A:
  7479. id = DPLL_ID_SKL_DPLL0;
  7480. break;
  7481. case PORT_B:
  7482. id = DPLL_ID_SKL_DPLL1;
  7483. break;
  7484. case PORT_C:
  7485. id = DPLL_ID_SKL_DPLL2;
  7486. break;
  7487. default:
  7488. DRM_ERROR("Incorrect port type\n");
  7489. return;
  7490. }
  7491. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7492. }
  7493. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7494. enum port port,
  7495. struct intel_crtc_state *pipe_config)
  7496. {
  7497. enum intel_dpll_id id;
  7498. u32 temp;
  7499. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7500. id = temp >> (port * 3 + 1);
  7501. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7502. return;
  7503. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7504. }
  7505. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7506. enum port port,
  7507. struct intel_crtc_state *pipe_config)
  7508. {
  7509. enum intel_dpll_id id;
  7510. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7511. switch (ddi_pll_sel) {
  7512. case PORT_CLK_SEL_WRPLL1:
  7513. id = DPLL_ID_WRPLL1;
  7514. break;
  7515. case PORT_CLK_SEL_WRPLL2:
  7516. id = DPLL_ID_WRPLL2;
  7517. break;
  7518. case PORT_CLK_SEL_SPLL:
  7519. id = DPLL_ID_SPLL;
  7520. break;
  7521. case PORT_CLK_SEL_LCPLL_810:
  7522. id = DPLL_ID_LCPLL_810;
  7523. break;
  7524. case PORT_CLK_SEL_LCPLL_1350:
  7525. id = DPLL_ID_LCPLL_1350;
  7526. break;
  7527. case PORT_CLK_SEL_LCPLL_2700:
  7528. id = DPLL_ID_LCPLL_2700;
  7529. break;
  7530. default:
  7531. MISSING_CASE(ddi_pll_sel);
  7532. /* fall through */
  7533. case PORT_CLK_SEL_NONE:
  7534. return;
  7535. }
  7536. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7537. }
  7538. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7539. struct intel_crtc_state *pipe_config,
  7540. u64 *power_domain_mask)
  7541. {
  7542. struct drm_device *dev = crtc->base.dev;
  7543. struct drm_i915_private *dev_priv = to_i915(dev);
  7544. enum intel_display_power_domain power_domain;
  7545. u32 tmp;
  7546. /*
  7547. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7548. * transcoder handled below.
  7549. */
  7550. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7551. /*
  7552. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7553. * consistency and less surprising code; it's in always on power).
  7554. */
  7555. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7556. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7557. enum pipe trans_edp_pipe;
  7558. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7559. default:
  7560. WARN(1, "unknown pipe linked to edp transcoder\n");
  7561. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7562. case TRANS_DDI_EDP_INPUT_A_ON:
  7563. trans_edp_pipe = PIPE_A;
  7564. break;
  7565. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7566. trans_edp_pipe = PIPE_B;
  7567. break;
  7568. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7569. trans_edp_pipe = PIPE_C;
  7570. break;
  7571. }
  7572. if (trans_edp_pipe == crtc->pipe)
  7573. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7574. }
  7575. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7576. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7577. return false;
  7578. *power_domain_mask |= BIT_ULL(power_domain);
  7579. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7580. return tmp & PIPECONF_ENABLE;
  7581. }
  7582. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7583. struct intel_crtc_state *pipe_config,
  7584. u64 *power_domain_mask)
  7585. {
  7586. struct drm_device *dev = crtc->base.dev;
  7587. struct drm_i915_private *dev_priv = to_i915(dev);
  7588. enum intel_display_power_domain power_domain;
  7589. enum port port;
  7590. enum transcoder cpu_transcoder;
  7591. u32 tmp;
  7592. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7593. if (port == PORT_A)
  7594. cpu_transcoder = TRANSCODER_DSI_A;
  7595. else
  7596. cpu_transcoder = TRANSCODER_DSI_C;
  7597. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7598. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7599. continue;
  7600. *power_domain_mask |= BIT_ULL(power_domain);
  7601. /*
  7602. * The PLL needs to be enabled with a valid divider
  7603. * configuration, otherwise accessing DSI registers will hang
  7604. * the machine. See BSpec North Display Engine
  7605. * registers/MIPI[BXT]. We can break out here early, since we
  7606. * need the same DSI PLL to be enabled for both DSI ports.
  7607. */
  7608. if (!intel_dsi_pll_is_enabled(dev_priv))
  7609. break;
  7610. /* XXX: this works for video mode only */
  7611. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7612. if (!(tmp & DPI_ENABLE))
  7613. continue;
  7614. tmp = I915_READ(MIPI_CTRL(port));
  7615. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7616. continue;
  7617. pipe_config->cpu_transcoder = cpu_transcoder;
  7618. break;
  7619. }
  7620. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7621. }
  7622. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7623. struct intel_crtc_state *pipe_config)
  7624. {
  7625. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7626. struct intel_shared_dpll *pll;
  7627. enum port port;
  7628. uint32_t tmp;
  7629. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7630. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7631. if (IS_CANNONLAKE(dev_priv))
  7632. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7633. else if (IS_GEN9_BC(dev_priv))
  7634. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7635. else if (IS_GEN9_LP(dev_priv))
  7636. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7637. else
  7638. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7639. pll = pipe_config->shared_dpll;
  7640. if (pll) {
  7641. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7642. &pipe_config->dpll_hw_state));
  7643. }
  7644. /*
  7645. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7646. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7647. * the PCH transcoder is on.
  7648. */
  7649. if (INTEL_GEN(dev_priv) < 9 &&
  7650. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7651. pipe_config->has_pch_encoder = true;
  7652. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7653. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7654. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7655. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7656. }
  7657. }
  7658. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7659. struct intel_crtc_state *pipe_config)
  7660. {
  7661. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7662. enum intel_display_power_domain power_domain;
  7663. u64 power_domain_mask;
  7664. bool active;
  7665. intel_crtc_init_scalers(crtc, pipe_config);
  7666. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7667. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7668. return false;
  7669. power_domain_mask = BIT_ULL(power_domain);
  7670. pipe_config->shared_dpll = NULL;
  7671. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7672. if (IS_GEN9_LP(dev_priv) &&
  7673. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7674. WARN_ON(active);
  7675. active = true;
  7676. }
  7677. if (!active)
  7678. goto out;
  7679. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7680. haswell_get_ddi_port_state(crtc, pipe_config);
  7681. intel_get_pipe_timings(crtc, pipe_config);
  7682. }
  7683. intel_get_pipe_src_size(crtc, pipe_config);
  7684. pipe_config->gamma_mode =
  7685. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7686. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7687. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7688. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7689. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7690. bool blend_mode_420 = tmp &
  7691. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7692. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7693. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7694. pipe_config->ycbcr420 != blend_mode_420)
  7695. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7696. } else if (clrspace_yuv) {
  7697. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7698. }
  7699. }
  7700. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7701. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7702. power_domain_mask |= BIT_ULL(power_domain);
  7703. if (INTEL_GEN(dev_priv) >= 9)
  7704. skylake_get_pfit_config(crtc, pipe_config);
  7705. else
  7706. ironlake_get_pfit_config(crtc, pipe_config);
  7707. }
  7708. if (hsw_crtc_supports_ips(crtc)) {
  7709. if (IS_HASWELL(dev_priv))
  7710. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7711. else {
  7712. /*
  7713. * We cannot readout IPS state on broadwell, set to
  7714. * true so we can set it to a defined state on first
  7715. * commit.
  7716. */
  7717. pipe_config->ips_enabled = true;
  7718. }
  7719. }
  7720. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7721. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7722. pipe_config->pixel_multiplier =
  7723. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7724. } else {
  7725. pipe_config->pixel_multiplier = 1;
  7726. }
  7727. out:
  7728. for_each_power_domain(power_domain, power_domain_mask)
  7729. intel_display_power_put(dev_priv, power_domain);
  7730. return active;
  7731. }
  7732. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7733. {
  7734. struct drm_i915_private *dev_priv =
  7735. to_i915(plane_state->base.plane->dev);
  7736. const struct drm_framebuffer *fb = plane_state->base.fb;
  7737. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7738. u32 base;
  7739. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7740. base = obj->phys_handle->busaddr;
  7741. else
  7742. base = intel_plane_ggtt_offset(plane_state);
  7743. base += plane_state->main.offset;
  7744. /* ILK+ do this automagically */
  7745. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7746. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7747. base += (plane_state->base.crtc_h *
  7748. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7749. return base;
  7750. }
  7751. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7752. {
  7753. int x = plane_state->base.crtc_x;
  7754. int y = plane_state->base.crtc_y;
  7755. u32 pos = 0;
  7756. if (x < 0) {
  7757. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7758. x = -x;
  7759. }
  7760. pos |= x << CURSOR_X_SHIFT;
  7761. if (y < 0) {
  7762. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7763. y = -y;
  7764. }
  7765. pos |= y << CURSOR_Y_SHIFT;
  7766. return pos;
  7767. }
  7768. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7769. {
  7770. const struct drm_mode_config *config =
  7771. &plane_state->base.plane->dev->mode_config;
  7772. int width = plane_state->base.crtc_w;
  7773. int height = plane_state->base.crtc_h;
  7774. return width > 0 && width <= config->cursor_width &&
  7775. height > 0 && height <= config->cursor_height;
  7776. }
  7777. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7778. struct intel_plane_state *plane_state)
  7779. {
  7780. const struct drm_framebuffer *fb = plane_state->base.fb;
  7781. int src_x, src_y;
  7782. u32 offset;
  7783. int ret;
  7784. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7785. &crtc_state->base,
  7786. &plane_state->clip,
  7787. DRM_PLANE_HELPER_NO_SCALING,
  7788. DRM_PLANE_HELPER_NO_SCALING,
  7789. true, true);
  7790. if (ret)
  7791. return ret;
  7792. if (!fb)
  7793. return 0;
  7794. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7795. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7796. return -EINVAL;
  7797. }
  7798. src_x = plane_state->base.src_x >> 16;
  7799. src_y = plane_state->base.src_y >> 16;
  7800. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7801. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7802. if (src_x != 0 || src_y != 0) {
  7803. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7804. return -EINVAL;
  7805. }
  7806. plane_state->main.offset = offset;
  7807. return 0;
  7808. }
  7809. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7810. const struct intel_plane_state *plane_state)
  7811. {
  7812. const struct drm_framebuffer *fb = plane_state->base.fb;
  7813. return CURSOR_ENABLE |
  7814. CURSOR_GAMMA_ENABLE |
  7815. CURSOR_FORMAT_ARGB |
  7816. CURSOR_STRIDE(fb->pitches[0]);
  7817. }
  7818. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7819. {
  7820. int width = plane_state->base.crtc_w;
  7821. /*
  7822. * 845g/865g are only limited by the width of their cursors,
  7823. * the height is arbitrary up to the precision of the register.
  7824. */
  7825. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7826. }
  7827. static int i845_check_cursor(struct intel_plane *plane,
  7828. struct intel_crtc_state *crtc_state,
  7829. struct intel_plane_state *plane_state)
  7830. {
  7831. const struct drm_framebuffer *fb = plane_state->base.fb;
  7832. int ret;
  7833. ret = intel_check_cursor(crtc_state, plane_state);
  7834. if (ret)
  7835. return ret;
  7836. /* if we want to turn off the cursor ignore width and height */
  7837. if (!fb)
  7838. return 0;
  7839. /* Check for which cursor types we support */
  7840. if (!i845_cursor_size_ok(plane_state)) {
  7841. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7842. plane_state->base.crtc_w,
  7843. plane_state->base.crtc_h);
  7844. return -EINVAL;
  7845. }
  7846. switch (fb->pitches[0]) {
  7847. case 256:
  7848. case 512:
  7849. case 1024:
  7850. case 2048:
  7851. break;
  7852. default:
  7853. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7854. fb->pitches[0]);
  7855. return -EINVAL;
  7856. }
  7857. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7858. return 0;
  7859. }
  7860. static void i845_update_cursor(struct intel_plane *plane,
  7861. const struct intel_crtc_state *crtc_state,
  7862. const struct intel_plane_state *plane_state)
  7863. {
  7864. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7865. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7866. unsigned long irqflags;
  7867. if (plane_state && plane_state->base.visible) {
  7868. unsigned int width = plane_state->base.crtc_w;
  7869. unsigned int height = plane_state->base.crtc_h;
  7870. cntl = plane_state->ctl;
  7871. size = (height << 12) | width;
  7872. base = intel_cursor_base(plane_state);
  7873. pos = intel_cursor_position(plane_state);
  7874. }
  7875. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7876. /* On these chipsets we can only modify the base/size/stride
  7877. * whilst the cursor is disabled.
  7878. */
  7879. if (plane->cursor.base != base ||
  7880. plane->cursor.size != size ||
  7881. plane->cursor.cntl != cntl) {
  7882. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7883. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7884. I915_WRITE_FW(CURSIZE, size);
  7885. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7886. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7887. plane->cursor.base = base;
  7888. plane->cursor.size = size;
  7889. plane->cursor.cntl = cntl;
  7890. } else {
  7891. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7892. }
  7893. POSTING_READ_FW(CURCNTR(PIPE_A));
  7894. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7895. }
  7896. static void i845_disable_cursor(struct intel_plane *plane,
  7897. struct intel_crtc *crtc)
  7898. {
  7899. i845_update_cursor(plane, NULL, NULL);
  7900. }
  7901. static bool i845_cursor_get_hw_state(struct intel_plane *plane)
  7902. {
  7903. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7904. enum intel_display_power_domain power_domain;
  7905. bool ret;
  7906. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  7907. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7908. return false;
  7909. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  7910. intel_display_power_put(dev_priv, power_domain);
  7911. return ret;
  7912. }
  7913. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7914. const struct intel_plane_state *plane_state)
  7915. {
  7916. struct drm_i915_private *dev_priv =
  7917. to_i915(plane_state->base.plane->dev);
  7918. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7919. u32 cntl;
  7920. cntl = MCURSOR_GAMMA_ENABLE;
  7921. if (HAS_DDI(dev_priv))
  7922. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7923. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7924. switch (plane_state->base.crtc_w) {
  7925. case 64:
  7926. cntl |= CURSOR_MODE_64_ARGB_AX;
  7927. break;
  7928. case 128:
  7929. cntl |= CURSOR_MODE_128_ARGB_AX;
  7930. break;
  7931. case 256:
  7932. cntl |= CURSOR_MODE_256_ARGB_AX;
  7933. break;
  7934. default:
  7935. MISSING_CASE(plane_state->base.crtc_w);
  7936. return 0;
  7937. }
  7938. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7939. cntl |= CURSOR_ROTATE_180;
  7940. return cntl;
  7941. }
  7942. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  7943. {
  7944. struct drm_i915_private *dev_priv =
  7945. to_i915(plane_state->base.plane->dev);
  7946. int width = plane_state->base.crtc_w;
  7947. int height = plane_state->base.crtc_h;
  7948. if (!intel_cursor_size_ok(plane_state))
  7949. return false;
  7950. /* Cursor width is limited to a few power-of-two sizes */
  7951. switch (width) {
  7952. case 256:
  7953. case 128:
  7954. case 64:
  7955. break;
  7956. default:
  7957. return false;
  7958. }
  7959. /*
  7960. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  7961. * height from 8 lines up to the cursor width, when the
  7962. * cursor is not rotated. Everything else requires square
  7963. * cursors.
  7964. */
  7965. if (HAS_CUR_FBC(dev_priv) &&
  7966. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  7967. if (height < 8 || height > width)
  7968. return false;
  7969. } else {
  7970. if (height != width)
  7971. return false;
  7972. }
  7973. return true;
  7974. }
  7975. static int i9xx_check_cursor(struct intel_plane *plane,
  7976. struct intel_crtc_state *crtc_state,
  7977. struct intel_plane_state *plane_state)
  7978. {
  7979. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7980. const struct drm_framebuffer *fb = plane_state->base.fb;
  7981. enum pipe pipe = plane->pipe;
  7982. int ret;
  7983. ret = intel_check_cursor(crtc_state, plane_state);
  7984. if (ret)
  7985. return ret;
  7986. /* if we want to turn off the cursor ignore width and height */
  7987. if (!fb)
  7988. return 0;
  7989. /* Check for which cursor types we support */
  7990. if (!i9xx_cursor_size_ok(plane_state)) {
  7991. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7992. plane_state->base.crtc_w,
  7993. plane_state->base.crtc_h);
  7994. return -EINVAL;
  7995. }
  7996. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  7997. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  7998. fb->pitches[0], plane_state->base.crtc_w);
  7999. return -EINVAL;
  8000. }
  8001. /*
  8002. * There's something wrong with the cursor on CHV pipe C.
  8003. * If it straddles the left edge of the screen then
  8004. * moving it away from the edge or disabling it often
  8005. * results in a pipe underrun, and often that can lead to
  8006. * dead pipe (constant underrun reported, and it scans
  8007. * out just a solid color). To recover from that, the
  8008. * display power well must be turned off and on again.
  8009. * Refuse the put the cursor into that compromised position.
  8010. */
  8011. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8012. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8013. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8014. return -EINVAL;
  8015. }
  8016. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8017. return 0;
  8018. }
  8019. static void i9xx_update_cursor(struct intel_plane *plane,
  8020. const struct intel_crtc_state *crtc_state,
  8021. const struct intel_plane_state *plane_state)
  8022. {
  8023. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8024. enum pipe pipe = plane->pipe;
  8025. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8026. unsigned long irqflags;
  8027. if (plane_state && plane_state->base.visible) {
  8028. cntl = plane_state->ctl;
  8029. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8030. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8031. base = intel_cursor_base(plane_state);
  8032. pos = intel_cursor_position(plane_state);
  8033. }
  8034. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8035. /*
  8036. * On some platforms writing CURCNTR first will also
  8037. * cause CURPOS to be armed by the CURBASE write.
  8038. * Without the CURCNTR write the CURPOS write would
  8039. * arm itself. Thus we always start the full update
  8040. * with a CURCNTR write.
  8041. *
  8042. * On other platforms CURPOS always requires the
  8043. * CURBASE write to arm the update. Additonally
  8044. * a write to any of the cursor register will cancel
  8045. * an already armed cursor update. Thus leaving out
  8046. * the CURBASE write after CURPOS could lead to a
  8047. * cursor that doesn't appear to move, or even change
  8048. * shape. Thus we always write CURBASE.
  8049. *
  8050. * CURCNTR and CUR_FBC_CTL are always
  8051. * armed by the CURBASE write only.
  8052. */
  8053. if (plane->cursor.base != base ||
  8054. plane->cursor.size != fbc_ctl ||
  8055. plane->cursor.cntl != cntl) {
  8056. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8057. if (HAS_CUR_FBC(dev_priv))
  8058. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8059. I915_WRITE_FW(CURPOS(pipe), pos);
  8060. I915_WRITE_FW(CURBASE(pipe), base);
  8061. plane->cursor.base = base;
  8062. plane->cursor.size = fbc_ctl;
  8063. plane->cursor.cntl = cntl;
  8064. } else {
  8065. I915_WRITE_FW(CURPOS(pipe), pos);
  8066. I915_WRITE_FW(CURBASE(pipe), base);
  8067. }
  8068. POSTING_READ_FW(CURBASE(pipe));
  8069. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8070. }
  8071. static void i9xx_disable_cursor(struct intel_plane *plane,
  8072. struct intel_crtc *crtc)
  8073. {
  8074. i9xx_update_cursor(plane, NULL, NULL);
  8075. }
  8076. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
  8077. {
  8078. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8079. enum intel_display_power_domain power_domain;
  8080. enum pipe pipe = plane->pipe;
  8081. bool ret;
  8082. /*
  8083. * Not 100% correct for planes that can move between pipes,
  8084. * but that's only the case for gen2-3 which don't have any
  8085. * display power wells.
  8086. */
  8087. power_domain = POWER_DOMAIN_PIPE(pipe);
  8088. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8089. return false;
  8090. ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  8091. intel_display_power_put(dev_priv, power_domain);
  8092. return ret;
  8093. }
  8094. /* VESA 640x480x72Hz mode to set on the pipe */
  8095. static const struct drm_display_mode load_detect_mode = {
  8096. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8097. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8098. };
  8099. struct drm_framebuffer *
  8100. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8101. struct drm_mode_fb_cmd2 *mode_cmd)
  8102. {
  8103. struct intel_framebuffer *intel_fb;
  8104. int ret;
  8105. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8106. if (!intel_fb)
  8107. return ERR_PTR(-ENOMEM);
  8108. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8109. if (ret)
  8110. goto err;
  8111. return &intel_fb->base;
  8112. err:
  8113. kfree(intel_fb);
  8114. return ERR_PTR(ret);
  8115. }
  8116. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8117. struct drm_crtc *crtc)
  8118. {
  8119. struct drm_plane *plane;
  8120. struct drm_plane_state *plane_state;
  8121. int ret, i;
  8122. ret = drm_atomic_add_affected_planes(state, crtc);
  8123. if (ret)
  8124. return ret;
  8125. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8126. if (plane_state->crtc != crtc)
  8127. continue;
  8128. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8129. if (ret)
  8130. return ret;
  8131. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8132. }
  8133. return 0;
  8134. }
  8135. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8136. const struct drm_display_mode *mode,
  8137. struct intel_load_detect_pipe *old,
  8138. struct drm_modeset_acquire_ctx *ctx)
  8139. {
  8140. struct intel_crtc *intel_crtc;
  8141. struct intel_encoder *intel_encoder =
  8142. intel_attached_encoder(connector);
  8143. struct drm_crtc *possible_crtc;
  8144. struct drm_encoder *encoder = &intel_encoder->base;
  8145. struct drm_crtc *crtc = NULL;
  8146. struct drm_device *dev = encoder->dev;
  8147. struct drm_i915_private *dev_priv = to_i915(dev);
  8148. struct drm_mode_config *config = &dev->mode_config;
  8149. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8150. struct drm_connector_state *connector_state;
  8151. struct intel_crtc_state *crtc_state;
  8152. int ret, i = -1;
  8153. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8154. connector->base.id, connector->name,
  8155. encoder->base.id, encoder->name);
  8156. old->restore_state = NULL;
  8157. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8158. /*
  8159. * Algorithm gets a little messy:
  8160. *
  8161. * - if the connector already has an assigned crtc, use it (but make
  8162. * sure it's on first)
  8163. *
  8164. * - try to find the first unused crtc that can drive this connector,
  8165. * and use that if we find one
  8166. */
  8167. /* See if we already have a CRTC for this connector */
  8168. if (connector->state->crtc) {
  8169. crtc = connector->state->crtc;
  8170. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8171. if (ret)
  8172. goto fail;
  8173. /* Make sure the crtc and connector are running */
  8174. goto found;
  8175. }
  8176. /* Find an unused one (if possible) */
  8177. for_each_crtc(dev, possible_crtc) {
  8178. i++;
  8179. if (!(encoder->possible_crtcs & (1 << i)))
  8180. continue;
  8181. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8182. if (ret)
  8183. goto fail;
  8184. if (possible_crtc->state->enable) {
  8185. drm_modeset_unlock(&possible_crtc->mutex);
  8186. continue;
  8187. }
  8188. crtc = possible_crtc;
  8189. break;
  8190. }
  8191. /*
  8192. * If we didn't find an unused CRTC, don't use any.
  8193. */
  8194. if (!crtc) {
  8195. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8196. ret = -ENODEV;
  8197. goto fail;
  8198. }
  8199. found:
  8200. intel_crtc = to_intel_crtc(crtc);
  8201. state = drm_atomic_state_alloc(dev);
  8202. restore_state = drm_atomic_state_alloc(dev);
  8203. if (!state || !restore_state) {
  8204. ret = -ENOMEM;
  8205. goto fail;
  8206. }
  8207. state->acquire_ctx = ctx;
  8208. restore_state->acquire_ctx = ctx;
  8209. connector_state = drm_atomic_get_connector_state(state, connector);
  8210. if (IS_ERR(connector_state)) {
  8211. ret = PTR_ERR(connector_state);
  8212. goto fail;
  8213. }
  8214. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8215. if (ret)
  8216. goto fail;
  8217. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8218. if (IS_ERR(crtc_state)) {
  8219. ret = PTR_ERR(crtc_state);
  8220. goto fail;
  8221. }
  8222. crtc_state->base.active = crtc_state->base.enable = true;
  8223. if (!mode)
  8224. mode = &load_detect_mode;
  8225. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8226. if (ret)
  8227. goto fail;
  8228. ret = intel_modeset_disable_planes(state, crtc);
  8229. if (ret)
  8230. goto fail;
  8231. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8232. if (!ret)
  8233. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8234. if (ret) {
  8235. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8236. goto fail;
  8237. }
  8238. ret = drm_atomic_commit(state);
  8239. if (ret) {
  8240. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8241. goto fail;
  8242. }
  8243. old->restore_state = restore_state;
  8244. drm_atomic_state_put(state);
  8245. /* let the connector get through one full cycle before testing */
  8246. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8247. return true;
  8248. fail:
  8249. if (state) {
  8250. drm_atomic_state_put(state);
  8251. state = NULL;
  8252. }
  8253. if (restore_state) {
  8254. drm_atomic_state_put(restore_state);
  8255. restore_state = NULL;
  8256. }
  8257. if (ret == -EDEADLK)
  8258. return ret;
  8259. return false;
  8260. }
  8261. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8262. struct intel_load_detect_pipe *old,
  8263. struct drm_modeset_acquire_ctx *ctx)
  8264. {
  8265. struct intel_encoder *intel_encoder =
  8266. intel_attached_encoder(connector);
  8267. struct drm_encoder *encoder = &intel_encoder->base;
  8268. struct drm_atomic_state *state = old->restore_state;
  8269. int ret;
  8270. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8271. connector->base.id, connector->name,
  8272. encoder->base.id, encoder->name);
  8273. if (!state)
  8274. return;
  8275. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8276. if (ret)
  8277. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8278. drm_atomic_state_put(state);
  8279. }
  8280. static int i9xx_pll_refclk(struct drm_device *dev,
  8281. const struct intel_crtc_state *pipe_config)
  8282. {
  8283. struct drm_i915_private *dev_priv = to_i915(dev);
  8284. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8285. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8286. return dev_priv->vbt.lvds_ssc_freq;
  8287. else if (HAS_PCH_SPLIT(dev_priv))
  8288. return 120000;
  8289. else if (!IS_GEN2(dev_priv))
  8290. return 96000;
  8291. else
  8292. return 48000;
  8293. }
  8294. /* Returns the clock of the currently programmed mode of the given pipe. */
  8295. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8296. struct intel_crtc_state *pipe_config)
  8297. {
  8298. struct drm_device *dev = crtc->base.dev;
  8299. struct drm_i915_private *dev_priv = to_i915(dev);
  8300. int pipe = pipe_config->cpu_transcoder;
  8301. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8302. u32 fp;
  8303. struct dpll clock;
  8304. int port_clock;
  8305. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8306. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8307. fp = pipe_config->dpll_hw_state.fp0;
  8308. else
  8309. fp = pipe_config->dpll_hw_state.fp1;
  8310. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8311. if (IS_PINEVIEW(dev_priv)) {
  8312. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8313. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8314. } else {
  8315. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8316. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8317. }
  8318. if (!IS_GEN2(dev_priv)) {
  8319. if (IS_PINEVIEW(dev_priv))
  8320. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8321. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8322. else
  8323. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8324. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8325. switch (dpll & DPLL_MODE_MASK) {
  8326. case DPLLB_MODE_DAC_SERIAL:
  8327. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8328. 5 : 10;
  8329. break;
  8330. case DPLLB_MODE_LVDS:
  8331. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8332. 7 : 14;
  8333. break;
  8334. default:
  8335. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8336. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8337. return;
  8338. }
  8339. if (IS_PINEVIEW(dev_priv))
  8340. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8341. else
  8342. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8343. } else {
  8344. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8345. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8346. if (is_lvds) {
  8347. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8348. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8349. if (lvds & LVDS_CLKB_POWER_UP)
  8350. clock.p2 = 7;
  8351. else
  8352. clock.p2 = 14;
  8353. } else {
  8354. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8355. clock.p1 = 2;
  8356. else {
  8357. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8358. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8359. }
  8360. if (dpll & PLL_P2_DIVIDE_BY_4)
  8361. clock.p2 = 4;
  8362. else
  8363. clock.p2 = 2;
  8364. }
  8365. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8366. }
  8367. /*
  8368. * This value includes pixel_multiplier. We will use
  8369. * port_clock to compute adjusted_mode.crtc_clock in the
  8370. * encoder's get_config() function.
  8371. */
  8372. pipe_config->port_clock = port_clock;
  8373. }
  8374. int intel_dotclock_calculate(int link_freq,
  8375. const struct intel_link_m_n *m_n)
  8376. {
  8377. /*
  8378. * The calculation for the data clock is:
  8379. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8380. * But we want to avoid losing precison if possible, so:
  8381. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8382. *
  8383. * and the link clock is simpler:
  8384. * link_clock = (m * link_clock) / n
  8385. */
  8386. if (!m_n->link_n)
  8387. return 0;
  8388. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8389. }
  8390. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8391. struct intel_crtc_state *pipe_config)
  8392. {
  8393. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8394. /* read out port_clock from the DPLL */
  8395. i9xx_crtc_clock_get(crtc, pipe_config);
  8396. /*
  8397. * In case there is an active pipe without active ports,
  8398. * we may need some idea for the dotclock anyway.
  8399. * Calculate one based on the FDI configuration.
  8400. */
  8401. pipe_config->base.adjusted_mode.crtc_clock =
  8402. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8403. &pipe_config->fdi_m_n);
  8404. }
  8405. /* Returns the currently programmed mode of the given encoder. */
  8406. struct drm_display_mode *
  8407. intel_encoder_current_mode(struct intel_encoder *encoder)
  8408. {
  8409. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8410. struct intel_crtc_state *crtc_state;
  8411. struct drm_display_mode *mode;
  8412. struct intel_crtc *crtc;
  8413. enum pipe pipe;
  8414. if (!encoder->get_hw_state(encoder, &pipe))
  8415. return NULL;
  8416. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8417. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8418. if (!mode)
  8419. return NULL;
  8420. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8421. if (!crtc_state) {
  8422. kfree(mode);
  8423. return NULL;
  8424. }
  8425. crtc_state->base.crtc = &crtc->base;
  8426. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8427. kfree(crtc_state);
  8428. kfree(mode);
  8429. return NULL;
  8430. }
  8431. encoder->get_config(encoder, crtc_state);
  8432. intel_mode_from_pipe_config(mode, crtc_state);
  8433. kfree(crtc_state);
  8434. return mode;
  8435. }
  8436. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8437. {
  8438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8439. drm_crtc_cleanup(crtc);
  8440. kfree(intel_crtc);
  8441. }
  8442. /**
  8443. * intel_wm_need_update - Check whether watermarks need updating
  8444. * @plane: drm plane
  8445. * @state: new plane state
  8446. *
  8447. * Check current plane state versus the new one to determine whether
  8448. * watermarks need to be recalculated.
  8449. *
  8450. * Returns true or false.
  8451. */
  8452. static bool intel_wm_need_update(struct drm_plane *plane,
  8453. struct drm_plane_state *state)
  8454. {
  8455. struct intel_plane_state *new = to_intel_plane_state(state);
  8456. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8457. /* Update watermarks on tiling or size changes. */
  8458. if (new->base.visible != cur->base.visible)
  8459. return true;
  8460. if (!cur->base.fb || !new->base.fb)
  8461. return false;
  8462. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8463. cur->base.rotation != new->base.rotation ||
  8464. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8465. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8466. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8467. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8468. return true;
  8469. return false;
  8470. }
  8471. static bool needs_scaling(const struct intel_plane_state *state)
  8472. {
  8473. int src_w = drm_rect_width(&state->base.src) >> 16;
  8474. int src_h = drm_rect_height(&state->base.src) >> 16;
  8475. int dst_w = drm_rect_width(&state->base.dst);
  8476. int dst_h = drm_rect_height(&state->base.dst);
  8477. return (src_w != dst_w || src_h != dst_h);
  8478. }
  8479. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8480. struct drm_crtc_state *crtc_state,
  8481. const struct intel_plane_state *old_plane_state,
  8482. struct drm_plane_state *plane_state)
  8483. {
  8484. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8485. struct drm_crtc *crtc = crtc_state->crtc;
  8486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8487. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8488. struct drm_device *dev = crtc->dev;
  8489. struct drm_i915_private *dev_priv = to_i915(dev);
  8490. bool mode_changed = needs_modeset(crtc_state);
  8491. bool was_crtc_enabled = old_crtc_state->base.active;
  8492. bool is_crtc_enabled = crtc_state->active;
  8493. bool turn_off, turn_on, visible, was_visible;
  8494. struct drm_framebuffer *fb = plane_state->fb;
  8495. int ret;
  8496. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8497. ret = skl_update_scaler_plane(
  8498. to_intel_crtc_state(crtc_state),
  8499. to_intel_plane_state(plane_state));
  8500. if (ret)
  8501. return ret;
  8502. }
  8503. was_visible = old_plane_state->base.visible;
  8504. visible = plane_state->visible;
  8505. if (!was_crtc_enabled && WARN_ON(was_visible))
  8506. was_visible = false;
  8507. /*
  8508. * Visibility is calculated as if the crtc was on, but
  8509. * after scaler setup everything depends on it being off
  8510. * when the crtc isn't active.
  8511. *
  8512. * FIXME this is wrong for watermarks. Watermarks should also
  8513. * be computed as if the pipe would be active. Perhaps move
  8514. * per-plane wm computation to the .check_plane() hook, and
  8515. * only combine the results from all planes in the current place?
  8516. */
  8517. if (!is_crtc_enabled) {
  8518. plane_state->visible = visible = false;
  8519. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8520. }
  8521. if (!was_visible && !visible)
  8522. return 0;
  8523. if (fb != old_plane_state->base.fb)
  8524. pipe_config->fb_changed = true;
  8525. turn_off = was_visible && (!visible || mode_changed);
  8526. turn_on = visible && (!was_visible || mode_changed);
  8527. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8528. intel_crtc->base.base.id, intel_crtc->base.name,
  8529. plane->base.base.id, plane->base.name,
  8530. fb ? fb->base.id : -1);
  8531. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8532. plane->base.base.id, plane->base.name,
  8533. was_visible, visible,
  8534. turn_off, turn_on, mode_changed);
  8535. if (turn_on) {
  8536. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8537. pipe_config->update_wm_pre = true;
  8538. /* must disable cxsr around plane enable/disable */
  8539. if (plane->id != PLANE_CURSOR)
  8540. pipe_config->disable_cxsr = true;
  8541. } else if (turn_off) {
  8542. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8543. pipe_config->update_wm_post = true;
  8544. /* must disable cxsr around plane enable/disable */
  8545. if (plane->id != PLANE_CURSOR)
  8546. pipe_config->disable_cxsr = true;
  8547. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8548. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8549. /* FIXME bollocks */
  8550. pipe_config->update_wm_pre = true;
  8551. pipe_config->update_wm_post = true;
  8552. }
  8553. }
  8554. if (visible || was_visible)
  8555. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8556. /*
  8557. * WaCxSRDisabledForSpriteScaling:ivb
  8558. *
  8559. * cstate->update_wm was already set above, so this flag will
  8560. * take effect when we commit and program watermarks.
  8561. */
  8562. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8563. needs_scaling(to_intel_plane_state(plane_state)) &&
  8564. !needs_scaling(old_plane_state))
  8565. pipe_config->disable_lp_wm = true;
  8566. return 0;
  8567. }
  8568. static bool encoders_cloneable(const struct intel_encoder *a,
  8569. const struct intel_encoder *b)
  8570. {
  8571. /* masks could be asymmetric, so check both ways */
  8572. return a == b || (a->cloneable & (1 << b->type) &&
  8573. b->cloneable & (1 << a->type));
  8574. }
  8575. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8576. struct intel_crtc *crtc,
  8577. struct intel_encoder *encoder)
  8578. {
  8579. struct intel_encoder *source_encoder;
  8580. struct drm_connector *connector;
  8581. struct drm_connector_state *connector_state;
  8582. int i;
  8583. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8584. if (connector_state->crtc != &crtc->base)
  8585. continue;
  8586. source_encoder =
  8587. to_intel_encoder(connector_state->best_encoder);
  8588. if (!encoders_cloneable(encoder, source_encoder))
  8589. return false;
  8590. }
  8591. return true;
  8592. }
  8593. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8594. struct drm_crtc_state *crtc_state)
  8595. {
  8596. struct drm_device *dev = crtc->dev;
  8597. struct drm_i915_private *dev_priv = to_i915(dev);
  8598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8599. struct intel_crtc_state *pipe_config =
  8600. to_intel_crtc_state(crtc_state);
  8601. struct drm_atomic_state *state = crtc_state->state;
  8602. int ret;
  8603. bool mode_changed = needs_modeset(crtc_state);
  8604. if (mode_changed && !crtc_state->active)
  8605. pipe_config->update_wm_post = true;
  8606. if (mode_changed && crtc_state->enable &&
  8607. dev_priv->display.crtc_compute_clock &&
  8608. !WARN_ON(pipe_config->shared_dpll)) {
  8609. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8610. pipe_config);
  8611. if (ret)
  8612. return ret;
  8613. }
  8614. if (crtc_state->color_mgmt_changed) {
  8615. ret = intel_color_check(crtc, crtc_state);
  8616. if (ret)
  8617. return ret;
  8618. /*
  8619. * Changing color management on Intel hardware is
  8620. * handled as part of planes update.
  8621. */
  8622. crtc_state->planes_changed = true;
  8623. }
  8624. ret = 0;
  8625. if (dev_priv->display.compute_pipe_wm) {
  8626. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8627. if (ret) {
  8628. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8629. return ret;
  8630. }
  8631. }
  8632. if (dev_priv->display.compute_intermediate_wm &&
  8633. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8634. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8635. return 0;
  8636. /*
  8637. * Calculate 'intermediate' watermarks that satisfy both the
  8638. * old state and the new state. We can program these
  8639. * immediately.
  8640. */
  8641. ret = dev_priv->display.compute_intermediate_wm(dev,
  8642. intel_crtc,
  8643. pipe_config);
  8644. if (ret) {
  8645. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8646. return ret;
  8647. }
  8648. } else if (dev_priv->display.compute_intermediate_wm) {
  8649. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8650. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8651. }
  8652. if (INTEL_GEN(dev_priv) >= 9) {
  8653. if (mode_changed)
  8654. ret = skl_update_scaler_crtc(pipe_config);
  8655. if (!ret)
  8656. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8657. pipe_config);
  8658. if (!ret)
  8659. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8660. pipe_config);
  8661. }
  8662. if (HAS_IPS(dev_priv))
  8663. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8664. return ret;
  8665. }
  8666. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8667. .atomic_begin = intel_begin_crtc_commit,
  8668. .atomic_flush = intel_finish_crtc_commit,
  8669. .atomic_check = intel_crtc_atomic_check,
  8670. };
  8671. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8672. {
  8673. struct intel_connector *connector;
  8674. struct drm_connector_list_iter conn_iter;
  8675. drm_connector_list_iter_begin(dev, &conn_iter);
  8676. for_each_intel_connector_iter(connector, &conn_iter) {
  8677. if (connector->base.state->crtc)
  8678. drm_connector_unreference(&connector->base);
  8679. if (connector->base.encoder) {
  8680. connector->base.state->best_encoder =
  8681. connector->base.encoder;
  8682. connector->base.state->crtc =
  8683. connector->base.encoder->crtc;
  8684. drm_connector_reference(&connector->base);
  8685. } else {
  8686. connector->base.state->best_encoder = NULL;
  8687. connector->base.state->crtc = NULL;
  8688. }
  8689. }
  8690. drm_connector_list_iter_end(&conn_iter);
  8691. }
  8692. static void
  8693. connected_sink_compute_bpp(struct intel_connector *connector,
  8694. struct intel_crtc_state *pipe_config)
  8695. {
  8696. const struct drm_display_info *info = &connector->base.display_info;
  8697. int bpp = pipe_config->pipe_bpp;
  8698. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8699. connector->base.base.id,
  8700. connector->base.name);
  8701. /* Don't use an invalid EDID bpc value */
  8702. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8703. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8704. bpp, info->bpc * 3);
  8705. pipe_config->pipe_bpp = info->bpc * 3;
  8706. }
  8707. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8708. if (info->bpc == 0 && bpp > 24) {
  8709. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8710. bpp);
  8711. pipe_config->pipe_bpp = 24;
  8712. }
  8713. }
  8714. static int
  8715. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8716. struct intel_crtc_state *pipe_config)
  8717. {
  8718. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8719. struct drm_atomic_state *state;
  8720. struct drm_connector *connector;
  8721. struct drm_connector_state *connector_state;
  8722. int bpp, i;
  8723. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8724. IS_CHERRYVIEW(dev_priv)))
  8725. bpp = 10*3;
  8726. else if (INTEL_GEN(dev_priv) >= 5)
  8727. bpp = 12*3;
  8728. else
  8729. bpp = 8*3;
  8730. pipe_config->pipe_bpp = bpp;
  8731. state = pipe_config->base.state;
  8732. /* Clamp display bpp to EDID value */
  8733. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8734. if (connector_state->crtc != &crtc->base)
  8735. continue;
  8736. connected_sink_compute_bpp(to_intel_connector(connector),
  8737. pipe_config);
  8738. }
  8739. return bpp;
  8740. }
  8741. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8742. {
  8743. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8744. "type: 0x%x flags: 0x%x\n",
  8745. mode->crtc_clock,
  8746. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8747. mode->crtc_hsync_end, mode->crtc_htotal,
  8748. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8749. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8750. }
  8751. static inline void
  8752. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8753. unsigned int lane_count, struct intel_link_m_n *m_n)
  8754. {
  8755. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8756. id, lane_count,
  8757. m_n->gmch_m, m_n->gmch_n,
  8758. m_n->link_m, m_n->link_n, m_n->tu);
  8759. }
  8760. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8761. static const char * const output_type_str[] = {
  8762. OUTPUT_TYPE(UNUSED),
  8763. OUTPUT_TYPE(ANALOG),
  8764. OUTPUT_TYPE(DVO),
  8765. OUTPUT_TYPE(SDVO),
  8766. OUTPUT_TYPE(LVDS),
  8767. OUTPUT_TYPE(TVOUT),
  8768. OUTPUT_TYPE(HDMI),
  8769. OUTPUT_TYPE(DP),
  8770. OUTPUT_TYPE(EDP),
  8771. OUTPUT_TYPE(DSI),
  8772. OUTPUT_TYPE(DDI),
  8773. OUTPUT_TYPE(DP_MST),
  8774. };
  8775. #undef OUTPUT_TYPE
  8776. static void snprintf_output_types(char *buf, size_t len,
  8777. unsigned int output_types)
  8778. {
  8779. char *str = buf;
  8780. int i;
  8781. str[0] = '\0';
  8782. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8783. int r;
  8784. if ((output_types & BIT(i)) == 0)
  8785. continue;
  8786. r = snprintf(str, len, "%s%s",
  8787. str != buf ? "," : "", output_type_str[i]);
  8788. if (r >= len)
  8789. break;
  8790. str += r;
  8791. len -= r;
  8792. output_types &= ~BIT(i);
  8793. }
  8794. WARN_ON_ONCE(output_types != 0);
  8795. }
  8796. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8797. struct intel_crtc_state *pipe_config,
  8798. const char *context)
  8799. {
  8800. struct drm_device *dev = crtc->base.dev;
  8801. struct drm_i915_private *dev_priv = to_i915(dev);
  8802. struct drm_plane *plane;
  8803. struct intel_plane *intel_plane;
  8804. struct intel_plane_state *state;
  8805. struct drm_framebuffer *fb;
  8806. char buf[64];
  8807. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8808. crtc->base.base.id, crtc->base.name, context);
  8809. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  8810. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  8811. buf, pipe_config->output_types);
  8812. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8813. transcoder_name(pipe_config->cpu_transcoder),
  8814. pipe_config->pipe_bpp, pipe_config->dither);
  8815. if (pipe_config->has_pch_encoder)
  8816. intel_dump_m_n_config(pipe_config, "fdi",
  8817. pipe_config->fdi_lanes,
  8818. &pipe_config->fdi_m_n);
  8819. if (pipe_config->ycbcr420)
  8820. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8821. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8822. intel_dump_m_n_config(pipe_config, "dp m_n",
  8823. pipe_config->lane_count, &pipe_config->dp_m_n);
  8824. if (pipe_config->has_drrs)
  8825. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8826. pipe_config->lane_count,
  8827. &pipe_config->dp_m2_n2);
  8828. }
  8829. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8830. pipe_config->has_audio, pipe_config->has_infoframe);
  8831. DRM_DEBUG_KMS("requested mode:\n");
  8832. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8833. DRM_DEBUG_KMS("adjusted mode:\n");
  8834. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8835. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8836. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8837. pipe_config->port_clock,
  8838. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8839. pipe_config->pixel_rate);
  8840. if (INTEL_GEN(dev_priv) >= 9)
  8841. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8842. crtc->num_scalers,
  8843. pipe_config->scaler_state.scaler_users,
  8844. pipe_config->scaler_state.scaler_id);
  8845. if (HAS_GMCH_DISPLAY(dev_priv))
  8846. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8847. pipe_config->gmch_pfit.control,
  8848. pipe_config->gmch_pfit.pgm_ratios,
  8849. pipe_config->gmch_pfit.lvds_border_bits);
  8850. else
  8851. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8852. pipe_config->pch_pfit.pos,
  8853. pipe_config->pch_pfit.size,
  8854. enableddisabled(pipe_config->pch_pfit.enabled));
  8855. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8856. pipe_config->ips_enabled, pipe_config->double_wide);
  8857. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8858. DRM_DEBUG_KMS("planes on this crtc\n");
  8859. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8860. struct drm_format_name_buf format_name;
  8861. intel_plane = to_intel_plane(plane);
  8862. if (intel_plane->pipe != crtc->pipe)
  8863. continue;
  8864. state = to_intel_plane_state(plane->state);
  8865. fb = state->base.fb;
  8866. if (!fb) {
  8867. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  8868. plane->base.id, plane->name, state->scaler_id);
  8869. continue;
  8870. }
  8871. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  8872. plane->base.id, plane->name,
  8873. fb->base.id, fb->width, fb->height,
  8874. drm_get_format_name(fb->format->format, &format_name));
  8875. if (INTEL_GEN(dev_priv) >= 9)
  8876. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  8877. state->scaler_id,
  8878. state->base.src.x1 >> 16,
  8879. state->base.src.y1 >> 16,
  8880. drm_rect_width(&state->base.src) >> 16,
  8881. drm_rect_height(&state->base.src) >> 16,
  8882. state->base.dst.x1, state->base.dst.y1,
  8883. drm_rect_width(&state->base.dst),
  8884. drm_rect_height(&state->base.dst));
  8885. }
  8886. }
  8887. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  8888. {
  8889. struct drm_device *dev = state->dev;
  8890. struct drm_connector *connector;
  8891. struct drm_connector_list_iter conn_iter;
  8892. unsigned int used_ports = 0;
  8893. unsigned int used_mst_ports = 0;
  8894. /*
  8895. * Walk the connector list instead of the encoder
  8896. * list to detect the problem on ddi platforms
  8897. * where there's just one encoder per digital port.
  8898. */
  8899. drm_connector_list_iter_begin(dev, &conn_iter);
  8900. drm_for_each_connector_iter(connector, &conn_iter) {
  8901. struct drm_connector_state *connector_state;
  8902. struct intel_encoder *encoder;
  8903. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  8904. if (!connector_state)
  8905. connector_state = connector->state;
  8906. if (!connector_state->best_encoder)
  8907. continue;
  8908. encoder = to_intel_encoder(connector_state->best_encoder);
  8909. WARN_ON(!connector_state->crtc);
  8910. switch (encoder->type) {
  8911. unsigned int port_mask;
  8912. case INTEL_OUTPUT_DDI:
  8913. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  8914. break;
  8915. case INTEL_OUTPUT_DP:
  8916. case INTEL_OUTPUT_HDMI:
  8917. case INTEL_OUTPUT_EDP:
  8918. port_mask = 1 << encoder->port;
  8919. /* the same port mustn't appear more than once */
  8920. if (used_ports & port_mask)
  8921. return false;
  8922. used_ports |= port_mask;
  8923. break;
  8924. case INTEL_OUTPUT_DP_MST:
  8925. used_mst_ports |=
  8926. 1 << encoder->port;
  8927. break;
  8928. default:
  8929. break;
  8930. }
  8931. }
  8932. drm_connector_list_iter_end(&conn_iter);
  8933. /* can't mix MST and SST/HDMI on the same port */
  8934. if (used_ports & used_mst_ports)
  8935. return false;
  8936. return true;
  8937. }
  8938. static void
  8939. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  8940. {
  8941. struct drm_i915_private *dev_priv =
  8942. to_i915(crtc_state->base.crtc->dev);
  8943. struct intel_crtc_scaler_state scaler_state;
  8944. struct intel_dpll_hw_state dpll_hw_state;
  8945. struct intel_shared_dpll *shared_dpll;
  8946. struct intel_crtc_wm_state wm_state;
  8947. bool force_thru, ips_force_disable;
  8948. /* FIXME: before the switch to atomic started, a new pipe_config was
  8949. * kzalloc'd. Code that depends on any field being zero should be
  8950. * fixed, so that the crtc_state can be safely duplicated. For now,
  8951. * only fields that are know to not cause problems are preserved. */
  8952. scaler_state = crtc_state->scaler_state;
  8953. shared_dpll = crtc_state->shared_dpll;
  8954. dpll_hw_state = crtc_state->dpll_hw_state;
  8955. force_thru = crtc_state->pch_pfit.force_thru;
  8956. ips_force_disable = crtc_state->ips_force_disable;
  8957. if (IS_G4X(dev_priv) ||
  8958. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  8959. wm_state = crtc_state->wm;
  8960. /* Keep base drm_crtc_state intact, only clear our extended struct */
  8961. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  8962. memset(&crtc_state->base + 1, 0,
  8963. sizeof(*crtc_state) - sizeof(crtc_state->base));
  8964. crtc_state->scaler_state = scaler_state;
  8965. crtc_state->shared_dpll = shared_dpll;
  8966. crtc_state->dpll_hw_state = dpll_hw_state;
  8967. crtc_state->pch_pfit.force_thru = force_thru;
  8968. crtc_state->ips_force_disable = ips_force_disable;
  8969. if (IS_G4X(dev_priv) ||
  8970. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  8971. crtc_state->wm = wm_state;
  8972. }
  8973. static int
  8974. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8975. struct intel_crtc_state *pipe_config)
  8976. {
  8977. struct drm_atomic_state *state = pipe_config->base.state;
  8978. struct intel_encoder *encoder;
  8979. struct drm_connector *connector;
  8980. struct drm_connector_state *connector_state;
  8981. int base_bpp, ret = -EINVAL;
  8982. int i;
  8983. bool retry = true;
  8984. clear_intel_crtc_state(pipe_config);
  8985. pipe_config->cpu_transcoder =
  8986. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8987. /*
  8988. * Sanitize sync polarity flags based on requested ones. If neither
  8989. * positive or negative polarity is requested, treat this as meaning
  8990. * negative polarity.
  8991. */
  8992. if (!(pipe_config->base.adjusted_mode.flags &
  8993. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8994. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8995. if (!(pipe_config->base.adjusted_mode.flags &
  8996. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8997. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8998. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8999. pipe_config);
  9000. if (base_bpp < 0)
  9001. goto fail;
  9002. /*
  9003. * Determine the real pipe dimensions. Note that stereo modes can
  9004. * increase the actual pipe size due to the frame doubling and
  9005. * insertion of additional space for blanks between the frame. This
  9006. * is stored in the crtc timings. We use the requested mode to do this
  9007. * computation to clearly distinguish it from the adjusted mode, which
  9008. * can be changed by the connectors in the below retry loop.
  9009. */
  9010. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9011. &pipe_config->pipe_src_w,
  9012. &pipe_config->pipe_src_h);
  9013. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9014. if (connector_state->crtc != crtc)
  9015. continue;
  9016. encoder = to_intel_encoder(connector_state->best_encoder);
  9017. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9018. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9019. goto fail;
  9020. }
  9021. /*
  9022. * Determine output_types before calling the .compute_config()
  9023. * hooks so that the hooks can use this information safely.
  9024. */
  9025. if (encoder->compute_output_type)
  9026. pipe_config->output_types |=
  9027. BIT(encoder->compute_output_type(encoder, pipe_config,
  9028. connector_state));
  9029. else
  9030. pipe_config->output_types |= BIT(encoder->type);
  9031. }
  9032. encoder_retry:
  9033. /* Ensure the port clock defaults are reset when retrying. */
  9034. pipe_config->port_clock = 0;
  9035. pipe_config->pixel_multiplier = 1;
  9036. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9037. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9038. CRTC_STEREO_DOUBLE);
  9039. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9040. * adjust it according to limitations or connector properties, and also
  9041. * a chance to reject the mode entirely.
  9042. */
  9043. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9044. if (connector_state->crtc != crtc)
  9045. continue;
  9046. encoder = to_intel_encoder(connector_state->best_encoder);
  9047. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9048. DRM_DEBUG_KMS("Encoder config failure\n");
  9049. goto fail;
  9050. }
  9051. }
  9052. /* Set default port clock if not overwritten by the encoder. Needs to be
  9053. * done afterwards in case the encoder adjusts the mode. */
  9054. if (!pipe_config->port_clock)
  9055. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9056. * pipe_config->pixel_multiplier;
  9057. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9058. if (ret < 0) {
  9059. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9060. goto fail;
  9061. }
  9062. if (ret == RETRY) {
  9063. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9064. ret = -EINVAL;
  9065. goto fail;
  9066. }
  9067. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9068. retry = false;
  9069. goto encoder_retry;
  9070. }
  9071. /* Dithering seems to not pass-through bits correctly when it should, so
  9072. * only enable it on 6bpc panels and when its not a compliance
  9073. * test requesting 6bpc video pattern.
  9074. */
  9075. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9076. !pipe_config->dither_force_disable;
  9077. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9078. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9079. fail:
  9080. return ret;
  9081. }
  9082. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9083. {
  9084. int diff;
  9085. if (clock1 == clock2)
  9086. return true;
  9087. if (!clock1 || !clock2)
  9088. return false;
  9089. diff = abs(clock1 - clock2);
  9090. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9091. return true;
  9092. return false;
  9093. }
  9094. static bool
  9095. intel_compare_m_n(unsigned int m, unsigned int n,
  9096. unsigned int m2, unsigned int n2,
  9097. bool exact)
  9098. {
  9099. if (m == m2 && n == n2)
  9100. return true;
  9101. if (exact || !m || !n || !m2 || !n2)
  9102. return false;
  9103. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9104. if (n > n2) {
  9105. while (n > n2) {
  9106. m2 <<= 1;
  9107. n2 <<= 1;
  9108. }
  9109. } else if (n < n2) {
  9110. while (n < n2) {
  9111. m <<= 1;
  9112. n <<= 1;
  9113. }
  9114. }
  9115. if (n != n2)
  9116. return false;
  9117. return intel_fuzzy_clock_check(m, m2);
  9118. }
  9119. static bool
  9120. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9121. struct intel_link_m_n *m2_n2,
  9122. bool adjust)
  9123. {
  9124. if (m_n->tu == m2_n2->tu &&
  9125. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9126. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9127. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9128. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9129. if (adjust)
  9130. *m2_n2 = *m_n;
  9131. return true;
  9132. }
  9133. return false;
  9134. }
  9135. static void __printf(3, 4)
  9136. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9137. {
  9138. char *level;
  9139. unsigned int category;
  9140. struct va_format vaf;
  9141. va_list args;
  9142. if (adjust) {
  9143. level = KERN_DEBUG;
  9144. category = DRM_UT_KMS;
  9145. } else {
  9146. level = KERN_ERR;
  9147. category = DRM_UT_NONE;
  9148. }
  9149. va_start(args, format);
  9150. vaf.fmt = format;
  9151. vaf.va = &args;
  9152. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9153. va_end(args);
  9154. }
  9155. static bool
  9156. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9157. struct intel_crtc_state *current_config,
  9158. struct intel_crtc_state *pipe_config,
  9159. bool adjust)
  9160. {
  9161. bool ret = true;
  9162. bool fixup_inherited = adjust &&
  9163. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9164. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9165. #define PIPE_CONF_CHECK_X(name) \
  9166. if (current_config->name != pipe_config->name) { \
  9167. pipe_config_err(adjust, __stringify(name), \
  9168. "(expected 0x%08x, found 0x%08x)\n", \
  9169. current_config->name, \
  9170. pipe_config->name); \
  9171. ret = false; \
  9172. }
  9173. #define PIPE_CONF_CHECK_I(name) \
  9174. if (current_config->name != pipe_config->name) { \
  9175. pipe_config_err(adjust, __stringify(name), \
  9176. "(expected %i, found %i)\n", \
  9177. current_config->name, \
  9178. pipe_config->name); \
  9179. ret = false; \
  9180. }
  9181. #define PIPE_CONF_CHECK_BOOL(name) \
  9182. if (current_config->name != pipe_config->name) { \
  9183. pipe_config_err(adjust, __stringify(name), \
  9184. "(expected %s, found %s)\n", \
  9185. yesno(current_config->name), \
  9186. yesno(pipe_config->name)); \
  9187. ret = false; \
  9188. }
  9189. /*
  9190. * Checks state where we only read out the enabling, but not the entire
  9191. * state itself (like full infoframes or ELD for audio). These states
  9192. * require a full modeset on bootup to fix up.
  9193. */
  9194. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) \
  9195. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9196. PIPE_CONF_CHECK_BOOL(name); \
  9197. } else { \
  9198. pipe_config_err(adjust, __stringify(name), \
  9199. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9200. yesno(current_config->name), \
  9201. yesno(pipe_config->name)); \
  9202. ret = false; \
  9203. }
  9204. #define PIPE_CONF_CHECK_P(name) \
  9205. if (current_config->name != pipe_config->name) { \
  9206. pipe_config_err(adjust, __stringify(name), \
  9207. "(expected %p, found %p)\n", \
  9208. current_config->name, \
  9209. pipe_config->name); \
  9210. ret = false; \
  9211. }
  9212. #define PIPE_CONF_CHECK_M_N(name) \
  9213. if (!intel_compare_link_m_n(&current_config->name, \
  9214. &pipe_config->name,\
  9215. adjust)) { \
  9216. pipe_config_err(adjust, __stringify(name), \
  9217. "(expected tu %i gmch %i/%i link %i/%i, " \
  9218. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9219. current_config->name.tu, \
  9220. current_config->name.gmch_m, \
  9221. current_config->name.gmch_n, \
  9222. current_config->name.link_m, \
  9223. current_config->name.link_n, \
  9224. pipe_config->name.tu, \
  9225. pipe_config->name.gmch_m, \
  9226. pipe_config->name.gmch_n, \
  9227. pipe_config->name.link_m, \
  9228. pipe_config->name.link_n); \
  9229. ret = false; \
  9230. }
  9231. /* This is required for BDW+ where there is only one set of registers for
  9232. * switching between high and low RR.
  9233. * This macro can be used whenever a comparison has to be made between one
  9234. * hw state and multiple sw state variables.
  9235. */
  9236. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9237. if (!intel_compare_link_m_n(&current_config->name, \
  9238. &pipe_config->name, adjust) && \
  9239. !intel_compare_link_m_n(&current_config->alt_name, \
  9240. &pipe_config->name, adjust)) { \
  9241. pipe_config_err(adjust, __stringify(name), \
  9242. "(expected tu %i gmch %i/%i link %i/%i, " \
  9243. "or tu %i gmch %i/%i link %i/%i, " \
  9244. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9245. current_config->name.tu, \
  9246. current_config->name.gmch_m, \
  9247. current_config->name.gmch_n, \
  9248. current_config->name.link_m, \
  9249. current_config->name.link_n, \
  9250. current_config->alt_name.tu, \
  9251. current_config->alt_name.gmch_m, \
  9252. current_config->alt_name.gmch_n, \
  9253. current_config->alt_name.link_m, \
  9254. current_config->alt_name.link_n, \
  9255. pipe_config->name.tu, \
  9256. pipe_config->name.gmch_m, \
  9257. pipe_config->name.gmch_n, \
  9258. pipe_config->name.link_m, \
  9259. pipe_config->name.link_n); \
  9260. ret = false; \
  9261. }
  9262. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9263. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9264. pipe_config_err(adjust, __stringify(name), \
  9265. "(%x) (expected %i, found %i)\n", \
  9266. (mask), \
  9267. current_config->name & (mask), \
  9268. pipe_config->name & (mask)); \
  9269. ret = false; \
  9270. }
  9271. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9272. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9273. pipe_config_err(adjust, __stringify(name), \
  9274. "(expected %i, found %i)\n", \
  9275. current_config->name, \
  9276. pipe_config->name); \
  9277. ret = false; \
  9278. }
  9279. #define PIPE_CONF_QUIRK(quirk) \
  9280. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9281. PIPE_CONF_CHECK_I(cpu_transcoder);
  9282. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9283. PIPE_CONF_CHECK_I(fdi_lanes);
  9284. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9285. PIPE_CONF_CHECK_I(lane_count);
  9286. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9287. if (INTEL_GEN(dev_priv) < 8) {
  9288. PIPE_CONF_CHECK_M_N(dp_m_n);
  9289. if (current_config->has_drrs)
  9290. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9291. } else
  9292. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9293. PIPE_CONF_CHECK_X(output_types);
  9294. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9295. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9296. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9297. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9298. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9299. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9300. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9301. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9302. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9303. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9304. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9305. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9306. PIPE_CONF_CHECK_I(pixel_multiplier);
  9307. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9308. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9309. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9310. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9311. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9312. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9313. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9314. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9315. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9316. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9317. DRM_MODE_FLAG_INTERLACE);
  9318. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9319. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9320. DRM_MODE_FLAG_PHSYNC);
  9321. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9322. DRM_MODE_FLAG_NHSYNC);
  9323. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9324. DRM_MODE_FLAG_PVSYNC);
  9325. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9326. DRM_MODE_FLAG_NVSYNC);
  9327. }
  9328. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9329. /* pfit ratios are autocomputed by the hw on gen4+ */
  9330. if (INTEL_GEN(dev_priv) < 4)
  9331. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9332. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9333. if (!adjust) {
  9334. PIPE_CONF_CHECK_I(pipe_src_w);
  9335. PIPE_CONF_CHECK_I(pipe_src_h);
  9336. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9337. if (current_config->pch_pfit.enabled) {
  9338. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9339. PIPE_CONF_CHECK_X(pch_pfit.size);
  9340. }
  9341. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9342. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9343. }
  9344. PIPE_CONF_CHECK_BOOL(double_wide);
  9345. PIPE_CONF_CHECK_P(shared_dpll);
  9346. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9347. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9348. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9349. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9350. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9351. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9352. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9353. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9354. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9355. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9356. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9357. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9358. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9359. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9360. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9361. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9362. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9363. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9364. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9365. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9366. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9367. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9368. PIPE_CONF_CHECK_X(dsi_pll.div);
  9369. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9370. PIPE_CONF_CHECK_I(pipe_bpp);
  9371. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9372. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9373. PIPE_CONF_CHECK_I(min_voltage_level);
  9374. #undef PIPE_CONF_CHECK_X
  9375. #undef PIPE_CONF_CHECK_I
  9376. #undef PIPE_CONF_CHECK_BOOL
  9377. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9378. #undef PIPE_CONF_CHECK_P
  9379. #undef PIPE_CONF_CHECK_FLAGS
  9380. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9381. #undef PIPE_CONF_QUIRK
  9382. return ret;
  9383. }
  9384. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9385. const struct intel_crtc_state *pipe_config)
  9386. {
  9387. if (pipe_config->has_pch_encoder) {
  9388. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9389. &pipe_config->fdi_m_n);
  9390. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9391. /*
  9392. * FDI already provided one idea for the dotclock.
  9393. * Yell if the encoder disagrees.
  9394. */
  9395. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9396. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9397. fdi_dotclock, dotclock);
  9398. }
  9399. }
  9400. static void verify_wm_state(struct drm_crtc *crtc,
  9401. struct drm_crtc_state *new_state)
  9402. {
  9403. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9404. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9405. struct skl_pipe_wm hw_wm, *sw_wm;
  9406. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9407. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9409. const enum pipe pipe = intel_crtc->pipe;
  9410. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9411. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9412. return;
  9413. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9414. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9415. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9416. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9417. /* planes */
  9418. for_each_universal_plane(dev_priv, pipe, plane) {
  9419. hw_plane_wm = &hw_wm.planes[plane];
  9420. sw_plane_wm = &sw_wm->planes[plane];
  9421. /* Watermarks */
  9422. for (level = 0; level <= max_level; level++) {
  9423. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9424. &sw_plane_wm->wm[level]))
  9425. continue;
  9426. 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",
  9427. pipe_name(pipe), plane + 1, level,
  9428. sw_plane_wm->wm[level].plane_en,
  9429. sw_plane_wm->wm[level].plane_res_b,
  9430. sw_plane_wm->wm[level].plane_res_l,
  9431. hw_plane_wm->wm[level].plane_en,
  9432. hw_plane_wm->wm[level].plane_res_b,
  9433. hw_plane_wm->wm[level].plane_res_l);
  9434. }
  9435. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9436. &sw_plane_wm->trans_wm)) {
  9437. 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",
  9438. pipe_name(pipe), plane + 1,
  9439. sw_plane_wm->trans_wm.plane_en,
  9440. sw_plane_wm->trans_wm.plane_res_b,
  9441. sw_plane_wm->trans_wm.plane_res_l,
  9442. hw_plane_wm->trans_wm.plane_en,
  9443. hw_plane_wm->trans_wm.plane_res_b,
  9444. hw_plane_wm->trans_wm.plane_res_l);
  9445. }
  9446. /* DDB */
  9447. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9448. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9449. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9450. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9451. pipe_name(pipe), plane + 1,
  9452. sw_ddb_entry->start, sw_ddb_entry->end,
  9453. hw_ddb_entry->start, hw_ddb_entry->end);
  9454. }
  9455. }
  9456. /*
  9457. * cursor
  9458. * If the cursor plane isn't active, we may not have updated it's ddb
  9459. * allocation. In that case since the ddb allocation will be updated
  9460. * once the plane becomes visible, we can skip this check
  9461. */
  9462. if (1) {
  9463. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9464. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9465. /* Watermarks */
  9466. for (level = 0; level <= max_level; level++) {
  9467. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9468. &sw_plane_wm->wm[level]))
  9469. continue;
  9470. 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",
  9471. pipe_name(pipe), level,
  9472. sw_plane_wm->wm[level].plane_en,
  9473. sw_plane_wm->wm[level].plane_res_b,
  9474. sw_plane_wm->wm[level].plane_res_l,
  9475. hw_plane_wm->wm[level].plane_en,
  9476. hw_plane_wm->wm[level].plane_res_b,
  9477. hw_plane_wm->wm[level].plane_res_l);
  9478. }
  9479. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9480. &sw_plane_wm->trans_wm)) {
  9481. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9482. pipe_name(pipe),
  9483. sw_plane_wm->trans_wm.plane_en,
  9484. sw_plane_wm->trans_wm.plane_res_b,
  9485. sw_plane_wm->trans_wm.plane_res_l,
  9486. hw_plane_wm->trans_wm.plane_en,
  9487. hw_plane_wm->trans_wm.plane_res_b,
  9488. hw_plane_wm->trans_wm.plane_res_l);
  9489. }
  9490. /* DDB */
  9491. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9492. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9493. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9494. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9495. pipe_name(pipe),
  9496. sw_ddb_entry->start, sw_ddb_entry->end,
  9497. hw_ddb_entry->start, hw_ddb_entry->end);
  9498. }
  9499. }
  9500. }
  9501. static void
  9502. verify_connector_state(struct drm_device *dev,
  9503. struct drm_atomic_state *state,
  9504. struct drm_crtc *crtc)
  9505. {
  9506. struct drm_connector *connector;
  9507. struct drm_connector_state *new_conn_state;
  9508. int i;
  9509. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9510. struct drm_encoder *encoder = connector->encoder;
  9511. struct drm_crtc_state *crtc_state = NULL;
  9512. if (new_conn_state->crtc != crtc)
  9513. continue;
  9514. if (crtc)
  9515. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9516. intel_connector_verify_state(crtc_state, new_conn_state);
  9517. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9518. "connector's atomic encoder doesn't match legacy encoder\n");
  9519. }
  9520. }
  9521. static void
  9522. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9523. {
  9524. struct intel_encoder *encoder;
  9525. struct drm_connector *connector;
  9526. struct drm_connector_state *old_conn_state, *new_conn_state;
  9527. int i;
  9528. for_each_intel_encoder(dev, encoder) {
  9529. bool enabled = false, found = false;
  9530. enum pipe pipe;
  9531. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9532. encoder->base.base.id,
  9533. encoder->base.name);
  9534. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9535. new_conn_state, i) {
  9536. if (old_conn_state->best_encoder == &encoder->base)
  9537. found = true;
  9538. if (new_conn_state->best_encoder != &encoder->base)
  9539. continue;
  9540. found = enabled = true;
  9541. I915_STATE_WARN(new_conn_state->crtc !=
  9542. encoder->base.crtc,
  9543. "connector's crtc doesn't match encoder crtc\n");
  9544. }
  9545. if (!found)
  9546. continue;
  9547. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9548. "encoder's enabled state mismatch "
  9549. "(expected %i, found %i)\n",
  9550. !!encoder->base.crtc, enabled);
  9551. if (!encoder->base.crtc) {
  9552. bool active;
  9553. active = encoder->get_hw_state(encoder, &pipe);
  9554. I915_STATE_WARN(active,
  9555. "encoder detached but still enabled on pipe %c.\n",
  9556. pipe_name(pipe));
  9557. }
  9558. }
  9559. }
  9560. static void
  9561. verify_crtc_state(struct drm_crtc *crtc,
  9562. struct drm_crtc_state *old_crtc_state,
  9563. struct drm_crtc_state *new_crtc_state)
  9564. {
  9565. struct drm_device *dev = crtc->dev;
  9566. struct drm_i915_private *dev_priv = to_i915(dev);
  9567. struct intel_encoder *encoder;
  9568. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9569. struct intel_crtc_state *pipe_config, *sw_config;
  9570. struct drm_atomic_state *old_state;
  9571. bool active;
  9572. old_state = old_crtc_state->state;
  9573. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9574. pipe_config = to_intel_crtc_state(old_crtc_state);
  9575. memset(pipe_config, 0, sizeof(*pipe_config));
  9576. pipe_config->base.crtc = crtc;
  9577. pipe_config->base.state = old_state;
  9578. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9579. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9580. /* we keep both pipes enabled on 830 */
  9581. if (IS_I830(dev_priv))
  9582. active = new_crtc_state->active;
  9583. I915_STATE_WARN(new_crtc_state->active != active,
  9584. "crtc active state doesn't match with hw state "
  9585. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9586. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9587. "transitional active state does not match atomic hw state "
  9588. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9589. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9590. enum pipe pipe;
  9591. active = encoder->get_hw_state(encoder, &pipe);
  9592. I915_STATE_WARN(active != new_crtc_state->active,
  9593. "[ENCODER:%i] active %i with crtc active %i\n",
  9594. encoder->base.base.id, active, new_crtc_state->active);
  9595. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9596. "Encoder connected to wrong pipe %c\n",
  9597. pipe_name(pipe));
  9598. if (active)
  9599. encoder->get_config(encoder, pipe_config);
  9600. }
  9601. intel_crtc_compute_pixel_rate(pipe_config);
  9602. if (!new_crtc_state->active)
  9603. return;
  9604. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9605. sw_config = to_intel_crtc_state(new_crtc_state);
  9606. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9607. pipe_config, false)) {
  9608. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9609. intel_dump_pipe_config(intel_crtc, pipe_config,
  9610. "[hw state]");
  9611. intel_dump_pipe_config(intel_crtc, sw_config,
  9612. "[sw state]");
  9613. }
  9614. }
  9615. static void
  9616. intel_verify_planes(struct intel_atomic_state *state)
  9617. {
  9618. struct intel_plane *plane;
  9619. const struct intel_plane_state *plane_state;
  9620. int i;
  9621. for_each_new_intel_plane_in_state(state, plane,
  9622. plane_state, i)
  9623. assert_plane(plane, plane_state->base.visible);
  9624. }
  9625. static void
  9626. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9627. struct intel_shared_dpll *pll,
  9628. struct drm_crtc *crtc,
  9629. struct drm_crtc_state *new_state)
  9630. {
  9631. struct intel_dpll_hw_state dpll_hw_state;
  9632. unsigned crtc_mask;
  9633. bool active;
  9634. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9635. DRM_DEBUG_KMS("%s\n", pll->name);
  9636. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9637. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9638. I915_STATE_WARN(!pll->on && pll->active_mask,
  9639. "pll in active use but not on in sw tracking\n");
  9640. I915_STATE_WARN(pll->on && !pll->active_mask,
  9641. "pll is on but not used by any active crtc\n");
  9642. I915_STATE_WARN(pll->on != active,
  9643. "pll on state mismatch (expected %i, found %i)\n",
  9644. pll->on, active);
  9645. }
  9646. if (!crtc) {
  9647. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9648. "more active pll users than references: %x vs %x\n",
  9649. pll->active_mask, pll->state.crtc_mask);
  9650. return;
  9651. }
  9652. crtc_mask = 1 << drm_crtc_index(crtc);
  9653. if (new_state->active)
  9654. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9655. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9656. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9657. else
  9658. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9659. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9660. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9661. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9662. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9663. crtc_mask, pll->state.crtc_mask);
  9664. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9665. &dpll_hw_state,
  9666. sizeof(dpll_hw_state)),
  9667. "pll hw state mismatch\n");
  9668. }
  9669. static void
  9670. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9671. struct drm_crtc_state *old_crtc_state,
  9672. struct drm_crtc_state *new_crtc_state)
  9673. {
  9674. struct drm_i915_private *dev_priv = to_i915(dev);
  9675. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9676. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9677. if (new_state->shared_dpll)
  9678. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9679. if (old_state->shared_dpll &&
  9680. old_state->shared_dpll != new_state->shared_dpll) {
  9681. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9682. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9683. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9684. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9685. pipe_name(drm_crtc_index(crtc)));
  9686. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9687. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9688. pipe_name(drm_crtc_index(crtc)));
  9689. }
  9690. }
  9691. static void
  9692. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9693. struct drm_atomic_state *state,
  9694. struct drm_crtc_state *old_state,
  9695. struct drm_crtc_state *new_state)
  9696. {
  9697. if (!needs_modeset(new_state) &&
  9698. !to_intel_crtc_state(new_state)->update_pipe)
  9699. return;
  9700. verify_wm_state(crtc, new_state);
  9701. verify_connector_state(crtc->dev, state, crtc);
  9702. verify_crtc_state(crtc, old_state, new_state);
  9703. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9704. }
  9705. static void
  9706. verify_disabled_dpll_state(struct drm_device *dev)
  9707. {
  9708. struct drm_i915_private *dev_priv = to_i915(dev);
  9709. int i;
  9710. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9711. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9712. }
  9713. static void
  9714. intel_modeset_verify_disabled(struct drm_device *dev,
  9715. struct drm_atomic_state *state)
  9716. {
  9717. verify_encoder_state(dev, state);
  9718. verify_connector_state(dev, state, NULL);
  9719. verify_disabled_dpll_state(dev);
  9720. }
  9721. static void update_scanline_offset(struct intel_crtc *crtc)
  9722. {
  9723. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9724. /*
  9725. * The scanline counter increments at the leading edge of hsync.
  9726. *
  9727. * On most platforms it starts counting from vtotal-1 on the
  9728. * first active line. That means the scanline counter value is
  9729. * always one less than what we would expect. Ie. just after
  9730. * start of vblank, which also occurs at start of hsync (on the
  9731. * last active line), the scanline counter will read vblank_start-1.
  9732. *
  9733. * On gen2 the scanline counter starts counting from 1 instead
  9734. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9735. * to keep the value positive), instead of adding one.
  9736. *
  9737. * On HSW+ the behaviour of the scanline counter depends on the output
  9738. * type. For DP ports it behaves like most other platforms, but on HDMI
  9739. * there's an extra 1 line difference. So we need to add two instead of
  9740. * one to the value.
  9741. *
  9742. * On VLV/CHV DSI the scanline counter would appear to increment
  9743. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9744. * that means we can't tell whether we're in vblank or not while
  9745. * we're on that particular line. We must still set scanline_offset
  9746. * to 1 so that the vblank timestamps come out correct when we query
  9747. * the scanline counter from within the vblank interrupt handler.
  9748. * However if queried just before the start of vblank we'll get an
  9749. * answer that's slightly in the future.
  9750. */
  9751. if (IS_GEN2(dev_priv)) {
  9752. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9753. int vtotal;
  9754. vtotal = adjusted_mode->crtc_vtotal;
  9755. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9756. vtotal /= 2;
  9757. crtc->scanline_offset = vtotal - 1;
  9758. } else if (HAS_DDI(dev_priv) &&
  9759. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9760. crtc->scanline_offset = 2;
  9761. } else
  9762. crtc->scanline_offset = 1;
  9763. }
  9764. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9765. {
  9766. struct drm_device *dev = state->dev;
  9767. struct drm_i915_private *dev_priv = to_i915(dev);
  9768. struct drm_crtc *crtc;
  9769. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9770. int i;
  9771. if (!dev_priv->display.crtc_compute_clock)
  9772. return;
  9773. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9775. struct intel_shared_dpll *old_dpll =
  9776. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9777. if (!needs_modeset(new_crtc_state))
  9778. continue;
  9779. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9780. if (!old_dpll)
  9781. continue;
  9782. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9783. }
  9784. }
  9785. /*
  9786. * This implements the workaround described in the "notes" section of the mode
  9787. * set sequence documentation. When going from no pipes or single pipe to
  9788. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9789. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9790. */
  9791. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9792. {
  9793. struct drm_crtc_state *crtc_state;
  9794. struct intel_crtc *intel_crtc;
  9795. struct drm_crtc *crtc;
  9796. struct intel_crtc_state *first_crtc_state = NULL;
  9797. struct intel_crtc_state *other_crtc_state = NULL;
  9798. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9799. int i;
  9800. /* look at all crtc's that are going to be enabled in during modeset */
  9801. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9802. intel_crtc = to_intel_crtc(crtc);
  9803. if (!crtc_state->active || !needs_modeset(crtc_state))
  9804. continue;
  9805. if (first_crtc_state) {
  9806. other_crtc_state = to_intel_crtc_state(crtc_state);
  9807. break;
  9808. } else {
  9809. first_crtc_state = to_intel_crtc_state(crtc_state);
  9810. first_pipe = intel_crtc->pipe;
  9811. }
  9812. }
  9813. /* No workaround needed? */
  9814. if (!first_crtc_state)
  9815. return 0;
  9816. /* w/a possibly needed, check how many crtc's are already enabled. */
  9817. for_each_intel_crtc(state->dev, intel_crtc) {
  9818. struct intel_crtc_state *pipe_config;
  9819. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9820. if (IS_ERR(pipe_config))
  9821. return PTR_ERR(pipe_config);
  9822. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9823. if (!pipe_config->base.active ||
  9824. needs_modeset(&pipe_config->base))
  9825. continue;
  9826. /* 2 or more enabled crtcs means no need for w/a */
  9827. if (enabled_pipe != INVALID_PIPE)
  9828. return 0;
  9829. enabled_pipe = intel_crtc->pipe;
  9830. }
  9831. if (enabled_pipe != INVALID_PIPE)
  9832. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9833. else if (other_crtc_state)
  9834. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9835. return 0;
  9836. }
  9837. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9838. {
  9839. struct drm_crtc *crtc;
  9840. /* Add all pipes to the state */
  9841. for_each_crtc(state->dev, crtc) {
  9842. struct drm_crtc_state *crtc_state;
  9843. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9844. if (IS_ERR(crtc_state))
  9845. return PTR_ERR(crtc_state);
  9846. }
  9847. return 0;
  9848. }
  9849. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9850. {
  9851. struct drm_crtc *crtc;
  9852. /*
  9853. * Add all pipes to the state, and force
  9854. * a modeset on all the active ones.
  9855. */
  9856. for_each_crtc(state->dev, crtc) {
  9857. struct drm_crtc_state *crtc_state;
  9858. int ret;
  9859. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9860. if (IS_ERR(crtc_state))
  9861. return PTR_ERR(crtc_state);
  9862. if (!crtc_state->active || needs_modeset(crtc_state))
  9863. continue;
  9864. crtc_state->mode_changed = true;
  9865. ret = drm_atomic_add_affected_connectors(state, crtc);
  9866. if (ret)
  9867. return ret;
  9868. ret = drm_atomic_add_affected_planes(state, crtc);
  9869. if (ret)
  9870. return ret;
  9871. }
  9872. return 0;
  9873. }
  9874. static int intel_modeset_checks(struct drm_atomic_state *state)
  9875. {
  9876. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9877. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9878. struct drm_crtc *crtc;
  9879. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9880. int ret = 0, i;
  9881. if (!check_digital_port_conflicts(state)) {
  9882. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9883. return -EINVAL;
  9884. }
  9885. intel_state->modeset = true;
  9886. intel_state->active_crtcs = dev_priv->active_crtcs;
  9887. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9888. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  9889. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9890. if (new_crtc_state->active)
  9891. intel_state->active_crtcs |= 1 << i;
  9892. else
  9893. intel_state->active_crtcs &= ~(1 << i);
  9894. if (old_crtc_state->active != new_crtc_state->active)
  9895. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  9896. }
  9897. /*
  9898. * See if the config requires any additional preparation, e.g.
  9899. * to adjust global state with pipes off. We need to do this
  9900. * here so we can get the modeset_pipe updated config for the new
  9901. * mode set on this crtc. For other crtcs we need to use the
  9902. * adjusted_mode bits in the crtc directly.
  9903. */
  9904. if (dev_priv->display.modeset_calc_cdclk) {
  9905. ret = dev_priv->display.modeset_calc_cdclk(state);
  9906. if (ret < 0)
  9907. return ret;
  9908. /*
  9909. * Writes to dev_priv->cdclk.logical must protected by
  9910. * holding all the crtc locks, even if we don't end up
  9911. * touching the hardware
  9912. */
  9913. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  9914. &intel_state->cdclk.logical)) {
  9915. ret = intel_lock_all_pipes(state);
  9916. if (ret < 0)
  9917. return ret;
  9918. }
  9919. /* All pipes must be switched off while we change the cdclk. */
  9920. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  9921. &intel_state->cdclk.actual)) {
  9922. ret = intel_modeset_all_pipes(state);
  9923. if (ret < 0)
  9924. return ret;
  9925. }
  9926. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  9927. intel_state->cdclk.logical.cdclk,
  9928. intel_state->cdclk.actual.cdclk);
  9929. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  9930. intel_state->cdclk.logical.voltage_level,
  9931. intel_state->cdclk.actual.voltage_level);
  9932. } else {
  9933. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  9934. }
  9935. intel_modeset_clear_plls(state);
  9936. if (IS_HASWELL(dev_priv))
  9937. return haswell_mode_set_planes_workaround(state);
  9938. return 0;
  9939. }
  9940. /*
  9941. * Handle calculation of various watermark data at the end of the atomic check
  9942. * phase. The code here should be run after the per-crtc and per-plane 'check'
  9943. * handlers to ensure that all derived state has been updated.
  9944. */
  9945. static int calc_watermark_data(struct drm_atomic_state *state)
  9946. {
  9947. struct drm_device *dev = state->dev;
  9948. struct drm_i915_private *dev_priv = to_i915(dev);
  9949. /* Is there platform-specific watermark information to calculate? */
  9950. if (dev_priv->display.compute_global_watermarks)
  9951. return dev_priv->display.compute_global_watermarks(state);
  9952. return 0;
  9953. }
  9954. /**
  9955. * intel_atomic_check - validate state object
  9956. * @dev: drm device
  9957. * @state: state to validate
  9958. */
  9959. static int intel_atomic_check(struct drm_device *dev,
  9960. struct drm_atomic_state *state)
  9961. {
  9962. struct drm_i915_private *dev_priv = to_i915(dev);
  9963. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9964. struct drm_crtc *crtc;
  9965. struct drm_crtc_state *old_crtc_state, *crtc_state;
  9966. int ret, i;
  9967. bool any_ms = false;
  9968. ret = drm_atomic_helper_check_modeset(dev, state);
  9969. if (ret)
  9970. return ret;
  9971. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  9972. struct intel_crtc_state *pipe_config =
  9973. to_intel_crtc_state(crtc_state);
  9974. /* Catch I915_MODE_FLAG_INHERITED */
  9975. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  9976. crtc_state->mode_changed = true;
  9977. if (!needs_modeset(crtc_state))
  9978. continue;
  9979. if (!crtc_state->enable) {
  9980. any_ms = true;
  9981. continue;
  9982. }
  9983. /* FIXME: For only active_changed we shouldn't need to do any
  9984. * state recomputation at all. */
  9985. ret = drm_atomic_add_affected_connectors(state, crtc);
  9986. if (ret)
  9987. return ret;
  9988. ret = intel_modeset_pipe_config(crtc, pipe_config);
  9989. if (ret) {
  9990. intel_dump_pipe_config(to_intel_crtc(crtc),
  9991. pipe_config, "[failed]");
  9992. return ret;
  9993. }
  9994. if (i915_modparams.fastboot &&
  9995. intel_pipe_config_compare(dev_priv,
  9996. to_intel_crtc_state(old_crtc_state),
  9997. pipe_config, true)) {
  9998. crtc_state->mode_changed = false;
  9999. pipe_config->update_pipe = true;
  10000. }
  10001. if (needs_modeset(crtc_state))
  10002. any_ms = true;
  10003. ret = drm_atomic_add_affected_planes(state, crtc);
  10004. if (ret)
  10005. return ret;
  10006. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10007. needs_modeset(crtc_state) ?
  10008. "[modeset]" : "[fastset]");
  10009. }
  10010. if (any_ms) {
  10011. ret = intel_modeset_checks(state);
  10012. if (ret)
  10013. return ret;
  10014. } else {
  10015. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10016. }
  10017. ret = drm_atomic_helper_check_planes(dev, state);
  10018. if (ret)
  10019. return ret;
  10020. intel_fbc_choose_crtc(dev_priv, intel_state);
  10021. return calc_watermark_data(state);
  10022. }
  10023. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10024. struct drm_atomic_state *state)
  10025. {
  10026. return drm_atomic_helper_prepare_planes(dev, state);
  10027. }
  10028. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10029. {
  10030. struct drm_device *dev = crtc->base.dev;
  10031. if (!dev->max_vblank_count)
  10032. return drm_crtc_accurate_vblank_count(&crtc->base);
  10033. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10034. }
  10035. static void intel_update_crtc(struct drm_crtc *crtc,
  10036. struct drm_atomic_state *state,
  10037. struct drm_crtc_state *old_crtc_state,
  10038. struct drm_crtc_state *new_crtc_state)
  10039. {
  10040. struct drm_device *dev = crtc->dev;
  10041. struct drm_i915_private *dev_priv = to_i915(dev);
  10042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10043. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10044. bool modeset = needs_modeset(new_crtc_state);
  10045. if (modeset) {
  10046. update_scanline_offset(intel_crtc);
  10047. dev_priv->display.crtc_enable(pipe_config, state);
  10048. } else {
  10049. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10050. pipe_config);
  10051. }
  10052. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10053. intel_fbc_enable(
  10054. intel_crtc, pipe_config,
  10055. to_intel_plane_state(crtc->primary->state));
  10056. }
  10057. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10058. }
  10059. static void intel_update_crtcs(struct drm_atomic_state *state)
  10060. {
  10061. struct drm_crtc *crtc;
  10062. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10063. int i;
  10064. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10065. if (!new_crtc_state->active)
  10066. continue;
  10067. intel_update_crtc(crtc, state, old_crtc_state,
  10068. new_crtc_state);
  10069. }
  10070. }
  10071. static void skl_update_crtcs(struct drm_atomic_state *state)
  10072. {
  10073. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10074. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10075. struct drm_crtc *crtc;
  10076. struct intel_crtc *intel_crtc;
  10077. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10078. struct intel_crtc_state *cstate;
  10079. unsigned int updated = 0;
  10080. bool progress;
  10081. enum pipe pipe;
  10082. int i;
  10083. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10084. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10085. /* ignore allocations for crtc's that have been turned off. */
  10086. if (new_crtc_state->active)
  10087. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10088. /*
  10089. * Whenever the number of active pipes changes, we need to make sure we
  10090. * update the pipes in the right order so that their ddb allocations
  10091. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10092. * cause pipe underruns and other bad stuff.
  10093. */
  10094. do {
  10095. progress = false;
  10096. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10097. bool vbl_wait = false;
  10098. unsigned int cmask = drm_crtc_mask(crtc);
  10099. intel_crtc = to_intel_crtc(crtc);
  10100. cstate = to_intel_crtc_state(new_crtc_state);
  10101. pipe = intel_crtc->pipe;
  10102. if (updated & cmask || !cstate->base.active)
  10103. continue;
  10104. if (skl_ddb_allocation_overlaps(dev_priv,
  10105. entries,
  10106. &cstate->wm.skl.ddb,
  10107. i))
  10108. continue;
  10109. updated |= cmask;
  10110. entries[i] = &cstate->wm.skl.ddb;
  10111. /*
  10112. * If this is an already active pipe, it's DDB changed,
  10113. * and this isn't the last pipe that needs updating
  10114. * then we need to wait for a vblank to pass for the
  10115. * new ddb allocation to take effect.
  10116. */
  10117. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10118. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10119. !new_crtc_state->active_changed &&
  10120. intel_state->wm_results.dirty_pipes != updated)
  10121. vbl_wait = true;
  10122. intel_update_crtc(crtc, state, old_crtc_state,
  10123. new_crtc_state);
  10124. if (vbl_wait)
  10125. intel_wait_for_vblank(dev_priv, pipe);
  10126. progress = true;
  10127. }
  10128. } while (progress);
  10129. }
  10130. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10131. {
  10132. struct intel_atomic_state *state, *next;
  10133. struct llist_node *freed;
  10134. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10135. llist_for_each_entry_safe(state, next, freed, freed)
  10136. drm_atomic_state_put(&state->base);
  10137. }
  10138. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10139. {
  10140. struct drm_i915_private *dev_priv =
  10141. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10142. intel_atomic_helper_free_state(dev_priv);
  10143. }
  10144. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10145. {
  10146. struct wait_queue_entry wait_fence, wait_reset;
  10147. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10148. init_wait_entry(&wait_fence, 0);
  10149. init_wait_entry(&wait_reset, 0);
  10150. for (;;) {
  10151. prepare_to_wait(&intel_state->commit_ready.wait,
  10152. &wait_fence, TASK_UNINTERRUPTIBLE);
  10153. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10154. &wait_reset, TASK_UNINTERRUPTIBLE);
  10155. if (i915_sw_fence_done(&intel_state->commit_ready)
  10156. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10157. break;
  10158. schedule();
  10159. }
  10160. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10161. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10162. }
  10163. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10164. {
  10165. struct drm_device *dev = state->dev;
  10166. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10167. struct drm_i915_private *dev_priv = to_i915(dev);
  10168. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10169. struct drm_crtc *crtc;
  10170. struct intel_crtc_state *intel_cstate;
  10171. u64 put_domains[I915_MAX_PIPES] = {};
  10172. int i;
  10173. intel_atomic_commit_fence_wait(intel_state);
  10174. drm_atomic_helper_wait_for_dependencies(state);
  10175. if (intel_state->modeset)
  10176. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10177. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10179. if (needs_modeset(new_crtc_state) ||
  10180. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10181. put_domains[to_intel_crtc(crtc)->pipe] =
  10182. modeset_get_crtc_power_domains(crtc,
  10183. to_intel_crtc_state(new_crtc_state));
  10184. }
  10185. if (!needs_modeset(new_crtc_state))
  10186. continue;
  10187. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10188. to_intel_crtc_state(new_crtc_state));
  10189. if (old_crtc_state->active) {
  10190. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10191. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10192. intel_crtc->active = false;
  10193. intel_fbc_disable(intel_crtc);
  10194. intel_disable_shared_dpll(intel_crtc);
  10195. /*
  10196. * Underruns don't always raise
  10197. * interrupts, so check manually.
  10198. */
  10199. intel_check_cpu_fifo_underruns(dev_priv);
  10200. intel_check_pch_fifo_underruns(dev_priv);
  10201. if (!new_crtc_state->active) {
  10202. /*
  10203. * Make sure we don't call initial_watermarks
  10204. * for ILK-style watermark updates.
  10205. *
  10206. * No clue what this is supposed to achieve.
  10207. */
  10208. if (INTEL_GEN(dev_priv) >= 9)
  10209. dev_priv->display.initial_watermarks(intel_state,
  10210. to_intel_crtc_state(new_crtc_state));
  10211. }
  10212. }
  10213. }
  10214. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10215. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10216. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10217. if (intel_state->modeset) {
  10218. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10219. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10220. /*
  10221. * SKL workaround: bspec recommends we disable the SAGV when we
  10222. * have more then one pipe enabled
  10223. */
  10224. if (!intel_can_enable_sagv(state))
  10225. intel_disable_sagv(dev_priv);
  10226. intel_modeset_verify_disabled(dev, state);
  10227. }
  10228. /* Complete the events for pipes that have now been disabled */
  10229. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10230. bool modeset = needs_modeset(new_crtc_state);
  10231. /* Complete events for now disable pipes here. */
  10232. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10233. spin_lock_irq(&dev->event_lock);
  10234. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10235. spin_unlock_irq(&dev->event_lock);
  10236. new_crtc_state->event = NULL;
  10237. }
  10238. }
  10239. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10240. dev_priv->display.update_crtcs(state);
  10241. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10242. * already, but still need the state for the delayed optimization. To
  10243. * fix this:
  10244. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10245. * - schedule that vblank worker _before_ calling hw_done
  10246. * - at the start of commit_tail, cancel it _synchrously
  10247. * - switch over to the vblank wait helper in the core after that since
  10248. * we don't need out special handling any more.
  10249. */
  10250. drm_atomic_helper_wait_for_flip_done(dev, state);
  10251. /*
  10252. * Now that the vblank has passed, we can go ahead and program the
  10253. * optimal watermarks on platforms that need two-step watermark
  10254. * programming.
  10255. *
  10256. * TODO: Move this (and other cleanup) to an async worker eventually.
  10257. */
  10258. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10259. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10260. if (dev_priv->display.optimize_watermarks)
  10261. dev_priv->display.optimize_watermarks(intel_state,
  10262. intel_cstate);
  10263. }
  10264. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10265. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10266. if (put_domains[i])
  10267. modeset_put_power_domains(dev_priv, put_domains[i]);
  10268. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10269. }
  10270. if (intel_state->modeset)
  10271. intel_verify_planes(intel_state);
  10272. if (intel_state->modeset && intel_can_enable_sagv(state))
  10273. intel_enable_sagv(dev_priv);
  10274. drm_atomic_helper_commit_hw_done(state);
  10275. if (intel_state->modeset) {
  10276. /* As one of the primary mmio accessors, KMS has a high
  10277. * likelihood of triggering bugs in unclaimed access. After we
  10278. * finish modesetting, see if an error has been flagged, and if
  10279. * so enable debugging for the next modeset - and hope we catch
  10280. * the culprit.
  10281. */
  10282. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10283. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10284. }
  10285. drm_atomic_helper_cleanup_planes(dev, state);
  10286. drm_atomic_helper_commit_cleanup_done(state);
  10287. drm_atomic_state_put(state);
  10288. intel_atomic_helper_free_state(dev_priv);
  10289. }
  10290. static void intel_atomic_commit_work(struct work_struct *work)
  10291. {
  10292. struct drm_atomic_state *state =
  10293. container_of(work, struct drm_atomic_state, commit_work);
  10294. intel_atomic_commit_tail(state);
  10295. }
  10296. static int __i915_sw_fence_call
  10297. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10298. enum i915_sw_fence_notify notify)
  10299. {
  10300. struct intel_atomic_state *state =
  10301. container_of(fence, struct intel_atomic_state, commit_ready);
  10302. switch (notify) {
  10303. case FENCE_COMPLETE:
  10304. /* we do blocking waits in the worker, nothing to do here */
  10305. break;
  10306. case FENCE_FREE:
  10307. {
  10308. struct intel_atomic_helper *helper =
  10309. &to_i915(state->base.dev)->atomic_helper;
  10310. if (llist_add(&state->freed, &helper->free_list))
  10311. schedule_work(&helper->free_work);
  10312. break;
  10313. }
  10314. }
  10315. return NOTIFY_DONE;
  10316. }
  10317. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10318. {
  10319. struct drm_plane_state *old_plane_state, *new_plane_state;
  10320. struct drm_plane *plane;
  10321. int i;
  10322. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10323. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10324. intel_fb_obj(new_plane_state->fb),
  10325. to_intel_plane(plane)->frontbuffer_bit);
  10326. }
  10327. /**
  10328. * intel_atomic_commit - commit validated state object
  10329. * @dev: DRM device
  10330. * @state: the top-level driver state object
  10331. * @nonblock: nonblocking commit
  10332. *
  10333. * This function commits a top-level state object that has been validated
  10334. * with drm_atomic_helper_check().
  10335. *
  10336. * RETURNS
  10337. * Zero for success or -errno.
  10338. */
  10339. static int intel_atomic_commit(struct drm_device *dev,
  10340. struct drm_atomic_state *state,
  10341. bool nonblock)
  10342. {
  10343. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10344. struct drm_i915_private *dev_priv = to_i915(dev);
  10345. int ret = 0;
  10346. drm_atomic_state_get(state);
  10347. i915_sw_fence_init(&intel_state->commit_ready,
  10348. intel_atomic_commit_ready);
  10349. /*
  10350. * The intel_legacy_cursor_update() fast path takes care
  10351. * of avoiding the vblank waits for simple cursor
  10352. * movement and flips. For cursor on/off and size changes,
  10353. * we want to perform the vblank waits so that watermark
  10354. * updates happen during the correct frames. Gen9+ have
  10355. * double buffered watermarks and so shouldn't need this.
  10356. *
  10357. * Unset state->legacy_cursor_update before the call to
  10358. * drm_atomic_helper_setup_commit() because otherwise
  10359. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10360. * we get FIFO underruns because we didn't wait
  10361. * for vblank.
  10362. *
  10363. * FIXME doing watermarks and fb cleanup from a vblank worker
  10364. * (assuming we had any) would solve these problems.
  10365. */
  10366. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10367. struct intel_crtc_state *new_crtc_state;
  10368. struct intel_crtc *crtc;
  10369. int i;
  10370. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10371. if (new_crtc_state->wm.need_postvbl_update ||
  10372. new_crtc_state->update_wm_post)
  10373. state->legacy_cursor_update = false;
  10374. }
  10375. ret = intel_atomic_prepare_commit(dev, state);
  10376. if (ret) {
  10377. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10378. i915_sw_fence_commit(&intel_state->commit_ready);
  10379. return ret;
  10380. }
  10381. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10382. if (!ret)
  10383. ret = drm_atomic_helper_swap_state(state, true);
  10384. if (ret) {
  10385. i915_sw_fence_commit(&intel_state->commit_ready);
  10386. drm_atomic_helper_cleanup_planes(dev, state);
  10387. return ret;
  10388. }
  10389. dev_priv->wm.distrust_bios_wm = false;
  10390. intel_shared_dpll_swap_state(state);
  10391. intel_atomic_track_fbs(state);
  10392. if (intel_state->modeset) {
  10393. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10394. sizeof(intel_state->min_cdclk));
  10395. memcpy(dev_priv->min_voltage_level,
  10396. intel_state->min_voltage_level,
  10397. sizeof(intel_state->min_voltage_level));
  10398. dev_priv->active_crtcs = intel_state->active_crtcs;
  10399. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10400. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10401. }
  10402. drm_atomic_state_get(state);
  10403. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10404. i915_sw_fence_commit(&intel_state->commit_ready);
  10405. if (nonblock)
  10406. queue_work(system_unbound_wq, &state->commit_work);
  10407. else
  10408. intel_atomic_commit_tail(state);
  10409. return 0;
  10410. }
  10411. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10412. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10413. .set_config = drm_atomic_helper_set_config,
  10414. .destroy = intel_crtc_destroy,
  10415. .page_flip = drm_atomic_helper_page_flip,
  10416. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10417. .atomic_destroy_state = intel_crtc_destroy_state,
  10418. .set_crc_source = intel_crtc_set_crc_source,
  10419. };
  10420. struct wait_rps_boost {
  10421. struct wait_queue_entry wait;
  10422. struct drm_crtc *crtc;
  10423. struct drm_i915_gem_request *request;
  10424. };
  10425. static int do_rps_boost(struct wait_queue_entry *_wait,
  10426. unsigned mode, int sync, void *key)
  10427. {
  10428. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10429. struct drm_i915_gem_request *rq = wait->request;
  10430. gen6_rps_boost(rq, NULL);
  10431. i915_gem_request_put(rq);
  10432. drm_crtc_vblank_put(wait->crtc);
  10433. list_del(&wait->wait.entry);
  10434. kfree(wait);
  10435. return 1;
  10436. }
  10437. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10438. struct dma_fence *fence)
  10439. {
  10440. struct wait_rps_boost *wait;
  10441. if (!dma_fence_is_i915(fence))
  10442. return;
  10443. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10444. return;
  10445. if (drm_crtc_vblank_get(crtc))
  10446. return;
  10447. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10448. if (!wait) {
  10449. drm_crtc_vblank_put(crtc);
  10450. return;
  10451. }
  10452. wait->request = to_request(dma_fence_get(fence));
  10453. wait->crtc = crtc;
  10454. wait->wait.func = do_rps_boost;
  10455. wait->wait.flags = 0;
  10456. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10457. }
  10458. /**
  10459. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10460. * @plane: drm plane to prepare for
  10461. * @fb: framebuffer to prepare for presentation
  10462. *
  10463. * Prepares a framebuffer for usage on a display plane. Generally this
  10464. * involves pinning the underlying object and updating the frontbuffer tracking
  10465. * bits. Some older platforms need special physical address handling for
  10466. * cursor planes.
  10467. *
  10468. * Must be called with struct_mutex held.
  10469. *
  10470. * Returns 0 on success, negative error code on failure.
  10471. */
  10472. int
  10473. intel_prepare_plane_fb(struct drm_plane *plane,
  10474. struct drm_plane_state *new_state)
  10475. {
  10476. struct intel_atomic_state *intel_state =
  10477. to_intel_atomic_state(new_state->state);
  10478. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10479. struct drm_framebuffer *fb = new_state->fb;
  10480. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10481. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10482. int ret;
  10483. if (old_obj) {
  10484. struct drm_crtc_state *crtc_state =
  10485. drm_atomic_get_existing_crtc_state(new_state->state,
  10486. plane->state->crtc);
  10487. /* Big Hammer, we also need to ensure that any pending
  10488. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10489. * current scanout is retired before unpinning the old
  10490. * framebuffer. Note that we rely on userspace rendering
  10491. * into the buffer attached to the pipe they are waiting
  10492. * on. If not, userspace generates a GPU hang with IPEHR
  10493. * point to the MI_WAIT_FOR_EVENT.
  10494. *
  10495. * This should only fail upon a hung GPU, in which case we
  10496. * can safely continue.
  10497. */
  10498. if (needs_modeset(crtc_state)) {
  10499. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10500. old_obj->resv, NULL,
  10501. false, 0,
  10502. GFP_KERNEL);
  10503. if (ret < 0)
  10504. return ret;
  10505. }
  10506. }
  10507. if (new_state->fence) { /* explicit fencing */
  10508. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10509. new_state->fence,
  10510. I915_FENCE_TIMEOUT,
  10511. GFP_KERNEL);
  10512. if (ret < 0)
  10513. return ret;
  10514. }
  10515. if (!obj)
  10516. return 0;
  10517. ret = i915_gem_object_pin_pages(obj);
  10518. if (ret)
  10519. return ret;
  10520. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10521. if (ret) {
  10522. i915_gem_object_unpin_pages(obj);
  10523. return ret;
  10524. }
  10525. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10526. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10527. const int align = intel_cursor_alignment(dev_priv);
  10528. ret = i915_gem_object_attach_phys(obj, align);
  10529. } else {
  10530. struct i915_vma *vma;
  10531. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10532. if (!IS_ERR(vma))
  10533. to_intel_plane_state(new_state)->vma = vma;
  10534. else
  10535. ret = PTR_ERR(vma);
  10536. }
  10537. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10538. mutex_unlock(&dev_priv->drm.struct_mutex);
  10539. i915_gem_object_unpin_pages(obj);
  10540. if (ret)
  10541. return ret;
  10542. if (!new_state->fence) { /* implicit fencing */
  10543. struct dma_fence *fence;
  10544. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10545. obj->resv, NULL,
  10546. false, I915_FENCE_TIMEOUT,
  10547. GFP_KERNEL);
  10548. if (ret < 0)
  10549. return ret;
  10550. fence = reservation_object_get_excl_rcu(obj->resv);
  10551. if (fence) {
  10552. add_rps_boost_after_vblank(new_state->crtc, fence);
  10553. dma_fence_put(fence);
  10554. }
  10555. } else {
  10556. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10557. }
  10558. return 0;
  10559. }
  10560. /**
  10561. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10562. * @plane: drm plane to clean up for
  10563. * @fb: old framebuffer that was on plane
  10564. *
  10565. * Cleans up a framebuffer that has just been removed from a plane.
  10566. *
  10567. * Must be called with struct_mutex held.
  10568. */
  10569. void
  10570. intel_cleanup_plane_fb(struct drm_plane *plane,
  10571. struct drm_plane_state *old_state)
  10572. {
  10573. struct i915_vma *vma;
  10574. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10575. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10576. if (vma) {
  10577. mutex_lock(&plane->dev->struct_mutex);
  10578. intel_unpin_fb_vma(vma);
  10579. mutex_unlock(&plane->dev->struct_mutex);
  10580. }
  10581. }
  10582. int
  10583. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10584. {
  10585. struct drm_i915_private *dev_priv;
  10586. int max_scale;
  10587. int crtc_clock, max_dotclk;
  10588. if (!intel_crtc || !crtc_state->base.enable)
  10589. return DRM_PLANE_HELPER_NO_SCALING;
  10590. dev_priv = to_i915(intel_crtc->base.dev);
  10591. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10592. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10593. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10594. max_dotclk *= 2;
  10595. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10596. return DRM_PLANE_HELPER_NO_SCALING;
  10597. /*
  10598. * skl max scale is lower of:
  10599. * close to 3 but not 3, -1 is for that purpose
  10600. * or
  10601. * cdclk/crtc_clock
  10602. */
  10603. max_scale = min((1 << 16) * 3 - 1,
  10604. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10605. return max_scale;
  10606. }
  10607. static int
  10608. intel_check_primary_plane(struct intel_plane *plane,
  10609. struct intel_crtc_state *crtc_state,
  10610. struct intel_plane_state *state)
  10611. {
  10612. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10613. struct drm_crtc *crtc = state->base.crtc;
  10614. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10615. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10616. bool can_position = false;
  10617. int ret;
  10618. if (INTEL_GEN(dev_priv) >= 9) {
  10619. /* use scaler when colorkey is not required */
  10620. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10621. min_scale = 1;
  10622. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10623. }
  10624. can_position = true;
  10625. }
  10626. ret = drm_atomic_helper_check_plane_state(&state->base,
  10627. &crtc_state->base,
  10628. &state->clip,
  10629. min_scale, max_scale,
  10630. can_position, true);
  10631. if (ret)
  10632. return ret;
  10633. if (!state->base.fb)
  10634. return 0;
  10635. if (INTEL_GEN(dev_priv) >= 9) {
  10636. ret = skl_check_plane_surface(state);
  10637. if (ret)
  10638. return ret;
  10639. state->ctl = skl_plane_ctl(crtc_state, state);
  10640. } else {
  10641. ret = i9xx_check_plane_surface(state);
  10642. if (ret)
  10643. return ret;
  10644. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10645. }
  10646. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10647. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10648. return 0;
  10649. }
  10650. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10651. struct drm_crtc_state *old_crtc_state)
  10652. {
  10653. struct drm_device *dev = crtc->dev;
  10654. struct drm_i915_private *dev_priv = to_i915(dev);
  10655. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10656. struct intel_crtc_state *old_intel_cstate =
  10657. to_intel_crtc_state(old_crtc_state);
  10658. struct intel_atomic_state *old_intel_state =
  10659. to_intel_atomic_state(old_crtc_state->state);
  10660. struct intel_crtc_state *intel_cstate =
  10661. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10662. bool modeset = needs_modeset(&intel_cstate->base);
  10663. if (!modeset &&
  10664. (intel_cstate->base.color_mgmt_changed ||
  10665. intel_cstate->update_pipe)) {
  10666. intel_color_set_csc(&intel_cstate->base);
  10667. intel_color_load_luts(&intel_cstate->base);
  10668. }
  10669. /* Perform vblank evasion around commit operation */
  10670. intel_pipe_update_start(intel_cstate);
  10671. if (modeset)
  10672. goto out;
  10673. if (intel_cstate->update_pipe)
  10674. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10675. else if (INTEL_GEN(dev_priv) >= 9)
  10676. skl_detach_scalers(intel_crtc);
  10677. out:
  10678. if (dev_priv->display.atomic_update_watermarks)
  10679. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10680. intel_cstate);
  10681. }
  10682. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10683. struct drm_crtc_state *old_crtc_state)
  10684. {
  10685. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10687. struct intel_atomic_state *old_intel_state =
  10688. to_intel_atomic_state(old_crtc_state->state);
  10689. struct intel_crtc_state *new_crtc_state =
  10690. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10691. intel_pipe_update_end(new_crtc_state);
  10692. if (new_crtc_state->update_pipe &&
  10693. !needs_modeset(&new_crtc_state->base) &&
  10694. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED) {
  10695. if (!IS_GEN2(dev_priv))
  10696. intel_set_cpu_fifo_underrun_reporting(dev_priv, intel_crtc->pipe, true);
  10697. if (new_crtc_state->has_pch_encoder) {
  10698. enum pipe pch_transcoder =
  10699. intel_crtc_pch_transcoder(intel_crtc);
  10700. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10701. }
  10702. }
  10703. }
  10704. /**
  10705. * intel_plane_destroy - destroy a plane
  10706. * @plane: plane to destroy
  10707. *
  10708. * Common destruction function for all types of planes (primary, cursor,
  10709. * sprite).
  10710. */
  10711. void intel_plane_destroy(struct drm_plane *plane)
  10712. {
  10713. drm_plane_cleanup(plane);
  10714. kfree(to_intel_plane(plane));
  10715. }
  10716. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10717. {
  10718. switch (format) {
  10719. case DRM_FORMAT_C8:
  10720. case DRM_FORMAT_RGB565:
  10721. case DRM_FORMAT_XRGB1555:
  10722. case DRM_FORMAT_XRGB8888:
  10723. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10724. modifier == I915_FORMAT_MOD_X_TILED;
  10725. default:
  10726. return false;
  10727. }
  10728. }
  10729. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10730. {
  10731. switch (format) {
  10732. case DRM_FORMAT_C8:
  10733. case DRM_FORMAT_RGB565:
  10734. case DRM_FORMAT_XRGB8888:
  10735. case DRM_FORMAT_XBGR8888:
  10736. case DRM_FORMAT_XRGB2101010:
  10737. case DRM_FORMAT_XBGR2101010:
  10738. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10739. modifier == I915_FORMAT_MOD_X_TILED;
  10740. default:
  10741. return false;
  10742. }
  10743. }
  10744. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10745. {
  10746. switch (format) {
  10747. case DRM_FORMAT_XRGB8888:
  10748. case DRM_FORMAT_XBGR8888:
  10749. case DRM_FORMAT_ARGB8888:
  10750. case DRM_FORMAT_ABGR8888:
  10751. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10752. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10753. return true;
  10754. /* fall through */
  10755. case DRM_FORMAT_RGB565:
  10756. case DRM_FORMAT_XRGB2101010:
  10757. case DRM_FORMAT_XBGR2101010:
  10758. case DRM_FORMAT_YUYV:
  10759. case DRM_FORMAT_YVYU:
  10760. case DRM_FORMAT_UYVY:
  10761. case DRM_FORMAT_VYUY:
  10762. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10763. return true;
  10764. /* fall through */
  10765. case DRM_FORMAT_C8:
  10766. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10767. modifier == I915_FORMAT_MOD_X_TILED ||
  10768. modifier == I915_FORMAT_MOD_Y_TILED)
  10769. return true;
  10770. /* fall through */
  10771. default:
  10772. return false;
  10773. }
  10774. }
  10775. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10776. uint32_t format,
  10777. uint64_t modifier)
  10778. {
  10779. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10780. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10781. return false;
  10782. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10783. modifier != DRM_FORMAT_MOD_LINEAR)
  10784. return false;
  10785. if (INTEL_GEN(dev_priv) >= 9)
  10786. return skl_mod_supported(format, modifier);
  10787. else if (INTEL_GEN(dev_priv) >= 4)
  10788. return i965_mod_supported(format, modifier);
  10789. else
  10790. return i8xx_mod_supported(format, modifier);
  10791. unreachable();
  10792. }
  10793. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10794. uint32_t format,
  10795. uint64_t modifier)
  10796. {
  10797. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10798. return false;
  10799. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10800. }
  10801. static struct drm_plane_funcs intel_plane_funcs = {
  10802. .update_plane = drm_atomic_helper_update_plane,
  10803. .disable_plane = drm_atomic_helper_disable_plane,
  10804. .destroy = intel_plane_destroy,
  10805. .atomic_get_property = intel_plane_atomic_get_property,
  10806. .atomic_set_property = intel_plane_atomic_set_property,
  10807. .atomic_duplicate_state = intel_plane_duplicate_state,
  10808. .atomic_destroy_state = intel_plane_destroy_state,
  10809. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10810. };
  10811. static int
  10812. intel_legacy_cursor_update(struct drm_plane *plane,
  10813. struct drm_crtc *crtc,
  10814. struct drm_framebuffer *fb,
  10815. int crtc_x, int crtc_y,
  10816. unsigned int crtc_w, unsigned int crtc_h,
  10817. uint32_t src_x, uint32_t src_y,
  10818. uint32_t src_w, uint32_t src_h,
  10819. struct drm_modeset_acquire_ctx *ctx)
  10820. {
  10821. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10822. int ret;
  10823. struct drm_plane_state *old_plane_state, *new_plane_state;
  10824. struct intel_plane *intel_plane = to_intel_plane(plane);
  10825. struct drm_framebuffer *old_fb;
  10826. struct drm_crtc_state *crtc_state = crtc->state;
  10827. struct i915_vma *old_vma, *vma;
  10828. /*
  10829. * When crtc is inactive or there is a modeset pending,
  10830. * wait for it to complete in the slowpath
  10831. */
  10832. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10833. to_intel_crtc_state(crtc_state)->update_pipe)
  10834. goto slow;
  10835. old_plane_state = plane->state;
  10836. /*
  10837. * Don't do an async update if there is an outstanding commit modifying
  10838. * the plane. This prevents our async update's changes from getting
  10839. * overridden by a previous synchronous update's state.
  10840. */
  10841. if (old_plane_state->commit &&
  10842. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  10843. goto slow;
  10844. /*
  10845. * If any parameters change that may affect watermarks,
  10846. * take the slowpath. Only changing fb or position should be
  10847. * in the fastpath.
  10848. */
  10849. if (old_plane_state->crtc != crtc ||
  10850. old_plane_state->src_w != src_w ||
  10851. old_plane_state->src_h != src_h ||
  10852. old_plane_state->crtc_w != crtc_w ||
  10853. old_plane_state->crtc_h != crtc_h ||
  10854. !old_plane_state->fb != !fb)
  10855. goto slow;
  10856. new_plane_state = intel_plane_duplicate_state(plane);
  10857. if (!new_plane_state)
  10858. return -ENOMEM;
  10859. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10860. new_plane_state->src_x = src_x;
  10861. new_plane_state->src_y = src_y;
  10862. new_plane_state->src_w = src_w;
  10863. new_plane_state->src_h = src_h;
  10864. new_plane_state->crtc_x = crtc_x;
  10865. new_plane_state->crtc_y = crtc_y;
  10866. new_plane_state->crtc_w = crtc_w;
  10867. new_plane_state->crtc_h = crtc_h;
  10868. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10869. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  10870. to_intel_plane_state(plane->state),
  10871. to_intel_plane_state(new_plane_state));
  10872. if (ret)
  10873. goto out_free;
  10874. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10875. if (ret)
  10876. goto out_free;
  10877. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10878. int align = intel_cursor_alignment(dev_priv);
  10879. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10880. if (ret) {
  10881. DRM_DEBUG_KMS("failed to attach phys object\n");
  10882. goto out_unlock;
  10883. }
  10884. } else {
  10885. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10886. if (IS_ERR(vma)) {
  10887. DRM_DEBUG_KMS("failed to pin object\n");
  10888. ret = PTR_ERR(vma);
  10889. goto out_unlock;
  10890. }
  10891. to_intel_plane_state(new_plane_state)->vma = vma;
  10892. }
  10893. old_fb = old_plane_state->fb;
  10894. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10895. intel_plane->frontbuffer_bit);
  10896. /* Swap plane state */
  10897. plane->state = new_plane_state;
  10898. if (plane->state->visible) {
  10899. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10900. intel_plane->update_plane(intel_plane,
  10901. to_intel_crtc_state(crtc->state),
  10902. to_intel_plane_state(plane->state));
  10903. } else {
  10904. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10905. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10906. }
  10907. old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
  10908. if (old_vma)
  10909. intel_unpin_fb_vma(old_vma);
  10910. out_unlock:
  10911. mutex_unlock(&dev_priv->drm.struct_mutex);
  10912. out_free:
  10913. if (ret)
  10914. intel_plane_destroy_state(plane, new_plane_state);
  10915. else
  10916. intel_plane_destroy_state(plane, old_plane_state);
  10917. return ret;
  10918. slow:
  10919. return drm_atomic_helper_update_plane(plane, crtc, fb,
  10920. crtc_x, crtc_y, crtc_w, crtc_h,
  10921. src_x, src_y, src_w, src_h, ctx);
  10922. }
  10923. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10924. .update_plane = intel_legacy_cursor_update,
  10925. .disable_plane = drm_atomic_helper_disable_plane,
  10926. .destroy = intel_plane_destroy,
  10927. .atomic_get_property = intel_plane_atomic_get_property,
  10928. .atomic_set_property = intel_plane_atomic_set_property,
  10929. .atomic_duplicate_state = intel_plane_duplicate_state,
  10930. .atomic_destroy_state = intel_plane_destroy_state,
  10931. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  10932. };
  10933. static struct intel_plane *
  10934. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  10935. {
  10936. struct intel_plane *primary = NULL;
  10937. struct intel_plane_state *state = NULL;
  10938. const uint32_t *intel_primary_formats;
  10939. unsigned int supported_rotations;
  10940. unsigned int num_formats;
  10941. const uint64_t *modifiers;
  10942. int ret;
  10943. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10944. if (!primary) {
  10945. ret = -ENOMEM;
  10946. goto fail;
  10947. }
  10948. state = intel_create_plane_state(&primary->base);
  10949. if (!state) {
  10950. ret = -ENOMEM;
  10951. goto fail;
  10952. }
  10953. primary->base.state = &state->base;
  10954. primary->can_scale = false;
  10955. primary->max_downscale = 1;
  10956. if (INTEL_GEN(dev_priv) >= 9) {
  10957. primary->can_scale = true;
  10958. state->scaler_id = -1;
  10959. }
  10960. primary->pipe = pipe;
  10961. /*
  10962. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  10963. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  10964. */
  10965. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  10966. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  10967. else
  10968. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  10969. primary->id = PLANE_PRIMARY;
  10970. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10971. primary->check_plane = intel_check_primary_plane;
  10972. if (INTEL_GEN(dev_priv) >= 10) {
  10973. intel_primary_formats = skl_primary_formats;
  10974. num_formats = ARRAY_SIZE(skl_primary_formats);
  10975. modifiers = skl_format_modifiers_ccs;
  10976. primary->update_plane = skl_update_plane;
  10977. primary->disable_plane = skl_disable_plane;
  10978. primary->get_hw_state = skl_plane_get_hw_state;
  10979. } else if (INTEL_GEN(dev_priv) >= 9) {
  10980. intel_primary_formats = skl_primary_formats;
  10981. num_formats = ARRAY_SIZE(skl_primary_formats);
  10982. if (pipe < PIPE_C)
  10983. modifiers = skl_format_modifiers_ccs;
  10984. else
  10985. modifiers = skl_format_modifiers_noccs;
  10986. primary->update_plane = skl_update_plane;
  10987. primary->disable_plane = skl_disable_plane;
  10988. primary->get_hw_state = skl_plane_get_hw_state;
  10989. } else if (INTEL_GEN(dev_priv) >= 4) {
  10990. intel_primary_formats = i965_primary_formats;
  10991. num_formats = ARRAY_SIZE(i965_primary_formats);
  10992. modifiers = i9xx_format_modifiers;
  10993. primary->update_plane = i9xx_update_plane;
  10994. primary->disable_plane = i9xx_disable_plane;
  10995. primary->get_hw_state = i9xx_plane_get_hw_state;
  10996. } else {
  10997. intel_primary_formats = i8xx_primary_formats;
  10998. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  10999. modifiers = i9xx_format_modifiers;
  11000. primary->update_plane = i9xx_update_plane;
  11001. primary->disable_plane = i9xx_disable_plane;
  11002. primary->get_hw_state = i9xx_plane_get_hw_state;
  11003. }
  11004. if (INTEL_GEN(dev_priv) >= 9)
  11005. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11006. 0, &intel_plane_funcs,
  11007. intel_primary_formats, num_formats,
  11008. modifiers,
  11009. DRM_PLANE_TYPE_PRIMARY,
  11010. "plane 1%c", pipe_name(pipe));
  11011. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11012. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11013. 0, &intel_plane_funcs,
  11014. intel_primary_formats, num_formats,
  11015. modifiers,
  11016. DRM_PLANE_TYPE_PRIMARY,
  11017. "primary %c", pipe_name(pipe));
  11018. else
  11019. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11020. 0, &intel_plane_funcs,
  11021. intel_primary_formats, num_formats,
  11022. modifiers,
  11023. DRM_PLANE_TYPE_PRIMARY,
  11024. "plane %c",
  11025. plane_name(primary->i9xx_plane));
  11026. if (ret)
  11027. goto fail;
  11028. if (INTEL_GEN(dev_priv) >= 10) {
  11029. supported_rotations =
  11030. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11031. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11032. DRM_MODE_REFLECT_X;
  11033. } else if (INTEL_GEN(dev_priv) >= 9) {
  11034. supported_rotations =
  11035. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11036. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11037. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11038. supported_rotations =
  11039. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11040. DRM_MODE_REFLECT_X;
  11041. } else if (INTEL_GEN(dev_priv) >= 4) {
  11042. supported_rotations =
  11043. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11044. } else {
  11045. supported_rotations = DRM_MODE_ROTATE_0;
  11046. }
  11047. if (INTEL_GEN(dev_priv) >= 4)
  11048. drm_plane_create_rotation_property(&primary->base,
  11049. DRM_MODE_ROTATE_0,
  11050. supported_rotations);
  11051. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11052. return primary;
  11053. fail:
  11054. kfree(state);
  11055. kfree(primary);
  11056. return ERR_PTR(ret);
  11057. }
  11058. static struct intel_plane *
  11059. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11060. enum pipe pipe)
  11061. {
  11062. struct intel_plane *cursor = NULL;
  11063. struct intel_plane_state *state = NULL;
  11064. int ret;
  11065. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11066. if (!cursor) {
  11067. ret = -ENOMEM;
  11068. goto fail;
  11069. }
  11070. state = intel_create_plane_state(&cursor->base);
  11071. if (!state) {
  11072. ret = -ENOMEM;
  11073. goto fail;
  11074. }
  11075. cursor->base.state = &state->base;
  11076. cursor->can_scale = false;
  11077. cursor->max_downscale = 1;
  11078. cursor->pipe = pipe;
  11079. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11080. cursor->id = PLANE_CURSOR;
  11081. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11082. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11083. cursor->update_plane = i845_update_cursor;
  11084. cursor->disable_plane = i845_disable_cursor;
  11085. cursor->get_hw_state = i845_cursor_get_hw_state;
  11086. cursor->check_plane = i845_check_cursor;
  11087. } else {
  11088. cursor->update_plane = i9xx_update_cursor;
  11089. cursor->disable_plane = i9xx_disable_cursor;
  11090. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11091. cursor->check_plane = i9xx_check_cursor;
  11092. }
  11093. cursor->cursor.base = ~0;
  11094. cursor->cursor.cntl = ~0;
  11095. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11096. cursor->cursor.size = ~0;
  11097. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11098. 0, &intel_cursor_plane_funcs,
  11099. intel_cursor_formats,
  11100. ARRAY_SIZE(intel_cursor_formats),
  11101. cursor_format_modifiers,
  11102. DRM_PLANE_TYPE_CURSOR,
  11103. "cursor %c", pipe_name(pipe));
  11104. if (ret)
  11105. goto fail;
  11106. if (INTEL_GEN(dev_priv) >= 4)
  11107. drm_plane_create_rotation_property(&cursor->base,
  11108. DRM_MODE_ROTATE_0,
  11109. DRM_MODE_ROTATE_0 |
  11110. DRM_MODE_ROTATE_180);
  11111. if (INTEL_GEN(dev_priv) >= 9)
  11112. state->scaler_id = -1;
  11113. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11114. return cursor;
  11115. fail:
  11116. kfree(state);
  11117. kfree(cursor);
  11118. return ERR_PTR(ret);
  11119. }
  11120. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11121. struct intel_crtc_state *crtc_state)
  11122. {
  11123. struct intel_crtc_scaler_state *scaler_state =
  11124. &crtc_state->scaler_state;
  11125. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11126. int i;
  11127. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11128. if (!crtc->num_scalers)
  11129. return;
  11130. for (i = 0; i < crtc->num_scalers; i++) {
  11131. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11132. scaler->in_use = 0;
  11133. scaler->mode = PS_SCALER_MODE_DYN;
  11134. }
  11135. scaler_state->scaler_id = -1;
  11136. }
  11137. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11138. {
  11139. struct intel_crtc *intel_crtc;
  11140. struct intel_crtc_state *crtc_state = NULL;
  11141. struct intel_plane *primary = NULL;
  11142. struct intel_plane *cursor = NULL;
  11143. int sprite, ret;
  11144. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11145. if (!intel_crtc)
  11146. return -ENOMEM;
  11147. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11148. if (!crtc_state) {
  11149. ret = -ENOMEM;
  11150. goto fail;
  11151. }
  11152. intel_crtc->config = crtc_state;
  11153. intel_crtc->base.state = &crtc_state->base;
  11154. crtc_state->base.crtc = &intel_crtc->base;
  11155. primary = intel_primary_plane_create(dev_priv, pipe);
  11156. if (IS_ERR(primary)) {
  11157. ret = PTR_ERR(primary);
  11158. goto fail;
  11159. }
  11160. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11161. for_each_sprite(dev_priv, pipe, sprite) {
  11162. struct intel_plane *plane;
  11163. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11164. if (IS_ERR(plane)) {
  11165. ret = PTR_ERR(plane);
  11166. goto fail;
  11167. }
  11168. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11169. }
  11170. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11171. if (IS_ERR(cursor)) {
  11172. ret = PTR_ERR(cursor);
  11173. goto fail;
  11174. }
  11175. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11176. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11177. &primary->base, &cursor->base,
  11178. &intel_crtc_funcs,
  11179. "pipe %c", pipe_name(pipe));
  11180. if (ret)
  11181. goto fail;
  11182. intel_crtc->pipe = pipe;
  11183. /* initialize shared scalers */
  11184. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11185. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11186. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] != NULL);
  11187. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] = intel_crtc;
  11188. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11189. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11190. intel_color_init(&intel_crtc->base);
  11191. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11192. return 0;
  11193. fail:
  11194. /*
  11195. * drm_mode_config_cleanup() will free up any
  11196. * crtcs/planes already initialized.
  11197. */
  11198. kfree(crtc_state);
  11199. kfree(intel_crtc);
  11200. return ret;
  11201. }
  11202. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11203. {
  11204. struct drm_device *dev = connector->base.dev;
  11205. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11206. if (!connector->base.state->crtc)
  11207. return INVALID_PIPE;
  11208. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11209. }
  11210. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11211. struct drm_file *file)
  11212. {
  11213. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11214. struct drm_crtc *drmmode_crtc;
  11215. struct intel_crtc *crtc;
  11216. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11217. if (!drmmode_crtc)
  11218. return -ENOENT;
  11219. crtc = to_intel_crtc(drmmode_crtc);
  11220. pipe_from_crtc_id->pipe = crtc->pipe;
  11221. return 0;
  11222. }
  11223. static int intel_encoder_clones(struct intel_encoder *encoder)
  11224. {
  11225. struct drm_device *dev = encoder->base.dev;
  11226. struct intel_encoder *source_encoder;
  11227. int index_mask = 0;
  11228. int entry = 0;
  11229. for_each_intel_encoder(dev, source_encoder) {
  11230. if (encoders_cloneable(encoder, source_encoder))
  11231. index_mask |= (1 << entry);
  11232. entry++;
  11233. }
  11234. return index_mask;
  11235. }
  11236. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11237. {
  11238. if (!IS_MOBILE(dev_priv))
  11239. return false;
  11240. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11241. return false;
  11242. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11243. return false;
  11244. return true;
  11245. }
  11246. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11247. {
  11248. if (INTEL_GEN(dev_priv) >= 9)
  11249. return false;
  11250. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11251. return false;
  11252. if (IS_CHERRYVIEW(dev_priv))
  11253. return false;
  11254. if (HAS_PCH_LPT_H(dev_priv) &&
  11255. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11256. return false;
  11257. /* DDI E can't be used if DDI A requires 4 lanes */
  11258. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11259. return false;
  11260. if (!dev_priv->vbt.int_crt_support)
  11261. return false;
  11262. return true;
  11263. }
  11264. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11265. {
  11266. int pps_num;
  11267. int pps_idx;
  11268. if (HAS_DDI(dev_priv))
  11269. return;
  11270. /*
  11271. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11272. * everywhere where registers can be write protected.
  11273. */
  11274. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11275. pps_num = 2;
  11276. else
  11277. pps_num = 1;
  11278. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11279. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11280. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11281. I915_WRITE(PP_CONTROL(pps_idx), val);
  11282. }
  11283. }
  11284. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11285. {
  11286. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11287. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11288. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11289. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11290. else
  11291. dev_priv->pps_mmio_base = PPS_BASE;
  11292. intel_pps_unlock_regs_wa(dev_priv);
  11293. }
  11294. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11295. {
  11296. struct intel_encoder *encoder;
  11297. bool dpd_is_edp = false;
  11298. intel_pps_init(dev_priv);
  11299. /*
  11300. * intel_edp_init_connector() depends on this completing first, to
  11301. * prevent the registeration of both eDP and LVDS and the incorrect
  11302. * sharing of the PPS.
  11303. */
  11304. intel_lvds_init(dev_priv);
  11305. if (intel_crt_present(dev_priv))
  11306. intel_crt_init(dev_priv);
  11307. if (IS_GEN9_LP(dev_priv)) {
  11308. /*
  11309. * FIXME: Broxton doesn't support port detection via the
  11310. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11311. * detect the ports.
  11312. */
  11313. intel_ddi_init(dev_priv, PORT_A);
  11314. intel_ddi_init(dev_priv, PORT_B);
  11315. intel_ddi_init(dev_priv, PORT_C);
  11316. intel_dsi_init(dev_priv);
  11317. } else if (HAS_DDI(dev_priv)) {
  11318. int found;
  11319. /*
  11320. * Haswell uses DDI functions to detect digital outputs.
  11321. * On SKL pre-D0 the strap isn't connected, so we assume
  11322. * it's there.
  11323. */
  11324. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11325. /* WaIgnoreDDIAStrap: skl */
  11326. if (found || IS_GEN9_BC(dev_priv))
  11327. intel_ddi_init(dev_priv, PORT_A);
  11328. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11329. * register */
  11330. found = I915_READ(SFUSE_STRAP);
  11331. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11332. intel_ddi_init(dev_priv, PORT_B);
  11333. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11334. intel_ddi_init(dev_priv, PORT_C);
  11335. if (found & SFUSE_STRAP_DDID_DETECTED)
  11336. intel_ddi_init(dev_priv, PORT_D);
  11337. /*
  11338. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11339. */
  11340. if (IS_GEN9_BC(dev_priv) &&
  11341. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11342. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11343. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11344. intel_ddi_init(dev_priv, PORT_E);
  11345. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11346. int found;
  11347. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11348. if (has_edp_a(dev_priv))
  11349. intel_dp_init(dev_priv, DP_A, PORT_A);
  11350. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11351. /* PCH SDVOB multiplex with HDMIB */
  11352. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11353. if (!found)
  11354. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11355. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11356. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11357. }
  11358. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11359. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11360. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11361. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11362. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11363. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11364. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11365. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11366. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11367. bool has_edp, has_port;
  11368. /*
  11369. * The DP_DETECTED bit is the latched state of the DDC
  11370. * SDA pin at boot. However since eDP doesn't require DDC
  11371. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11372. * eDP ports may have been muxed to an alternate function.
  11373. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11374. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11375. * detect eDP ports.
  11376. *
  11377. * Sadly the straps seem to be missing sometimes even for HDMI
  11378. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11379. * and VBT for the presence of the port. Additionally we can't
  11380. * trust the port type the VBT declares as we've seen at least
  11381. * HDMI ports that the VBT claim are DP or eDP.
  11382. */
  11383. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11384. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11385. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11386. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11387. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11388. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11389. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11390. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11391. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11392. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11393. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11394. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11395. if (IS_CHERRYVIEW(dev_priv)) {
  11396. /*
  11397. * eDP not supported on port D,
  11398. * so no need to worry about it
  11399. */
  11400. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11401. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11402. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11403. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11404. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11405. }
  11406. intel_dsi_init(dev_priv);
  11407. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11408. bool found = false;
  11409. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11410. DRM_DEBUG_KMS("probing SDVOB\n");
  11411. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11412. if (!found && IS_G4X(dev_priv)) {
  11413. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11414. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11415. }
  11416. if (!found && IS_G4X(dev_priv))
  11417. intel_dp_init(dev_priv, DP_B, PORT_B);
  11418. }
  11419. /* Before G4X SDVOC doesn't have its own detect register */
  11420. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11421. DRM_DEBUG_KMS("probing SDVOC\n");
  11422. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11423. }
  11424. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11425. if (IS_G4X(dev_priv)) {
  11426. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11427. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11428. }
  11429. if (IS_G4X(dev_priv))
  11430. intel_dp_init(dev_priv, DP_C, PORT_C);
  11431. }
  11432. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11433. intel_dp_init(dev_priv, DP_D, PORT_D);
  11434. } else if (IS_GEN2(dev_priv))
  11435. intel_dvo_init(dev_priv);
  11436. if (SUPPORTS_TV(dev_priv))
  11437. intel_tv_init(dev_priv);
  11438. intel_psr_init(dev_priv);
  11439. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11440. encoder->base.possible_crtcs = encoder->crtc_mask;
  11441. encoder->base.possible_clones =
  11442. intel_encoder_clones(encoder);
  11443. }
  11444. intel_init_pch_refclk(dev_priv);
  11445. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11446. }
  11447. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11448. {
  11449. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11450. drm_framebuffer_cleanup(fb);
  11451. i915_gem_object_lock(intel_fb->obj);
  11452. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11453. i915_gem_object_unlock(intel_fb->obj);
  11454. i915_gem_object_put(intel_fb->obj);
  11455. kfree(intel_fb);
  11456. }
  11457. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11458. struct drm_file *file,
  11459. unsigned int *handle)
  11460. {
  11461. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11462. struct drm_i915_gem_object *obj = intel_fb->obj;
  11463. if (obj->userptr.mm) {
  11464. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11465. return -EINVAL;
  11466. }
  11467. return drm_gem_handle_create(file, &obj->base, handle);
  11468. }
  11469. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11470. struct drm_file *file,
  11471. unsigned flags, unsigned color,
  11472. struct drm_clip_rect *clips,
  11473. unsigned num_clips)
  11474. {
  11475. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11476. i915_gem_object_flush_if_display(obj);
  11477. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11478. return 0;
  11479. }
  11480. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11481. .destroy = intel_user_framebuffer_destroy,
  11482. .create_handle = intel_user_framebuffer_create_handle,
  11483. .dirty = intel_user_framebuffer_dirty,
  11484. };
  11485. static
  11486. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11487. uint64_t fb_modifier, uint32_t pixel_format)
  11488. {
  11489. u32 gen = INTEL_GEN(dev_priv);
  11490. if (gen >= 9) {
  11491. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11492. /* "The stride in bytes must not exceed the of the size of 8K
  11493. * pixels and 32K bytes."
  11494. */
  11495. return min(8192 * cpp, 32768);
  11496. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11497. return 32*1024;
  11498. } else if (gen >= 4) {
  11499. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11500. return 16*1024;
  11501. else
  11502. return 32*1024;
  11503. } else if (gen >= 3) {
  11504. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11505. return 8*1024;
  11506. else
  11507. return 16*1024;
  11508. } else {
  11509. /* XXX DSPC is limited to 4k tiled */
  11510. return 8*1024;
  11511. }
  11512. }
  11513. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11514. struct drm_i915_gem_object *obj,
  11515. struct drm_mode_fb_cmd2 *mode_cmd)
  11516. {
  11517. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11518. struct drm_framebuffer *fb = &intel_fb->base;
  11519. struct drm_format_name_buf format_name;
  11520. u32 pitch_limit;
  11521. unsigned int tiling, stride;
  11522. int ret = -EINVAL;
  11523. int i;
  11524. i915_gem_object_lock(obj);
  11525. obj->framebuffer_references++;
  11526. tiling = i915_gem_object_get_tiling(obj);
  11527. stride = i915_gem_object_get_stride(obj);
  11528. i915_gem_object_unlock(obj);
  11529. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11530. /*
  11531. * If there's a fence, enforce that
  11532. * the fb modifier and tiling mode match.
  11533. */
  11534. if (tiling != I915_TILING_NONE &&
  11535. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11536. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11537. goto err;
  11538. }
  11539. } else {
  11540. if (tiling == I915_TILING_X) {
  11541. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11542. } else if (tiling == I915_TILING_Y) {
  11543. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11544. goto err;
  11545. }
  11546. }
  11547. /* Passed in modifier sanity checking. */
  11548. switch (mode_cmd->modifier[0]) {
  11549. case I915_FORMAT_MOD_Y_TILED_CCS:
  11550. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11551. switch (mode_cmd->pixel_format) {
  11552. case DRM_FORMAT_XBGR8888:
  11553. case DRM_FORMAT_ABGR8888:
  11554. case DRM_FORMAT_XRGB8888:
  11555. case DRM_FORMAT_ARGB8888:
  11556. break;
  11557. default:
  11558. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11559. goto err;
  11560. }
  11561. /* fall through */
  11562. case I915_FORMAT_MOD_Y_TILED:
  11563. case I915_FORMAT_MOD_Yf_TILED:
  11564. if (INTEL_GEN(dev_priv) < 9) {
  11565. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11566. mode_cmd->modifier[0]);
  11567. goto err;
  11568. }
  11569. case DRM_FORMAT_MOD_LINEAR:
  11570. case I915_FORMAT_MOD_X_TILED:
  11571. break;
  11572. default:
  11573. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11574. mode_cmd->modifier[0]);
  11575. goto err;
  11576. }
  11577. /*
  11578. * gen2/3 display engine uses the fence if present,
  11579. * so the tiling mode must match the fb modifier exactly.
  11580. */
  11581. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11582. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11583. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11584. goto err;
  11585. }
  11586. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11587. mode_cmd->pixel_format);
  11588. if (mode_cmd->pitches[0] > pitch_limit) {
  11589. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11590. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11591. "tiled" : "linear",
  11592. mode_cmd->pitches[0], pitch_limit);
  11593. goto err;
  11594. }
  11595. /*
  11596. * If there's a fence, enforce that
  11597. * the fb pitch and fence stride match.
  11598. */
  11599. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11600. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11601. mode_cmd->pitches[0], stride);
  11602. goto err;
  11603. }
  11604. /* Reject formats not supported by any plane early. */
  11605. switch (mode_cmd->pixel_format) {
  11606. case DRM_FORMAT_C8:
  11607. case DRM_FORMAT_RGB565:
  11608. case DRM_FORMAT_XRGB8888:
  11609. case DRM_FORMAT_ARGB8888:
  11610. break;
  11611. case DRM_FORMAT_XRGB1555:
  11612. if (INTEL_GEN(dev_priv) > 3) {
  11613. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11614. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11615. goto err;
  11616. }
  11617. break;
  11618. case DRM_FORMAT_ABGR8888:
  11619. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11620. INTEL_GEN(dev_priv) < 9) {
  11621. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11622. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11623. goto err;
  11624. }
  11625. break;
  11626. case DRM_FORMAT_XBGR8888:
  11627. case DRM_FORMAT_XRGB2101010:
  11628. case DRM_FORMAT_XBGR2101010:
  11629. if (INTEL_GEN(dev_priv) < 4) {
  11630. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11631. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11632. goto err;
  11633. }
  11634. break;
  11635. case DRM_FORMAT_ABGR2101010:
  11636. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11637. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11638. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11639. goto err;
  11640. }
  11641. break;
  11642. case DRM_FORMAT_YUYV:
  11643. case DRM_FORMAT_UYVY:
  11644. case DRM_FORMAT_YVYU:
  11645. case DRM_FORMAT_VYUY:
  11646. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11647. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11648. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11649. goto err;
  11650. }
  11651. break;
  11652. default:
  11653. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11654. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11655. goto err;
  11656. }
  11657. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11658. if (mode_cmd->offsets[0] != 0)
  11659. goto err;
  11660. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11661. for (i = 0; i < fb->format->num_planes; i++) {
  11662. u32 stride_alignment;
  11663. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11664. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11665. goto err;
  11666. }
  11667. stride_alignment = intel_fb_stride_alignment(fb, i);
  11668. /*
  11669. * Display WA #0531: skl,bxt,kbl,glk
  11670. *
  11671. * Render decompression and plane width > 3840
  11672. * combined with horizontal panning requires the
  11673. * plane stride to be a multiple of 4. We'll just
  11674. * require the entire fb to accommodate that to avoid
  11675. * potential runtime errors at plane configuration time.
  11676. */
  11677. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11678. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11679. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11680. stride_alignment *= 4;
  11681. if (fb->pitches[i] & (stride_alignment - 1)) {
  11682. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11683. i, fb->pitches[i], stride_alignment);
  11684. goto err;
  11685. }
  11686. }
  11687. intel_fb->obj = obj;
  11688. ret = intel_fill_fb_info(dev_priv, fb);
  11689. if (ret)
  11690. goto err;
  11691. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11692. if (ret) {
  11693. DRM_ERROR("framebuffer init failed %d\n", ret);
  11694. goto err;
  11695. }
  11696. return 0;
  11697. err:
  11698. i915_gem_object_lock(obj);
  11699. obj->framebuffer_references--;
  11700. i915_gem_object_unlock(obj);
  11701. return ret;
  11702. }
  11703. static struct drm_framebuffer *
  11704. intel_user_framebuffer_create(struct drm_device *dev,
  11705. struct drm_file *filp,
  11706. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11707. {
  11708. struct drm_framebuffer *fb;
  11709. struct drm_i915_gem_object *obj;
  11710. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11711. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11712. if (!obj)
  11713. return ERR_PTR(-ENOENT);
  11714. fb = intel_framebuffer_create(obj, &mode_cmd);
  11715. if (IS_ERR(fb))
  11716. i915_gem_object_put(obj);
  11717. return fb;
  11718. }
  11719. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11720. {
  11721. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11722. drm_atomic_state_default_release(state);
  11723. i915_sw_fence_fini(&intel_state->commit_ready);
  11724. kfree(state);
  11725. }
  11726. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11727. .fb_create = intel_user_framebuffer_create,
  11728. .get_format_info = intel_get_format_info,
  11729. .output_poll_changed = intel_fbdev_output_poll_changed,
  11730. .atomic_check = intel_atomic_check,
  11731. .atomic_commit = intel_atomic_commit,
  11732. .atomic_state_alloc = intel_atomic_state_alloc,
  11733. .atomic_state_clear = intel_atomic_state_clear,
  11734. .atomic_state_free = intel_atomic_state_free,
  11735. };
  11736. /**
  11737. * intel_init_display_hooks - initialize the display modesetting hooks
  11738. * @dev_priv: device private
  11739. */
  11740. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11741. {
  11742. intel_init_cdclk_hooks(dev_priv);
  11743. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11744. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11745. dev_priv->display.get_initial_plane_config =
  11746. skylake_get_initial_plane_config;
  11747. dev_priv->display.crtc_compute_clock =
  11748. haswell_crtc_compute_clock;
  11749. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11750. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11751. } else if (HAS_DDI(dev_priv)) {
  11752. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11753. dev_priv->display.get_initial_plane_config =
  11754. i9xx_get_initial_plane_config;
  11755. dev_priv->display.crtc_compute_clock =
  11756. haswell_crtc_compute_clock;
  11757. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11758. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11759. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11760. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11761. dev_priv->display.get_initial_plane_config =
  11762. i9xx_get_initial_plane_config;
  11763. dev_priv->display.crtc_compute_clock =
  11764. ironlake_crtc_compute_clock;
  11765. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11766. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11767. } else if (IS_CHERRYVIEW(dev_priv)) {
  11768. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11769. dev_priv->display.get_initial_plane_config =
  11770. i9xx_get_initial_plane_config;
  11771. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11772. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11773. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11774. } else if (IS_VALLEYVIEW(dev_priv)) {
  11775. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11776. dev_priv->display.get_initial_plane_config =
  11777. i9xx_get_initial_plane_config;
  11778. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11779. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11780. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11781. } else if (IS_G4X(dev_priv)) {
  11782. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11783. dev_priv->display.get_initial_plane_config =
  11784. i9xx_get_initial_plane_config;
  11785. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11786. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11787. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11788. } else if (IS_PINEVIEW(dev_priv)) {
  11789. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11790. dev_priv->display.get_initial_plane_config =
  11791. i9xx_get_initial_plane_config;
  11792. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11793. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11794. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11795. } else if (!IS_GEN2(dev_priv)) {
  11796. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11797. dev_priv->display.get_initial_plane_config =
  11798. i9xx_get_initial_plane_config;
  11799. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11800. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11801. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11802. } else {
  11803. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11804. dev_priv->display.get_initial_plane_config =
  11805. i9xx_get_initial_plane_config;
  11806. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11807. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11808. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11809. }
  11810. if (IS_GEN5(dev_priv)) {
  11811. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11812. } else if (IS_GEN6(dev_priv)) {
  11813. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11814. } else if (IS_IVYBRIDGE(dev_priv)) {
  11815. /* FIXME: detect B0+ stepping and use auto training */
  11816. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11817. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11818. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11819. }
  11820. if (INTEL_GEN(dev_priv) >= 9)
  11821. dev_priv->display.update_crtcs = skl_update_crtcs;
  11822. else
  11823. dev_priv->display.update_crtcs = intel_update_crtcs;
  11824. }
  11825. /*
  11826. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11827. */
  11828. static void quirk_ssc_force_disable(struct drm_device *dev)
  11829. {
  11830. struct drm_i915_private *dev_priv = to_i915(dev);
  11831. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11832. DRM_INFO("applying lvds SSC disable quirk\n");
  11833. }
  11834. /*
  11835. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11836. * brightness value
  11837. */
  11838. static void quirk_invert_brightness(struct drm_device *dev)
  11839. {
  11840. struct drm_i915_private *dev_priv = to_i915(dev);
  11841. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11842. DRM_INFO("applying inverted panel brightness quirk\n");
  11843. }
  11844. /* Some VBT's incorrectly indicate no backlight is present */
  11845. static void quirk_backlight_present(struct drm_device *dev)
  11846. {
  11847. struct drm_i915_private *dev_priv = to_i915(dev);
  11848. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11849. DRM_INFO("applying backlight present quirk\n");
  11850. }
  11851. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11852. * which is 300 ms greater than eDP spec T12 min.
  11853. */
  11854. static void quirk_increase_t12_delay(struct drm_device *dev)
  11855. {
  11856. struct drm_i915_private *dev_priv = to_i915(dev);
  11857. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11858. DRM_INFO("Applying T12 delay quirk\n");
  11859. }
  11860. struct intel_quirk {
  11861. int device;
  11862. int subsystem_vendor;
  11863. int subsystem_device;
  11864. void (*hook)(struct drm_device *dev);
  11865. };
  11866. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11867. struct intel_dmi_quirk {
  11868. void (*hook)(struct drm_device *dev);
  11869. const struct dmi_system_id (*dmi_id_list)[];
  11870. };
  11871. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11872. {
  11873. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11874. return 1;
  11875. }
  11876. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11877. {
  11878. .dmi_id_list = &(const struct dmi_system_id[]) {
  11879. {
  11880. .callback = intel_dmi_reverse_brightness,
  11881. .ident = "NCR Corporation",
  11882. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11883. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11884. },
  11885. },
  11886. { } /* terminating entry */
  11887. },
  11888. .hook = quirk_invert_brightness,
  11889. },
  11890. };
  11891. static struct intel_quirk intel_quirks[] = {
  11892. /* Lenovo U160 cannot use SSC on LVDS */
  11893. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11894. /* Sony Vaio Y cannot use SSC on LVDS */
  11895. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11896. /* Acer Aspire 5734Z must invert backlight brightness */
  11897. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11898. /* Acer/eMachines G725 */
  11899. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11900. /* Acer/eMachines e725 */
  11901. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11902. /* Acer/Packard Bell NCL20 */
  11903. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11904. /* Acer Aspire 4736Z */
  11905. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11906. /* Acer Aspire 5336 */
  11907. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11908. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11909. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11910. /* Acer C720 Chromebook (Core i3 4005U) */
  11911. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11912. /* Apple Macbook 2,1 (Core 2 T7400) */
  11913. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11914. /* Apple Macbook 4,1 */
  11915. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11916. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11917. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11918. /* HP Chromebook 14 (Celeron 2955U) */
  11919. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11920. /* Dell Chromebook 11 */
  11921. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11922. /* Dell Chromebook 11 (2015 version) */
  11923. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11924. /* Toshiba Satellite P50-C-18C */
  11925. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11926. };
  11927. static void intel_init_quirks(struct drm_device *dev)
  11928. {
  11929. struct pci_dev *d = dev->pdev;
  11930. int i;
  11931. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11932. struct intel_quirk *q = &intel_quirks[i];
  11933. if (d->device == q->device &&
  11934. (d->subsystem_vendor == q->subsystem_vendor ||
  11935. q->subsystem_vendor == PCI_ANY_ID) &&
  11936. (d->subsystem_device == q->subsystem_device ||
  11937. q->subsystem_device == PCI_ANY_ID))
  11938. q->hook(dev);
  11939. }
  11940. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11941. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11942. intel_dmi_quirks[i].hook(dev);
  11943. }
  11944. }
  11945. /* Disable the VGA plane that we never use */
  11946. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  11947. {
  11948. struct pci_dev *pdev = dev_priv->drm.pdev;
  11949. u8 sr1;
  11950. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  11951. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11952. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  11953. outb(SR01, VGA_SR_INDEX);
  11954. sr1 = inb(VGA_SR_DATA);
  11955. outb(sr1 | 1<<5, VGA_SR_DATA);
  11956. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  11957. udelay(300);
  11958. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11959. POSTING_READ(vga_reg);
  11960. }
  11961. void intel_modeset_init_hw(struct drm_device *dev)
  11962. {
  11963. struct drm_i915_private *dev_priv = to_i915(dev);
  11964. intel_update_cdclk(dev_priv);
  11965. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  11966. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  11967. }
  11968. /*
  11969. * Calculate what we think the watermarks should be for the state we've read
  11970. * out of the hardware and then immediately program those watermarks so that
  11971. * we ensure the hardware settings match our internal state.
  11972. *
  11973. * We can calculate what we think WM's should be by creating a duplicate of the
  11974. * current state (which was constructed during hardware readout) and running it
  11975. * through the atomic check code to calculate new watermark values in the
  11976. * state object.
  11977. */
  11978. static void sanitize_watermarks(struct drm_device *dev)
  11979. {
  11980. struct drm_i915_private *dev_priv = to_i915(dev);
  11981. struct drm_atomic_state *state;
  11982. struct intel_atomic_state *intel_state;
  11983. struct drm_crtc *crtc;
  11984. struct drm_crtc_state *cstate;
  11985. struct drm_modeset_acquire_ctx ctx;
  11986. int ret;
  11987. int i;
  11988. /* Only supported on platforms that use atomic watermark design */
  11989. if (!dev_priv->display.optimize_watermarks)
  11990. return;
  11991. /*
  11992. * We need to hold connection_mutex before calling duplicate_state so
  11993. * that the connector loop is protected.
  11994. */
  11995. drm_modeset_acquire_init(&ctx, 0);
  11996. retry:
  11997. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  11998. if (ret == -EDEADLK) {
  11999. drm_modeset_backoff(&ctx);
  12000. goto retry;
  12001. } else if (WARN_ON(ret)) {
  12002. goto fail;
  12003. }
  12004. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12005. if (WARN_ON(IS_ERR(state)))
  12006. goto fail;
  12007. intel_state = to_intel_atomic_state(state);
  12008. /*
  12009. * Hardware readout is the only time we don't want to calculate
  12010. * intermediate watermarks (since we don't trust the current
  12011. * watermarks).
  12012. */
  12013. if (!HAS_GMCH_DISPLAY(dev_priv))
  12014. intel_state->skip_intermediate_wm = true;
  12015. ret = intel_atomic_check(dev, state);
  12016. if (ret) {
  12017. /*
  12018. * If we fail here, it means that the hardware appears to be
  12019. * programmed in a way that shouldn't be possible, given our
  12020. * understanding of watermark requirements. This might mean a
  12021. * mistake in the hardware readout code or a mistake in the
  12022. * watermark calculations for a given platform. Raise a WARN
  12023. * so that this is noticeable.
  12024. *
  12025. * If this actually happens, we'll have to just leave the
  12026. * BIOS-programmed watermarks untouched and hope for the best.
  12027. */
  12028. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12029. goto put_state;
  12030. }
  12031. /* Write calculated watermark values back */
  12032. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12033. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12034. cs->wm.need_postvbl_update = true;
  12035. dev_priv->display.optimize_watermarks(intel_state, cs);
  12036. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12037. }
  12038. put_state:
  12039. drm_atomic_state_put(state);
  12040. fail:
  12041. drm_modeset_drop_locks(&ctx);
  12042. drm_modeset_acquire_fini(&ctx);
  12043. }
  12044. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12045. {
  12046. if (IS_GEN5(dev_priv)) {
  12047. u32 fdi_pll_clk =
  12048. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12049. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12050. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12051. dev_priv->fdi_pll_freq = 270000;
  12052. } else {
  12053. return;
  12054. }
  12055. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12056. }
  12057. int intel_modeset_init(struct drm_device *dev)
  12058. {
  12059. struct drm_i915_private *dev_priv = to_i915(dev);
  12060. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12061. enum pipe pipe;
  12062. struct intel_crtc *crtc;
  12063. drm_mode_config_init(dev);
  12064. dev->mode_config.min_width = 0;
  12065. dev->mode_config.min_height = 0;
  12066. dev->mode_config.preferred_depth = 24;
  12067. dev->mode_config.prefer_shadow = 1;
  12068. dev->mode_config.allow_fb_modifiers = true;
  12069. dev->mode_config.funcs = &intel_mode_funcs;
  12070. init_llist_head(&dev_priv->atomic_helper.free_list);
  12071. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12072. intel_atomic_helper_free_state_worker);
  12073. intel_init_quirks(dev);
  12074. intel_init_pm(dev_priv);
  12075. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12076. return 0;
  12077. /*
  12078. * There may be no VBT; and if the BIOS enabled SSC we can
  12079. * just keep using it to avoid unnecessary flicker. Whereas if the
  12080. * BIOS isn't using it, don't assume it will work even if the VBT
  12081. * indicates as much.
  12082. */
  12083. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12084. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12085. DREF_SSC1_ENABLE);
  12086. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12087. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12088. bios_lvds_use_ssc ? "en" : "dis",
  12089. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12090. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12091. }
  12092. }
  12093. if (IS_GEN2(dev_priv)) {
  12094. dev->mode_config.max_width = 2048;
  12095. dev->mode_config.max_height = 2048;
  12096. } else if (IS_GEN3(dev_priv)) {
  12097. dev->mode_config.max_width = 4096;
  12098. dev->mode_config.max_height = 4096;
  12099. } else {
  12100. dev->mode_config.max_width = 8192;
  12101. dev->mode_config.max_height = 8192;
  12102. }
  12103. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12104. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12105. dev->mode_config.cursor_height = 1023;
  12106. } else if (IS_GEN2(dev_priv)) {
  12107. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12108. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12109. } else {
  12110. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12111. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12112. }
  12113. dev->mode_config.fb_base = ggtt->gmadr.start;
  12114. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12115. INTEL_INFO(dev_priv)->num_pipes,
  12116. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12117. for_each_pipe(dev_priv, pipe) {
  12118. int ret;
  12119. ret = intel_crtc_init(dev_priv, pipe);
  12120. if (ret) {
  12121. drm_mode_config_cleanup(dev);
  12122. return ret;
  12123. }
  12124. }
  12125. intel_shared_dpll_init(dev);
  12126. intel_update_fdi_pll_freq(dev_priv);
  12127. intel_update_czclk(dev_priv);
  12128. intel_modeset_init_hw(dev);
  12129. if (dev_priv->max_cdclk_freq == 0)
  12130. intel_update_max_cdclk(dev_priv);
  12131. /* Just disable it once at startup */
  12132. i915_disable_vga(dev_priv);
  12133. intel_setup_outputs(dev_priv);
  12134. drm_modeset_lock_all(dev);
  12135. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12136. drm_modeset_unlock_all(dev);
  12137. for_each_intel_crtc(dev, crtc) {
  12138. struct intel_initial_plane_config plane_config = {};
  12139. if (!crtc->active)
  12140. continue;
  12141. /*
  12142. * Note that reserving the BIOS fb up front prevents us
  12143. * from stuffing other stolen allocations like the ring
  12144. * on top. This prevents some ugliness at boot time, and
  12145. * can even allow for smooth boot transitions if the BIOS
  12146. * fb is large enough for the active pipe configuration.
  12147. */
  12148. dev_priv->display.get_initial_plane_config(crtc,
  12149. &plane_config);
  12150. /*
  12151. * If the fb is shared between multiple heads, we'll
  12152. * just get the first one.
  12153. */
  12154. intel_find_initial_plane_obj(crtc, &plane_config);
  12155. }
  12156. /*
  12157. * Make sure hardware watermarks really match the state we read out.
  12158. * Note that we need to do this after reconstructing the BIOS fb's
  12159. * since the watermark calculation done here will use pstate->fb.
  12160. */
  12161. if (!HAS_GMCH_DISPLAY(dev_priv))
  12162. sanitize_watermarks(dev);
  12163. return 0;
  12164. }
  12165. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12166. {
  12167. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12168. /* 640x480@60Hz, ~25175 kHz */
  12169. struct dpll clock = {
  12170. .m1 = 18,
  12171. .m2 = 7,
  12172. .p1 = 13,
  12173. .p2 = 4,
  12174. .n = 2,
  12175. };
  12176. u32 dpll, fp;
  12177. int i;
  12178. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12179. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12180. pipe_name(pipe), clock.vco, clock.dot);
  12181. fp = i9xx_dpll_compute_fp(&clock);
  12182. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12183. DPLL_VGA_MODE_DIS |
  12184. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12185. PLL_P2_DIVIDE_BY_4 |
  12186. PLL_REF_INPUT_DREFCLK |
  12187. DPLL_VCO_ENABLE;
  12188. I915_WRITE(FP0(pipe), fp);
  12189. I915_WRITE(FP1(pipe), fp);
  12190. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12191. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12192. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12193. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12194. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12195. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12196. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12197. /*
  12198. * Apparently we need to have VGA mode enabled prior to changing
  12199. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12200. * dividers, even though the register value does change.
  12201. */
  12202. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12203. I915_WRITE(DPLL(pipe), dpll);
  12204. /* Wait for the clocks to stabilize. */
  12205. POSTING_READ(DPLL(pipe));
  12206. udelay(150);
  12207. /* The pixel multiplier can only be updated once the
  12208. * DPLL is enabled and the clocks are stable.
  12209. *
  12210. * So write it again.
  12211. */
  12212. I915_WRITE(DPLL(pipe), dpll);
  12213. /* We do this three times for luck */
  12214. for (i = 0; i < 3 ; i++) {
  12215. I915_WRITE(DPLL(pipe), dpll);
  12216. POSTING_READ(DPLL(pipe));
  12217. udelay(150); /* wait for warmup */
  12218. }
  12219. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12220. POSTING_READ(PIPECONF(pipe));
  12221. intel_wait_for_pipe_scanline_moving(crtc);
  12222. }
  12223. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12224. {
  12225. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12226. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12227. pipe_name(pipe));
  12228. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12229. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12230. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12231. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
  12232. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
  12233. I915_WRITE(PIPECONF(pipe), 0);
  12234. POSTING_READ(PIPECONF(pipe));
  12235. intel_wait_for_pipe_scanline_stopped(crtc);
  12236. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12237. POSTING_READ(DPLL(pipe));
  12238. }
  12239. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12240. struct intel_plane *plane)
  12241. {
  12242. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12243. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  12244. u32 val = I915_READ(DSPCNTR(i9xx_plane));
  12245. return (val & DISPLAY_PLANE_ENABLE) == 0 ||
  12246. (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
  12247. }
  12248. static void
  12249. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12250. {
  12251. struct intel_crtc *crtc;
  12252. if (INTEL_GEN(dev_priv) >= 4)
  12253. return;
  12254. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12255. struct intel_plane *plane =
  12256. to_intel_plane(crtc->base.primary);
  12257. if (intel_plane_mapping_ok(crtc, plane))
  12258. continue;
  12259. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12260. plane->base.name);
  12261. intel_plane_disable_noatomic(crtc, plane);
  12262. }
  12263. }
  12264. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12265. {
  12266. struct drm_device *dev = crtc->base.dev;
  12267. struct intel_encoder *encoder;
  12268. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12269. return true;
  12270. return false;
  12271. }
  12272. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12273. {
  12274. struct drm_device *dev = encoder->base.dev;
  12275. struct intel_connector *connector;
  12276. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12277. return connector;
  12278. return NULL;
  12279. }
  12280. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12281. enum pipe pch_transcoder)
  12282. {
  12283. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12284. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12285. }
  12286. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12287. struct drm_modeset_acquire_ctx *ctx)
  12288. {
  12289. struct drm_device *dev = crtc->base.dev;
  12290. struct drm_i915_private *dev_priv = to_i915(dev);
  12291. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12292. /* Clear any frame start delays used for debugging left by the BIOS */
  12293. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12294. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12295. I915_WRITE(reg,
  12296. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12297. }
  12298. /* restore vblank interrupts to correct state */
  12299. drm_crtc_vblank_reset(&crtc->base);
  12300. if (crtc->active) {
  12301. struct intel_plane *plane;
  12302. drm_crtc_vblank_on(&crtc->base);
  12303. /* Disable everything but the primary plane */
  12304. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12305. const struct intel_plane_state *plane_state =
  12306. to_intel_plane_state(plane->base.state);
  12307. if (plane_state->base.visible &&
  12308. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12309. intel_plane_disable_noatomic(crtc, plane);
  12310. }
  12311. }
  12312. /* Adjust the state of the output pipe according to whether we
  12313. * have active connectors/encoders. */
  12314. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12315. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12316. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12317. /*
  12318. * We start out with underrun reporting disabled to avoid races.
  12319. * For correct bookkeeping mark this on active crtcs.
  12320. *
  12321. * Also on gmch platforms we dont have any hardware bits to
  12322. * disable the underrun reporting. Which means we need to start
  12323. * out with underrun reporting disabled also on inactive pipes,
  12324. * since otherwise we'll complain about the garbage we read when
  12325. * e.g. coming up after runtime pm.
  12326. *
  12327. * No protection against concurrent access is required - at
  12328. * worst a fifo underrun happens which also sets this to false.
  12329. */
  12330. crtc->cpu_fifo_underrun_disabled = true;
  12331. /*
  12332. * We track the PCH trancoder underrun reporting state
  12333. * within the crtc. With crtc for pipe A housing the underrun
  12334. * reporting state for PCH transcoder A, crtc for pipe B housing
  12335. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12336. * and marking underrun reporting as disabled for the non-existing
  12337. * PCH transcoders B and C would prevent enabling the south
  12338. * error interrupt (see cpt_can_enable_serr_int()).
  12339. */
  12340. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12341. crtc->pch_fifo_underrun_disabled = true;
  12342. }
  12343. }
  12344. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12345. {
  12346. struct intel_connector *connector;
  12347. /* We need to check both for a crtc link (meaning that the
  12348. * encoder is active and trying to read from a pipe) and the
  12349. * pipe itself being active. */
  12350. bool has_active_crtc = encoder->base.crtc &&
  12351. to_intel_crtc(encoder->base.crtc)->active;
  12352. connector = intel_encoder_find_connector(encoder);
  12353. if (connector && !has_active_crtc) {
  12354. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12355. encoder->base.base.id,
  12356. encoder->base.name);
  12357. /* Connector is active, but has no active pipe. This is
  12358. * fallout from our resume register restoring. Disable
  12359. * the encoder manually again. */
  12360. if (encoder->base.crtc) {
  12361. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12362. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12363. encoder->base.base.id,
  12364. encoder->base.name);
  12365. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12366. if (encoder->post_disable)
  12367. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12368. }
  12369. encoder->base.crtc = NULL;
  12370. /* Inconsistent output/port/pipe state happens presumably due to
  12371. * a bug in one of the get_hw_state functions. Or someplace else
  12372. * in our code, like the register restore mess on resume. Clamp
  12373. * things to off as a safer default. */
  12374. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12375. connector->base.encoder = NULL;
  12376. }
  12377. }
  12378. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12379. {
  12380. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12381. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12382. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12383. i915_disable_vga(dev_priv);
  12384. }
  12385. }
  12386. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12387. {
  12388. /* This function can be called both from intel_modeset_setup_hw_state or
  12389. * at a very early point in our resume sequence, where the power well
  12390. * structures are not yet restored. Since this function is at a very
  12391. * paranoid "someone might have enabled VGA while we were not looking"
  12392. * level, just check if the power well is enabled instead of trying to
  12393. * follow the "don't touch the power well if we don't need it" policy
  12394. * the rest of the driver uses. */
  12395. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12396. return;
  12397. i915_redisable_vga_power_on(dev_priv);
  12398. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12399. }
  12400. /* FIXME read out full plane state for all planes */
  12401. static void readout_plane_state(struct intel_crtc *crtc)
  12402. {
  12403. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12404. struct intel_crtc_state *crtc_state =
  12405. to_intel_crtc_state(crtc->base.state);
  12406. struct intel_plane *plane;
  12407. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12408. struct intel_plane_state *plane_state =
  12409. to_intel_plane_state(plane->base.state);
  12410. bool visible = plane->get_hw_state(plane);
  12411. intel_set_plane_visible(crtc_state, plane_state, visible);
  12412. }
  12413. }
  12414. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12415. {
  12416. struct drm_i915_private *dev_priv = to_i915(dev);
  12417. enum pipe pipe;
  12418. struct intel_crtc *crtc;
  12419. struct intel_encoder *encoder;
  12420. struct intel_connector *connector;
  12421. struct drm_connector_list_iter conn_iter;
  12422. int i;
  12423. dev_priv->active_crtcs = 0;
  12424. for_each_intel_crtc(dev, crtc) {
  12425. struct intel_crtc_state *crtc_state =
  12426. to_intel_crtc_state(crtc->base.state);
  12427. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12428. memset(crtc_state, 0, sizeof(*crtc_state));
  12429. crtc_state->base.crtc = &crtc->base;
  12430. crtc_state->base.active = crtc_state->base.enable =
  12431. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12432. crtc->base.enabled = crtc_state->base.enable;
  12433. crtc->active = crtc_state->base.active;
  12434. if (crtc_state->base.active)
  12435. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12436. readout_plane_state(crtc);
  12437. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12438. crtc->base.base.id, crtc->base.name,
  12439. enableddisabled(crtc_state->base.active));
  12440. }
  12441. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12442. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12443. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12444. &pll->state.hw_state);
  12445. pll->state.crtc_mask = 0;
  12446. for_each_intel_crtc(dev, crtc) {
  12447. struct intel_crtc_state *crtc_state =
  12448. to_intel_crtc_state(crtc->base.state);
  12449. if (crtc_state->base.active &&
  12450. crtc_state->shared_dpll == pll)
  12451. pll->state.crtc_mask |= 1 << crtc->pipe;
  12452. }
  12453. pll->active_mask = pll->state.crtc_mask;
  12454. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12455. pll->name, pll->state.crtc_mask, pll->on);
  12456. }
  12457. for_each_intel_encoder(dev, encoder) {
  12458. pipe = 0;
  12459. if (encoder->get_hw_state(encoder, &pipe)) {
  12460. struct intel_crtc_state *crtc_state;
  12461. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12462. crtc_state = to_intel_crtc_state(crtc->base.state);
  12463. encoder->base.crtc = &crtc->base;
  12464. encoder->get_config(encoder, crtc_state);
  12465. } else {
  12466. encoder->base.crtc = NULL;
  12467. }
  12468. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12469. encoder->base.base.id, encoder->base.name,
  12470. enableddisabled(encoder->base.crtc),
  12471. pipe_name(pipe));
  12472. }
  12473. drm_connector_list_iter_begin(dev, &conn_iter);
  12474. for_each_intel_connector_iter(connector, &conn_iter) {
  12475. if (connector->get_hw_state(connector)) {
  12476. connector->base.dpms = DRM_MODE_DPMS_ON;
  12477. encoder = connector->encoder;
  12478. connector->base.encoder = &encoder->base;
  12479. if (encoder->base.crtc &&
  12480. encoder->base.crtc->state->active) {
  12481. /*
  12482. * This has to be done during hardware readout
  12483. * because anything calling .crtc_disable may
  12484. * rely on the connector_mask being accurate.
  12485. */
  12486. encoder->base.crtc->state->connector_mask |=
  12487. 1 << drm_connector_index(&connector->base);
  12488. encoder->base.crtc->state->encoder_mask |=
  12489. 1 << drm_encoder_index(&encoder->base);
  12490. }
  12491. } else {
  12492. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12493. connector->base.encoder = NULL;
  12494. }
  12495. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12496. connector->base.base.id, connector->base.name,
  12497. enableddisabled(connector->base.encoder));
  12498. }
  12499. drm_connector_list_iter_end(&conn_iter);
  12500. for_each_intel_crtc(dev, crtc) {
  12501. struct intel_crtc_state *crtc_state =
  12502. to_intel_crtc_state(crtc->base.state);
  12503. int min_cdclk = 0;
  12504. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12505. if (crtc_state->base.active) {
  12506. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12507. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12508. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12509. /*
  12510. * The initial mode needs to be set in order to keep
  12511. * the atomic core happy. It wants a valid mode if the
  12512. * crtc's enabled, so we do the above call.
  12513. *
  12514. * But we don't set all the derived state fully, hence
  12515. * set a flag to indicate that a full recalculation is
  12516. * needed on the next commit.
  12517. */
  12518. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12519. intel_crtc_compute_pixel_rate(crtc_state);
  12520. if (dev_priv->display.modeset_calc_cdclk) {
  12521. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12522. if (WARN_ON(min_cdclk < 0))
  12523. min_cdclk = 0;
  12524. }
  12525. drm_calc_timestamping_constants(&crtc->base,
  12526. &crtc_state->base.adjusted_mode);
  12527. update_scanline_offset(crtc);
  12528. }
  12529. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12530. dev_priv->min_voltage_level[crtc->pipe] =
  12531. crtc_state->min_voltage_level;
  12532. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12533. }
  12534. }
  12535. static void
  12536. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12537. {
  12538. struct intel_encoder *encoder;
  12539. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12540. u64 get_domains;
  12541. enum intel_display_power_domain domain;
  12542. if (!encoder->get_power_domains)
  12543. continue;
  12544. get_domains = encoder->get_power_domains(encoder);
  12545. for_each_power_domain(domain, get_domains)
  12546. intel_display_power_get(dev_priv, domain);
  12547. }
  12548. }
  12549. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12550. {
  12551. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12552. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12553. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12554. DARBF_GATING_DIS);
  12555. if (IS_HASWELL(dev_priv)) {
  12556. /*
  12557. * WaRsPkgCStateDisplayPMReq:hsw
  12558. * System hang if this isn't done before disabling all planes!
  12559. */
  12560. I915_WRITE(CHICKEN_PAR1_1,
  12561. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12562. }
  12563. }
  12564. /* Scan out the current hw modeset state,
  12565. * and sanitizes it to the current state
  12566. */
  12567. static void
  12568. intel_modeset_setup_hw_state(struct drm_device *dev,
  12569. struct drm_modeset_acquire_ctx *ctx)
  12570. {
  12571. struct drm_i915_private *dev_priv = to_i915(dev);
  12572. enum pipe pipe;
  12573. struct intel_crtc *crtc;
  12574. struct intel_encoder *encoder;
  12575. int i;
  12576. intel_early_display_was(dev_priv);
  12577. intel_modeset_readout_hw_state(dev);
  12578. /* HW state is read out, now we need to sanitize this mess. */
  12579. get_encoder_power_domains(dev_priv);
  12580. intel_sanitize_plane_mapping(dev_priv);
  12581. for_each_intel_encoder(dev, encoder) {
  12582. intel_sanitize_encoder(encoder);
  12583. }
  12584. for_each_pipe(dev_priv, pipe) {
  12585. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12586. intel_sanitize_crtc(crtc, ctx);
  12587. intel_dump_pipe_config(crtc, crtc->config,
  12588. "[setup_hw_state]");
  12589. }
  12590. intel_modeset_update_connector_atomic_state(dev);
  12591. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12592. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12593. if (!pll->on || pll->active_mask)
  12594. continue;
  12595. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12596. pll->funcs.disable(dev_priv, pll);
  12597. pll->on = false;
  12598. }
  12599. if (IS_G4X(dev_priv)) {
  12600. g4x_wm_get_hw_state(dev);
  12601. g4x_wm_sanitize(dev_priv);
  12602. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12603. vlv_wm_get_hw_state(dev);
  12604. vlv_wm_sanitize(dev_priv);
  12605. } else if (INTEL_GEN(dev_priv) >= 9) {
  12606. skl_wm_get_hw_state(dev);
  12607. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12608. ilk_wm_get_hw_state(dev);
  12609. }
  12610. for_each_intel_crtc(dev, crtc) {
  12611. u64 put_domains;
  12612. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12613. if (WARN_ON(put_domains))
  12614. modeset_put_power_domains(dev_priv, put_domains);
  12615. }
  12616. intel_display_set_init_power(dev_priv, false);
  12617. intel_power_domains_verify_state(dev_priv);
  12618. intel_fbc_init_pipe_state(dev_priv);
  12619. }
  12620. void intel_display_resume(struct drm_device *dev)
  12621. {
  12622. struct drm_i915_private *dev_priv = to_i915(dev);
  12623. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12624. struct drm_modeset_acquire_ctx ctx;
  12625. int ret;
  12626. dev_priv->modeset_restore_state = NULL;
  12627. if (state)
  12628. state->acquire_ctx = &ctx;
  12629. drm_modeset_acquire_init(&ctx, 0);
  12630. while (1) {
  12631. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12632. if (ret != -EDEADLK)
  12633. break;
  12634. drm_modeset_backoff(&ctx);
  12635. }
  12636. if (!ret)
  12637. ret = __intel_display_resume(dev, state, &ctx);
  12638. intel_enable_ipc(dev_priv);
  12639. drm_modeset_drop_locks(&ctx);
  12640. drm_modeset_acquire_fini(&ctx);
  12641. if (ret)
  12642. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12643. if (state)
  12644. drm_atomic_state_put(state);
  12645. }
  12646. int intel_connector_register(struct drm_connector *connector)
  12647. {
  12648. struct intel_connector *intel_connector = to_intel_connector(connector);
  12649. int ret;
  12650. ret = intel_backlight_device_register(intel_connector);
  12651. if (ret)
  12652. goto err;
  12653. return 0;
  12654. err:
  12655. return ret;
  12656. }
  12657. void intel_connector_unregister(struct drm_connector *connector)
  12658. {
  12659. struct intel_connector *intel_connector = to_intel_connector(connector);
  12660. intel_backlight_device_unregister(intel_connector);
  12661. intel_panel_destroy_backlight(connector);
  12662. }
  12663. static void intel_hpd_poll_fini(struct drm_device *dev)
  12664. {
  12665. struct intel_connector *connector;
  12666. struct drm_connector_list_iter conn_iter;
  12667. /* Kill all the work that may have been queued by hpd. */
  12668. drm_connector_list_iter_begin(dev, &conn_iter);
  12669. for_each_intel_connector_iter(connector, &conn_iter) {
  12670. if (connector->modeset_retry_work.func)
  12671. cancel_work_sync(&connector->modeset_retry_work);
  12672. }
  12673. drm_connector_list_iter_end(&conn_iter);
  12674. }
  12675. void intel_modeset_cleanup(struct drm_device *dev)
  12676. {
  12677. struct drm_i915_private *dev_priv = to_i915(dev);
  12678. flush_work(&dev_priv->atomic_helper.free_work);
  12679. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12680. intel_disable_gt_powersave(dev_priv);
  12681. /*
  12682. * Interrupts and polling as the first thing to avoid creating havoc.
  12683. * Too much stuff here (turning of connectors, ...) would
  12684. * experience fancy races otherwise.
  12685. */
  12686. intel_irq_uninstall(dev_priv);
  12687. /*
  12688. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12689. * poll handlers. Hence disable polling after hpd handling is shut down.
  12690. */
  12691. intel_hpd_poll_fini(dev);
  12692. /* poll work can call into fbdev, hence clean that up afterwards */
  12693. intel_fbdev_fini(dev_priv);
  12694. intel_unregister_dsm_handler();
  12695. intel_fbc_global_disable(dev_priv);
  12696. /* flush any delayed tasks or pending work */
  12697. flush_scheduled_work();
  12698. drm_mode_config_cleanup(dev);
  12699. intel_cleanup_overlay(dev_priv);
  12700. intel_cleanup_gt_powersave(dev_priv);
  12701. intel_teardown_gmbus(dev_priv);
  12702. }
  12703. void intel_connector_attach_encoder(struct intel_connector *connector,
  12704. struct intel_encoder *encoder)
  12705. {
  12706. connector->encoder = encoder;
  12707. drm_mode_connector_attach_encoder(&connector->base,
  12708. &encoder->base);
  12709. }
  12710. /*
  12711. * set vga decode state - true == enable VGA decode
  12712. */
  12713. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12714. {
  12715. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12716. u16 gmch_ctrl;
  12717. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12718. DRM_ERROR("failed to read control word\n");
  12719. return -EIO;
  12720. }
  12721. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12722. return 0;
  12723. if (state)
  12724. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12725. else
  12726. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12727. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12728. DRM_ERROR("failed to write control word\n");
  12729. return -EIO;
  12730. }
  12731. return 0;
  12732. }
  12733. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12734. struct intel_display_error_state {
  12735. u32 power_well_driver;
  12736. int num_transcoders;
  12737. struct intel_cursor_error_state {
  12738. u32 control;
  12739. u32 position;
  12740. u32 base;
  12741. u32 size;
  12742. } cursor[I915_MAX_PIPES];
  12743. struct intel_pipe_error_state {
  12744. bool power_domain_on;
  12745. u32 source;
  12746. u32 stat;
  12747. } pipe[I915_MAX_PIPES];
  12748. struct intel_plane_error_state {
  12749. u32 control;
  12750. u32 stride;
  12751. u32 size;
  12752. u32 pos;
  12753. u32 addr;
  12754. u32 surface;
  12755. u32 tile_offset;
  12756. } plane[I915_MAX_PIPES];
  12757. struct intel_transcoder_error_state {
  12758. bool power_domain_on;
  12759. enum transcoder cpu_transcoder;
  12760. u32 conf;
  12761. u32 htotal;
  12762. u32 hblank;
  12763. u32 hsync;
  12764. u32 vtotal;
  12765. u32 vblank;
  12766. u32 vsync;
  12767. } transcoder[4];
  12768. };
  12769. struct intel_display_error_state *
  12770. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12771. {
  12772. struct intel_display_error_state *error;
  12773. int transcoders[] = {
  12774. TRANSCODER_A,
  12775. TRANSCODER_B,
  12776. TRANSCODER_C,
  12777. TRANSCODER_EDP,
  12778. };
  12779. int i;
  12780. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12781. return NULL;
  12782. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12783. if (error == NULL)
  12784. return NULL;
  12785. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12786. error->power_well_driver =
  12787. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12788. for_each_pipe(dev_priv, i) {
  12789. error->pipe[i].power_domain_on =
  12790. __intel_display_power_is_enabled(dev_priv,
  12791. POWER_DOMAIN_PIPE(i));
  12792. if (!error->pipe[i].power_domain_on)
  12793. continue;
  12794. error->cursor[i].control = I915_READ(CURCNTR(i));
  12795. error->cursor[i].position = I915_READ(CURPOS(i));
  12796. error->cursor[i].base = I915_READ(CURBASE(i));
  12797. error->plane[i].control = I915_READ(DSPCNTR(i));
  12798. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12799. if (INTEL_GEN(dev_priv) <= 3) {
  12800. error->plane[i].size = I915_READ(DSPSIZE(i));
  12801. error->plane[i].pos = I915_READ(DSPPOS(i));
  12802. }
  12803. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12804. error->plane[i].addr = I915_READ(DSPADDR(i));
  12805. if (INTEL_GEN(dev_priv) >= 4) {
  12806. error->plane[i].surface = I915_READ(DSPSURF(i));
  12807. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12808. }
  12809. error->pipe[i].source = I915_READ(PIPESRC(i));
  12810. if (HAS_GMCH_DISPLAY(dev_priv))
  12811. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12812. }
  12813. /* Note: this does not include DSI transcoders. */
  12814. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12815. if (HAS_DDI(dev_priv))
  12816. error->num_transcoders++; /* Account for eDP. */
  12817. for (i = 0; i < error->num_transcoders; i++) {
  12818. enum transcoder cpu_transcoder = transcoders[i];
  12819. error->transcoder[i].power_domain_on =
  12820. __intel_display_power_is_enabled(dev_priv,
  12821. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12822. if (!error->transcoder[i].power_domain_on)
  12823. continue;
  12824. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12825. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12826. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12827. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12828. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12829. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12830. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12831. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12832. }
  12833. return error;
  12834. }
  12835. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12836. void
  12837. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12838. struct intel_display_error_state *error)
  12839. {
  12840. struct drm_i915_private *dev_priv = m->i915;
  12841. int i;
  12842. if (!error)
  12843. return;
  12844. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12845. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12846. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12847. error->power_well_driver);
  12848. for_each_pipe(dev_priv, i) {
  12849. err_printf(m, "Pipe [%d]:\n", i);
  12850. err_printf(m, " Power: %s\n",
  12851. onoff(error->pipe[i].power_domain_on));
  12852. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12853. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12854. err_printf(m, "Plane [%d]:\n", i);
  12855. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12856. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12857. if (INTEL_GEN(dev_priv) <= 3) {
  12858. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12859. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12860. }
  12861. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12862. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12863. if (INTEL_GEN(dev_priv) >= 4) {
  12864. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12865. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12866. }
  12867. err_printf(m, "Cursor [%d]:\n", i);
  12868. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12869. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12870. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12871. }
  12872. for (i = 0; i < error->num_transcoders; i++) {
  12873. err_printf(m, "CPU transcoder: %s\n",
  12874. transcoder_name(error->transcoder[i].cpu_transcoder));
  12875. err_printf(m, " Power: %s\n",
  12876. onoff(error->transcoder[i].power_domain_on));
  12877. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12878. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12879. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12880. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12881. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12882. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12883. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12884. }
  12885. }
  12886. #endif