intel_display.c 438 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540
  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_GEN(dev_priv) >= 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_GEN(dev_priv) >= 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. /*
  2047. * From the Sky Lake PRM:
  2048. * "The Color Control Surface (CCS) contains the compression status of
  2049. * the cache-line pairs. The compression state of the cache-line pair
  2050. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2051. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2052. * cache-line-pairs. CCS is always Y tiled."
  2053. *
  2054. * Since cache line pairs refers to horizontally adjacent cache lines,
  2055. * each cache line in the CCS corresponds to an area of 32x16 cache
  2056. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2057. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2058. * main surface.
  2059. */
  2060. static const struct drm_format_info ccs_formats[] = {
  2061. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2062. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2063. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2064. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2065. };
  2066. static const struct drm_format_info *
  2067. lookup_format_info(const struct drm_format_info formats[],
  2068. int num_formats, u32 format)
  2069. {
  2070. int i;
  2071. for (i = 0; i < num_formats; i++) {
  2072. if (formats[i].format == format)
  2073. return &formats[i];
  2074. }
  2075. return NULL;
  2076. }
  2077. static const struct drm_format_info *
  2078. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2079. {
  2080. switch (cmd->modifier[0]) {
  2081. case I915_FORMAT_MOD_Y_TILED_CCS:
  2082. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2083. return lookup_format_info(ccs_formats,
  2084. ARRAY_SIZE(ccs_formats),
  2085. cmd->pixel_format);
  2086. default:
  2087. return NULL;
  2088. }
  2089. }
  2090. static int
  2091. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2092. struct drm_framebuffer *fb)
  2093. {
  2094. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2095. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2096. u32 gtt_offset_rotated = 0;
  2097. unsigned int max_size = 0;
  2098. int i, num_planes = fb->format->num_planes;
  2099. unsigned int tile_size = intel_tile_size(dev_priv);
  2100. for (i = 0; i < num_planes; i++) {
  2101. unsigned int width, height;
  2102. unsigned int cpp, size;
  2103. u32 offset;
  2104. int x, y;
  2105. int ret;
  2106. cpp = fb->format->cpp[i];
  2107. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2108. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2109. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2110. if (ret) {
  2111. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2112. i, fb->offsets[i]);
  2113. return ret;
  2114. }
  2115. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2116. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2117. int hsub = fb->format->hsub;
  2118. int vsub = fb->format->vsub;
  2119. int tile_width, tile_height;
  2120. int main_x, main_y;
  2121. int ccs_x, ccs_y;
  2122. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2123. tile_width *= hsub;
  2124. tile_height *= vsub;
  2125. ccs_x = (x * hsub) % tile_width;
  2126. ccs_y = (y * vsub) % tile_height;
  2127. main_x = intel_fb->normal[0].x % tile_width;
  2128. main_y = intel_fb->normal[0].y % tile_height;
  2129. /*
  2130. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2131. * x/y offsets must match between CCS and the main surface.
  2132. */
  2133. if (main_x != ccs_x || main_y != ccs_y) {
  2134. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2135. main_x, main_y,
  2136. ccs_x, ccs_y,
  2137. intel_fb->normal[0].x,
  2138. intel_fb->normal[0].y,
  2139. x, y);
  2140. return -EINVAL;
  2141. }
  2142. }
  2143. /*
  2144. * The fence (if used) is aligned to the start of the object
  2145. * so having the framebuffer wrap around across the edge of the
  2146. * fenced region doesn't really work. We have no API to configure
  2147. * the fence start offset within the object (nor could we probably
  2148. * on gen2/3). So it's just easier if we just require that the
  2149. * fb layout agrees with the fence layout. We already check that the
  2150. * fb stride matches the fence stride elsewhere.
  2151. */
  2152. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2153. (x + width) * cpp > fb->pitches[i]) {
  2154. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2155. i, fb->offsets[i]);
  2156. return -EINVAL;
  2157. }
  2158. /*
  2159. * First pixel of the framebuffer from
  2160. * the start of the normal gtt mapping.
  2161. */
  2162. intel_fb->normal[i].x = x;
  2163. intel_fb->normal[i].y = y;
  2164. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2165. fb, i, fb->pitches[i],
  2166. DRM_MODE_ROTATE_0, tile_size);
  2167. offset /= tile_size;
  2168. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2169. unsigned int tile_width, tile_height;
  2170. unsigned int pitch_tiles;
  2171. struct drm_rect r;
  2172. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2173. rot_info->plane[i].offset = offset;
  2174. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2175. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2176. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2177. intel_fb->rotated[i].pitch =
  2178. rot_info->plane[i].height * tile_height;
  2179. /* how many tiles does this plane need */
  2180. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2181. /*
  2182. * If the plane isn't horizontally tile aligned,
  2183. * we need one more tile.
  2184. */
  2185. if (x != 0)
  2186. size++;
  2187. /* rotate the x/y offsets to match the GTT view */
  2188. r.x1 = x;
  2189. r.y1 = y;
  2190. r.x2 = x + width;
  2191. r.y2 = y + height;
  2192. drm_rect_rotate(&r,
  2193. rot_info->plane[i].width * tile_width,
  2194. rot_info->plane[i].height * tile_height,
  2195. DRM_MODE_ROTATE_270);
  2196. x = r.x1;
  2197. y = r.y1;
  2198. /* rotate the tile dimensions to match the GTT view */
  2199. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2200. swap(tile_width, tile_height);
  2201. /*
  2202. * We only keep the x/y offsets, so push all of the
  2203. * gtt offset into the x/y offsets.
  2204. */
  2205. __intel_adjust_tile_offset(&x, &y,
  2206. tile_width, tile_height,
  2207. tile_size, pitch_tiles,
  2208. gtt_offset_rotated * tile_size, 0);
  2209. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2210. /*
  2211. * First pixel of the framebuffer from
  2212. * the start of the rotated gtt mapping.
  2213. */
  2214. intel_fb->rotated[i].x = x;
  2215. intel_fb->rotated[i].y = y;
  2216. } else {
  2217. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2218. x * cpp, tile_size);
  2219. }
  2220. /* how many tiles in total needed in the bo */
  2221. max_size = max(max_size, offset + size);
  2222. }
  2223. if (max_size * tile_size > intel_fb->obj->base.size) {
  2224. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2225. max_size * tile_size, intel_fb->obj->base.size);
  2226. return -EINVAL;
  2227. }
  2228. return 0;
  2229. }
  2230. static int i9xx_format_to_fourcc(int format)
  2231. {
  2232. switch (format) {
  2233. case DISPPLANE_8BPP:
  2234. return DRM_FORMAT_C8;
  2235. case DISPPLANE_BGRX555:
  2236. return DRM_FORMAT_XRGB1555;
  2237. case DISPPLANE_BGRX565:
  2238. return DRM_FORMAT_RGB565;
  2239. default:
  2240. case DISPPLANE_BGRX888:
  2241. return DRM_FORMAT_XRGB8888;
  2242. case DISPPLANE_RGBX888:
  2243. return DRM_FORMAT_XBGR8888;
  2244. case DISPPLANE_BGRX101010:
  2245. return DRM_FORMAT_XRGB2101010;
  2246. case DISPPLANE_RGBX101010:
  2247. return DRM_FORMAT_XBGR2101010;
  2248. }
  2249. }
  2250. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2251. {
  2252. switch (format) {
  2253. case PLANE_CTL_FORMAT_RGB_565:
  2254. return DRM_FORMAT_RGB565;
  2255. default:
  2256. case PLANE_CTL_FORMAT_XRGB_8888:
  2257. if (rgb_order) {
  2258. if (alpha)
  2259. return DRM_FORMAT_ABGR8888;
  2260. else
  2261. return DRM_FORMAT_XBGR8888;
  2262. } else {
  2263. if (alpha)
  2264. return DRM_FORMAT_ARGB8888;
  2265. else
  2266. return DRM_FORMAT_XRGB8888;
  2267. }
  2268. case PLANE_CTL_FORMAT_XRGB_2101010:
  2269. if (rgb_order)
  2270. return DRM_FORMAT_XBGR2101010;
  2271. else
  2272. return DRM_FORMAT_XRGB2101010;
  2273. }
  2274. }
  2275. static bool
  2276. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2277. struct intel_initial_plane_config *plane_config)
  2278. {
  2279. struct drm_device *dev = crtc->base.dev;
  2280. struct drm_i915_private *dev_priv = to_i915(dev);
  2281. struct drm_i915_gem_object *obj = NULL;
  2282. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2283. struct drm_framebuffer *fb = &plane_config->fb->base;
  2284. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2285. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2286. PAGE_SIZE);
  2287. size_aligned -= base_aligned;
  2288. if (plane_config->size == 0)
  2289. return false;
  2290. /* If the FB is too big, just don't use it since fbdev is not very
  2291. * important and we should probably use that space with FBC or other
  2292. * features. */
  2293. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2294. return false;
  2295. mutex_lock(&dev->struct_mutex);
  2296. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2297. base_aligned,
  2298. base_aligned,
  2299. size_aligned);
  2300. mutex_unlock(&dev->struct_mutex);
  2301. if (!obj)
  2302. return false;
  2303. if (plane_config->tiling == I915_TILING_X)
  2304. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2305. mode_cmd.pixel_format = fb->format->format;
  2306. mode_cmd.width = fb->width;
  2307. mode_cmd.height = fb->height;
  2308. mode_cmd.pitches[0] = fb->pitches[0];
  2309. mode_cmd.modifier[0] = fb->modifier;
  2310. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2311. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2312. DRM_DEBUG_KMS("intel fb init failed\n");
  2313. goto out_unref_obj;
  2314. }
  2315. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2316. return true;
  2317. out_unref_obj:
  2318. i915_gem_object_put(obj);
  2319. return false;
  2320. }
  2321. static void
  2322. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2323. struct intel_plane_state *plane_state,
  2324. bool visible)
  2325. {
  2326. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2327. plane_state->base.visible = visible;
  2328. /* FIXME pre-g4x don't work like this */
  2329. if (visible) {
  2330. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2331. crtc_state->active_planes |= BIT(plane->id);
  2332. } else {
  2333. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2334. crtc_state->active_planes &= ~BIT(plane->id);
  2335. }
  2336. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2337. crtc_state->base.crtc->name,
  2338. crtc_state->active_planes);
  2339. }
  2340. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2341. struct intel_plane *plane)
  2342. {
  2343. struct intel_crtc_state *crtc_state =
  2344. to_intel_crtc_state(crtc->base.state);
  2345. struct intel_plane_state *plane_state =
  2346. to_intel_plane_state(plane->base.state);
  2347. intel_set_plane_visible(crtc_state, plane_state, false);
  2348. if (plane->id == PLANE_PRIMARY)
  2349. intel_pre_disable_primary_noatomic(&crtc->base);
  2350. trace_intel_disable_plane(&plane->base, crtc);
  2351. plane->disable_plane(plane, crtc);
  2352. }
  2353. static void
  2354. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2355. struct intel_initial_plane_config *plane_config)
  2356. {
  2357. struct drm_device *dev = intel_crtc->base.dev;
  2358. struct drm_i915_private *dev_priv = to_i915(dev);
  2359. struct drm_crtc *c;
  2360. struct drm_i915_gem_object *obj;
  2361. struct drm_plane *primary = intel_crtc->base.primary;
  2362. struct drm_plane_state *plane_state = primary->state;
  2363. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2364. struct intel_plane *intel_plane = to_intel_plane(primary);
  2365. struct intel_plane_state *intel_state =
  2366. to_intel_plane_state(plane_state);
  2367. struct drm_framebuffer *fb;
  2368. if (!plane_config->fb)
  2369. return;
  2370. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2371. fb = &plane_config->fb->base;
  2372. goto valid_fb;
  2373. }
  2374. kfree(plane_config->fb);
  2375. /*
  2376. * Failed to alloc the obj, check to see if we should share
  2377. * an fb with another CRTC instead
  2378. */
  2379. for_each_crtc(dev, c) {
  2380. struct intel_plane_state *state;
  2381. if (c == &intel_crtc->base)
  2382. continue;
  2383. if (!to_intel_crtc(c)->active)
  2384. continue;
  2385. state = to_intel_plane_state(c->primary->state);
  2386. if (!state->vma)
  2387. continue;
  2388. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2389. fb = c->primary->fb;
  2390. drm_framebuffer_get(fb);
  2391. goto valid_fb;
  2392. }
  2393. }
  2394. /*
  2395. * We've failed to reconstruct the BIOS FB. Current display state
  2396. * indicates that the primary plane is visible, but has a NULL FB,
  2397. * which will lead to problems later if we don't fix it up. The
  2398. * simplest solution is to just disable the primary plane now and
  2399. * pretend the BIOS never had it enabled.
  2400. */
  2401. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2402. return;
  2403. valid_fb:
  2404. mutex_lock(&dev->struct_mutex);
  2405. intel_state->vma =
  2406. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2407. mutex_unlock(&dev->struct_mutex);
  2408. if (IS_ERR(intel_state->vma)) {
  2409. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2410. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2411. intel_state->vma = NULL;
  2412. drm_framebuffer_put(fb);
  2413. return;
  2414. }
  2415. plane_state->src_x = 0;
  2416. plane_state->src_y = 0;
  2417. plane_state->src_w = fb->width << 16;
  2418. plane_state->src_h = fb->height << 16;
  2419. plane_state->crtc_x = 0;
  2420. plane_state->crtc_y = 0;
  2421. plane_state->crtc_w = fb->width;
  2422. plane_state->crtc_h = fb->height;
  2423. intel_state->base.src = drm_plane_state_src(plane_state);
  2424. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2425. obj = intel_fb_obj(fb);
  2426. if (i915_gem_object_is_tiled(obj))
  2427. dev_priv->preserve_bios_swizzle = true;
  2428. drm_framebuffer_get(fb);
  2429. primary->fb = primary->state->fb = fb;
  2430. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2431. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2432. to_intel_plane_state(plane_state),
  2433. true);
  2434. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2435. &obj->frontbuffer_bits);
  2436. }
  2437. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2438. unsigned int rotation)
  2439. {
  2440. int cpp = fb->format->cpp[plane];
  2441. switch (fb->modifier) {
  2442. case DRM_FORMAT_MOD_LINEAR:
  2443. case I915_FORMAT_MOD_X_TILED:
  2444. switch (cpp) {
  2445. case 8:
  2446. return 4096;
  2447. case 4:
  2448. case 2:
  2449. case 1:
  2450. return 8192;
  2451. default:
  2452. MISSING_CASE(cpp);
  2453. break;
  2454. }
  2455. break;
  2456. case I915_FORMAT_MOD_Y_TILED_CCS:
  2457. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2458. /* FIXME AUX plane? */
  2459. case I915_FORMAT_MOD_Y_TILED:
  2460. case I915_FORMAT_MOD_Yf_TILED:
  2461. switch (cpp) {
  2462. case 8:
  2463. return 2048;
  2464. case 4:
  2465. return 4096;
  2466. case 2:
  2467. case 1:
  2468. return 8192;
  2469. default:
  2470. MISSING_CASE(cpp);
  2471. break;
  2472. }
  2473. break;
  2474. default:
  2475. MISSING_CASE(fb->modifier);
  2476. }
  2477. return 2048;
  2478. }
  2479. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2480. int main_x, int main_y, u32 main_offset)
  2481. {
  2482. const struct drm_framebuffer *fb = plane_state->base.fb;
  2483. int hsub = fb->format->hsub;
  2484. int vsub = fb->format->vsub;
  2485. int aux_x = plane_state->aux.x;
  2486. int aux_y = plane_state->aux.y;
  2487. u32 aux_offset = plane_state->aux.offset;
  2488. u32 alignment = intel_surf_alignment(fb, 1);
  2489. while (aux_offset >= main_offset && aux_y <= main_y) {
  2490. int x, y;
  2491. if (aux_x == main_x && aux_y == main_y)
  2492. break;
  2493. if (aux_offset == 0)
  2494. break;
  2495. x = aux_x / hsub;
  2496. y = aux_y / vsub;
  2497. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2498. aux_offset, aux_offset - alignment);
  2499. aux_x = x * hsub + aux_x % hsub;
  2500. aux_y = y * vsub + aux_y % vsub;
  2501. }
  2502. if (aux_x != main_x || aux_y != main_y)
  2503. return false;
  2504. plane_state->aux.offset = aux_offset;
  2505. plane_state->aux.x = aux_x;
  2506. plane_state->aux.y = aux_y;
  2507. return true;
  2508. }
  2509. static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
  2510. struct intel_plane_state *plane_state)
  2511. {
  2512. struct drm_i915_private *dev_priv =
  2513. to_i915(plane_state->base.plane->dev);
  2514. const struct drm_framebuffer *fb = plane_state->base.fb;
  2515. unsigned int rotation = plane_state->base.rotation;
  2516. int x = plane_state->base.src.x1 >> 16;
  2517. int y = plane_state->base.src.y1 >> 16;
  2518. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2519. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2520. int dst_x = plane_state->base.dst.x1;
  2521. int pipe_src_w = crtc_state->pipe_src_w;
  2522. int max_width = skl_max_plane_width(fb, 0, rotation);
  2523. int max_height = 4096;
  2524. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2525. if (w > max_width || h > max_height) {
  2526. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2527. w, h, max_width, max_height);
  2528. return -EINVAL;
  2529. }
  2530. /*
  2531. * Display WA #1175: cnl,glk
  2532. * Planes other than the cursor may cause FIFO underflow and display
  2533. * corruption if starting less than 4 pixels from the right edge of
  2534. * the screen.
  2535. * Besides the above WA fix the similar problem, where planes other
  2536. * than the cursor ending less than 4 pixels from the left edge of the
  2537. * screen may cause FIFO underflow and display corruption.
  2538. */
  2539. if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  2540. (dst_x + w < 4 || dst_x > pipe_src_w - 4)) {
  2541. DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
  2542. dst_x + w < 4 ? "end" : "start",
  2543. dst_x + w < 4 ? dst_x + w : dst_x,
  2544. 4, pipe_src_w - 4);
  2545. return -ERANGE;
  2546. }
  2547. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2548. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2549. alignment = intel_surf_alignment(fb, 0);
  2550. /*
  2551. * AUX surface offset is specified as the distance from the
  2552. * main surface offset, and it must be non-negative. Make
  2553. * sure that is what we will get.
  2554. */
  2555. if (offset > aux_offset)
  2556. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2557. offset, aux_offset & ~(alignment - 1));
  2558. /*
  2559. * When using an X-tiled surface, the plane blows up
  2560. * if the x offset + width exceed the stride.
  2561. *
  2562. * TODO: linear and Y-tiled seem fine, Yf untested,
  2563. */
  2564. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2565. int cpp = fb->format->cpp[0];
  2566. while ((x + w) * cpp > fb->pitches[0]) {
  2567. if (offset == 0) {
  2568. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2569. return -EINVAL;
  2570. }
  2571. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2572. offset, offset - alignment);
  2573. }
  2574. }
  2575. /*
  2576. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2577. * they match with the main surface x/y offsets.
  2578. */
  2579. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2580. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2581. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2582. if (offset == 0)
  2583. break;
  2584. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2585. offset, offset - alignment);
  2586. }
  2587. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2588. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2589. return -EINVAL;
  2590. }
  2591. }
  2592. plane_state->main.offset = offset;
  2593. plane_state->main.x = x;
  2594. plane_state->main.y = y;
  2595. return 0;
  2596. }
  2597. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2598. {
  2599. const struct drm_framebuffer *fb = plane_state->base.fb;
  2600. unsigned int rotation = plane_state->base.rotation;
  2601. int max_width = skl_max_plane_width(fb, 1, rotation);
  2602. int max_height = 4096;
  2603. int x = plane_state->base.src.x1 >> 17;
  2604. int y = plane_state->base.src.y1 >> 17;
  2605. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2606. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2607. u32 offset;
  2608. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2609. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2610. /* FIXME not quite sure how/if these apply to the chroma plane */
  2611. if (w > max_width || h > max_height) {
  2612. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2613. w, h, max_width, max_height);
  2614. return -EINVAL;
  2615. }
  2616. plane_state->aux.offset = offset;
  2617. plane_state->aux.x = x;
  2618. plane_state->aux.y = y;
  2619. return 0;
  2620. }
  2621. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2622. {
  2623. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2624. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2625. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2626. const struct drm_framebuffer *fb = plane_state->base.fb;
  2627. int src_x = plane_state->base.src.x1 >> 16;
  2628. int src_y = plane_state->base.src.y1 >> 16;
  2629. int hsub = fb->format->hsub;
  2630. int vsub = fb->format->vsub;
  2631. int x = src_x / hsub;
  2632. int y = src_y / vsub;
  2633. u32 offset;
  2634. if (!skl_plane_has_ccs(dev_priv, crtc->pipe, plane->id)) {
  2635. DRM_DEBUG_KMS("No RC support on %s\n", plane->base.name);
  2636. return -EINVAL;
  2637. }
  2638. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2639. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2640. plane_state->base.rotation);
  2641. return -EINVAL;
  2642. }
  2643. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2644. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2645. plane_state->aux.offset = offset;
  2646. plane_state->aux.x = x * hsub + src_x % hsub;
  2647. plane_state->aux.y = y * vsub + src_y % vsub;
  2648. return 0;
  2649. }
  2650. int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
  2651. struct intel_plane_state *plane_state)
  2652. {
  2653. const struct drm_framebuffer *fb = plane_state->base.fb;
  2654. unsigned int rotation = plane_state->base.rotation;
  2655. int ret;
  2656. if (rotation & DRM_MODE_REFLECT_X &&
  2657. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2658. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2659. return -EINVAL;
  2660. }
  2661. if (!plane_state->base.visible)
  2662. return 0;
  2663. /* Rotate src coordinates to match rotated GTT view */
  2664. if (drm_rotation_90_or_270(rotation))
  2665. drm_rect_rotate(&plane_state->base.src,
  2666. fb->width << 16, fb->height << 16,
  2667. DRM_MODE_ROTATE_270);
  2668. /*
  2669. * Handle the AUX surface first since
  2670. * the main surface setup depends on it.
  2671. */
  2672. if (fb->format->format == DRM_FORMAT_NV12) {
  2673. ret = skl_check_nv12_aux_surface(plane_state);
  2674. if (ret)
  2675. return ret;
  2676. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2677. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2678. ret = skl_check_ccs_aux_surface(plane_state);
  2679. if (ret)
  2680. return ret;
  2681. } else {
  2682. plane_state->aux.offset = ~0xfff;
  2683. plane_state->aux.x = 0;
  2684. plane_state->aux.y = 0;
  2685. }
  2686. ret = skl_check_main_surface(crtc_state, plane_state);
  2687. if (ret)
  2688. return ret;
  2689. return 0;
  2690. }
  2691. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2692. const struct intel_plane_state *plane_state)
  2693. {
  2694. struct drm_i915_private *dev_priv =
  2695. to_i915(plane_state->base.plane->dev);
  2696. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2697. const struct drm_framebuffer *fb = plane_state->base.fb;
  2698. unsigned int rotation = plane_state->base.rotation;
  2699. u32 dspcntr;
  2700. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2701. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2702. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2703. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2704. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2705. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2706. if (INTEL_GEN(dev_priv) < 4)
  2707. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2708. switch (fb->format->format) {
  2709. case DRM_FORMAT_C8:
  2710. dspcntr |= DISPPLANE_8BPP;
  2711. break;
  2712. case DRM_FORMAT_XRGB1555:
  2713. dspcntr |= DISPPLANE_BGRX555;
  2714. break;
  2715. case DRM_FORMAT_RGB565:
  2716. dspcntr |= DISPPLANE_BGRX565;
  2717. break;
  2718. case DRM_FORMAT_XRGB8888:
  2719. dspcntr |= DISPPLANE_BGRX888;
  2720. break;
  2721. case DRM_FORMAT_XBGR8888:
  2722. dspcntr |= DISPPLANE_RGBX888;
  2723. break;
  2724. case DRM_FORMAT_XRGB2101010:
  2725. dspcntr |= DISPPLANE_BGRX101010;
  2726. break;
  2727. case DRM_FORMAT_XBGR2101010:
  2728. dspcntr |= DISPPLANE_RGBX101010;
  2729. break;
  2730. default:
  2731. MISSING_CASE(fb->format->format);
  2732. return 0;
  2733. }
  2734. if (INTEL_GEN(dev_priv) >= 4 &&
  2735. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2736. dspcntr |= DISPPLANE_TILED;
  2737. if (rotation & DRM_MODE_ROTATE_180)
  2738. dspcntr |= DISPPLANE_ROTATE_180;
  2739. if (rotation & DRM_MODE_REFLECT_X)
  2740. dspcntr |= DISPPLANE_MIRROR;
  2741. return dspcntr;
  2742. }
  2743. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2744. {
  2745. struct drm_i915_private *dev_priv =
  2746. to_i915(plane_state->base.plane->dev);
  2747. int src_x = plane_state->base.src.x1 >> 16;
  2748. int src_y = plane_state->base.src.y1 >> 16;
  2749. u32 offset;
  2750. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2751. if (INTEL_GEN(dev_priv) >= 4)
  2752. offset = intel_compute_tile_offset(&src_x, &src_y,
  2753. plane_state, 0);
  2754. else
  2755. offset = 0;
  2756. /* HSW/BDW do this automagically in hardware */
  2757. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2758. unsigned int rotation = plane_state->base.rotation;
  2759. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2760. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2761. if (rotation & DRM_MODE_ROTATE_180) {
  2762. src_x += src_w - 1;
  2763. src_y += src_h - 1;
  2764. } else if (rotation & DRM_MODE_REFLECT_X) {
  2765. src_x += src_w - 1;
  2766. }
  2767. }
  2768. plane_state->main.offset = offset;
  2769. plane_state->main.x = src_x;
  2770. plane_state->main.y = src_y;
  2771. return 0;
  2772. }
  2773. static void i9xx_update_plane(struct intel_plane *plane,
  2774. const struct intel_crtc_state *crtc_state,
  2775. const struct intel_plane_state *plane_state)
  2776. {
  2777. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2778. const struct drm_framebuffer *fb = plane_state->base.fb;
  2779. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2780. u32 linear_offset;
  2781. u32 dspcntr = plane_state->ctl;
  2782. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2783. int x = plane_state->main.x;
  2784. int y = plane_state->main.y;
  2785. unsigned long irqflags;
  2786. u32 dspaddr_offset;
  2787. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2788. if (INTEL_GEN(dev_priv) >= 4)
  2789. dspaddr_offset = plane_state->main.offset;
  2790. else
  2791. dspaddr_offset = linear_offset;
  2792. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2793. if (INTEL_GEN(dev_priv) < 4) {
  2794. /* pipesrc and dspsize control the size that is scaled from,
  2795. * which should always be the user's requested size.
  2796. */
  2797. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2798. ((crtc_state->pipe_src_h - 1) << 16) |
  2799. (crtc_state->pipe_src_w - 1));
  2800. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2801. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2802. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2803. ((crtc_state->pipe_src_h - 1) << 16) |
  2804. (crtc_state->pipe_src_w - 1));
  2805. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2806. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2807. }
  2808. I915_WRITE_FW(reg, dspcntr);
  2809. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2810. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2811. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2812. intel_plane_ggtt_offset(plane_state) +
  2813. dspaddr_offset);
  2814. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2815. } else if (INTEL_GEN(dev_priv) >= 4) {
  2816. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2817. intel_plane_ggtt_offset(plane_state) +
  2818. dspaddr_offset);
  2819. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2820. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2821. } else {
  2822. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2823. intel_plane_ggtt_offset(plane_state) +
  2824. dspaddr_offset);
  2825. }
  2826. POSTING_READ_FW(reg);
  2827. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2828. }
  2829. static void i9xx_disable_plane(struct intel_plane *plane,
  2830. struct intel_crtc *crtc)
  2831. {
  2832. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2833. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2834. unsigned long irqflags;
  2835. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2836. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2837. if (INTEL_GEN(dev_priv) >= 4)
  2838. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2839. else
  2840. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2841. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2842. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2843. }
  2844. static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
  2845. {
  2846. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2847. enum intel_display_power_domain power_domain;
  2848. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2849. enum pipe pipe = plane->pipe;
  2850. bool ret;
  2851. /*
  2852. * Not 100% correct for planes that can move between pipes,
  2853. * but that's only the case for gen2-4 which don't have any
  2854. * display power wells.
  2855. */
  2856. power_domain = POWER_DOMAIN_PIPE(pipe);
  2857. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2858. return false;
  2859. ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
  2860. intel_display_power_put(dev_priv, power_domain);
  2861. return ret;
  2862. }
  2863. static u32
  2864. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2865. {
  2866. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2867. return 64;
  2868. else
  2869. return intel_tile_width_bytes(fb, plane);
  2870. }
  2871. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2872. {
  2873. struct drm_device *dev = intel_crtc->base.dev;
  2874. struct drm_i915_private *dev_priv = to_i915(dev);
  2875. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2876. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2877. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2878. }
  2879. /*
  2880. * This function detaches (aka. unbinds) unused scalers in hardware
  2881. */
  2882. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2883. {
  2884. struct intel_crtc_scaler_state *scaler_state;
  2885. int i;
  2886. scaler_state = &intel_crtc->config->scaler_state;
  2887. /* loop through and disable scalers that aren't in use */
  2888. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2889. if (!scaler_state->scalers[i].in_use)
  2890. skl_detach_scaler(intel_crtc, i);
  2891. }
  2892. }
  2893. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2894. unsigned int rotation)
  2895. {
  2896. u32 stride;
  2897. if (plane >= fb->format->num_planes)
  2898. return 0;
  2899. stride = intel_fb_pitch(fb, plane, rotation);
  2900. /*
  2901. * The stride is either expressed as a multiple of 64 bytes chunks for
  2902. * linear buffers or in number of tiles for tiled buffers.
  2903. */
  2904. if (drm_rotation_90_or_270(rotation))
  2905. stride /= intel_tile_height(fb, plane);
  2906. else
  2907. stride /= intel_fb_stride_alignment(fb, plane);
  2908. return stride;
  2909. }
  2910. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2911. {
  2912. switch (pixel_format) {
  2913. case DRM_FORMAT_C8:
  2914. return PLANE_CTL_FORMAT_INDEXED;
  2915. case DRM_FORMAT_RGB565:
  2916. return PLANE_CTL_FORMAT_RGB_565;
  2917. case DRM_FORMAT_XBGR8888:
  2918. case DRM_FORMAT_ABGR8888:
  2919. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2920. case DRM_FORMAT_XRGB8888:
  2921. case DRM_FORMAT_ARGB8888:
  2922. return PLANE_CTL_FORMAT_XRGB_8888;
  2923. case DRM_FORMAT_XRGB2101010:
  2924. return PLANE_CTL_FORMAT_XRGB_2101010;
  2925. case DRM_FORMAT_XBGR2101010:
  2926. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2927. case DRM_FORMAT_YUYV:
  2928. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2929. case DRM_FORMAT_YVYU:
  2930. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2931. case DRM_FORMAT_UYVY:
  2932. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2933. case DRM_FORMAT_VYUY:
  2934. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2935. default:
  2936. MISSING_CASE(pixel_format);
  2937. }
  2938. return 0;
  2939. }
  2940. /*
  2941. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2942. * to be already pre-multiplied. We need to add a knob (or a different
  2943. * DRM_FORMAT) for user-space to configure that.
  2944. */
  2945. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  2946. {
  2947. switch (pixel_format) {
  2948. case DRM_FORMAT_ABGR8888:
  2949. case DRM_FORMAT_ARGB8888:
  2950. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2951. default:
  2952. return PLANE_CTL_ALPHA_DISABLE;
  2953. }
  2954. }
  2955. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  2956. {
  2957. switch (pixel_format) {
  2958. case DRM_FORMAT_ABGR8888:
  2959. case DRM_FORMAT_ARGB8888:
  2960. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  2961. default:
  2962. return PLANE_COLOR_ALPHA_DISABLE;
  2963. }
  2964. }
  2965. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2966. {
  2967. switch (fb_modifier) {
  2968. case DRM_FORMAT_MOD_LINEAR:
  2969. break;
  2970. case I915_FORMAT_MOD_X_TILED:
  2971. return PLANE_CTL_TILED_X;
  2972. case I915_FORMAT_MOD_Y_TILED:
  2973. return PLANE_CTL_TILED_Y;
  2974. case I915_FORMAT_MOD_Y_TILED_CCS:
  2975. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2976. case I915_FORMAT_MOD_Yf_TILED:
  2977. return PLANE_CTL_TILED_YF;
  2978. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2979. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  2980. default:
  2981. MISSING_CASE(fb_modifier);
  2982. }
  2983. return 0;
  2984. }
  2985. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  2986. {
  2987. switch (rotate) {
  2988. case DRM_MODE_ROTATE_0:
  2989. break;
  2990. /*
  2991. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2992. * while i915 HW rotation is clockwise, thats why this swapping.
  2993. */
  2994. case DRM_MODE_ROTATE_90:
  2995. return PLANE_CTL_ROTATE_270;
  2996. case DRM_MODE_ROTATE_180:
  2997. return PLANE_CTL_ROTATE_180;
  2998. case DRM_MODE_ROTATE_270:
  2999. return PLANE_CTL_ROTATE_90;
  3000. default:
  3001. MISSING_CASE(rotate);
  3002. }
  3003. return 0;
  3004. }
  3005. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3006. {
  3007. switch (reflect) {
  3008. case 0:
  3009. break;
  3010. case DRM_MODE_REFLECT_X:
  3011. return PLANE_CTL_FLIP_HORIZONTAL;
  3012. case DRM_MODE_REFLECT_Y:
  3013. default:
  3014. MISSING_CASE(reflect);
  3015. }
  3016. return 0;
  3017. }
  3018. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3019. const struct intel_plane_state *plane_state)
  3020. {
  3021. struct drm_i915_private *dev_priv =
  3022. to_i915(plane_state->base.plane->dev);
  3023. const struct drm_framebuffer *fb = plane_state->base.fb;
  3024. unsigned int rotation = plane_state->base.rotation;
  3025. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3026. u32 plane_ctl;
  3027. plane_ctl = PLANE_CTL_ENABLE;
  3028. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3029. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3030. plane_ctl |=
  3031. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3032. PLANE_CTL_PIPE_CSC_ENABLE |
  3033. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3034. }
  3035. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3036. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3037. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3038. if (INTEL_GEN(dev_priv) >= 10)
  3039. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3040. DRM_MODE_REFLECT_MASK);
  3041. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3042. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3043. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3044. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3045. return plane_ctl;
  3046. }
  3047. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3048. const struct intel_plane_state *plane_state)
  3049. {
  3050. const struct drm_framebuffer *fb = plane_state->base.fb;
  3051. u32 plane_color_ctl = 0;
  3052. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3053. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3054. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3055. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3056. return plane_color_ctl;
  3057. }
  3058. static int
  3059. __intel_display_resume(struct drm_device *dev,
  3060. struct drm_atomic_state *state,
  3061. struct drm_modeset_acquire_ctx *ctx)
  3062. {
  3063. struct drm_crtc_state *crtc_state;
  3064. struct drm_crtc *crtc;
  3065. int i, ret;
  3066. intel_modeset_setup_hw_state(dev, ctx);
  3067. i915_redisable_vga(to_i915(dev));
  3068. if (!state)
  3069. return 0;
  3070. /*
  3071. * We've duplicated the state, pointers to the old state are invalid.
  3072. *
  3073. * Don't attempt to use the old state until we commit the duplicated state.
  3074. */
  3075. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3076. /*
  3077. * Force recalculation even if we restore
  3078. * current state. With fast modeset this may not result
  3079. * in a modeset when the state is compatible.
  3080. */
  3081. crtc_state->mode_changed = true;
  3082. }
  3083. /* ignore any reset values/BIOS leftovers in the WM registers */
  3084. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3085. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3086. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3087. WARN_ON(ret == -EDEADLK);
  3088. return ret;
  3089. }
  3090. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3091. {
  3092. return intel_has_gpu_reset(dev_priv) &&
  3093. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3094. }
  3095. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3096. {
  3097. struct drm_device *dev = &dev_priv->drm;
  3098. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3099. struct drm_atomic_state *state;
  3100. int ret;
  3101. /* reset doesn't touch the display */
  3102. if (!i915_modparams.force_reset_modeset_test &&
  3103. !gpu_reset_clobbers_display(dev_priv))
  3104. return;
  3105. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3106. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3107. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3108. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3109. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3110. i915_gem_set_wedged(dev_priv);
  3111. }
  3112. /*
  3113. * Need mode_config.mutex so that we don't
  3114. * trample ongoing ->detect() and whatnot.
  3115. */
  3116. mutex_lock(&dev->mode_config.mutex);
  3117. drm_modeset_acquire_init(ctx, 0);
  3118. while (1) {
  3119. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3120. if (ret != -EDEADLK)
  3121. break;
  3122. drm_modeset_backoff(ctx);
  3123. }
  3124. /*
  3125. * Disabling the crtcs gracefully seems nicer. Also the
  3126. * g33 docs say we should at least disable all the planes.
  3127. */
  3128. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3129. if (IS_ERR(state)) {
  3130. ret = PTR_ERR(state);
  3131. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3132. return;
  3133. }
  3134. ret = drm_atomic_helper_disable_all(dev, ctx);
  3135. if (ret) {
  3136. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3137. drm_atomic_state_put(state);
  3138. return;
  3139. }
  3140. dev_priv->modeset_restore_state = state;
  3141. state->acquire_ctx = ctx;
  3142. }
  3143. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3144. {
  3145. struct drm_device *dev = &dev_priv->drm;
  3146. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3147. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3148. int ret;
  3149. /* reset doesn't touch the display */
  3150. if (!i915_modparams.force_reset_modeset_test &&
  3151. !gpu_reset_clobbers_display(dev_priv))
  3152. return;
  3153. if (!state)
  3154. goto unlock;
  3155. dev_priv->modeset_restore_state = NULL;
  3156. /* reset doesn't touch the display */
  3157. if (!gpu_reset_clobbers_display(dev_priv)) {
  3158. /* for testing only restore the display */
  3159. ret = __intel_display_resume(dev, state, ctx);
  3160. if (ret)
  3161. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3162. } else {
  3163. /*
  3164. * The display has been reset as well,
  3165. * so need a full re-initialization.
  3166. */
  3167. intel_runtime_pm_disable_interrupts(dev_priv);
  3168. intel_runtime_pm_enable_interrupts(dev_priv);
  3169. intel_pps_unlock_regs_wa(dev_priv);
  3170. intel_modeset_init_hw(dev);
  3171. intel_init_clock_gating(dev_priv);
  3172. spin_lock_irq(&dev_priv->irq_lock);
  3173. if (dev_priv->display.hpd_irq_setup)
  3174. dev_priv->display.hpd_irq_setup(dev_priv);
  3175. spin_unlock_irq(&dev_priv->irq_lock);
  3176. ret = __intel_display_resume(dev, state, ctx);
  3177. if (ret)
  3178. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3179. intel_hpd_init(dev_priv);
  3180. }
  3181. drm_atomic_state_put(state);
  3182. unlock:
  3183. drm_modeset_drop_locks(ctx);
  3184. drm_modeset_acquire_fini(ctx);
  3185. mutex_unlock(&dev->mode_config.mutex);
  3186. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3187. }
  3188. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3189. const struct intel_crtc_state *new_crtc_state)
  3190. {
  3191. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3192. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3193. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3194. crtc->base.mode = new_crtc_state->base.mode;
  3195. /*
  3196. * Update pipe size and adjust fitter if needed: the reason for this is
  3197. * that in compute_mode_changes we check the native mode (not the pfit
  3198. * mode) to see if we can flip rather than do a full mode set. In the
  3199. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3200. * pfit state, we'll end up with a big fb scanned out into the wrong
  3201. * sized surface.
  3202. */
  3203. I915_WRITE(PIPESRC(crtc->pipe),
  3204. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3205. (new_crtc_state->pipe_src_h - 1));
  3206. /* on skylake this is done by detaching scalers */
  3207. if (INTEL_GEN(dev_priv) >= 9) {
  3208. skl_detach_scalers(crtc);
  3209. if (new_crtc_state->pch_pfit.enabled)
  3210. skylake_pfit_enable(crtc);
  3211. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3212. if (new_crtc_state->pch_pfit.enabled)
  3213. ironlake_pfit_enable(crtc);
  3214. else if (old_crtc_state->pch_pfit.enabled)
  3215. ironlake_pfit_disable(crtc, true);
  3216. }
  3217. }
  3218. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3219. {
  3220. struct drm_device *dev = crtc->base.dev;
  3221. struct drm_i915_private *dev_priv = to_i915(dev);
  3222. int pipe = crtc->pipe;
  3223. i915_reg_t reg;
  3224. u32 temp;
  3225. /* enable normal train */
  3226. reg = FDI_TX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. if (IS_IVYBRIDGE(dev_priv)) {
  3229. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3230. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3231. } else {
  3232. temp &= ~FDI_LINK_TRAIN_NONE;
  3233. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3234. }
  3235. I915_WRITE(reg, temp);
  3236. reg = FDI_RX_CTL(pipe);
  3237. temp = I915_READ(reg);
  3238. if (HAS_PCH_CPT(dev_priv)) {
  3239. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3240. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3241. } else {
  3242. temp &= ~FDI_LINK_TRAIN_NONE;
  3243. temp |= FDI_LINK_TRAIN_NONE;
  3244. }
  3245. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3246. /* wait one idle pattern time */
  3247. POSTING_READ(reg);
  3248. udelay(1000);
  3249. /* IVB wants error correction enabled */
  3250. if (IS_IVYBRIDGE(dev_priv))
  3251. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3252. FDI_FE_ERRC_ENABLE);
  3253. }
  3254. /* The FDI link training functions for ILK/Ibexpeak. */
  3255. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3256. const struct intel_crtc_state *crtc_state)
  3257. {
  3258. struct drm_device *dev = crtc->base.dev;
  3259. struct drm_i915_private *dev_priv = to_i915(dev);
  3260. int pipe = crtc->pipe;
  3261. i915_reg_t reg;
  3262. u32 temp, tries;
  3263. /* FDI needs bits from pipe first */
  3264. assert_pipe_enabled(dev_priv, pipe);
  3265. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3266. for train result */
  3267. reg = FDI_RX_IMR(pipe);
  3268. temp = I915_READ(reg);
  3269. temp &= ~FDI_RX_SYMBOL_LOCK;
  3270. temp &= ~FDI_RX_BIT_LOCK;
  3271. I915_WRITE(reg, temp);
  3272. I915_READ(reg);
  3273. udelay(150);
  3274. /* enable CPU FDI TX and PCH FDI RX */
  3275. reg = FDI_TX_CTL(pipe);
  3276. temp = I915_READ(reg);
  3277. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3278. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3279. temp &= ~FDI_LINK_TRAIN_NONE;
  3280. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3281. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3282. reg = FDI_RX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. temp &= ~FDI_LINK_TRAIN_NONE;
  3285. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3286. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3287. POSTING_READ(reg);
  3288. udelay(150);
  3289. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3290. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3291. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3292. FDI_RX_PHASE_SYNC_POINTER_EN);
  3293. reg = FDI_RX_IIR(pipe);
  3294. for (tries = 0; tries < 5; tries++) {
  3295. temp = I915_READ(reg);
  3296. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3297. if ((temp & FDI_RX_BIT_LOCK)) {
  3298. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3299. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3300. break;
  3301. }
  3302. }
  3303. if (tries == 5)
  3304. DRM_ERROR("FDI train 1 fail!\n");
  3305. /* Train 2 */
  3306. reg = FDI_TX_CTL(pipe);
  3307. temp = I915_READ(reg);
  3308. temp &= ~FDI_LINK_TRAIN_NONE;
  3309. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3310. I915_WRITE(reg, temp);
  3311. reg = FDI_RX_CTL(pipe);
  3312. temp = I915_READ(reg);
  3313. temp &= ~FDI_LINK_TRAIN_NONE;
  3314. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3315. I915_WRITE(reg, temp);
  3316. POSTING_READ(reg);
  3317. udelay(150);
  3318. reg = FDI_RX_IIR(pipe);
  3319. for (tries = 0; tries < 5; tries++) {
  3320. temp = I915_READ(reg);
  3321. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3322. if (temp & FDI_RX_SYMBOL_LOCK) {
  3323. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3324. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3325. break;
  3326. }
  3327. }
  3328. if (tries == 5)
  3329. DRM_ERROR("FDI train 2 fail!\n");
  3330. DRM_DEBUG_KMS("FDI train done\n");
  3331. }
  3332. static const int snb_b_fdi_train_param[] = {
  3333. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3334. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3335. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3336. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3337. };
  3338. /* The FDI link training functions for SNB/Cougarpoint. */
  3339. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3340. const struct intel_crtc_state *crtc_state)
  3341. {
  3342. struct drm_device *dev = crtc->base.dev;
  3343. struct drm_i915_private *dev_priv = to_i915(dev);
  3344. int pipe = crtc->pipe;
  3345. i915_reg_t reg;
  3346. u32 temp, i, retry;
  3347. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3348. for train result */
  3349. reg = FDI_RX_IMR(pipe);
  3350. temp = I915_READ(reg);
  3351. temp &= ~FDI_RX_SYMBOL_LOCK;
  3352. temp &= ~FDI_RX_BIT_LOCK;
  3353. I915_WRITE(reg, temp);
  3354. POSTING_READ(reg);
  3355. udelay(150);
  3356. /* enable CPU FDI TX and PCH FDI RX */
  3357. reg = FDI_TX_CTL(pipe);
  3358. temp = I915_READ(reg);
  3359. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3360. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3361. temp &= ~FDI_LINK_TRAIN_NONE;
  3362. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3363. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3364. /* SNB-B */
  3365. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3366. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3367. I915_WRITE(FDI_RX_MISC(pipe),
  3368. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3369. reg = FDI_RX_CTL(pipe);
  3370. temp = I915_READ(reg);
  3371. if (HAS_PCH_CPT(dev_priv)) {
  3372. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3373. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3374. } else {
  3375. temp &= ~FDI_LINK_TRAIN_NONE;
  3376. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3377. }
  3378. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3379. POSTING_READ(reg);
  3380. udelay(150);
  3381. for (i = 0; i < 4; i++) {
  3382. reg = FDI_TX_CTL(pipe);
  3383. temp = I915_READ(reg);
  3384. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3385. temp |= snb_b_fdi_train_param[i];
  3386. I915_WRITE(reg, temp);
  3387. POSTING_READ(reg);
  3388. udelay(500);
  3389. for (retry = 0; retry < 5; retry++) {
  3390. reg = FDI_RX_IIR(pipe);
  3391. temp = I915_READ(reg);
  3392. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3393. if (temp & FDI_RX_BIT_LOCK) {
  3394. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3395. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3396. break;
  3397. }
  3398. udelay(50);
  3399. }
  3400. if (retry < 5)
  3401. break;
  3402. }
  3403. if (i == 4)
  3404. DRM_ERROR("FDI train 1 fail!\n");
  3405. /* Train 2 */
  3406. reg = FDI_TX_CTL(pipe);
  3407. temp = I915_READ(reg);
  3408. temp &= ~FDI_LINK_TRAIN_NONE;
  3409. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3410. if (IS_GEN6(dev_priv)) {
  3411. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3412. /* SNB-B */
  3413. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3414. }
  3415. I915_WRITE(reg, temp);
  3416. reg = FDI_RX_CTL(pipe);
  3417. temp = I915_READ(reg);
  3418. if (HAS_PCH_CPT(dev_priv)) {
  3419. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3420. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3421. } else {
  3422. temp &= ~FDI_LINK_TRAIN_NONE;
  3423. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3424. }
  3425. I915_WRITE(reg, temp);
  3426. POSTING_READ(reg);
  3427. udelay(150);
  3428. for (i = 0; i < 4; i++) {
  3429. reg = FDI_TX_CTL(pipe);
  3430. temp = I915_READ(reg);
  3431. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3432. temp |= snb_b_fdi_train_param[i];
  3433. I915_WRITE(reg, temp);
  3434. POSTING_READ(reg);
  3435. udelay(500);
  3436. for (retry = 0; retry < 5; retry++) {
  3437. reg = FDI_RX_IIR(pipe);
  3438. temp = I915_READ(reg);
  3439. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3440. if (temp & FDI_RX_SYMBOL_LOCK) {
  3441. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3442. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3443. break;
  3444. }
  3445. udelay(50);
  3446. }
  3447. if (retry < 5)
  3448. break;
  3449. }
  3450. if (i == 4)
  3451. DRM_ERROR("FDI train 2 fail!\n");
  3452. DRM_DEBUG_KMS("FDI train done.\n");
  3453. }
  3454. /* Manual link training for Ivy Bridge A0 parts */
  3455. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3456. const struct intel_crtc_state *crtc_state)
  3457. {
  3458. struct drm_device *dev = crtc->base.dev;
  3459. struct drm_i915_private *dev_priv = to_i915(dev);
  3460. int pipe = crtc->pipe;
  3461. i915_reg_t reg;
  3462. u32 temp, i, j;
  3463. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3464. for train result */
  3465. reg = FDI_RX_IMR(pipe);
  3466. temp = I915_READ(reg);
  3467. temp &= ~FDI_RX_SYMBOL_LOCK;
  3468. temp &= ~FDI_RX_BIT_LOCK;
  3469. I915_WRITE(reg, temp);
  3470. POSTING_READ(reg);
  3471. udelay(150);
  3472. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3473. I915_READ(FDI_RX_IIR(pipe)));
  3474. /* Try each vswing and preemphasis setting twice before moving on */
  3475. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3476. /* disable first in case we need to retry */
  3477. reg = FDI_TX_CTL(pipe);
  3478. temp = I915_READ(reg);
  3479. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3480. temp &= ~FDI_TX_ENABLE;
  3481. I915_WRITE(reg, temp);
  3482. reg = FDI_RX_CTL(pipe);
  3483. temp = I915_READ(reg);
  3484. temp &= ~FDI_LINK_TRAIN_AUTO;
  3485. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3486. temp &= ~FDI_RX_ENABLE;
  3487. I915_WRITE(reg, temp);
  3488. /* enable CPU FDI TX and PCH FDI RX */
  3489. reg = FDI_TX_CTL(pipe);
  3490. temp = I915_READ(reg);
  3491. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3492. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3493. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3494. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3495. temp |= snb_b_fdi_train_param[j/2];
  3496. temp |= FDI_COMPOSITE_SYNC;
  3497. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3498. I915_WRITE(FDI_RX_MISC(pipe),
  3499. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3500. reg = FDI_RX_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3503. temp |= FDI_COMPOSITE_SYNC;
  3504. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3505. POSTING_READ(reg);
  3506. udelay(1); /* should be 0.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_BIT_LOCK ||
  3512. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3513. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3514. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3515. i);
  3516. break;
  3517. }
  3518. udelay(1); /* should be 0.5us */
  3519. }
  3520. if (i == 4) {
  3521. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3522. continue;
  3523. }
  3524. /* Train 2 */
  3525. reg = FDI_TX_CTL(pipe);
  3526. temp = I915_READ(reg);
  3527. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3528. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3529. I915_WRITE(reg, temp);
  3530. reg = FDI_RX_CTL(pipe);
  3531. temp = I915_READ(reg);
  3532. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3533. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3534. I915_WRITE(reg, temp);
  3535. POSTING_READ(reg);
  3536. udelay(2); /* should be 1.5us */
  3537. for (i = 0; i < 4; i++) {
  3538. reg = FDI_RX_IIR(pipe);
  3539. temp = I915_READ(reg);
  3540. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3541. if (temp & FDI_RX_SYMBOL_LOCK ||
  3542. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3543. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3544. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3545. i);
  3546. goto train_done;
  3547. }
  3548. udelay(2); /* should be 1.5us */
  3549. }
  3550. if (i == 4)
  3551. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3552. }
  3553. train_done:
  3554. DRM_DEBUG_KMS("FDI train done.\n");
  3555. }
  3556. static void ironlake_fdi_pll_enable(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. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3564. reg = FDI_RX_CTL(pipe);
  3565. temp = I915_READ(reg);
  3566. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3567. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3568. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3569. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3570. POSTING_READ(reg);
  3571. udelay(200);
  3572. /* Switch from Rawclk to PCDclk */
  3573. temp = I915_READ(reg);
  3574. I915_WRITE(reg, temp | FDI_PCDCLK);
  3575. POSTING_READ(reg);
  3576. udelay(200);
  3577. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3578. reg = FDI_TX_CTL(pipe);
  3579. temp = I915_READ(reg);
  3580. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3581. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3582. POSTING_READ(reg);
  3583. udelay(100);
  3584. }
  3585. }
  3586. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3587. {
  3588. struct drm_device *dev = intel_crtc->base.dev;
  3589. struct drm_i915_private *dev_priv = to_i915(dev);
  3590. int pipe = intel_crtc->pipe;
  3591. i915_reg_t reg;
  3592. u32 temp;
  3593. /* Switch from PCDclk to Rawclk */
  3594. reg = FDI_RX_CTL(pipe);
  3595. temp = I915_READ(reg);
  3596. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3597. /* Disable CPU FDI TX PLL */
  3598. reg = FDI_TX_CTL(pipe);
  3599. temp = I915_READ(reg);
  3600. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3601. POSTING_READ(reg);
  3602. udelay(100);
  3603. reg = FDI_RX_CTL(pipe);
  3604. temp = I915_READ(reg);
  3605. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3606. /* Wait for the clocks to turn off. */
  3607. POSTING_READ(reg);
  3608. udelay(100);
  3609. }
  3610. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3611. {
  3612. struct drm_device *dev = crtc->dev;
  3613. struct drm_i915_private *dev_priv = to_i915(dev);
  3614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3615. int pipe = intel_crtc->pipe;
  3616. i915_reg_t reg;
  3617. u32 temp;
  3618. /* disable CPU FDI tx and PCH FDI rx */
  3619. reg = FDI_TX_CTL(pipe);
  3620. temp = I915_READ(reg);
  3621. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3622. POSTING_READ(reg);
  3623. reg = FDI_RX_CTL(pipe);
  3624. temp = I915_READ(reg);
  3625. temp &= ~(0x7 << 16);
  3626. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3627. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3628. POSTING_READ(reg);
  3629. udelay(100);
  3630. /* Ironlake workaround, disable clock pointer after downing FDI */
  3631. if (HAS_PCH_IBX(dev_priv))
  3632. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3633. /* still set train pattern 1 */
  3634. reg = FDI_TX_CTL(pipe);
  3635. temp = I915_READ(reg);
  3636. temp &= ~FDI_LINK_TRAIN_NONE;
  3637. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3638. I915_WRITE(reg, temp);
  3639. reg = FDI_RX_CTL(pipe);
  3640. temp = I915_READ(reg);
  3641. if (HAS_PCH_CPT(dev_priv)) {
  3642. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3643. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3644. } else {
  3645. temp &= ~FDI_LINK_TRAIN_NONE;
  3646. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3647. }
  3648. /* BPC in FDI rx is consistent with that in PIPECONF */
  3649. temp &= ~(0x07 << 16);
  3650. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3651. I915_WRITE(reg, temp);
  3652. POSTING_READ(reg);
  3653. udelay(100);
  3654. }
  3655. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3656. {
  3657. struct drm_crtc *crtc;
  3658. bool cleanup_done;
  3659. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3660. struct drm_crtc_commit *commit;
  3661. spin_lock(&crtc->commit_lock);
  3662. commit = list_first_entry_or_null(&crtc->commit_list,
  3663. struct drm_crtc_commit, commit_entry);
  3664. cleanup_done = commit ?
  3665. try_wait_for_completion(&commit->cleanup_done) : true;
  3666. spin_unlock(&crtc->commit_lock);
  3667. if (cleanup_done)
  3668. continue;
  3669. drm_crtc_wait_one_vblank(crtc);
  3670. return true;
  3671. }
  3672. return false;
  3673. }
  3674. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3675. {
  3676. u32 temp;
  3677. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3678. mutex_lock(&dev_priv->sb_lock);
  3679. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3680. temp |= SBI_SSCCTL_DISABLE;
  3681. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3682. mutex_unlock(&dev_priv->sb_lock);
  3683. }
  3684. /* Program iCLKIP clock to the desired frequency */
  3685. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3686. {
  3687. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3688. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3689. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3690. u32 temp;
  3691. lpt_disable_iclkip(dev_priv);
  3692. /* The iCLK virtual clock root frequency is in MHz,
  3693. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3694. * divisors, it is necessary to divide one by another, so we
  3695. * convert the virtual clock precision to KHz here for higher
  3696. * precision.
  3697. */
  3698. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3699. u32 iclk_virtual_root_freq = 172800 * 1000;
  3700. u32 iclk_pi_range = 64;
  3701. u32 desired_divisor;
  3702. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3703. clock << auxdiv);
  3704. divsel = (desired_divisor / iclk_pi_range) - 2;
  3705. phaseinc = desired_divisor % iclk_pi_range;
  3706. /*
  3707. * Near 20MHz is a corner case which is
  3708. * out of range for the 7-bit divisor
  3709. */
  3710. if (divsel <= 0x7f)
  3711. break;
  3712. }
  3713. /* This should not happen with any sane values */
  3714. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3715. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3716. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3717. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3718. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3719. clock,
  3720. auxdiv,
  3721. divsel,
  3722. phasedir,
  3723. phaseinc);
  3724. mutex_lock(&dev_priv->sb_lock);
  3725. /* Program SSCDIVINTPHASE6 */
  3726. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3727. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3728. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3729. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3730. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3731. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3732. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3733. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3734. /* Program SSCAUXDIV */
  3735. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3736. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3737. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3738. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3739. /* Enable modulator and associated divider */
  3740. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3741. temp &= ~SBI_SSCCTL_DISABLE;
  3742. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3743. mutex_unlock(&dev_priv->sb_lock);
  3744. /* Wait for initialization time */
  3745. udelay(24);
  3746. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3747. }
  3748. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3749. {
  3750. u32 divsel, phaseinc, auxdiv;
  3751. u32 iclk_virtual_root_freq = 172800 * 1000;
  3752. u32 iclk_pi_range = 64;
  3753. u32 desired_divisor;
  3754. u32 temp;
  3755. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3756. return 0;
  3757. mutex_lock(&dev_priv->sb_lock);
  3758. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3759. if (temp & SBI_SSCCTL_DISABLE) {
  3760. mutex_unlock(&dev_priv->sb_lock);
  3761. return 0;
  3762. }
  3763. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3764. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3765. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3766. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3767. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3768. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3769. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3770. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3771. mutex_unlock(&dev_priv->sb_lock);
  3772. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3773. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3774. desired_divisor << auxdiv);
  3775. }
  3776. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3777. enum pipe pch_transcoder)
  3778. {
  3779. struct drm_device *dev = crtc->base.dev;
  3780. struct drm_i915_private *dev_priv = to_i915(dev);
  3781. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3782. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3783. I915_READ(HTOTAL(cpu_transcoder)));
  3784. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3785. I915_READ(HBLANK(cpu_transcoder)));
  3786. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3787. I915_READ(HSYNC(cpu_transcoder)));
  3788. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3789. I915_READ(VTOTAL(cpu_transcoder)));
  3790. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3791. I915_READ(VBLANK(cpu_transcoder)));
  3792. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3793. I915_READ(VSYNC(cpu_transcoder)));
  3794. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3795. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3796. }
  3797. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3798. {
  3799. struct drm_i915_private *dev_priv = to_i915(dev);
  3800. uint32_t temp;
  3801. temp = I915_READ(SOUTH_CHICKEN1);
  3802. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3803. return;
  3804. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3805. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3806. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3807. if (enable)
  3808. temp |= FDI_BC_BIFURCATION_SELECT;
  3809. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3810. I915_WRITE(SOUTH_CHICKEN1, temp);
  3811. POSTING_READ(SOUTH_CHICKEN1);
  3812. }
  3813. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3814. {
  3815. struct drm_device *dev = intel_crtc->base.dev;
  3816. switch (intel_crtc->pipe) {
  3817. case PIPE_A:
  3818. break;
  3819. case PIPE_B:
  3820. if (intel_crtc->config->fdi_lanes > 2)
  3821. cpt_set_fdi_bc_bifurcation(dev, false);
  3822. else
  3823. cpt_set_fdi_bc_bifurcation(dev, true);
  3824. break;
  3825. case PIPE_C:
  3826. cpt_set_fdi_bc_bifurcation(dev, true);
  3827. break;
  3828. default:
  3829. BUG();
  3830. }
  3831. }
  3832. /* Return which DP Port should be selected for Transcoder DP control */
  3833. static enum port
  3834. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3835. {
  3836. struct drm_device *dev = crtc->base.dev;
  3837. struct intel_encoder *encoder;
  3838. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3839. if (encoder->type == INTEL_OUTPUT_DP ||
  3840. encoder->type == INTEL_OUTPUT_EDP)
  3841. return encoder->port;
  3842. }
  3843. return -1;
  3844. }
  3845. /*
  3846. * Enable PCH resources required for PCH ports:
  3847. * - PCH PLLs
  3848. * - FDI training & RX/TX
  3849. * - update transcoder timings
  3850. * - DP transcoding bits
  3851. * - transcoder
  3852. */
  3853. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3854. {
  3855. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3856. struct drm_device *dev = crtc->base.dev;
  3857. struct drm_i915_private *dev_priv = to_i915(dev);
  3858. int pipe = crtc->pipe;
  3859. u32 temp;
  3860. assert_pch_transcoder_disabled(dev_priv, pipe);
  3861. if (IS_IVYBRIDGE(dev_priv))
  3862. ivybridge_update_fdi_bc_bifurcation(crtc);
  3863. /* Write the TU size bits before fdi link training, so that error
  3864. * detection works. */
  3865. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3866. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3867. /* For PCH output, training FDI link */
  3868. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3869. /* We need to program the right clock selection before writing the pixel
  3870. * mutliplier into the DPLL. */
  3871. if (HAS_PCH_CPT(dev_priv)) {
  3872. u32 sel;
  3873. temp = I915_READ(PCH_DPLL_SEL);
  3874. temp |= TRANS_DPLL_ENABLE(pipe);
  3875. sel = TRANS_DPLLB_SEL(pipe);
  3876. if (crtc_state->shared_dpll ==
  3877. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3878. temp |= sel;
  3879. else
  3880. temp &= ~sel;
  3881. I915_WRITE(PCH_DPLL_SEL, temp);
  3882. }
  3883. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3884. * transcoder, and we actually should do this to not upset any PCH
  3885. * transcoder that already use the clock when we share it.
  3886. *
  3887. * Note that enable_shared_dpll tries to do the right thing, but
  3888. * get_shared_dpll unconditionally resets the pll - we need that to have
  3889. * the right LVDS enable sequence. */
  3890. intel_enable_shared_dpll(crtc);
  3891. /* set transcoder timing, panel must allow it */
  3892. assert_panel_unlocked(dev_priv, pipe);
  3893. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3894. intel_fdi_normal_train(crtc);
  3895. /* For PCH DP, enable TRANS_DP_CTL */
  3896. if (HAS_PCH_CPT(dev_priv) &&
  3897. intel_crtc_has_dp_encoder(crtc_state)) {
  3898. const struct drm_display_mode *adjusted_mode =
  3899. &crtc_state->base.adjusted_mode;
  3900. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3901. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3902. temp = I915_READ(reg);
  3903. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3904. TRANS_DP_SYNC_MASK |
  3905. TRANS_DP_BPC_MASK);
  3906. temp |= TRANS_DP_OUTPUT_ENABLE;
  3907. temp |= bpc << 9; /* same format but at 11:9 */
  3908. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3909. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3910. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3911. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3912. switch (intel_trans_dp_port_sel(crtc)) {
  3913. case PORT_B:
  3914. temp |= TRANS_DP_PORT_SEL_B;
  3915. break;
  3916. case PORT_C:
  3917. temp |= TRANS_DP_PORT_SEL_C;
  3918. break;
  3919. case PORT_D:
  3920. temp |= TRANS_DP_PORT_SEL_D;
  3921. break;
  3922. default:
  3923. BUG();
  3924. }
  3925. I915_WRITE(reg, temp);
  3926. }
  3927. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3928. }
  3929. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3930. {
  3931. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3932. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3933. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3934. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3935. lpt_program_iclkip(crtc);
  3936. /* Set transcoder timing. */
  3937. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3938. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3939. }
  3940. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3941. {
  3942. struct drm_i915_private *dev_priv = to_i915(dev);
  3943. i915_reg_t dslreg = PIPEDSL(pipe);
  3944. u32 temp;
  3945. temp = I915_READ(dslreg);
  3946. udelay(500);
  3947. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3948. if (wait_for(I915_READ(dslreg) != temp, 5))
  3949. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3950. }
  3951. }
  3952. static int
  3953. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3954. unsigned int scaler_user, int *scaler_id,
  3955. int src_w, int src_h, int dst_w, int dst_h)
  3956. {
  3957. struct intel_crtc_scaler_state *scaler_state =
  3958. &crtc_state->scaler_state;
  3959. struct intel_crtc *intel_crtc =
  3960. to_intel_crtc(crtc_state->base.crtc);
  3961. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3962. const struct drm_display_mode *adjusted_mode =
  3963. &crtc_state->base.adjusted_mode;
  3964. int need_scaling;
  3965. /*
  3966. * Src coordinates are already rotated by 270 degrees for
  3967. * the 90/270 degree plane rotation cases (to match the
  3968. * GTT mapping), hence no need to account for rotation here.
  3969. */
  3970. need_scaling = src_w != dst_w || src_h != dst_h;
  3971. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  3972. need_scaling = true;
  3973. /*
  3974. * Scaling/fitting not supported in IF-ID mode in GEN9+
  3975. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  3976. * Once NV12 is enabled, handle it here while allocating scaler
  3977. * for NV12.
  3978. */
  3979. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  3980. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3981. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  3982. return -EINVAL;
  3983. }
  3984. /*
  3985. * if plane is being disabled or scaler is no more required or force detach
  3986. * - free scaler binded to this plane/crtc
  3987. * - in order to do this, update crtc->scaler_usage
  3988. *
  3989. * Here scaler state in crtc_state is set free so that
  3990. * scaler can be assigned to other user. Actual register
  3991. * update to free the scaler is done in plane/panel-fit programming.
  3992. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3993. */
  3994. if (force_detach || !need_scaling) {
  3995. if (*scaler_id >= 0) {
  3996. scaler_state->scaler_users &= ~(1 << scaler_user);
  3997. scaler_state->scalers[*scaler_id].in_use = 0;
  3998. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3999. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4000. intel_crtc->pipe, scaler_user, *scaler_id,
  4001. scaler_state->scaler_users);
  4002. *scaler_id = -1;
  4003. }
  4004. return 0;
  4005. }
  4006. /* range checks */
  4007. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4008. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4009. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4010. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4011. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4012. "size is out of scaler range\n",
  4013. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4014. return -EINVAL;
  4015. }
  4016. /* mark this plane as a scaler user in crtc_state */
  4017. scaler_state->scaler_users |= (1 << scaler_user);
  4018. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4019. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4020. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4021. scaler_state->scaler_users);
  4022. return 0;
  4023. }
  4024. /**
  4025. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4026. *
  4027. * @state: crtc's scaler state
  4028. *
  4029. * Return
  4030. * 0 - scaler_usage updated successfully
  4031. * error - requested scaling cannot be supported or other error condition
  4032. */
  4033. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4034. {
  4035. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4036. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4037. &state->scaler_state.scaler_id,
  4038. state->pipe_src_w, state->pipe_src_h,
  4039. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4040. }
  4041. /**
  4042. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4043. *
  4044. * @state: crtc's scaler state
  4045. * @plane_state: atomic plane state to update
  4046. *
  4047. * Return
  4048. * 0 - scaler_usage updated successfully
  4049. * error - requested scaling cannot be supported or other error condition
  4050. */
  4051. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4052. struct intel_plane_state *plane_state)
  4053. {
  4054. struct intel_plane *intel_plane =
  4055. to_intel_plane(plane_state->base.plane);
  4056. struct drm_framebuffer *fb = plane_state->base.fb;
  4057. int ret;
  4058. bool force_detach = !fb || !plane_state->base.visible;
  4059. ret = skl_update_scaler(crtc_state, force_detach,
  4060. drm_plane_index(&intel_plane->base),
  4061. &plane_state->scaler_id,
  4062. drm_rect_width(&plane_state->base.src) >> 16,
  4063. drm_rect_height(&plane_state->base.src) >> 16,
  4064. drm_rect_width(&plane_state->base.dst),
  4065. drm_rect_height(&plane_state->base.dst));
  4066. if (ret || plane_state->scaler_id < 0)
  4067. return ret;
  4068. /* check colorkey */
  4069. if (plane_state->ckey.flags) {
  4070. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4071. intel_plane->base.base.id,
  4072. intel_plane->base.name);
  4073. return -EINVAL;
  4074. }
  4075. /* Check src format */
  4076. switch (fb->format->format) {
  4077. case DRM_FORMAT_RGB565:
  4078. case DRM_FORMAT_XBGR8888:
  4079. case DRM_FORMAT_XRGB8888:
  4080. case DRM_FORMAT_ABGR8888:
  4081. case DRM_FORMAT_ARGB8888:
  4082. case DRM_FORMAT_XRGB2101010:
  4083. case DRM_FORMAT_XBGR2101010:
  4084. case DRM_FORMAT_YUYV:
  4085. case DRM_FORMAT_YVYU:
  4086. case DRM_FORMAT_UYVY:
  4087. case DRM_FORMAT_VYUY:
  4088. break;
  4089. default:
  4090. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4091. intel_plane->base.base.id, intel_plane->base.name,
  4092. fb->base.id, fb->format->format);
  4093. return -EINVAL;
  4094. }
  4095. return 0;
  4096. }
  4097. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4098. {
  4099. int i;
  4100. for (i = 0; i < crtc->num_scalers; i++)
  4101. skl_detach_scaler(crtc, i);
  4102. }
  4103. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4104. {
  4105. struct drm_device *dev = crtc->base.dev;
  4106. struct drm_i915_private *dev_priv = to_i915(dev);
  4107. int pipe = crtc->pipe;
  4108. struct intel_crtc_scaler_state *scaler_state =
  4109. &crtc->config->scaler_state;
  4110. if (crtc->config->pch_pfit.enabled) {
  4111. int id;
  4112. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4113. return;
  4114. id = scaler_state->scaler_id;
  4115. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4116. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4117. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4118. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4119. }
  4120. }
  4121. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4122. {
  4123. struct drm_device *dev = crtc->base.dev;
  4124. struct drm_i915_private *dev_priv = to_i915(dev);
  4125. int pipe = crtc->pipe;
  4126. if (crtc->config->pch_pfit.enabled) {
  4127. /* Force use of hard-coded filter coefficients
  4128. * as some pre-programmed values are broken,
  4129. * e.g. x201.
  4130. */
  4131. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4132. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4133. PF_PIPE_SEL_IVB(pipe));
  4134. else
  4135. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4136. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4137. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4138. }
  4139. }
  4140. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4141. {
  4142. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4143. struct drm_device *dev = crtc->base.dev;
  4144. struct drm_i915_private *dev_priv = to_i915(dev);
  4145. if (!crtc_state->ips_enabled)
  4146. return;
  4147. /*
  4148. * We can only enable IPS after we enable a plane and wait for a vblank
  4149. * This function is called from post_plane_update, which is run after
  4150. * a vblank wait.
  4151. */
  4152. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4153. if (IS_BROADWELL(dev_priv)) {
  4154. mutex_lock(&dev_priv->pcu_lock);
  4155. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4156. IPS_ENABLE | IPS_PCODE_CONTROL));
  4157. mutex_unlock(&dev_priv->pcu_lock);
  4158. /* Quoting Art Runyan: "its not safe to expect any particular
  4159. * value in IPS_CTL bit 31 after enabling IPS through the
  4160. * mailbox." Moreover, the mailbox may return a bogus state,
  4161. * so we need to just enable it and continue on.
  4162. */
  4163. } else {
  4164. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4165. /* The bit only becomes 1 in the next vblank, so this wait here
  4166. * is essentially intel_wait_for_vblank. If we don't have this
  4167. * and don't wait for vblanks until the end of crtc_enable, then
  4168. * the HW state readout code will complain that the expected
  4169. * IPS_CTL value is not the one we read. */
  4170. if (intel_wait_for_register(dev_priv,
  4171. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4172. 50))
  4173. DRM_ERROR("Timed out waiting for IPS enable\n");
  4174. }
  4175. }
  4176. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4177. {
  4178. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4179. struct drm_device *dev = crtc->base.dev;
  4180. struct drm_i915_private *dev_priv = to_i915(dev);
  4181. if (!crtc_state->ips_enabled)
  4182. return;
  4183. if (IS_BROADWELL(dev_priv)) {
  4184. mutex_lock(&dev_priv->pcu_lock);
  4185. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4186. mutex_unlock(&dev_priv->pcu_lock);
  4187. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4188. if (intel_wait_for_register(dev_priv,
  4189. IPS_CTL, IPS_ENABLE, 0,
  4190. 42))
  4191. DRM_ERROR("Timed out waiting for IPS disable\n");
  4192. } else {
  4193. I915_WRITE(IPS_CTL, 0);
  4194. POSTING_READ(IPS_CTL);
  4195. }
  4196. /* We need to wait for a vblank before we can disable the plane. */
  4197. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4198. }
  4199. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4200. {
  4201. if (intel_crtc->overlay) {
  4202. struct drm_device *dev = intel_crtc->base.dev;
  4203. mutex_lock(&dev->struct_mutex);
  4204. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4205. mutex_unlock(&dev->struct_mutex);
  4206. }
  4207. /* Let userspace switch the overlay on again. In most cases userspace
  4208. * has to recompute where to put it anyway.
  4209. */
  4210. }
  4211. /**
  4212. * intel_post_enable_primary - Perform operations after enabling primary plane
  4213. * @crtc: the CRTC whose primary plane was just enabled
  4214. *
  4215. * Performs potentially sleeping operations that must be done after the primary
  4216. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4217. * called due to an explicit primary plane update, or due to an implicit
  4218. * re-enable that is caused when a sprite plane is updated to no longer
  4219. * completely hide the primary plane.
  4220. */
  4221. static void
  4222. intel_post_enable_primary(struct drm_crtc *crtc,
  4223. const struct intel_crtc_state *new_crtc_state)
  4224. {
  4225. struct drm_device *dev = crtc->dev;
  4226. struct drm_i915_private *dev_priv = to_i915(dev);
  4227. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4228. int pipe = intel_crtc->pipe;
  4229. /*
  4230. * Gen2 reports pipe underruns whenever all planes are disabled.
  4231. * So don't enable underrun reporting before at least some planes
  4232. * are enabled.
  4233. * FIXME: Need to fix the logic to work when we turn off all planes
  4234. * but leave the pipe running.
  4235. */
  4236. if (IS_GEN2(dev_priv))
  4237. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4238. /* Underruns don't always raise interrupts, so check manually. */
  4239. intel_check_cpu_fifo_underruns(dev_priv);
  4240. intel_check_pch_fifo_underruns(dev_priv);
  4241. }
  4242. /* FIXME get rid of this and use pre_plane_update */
  4243. static void
  4244. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4245. {
  4246. struct drm_device *dev = crtc->dev;
  4247. struct drm_i915_private *dev_priv = to_i915(dev);
  4248. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4249. int pipe = intel_crtc->pipe;
  4250. /*
  4251. * Gen2 reports pipe underruns whenever all planes are disabled.
  4252. * So disable underrun reporting before all the planes get disabled.
  4253. */
  4254. if (IS_GEN2(dev_priv))
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4256. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4257. /*
  4258. * Vblank time updates from the shadow to live plane control register
  4259. * are blocked if the memory self-refresh mode is active at that
  4260. * moment. So to make sure the plane gets truly disabled, disable
  4261. * first the self-refresh mode. The self-refresh enable bit in turn
  4262. * will be checked/applied by the HW only at the next frame start
  4263. * event which is after the vblank start event, so we need to have a
  4264. * wait-for-vblank between disabling the plane and the pipe.
  4265. */
  4266. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4267. intel_set_memory_cxsr(dev_priv, false))
  4268. intel_wait_for_vblank(dev_priv, pipe);
  4269. }
  4270. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4271. const struct intel_crtc_state *new_crtc_state)
  4272. {
  4273. if (!old_crtc_state->ips_enabled)
  4274. return false;
  4275. if (needs_modeset(&new_crtc_state->base))
  4276. return true;
  4277. return !new_crtc_state->ips_enabled;
  4278. }
  4279. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4280. const struct intel_crtc_state *new_crtc_state)
  4281. {
  4282. if (!new_crtc_state->ips_enabled)
  4283. return false;
  4284. if (needs_modeset(&new_crtc_state->base))
  4285. return true;
  4286. /*
  4287. * We can't read out IPS on broadwell, assume the worst and
  4288. * forcibly enable IPS on the first fastset.
  4289. */
  4290. if (new_crtc_state->update_pipe &&
  4291. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4292. return true;
  4293. return !old_crtc_state->ips_enabled;
  4294. }
  4295. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4296. {
  4297. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4298. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4299. struct intel_crtc_state *pipe_config =
  4300. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4301. crtc);
  4302. struct drm_plane *primary = crtc->base.primary;
  4303. struct drm_plane_state *old_pri_state =
  4304. drm_atomic_get_existing_plane_state(old_state, primary);
  4305. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4306. if (pipe_config->update_wm_post && pipe_config->base.active)
  4307. intel_update_watermarks(crtc);
  4308. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4309. hsw_enable_ips(pipe_config);
  4310. if (old_pri_state) {
  4311. struct intel_plane_state *primary_state =
  4312. intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
  4313. to_intel_plane(primary));
  4314. struct intel_plane_state *old_primary_state =
  4315. to_intel_plane_state(old_pri_state);
  4316. intel_fbc_post_update(crtc);
  4317. if (primary_state->base.visible &&
  4318. (needs_modeset(&pipe_config->base) ||
  4319. !old_primary_state->base.visible))
  4320. intel_post_enable_primary(&crtc->base, pipe_config);
  4321. }
  4322. }
  4323. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4324. struct intel_crtc_state *pipe_config)
  4325. {
  4326. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4327. struct drm_device *dev = crtc->base.dev;
  4328. struct drm_i915_private *dev_priv = to_i915(dev);
  4329. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4330. struct drm_plane *primary = crtc->base.primary;
  4331. struct drm_plane_state *old_pri_state =
  4332. drm_atomic_get_existing_plane_state(old_state, primary);
  4333. bool modeset = needs_modeset(&pipe_config->base);
  4334. struct intel_atomic_state *old_intel_state =
  4335. to_intel_atomic_state(old_state);
  4336. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4337. hsw_disable_ips(old_crtc_state);
  4338. if (old_pri_state) {
  4339. struct intel_plane_state *primary_state =
  4340. intel_atomic_get_new_plane_state(old_intel_state,
  4341. to_intel_plane(primary));
  4342. struct intel_plane_state *old_primary_state =
  4343. to_intel_plane_state(old_pri_state);
  4344. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4345. /*
  4346. * Gen2 reports pipe underruns whenever all planes are disabled.
  4347. * So disable underrun reporting before all the planes get disabled.
  4348. */
  4349. if (IS_GEN2(dev_priv) && old_primary_state->base.visible &&
  4350. (modeset || !primary_state->base.visible))
  4351. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4352. }
  4353. /*
  4354. * Vblank time updates from the shadow to live plane control register
  4355. * are blocked if the memory self-refresh mode is active at that
  4356. * moment. So to make sure the plane gets truly disabled, disable
  4357. * first the self-refresh mode. The self-refresh enable bit in turn
  4358. * will be checked/applied by the HW only at the next frame start
  4359. * event which is after the vblank start event, so we need to have a
  4360. * wait-for-vblank between disabling the plane and the pipe.
  4361. */
  4362. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4363. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4364. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4365. /*
  4366. * IVB workaround: must disable low power watermarks for at least
  4367. * one frame before enabling scaling. LP watermarks can be re-enabled
  4368. * when scaling is disabled.
  4369. *
  4370. * WaCxSRDisabledForSpriteScaling:ivb
  4371. */
  4372. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4373. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4374. /*
  4375. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4376. * watermark programming here.
  4377. */
  4378. if (needs_modeset(&pipe_config->base))
  4379. return;
  4380. /*
  4381. * For platforms that support atomic watermarks, program the
  4382. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4383. * will be the intermediate values that are safe for both pre- and
  4384. * post- vblank; when vblank happens, the 'active' values will be set
  4385. * to the final 'target' values and we'll do this again to get the
  4386. * optimal watermarks. For gen9+ platforms, the values we program here
  4387. * will be the final target values which will get automatically latched
  4388. * at vblank time; no further programming will be necessary.
  4389. *
  4390. * If a platform hasn't been transitioned to atomic watermarks yet,
  4391. * we'll continue to update watermarks the old way, if flags tell
  4392. * us to.
  4393. */
  4394. if (dev_priv->display.initial_watermarks != NULL)
  4395. dev_priv->display.initial_watermarks(old_intel_state,
  4396. pipe_config);
  4397. else if (pipe_config->update_wm_pre)
  4398. intel_update_watermarks(crtc);
  4399. }
  4400. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4401. {
  4402. struct drm_device *dev = crtc->dev;
  4403. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4404. struct drm_plane *p;
  4405. int pipe = intel_crtc->pipe;
  4406. intel_crtc_dpms_overlay_disable(intel_crtc);
  4407. drm_for_each_plane_mask(p, dev, plane_mask)
  4408. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4409. /*
  4410. * FIXME: Once we grow proper nuclear flip support out of this we need
  4411. * to compute the mask of flip planes precisely. For the time being
  4412. * consider this a flip to a NULL plane.
  4413. */
  4414. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4415. }
  4416. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4417. struct intel_crtc_state *crtc_state,
  4418. struct drm_atomic_state *old_state)
  4419. {
  4420. struct drm_connector_state *conn_state;
  4421. struct drm_connector *conn;
  4422. int i;
  4423. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4424. struct intel_encoder *encoder =
  4425. to_intel_encoder(conn_state->best_encoder);
  4426. if (conn_state->crtc != crtc)
  4427. continue;
  4428. if (encoder->pre_pll_enable)
  4429. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4430. }
  4431. }
  4432. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4433. struct intel_crtc_state *crtc_state,
  4434. struct drm_atomic_state *old_state)
  4435. {
  4436. struct drm_connector_state *conn_state;
  4437. struct drm_connector *conn;
  4438. int i;
  4439. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4440. struct intel_encoder *encoder =
  4441. to_intel_encoder(conn_state->best_encoder);
  4442. if (conn_state->crtc != crtc)
  4443. continue;
  4444. if (encoder->pre_enable)
  4445. encoder->pre_enable(encoder, crtc_state, conn_state);
  4446. }
  4447. }
  4448. static void intel_encoders_enable(struct drm_crtc *crtc,
  4449. struct intel_crtc_state *crtc_state,
  4450. struct drm_atomic_state *old_state)
  4451. {
  4452. struct drm_connector_state *conn_state;
  4453. struct drm_connector *conn;
  4454. int i;
  4455. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4456. struct intel_encoder *encoder =
  4457. to_intel_encoder(conn_state->best_encoder);
  4458. if (conn_state->crtc != crtc)
  4459. continue;
  4460. encoder->enable(encoder, crtc_state, conn_state);
  4461. intel_opregion_notify_encoder(encoder, true);
  4462. }
  4463. }
  4464. static void intel_encoders_disable(struct drm_crtc *crtc,
  4465. struct intel_crtc_state *old_crtc_state,
  4466. struct drm_atomic_state *old_state)
  4467. {
  4468. struct drm_connector_state *old_conn_state;
  4469. struct drm_connector *conn;
  4470. int i;
  4471. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4472. struct intel_encoder *encoder =
  4473. to_intel_encoder(old_conn_state->best_encoder);
  4474. if (old_conn_state->crtc != crtc)
  4475. continue;
  4476. intel_opregion_notify_encoder(encoder, false);
  4477. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4478. }
  4479. }
  4480. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4481. struct intel_crtc_state *old_crtc_state,
  4482. struct drm_atomic_state *old_state)
  4483. {
  4484. struct drm_connector_state *old_conn_state;
  4485. struct drm_connector *conn;
  4486. int i;
  4487. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4488. struct intel_encoder *encoder =
  4489. to_intel_encoder(old_conn_state->best_encoder);
  4490. if (old_conn_state->crtc != crtc)
  4491. continue;
  4492. if (encoder->post_disable)
  4493. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4494. }
  4495. }
  4496. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4497. struct intel_crtc_state *old_crtc_state,
  4498. struct drm_atomic_state *old_state)
  4499. {
  4500. struct drm_connector_state *old_conn_state;
  4501. struct drm_connector *conn;
  4502. int i;
  4503. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4504. struct intel_encoder *encoder =
  4505. to_intel_encoder(old_conn_state->best_encoder);
  4506. if (old_conn_state->crtc != crtc)
  4507. continue;
  4508. if (encoder->post_pll_disable)
  4509. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4510. }
  4511. }
  4512. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4513. struct drm_atomic_state *old_state)
  4514. {
  4515. struct drm_crtc *crtc = pipe_config->base.crtc;
  4516. struct drm_device *dev = crtc->dev;
  4517. struct drm_i915_private *dev_priv = to_i915(dev);
  4518. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4519. int pipe = intel_crtc->pipe;
  4520. struct intel_atomic_state *old_intel_state =
  4521. to_intel_atomic_state(old_state);
  4522. if (WARN_ON(intel_crtc->active))
  4523. return;
  4524. /*
  4525. * Sometimes spurious CPU pipe underruns happen during FDI
  4526. * training, at least with VGA+HDMI cloning. Suppress them.
  4527. *
  4528. * On ILK we get an occasional spurious CPU pipe underruns
  4529. * between eDP port A enable and vdd enable. Also PCH port
  4530. * enable seems to result in the occasional CPU pipe underrun.
  4531. *
  4532. * Spurious PCH underruns also occur during PCH enabling.
  4533. */
  4534. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4535. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4536. if (intel_crtc->config->has_pch_encoder)
  4537. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4538. if (intel_crtc->config->has_pch_encoder)
  4539. intel_prepare_shared_dpll(intel_crtc);
  4540. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4541. intel_dp_set_m_n(intel_crtc, M1_N1);
  4542. intel_set_pipe_timings(intel_crtc);
  4543. intel_set_pipe_src_size(intel_crtc);
  4544. if (intel_crtc->config->has_pch_encoder) {
  4545. intel_cpu_transcoder_set_m_n(intel_crtc,
  4546. &intel_crtc->config->fdi_m_n, NULL);
  4547. }
  4548. ironlake_set_pipeconf(crtc);
  4549. intel_crtc->active = true;
  4550. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4551. if (intel_crtc->config->has_pch_encoder) {
  4552. /* Note: FDI PLL enabling _must_ be done before we enable the
  4553. * cpu pipes, hence this is separate from all the other fdi/pch
  4554. * enabling. */
  4555. ironlake_fdi_pll_enable(intel_crtc);
  4556. } else {
  4557. assert_fdi_tx_disabled(dev_priv, pipe);
  4558. assert_fdi_rx_disabled(dev_priv, pipe);
  4559. }
  4560. ironlake_pfit_enable(intel_crtc);
  4561. /*
  4562. * On ILK+ LUT must be loaded before the pipe is running but with
  4563. * clocks enabled
  4564. */
  4565. intel_color_load_luts(&pipe_config->base);
  4566. if (dev_priv->display.initial_watermarks != NULL)
  4567. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4568. intel_enable_pipe(pipe_config);
  4569. if (intel_crtc->config->has_pch_encoder)
  4570. ironlake_pch_enable(pipe_config);
  4571. assert_vblank_disabled(crtc);
  4572. drm_crtc_vblank_on(crtc);
  4573. intel_encoders_enable(crtc, pipe_config, old_state);
  4574. if (HAS_PCH_CPT(dev_priv))
  4575. cpt_verify_modeset(dev, intel_crtc->pipe);
  4576. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4577. if (intel_crtc->config->has_pch_encoder)
  4578. intel_wait_for_vblank(dev_priv, pipe);
  4579. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4580. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4581. }
  4582. /* IPS only exists on ULT machines and is tied to pipe A. */
  4583. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4584. {
  4585. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4586. }
  4587. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4588. enum pipe pipe, bool apply)
  4589. {
  4590. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4591. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4592. if (apply)
  4593. val |= mask;
  4594. else
  4595. val &= ~mask;
  4596. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4597. }
  4598. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4599. {
  4600. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4601. enum pipe pipe = crtc->pipe;
  4602. uint32_t val;
  4603. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4604. /* Program B credit equally to all pipes */
  4605. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4606. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4607. }
  4608. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4609. struct drm_atomic_state *old_state)
  4610. {
  4611. struct drm_crtc *crtc = pipe_config->base.crtc;
  4612. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4614. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4615. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4616. struct intel_atomic_state *old_intel_state =
  4617. to_intel_atomic_state(old_state);
  4618. bool psl_clkgate_wa;
  4619. if (WARN_ON(intel_crtc->active))
  4620. return;
  4621. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4622. if (intel_crtc->config->shared_dpll)
  4623. intel_enable_shared_dpll(intel_crtc);
  4624. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4625. intel_dp_set_m_n(intel_crtc, M1_N1);
  4626. if (!transcoder_is_dsi(cpu_transcoder))
  4627. intel_set_pipe_timings(intel_crtc);
  4628. intel_set_pipe_src_size(intel_crtc);
  4629. if (cpu_transcoder != TRANSCODER_EDP &&
  4630. !transcoder_is_dsi(cpu_transcoder)) {
  4631. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4632. intel_crtc->config->pixel_multiplier - 1);
  4633. }
  4634. if (intel_crtc->config->has_pch_encoder) {
  4635. intel_cpu_transcoder_set_m_n(intel_crtc,
  4636. &intel_crtc->config->fdi_m_n, NULL);
  4637. }
  4638. if (!transcoder_is_dsi(cpu_transcoder))
  4639. haswell_set_pipeconf(crtc);
  4640. haswell_set_pipemisc(crtc);
  4641. intel_color_set_csc(&pipe_config->base);
  4642. intel_crtc->active = true;
  4643. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4644. if (!transcoder_is_dsi(cpu_transcoder))
  4645. intel_ddi_enable_pipe_clock(pipe_config);
  4646. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4647. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4648. intel_crtc->config->pch_pfit.enabled;
  4649. if (psl_clkgate_wa)
  4650. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4651. if (INTEL_GEN(dev_priv) >= 9)
  4652. skylake_pfit_enable(intel_crtc);
  4653. else
  4654. ironlake_pfit_enable(intel_crtc);
  4655. /*
  4656. * On ILK+ LUT must be loaded before the pipe is running but with
  4657. * clocks enabled
  4658. */
  4659. intel_color_load_luts(&pipe_config->base);
  4660. intel_ddi_set_pipe_settings(pipe_config);
  4661. if (!transcoder_is_dsi(cpu_transcoder))
  4662. intel_ddi_enable_transcoder_func(pipe_config);
  4663. if (dev_priv->display.initial_watermarks != NULL)
  4664. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4665. if (INTEL_GEN(dev_priv) >= 11)
  4666. icl_pipe_mbus_enable(intel_crtc);
  4667. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4668. if (!transcoder_is_dsi(cpu_transcoder))
  4669. intel_enable_pipe(pipe_config);
  4670. if (intel_crtc->config->has_pch_encoder)
  4671. lpt_pch_enable(pipe_config);
  4672. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4673. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4674. assert_vblank_disabled(crtc);
  4675. drm_crtc_vblank_on(crtc);
  4676. intel_encoders_enable(crtc, pipe_config, old_state);
  4677. if (psl_clkgate_wa) {
  4678. intel_wait_for_vblank(dev_priv, pipe);
  4679. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4680. }
  4681. /* If we change the relative order between pipe/planes enabling, we need
  4682. * to change the workaround. */
  4683. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4684. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4685. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4686. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4687. }
  4688. }
  4689. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4690. {
  4691. struct drm_device *dev = crtc->base.dev;
  4692. struct drm_i915_private *dev_priv = to_i915(dev);
  4693. int pipe = crtc->pipe;
  4694. /* To avoid upsetting the power well on haswell only disable the pfit if
  4695. * it's in use. The hw state code will make sure we get this right. */
  4696. if (force || crtc->config->pch_pfit.enabled) {
  4697. I915_WRITE(PF_CTL(pipe), 0);
  4698. I915_WRITE(PF_WIN_POS(pipe), 0);
  4699. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4700. }
  4701. }
  4702. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4703. struct drm_atomic_state *old_state)
  4704. {
  4705. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4706. struct drm_device *dev = crtc->dev;
  4707. struct drm_i915_private *dev_priv = to_i915(dev);
  4708. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4709. int pipe = intel_crtc->pipe;
  4710. /*
  4711. * Sometimes spurious CPU pipe underruns happen when the
  4712. * pipe is already disabled, but FDI RX/TX is still enabled.
  4713. * Happens at least with VGA+HDMI cloning. Suppress them.
  4714. */
  4715. if (intel_crtc->config->has_pch_encoder) {
  4716. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4717. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4718. }
  4719. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4720. drm_crtc_vblank_off(crtc);
  4721. assert_vblank_disabled(crtc);
  4722. intel_disable_pipe(old_crtc_state);
  4723. ironlake_pfit_disable(intel_crtc, false);
  4724. if (intel_crtc->config->has_pch_encoder)
  4725. ironlake_fdi_disable(crtc);
  4726. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4727. if (intel_crtc->config->has_pch_encoder) {
  4728. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4729. if (HAS_PCH_CPT(dev_priv)) {
  4730. i915_reg_t reg;
  4731. u32 temp;
  4732. /* disable TRANS_DP_CTL */
  4733. reg = TRANS_DP_CTL(pipe);
  4734. temp = I915_READ(reg);
  4735. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4736. TRANS_DP_PORT_SEL_MASK);
  4737. temp |= TRANS_DP_PORT_SEL_NONE;
  4738. I915_WRITE(reg, temp);
  4739. /* disable DPLL_SEL */
  4740. temp = I915_READ(PCH_DPLL_SEL);
  4741. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4742. I915_WRITE(PCH_DPLL_SEL, temp);
  4743. }
  4744. ironlake_fdi_pll_disable(intel_crtc);
  4745. }
  4746. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4747. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4748. }
  4749. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4750. struct drm_atomic_state *old_state)
  4751. {
  4752. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4753. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4754. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4755. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4756. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4757. drm_crtc_vblank_off(crtc);
  4758. assert_vblank_disabled(crtc);
  4759. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4760. if (!transcoder_is_dsi(cpu_transcoder))
  4761. intel_disable_pipe(old_crtc_state);
  4762. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4763. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4764. if (!transcoder_is_dsi(cpu_transcoder))
  4765. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4766. if (INTEL_GEN(dev_priv) >= 9)
  4767. skylake_scaler_disable(intel_crtc);
  4768. else
  4769. ironlake_pfit_disable(intel_crtc, false);
  4770. if (!transcoder_is_dsi(cpu_transcoder))
  4771. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4772. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4773. }
  4774. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4775. {
  4776. struct drm_device *dev = crtc->base.dev;
  4777. struct drm_i915_private *dev_priv = to_i915(dev);
  4778. struct intel_crtc_state *pipe_config = crtc->config;
  4779. if (!pipe_config->gmch_pfit.control)
  4780. return;
  4781. /*
  4782. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4783. * according to register description and PRM.
  4784. */
  4785. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4786. assert_pipe_disabled(dev_priv, crtc->pipe);
  4787. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4788. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4789. /* Border color in case we don't scale up to the full screen. Black by
  4790. * default, change to something else for debugging. */
  4791. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4792. }
  4793. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4794. {
  4795. switch (port) {
  4796. case PORT_A:
  4797. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4798. case PORT_B:
  4799. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4800. case PORT_C:
  4801. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4802. case PORT_D:
  4803. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4804. case PORT_E:
  4805. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4806. case PORT_F:
  4807. return POWER_DOMAIN_PORT_DDI_F_LANES;
  4808. default:
  4809. MISSING_CASE(port);
  4810. return POWER_DOMAIN_PORT_OTHER;
  4811. }
  4812. }
  4813. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4814. struct intel_crtc_state *crtc_state)
  4815. {
  4816. struct drm_device *dev = crtc->dev;
  4817. struct drm_i915_private *dev_priv = to_i915(dev);
  4818. struct drm_encoder *encoder;
  4819. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4820. enum pipe pipe = intel_crtc->pipe;
  4821. u64 mask;
  4822. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4823. if (!crtc_state->base.active)
  4824. return 0;
  4825. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  4826. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  4827. if (crtc_state->pch_pfit.enabled ||
  4828. crtc_state->pch_pfit.force_thru)
  4829. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4830. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4831. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4832. mask |= BIT_ULL(intel_encoder->power_domain);
  4833. }
  4834. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4835. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  4836. if (crtc_state->shared_dpll)
  4837. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4838. return mask;
  4839. }
  4840. static u64
  4841. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4842. struct intel_crtc_state *crtc_state)
  4843. {
  4844. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4846. enum intel_display_power_domain domain;
  4847. u64 domains, new_domains, old_domains;
  4848. old_domains = intel_crtc->enabled_power_domains;
  4849. intel_crtc->enabled_power_domains = new_domains =
  4850. get_crtc_power_domains(crtc, crtc_state);
  4851. domains = new_domains & ~old_domains;
  4852. for_each_power_domain(domain, domains)
  4853. intel_display_power_get(dev_priv, domain);
  4854. return old_domains & ~new_domains;
  4855. }
  4856. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4857. u64 domains)
  4858. {
  4859. enum intel_display_power_domain domain;
  4860. for_each_power_domain(domain, domains)
  4861. intel_display_power_put(dev_priv, domain);
  4862. }
  4863. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4864. struct drm_atomic_state *old_state)
  4865. {
  4866. struct intel_atomic_state *old_intel_state =
  4867. to_intel_atomic_state(old_state);
  4868. struct drm_crtc *crtc = pipe_config->base.crtc;
  4869. struct drm_device *dev = crtc->dev;
  4870. struct drm_i915_private *dev_priv = to_i915(dev);
  4871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4872. int pipe = intel_crtc->pipe;
  4873. if (WARN_ON(intel_crtc->active))
  4874. return;
  4875. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4876. intel_dp_set_m_n(intel_crtc, M1_N1);
  4877. intel_set_pipe_timings(intel_crtc);
  4878. intel_set_pipe_src_size(intel_crtc);
  4879. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4880. struct drm_i915_private *dev_priv = to_i915(dev);
  4881. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4882. I915_WRITE(CHV_CANVAS(pipe), 0);
  4883. }
  4884. i9xx_set_pipeconf(intel_crtc);
  4885. intel_crtc->active = true;
  4886. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4887. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4888. if (IS_CHERRYVIEW(dev_priv)) {
  4889. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4890. chv_enable_pll(intel_crtc, intel_crtc->config);
  4891. } else {
  4892. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4893. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4894. }
  4895. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4896. i9xx_pfit_enable(intel_crtc);
  4897. intel_color_load_luts(&pipe_config->base);
  4898. dev_priv->display.initial_watermarks(old_intel_state,
  4899. pipe_config);
  4900. intel_enable_pipe(pipe_config);
  4901. assert_vblank_disabled(crtc);
  4902. drm_crtc_vblank_on(crtc);
  4903. intel_encoders_enable(crtc, pipe_config, old_state);
  4904. }
  4905. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4906. {
  4907. struct drm_device *dev = crtc->base.dev;
  4908. struct drm_i915_private *dev_priv = to_i915(dev);
  4909. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4910. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4911. }
  4912. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4913. struct drm_atomic_state *old_state)
  4914. {
  4915. struct intel_atomic_state *old_intel_state =
  4916. to_intel_atomic_state(old_state);
  4917. struct drm_crtc *crtc = pipe_config->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. enum pipe pipe = intel_crtc->pipe;
  4922. if (WARN_ON(intel_crtc->active))
  4923. return;
  4924. i9xx_set_pll_dividers(intel_crtc);
  4925. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4926. intel_dp_set_m_n(intel_crtc, M1_N1);
  4927. intel_set_pipe_timings(intel_crtc);
  4928. intel_set_pipe_src_size(intel_crtc);
  4929. i9xx_set_pipeconf(intel_crtc);
  4930. intel_crtc->active = true;
  4931. if (!IS_GEN2(dev_priv))
  4932. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4933. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4934. i9xx_enable_pll(intel_crtc, pipe_config);
  4935. i9xx_pfit_enable(intel_crtc);
  4936. intel_color_load_luts(&pipe_config->base);
  4937. if (dev_priv->display.initial_watermarks != NULL)
  4938. dev_priv->display.initial_watermarks(old_intel_state,
  4939. intel_crtc->config);
  4940. else
  4941. intel_update_watermarks(intel_crtc);
  4942. intel_enable_pipe(pipe_config);
  4943. assert_vblank_disabled(crtc);
  4944. drm_crtc_vblank_on(crtc);
  4945. intel_encoders_enable(crtc, pipe_config, old_state);
  4946. }
  4947. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4948. {
  4949. struct drm_device *dev = crtc->base.dev;
  4950. struct drm_i915_private *dev_priv = to_i915(dev);
  4951. if (!crtc->config->gmch_pfit.control)
  4952. return;
  4953. assert_pipe_disabled(dev_priv, crtc->pipe);
  4954. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4955. I915_READ(PFIT_CONTROL));
  4956. I915_WRITE(PFIT_CONTROL, 0);
  4957. }
  4958. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4959. struct drm_atomic_state *old_state)
  4960. {
  4961. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4962. struct drm_device *dev = crtc->dev;
  4963. struct drm_i915_private *dev_priv = to_i915(dev);
  4964. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4965. int pipe = intel_crtc->pipe;
  4966. /*
  4967. * On gen2 planes are double buffered but the pipe isn't, so we must
  4968. * wait for planes to fully turn off before disabling the pipe.
  4969. */
  4970. if (IS_GEN2(dev_priv))
  4971. intel_wait_for_vblank(dev_priv, pipe);
  4972. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4973. drm_crtc_vblank_off(crtc);
  4974. assert_vblank_disabled(crtc);
  4975. intel_disable_pipe(old_crtc_state);
  4976. i9xx_pfit_disable(intel_crtc);
  4977. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4978. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4979. if (IS_CHERRYVIEW(dev_priv))
  4980. chv_disable_pll(dev_priv, pipe);
  4981. else if (IS_VALLEYVIEW(dev_priv))
  4982. vlv_disable_pll(dev_priv, pipe);
  4983. else
  4984. i9xx_disable_pll(intel_crtc);
  4985. }
  4986. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4987. if (!IS_GEN2(dev_priv))
  4988. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4989. if (!dev_priv->display.initial_watermarks)
  4990. intel_update_watermarks(intel_crtc);
  4991. /* clock the pipe down to 640x480@60 to potentially save power */
  4992. if (IS_I830(dev_priv))
  4993. i830_enable_pipe(dev_priv, pipe);
  4994. }
  4995. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  4996. struct drm_modeset_acquire_ctx *ctx)
  4997. {
  4998. struct intel_encoder *encoder;
  4999. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5000. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5001. enum intel_display_power_domain domain;
  5002. struct intel_plane *plane;
  5003. u64 domains;
  5004. struct drm_atomic_state *state;
  5005. struct intel_crtc_state *crtc_state;
  5006. int ret;
  5007. if (!intel_crtc->active)
  5008. return;
  5009. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5010. const struct intel_plane_state *plane_state =
  5011. to_intel_plane_state(plane->base.state);
  5012. if (plane_state->base.visible)
  5013. intel_plane_disable_noatomic(intel_crtc, plane);
  5014. }
  5015. state = drm_atomic_state_alloc(crtc->dev);
  5016. if (!state) {
  5017. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5018. crtc->base.id, crtc->name);
  5019. return;
  5020. }
  5021. state->acquire_ctx = ctx;
  5022. /* Everything's already locked, -EDEADLK can't happen. */
  5023. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5024. ret = drm_atomic_add_affected_connectors(state, crtc);
  5025. WARN_ON(IS_ERR(crtc_state) || ret);
  5026. dev_priv->display.crtc_disable(crtc_state, state);
  5027. drm_atomic_state_put(state);
  5028. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5029. crtc->base.id, crtc->name);
  5030. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5031. crtc->state->active = false;
  5032. intel_crtc->active = false;
  5033. crtc->enabled = false;
  5034. crtc->state->connector_mask = 0;
  5035. crtc->state->encoder_mask = 0;
  5036. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5037. encoder->base.crtc = NULL;
  5038. intel_fbc_disable(intel_crtc);
  5039. intel_update_watermarks(intel_crtc);
  5040. intel_disable_shared_dpll(intel_crtc);
  5041. domains = intel_crtc->enabled_power_domains;
  5042. for_each_power_domain(domain, domains)
  5043. intel_display_power_put(dev_priv, domain);
  5044. intel_crtc->enabled_power_domains = 0;
  5045. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5046. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5047. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5048. }
  5049. /*
  5050. * turn all crtc's off, but do not adjust state
  5051. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5052. */
  5053. int intel_display_suspend(struct drm_device *dev)
  5054. {
  5055. struct drm_i915_private *dev_priv = to_i915(dev);
  5056. struct drm_atomic_state *state;
  5057. int ret;
  5058. state = drm_atomic_helper_suspend(dev);
  5059. ret = PTR_ERR_OR_ZERO(state);
  5060. if (ret)
  5061. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5062. else
  5063. dev_priv->modeset_restore_state = state;
  5064. return ret;
  5065. }
  5066. void intel_encoder_destroy(struct drm_encoder *encoder)
  5067. {
  5068. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5069. drm_encoder_cleanup(encoder);
  5070. kfree(intel_encoder);
  5071. }
  5072. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5073. * internal consistency). */
  5074. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5075. struct drm_connector_state *conn_state)
  5076. {
  5077. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5078. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5079. connector->base.base.id,
  5080. connector->base.name);
  5081. if (connector->get_hw_state(connector)) {
  5082. struct intel_encoder *encoder = connector->encoder;
  5083. I915_STATE_WARN(!crtc_state,
  5084. "connector enabled without attached crtc\n");
  5085. if (!crtc_state)
  5086. return;
  5087. I915_STATE_WARN(!crtc_state->active,
  5088. "connector is active, but attached crtc isn't\n");
  5089. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5090. return;
  5091. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5092. "atomic encoder doesn't match attached encoder\n");
  5093. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5094. "attached encoder crtc differs from connector crtc\n");
  5095. } else {
  5096. I915_STATE_WARN(crtc_state && crtc_state->active,
  5097. "attached crtc is active, but connector isn't\n");
  5098. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5099. "best encoder set without crtc!\n");
  5100. }
  5101. }
  5102. int intel_connector_init(struct intel_connector *connector)
  5103. {
  5104. struct intel_digital_connector_state *conn_state;
  5105. /*
  5106. * Allocate enough memory to hold intel_digital_connector_state,
  5107. * This might be a few bytes too many, but for connectors that don't
  5108. * need it we'll free the state and allocate a smaller one on the first
  5109. * succesful commit anyway.
  5110. */
  5111. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5112. if (!conn_state)
  5113. return -ENOMEM;
  5114. __drm_atomic_helper_connector_reset(&connector->base,
  5115. &conn_state->base);
  5116. return 0;
  5117. }
  5118. struct intel_connector *intel_connector_alloc(void)
  5119. {
  5120. struct intel_connector *connector;
  5121. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5122. if (!connector)
  5123. return NULL;
  5124. if (intel_connector_init(connector) < 0) {
  5125. kfree(connector);
  5126. return NULL;
  5127. }
  5128. return connector;
  5129. }
  5130. /*
  5131. * Free the bits allocated by intel_connector_alloc.
  5132. * This should only be used after intel_connector_alloc has returned
  5133. * successfully, and before drm_connector_init returns successfully.
  5134. * Otherwise the destroy callbacks for the connector and the state should
  5135. * take care of proper cleanup/free
  5136. */
  5137. void intel_connector_free(struct intel_connector *connector)
  5138. {
  5139. kfree(to_intel_digital_connector_state(connector->base.state));
  5140. kfree(connector);
  5141. }
  5142. /* Simple connector->get_hw_state implementation for encoders that support only
  5143. * one connector and no cloning and hence the encoder state determines the state
  5144. * of the connector. */
  5145. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5146. {
  5147. enum pipe pipe = 0;
  5148. struct intel_encoder *encoder = connector->encoder;
  5149. return encoder->get_hw_state(encoder, &pipe);
  5150. }
  5151. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5152. {
  5153. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5154. return crtc_state->fdi_lanes;
  5155. return 0;
  5156. }
  5157. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5158. struct intel_crtc_state *pipe_config)
  5159. {
  5160. struct drm_i915_private *dev_priv = to_i915(dev);
  5161. struct drm_atomic_state *state = pipe_config->base.state;
  5162. struct intel_crtc *other_crtc;
  5163. struct intel_crtc_state *other_crtc_state;
  5164. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5165. pipe_name(pipe), pipe_config->fdi_lanes);
  5166. if (pipe_config->fdi_lanes > 4) {
  5167. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5168. pipe_name(pipe), pipe_config->fdi_lanes);
  5169. return -EINVAL;
  5170. }
  5171. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5172. if (pipe_config->fdi_lanes > 2) {
  5173. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5174. pipe_config->fdi_lanes);
  5175. return -EINVAL;
  5176. } else {
  5177. return 0;
  5178. }
  5179. }
  5180. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5181. return 0;
  5182. /* Ivybridge 3 pipe is really complicated */
  5183. switch (pipe) {
  5184. case PIPE_A:
  5185. return 0;
  5186. case PIPE_B:
  5187. if (pipe_config->fdi_lanes <= 2)
  5188. return 0;
  5189. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5190. other_crtc_state =
  5191. intel_atomic_get_crtc_state(state, other_crtc);
  5192. if (IS_ERR(other_crtc_state))
  5193. return PTR_ERR(other_crtc_state);
  5194. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5195. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5196. pipe_name(pipe), pipe_config->fdi_lanes);
  5197. return -EINVAL;
  5198. }
  5199. return 0;
  5200. case PIPE_C:
  5201. if (pipe_config->fdi_lanes > 2) {
  5202. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5203. pipe_name(pipe), pipe_config->fdi_lanes);
  5204. return -EINVAL;
  5205. }
  5206. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5207. other_crtc_state =
  5208. intel_atomic_get_crtc_state(state, other_crtc);
  5209. if (IS_ERR(other_crtc_state))
  5210. return PTR_ERR(other_crtc_state);
  5211. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5212. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5213. return -EINVAL;
  5214. }
  5215. return 0;
  5216. default:
  5217. BUG();
  5218. }
  5219. }
  5220. #define RETRY 1
  5221. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5222. struct intel_crtc_state *pipe_config)
  5223. {
  5224. struct drm_device *dev = intel_crtc->base.dev;
  5225. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5226. int lane, link_bw, fdi_dotclock, ret;
  5227. bool needs_recompute = false;
  5228. retry:
  5229. /* FDI is a binary signal running at ~2.7GHz, encoding
  5230. * each output octet as 10 bits. The actual frequency
  5231. * is stored as a divider into a 100MHz clock, and the
  5232. * mode pixel clock is stored in units of 1KHz.
  5233. * Hence the bw of each lane in terms of the mode signal
  5234. * is:
  5235. */
  5236. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5237. fdi_dotclock = adjusted_mode->crtc_clock;
  5238. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5239. pipe_config->pipe_bpp);
  5240. pipe_config->fdi_lanes = lane;
  5241. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5242. link_bw, &pipe_config->fdi_m_n, false);
  5243. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5244. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5245. pipe_config->pipe_bpp -= 2*3;
  5246. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5247. pipe_config->pipe_bpp);
  5248. needs_recompute = true;
  5249. pipe_config->bw_constrained = true;
  5250. goto retry;
  5251. }
  5252. if (needs_recompute)
  5253. return RETRY;
  5254. return ret;
  5255. }
  5256. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5257. {
  5258. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5259. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5260. /* IPS only exists on ULT machines and is tied to pipe A. */
  5261. if (!hsw_crtc_supports_ips(crtc))
  5262. return false;
  5263. if (!i915_modparams.enable_ips)
  5264. return false;
  5265. if (crtc_state->pipe_bpp > 24)
  5266. return false;
  5267. /*
  5268. * We compare against max which means we must take
  5269. * the increased cdclk requirement into account when
  5270. * calculating the new cdclk.
  5271. *
  5272. * Should measure whether using a lower cdclk w/o IPS
  5273. */
  5274. if (IS_BROADWELL(dev_priv) &&
  5275. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5276. return false;
  5277. return true;
  5278. }
  5279. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5280. {
  5281. struct drm_i915_private *dev_priv =
  5282. to_i915(crtc_state->base.crtc->dev);
  5283. struct intel_atomic_state *intel_state =
  5284. to_intel_atomic_state(crtc_state->base.state);
  5285. if (!hsw_crtc_state_ips_capable(crtc_state))
  5286. return false;
  5287. if (crtc_state->ips_force_disable)
  5288. return false;
  5289. /* IPS should be fine as long as at least one plane is enabled. */
  5290. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5291. return false;
  5292. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5293. if (IS_BROADWELL(dev_priv) &&
  5294. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5295. return false;
  5296. return true;
  5297. }
  5298. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5299. {
  5300. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5301. /* GDG double wide on either pipe, otherwise pipe A only */
  5302. return INTEL_GEN(dev_priv) < 4 &&
  5303. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5304. }
  5305. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5306. {
  5307. uint32_t pixel_rate;
  5308. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5309. /*
  5310. * We only use IF-ID interlacing. If we ever use
  5311. * PF-ID we'll need to adjust the pixel_rate here.
  5312. */
  5313. if (pipe_config->pch_pfit.enabled) {
  5314. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5315. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5316. pipe_w = pipe_config->pipe_src_w;
  5317. pipe_h = pipe_config->pipe_src_h;
  5318. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5319. pfit_h = pfit_size & 0xFFFF;
  5320. if (pipe_w < pfit_w)
  5321. pipe_w = pfit_w;
  5322. if (pipe_h < pfit_h)
  5323. pipe_h = pfit_h;
  5324. if (WARN_ON(!pfit_w || !pfit_h))
  5325. return pixel_rate;
  5326. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5327. pfit_w * pfit_h);
  5328. }
  5329. return pixel_rate;
  5330. }
  5331. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5332. {
  5333. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5334. if (HAS_GMCH_DISPLAY(dev_priv))
  5335. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5336. crtc_state->pixel_rate =
  5337. crtc_state->base.adjusted_mode.crtc_clock;
  5338. else
  5339. crtc_state->pixel_rate =
  5340. ilk_pipe_pixel_rate(crtc_state);
  5341. }
  5342. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5343. struct intel_crtc_state *pipe_config)
  5344. {
  5345. struct drm_device *dev = crtc->base.dev;
  5346. struct drm_i915_private *dev_priv = to_i915(dev);
  5347. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5348. int clock_limit = dev_priv->max_dotclk_freq;
  5349. if (INTEL_GEN(dev_priv) < 4) {
  5350. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5351. /*
  5352. * Enable double wide mode when the dot clock
  5353. * is > 90% of the (display) core speed.
  5354. */
  5355. if (intel_crtc_supports_double_wide(crtc) &&
  5356. adjusted_mode->crtc_clock > clock_limit) {
  5357. clock_limit = dev_priv->max_dotclk_freq;
  5358. pipe_config->double_wide = true;
  5359. }
  5360. }
  5361. if (adjusted_mode->crtc_clock > clock_limit) {
  5362. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5363. adjusted_mode->crtc_clock, clock_limit,
  5364. yesno(pipe_config->double_wide));
  5365. return -EINVAL;
  5366. }
  5367. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5368. /*
  5369. * There is only one pipe CSC unit per pipe, and we need that
  5370. * for output conversion from RGB->YCBCR. So if CTM is already
  5371. * applied we can't support YCBCR420 output.
  5372. */
  5373. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5374. return -EINVAL;
  5375. }
  5376. /*
  5377. * Pipe horizontal size must be even in:
  5378. * - DVO ganged mode
  5379. * - LVDS dual channel mode
  5380. * - Double wide pipe
  5381. */
  5382. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5383. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5384. pipe_config->pipe_src_w &= ~1;
  5385. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5386. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5387. */
  5388. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5389. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5390. return -EINVAL;
  5391. intel_crtc_compute_pixel_rate(pipe_config);
  5392. if (pipe_config->has_pch_encoder)
  5393. return ironlake_fdi_compute_config(crtc, pipe_config);
  5394. return 0;
  5395. }
  5396. static void
  5397. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5398. {
  5399. while (*num > DATA_LINK_M_N_MASK ||
  5400. *den > DATA_LINK_M_N_MASK) {
  5401. *num >>= 1;
  5402. *den >>= 1;
  5403. }
  5404. }
  5405. static void compute_m_n(unsigned int m, unsigned int n,
  5406. uint32_t *ret_m, uint32_t *ret_n,
  5407. bool reduce_m_n)
  5408. {
  5409. /*
  5410. * Reduce M/N as much as possible without loss in precision. Several DP
  5411. * dongles in particular seem to be fussy about too large *link* M/N
  5412. * values. The passed in values are more likely to have the least
  5413. * significant bits zero than M after rounding below, so do this first.
  5414. */
  5415. if (reduce_m_n) {
  5416. while ((m & 1) == 0 && (n & 1) == 0) {
  5417. m >>= 1;
  5418. n >>= 1;
  5419. }
  5420. }
  5421. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5422. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5423. intel_reduce_m_n_ratio(ret_m, ret_n);
  5424. }
  5425. void
  5426. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5427. int pixel_clock, int link_clock,
  5428. struct intel_link_m_n *m_n,
  5429. bool reduce_m_n)
  5430. {
  5431. m_n->tu = 64;
  5432. compute_m_n(bits_per_pixel * pixel_clock,
  5433. link_clock * nlanes * 8,
  5434. &m_n->gmch_m, &m_n->gmch_n,
  5435. reduce_m_n);
  5436. compute_m_n(pixel_clock, link_clock,
  5437. &m_n->link_m, &m_n->link_n,
  5438. reduce_m_n);
  5439. }
  5440. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5441. {
  5442. if (i915_modparams.panel_use_ssc >= 0)
  5443. return i915_modparams.panel_use_ssc != 0;
  5444. return dev_priv->vbt.lvds_use_ssc
  5445. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5446. }
  5447. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5448. {
  5449. return (1 << dpll->n) << 16 | dpll->m2;
  5450. }
  5451. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5452. {
  5453. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5454. }
  5455. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5456. struct intel_crtc_state *crtc_state,
  5457. struct dpll *reduced_clock)
  5458. {
  5459. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5460. u32 fp, fp2 = 0;
  5461. if (IS_PINEVIEW(dev_priv)) {
  5462. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5463. if (reduced_clock)
  5464. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5465. } else {
  5466. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5467. if (reduced_clock)
  5468. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5469. }
  5470. crtc_state->dpll_hw_state.fp0 = fp;
  5471. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5472. reduced_clock) {
  5473. crtc_state->dpll_hw_state.fp1 = fp2;
  5474. } else {
  5475. crtc_state->dpll_hw_state.fp1 = fp;
  5476. }
  5477. }
  5478. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5479. pipe)
  5480. {
  5481. u32 reg_val;
  5482. /*
  5483. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5484. * and set it to a reasonable value instead.
  5485. */
  5486. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5487. reg_val &= 0xffffff00;
  5488. reg_val |= 0x00000030;
  5489. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5490. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5491. reg_val &= 0x00ffffff;
  5492. reg_val |= 0x8c000000;
  5493. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5494. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5495. reg_val &= 0xffffff00;
  5496. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5497. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5498. reg_val &= 0x00ffffff;
  5499. reg_val |= 0xb0000000;
  5500. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5501. }
  5502. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5503. struct intel_link_m_n *m_n)
  5504. {
  5505. struct drm_device *dev = crtc->base.dev;
  5506. struct drm_i915_private *dev_priv = to_i915(dev);
  5507. int pipe = crtc->pipe;
  5508. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5509. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5510. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5511. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5512. }
  5513. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5514. struct intel_link_m_n *m_n,
  5515. struct intel_link_m_n *m2_n2)
  5516. {
  5517. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5518. int pipe = crtc->pipe;
  5519. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5520. if (INTEL_GEN(dev_priv) >= 5) {
  5521. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5522. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5523. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5524. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5525. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5526. * for gen < 8) and if DRRS is supported (to make sure the
  5527. * registers are not unnecessarily accessed).
  5528. */
  5529. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5530. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5531. I915_WRITE(PIPE_DATA_M2(transcoder),
  5532. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5533. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5534. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5535. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5536. }
  5537. } else {
  5538. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5539. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5540. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5541. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5542. }
  5543. }
  5544. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5545. {
  5546. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5547. if (m_n == M1_N1) {
  5548. dp_m_n = &crtc->config->dp_m_n;
  5549. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5550. } else if (m_n == M2_N2) {
  5551. /*
  5552. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5553. * needs to be programmed into M1_N1.
  5554. */
  5555. dp_m_n = &crtc->config->dp_m2_n2;
  5556. } else {
  5557. DRM_ERROR("Unsupported divider value\n");
  5558. return;
  5559. }
  5560. if (crtc->config->has_pch_encoder)
  5561. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5562. else
  5563. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5564. }
  5565. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5566. struct intel_crtc_state *pipe_config)
  5567. {
  5568. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5569. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5570. if (crtc->pipe != PIPE_A)
  5571. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5572. /* DPLL not used with DSI, but still need the rest set up */
  5573. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5574. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5575. DPLL_EXT_BUFFER_ENABLE_VLV;
  5576. pipe_config->dpll_hw_state.dpll_md =
  5577. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5578. }
  5579. static void chv_compute_dpll(struct intel_crtc *crtc,
  5580. struct intel_crtc_state *pipe_config)
  5581. {
  5582. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5583. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5584. if (crtc->pipe != PIPE_A)
  5585. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5586. /* DPLL not used with DSI, but still need the rest set up */
  5587. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5588. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5589. pipe_config->dpll_hw_state.dpll_md =
  5590. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5591. }
  5592. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5593. const struct intel_crtc_state *pipe_config)
  5594. {
  5595. struct drm_device *dev = crtc->base.dev;
  5596. struct drm_i915_private *dev_priv = to_i915(dev);
  5597. enum pipe pipe = crtc->pipe;
  5598. u32 mdiv;
  5599. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5600. u32 coreclk, reg_val;
  5601. /* Enable Refclk */
  5602. I915_WRITE(DPLL(pipe),
  5603. pipe_config->dpll_hw_state.dpll &
  5604. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5605. /* No need to actually set up the DPLL with DSI */
  5606. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5607. return;
  5608. mutex_lock(&dev_priv->sb_lock);
  5609. bestn = pipe_config->dpll.n;
  5610. bestm1 = pipe_config->dpll.m1;
  5611. bestm2 = pipe_config->dpll.m2;
  5612. bestp1 = pipe_config->dpll.p1;
  5613. bestp2 = pipe_config->dpll.p2;
  5614. /* See eDP HDMI DPIO driver vbios notes doc */
  5615. /* PLL B needs special handling */
  5616. if (pipe == PIPE_B)
  5617. vlv_pllb_recal_opamp(dev_priv, pipe);
  5618. /* Set up Tx target for periodic Rcomp update */
  5619. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5620. /* Disable target IRef on PLL */
  5621. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5622. reg_val &= 0x00ffffff;
  5623. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5624. /* Disable fast lock */
  5625. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5626. /* Set idtafcrecal before PLL is enabled */
  5627. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5628. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5629. mdiv |= ((bestn << DPIO_N_SHIFT));
  5630. mdiv |= (1 << DPIO_K_SHIFT);
  5631. /*
  5632. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5633. * but we don't support that).
  5634. * Note: don't use the DAC post divider as it seems unstable.
  5635. */
  5636. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5637. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5638. mdiv |= DPIO_ENABLE_CALIBRATION;
  5639. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5640. /* Set HBR and RBR LPF coefficients */
  5641. if (pipe_config->port_clock == 162000 ||
  5642. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5643. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5644. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5645. 0x009f0003);
  5646. else
  5647. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5648. 0x00d0000f);
  5649. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5650. /* Use SSC source */
  5651. if (pipe == PIPE_A)
  5652. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5653. 0x0df40000);
  5654. else
  5655. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5656. 0x0df70000);
  5657. } else { /* HDMI or VGA */
  5658. /* Use bend source */
  5659. if (pipe == PIPE_A)
  5660. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5661. 0x0df70000);
  5662. else
  5663. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5664. 0x0df40000);
  5665. }
  5666. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5667. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5668. if (intel_crtc_has_dp_encoder(crtc->config))
  5669. coreclk |= 0x01000000;
  5670. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5671. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5672. mutex_unlock(&dev_priv->sb_lock);
  5673. }
  5674. static void chv_prepare_pll(struct intel_crtc *crtc,
  5675. const struct intel_crtc_state *pipe_config)
  5676. {
  5677. struct drm_device *dev = crtc->base.dev;
  5678. struct drm_i915_private *dev_priv = to_i915(dev);
  5679. enum pipe pipe = crtc->pipe;
  5680. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5681. u32 loopfilter, tribuf_calcntr;
  5682. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5683. u32 dpio_val;
  5684. int vco;
  5685. /* Enable Refclk and SSC */
  5686. I915_WRITE(DPLL(pipe),
  5687. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5688. /* No need to actually set up the DPLL with DSI */
  5689. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5690. return;
  5691. bestn = pipe_config->dpll.n;
  5692. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5693. bestm1 = pipe_config->dpll.m1;
  5694. bestm2 = pipe_config->dpll.m2 >> 22;
  5695. bestp1 = pipe_config->dpll.p1;
  5696. bestp2 = pipe_config->dpll.p2;
  5697. vco = pipe_config->dpll.vco;
  5698. dpio_val = 0;
  5699. loopfilter = 0;
  5700. mutex_lock(&dev_priv->sb_lock);
  5701. /* p1 and p2 divider */
  5702. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5703. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5704. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5705. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5706. 1 << DPIO_CHV_K_DIV_SHIFT);
  5707. /* Feedback post-divider - m2 */
  5708. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5709. /* Feedback refclk divider - n and m1 */
  5710. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5711. DPIO_CHV_M1_DIV_BY_2 |
  5712. 1 << DPIO_CHV_N_DIV_SHIFT);
  5713. /* M2 fraction division */
  5714. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5715. /* M2 fraction division enable */
  5716. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5717. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5718. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5719. if (bestm2_frac)
  5720. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5721. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5722. /* Program digital lock detect threshold */
  5723. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5724. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5725. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5726. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5727. if (!bestm2_frac)
  5728. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5729. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5730. /* Loop filter */
  5731. if (vco == 5400000) {
  5732. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5733. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5734. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5735. tribuf_calcntr = 0x9;
  5736. } else if (vco <= 6200000) {
  5737. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5738. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5739. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5740. tribuf_calcntr = 0x9;
  5741. } else if (vco <= 6480000) {
  5742. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5743. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5744. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5745. tribuf_calcntr = 0x8;
  5746. } else {
  5747. /* Not supported. Apply the same limits as in the max case */
  5748. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5749. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5750. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5751. tribuf_calcntr = 0;
  5752. }
  5753. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5754. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5755. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5756. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5757. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5758. /* AFC Recal */
  5759. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5760. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5761. DPIO_AFC_RECAL);
  5762. mutex_unlock(&dev_priv->sb_lock);
  5763. }
  5764. /**
  5765. * vlv_force_pll_on - forcibly enable just the PLL
  5766. * @dev_priv: i915 private structure
  5767. * @pipe: pipe PLL to enable
  5768. * @dpll: PLL configuration
  5769. *
  5770. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5771. * in cases where we need the PLL enabled even when @pipe is not going to
  5772. * be enabled.
  5773. */
  5774. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5775. const struct dpll *dpll)
  5776. {
  5777. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5778. struct intel_crtc_state *pipe_config;
  5779. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5780. if (!pipe_config)
  5781. return -ENOMEM;
  5782. pipe_config->base.crtc = &crtc->base;
  5783. pipe_config->pixel_multiplier = 1;
  5784. pipe_config->dpll = *dpll;
  5785. if (IS_CHERRYVIEW(dev_priv)) {
  5786. chv_compute_dpll(crtc, pipe_config);
  5787. chv_prepare_pll(crtc, pipe_config);
  5788. chv_enable_pll(crtc, pipe_config);
  5789. } else {
  5790. vlv_compute_dpll(crtc, pipe_config);
  5791. vlv_prepare_pll(crtc, pipe_config);
  5792. vlv_enable_pll(crtc, pipe_config);
  5793. }
  5794. kfree(pipe_config);
  5795. return 0;
  5796. }
  5797. /**
  5798. * vlv_force_pll_off - forcibly disable just the PLL
  5799. * @dev_priv: i915 private structure
  5800. * @pipe: pipe PLL to disable
  5801. *
  5802. * Disable the PLL for @pipe. To be used in cases where we need
  5803. * the PLL enabled even when @pipe is not going to be enabled.
  5804. */
  5805. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5806. {
  5807. if (IS_CHERRYVIEW(dev_priv))
  5808. chv_disable_pll(dev_priv, pipe);
  5809. else
  5810. vlv_disable_pll(dev_priv, pipe);
  5811. }
  5812. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5813. struct intel_crtc_state *crtc_state,
  5814. struct dpll *reduced_clock)
  5815. {
  5816. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5817. u32 dpll;
  5818. struct dpll *clock = &crtc_state->dpll;
  5819. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5820. dpll = DPLL_VGA_MODE_DIS;
  5821. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5822. dpll |= DPLLB_MODE_LVDS;
  5823. else
  5824. dpll |= DPLLB_MODE_DAC_SERIAL;
  5825. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5826. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5827. dpll |= (crtc_state->pixel_multiplier - 1)
  5828. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5829. }
  5830. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5831. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5832. dpll |= DPLL_SDVO_HIGH_SPEED;
  5833. if (intel_crtc_has_dp_encoder(crtc_state))
  5834. dpll |= DPLL_SDVO_HIGH_SPEED;
  5835. /* compute bitmask from p1 value */
  5836. if (IS_PINEVIEW(dev_priv))
  5837. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5838. else {
  5839. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5840. if (IS_G4X(dev_priv) && reduced_clock)
  5841. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5842. }
  5843. switch (clock->p2) {
  5844. case 5:
  5845. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5846. break;
  5847. case 7:
  5848. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5849. break;
  5850. case 10:
  5851. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5852. break;
  5853. case 14:
  5854. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5855. break;
  5856. }
  5857. if (INTEL_GEN(dev_priv) >= 4)
  5858. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5859. if (crtc_state->sdvo_tv_clock)
  5860. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5861. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5862. intel_panel_use_ssc(dev_priv))
  5863. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5864. else
  5865. dpll |= PLL_REF_INPUT_DREFCLK;
  5866. dpll |= DPLL_VCO_ENABLE;
  5867. crtc_state->dpll_hw_state.dpll = dpll;
  5868. if (INTEL_GEN(dev_priv) >= 4) {
  5869. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5870. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5871. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5872. }
  5873. }
  5874. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5875. struct intel_crtc_state *crtc_state,
  5876. struct dpll *reduced_clock)
  5877. {
  5878. struct drm_device *dev = crtc->base.dev;
  5879. struct drm_i915_private *dev_priv = to_i915(dev);
  5880. u32 dpll;
  5881. struct dpll *clock = &crtc_state->dpll;
  5882. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5883. dpll = DPLL_VGA_MODE_DIS;
  5884. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5885. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5886. } else {
  5887. if (clock->p1 == 2)
  5888. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5889. else
  5890. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5891. if (clock->p2 == 4)
  5892. dpll |= PLL_P2_DIVIDE_BY_4;
  5893. }
  5894. if (!IS_I830(dev_priv) &&
  5895. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5896. dpll |= DPLL_DVO_2X_MODE;
  5897. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5898. intel_panel_use_ssc(dev_priv))
  5899. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5900. else
  5901. dpll |= PLL_REF_INPUT_DREFCLK;
  5902. dpll |= DPLL_VCO_ENABLE;
  5903. crtc_state->dpll_hw_state.dpll = dpll;
  5904. }
  5905. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5906. {
  5907. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5908. enum pipe pipe = intel_crtc->pipe;
  5909. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5910. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5911. uint32_t crtc_vtotal, crtc_vblank_end;
  5912. int vsyncshift = 0;
  5913. /* We need to be careful not to changed the adjusted mode, for otherwise
  5914. * the hw state checker will get angry at the mismatch. */
  5915. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5916. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5917. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5918. /* the chip adds 2 halflines automatically */
  5919. crtc_vtotal -= 1;
  5920. crtc_vblank_end -= 1;
  5921. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5922. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5923. else
  5924. vsyncshift = adjusted_mode->crtc_hsync_start -
  5925. adjusted_mode->crtc_htotal / 2;
  5926. if (vsyncshift < 0)
  5927. vsyncshift += adjusted_mode->crtc_htotal;
  5928. }
  5929. if (INTEL_GEN(dev_priv) > 3)
  5930. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5931. I915_WRITE(HTOTAL(cpu_transcoder),
  5932. (adjusted_mode->crtc_hdisplay - 1) |
  5933. ((adjusted_mode->crtc_htotal - 1) << 16));
  5934. I915_WRITE(HBLANK(cpu_transcoder),
  5935. (adjusted_mode->crtc_hblank_start - 1) |
  5936. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5937. I915_WRITE(HSYNC(cpu_transcoder),
  5938. (adjusted_mode->crtc_hsync_start - 1) |
  5939. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5940. I915_WRITE(VTOTAL(cpu_transcoder),
  5941. (adjusted_mode->crtc_vdisplay - 1) |
  5942. ((crtc_vtotal - 1) << 16));
  5943. I915_WRITE(VBLANK(cpu_transcoder),
  5944. (adjusted_mode->crtc_vblank_start - 1) |
  5945. ((crtc_vblank_end - 1) << 16));
  5946. I915_WRITE(VSYNC(cpu_transcoder),
  5947. (adjusted_mode->crtc_vsync_start - 1) |
  5948. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5949. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5950. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5951. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5952. * bits. */
  5953. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5954. (pipe == PIPE_B || pipe == PIPE_C))
  5955. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5956. }
  5957. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5958. {
  5959. struct drm_device *dev = intel_crtc->base.dev;
  5960. struct drm_i915_private *dev_priv = to_i915(dev);
  5961. enum pipe pipe = intel_crtc->pipe;
  5962. /* pipesrc controls the size that is scaled from, which should
  5963. * always be the user's requested size.
  5964. */
  5965. I915_WRITE(PIPESRC(pipe),
  5966. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5967. (intel_crtc->config->pipe_src_h - 1));
  5968. }
  5969. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5970. struct intel_crtc_state *pipe_config)
  5971. {
  5972. struct drm_device *dev = crtc->base.dev;
  5973. struct drm_i915_private *dev_priv = to_i915(dev);
  5974. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5975. uint32_t tmp;
  5976. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5977. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5978. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5979. tmp = I915_READ(HBLANK(cpu_transcoder));
  5980. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5981. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5982. tmp = I915_READ(HSYNC(cpu_transcoder));
  5983. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5984. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5985. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5986. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5987. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5988. tmp = I915_READ(VBLANK(cpu_transcoder));
  5989. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5990. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5991. tmp = I915_READ(VSYNC(cpu_transcoder));
  5992. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5993. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5994. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5995. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5996. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5997. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5998. }
  5999. }
  6000. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6001. struct intel_crtc_state *pipe_config)
  6002. {
  6003. struct drm_device *dev = crtc->base.dev;
  6004. struct drm_i915_private *dev_priv = to_i915(dev);
  6005. u32 tmp;
  6006. tmp = I915_READ(PIPESRC(crtc->pipe));
  6007. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6008. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6009. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6010. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6011. }
  6012. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6013. struct intel_crtc_state *pipe_config)
  6014. {
  6015. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6016. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6017. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6018. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6019. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6020. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6021. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6022. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6023. mode->flags = pipe_config->base.adjusted_mode.flags;
  6024. mode->type = DRM_MODE_TYPE_DRIVER;
  6025. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6026. mode->hsync = drm_mode_hsync(mode);
  6027. mode->vrefresh = drm_mode_vrefresh(mode);
  6028. drm_mode_set_name(mode);
  6029. }
  6030. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6031. {
  6032. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6033. uint32_t pipeconf;
  6034. pipeconf = 0;
  6035. /* we keep both pipes enabled on 830 */
  6036. if (IS_I830(dev_priv))
  6037. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6038. if (intel_crtc->config->double_wide)
  6039. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6040. /* only g4x and later have fancy bpc/dither controls */
  6041. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6042. IS_CHERRYVIEW(dev_priv)) {
  6043. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6044. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6045. pipeconf |= PIPECONF_DITHER_EN |
  6046. PIPECONF_DITHER_TYPE_SP;
  6047. switch (intel_crtc->config->pipe_bpp) {
  6048. case 18:
  6049. pipeconf |= PIPECONF_6BPC;
  6050. break;
  6051. case 24:
  6052. pipeconf |= PIPECONF_8BPC;
  6053. break;
  6054. case 30:
  6055. pipeconf |= PIPECONF_10BPC;
  6056. break;
  6057. default:
  6058. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6059. BUG();
  6060. }
  6061. }
  6062. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6063. if (INTEL_GEN(dev_priv) < 4 ||
  6064. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6065. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6066. else
  6067. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6068. } else
  6069. pipeconf |= PIPECONF_PROGRESSIVE;
  6070. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6071. intel_crtc->config->limited_color_range)
  6072. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6073. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6074. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6075. }
  6076. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6077. struct intel_crtc_state *crtc_state)
  6078. {
  6079. struct drm_device *dev = crtc->base.dev;
  6080. struct drm_i915_private *dev_priv = to_i915(dev);
  6081. const struct intel_limit *limit;
  6082. int refclk = 48000;
  6083. memset(&crtc_state->dpll_hw_state, 0,
  6084. sizeof(crtc_state->dpll_hw_state));
  6085. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6086. if (intel_panel_use_ssc(dev_priv)) {
  6087. refclk = dev_priv->vbt.lvds_ssc_freq;
  6088. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6089. }
  6090. limit = &intel_limits_i8xx_lvds;
  6091. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6092. limit = &intel_limits_i8xx_dvo;
  6093. } else {
  6094. limit = &intel_limits_i8xx_dac;
  6095. }
  6096. if (!crtc_state->clock_set &&
  6097. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6098. refclk, NULL, &crtc_state->dpll)) {
  6099. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6100. return -EINVAL;
  6101. }
  6102. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6103. return 0;
  6104. }
  6105. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6106. struct intel_crtc_state *crtc_state)
  6107. {
  6108. struct drm_device *dev = crtc->base.dev;
  6109. struct drm_i915_private *dev_priv = to_i915(dev);
  6110. const struct intel_limit *limit;
  6111. int refclk = 96000;
  6112. memset(&crtc_state->dpll_hw_state, 0,
  6113. sizeof(crtc_state->dpll_hw_state));
  6114. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6115. if (intel_panel_use_ssc(dev_priv)) {
  6116. refclk = dev_priv->vbt.lvds_ssc_freq;
  6117. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6118. }
  6119. if (intel_is_dual_link_lvds(dev))
  6120. limit = &intel_limits_g4x_dual_channel_lvds;
  6121. else
  6122. limit = &intel_limits_g4x_single_channel_lvds;
  6123. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6124. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6125. limit = &intel_limits_g4x_hdmi;
  6126. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6127. limit = &intel_limits_g4x_sdvo;
  6128. } else {
  6129. /* The option is for other outputs */
  6130. limit = &intel_limits_i9xx_sdvo;
  6131. }
  6132. if (!crtc_state->clock_set &&
  6133. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6134. refclk, NULL, &crtc_state->dpll)) {
  6135. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6136. return -EINVAL;
  6137. }
  6138. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6139. return 0;
  6140. }
  6141. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6142. struct intel_crtc_state *crtc_state)
  6143. {
  6144. struct drm_device *dev = crtc->base.dev;
  6145. struct drm_i915_private *dev_priv = to_i915(dev);
  6146. const struct intel_limit *limit;
  6147. int refclk = 96000;
  6148. memset(&crtc_state->dpll_hw_state, 0,
  6149. sizeof(crtc_state->dpll_hw_state));
  6150. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6151. if (intel_panel_use_ssc(dev_priv)) {
  6152. refclk = dev_priv->vbt.lvds_ssc_freq;
  6153. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6154. }
  6155. limit = &intel_limits_pineview_lvds;
  6156. } else {
  6157. limit = &intel_limits_pineview_sdvo;
  6158. }
  6159. if (!crtc_state->clock_set &&
  6160. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6161. refclk, NULL, &crtc_state->dpll)) {
  6162. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6163. return -EINVAL;
  6164. }
  6165. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6166. return 0;
  6167. }
  6168. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6169. struct intel_crtc_state *crtc_state)
  6170. {
  6171. struct drm_device *dev = crtc->base.dev;
  6172. struct drm_i915_private *dev_priv = to_i915(dev);
  6173. const struct intel_limit *limit;
  6174. int refclk = 96000;
  6175. memset(&crtc_state->dpll_hw_state, 0,
  6176. sizeof(crtc_state->dpll_hw_state));
  6177. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6178. if (intel_panel_use_ssc(dev_priv)) {
  6179. refclk = dev_priv->vbt.lvds_ssc_freq;
  6180. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6181. }
  6182. limit = &intel_limits_i9xx_lvds;
  6183. } else {
  6184. limit = &intel_limits_i9xx_sdvo;
  6185. }
  6186. if (!crtc_state->clock_set &&
  6187. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6188. refclk, NULL, &crtc_state->dpll)) {
  6189. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6190. return -EINVAL;
  6191. }
  6192. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6193. return 0;
  6194. }
  6195. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6196. struct intel_crtc_state *crtc_state)
  6197. {
  6198. int refclk = 100000;
  6199. const struct intel_limit *limit = &intel_limits_chv;
  6200. memset(&crtc_state->dpll_hw_state, 0,
  6201. sizeof(crtc_state->dpll_hw_state));
  6202. if (!crtc_state->clock_set &&
  6203. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6204. refclk, NULL, &crtc_state->dpll)) {
  6205. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6206. return -EINVAL;
  6207. }
  6208. chv_compute_dpll(crtc, crtc_state);
  6209. return 0;
  6210. }
  6211. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6212. struct intel_crtc_state *crtc_state)
  6213. {
  6214. int refclk = 100000;
  6215. const struct intel_limit *limit = &intel_limits_vlv;
  6216. memset(&crtc_state->dpll_hw_state, 0,
  6217. sizeof(crtc_state->dpll_hw_state));
  6218. if (!crtc_state->clock_set &&
  6219. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6220. refclk, NULL, &crtc_state->dpll)) {
  6221. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6222. return -EINVAL;
  6223. }
  6224. vlv_compute_dpll(crtc, crtc_state);
  6225. return 0;
  6226. }
  6227. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6228. struct intel_crtc_state *pipe_config)
  6229. {
  6230. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6231. uint32_t tmp;
  6232. if (INTEL_GEN(dev_priv) <= 3 &&
  6233. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6234. return;
  6235. tmp = I915_READ(PFIT_CONTROL);
  6236. if (!(tmp & PFIT_ENABLE))
  6237. return;
  6238. /* Check whether the pfit is attached to our pipe. */
  6239. if (INTEL_GEN(dev_priv) < 4) {
  6240. if (crtc->pipe != PIPE_B)
  6241. return;
  6242. } else {
  6243. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6244. return;
  6245. }
  6246. pipe_config->gmch_pfit.control = tmp;
  6247. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6248. }
  6249. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6250. struct intel_crtc_state *pipe_config)
  6251. {
  6252. struct drm_device *dev = crtc->base.dev;
  6253. struct drm_i915_private *dev_priv = to_i915(dev);
  6254. int pipe = pipe_config->cpu_transcoder;
  6255. struct dpll clock;
  6256. u32 mdiv;
  6257. int refclk = 100000;
  6258. /* In case of DSI, DPLL will not be used */
  6259. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6260. return;
  6261. mutex_lock(&dev_priv->sb_lock);
  6262. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6263. mutex_unlock(&dev_priv->sb_lock);
  6264. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6265. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6266. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6267. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6268. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6269. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6270. }
  6271. static void
  6272. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6273. struct intel_initial_plane_config *plane_config)
  6274. {
  6275. struct drm_device *dev = crtc->base.dev;
  6276. struct drm_i915_private *dev_priv = to_i915(dev);
  6277. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6278. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6279. enum pipe pipe = crtc->pipe;
  6280. u32 val, base, offset;
  6281. int fourcc, pixel_format;
  6282. unsigned int aligned_height;
  6283. struct drm_framebuffer *fb;
  6284. struct intel_framebuffer *intel_fb;
  6285. if (!plane->get_hw_state(plane))
  6286. return;
  6287. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6288. if (!intel_fb) {
  6289. DRM_DEBUG_KMS("failed to alloc fb\n");
  6290. return;
  6291. }
  6292. fb = &intel_fb->base;
  6293. fb->dev = dev;
  6294. val = I915_READ(DSPCNTR(i9xx_plane));
  6295. if (INTEL_GEN(dev_priv) >= 4) {
  6296. if (val & DISPPLANE_TILED) {
  6297. plane_config->tiling = I915_TILING_X;
  6298. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6299. }
  6300. }
  6301. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6302. fourcc = i9xx_format_to_fourcc(pixel_format);
  6303. fb->format = drm_format_info(fourcc);
  6304. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6305. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6306. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6307. } else if (INTEL_GEN(dev_priv) >= 4) {
  6308. if (plane_config->tiling)
  6309. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6310. else
  6311. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6312. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6313. } else {
  6314. base = I915_READ(DSPADDR(i9xx_plane));
  6315. }
  6316. plane_config->base = base;
  6317. val = I915_READ(PIPESRC(pipe));
  6318. fb->width = ((val >> 16) & 0xfff) + 1;
  6319. fb->height = ((val >> 0) & 0xfff) + 1;
  6320. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6321. fb->pitches[0] = val & 0xffffffc0;
  6322. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6323. plane_config->size = fb->pitches[0] * aligned_height;
  6324. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6325. crtc->base.name, plane->base.name, fb->width, fb->height,
  6326. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6327. plane_config->size);
  6328. plane_config->fb = intel_fb;
  6329. }
  6330. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6331. struct intel_crtc_state *pipe_config)
  6332. {
  6333. struct drm_device *dev = crtc->base.dev;
  6334. struct drm_i915_private *dev_priv = to_i915(dev);
  6335. int pipe = pipe_config->cpu_transcoder;
  6336. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6337. struct dpll clock;
  6338. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6339. int refclk = 100000;
  6340. /* In case of DSI, DPLL will not be used */
  6341. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6342. return;
  6343. mutex_lock(&dev_priv->sb_lock);
  6344. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6345. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6346. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6347. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6348. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6349. mutex_unlock(&dev_priv->sb_lock);
  6350. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6351. clock.m2 = (pll_dw0 & 0xff) << 22;
  6352. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6353. clock.m2 |= pll_dw2 & 0x3fffff;
  6354. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6355. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6356. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6357. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6358. }
  6359. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6360. struct intel_crtc_state *pipe_config)
  6361. {
  6362. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6363. enum intel_display_power_domain power_domain;
  6364. uint32_t tmp;
  6365. bool ret;
  6366. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6367. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6368. return false;
  6369. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6370. pipe_config->shared_dpll = NULL;
  6371. ret = false;
  6372. tmp = I915_READ(PIPECONF(crtc->pipe));
  6373. if (!(tmp & PIPECONF_ENABLE))
  6374. goto out;
  6375. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6376. IS_CHERRYVIEW(dev_priv)) {
  6377. switch (tmp & PIPECONF_BPC_MASK) {
  6378. case PIPECONF_6BPC:
  6379. pipe_config->pipe_bpp = 18;
  6380. break;
  6381. case PIPECONF_8BPC:
  6382. pipe_config->pipe_bpp = 24;
  6383. break;
  6384. case PIPECONF_10BPC:
  6385. pipe_config->pipe_bpp = 30;
  6386. break;
  6387. default:
  6388. break;
  6389. }
  6390. }
  6391. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6392. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6393. pipe_config->limited_color_range = true;
  6394. if (INTEL_GEN(dev_priv) < 4)
  6395. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6396. intel_get_pipe_timings(crtc, pipe_config);
  6397. intel_get_pipe_src_size(crtc, pipe_config);
  6398. i9xx_get_pfit_config(crtc, pipe_config);
  6399. if (INTEL_GEN(dev_priv) >= 4) {
  6400. /* No way to read it out on pipes B and C */
  6401. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6402. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6403. else
  6404. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6405. pipe_config->pixel_multiplier =
  6406. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6407. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6408. pipe_config->dpll_hw_state.dpll_md = tmp;
  6409. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6410. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6411. tmp = I915_READ(DPLL(crtc->pipe));
  6412. pipe_config->pixel_multiplier =
  6413. ((tmp & SDVO_MULTIPLIER_MASK)
  6414. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6415. } else {
  6416. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6417. * port and will be fixed up in the encoder->get_config
  6418. * function. */
  6419. pipe_config->pixel_multiplier = 1;
  6420. }
  6421. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6422. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6423. /*
  6424. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6425. * on 830. Filter it out here so that we don't
  6426. * report errors due to that.
  6427. */
  6428. if (IS_I830(dev_priv))
  6429. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6430. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6431. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6432. } else {
  6433. /* Mask out read-only status bits. */
  6434. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6435. DPLL_PORTC_READY_MASK |
  6436. DPLL_PORTB_READY_MASK);
  6437. }
  6438. if (IS_CHERRYVIEW(dev_priv))
  6439. chv_crtc_clock_get(crtc, pipe_config);
  6440. else if (IS_VALLEYVIEW(dev_priv))
  6441. vlv_crtc_clock_get(crtc, pipe_config);
  6442. else
  6443. i9xx_crtc_clock_get(crtc, pipe_config);
  6444. /*
  6445. * Normally the dotclock is filled in by the encoder .get_config()
  6446. * but in case the pipe is enabled w/o any ports we need a sane
  6447. * default.
  6448. */
  6449. pipe_config->base.adjusted_mode.crtc_clock =
  6450. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6451. ret = true;
  6452. out:
  6453. intel_display_power_put(dev_priv, power_domain);
  6454. return ret;
  6455. }
  6456. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6457. {
  6458. struct intel_encoder *encoder;
  6459. int i;
  6460. u32 val, final;
  6461. bool has_lvds = false;
  6462. bool has_cpu_edp = false;
  6463. bool has_panel = false;
  6464. bool has_ck505 = false;
  6465. bool can_ssc = false;
  6466. bool using_ssc_source = false;
  6467. /* We need to take the global config into account */
  6468. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6469. switch (encoder->type) {
  6470. case INTEL_OUTPUT_LVDS:
  6471. has_panel = true;
  6472. has_lvds = true;
  6473. break;
  6474. case INTEL_OUTPUT_EDP:
  6475. has_panel = true;
  6476. if (encoder->port == PORT_A)
  6477. has_cpu_edp = true;
  6478. break;
  6479. default:
  6480. break;
  6481. }
  6482. }
  6483. if (HAS_PCH_IBX(dev_priv)) {
  6484. has_ck505 = dev_priv->vbt.display_clock_mode;
  6485. can_ssc = has_ck505;
  6486. } else {
  6487. has_ck505 = false;
  6488. can_ssc = true;
  6489. }
  6490. /* Check if any DPLLs are using the SSC source */
  6491. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6492. u32 temp = I915_READ(PCH_DPLL(i));
  6493. if (!(temp & DPLL_VCO_ENABLE))
  6494. continue;
  6495. if ((temp & PLL_REF_INPUT_MASK) ==
  6496. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6497. using_ssc_source = true;
  6498. break;
  6499. }
  6500. }
  6501. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6502. has_panel, has_lvds, has_ck505, using_ssc_source);
  6503. /* Ironlake: try to setup display ref clock before DPLL
  6504. * enabling. This is only under driver's control after
  6505. * PCH B stepping, previous chipset stepping should be
  6506. * ignoring this setting.
  6507. */
  6508. val = I915_READ(PCH_DREF_CONTROL);
  6509. /* As we must carefully and slowly disable/enable each source in turn,
  6510. * compute the final state we want first and check if we need to
  6511. * make any changes at all.
  6512. */
  6513. final = val;
  6514. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6515. if (has_ck505)
  6516. final |= DREF_NONSPREAD_CK505_ENABLE;
  6517. else
  6518. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6519. final &= ~DREF_SSC_SOURCE_MASK;
  6520. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6521. final &= ~DREF_SSC1_ENABLE;
  6522. if (has_panel) {
  6523. final |= DREF_SSC_SOURCE_ENABLE;
  6524. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6525. final |= DREF_SSC1_ENABLE;
  6526. if (has_cpu_edp) {
  6527. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6528. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6529. else
  6530. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6531. } else
  6532. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6533. } else if (using_ssc_source) {
  6534. final |= DREF_SSC_SOURCE_ENABLE;
  6535. final |= DREF_SSC1_ENABLE;
  6536. }
  6537. if (final == val)
  6538. return;
  6539. /* Always enable nonspread source */
  6540. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6541. if (has_ck505)
  6542. val |= DREF_NONSPREAD_CK505_ENABLE;
  6543. else
  6544. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6545. if (has_panel) {
  6546. val &= ~DREF_SSC_SOURCE_MASK;
  6547. val |= DREF_SSC_SOURCE_ENABLE;
  6548. /* SSC must be turned on before enabling the CPU output */
  6549. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6550. DRM_DEBUG_KMS("Using SSC on panel\n");
  6551. val |= DREF_SSC1_ENABLE;
  6552. } else
  6553. val &= ~DREF_SSC1_ENABLE;
  6554. /* Get SSC going before enabling the outputs */
  6555. I915_WRITE(PCH_DREF_CONTROL, val);
  6556. POSTING_READ(PCH_DREF_CONTROL);
  6557. udelay(200);
  6558. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6559. /* Enable CPU source on CPU attached eDP */
  6560. if (has_cpu_edp) {
  6561. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6562. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6563. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6564. } else
  6565. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6566. } else
  6567. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6568. I915_WRITE(PCH_DREF_CONTROL, val);
  6569. POSTING_READ(PCH_DREF_CONTROL);
  6570. udelay(200);
  6571. } else {
  6572. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6573. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6574. /* Turn off CPU output */
  6575. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6576. I915_WRITE(PCH_DREF_CONTROL, val);
  6577. POSTING_READ(PCH_DREF_CONTROL);
  6578. udelay(200);
  6579. if (!using_ssc_source) {
  6580. DRM_DEBUG_KMS("Disabling SSC source\n");
  6581. /* Turn off the SSC source */
  6582. val &= ~DREF_SSC_SOURCE_MASK;
  6583. val |= DREF_SSC_SOURCE_DISABLE;
  6584. /* Turn off SSC1 */
  6585. val &= ~DREF_SSC1_ENABLE;
  6586. I915_WRITE(PCH_DREF_CONTROL, val);
  6587. POSTING_READ(PCH_DREF_CONTROL);
  6588. udelay(200);
  6589. }
  6590. }
  6591. BUG_ON(val != final);
  6592. }
  6593. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6594. {
  6595. uint32_t tmp;
  6596. tmp = I915_READ(SOUTH_CHICKEN2);
  6597. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6598. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6599. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6600. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6601. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6602. tmp = I915_READ(SOUTH_CHICKEN2);
  6603. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6604. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6605. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6606. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6607. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6608. }
  6609. /* WaMPhyProgramming:hsw */
  6610. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6611. {
  6612. uint32_t tmp;
  6613. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6614. tmp &= ~(0xFF << 24);
  6615. tmp |= (0x12 << 24);
  6616. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6617. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6618. tmp |= (1 << 11);
  6619. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6620. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6621. tmp |= (1 << 11);
  6622. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6623. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6624. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6625. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6626. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6627. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6628. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6629. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6630. tmp &= ~(7 << 13);
  6631. tmp |= (5 << 13);
  6632. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6633. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6634. tmp &= ~(7 << 13);
  6635. tmp |= (5 << 13);
  6636. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6637. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6638. tmp &= ~0xFF;
  6639. tmp |= 0x1C;
  6640. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6641. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6642. tmp &= ~0xFF;
  6643. tmp |= 0x1C;
  6644. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6645. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6646. tmp &= ~(0xFF << 16);
  6647. tmp |= (0x1C << 16);
  6648. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6649. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6650. tmp &= ~(0xFF << 16);
  6651. tmp |= (0x1C << 16);
  6652. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6653. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6654. tmp |= (1 << 27);
  6655. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6656. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6657. tmp |= (1 << 27);
  6658. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6659. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6660. tmp &= ~(0xF << 28);
  6661. tmp |= (4 << 28);
  6662. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6663. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6664. tmp &= ~(0xF << 28);
  6665. tmp |= (4 << 28);
  6666. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6667. }
  6668. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6669. * Programming" based on the parameters passed:
  6670. * - Sequence to enable CLKOUT_DP
  6671. * - Sequence to enable CLKOUT_DP without spread
  6672. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6673. */
  6674. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6675. bool with_spread, bool with_fdi)
  6676. {
  6677. uint32_t reg, tmp;
  6678. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6679. with_spread = true;
  6680. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6681. with_fdi, "LP PCH doesn't have FDI\n"))
  6682. with_fdi = false;
  6683. mutex_lock(&dev_priv->sb_lock);
  6684. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6685. tmp &= ~SBI_SSCCTL_DISABLE;
  6686. tmp |= SBI_SSCCTL_PATHALT;
  6687. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6688. udelay(24);
  6689. if (with_spread) {
  6690. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6691. tmp &= ~SBI_SSCCTL_PATHALT;
  6692. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6693. if (with_fdi) {
  6694. lpt_reset_fdi_mphy(dev_priv);
  6695. lpt_program_fdi_mphy(dev_priv);
  6696. }
  6697. }
  6698. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6699. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6700. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6701. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6702. mutex_unlock(&dev_priv->sb_lock);
  6703. }
  6704. /* Sequence to disable CLKOUT_DP */
  6705. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6706. {
  6707. uint32_t reg, tmp;
  6708. mutex_lock(&dev_priv->sb_lock);
  6709. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6710. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6711. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6712. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6713. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6714. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6715. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6716. tmp |= SBI_SSCCTL_PATHALT;
  6717. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6718. udelay(32);
  6719. }
  6720. tmp |= SBI_SSCCTL_DISABLE;
  6721. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6722. }
  6723. mutex_unlock(&dev_priv->sb_lock);
  6724. }
  6725. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6726. static const uint16_t sscdivintphase[] = {
  6727. [BEND_IDX( 50)] = 0x3B23,
  6728. [BEND_IDX( 45)] = 0x3B23,
  6729. [BEND_IDX( 40)] = 0x3C23,
  6730. [BEND_IDX( 35)] = 0x3C23,
  6731. [BEND_IDX( 30)] = 0x3D23,
  6732. [BEND_IDX( 25)] = 0x3D23,
  6733. [BEND_IDX( 20)] = 0x3E23,
  6734. [BEND_IDX( 15)] = 0x3E23,
  6735. [BEND_IDX( 10)] = 0x3F23,
  6736. [BEND_IDX( 5)] = 0x3F23,
  6737. [BEND_IDX( 0)] = 0x0025,
  6738. [BEND_IDX( -5)] = 0x0025,
  6739. [BEND_IDX(-10)] = 0x0125,
  6740. [BEND_IDX(-15)] = 0x0125,
  6741. [BEND_IDX(-20)] = 0x0225,
  6742. [BEND_IDX(-25)] = 0x0225,
  6743. [BEND_IDX(-30)] = 0x0325,
  6744. [BEND_IDX(-35)] = 0x0325,
  6745. [BEND_IDX(-40)] = 0x0425,
  6746. [BEND_IDX(-45)] = 0x0425,
  6747. [BEND_IDX(-50)] = 0x0525,
  6748. };
  6749. /*
  6750. * Bend CLKOUT_DP
  6751. * steps -50 to 50 inclusive, in steps of 5
  6752. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6753. * change in clock period = -(steps / 10) * 5.787 ps
  6754. */
  6755. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6756. {
  6757. uint32_t tmp;
  6758. int idx = BEND_IDX(steps);
  6759. if (WARN_ON(steps % 5 != 0))
  6760. return;
  6761. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6762. return;
  6763. mutex_lock(&dev_priv->sb_lock);
  6764. if (steps % 10 != 0)
  6765. tmp = 0xAAAAAAAB;
  6766. else
  6767. tmp = 0x00000000;
  6768. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6769. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6770. tmp &= 0xffff0000;
  6771. tmp |= sscdivintphase[idx];
  6772. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6773. mutex_unlock(&dev_priv->sb_lock);
  6774. }
  6775. #undef BEND_IDX
  6776. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6777. {
  6778. struct intel_encoder *encoder;
  6779. bool has_vga = false;
  6780. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6781. switch (encoder->type) {
  6782. case INTEL_OUTPUT_ANALOG:
  6783. has_vga = true;
  6784. break;
  6785. default:
  6786. break;
  6787. }
  6788. }
  6789. if (has_vga) {
  6790. lpt_bend_clkout_dp(dev_priv, 0);
  6791. lpt_enable_clkout_dp(dev_priv, true, true);
  6792. } else {
  6793. lpt_disable_clkout_dp(dev_priv);
  6794. }
  6795. }
  6796. /*
  6797. * Initialize reference clocks when the driver loads
  6798. */
  6799. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6800. {
  6801. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6802. ironlake_init_pch_refclk(dev_priv);
  6803. else if (HAS_PCH_LPT(dev_priv))
  6804. lpt_init_pch_refclk(dev_priv);
  6805. }
  6806. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6807. {
  6808. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6810. int pipe = intel_crtc->pipe;
  6811. uint32_t val;
  6812. val = 0;
  6813. switch (intel_crtc->config->pipe_bpp) {
  6814. case 18:
  6815. val |= PIPECONF_6BPC;
  6816. break;
  6817. case 24:
  6818. val |= PIPECONF_8BPC;
  6819. break;
  6820. case 30:
  6821. val |= PIPECONF_10BPC;
  6822. break;
  6823. case 36:
  6824. val |= PIPECONF_12BPC;
  6825. break;
  6826. default:
  6827. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6828. BUG();
  6829. }
  6830. if (intel_crtc->config->dither)
  6831. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6832. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6833. val |= PIPECONF_INTERLACED_ILK;
  6834. else
  6835. val |= PIPECONF_PROGRESSIVE;
  6836. if (intel_crtc->config->limited_color_range)
  6837. val |= PIPECONF_COLOR_RANGE_SELECT;
  6838. I915_WRITE(PIPECONF(pipe), val);
  6839. POSTING_READ(PIPECONF(pipe));
  6840. }
  6841. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6842. {
  6843. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6845. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6846. u32 val = 0;
  6847. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6848. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6849. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6850. val |= PIPECONF_INTERLACED_ILK;
  6851. else
  6852. val |= PIPECONF_PROGRESSIVE;
  6853. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6854. POSTING_READ(PIPECONF(cpu_transcoder));
  6855. }
  6856. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6857. {
  6858. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6859. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6860. struct intel_crtc_state *config = intel_crtc->config;
  6861. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  6862. u32 val = 0;
  6863. switch (intel_crtc->config->pipe_bpp) {
  6864. case 18:
  6865. val |= PIPEMISC_DITHER_6_BPC;
  6866. break;
  6867. case 24:
  6868. val |= PIPEMISC_DITHER_8_BPC;
  6869. break;
  6870. case 30:
  6871. val |= PIPEMISC_DITHER_10_BPC;
  6872. break;
  6873. case 36:
  6874. val |= PIPEMISC_DITHER_12_BPC;
  6875. break;
  6876. default:
  6877. /* Case prevented by pipe_config_set_bpp. */
  6878. BUG();
  6879. }
  6880. if (intel_crtc->config->dither)
  6881. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6882. if (config->ycbcr420) {
  6883. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6884. PIPEMISC_YUV420_ENABLE |
  6885. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6886. }
  6887. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6888. }
  6889. }
  6890. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6891. {
  6892. /*
  6893. * Account for spread spectrum to avoid
  6894. * oversubscribing the link. Max center spread
  6895. * is 2.5%; use 5% for safety's sake.
  6896. */
  6897. u32 bps = target_clock * bpp * 21 / 20;
  6898. return DIV_ROUND_UP(bps, link_bw * 8);
  6899. }
  6900. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6901. {
  6902. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6903. }
  6904. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6905. struct intel_crtc_state *crtc_state,
  6906. struct dpll *reduced_clock)
  6907. {
  6908. struct drm_crtc *crtc = &intel_crtc->base;
  6909. struct drm_device *dev = crtc->dev;
  6910. struct drm_i915_private *dev_priv = to_i915(dev);
  6911. u32 dpll, fp, fp2;
  6912. int factor;
  6913. /* Enable autotuning of the PLL clock (if permissible) */
  6914. factor = 21;
  6915. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6916. if ((intel_panel_use_ssc(dev_priv) &&
  6917. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6918. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6919. factor = 25;
  6920. } else if (crtc_state->sdvo_tv_clock)
  6921. factor = 20;
  6922. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6923. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6924. fp |= FP_CB_TUNE;
  6925. if (reduced_clock) {
  6926. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6927. if (reduced_clock->m < factor * reduced_clock->n)
  6928. fp2 |= FP_CB_TUNE;
  6929. } else {
  6930. fp2 = fp;
  6931. }
  6932. dpll = 0;
  6933. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6934. dpll |= DPLLB_MODE_LVDS;
  6935. else
  6936. dpll |= DPLLB_MODE_DAC_SERIAL;
  6937. dpll |= (crtc_state->pixel_multiplier - 1)
  6938. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6939. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6940. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6941. dpll |= DPLL_SDVO_HIGH_SPEED;
  6942. if (intel_crtc_has_dp_encoder(crtc_state))
  6943. dpll |= DPLL_SDVO_HIGH_SPEED;
  6944. /*
  6945. * The high speed IO clock is only really required for
  6946. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6947. * possible to share the DPLL between CRT and HDMI. Enabling
  6948. * the clock needlessly does no real harm, except use up a
  6949. * bit of power potentially.
  6950. *
  6951. * We'll limit this to IVB with 3 pipes, since it has only two
  6952. * DPLLs and so DPLL sharing is the only way to get three pipes
  6953. * driving PCH ports at the same time. On SNB we could do this,
  6954. * and potentially avoid enabling the second DPLL, but it's not
  6955. * clear if it''s a win or loss power wise. No point in doing
  6956. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6957. */
  6958. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6959. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6960. dpll |= DPLL_SDVO_HIGH_SPEED;
  6961. /* compute bitmask from p1 value */
  6962. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6963. /* also FPA1 */
  6964. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6965. switch (crtc_state->dpll.p2) {
  6966. case 5:
  6967. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6968. break;
  6969. case 7:
  6970. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6971. break;
  6972. case 10:
  6973. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6974. break;
  6975. case 14:
  6976. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6977. break;
  6978. }
  6979. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6980. intel_panel_use_ssc(dev_priv))
  6981. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6982. else
  6983. dpll |= PLL_REF_INPUT_DREFCLK;
  6984. dpll |= DPLL_VCO_ENABLE;
  6985. crtc_state->dpll_hw_state.dpll = dpll;
  6986. crtc_state->dpll_hw_state.fp0 = fp;
  6987. crtc_state->dpll_hw_state.fp1 = fp2;
  6988. }
  6989. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6990. struct intel_crtc_state *crtc_state)
  6991. {
  6992. struct drm_device *dev = crtc->base.dev;
  6993. struct drm_i915_private *dev_priv = to_i915(dev);
  6994. const struct intel_limit *limit;
  6995. int refclk = 120000;
  6996. memset(&crtc_state->dpll_hw_state, 0,
  6997. sizeof(crtc_state->dpll_hw_state));
  6998. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6999. if (!crtc_state->has_pch_encoder)
  7000. return 0;
  7001. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7002. if (intel_panel_use_ssc(dev_priv)) {
  7003. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7004. dev_priv->vbt.lvds_ssc_freq);
  7005. refclk = dev_priv->vbt.lvds_ssc_freq;
  7006. }
  7007. if (intel_is_dual_link_lvds(dev)) {
  7008. if (refclk == 100000)
  7009. limit = &intel_limits_ironlake_dual_lvds_100m;
  7010. else
  7011. limit = &intel_limits_ironlake_dual_lvds;
  7012. } else {
  7013. if (refclk == 100000)
  7014. limit = &intel_limits_ironlake_single_lvds_100m;
  7015. else
  7016. limit = &intel_limits_ironlake_single_lvds;
  7017. }
  7018. } else {
  7019. limit = &intel_limits_ironlake_dac;
  7020. }
  7021. if (!crtc_state->clock_set &&
  7022. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7023. refclk, NULL, &crtc_state->dpll)) {
  7024. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7025. return -EINVAL;
  7026. }
  7027. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7028. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7029. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7030. pipe_name(crtc->pipe));
  7031. return -EINVAL;
  7032. }
  7033. return 0;
  7034. }
  7035. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7036. struct intel_link_m_n *m_n)
  7037. {
  7038. struct drm_device *dev = crtc->base.dev;
  7039. struct drm_i915_private *dev_priv = to_i915(dev);
  7040. enum pipe pipe = crtc->pipe;
  7041. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7042. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7043. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7044. & ~TU_SIZE_MASK;
  7045. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7046. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7047. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7048. }
  7049. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7050. enum transcoder transcoder,
  7051. struct intel_link_m_n *m_n,
  7052. struct intel_link_m_n *m2_n2)
  7053. {
  7054. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7055. enum pipe pipe = crtc->pipe;
  7056. if (INTEL_GEN(dev_priv) >= 5) {
  7057. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7058. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7059. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7060. & ~TU_SIZE_MASK;
  7061. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7062. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7063. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7064. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7065. * gen < 8) and if DRRS is supported (to make sure the
  7066. * registers are not unnecessarily read).
  7067. */
  7068. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7069. crtc->config->has_drrs) {
  7070. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7071. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7072. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7073. & ~TU_SIZE_MASK;
  7074. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7075. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7076. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7077. }
  7078. } else {
  7079. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7080. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7081. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7082. & ~TU_SIZE_MASK;
  7083. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7084. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7085. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7086. }
  7087. }
  7088. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7089. struct intel_crtc_state *pipe_config)
  7090. {
  7091. if (pipe_config->has_pch_encoder)
  7092. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7093. else
  7094. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7095. &pipe_config->dp_m_n,
  7096. &pipe_config->dp_m2_n2);
  7097. }
  7098. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7099. struct intel_crtc_state *pipe_config)
  7100. {
  7101. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7102. &pipe_config->fdi_m_n, NULL);
  7103. }
  7104. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7105. struct intel_crtc_state *pipe_config)
  7106. {
  7107. struct drm_device *dev = crtc->base.dev;
  7108. struct drm_i915_private *dev_priv = to_i915(dev);
  7109. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7110. uint32_t ps_ctrl = 0;
  7111. int id = -1;
  7112. int i;
  7113. /* find scaler attached to this pipe */
  7114. for (i = 0; i < crtc->num_scalers; i++) {
  7115. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7116. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7117. id = i;
  7118. pipe_config->pch_pfit.enabled = true;
  7119. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7120. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7121. break;
  7122. }
  7123. }
  7124. scaler_state->scaler_id = id;
  7125. if (id >= 0) {
  7126. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7127. } else {
  7128. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7129. }
  7130. }
  7131. static void
  7132. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7133. struct intel_initial_plane_config *plane_config)
  7134. {
  7135. struct drm_device *dev = crtc->base.dev;
  7136. struct drm_i915_private *dev_priv = to_i915(dev);
  7137. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7138. enum plane_id plane_id = plane->id;
  7139. enum pipe pipe = crtc->pipe;
  7140. u32 val, base, offset, stride_mult, tiling, alpha;
  7141. int fourcc, pixel_format;
  7142. unsigned int aligned_height;
  7143. struct drm_framebuffer *fb;
  7144. struct intel_framebuffer *intel_fb;
  7145. if (!plane->get_hw_state(plane))
  7146. return;
  7147. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7148. if (!intel_fb) {
  7149. DRM_DEBUG_KMS("failed to alloc fb\n");
  7150. return;
  7151. }
  7152. fb = &intel_fb->base;
  7153. fb->dev = dev;
  7154. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7155. if (INTEL_GEN(dev_priv) >= 11)
  7156. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7157. else
  7158. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7159. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7160. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7161. alpha &= PLANE_COLOR_ALPHA_MASK;
  7162. } else {
  7163. alpha = val & PLANE_CTL_ALPHA_MASK;
  7164. }
  7165. fourcc = skl_format_to_fourcc(pixel_format,
  7166. val & PLANE_CTL_ORDER_RGBX, alpha);
  7167. fb->format = drm_format_info(fourcc);
  7168. tiling = val & PLANE_CTL_TILED_MASK;
  7169. switch (tiling) {
  7170. case PLANE_CTL_TILED_LINEAR:
  7171. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7172. break;
  7173. case PLANE_CTL_TILED_X:
  7174. plane_config->tiling = I915_TILING_X;
  7175. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7176. break;
  7177. case PLANE_CTL_TILED_Y:
  7178. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7179. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7180. else
  7181. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7182. break;
  7183. case PLANE_CTL_TILED_YF:
  7184. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7185. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7186. else
  7187. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7188. break;
  7189. default:
  7190. MISSING_CASE(tiling);
  7191. goto error;
  7192. }
  7193. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7194. plane_config->base = base;
  7195. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7196. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7197. fb->height = ((val >> 16) & 0xfff) + 1;
  7198. fb->width = ((val >> 0) & 0x1fff) + 1;
  7199. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7200. stride_mult = intel_fb_stride_alignment(fb, 0);
  7201. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7202. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7203. plane_config->size = fb->pitches[0] * aligned_height;
  7204. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7205. crtc->base.name, plane->base.name, fb->width, fb->height,
  7206. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7207. plane_config->size);
  7208. plane_config->fb = intel_fb;
  7209. return;
  7210. error:
  7211. kfree(intel_fb);
  7212. }
  7213. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7214. struct intel_crtc_state *pipe_config)
  7215. {
  7216. struct drm_device *dev = crtc->base.dev;
  7217. struct drm_i915_private *dev_priv = to_i915(dev);
  7218. uint32_t tmp;
  7219. tmp = I915_READ(PF_CTL(crtc->pipe));
  7220. if (tmp & PF_ENABLE) {
  7221. pipe_config->pch_pfit.enabled = true;
  7222. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7223. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7224. /* We currently do not free assignements of panel fitters on
  7225. * ivb/hsw (since we don't use the higher upscaling modes which
  7226. * differentiates them) so just WARN about this case for now. */
  7227. if (IS_GEN7(dev_priv)) {
  7228. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7229. PF_PIPE_SEL_IVB(crtc->pipe));
  7230. }
  7231. }
  7232. }
  7233. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7234. struct intel_crtc_state *pipe_config)
  7235. {
  7236. struct drm_device *dev = crtc->base.dev;
  7237. struct drm_i915_private *dev_priv = to_i915(dev);
  7238. enum intel_display_power_domain power_domain;
  7239. uint32_t tmp;
  7240. bool ret;
  7241. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7242. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7243. return false;
  7244. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7245. pipe_config->shared_dpll = NULL;
  7246. ret = false;
  7247. tmp = I915_READ(PIPECONF(crtc->pipe));
  7248. if (!(tmp & PIPECONF_ENABLE))
  7249. goto out;
  7250. switch (tmp & PIPECONF_BPC_MASK) {
  7251. case PIPECONF_6BPC:
  7252. pipe_config->pipe_bpp = 18;
  7253. break;
  7254. case PIPECONF_8BPC:
  7255. pipe_config->pipe_bpp = 24;
  7256. break;
  7257. case PIPECONF_10BPC:
  7258. pipe_config->pipe_bpp = 30;
  7259. break;
  7260. case PIPECONF_12BPC:
  7261. pipe_config->pipe_bpp = 36;
  7262. break;
  7263. default:
  7264. break;
  7265. }
  7266. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7267. pipe_config->limited_color_range = true;
  7268. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7269. struct intel_shared_dpll *pll;
  7270. enum intel_dpll_id pll_id;
  7271. pipe_config->has_pch_encoder = true;
  7272. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7273. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7274. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7275. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7276. if (HAS_PCH_IBX(dev_priv)) {
  7277. /*
  7278. * The pipe->pch transcoder and pch transcoder->pll
  7279. * mapping is fixed.
  7280. */
  7281. pll_id = (enum intel_dpll_id) crtc->pipe;
  7282. } else {
  7283. tmp = I915_READ(PCH_DPLL_SEL);
  7284. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7285. pll_id = DPLL_ID_PCH_PLL_B;
  7286. else
  7287. pll_id= DPLL_ID_PCH_PLL_A;
  7288. }
  7289. pipe_config->shared_dpll =
  7290. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7291. pll = pipe_config->shared_dpll;
  7292. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7293. &pipe_config->dpll_hw_state));
  7294. tmp = pipe_config->dpll_hw_state.dpll;
  7295. pipe_config->pixel_multiplier =
  7296. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7297. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7298. ironlake_pch_clock_get(crtc, pipe_config);
  7299. } else {
  7300. pipe_config->pixel_multiplier = 1;
  7301. }
  7302. intel_get_pipe_timings(crtc, pipe_config);
  7303. intel_get_pipe_src_size(crtc, pipe_config);
  7304. ironlake_get_pfit_config(crtc, pipe_config);
  7305. ret = true;
  7306. out:
  7307. intel_display_power_put(dev_priv, power_domain);
  7308. return ret;
  7309. }
  7310. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7311. {
  7312. struct drm_device *dev = &dev_priv->drm;
  7313. struct intel_crtc *crtc;
  7314. for_each_intel_crtc(dev, crtc)
  7315. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7316. pipe_name(crtc->pipe));
  7317. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7318. "Display power well on\n");
  7319. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7320. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7321. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7322. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7323. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7324. "CPU PWM1 enabled\n");
  7325. if (IS_HASWELL(dev_priv))
  7326. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7327. "CPU PWM2 enabled\n");
  7328. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7329. "PCH PWM1 enabled\n");
  7330. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7331. "Utility pin enabled\n");
  7332. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7333. /*
  7334. * In theory we can still leave IRQs enabled, as long as only the HPD
  7335. * interrupts remain enabled. We used to check for that, but since it's
  7336. * gen-specific and since we only disable LCPLL after we fully disable
  7337. * the interrupts, the check below should be enough.
  7338. */
  7339. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7340. }
  7341. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7342. {
  7343. if (IS_HASWELL(dev_priv))
  7344. return I915_READ(D_COMP_HSW);
  7345. else
  7346. return I915_READ(D_COMP_BDW);
  7347. }
  7348. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7349. {
  7350. if (IS_HASWELL(dev_priv)) {
  7351. mutex_lock(&dev_priv->pcu_lock);
  7352. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7353. val))
  7354. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7355. mutex_unlock(&dev_priv->pcu_lock);
  7356. } else {
  7357. I915_WRITE(D_COMP_BDW, val);
  7358. POSTING_READ(D_COMP_BDW);
  7359. }
  7360. }
  7361. /*
  7362. * This function implements pieces of two sequences from BSpec:
  7363. * - Sequence for display software to disable LCPLL
  7364. * - Sequence for display software to allow package C8+
  7365. * The steps implemented here are just the steps that actually touch the LCPLL
  7366. * register. Callers should take care of disabling all the display engine
  7367. * functions, doing the mode unset, fixing interrupts, etc.
  7368. */
  7369. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7370. bool switch_to_fclk, bool allow_power_down)
  7371. {
  7372. uint32_t val;
  7373. assert_can_disable_lcpll(dev_priv);
  7374. val = I915_READ(LCPLL_CTL);
  7375. if (switch_to_fclk) {
  7376. val |= LCPLL_CD_SOURCE_FCLK;
  7377. I915_WRITE(LCPLL_CTL, val);
  7378. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7379. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7380. DRM_ERROR("Switching to FCLK failed\n");
  7381. val = I915_READ(LCPLL_CTL);
  7382. }
  7383. val |= LCPLL_PLL_DISABLE;
  7384. I915_WRITE(LCPLL_CTL, val);
  7385. POSTING_READ(LCPLL_CTL);
  7386. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7387. DRM_ERROR("LCPLL still locked\n");
  7388. val = hsw_read_dcomp(dev_priv);
  7389. val |= D_COMP_COMP_DISABLE;
  7390. hsw_write_dcomp(dev_priv, val);
  7391. ndelay(100);
  7392. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7393. 1))
  7394. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7395. if (allow_power_down) {
  7396. val = I915_READ(LCPLL_CTL);
  7397. val |= LCPLL_POWER_DOWN_ALLOW;
  7398. I915_WRITE(LCPLL_CTL, val);
  7399. POSTING_READ(LCPLL_CTL);
  7400. }
  7401. }
  7402. /*
  7403. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7404. * source.
  7405. */
  7406. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7407. {
  7408. uint32_t val;
  7409. val = I915_READ(LCPLL_CTL);
  7410. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7411. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7412. return;
  7413. /*
  7414. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7415. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7416. */
  7417. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7418. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7419. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7420. I915_WRITE(LCPLL_CTL, val);
  7421. POSTING_READ(LCPLL_CTL);
  7422. }
  7423. val = hsw_read_dcomp(dev_priv);
  7424. val |= D_COMP_COMP_FORCE;
  7425. val &= ~D_COMP_COMP_DISABLE;
  7426. hsw_write_dcomp(dev_priv, val);
  7427. val = I915_READ(LCPLL_CTL);
  7428. val &= ~LCPLL_PLL_DISABLE;
  7429. I915_WRITE(LCPLL_CTL, val);
  7430. if (intel_wait_for_register(dev_priv,
  7431. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7432. 5))
  7433. DRM_ERROR("LCPLL not locked yet\n");
  7434. if (val & LCPLL_CD_SOURCE_FCLK) {
  7435. val = I915_READ(LCPLL_CTL);
  7436. val &= ~LCPLL_CD_SOURCE_FCLK;
  7437. I915_WRITE(LCPLL_CTL, val);
  7438. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7439. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7440. DRM_ERROR("Switching back to LCPLL failed\n");
  7441. }
  7442. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7443. intel_update_cdclk(dev_priv);
  7444. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7445. }
  7446. /*
  7447. * Package states C8 and deeper are really deep PC states that can only be
  7448. * reached when all the devices on the system allow it, so even if the graphics
  7449. * device allows PC8+, it doesn't mean the system will actually get to these
  7450. * states. Our driver only allows PC8+ when going into runtime PM.
  7451. *
  7452. * The requirements for PC8+ are that all the outputs are disabled, the power
  7453. * well is disabled and most interrupts are disabled, and these are also
  7454. * requirements for runtime PM. When these conditions are met, we manually do
  7455. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7456. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7457. * hang the machine.
  7458. *
  7459. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7460. * the state of some registers, so when we come back from PC8+ we need to
  7461. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7462. * need to take care of the registers kept by RC6. Notice that this happens even
  7463. * if we don't put the device in PCI D3 state (which is what currently happens
  7464. * because of the runtime PM support).
  7465. *
  7466. * For more, read "Display Sequences for Package C8" on the hardware
  7467. * documentation.
  7468. */
  7469. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7470. {
  7471. uint32_t val;
  7472. DRM_DEBUG_KMS("Enabling package C8+\n");
  7473. if (HAS_PCH_LPT_LP(dev_priv)) {
  7474. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7475. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7476. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7477. }
  7478. lpt_disable_clkout_dp(dev_priv);
  7479. hsw_disable_lcpll(dev_priv, true, true);
  7480. }
  7481. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7482. {
  7483. uint32_t val;
  7484. DRM_DEBUG_KMS("Disabling package C8+\n");
  7485. hsw_restore_lcpll(dev_priv);
  7486. lpt_init_pch_refclk(dev_priv);
  7487. if (HAS_PCH_LPT_LP(dev_priv)) {
  7488. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7489. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7490. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7491. }
  7492. }
  7493. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7494. struct intel_crtc_state *crtc_state)
  7495. {
  7496. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7497. struct intel_encoder *encoder =
  7498. intel_ddi_get_crtc_new_encoder(crtc_state);
  7499. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7500. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7501. pipe_name(crtc->pipe));
  7502. return -EINVAL;
  7503. }
  7504. }
  7505. return 0;
  7506. }
  7507. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7508. enum port port,
  7509. struct intel_crtc_state *pipe_config)
  7510. {
  7511. enum intel_dpll_id id;
  7512. u32 temp;
  7513. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7514. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7515. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7516. return;
  7517. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7518. }
  7519. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7520. enum port port,
  7521. struct intel_crtc_state *pipe_config)
  7522. {
  7523. enum intel_dpll_id id;
  7524. switch (port) {
  7525. case PORT_A:
  7526. id = DPLL_ID_SKL_DPLL0;
  7527. break;
  7528. case PORT_B:
  7529. id = DPLL_ID_SKL_DPLL1;
  7530. break;
  7531. case PORT_C:
  7532. id = DPLL_ID_SKL_DPLL2;
  7533. break;
  7534. default:
  7535. DRM_ERROR("Incorrect port type\n");
  7536. return;
  7537. }
  7538. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7539. }
  7540. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7541. enum port port,
  7542. struct intel_crtc_state *pipe_config)
  7543. {
  7544. enum intel_dpll_id id;
  7545. u32 temp;
  7546. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7547. id = temp >> (port * 3 + 1);
  7548. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7549. return;
  7550. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7551. }
  7552. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7553. enum port port,
  7554. struct intel_crtc_state *pipe_config)
  7555. {
  7556. enum intel_dpll_id id;
  7557. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7558. switch (ddi_pll_sel) {
  7559. case PORT_CLK_SEL_WRPLL1:
  7560. id = DPLL_ID_WRPLL1;
  7561. break;
  7562. case PORT_CLK_SEL_WRPLL2:
  7563. id = DPLL_ID_WRPLL2;
  7564. break;
  7565. case PORT_CLK_SEL_SPLL:
  7566. id = DPLL_ID_SPLL;
  7567. break;
  7568. case PORT_CLK_SEL_LCPLL_810:
  7569. id = DPLL_ID_LCPLL_810;
  7570. break;
  7571. case PORT_CLK_SEL_LCPLL_1350:
  7572. id = DPLL_ID_LCPLL_1350;
  7573. break;
  7574. case PORT_CLK_SEL_LCPLL_2700:
  7575. id = DPLL_ID_LCPLL_2700;
  7576. break;
  7577. default:
  7578. MISSING_CASE(ddi_pll_sel);
  7579. /* fall through */
  7580. case PORT_CLK_SEL_NONE:
  7581. return;
  7582. }
  7583. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7584. }
  7585. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7586. struct intel_crtc_state *pipe_config,
  7587. u64 *power_domain_mask)
  7588. {
  7589. struct drm_device *dev = crtc->base.dev;
  7590. struct drm_i915_private *dev_priv = to_i915(dev);
  7591. enum intel_display_power_domain power_domain;
  7592. u32 tmp;
  7593. /*
  7594. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7595. * transcoder handled below.
  7596. */
  7597. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7598. /*
  7599. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7600. * consistency and less surprising code; it's in always on power).
  7601. */
  7602. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7603. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7604. enum pipe trans_edp_pipe;
  7605. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7606. default:
  7607. WARN(1, "unknown pipe linked to edp transcoder\n");
  7608. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7609. case TRANS_DDI_EDP_INPUT_A_ON:
  7610. trans_edp_pipe = PIPE_A;
  7611. break;
  7612. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7613. trans_edp_pipe = PIPE_B;
  7614. break;
  7615. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7616. trans_edp_pipe = PIPE_C;
  7617. break;
  7618. }
  7619. if (trans_edp_pipe == crtc->pipe)
  7620. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7621. }
  7622. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7623. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7624. return false;
  7625. *power_domain_mask |= BIT_ULL(power_domain);
  7626. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7627. return tmp & PIPECONF_ENABLE;
  7628. }
  7629. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7630. struct intel_crtc_state *pipe_config,
  7631. u64 *power_domain_mask)
  7632. {
  7633. struct drm_device *dev = crtc->base.dev;
  7634. struct drm_i915_private *dev_priv = to_i915(dev);
  7635. enum intel_display_power_domain power_domain;
  7636. enum port port;
  7637. enum transcoder cpu_transcoder;
  7638. u32 tmp;
  7639. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7640. if (port == PORT_A)
  7641. cpu_transcoder = TRANSCODER_DSI_A;
  7642. else
  7643. cpu_transcoder = TRANSCODER_DSI_C;
  7644. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7645. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7646. continue;
  7647. *power_domain_mask |= BIT_ULL(power_domain);
  7648. /*
  7649. * The PLL needs to be enabled with a valid divider
  7650. * configuration, otherwise accessing DSI registers will hang
  7651. * the machine. See BSpec North Display Engine
  7652. * registers/MIPI[BXT]. We can break out here early, since we
  7653. * need the same DSI PLL to be enabled for both DSI ports.
  7654. */
  7655. if (!intel_dsi_pll_is_enabled(dev_priv))
  7656. break;
  7657. /* XXX: this works for video mode only */
  7658. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7659. if (!(tmp & DPI_ENABLE))
  7660. continue;
  7661. tmp = I915_READ(MIPI_CTRL(port));
  7662. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7663. continue;
  7664. pipe_config->cpu_transcoder = cpu_transcoder;
  7665. break;
  7666. }
  7667. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7668. }
  7669. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7670. struct intel_crtc_state *pipe_config)
  7671. {
  7672. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7673. struct intel_shared_dpll *pll;
  7674. enum port port;
  7675. uint32_t tmp;
  7676. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7677. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7678. if (IS_CANNONLAKE(dev_priv))
  7679. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7680. else if (IS_GEN9_BC(dev_priv))
  7681. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7682. else if (IS_GEN9_LP(dev_priv))
  7683. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7684. else
  7685. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7686. pll = pipe_config->shared_dpll;
  7687. if (pll) {
  7688. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7689. &pipe_config->dpll_hw_state));
  7690. }
  7691. /*
  7692. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7693. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7694. * the PCH transcoder is on.
  7695. */
  7696. if (INTEL_GEN(dev_priv) < 9 &&
  7697. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7698. pipe_config->has_pch_encoder = true;
  7699. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7700. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7701. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7702. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7703. }
  7704. }
  7705. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7706. struct intel_crtc_state *pipe_config)
  7707. {
  7708. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7709. enum intel_display_power_domain power_domain;
  7710. u64 power_domain_mask;
  7711. bool active;
  7712. intel_crtc_init_scalers(crtc, pipe_config);
  7713. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7714. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7715. return false;
  7716. power_domain_mask = BIT_ULL(power_domain);
  7717. pipe_config->shared_dpll = NULL;
  7718. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7719. if (IS_GEN9_LP(dev_priv) &&
  7720. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7721. WARN_ON(active);
  7722. active = true;
  7723. }
  7724. if (!active)
  7725. goto out;
  7726. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7727. haswell_get_ddi_port_state(crtc, pipe_config);
  7728. intel_get_pipe_timings(crtc, pipe_config);
  7729. }
  7730. intel_get_pipe_src_size(crtc, pipe_config);
  7731. pipe_config->gamma_mode =
  7732. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7733. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7734. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7735. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7736. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7737. bool blend_mode_420 = tmp &
  7738. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7739. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7740. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7741. pipe_config->ycbcr420 != blend_mode_420)
  7742. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7743. } else if (clrspace_yuv) {
  7744. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7745. }
  7746. }
  7747. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7748. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7749. power_domain_mask |= BIT_ULL(power_domain);
  7750. if (INTEL_GEN(dev_priv) >= 9)
  7751. skylake_get_pfit_config(crtc, pipe_config);
  7752. else
  7753. ironlake_get_pfit_config(crtc, pipe_config);
  7754. }
  7755. if (hsw_crtc_supports_ips(crtc)) {
  7756. if (IS_HASWELL(dev_priv))
  7757. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7758. else {
  7759. /*
  7760. * We cannot readout IPS state on broadwell, set to
  7761. * true so we can set it to a defined state on first
  7762. * commit.
  7763. */
  7764. pipe_config->ips_enabled = true;
  7765. }
  7766. }
  7767. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7768. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7769. pipe_config->pixel_multiplier =
  7770. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7771. } else {
  7772. pipe_config->pixel_multiplier = 1;
  7773. }
  7774. out:
  7775. for_each_power_domain(power_domain, power_domain_mask)
  7776. intel_display_power_put(dev_priv, power_domain);
  7777. return active;
  7778. }
  7779. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7780. {
  7781. struct drm_i915_private *dev_priv =
  7782. to_i915(plane_state->base.plane->dev);
  7783. const struct drm_framebuffer *fb = plane_state->base.fb;
  7784. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7785. u32 base;
  7786. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7787. base = obj->phys_handle->busaddr;
  7788. else
  7789. base = intel_plane_ggtt_offset(plane_state);
  7790. base += plane_state->main.offset;
  7791. /* ILK+ do this automagically */
  7792. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7793. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7794. base += (plane_state->base.crtc_h *
  7795. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7796. return base;
  7797. }
  7798. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7799. {
  7800. int x = plane_state->base.crtc_x;
  7801. int y = plane_state->base.crtc_y;
  7802. u32 pos = 0;
  7803. if (x < 0) {
  7804. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7805. x = -x;
  7806. }
  7807. pos |= x << CURSOR_X_SHIFT;
  7808. if (y < 0) {
  7809. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7810. y = -y;
  7811. }
  7812. pos |= y << CURSOR_Y_SHIFT;
  7813. return pos;
  7814. }
  7815. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7816. {
  7817. const struct drm_mode_config *config =
  7818. &plane_state->base.plane->dev->mode_config;
  7819. int width = plane_state->base.crtc_w;
  7820. int height = plane_state->base.crtc_h;
  7821. return width > 0 && width <= config->cursor_width &&
  7822. height > 0 && height <= config->cursor_height;
  7823. }
  7824. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7825. struct intel_plane_state *plane_state)
  7826. {
  7827. const struct drm_framebuffer *fb = plane_state->base.fb;
  7828. int src_x, src_y;
  7829. u32 offset;
  7830. int ret;
  7831. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7832. &crtc_state->base,
  7833. &plane_state->clip,
  7834. DRM_PLANE_HELPER_NO_SCALING,
  7835. DRM_PLANE_HELPER_NO_SCALING,
  7836. true, true);
  7837. if (ret)
  7838. return ret;
  7839. if (!fb)
  7840. return 0;
  7841. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7842. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7843. return -EINVAL;
  7844. }
  7845. src_x = plane_state->base.src_x >> 16;
  7846. src_y = plane_state->base.src_y >> 16;
  7847. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7848. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7849. if (src_x != 0 || src_y != 0) {
  7850. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7851. return -EINVAL;
  7852. }
  7853. plane_state->main.offset = offset;
  7854. return 0;
  7855. }
  7856. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7857. const struct intel_plane_state *plane_state)
  7858. {
  7859. const struct drm_framebuffer *fb = plane_state->base.fb;
  7860. return CURSOR_ENABLE |
  7861. CURSOR_GAMMA_ENABLE |
  7862. CURSOR_FORMAT_ARGB |
  7863. CURSOR_STRIDE(fb->pitches[0]);
  7864. }
  7865. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7866. {
  7867. int width = plane_state->base.crtc_w;
  7868. /*
  7869. * 845g/865g are only limited by the width of their cursors,
  7870. * the height is arbitrary up to the precision of the register.
  7871. */
  7872. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7873. }
  7874. static int i845_check_cursor(struct intel_plane *plane,
  7875. struct intel_crtc_state *crtc_state,
  7876. struct intel_plane_state *plane_state)
  7877. {
  7878. const struct drm_framebuffer *fb = plane_state->base.fb;
  7879. int ret;
  7880. ret = intel_check_cursor(crtc_state, plane_state);
  7881. if (ret)
  7882. return ret;
  7883. /* if we want to turn off the cursor ignore width and height */
  7884. if (!fb)
  7885. return 0;
  7886. /* Check for which cursor types we support */
  7887. if (!i845_cursor_size_ok(plane_state)) {
  7888. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7889. plane_state->base.crtc_w,
  7890. plane_state->base.crtc_h);
  7891. return -EINVAL;
  7892. }
  7893. switch (fb->pitches[0]) {
  7894. case 256:
  7895. case 512:
  7896. case 1024:
  7897. case 2048:
  7898. break;
  7899. default:
  7900. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7901. fb->pitches[0]);
  7902. return -EINVAL;
  7903. }
  7904. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7905. return 0;
  7906. }
  7907. static void i845_update_cursor(struct intel_plane *plane,
  7908. const struct intel_crtc_state *crtc_state,
  7909. const struct intel_plane_state *plane_state)
  7910. {
  7911. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7912. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7913. unsigned long irqflags;
  7914. if (plane_state && plane_state->base.visible) {
  7915. unsigned int width = plane_state->base.crtc_w;
  7916. unsigned int height = plane_state->base.crtc_h;
  7917. cntl = plane_state->ctl;
  7918. size = (height << 12) | width;
  7919. base = intel_cursor_base(plane_state);
  7920. pos = intel_cursor_position(plane_state);
  7921. }
  7922. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7923. /* On these chipsets we can only modify the base/size/stride
  7924. * whilst the cursor is disabled.
  7925. */
  7926. if (plane->cursor.base != base ||
  7927. plane->cursor.size != size ||
  7928. plane->cursor.cntl != cntl) {
  7929. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7930. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7931. I915_WRITE_FW(CURSIZE, size);
  7932. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7933. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7934. plane->cursor.base = base;
  7935. plane->cursor.size = size;
  7936. plane->cursor.cntl = cntl;
  7937. } else {
  7938. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7939. }
  7940. POSTING_READ_FW(CURCNTR(PIPE_A));
  7941. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7942. }
  7943. static void i845_disable_cursor(struct intel_plane *plane,
  7944. struct intel_crtc *crtc)
  7945. {
  7946. i845_update_cursor(plane, NULL, NULL);
  7947. }
  7948. static bool i845_cursor_get_hw_state(struct intel_plane *plane)
  7949. {
  7950. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7951. enum intel_display_power_domain power_domain;
  7952. bool ret;
  7953. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  7954. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7955. return false;
  7956. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  7957. intel_display_power_put(dev_priv, power_domain);
  7958. return ret;
  7959. }
  7960. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7961. const struct intel_plane_state *plane_state)
  7962. {
  7963. struct drm_i915_private *dev_priv =
  7964. to_i915(plane_state->base.plane->dev);
  7965. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7966. u32 cntl;
  7967. cntl = MCURSOR_GAMMA_ENABLE;
  7968. if (HAS_DDI(dev_priv))
  7969. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7970. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7971. switch (plane_state->base.crtc_w) {
  7972. case 64:
  7973. cntl |= CURSOR_MODE_64_ARGB_AX;
  7974. break;
  7975. case 128:
  7976. cntl |= CURSOR_MODE_128_ARGB_AX;
  7977. break;
  7978. case 256:
  7979. cntl |= CURSOR_MODE_256_ARGB_AX;
  7980. break;
  7981. default:
  7982. MISSING_CASE(plane_state->base.crtc_w);
  7983. return 0;
  7984. }
  7985. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7986. cntl |= CURSOR_ROTATE_180;
  7987. return cntl;
  7988. }
  7989. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  7990. {
  7991. struct drm_i915_private *dev_priv =
  7992. to_i915(plane_state->base.plane->dev);
  7993. int width = plane_state->base.crtc_w;
  7994. int height = plane_state->base.crtc_h;
  7995. if (!intel_cursor_size_ok(plane_state))
  7996. return false;
  7997. /* Cursor width is limited to a few power-of-two sizes */
  7998. switch (width) {
  7999. case 256:
  8000. case 128:
  8001. case 64:
  8002. break;
  8003. default:
  8004. return false;
  8005. }
  8006. /*
  8007. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8008. * height from 8 lines up to the cursor width, when the
  8009. * cursor is not rotated. Everything else requires square
  8010. * cursors.
  8011. */
  8012. if (HAS_CUR_FBC(dev_priv) &&
  8013. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8014. if (height < 8 || height > width)
  8015. return false;
  8016. } else {
  8017. if (height != width)
  8018. return false;
  8019. }
  8020. return true;
  8021. }
  8022. static int i9xx_check_cursor(struct intel_plane *plane,
  8023. struct intel_crtc_state *crtc_state,
  8024. struct intel_plane_state *plane_state)
  8025. {
  8026. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8027. const struct drm_framebuffer *fb = plane_state->base.fb;
  8028. enum pipe pipe = plane->pipe;
  8029. int ret;
  8030. ret = intel_check_cursor(crtc_state, plane_state);
  8031. if (ret)
  8032. return ret;
  8033. /* if we want to turn off the cursor ignore width and height */
  8034. if (!fb)
  8035. return 0;
  8036. /* Check for which cursor types we support */
  8037. if (!i9xx_cursor_size_ok(plane_state)) {
  8038. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8039. plane_state->base.crtc_w,
  8040. plane_state->base.crtc_h);
  8041. return -EINVAL;
  8042. }
  8043. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8044. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8045. fb->pitches[0], plane_state->base.crtc_w);
  8046. return -EINVAL;
  8047. }
  8048. /*
  8049. * There's something wrong with the cursor on CHV pipe C.
  8050. * If it straddles the left edge of the screen then
  8051. * moving it away from the edge or disabling it often
  8052. * results in a pipe underrun, and often that can lead to
  8053. * dead pipe (constant underrun reported, and it scans
  8054. * out just a solid color). To recover from that, the
  8055. * display power well must be turned off and on again.
  8056. * Refuse the put the cursor into that compromised position.
  8057. */
  8058. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8059. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8060. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8061. return -EINVAL;
  8062. }
  8063. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8064. return 0;
  8065. }
  8066. static void i9xx_update_cursor(struct intel_plane *plane,
  8067. const struct intel_crtc_state *crtc_state,
  8068. const struct intel_plane_state *plane_state)
  8069. {
  8070. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8071. enum pipe pipe = plane->pipe;
  8072. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8073. unsigned long irqflags;
  8074. if (plane_state && plane_state->base.visible) {
  8075. cntl = plane_state->ctl;
  8076. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8077. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8078. base = intel_cursor_base(plane_state);
  8079. pos = intel_cursor_position(plane_state);
  8080. }
  8081. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8082. /*
  8083. * On some platforms writing CURCNTR first will also
  8084. * cause CURPOS to be armed by the CURBASE write.
  8085. * Without the CURCNTR write the CURPOS write would
  8086. * arm itself. Thus we always start the full update
  8087. * with a CURCNTR write.
  8088. *
  8089. * On other platforms CURPOS always requires the
  8090. * CURBASE write to arm the update. Additonally
  8091. * a write to any of the cursor register will cancel
  8092. * an already armed cursor update. Thus leaving out
  8093. * the CURBASE write after CURPOS could lead to a
  8094. * cursor that doesn't appear to move, or even change
  8095. * shape. Thus we always write CURBASE.
  8096. *
  8097. * CURCNTR and CUR_FBC_CTL are always
  8098. * armed by the CURBASE write only.
  8099. */
  8100. if (plane->cursor.base != base ||
  8101. plane->cursor.size != fbc_ctl ||
  8102. plane->cursor.cntl != cntl) {
  8103. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8104. if (HAS_CUR_FBC(dev_priv))
  8105. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8106. I915_WRITE_FW(CURPOS(pipe), pos);
  8107. I915_WRITE_FW(CURBASE(pipe), base);
  8108. plane->cursor.base = base;
  8109. plane->cursor.size = fbc_ctl;
  8110. plane->cursor.cntl = cntl;
  8111. } else {
  8112. I915_WRITE_FW(CURPOS(pipe), pos);
  8113. I915_WRITE_FW(CURBASE(pipe), base);
  8114. }
  8115. POSTING_READ_FW(CURBASE(pipe));
  8116. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8117. }
  8118. static void i9xx_disable_cursor(struct intel_plane *plane,
  8119. struct intel_crtc *crtc)
  8120. {
  8121. i9xx_update_cursor(plane, NULL, NULL);
  8122. }
  8123. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
  8124. {
  8125. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8126. enum intel_display_power_domain power_domain;
  8127. enum pipe pipe = plane->pipe;
  8128. bool ret;
  8129. /*
  8130. * Not 100% correct for planes that can move between pipes,
  8131. * but that's only the case for gen2-3 which don't have any
  8132. * display power wells.
  8133. */
  8134. power_domain = POWER_DOMAIN_PIPE(pipe);
  8135. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8136. return false;
  8137. ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  8138. intel_display_power_put(dev_priv, power_domain);
  8139. return ret;
  8140. }
  8141. /* VESA 640x480x72Hz mode to set on the pipe */
  8142. static const struct drm_display_mode load_detect_mode = {
  8143. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8144. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8145. };
  8146. struct drm_framebuffer *
  8147. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8148. struct drm_mode_fb_cmd2 *mode_cmd)
  8149. {
  8150. struct intel_framebuffer *intel_fb;
  8151. int ret;
  8152. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8153. if (!intel_fb)
  8154. return ERR_PTR(-ENOMEM);
  8155. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8156. if (ret)
  8157. goto err;
  8158. return &intel_fb->base;
  8159. err:
  8160. kfree(intel_fb);
  8161. return ERR_PTR(ret);
  8162. }
  8163. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8164. struct drm_crtc *crtc)
  8165. {
  8166. struct drm_plane *plane;
  8167. struct drm_plane_state *plane_state;
  8168. int ret, i;
  8169. ret = drm_atomic_add_affected_planes(state, crtc);
  8170. if (ret)
  8171. return ret;
  8172. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8173. if (plane_state->crtc != crtc)
  8174. continue;
  8175. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8176. if (ret)
  8177. return ret;
  8178. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8179. }
  8180. return 0;
  8181. }
  8182. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8183. const struct drm_display_mode *mode,
  8184. struct intel_load_detect_pipe *old,
  8185. struct drm_modeset_acquire_ctx *ctx)
  8186. {
  8187. struct intel_crtc *intel_crtc;
  8188. struct intel_encoder *intel_encoder =
  8189. intel_attached_encoder(connector);
  8190. struct drm_crtc *possible_crtc;
  8191. struct drm_encoder *encoder = &intel_encoder->base;
  8192. struct drm_crtc *crtc = NULL;
  8193. struct drm_device *dev = encoder->dev;
  8194. struct drm_i915_private *dev_priv = to_i915(dev);
  8195. struct drm_mode_config *config = &dev->mode_config;
  8196. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8197. struct drm_connector_state *connector_state;
  8198. struct intel_crtc_state *crtc_state;
  8199. int ret, i = -1;
  8200. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8201. connector->base.id, connector->name,
  8202. encoder->base.id, encoder->name);
  8203. old->restore_state = NULL;
  8204. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8205. /*
  8206. * Algorithm gets a little messy:
  8207. *
  8208. * - if the connector already has an assigned crtc, use it (but make
  8209. * sure it's on first)
  8210. *
  8211. * - try to find the first unused crtc that can drive this connector,
  8212. * and use that if we find one
  8213. */
  8214. /* See if we already have a CRTC for this connector */
  8215. if (connector->state->crtc) {
  8216. crtc = connector->state->crtc;
  8217. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8218. if (ret)
  8219. goto fail;
  8220. /* Make sure the crtc and connector are running */
  8221. goto found;
  8222. }
  8223. /* Find an unused one (if possible) */
  8224. for_each_crtc(dev, possible_crtc) {
  8225. i++;
  8226. if (!(encoder->possible_crtcs & (1 << i)))
  8227. continue;
  8228. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8229. if (ret)
  8230. goto fail;
  8231. if (possible_crtc->state->enable) {
  8232. drm_modeset_unlock(&possible_crtc->mutex);
  8233. continue;
  8234. }
  8235. crtc = possible_crtc;
  8236. break;
  8237. }
  8238. /*
  8239. * If we didn't find an unused CRTC, don't use any.
  8240. */
  8241. if (!crtc) {
  8242. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8243. ret = -ENODEV;
  8244. goto fail;
  8245. }
  8246. found:
  8247. intel_crtc = to_intel_crtc(crtc);
  8248. state = drm_atomic_state_alloc(dev);
  8249. restore_state = drm_atomic_state_alloc(dev);
  8250. if (!state || !restore_state) {
  8251. ret = -ENOMEM;
  8252. goto fail;
  8253. }
  8254. state->acquire_ctx = ctx;
  8255. restore_state->acquire_ctx = ctx;
  8256. connector_state = drm_atomic_get_connector_state(state, connector);
  8257. if (IS_ERR(connector_state)) {
  8258. ret = PTR_ERR(connector_state);
  8259. goto fail;
  8260. }
  8261. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8262. if (ret)
  8263. goto fail;
  8264. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8265. if (IS_ERR(crtc_state)) {
  8266. ret = PTR_ERR(crtc_state);
  8267. goto fail;
  8268. }
  8269. crtc_state->base.active = crtc_state->base.enable = true;
  8270. if (!mode)
  8271. mode = &load_detect_mode;
  8272. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8273. if (ret)
  8274. goto fail;
  8275. ret = intel_modeset_disable_planes(state, crtc);
  8276. if (ret)
  8277. goto fail;
  8278. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8279. if (!ret)
  8280. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8281. if (ret) {
  8282. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8283. goto fail;
  8284. }
  8285. ret = drm_atomic_commit(state);
  8286. if (ret) {
  8287. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8288. goto fail;
  8289. }
  8290. old->restore_state = restore_state;
  8291. drm_atomic_state_put(state);
  8292. /* let the connector get through one full cycle before testing */
  8293. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8294. return true;
  8295. fail:
  8296. if (state) {
  8297. drm_atomic_state_put(state);
  8298. state = NULL;
  8299. }
  8300. if (restore_state) {
  8301. drm_atomic_state_put(restore_state);
  8302. restore_state = NULL;
  8303. }
  8304. if (ret == -EDEADLK)
  8305. return ret;
  8306. return false;
  8307. }
  8308. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8309. struct intel_load_detect_pipe *old,
  8310. struct drm_modeset_acquire_ctx *ctx)
  8311. {
  8312. struct intel_encoder *intel_encoder =
  8313. intel_attached_encoder(connector);
  8314. struct drm_encoder *encoder = &intel_encoder->base;
  8315. struct drm_atomic_state *state = old->restore_state;
  8316. int ret;
  8317. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8318. connector->base.id, connector->name,
  8319. encoder->base.id, encoder->name);
  8320. if (!state)
  8321. return;
  8322. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8323. if (ret)
  8324. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8325. drm_atomic_state_put(state);
  8326. }
  8327. static int i9xx_pll_refclk(struct drm_device *dev,
  8328. const struct intel_crtc_state *pipe_config)
  8329. {
  8330. struct drm_i915_private *dev_priv = to_i915(dev);
  8331. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8332. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8333. return dev_priv->vbt.lvds_ssc_freq;
  8334. else if (HAS_PCH_SPLIT(dev_priv))
  8335. return 120000;
  8336. else if (!IS_GEN2(dev_priv))
  8337. return 96000;
  8338. else
  8339. return 48000;
  8340. }
  8341. /* Returns the clock of the currently programmed mode of the given pipe. */
  8342. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8343. struct intel_crtc_state *pipe_config)
  8344. {
  8345. struct drm_device *dev = crtc->base.dev;
  8346. struct drm_i915_private *dev_priv = to_i915(dev);
  8347. int pipe = pipe_config->cpu_transcoder;
  8348. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8349. u32 fp;
  8350. struct dpll clock;
  8351. int port_clock;
  8352. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8353. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8354. fp = pipe_config->dpll_hw_state.fp0;
  8355. else
  8356. fp = pipe_config->dpll_hw_state.fp1;
  8357. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8358. if (IS_PINEVIEW(dev_priv)) {
  8359. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8360. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8361. } else {
  8362. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8363. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8364. }
  8365. if (!IS_GEN2(dev_priv)) {
  8366. if (IS_PINEVIEW(dev_priv))
  8367. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8368. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8369. else
  8370. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8371. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8372. switch (dpll & DPLL_MODE_MASK) {
  8373. case DPLLB_MODE_DAC_SERIAL:
  8374. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8375. 5 : 10;
  8376. break;
  8377. case DPLLB_MODE_LVDS:
  8378. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8379. 7 : 14;
  8380. break;
  8381. default:
  8382. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8383. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8384. return;
  8385. }
  8386. if (IS_PINEVIEW(dev_priv))
  8387. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8388. else
  8389. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8390. } else {
  8391. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8392. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8393. if (is_lvds) {
  8394. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8395. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8396. if (lvds & LVDS_CLKB_POWER_UP)
  8397. clock.p2 = 7;
  8398. else
  8399. clock.p2 = 14;
  8400. } else {
  8401. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8402. clock.p1 = 2;
  8403. else {
  8404. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8405. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8406. }
  8407. if (dpll & PLL_P2_DIVIDE_BY_4)
  8408. clock.p2 = 4;
  8409. else
  8410. clock.p2 = 2;
  8411. }
  8412. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8413. }
  8414. /*
  8415. * This value includes pixel_multiplier. We will use
  8416. * port_clock to compute adjusted_mode.crtc_clock in the
  8417. * encoder's get_config() function.
  8418. */
  8419. pipe_config->port_clock = port_clock;
  8420. }
  8421. int intel_dotclock_calculate(int link_freq,
  8422. const struct intel_link_m_n *m_n)
  8423. {
  8424. /*
  8425. * The calculation for the data clock is:
  8426. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8427. * But we want to avoid losing precison if possible, so:
  8428. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8429. *
  8430. * and the link clock is simpler:
  8431. * link_clock = (m * link_clock) / n
  8432. */
  8433. if (!m_n->link_n)
  8434. return 0;
  8435. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8436. }
  8437. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8438. struct intel_crtc_state *pipe_config)
  8439. {
  8440. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8441. /* read out port_clock from the DPLL */
  8442. i9xx_crtc_clock_get(crtc, pipe_config);
  8443. /*
  8444. * In case there is an active pipe without active ports,
  8445. * we may need some idea for the dotclock anyway.
  8446. * Calculate one based on the FDI configuration.
  8447. */
  8448. pipe_config->base.adjusted_mode.crtc_clock =
  8449. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8450. &pipe_config->fdi_m_n);
  8451. }
  8452. /* Returns the currently programmed mode of the given encoder. */
  8453. struct drm_display_mode *
  8454. intel_encoder_current_mode(struct intel_encoder *encoder)
  8455. {
  8456. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8457. struct intel_crtc_state *crtc_state;
  8458. struct drm_display_mode *mode;
  8459. struct intel_crtc *crtc;
  8460. enum pipe pipe;
  8461. if (!encoder->get_hw_state(encoder, &pipe))
  8462. return NULL;
  8463. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8464. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8465. if (!mode)
  8466. return NULL;
  8467. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8468. if (!crtc_state) {
  8469. kfree(mode);
  8470. return NULL;
  8471. }
  8472. crtc_state->base.crtc = &crtc->base;
  8473. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8474. kfree(crtc_state);
  8475. kfree(mode);
  8476. return NULL;
  8477. }
  8478. encoder->get_config(encoder, crtc_state);
  8479. intel_mode_from_pipe_config(mode, crtc_state);
  8480. kfree(crtc_state);
  8481. return mode;
  8482. }
  8483. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8484. {
  8485. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8486. drm_crtc_cleanup(crtc);
  8487. kfree(intel_crtc);
  8488. }
  8489. /**
  8490. * intel_wm_need_update - Check whether watermarks need updating
  8491. * @plane: drm plane
  8492. * @state: new plane state
  8493. *
  8494. * Check current plane state versus the new one to determine whether
  8495. * watermarks need to be recalculated.
  8496. *
  8497. * Returns true or false.
  8498. */
  8499. static bool intel_wm_need_update(struct drm_plane *plane,
  8500. struct drm_plane_state *state)
  8501. {
  8502. struct intel_plane_state *new = to_intel_plane_state(state);
  8503. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8504. /* Update watermarks on tiling or size changes. */
  8505. if (new->base.visible != cur->base.visible)
  8506. return true;
  8507. if (!cur->base.fb || !new->base.fb)
  8508. return false;
  8509. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8510. cur->base.rotation != new->base.rotation ||
  8511. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8512. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8513. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8514. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8515. return true;
  8516. return false;
  8517. }
  8518. static bool needs_scaling(const struct intel_plane_state *state)
  8519. {
  8520. int src_w = drm_rect_width(&state->base.src) >> 16;
  8521. int src_h = drm_rect_height(&state->base.src) >> 16;
  8522. int dst_w = drm_rect_width(&state->base.dst);
  8523. int dst_h = drm_rect_height(&state->base.dst);
  8524. return (src_w != dst_w || src_h != dst_h);
  8525. }
  8526. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8527. struct drm_crtc_state *crtc_state,
  8528. const struct intel_plane_state *old_plane_state,
  8529. struct drm_plane_state *plane_state)
  8530. {
  8531. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8532. struct drm_crtc *crtc = crtc_state->crtc;
  8533. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8534. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8535. struct drm_device *dev = crtc->dev;
  8536. struct drm_i915_private *dev_priv = to_i915(dev);
  8537. bool mode_changed = needs_modeset(crtc_state);
  8538. bool was_crtc_enabled = old_crtc_state->base.active;
  8539. bool is_crtc_enabled = crtc_state->active;
  8540. bool turn_off, turn_on, visible, was_visible;
  8541. struct drm_framebuffer *fb = plane_state->fb;
  8542. int ret;
  8543. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8544. ret = skl_update_scaler_plane(
  8545. to_intel_crtc_state(crtc_state),
  8546. to_intel_plane_state(plane_state));
  8547. if (ret)
  8548. return ret;
  8549. }
  8550. was_visible = old_plane_state->base.visible;
  8551. visible = plane_state->visible;
  8552. if (!was_crtc_enabled && WARN_ON(was_visible))
  8553. was_visible = false;
  8554. /*
  8555. * Visibility is calculated as if the crtc was on, but
  8556. * after scaler setup everything depends on it being off
  8557. * when the crtc isn't active.
  8558. *
  8559. * FIXME this is wrong for watermarks. Watermarks should also
  8560. * be computed as if the pipe would be active. Perhaps move
  8561. * per-plane wm computation to the .check_plane() hook, and
  8562. * only combine the results from all planes in the current place?
  8563. */
  8564. if (!is_crtc_enabled) {
  8565. plane_state->visible = visible = false;
  8566. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8567. }
  8568. if (!was_visible && !visible)
  8569. return 0;
  8570. if (fb != old_plane_state->base.fb)
  8571. pipe_config->fb_changed = true;
  8572. turn_off = was_visible && (!visible || mode_changed);
  8573. turn_on = visible && (!was_visible || mode_changed);
  8574. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8575. intel_crtc->base.base.id, intel_crtc->base.name,
  8576. plane->base.base.id, plane->base.name,
  8577. fb ? fb->base.id : -1);
  8578. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8579. plane->base.base.id, plane->base.name,
  8580. was_visible, visible,
  8581. turn_off, turn_on, mode_changed);
  8582. if (turn_on) {
  8583. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8584. pipe_config->update_wm_pre = true;
  8585. /* must disable cxsr around plane enable/disable */
  8586. if (plane->id != PLANE_CURSOR)
  8587. pipe_config->disable_cxsr = true;
  8588. } else if (turn_off) {
  8589. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8590. pipe_config->update_wm_post = true;
  8591. /* must disable cxsr around plane enable/disable */
  8592. if (plane->id != PLANE_CURSOR)
  8593. pipe_config->disable_cxsr = true;
  8594. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8595. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8596. /* FIXME bollocks */
  8597. pipe_config->update_wm_pre = true;
  8598. pipe_config->update_wm_post = true;
  8599. }
  8600. }
  8601. if (visible || was_visible)
  8602. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8603. /*
  8604. * WaCxSRDisabledForSpriteScaling:ivb
  8605. *
  8606. * cstate->update_wm was already set above, so this flag will
  8607. * take effect when we commit and program watermarks.
  8608. */
  8609. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8610. needs_scaling(to_intel_plane_state(plane_state)) &&
  8611. !needs_scaling(old_plane_state))
  8612. pipe_config->disable_lp_wm = true;
  8613. return 0;
  8614. }
  8615. static bool encoders_cloneable(const struct intel_encoder *a,
  8616. const struct intel_encoder *b)
  8617. {
  8618. /* masks could be asymmetric, so check both ways */
  8619. return a == b || (a->cloneable & (1 << b->type) &&
  8620. b->cloneable & (1 << a->type));
  8621. }
  8622. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8623. struct intel_crtc *crtc,
  8624. struct intel_encoder *encoder)
  8625. {
  8626. struct intel_encoder *source_encoder;
  8627. struct drm_connector *connector;
  8628. struct drm_connector_state *connector_state;
  8629. int i;
  8630. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8631. if (connector_state->crtc != &crtc->base)
  8632. continue;
  8633. source_encoder =
  8634. to_intel_encoder(connector_state->best_encoder);
  8635. if (!encoders_cloneable(encoder, source_encoder))
  8636. return false;
  8637. }
  8638. return true;
  8639. }
  8640. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8641. struct drm_crtc_state *crtc_state)
  8642. {
  8643. struct drm_device *dev = crtc->dev;
  8644. struct drm_i915_private *dev_priv = to_i915(dev);
  8645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8646. struct intel_crtc_state *pipe_config =
  8647. to_intel_crtc_state(crtc_state);
  8648. struct drm_atomic_state *state = crtc_state->state;
  8649. int ret;
  8650. bool mode_changed = needs_modeset(crtc_state);
  8651. if (mode_changed && !crtc_state->active)
  8652. pipe_config->update_wm_post = true;
  8653. if (mode_changed && crtc_state->enable &&
  8654. dev_priv->display.crtc_compute_clock &&
  8655. !WARN_ON(pipe_config->shared_dpll)) {
  8656. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8657. pipe_config);
  8658. if (ret)
  8659. return ret;
  8660. }
  8661. if (crtc_state->color_mgmt_changed) {
  8662. ret = intel_color_check(crtc, crtc_state);
  8663. if (ret)
  8664. return ret;
  8665. /*
  8666. * Changing color management on Intel hardware is
  8667. * handled as part of planes update.
  8668. */
  8669. crtc_state->planes_changed = true;
  8670. }
  8671. ret = 0;
  8672. if (dev_priv->display.compute_pipe_wm) {
  8673. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8674. if (ret) {
  8675. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8676. return ret;
  8677. }
  8678. }
  8679. if (dev_priv->display.compute_intermediate_wm &&
  8680. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8681. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8682. return 0;
  8683. /*
  8684. * Calculate 'intermediate' watermarks that satisfy both the
  8685. * old state and the new state. We can program these
  8686. * immediately.
  8687. */
  8688. ret = dev_priv->display.compute_intermediate_wm(dev,
  8689. intel_crtc,
  8690. pipe_config);
  8691. if (ret) {
  8692. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8693. return ret;
  8694. }
  8695. } else if (dev_priv->display.compute_intermediate_wm) {
  8696. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8697. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8698. }
  8699. if (INTEL_GEN(dev_priv) >= 9) {
  8700. if (mode_changed)
  8701. ret = skl_update_scaler_crtc(pipe_config);
  8702. if (!ret)
  8703. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8704. pipe_config);
  8705. if (!ret)
  8706. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8707. pipe_config);
  8708. }
  8709. if (HAS_IPS(dev_priv))
  8710. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8711. return ret;
  8712. }
  8713. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8714. .atomic_begin = intel_begin_crtc_commit,
  8715. .atomic_flush = intel_finish_crtc_commit,
  8716. .atomic_check = intel_crtc_atomic_check,
  8717. };
  8718. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8719. {
  8720. struct intel_connector *connector;
  8721. struct drm_connector_list_iter conn_iter;
  8722. drm_connector_list_iter_begin(dev, &conn_iter);
  8723. for_each_intel_connector_iter(connector, &conn_iter) {
  8724. if (connector->base.state->crtc)
  8725. drm_connector_unreference(&connector->base);
  8726. if (connector->base.encoder) {
  8727. connector->base.state->best_encoder =
  8728. connector->base.encoder;
  8729. connector->base.state->crtc =
  8730. connector->base.encoder->crtc;
  8731. drm_connector_reference(&connector->base);
  8732. } else {
  8733. connector->base.state->best_encoder = NULL;
  8734. connector->base.state->crtc = NULL;
  8735. }
  8736. }
  8737. drm_connector_list_iter_end(&conn_iter);
  8738. }
  8739. static void
  8740. connected_sink_compute_bpp(struct intel_connector *connector,
  8741. struct intel_crtc_state *pipe_config)
  8742. {
  8743. const struct drm_display_info *info = &connector->base.display_info;
  8744. int bpp = pipe_config->pipe_bpp;
  8745. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8746. connector->base.base.id,
  8747. connector->base.name);
  8748. /* Don't use an invalid EDID bpc value */
  8749. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8750. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8751. bpp, info->bpc * 3);
  8752. pipe_config->pipe_bpp = info->bpc * 3;
  8753. }
  8754. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8755. if (info->bpc == 0 && bpp > 24) {
  8756. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8757. bpp);
  8758. pipe_config->pipe_bpp = 24;
  8759. }
  8760. }
  8761. static int
  8762. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8763. struct intel_crtc_state *pipe_config)
  8764. {
  8765. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8766. struct drm_atomic_state *state;
  8767. struct drm_connector *connector;
  8768. struct drm_connector_state *connector_state;
  8769. int bpp, i;
  8770. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8771. IS_CHERRYVIEW(dev_priv)))
  8772. bpp = 10*3;
  8773. else if (INTEL_GEN(dev_priv) >= 5)
  8774. bpp = 12*3;
  8775. else
  8776. bpp = 8*3;
  8777. pipe_config->pipe_bpp = bpp;
  8778. state = pipe_config->base.state;
  8779. /* Clamp display bpp to EDID value */
  8780. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8781. if (connector_state->crtc != &crtc->base)
  8782. continue;
  8783. connected_sink_compute_bpp(to_intel_connector(connector),
  8784. pipe_config);
  8785. }
  8786. return bpp;
  8787. }
  8788. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8789. {
  8790. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8791. "type: 0x%x flags: 0x%x\n",
  8792. mode->crtc_clock,
  8793. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8794. mode->crtc_hsync_end, mode->crtc_htotal,
  8795. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8796. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8797. }
  8798. static inline void
  8799. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8800. unsigned int lane_count, struct intel_link_m_n *m_n)
  8801. {
  8802. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8803. id, lane_count,
  8804. m_n->gmch_m, m_n->gmch_n,
  8805. m_n->link_m, m_n->link_n, m_n->tu);
  8806. }
  8807. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8808. static const char * const output_type_str[] = {
  8809. OUTPUT_TYPE(UNUSED),
  8810. OUTPUT_TYPE(ANALOG),
  8811. OUTPUT_TYPE(DVO),
  8812. OUTPUT_TYPE(SDVO),
  8813. OUTPUT_TYPE(LVDS),
  8814. OUTPUT_TYPE(TVOUT),
  8815. OUTPUT_TYPE(HDMI),
  8816. OUTPUT_TYPE(DP),
  8817. OUTPUT_TYPE(EDP),
  8818. OUTPUT_TYPE(DSI),
  8819. OUTPUT_TYPE(DDI),
  8820. OUTPUT_TYPE(DP_MST),
  8821. };
  8822. #undef OUTPUT_TYPE
  8823. static void snprintf_output_types(char *buf, size_t len,
  8824. unsigned int output_types)
  8825. {
  8826. char *str = buf;
  8827. int i;
  8828. str[0] = '\0';
  8829. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8830. int r;
  8831. if ((output_types & BIT(i)) == 0)
  8832. continue;
  8833. r = snprintf(str, len, "%s%s",
  8834. str != buf ? "," : "", output_type_str[i]);
  8835. if (r >= len)
  8836. break;
  8837. str += r;
  8838. len -= r;
  8839. output_types &= ~BIT(i);
  8840. }
  8841. WARN_ON_ONCE(output_types != 0);
  8842. }
  8843. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8844. struct intel_crtc_state *pipe_config,
  8845. const char *context)
  8846. {
  8847. struct drm_device *dev = crtc->base.dev;
  8848. struct drm_i915_private *dev_priv = to_i915(dev);
  8849. struct drm_plane *plane;
  8850. struct intel_plane *intel_plane;
  8851. struct intel_plane_state *state;
  8852. struct drm_framebuffer *fb;
  8853. char buf[64];
  8854. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8855. crtc->base.base.id, crtc->base.name, context);
  8856. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  8857. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  8858. buf, pipe_config->output_types);
  8859. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8860. transcoder_name(pipe_config->cpu_transcoder),
  8861. pipe_config->pipe_bpp, pipe_config->dither);
  8862. if (pipe_config->has_pch_encoder)
  8863. intel_dump_m_n_config(pipe_config, "fdi",
  8864. pipe_config->fdi_lanes,
  8865. &pipe_config->fdi_m_n);
  8866. if (pipe_config->ycbcr420)
  8867. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8868. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8869. intel_dump_m_n_config(pipe_config, "dp m_n",
  8870. pipe_config->lane_count, &pipe_config->dp_m_n);
  8871. if (pipe_config->has_drrs)
  8872. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8873. pipe_config->lane_count,
  8874. &pipe_config->dp_m2_n2);
  8875. }
  8876. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8877. pipe_config->has_audio, pipe_config->has_infoframe);
  8878. DRM_DEBUG_KMS("requested mode:\n");
  8879. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8880. DRM_DEBUG_KMS("adjusted mode:\n");
  8881. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8882. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8883. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8884. pipe_config->port_clock,
  8885. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8886. pipe_config->pixel_rate);
  8887. if (INTEL_GEN(dev_priv) >= 9)
  8888. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8889. crtc->num_scalers,
  8890. pipe_config->scaler_state.scaler_users,
  8891. pipe_config->scaler_state.scaler_id);
  8892. if (HAS_GMCH_DISPLAY(dev_priv))
  8893. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8894. pipe_config->gmch_pfit.control,
  8895. pipe_config->gmch_pfit.pgm_ratios,
  8896. pipe_config->gmch_pfit.lvds_border_bits);
  8897. else
  8898. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8899. pipe_config->pch_pfit.pos,
  8900. pipe_config->pch_pfit.size,
  8901. enableddisabled(pipe_config->pch_pfit.enabled));
  8902. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8903. pipe_config->ips_enabled, pipe_config->double_wide);
  8904. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8905. DRM_DEBUG_KMS("planes on this crtc\n");
  8906. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8907. struct drm_format_name_buf format_name;
  8908. intel_plane = to_intel_plane(plane);
  8909. if (intel_plane->pipe != crtc->pipe)
  8910. continue;
  8911. state = to_intel_plane_state(plane->state);
  8912. fb = state->base.fb;
  8913. if (!fb) {
  8914. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  8915. plane->base.id, plane->name, state->scaler_id);
  8916. continue;
  8917. }
  8918. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  8919. plane->base.id, plane->name,
  8920. fb->base.id, fb->width, fb->height,
  8921. drm_get_format_name(fb->format->format, &format_name));
  8922. if (INTEL_GEN(dev_priv) >= 9)
  8923. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  8924. state->scaler_id,
  8925. state->base.src.x1 >> 16,
  8926. state->base.src.y1 >> 16,
  8927. drm_rect_width(&state->base.src) >> 16,
  8928. drm_rect_height(&state->base.src) >> 16,
  8929. state->base.dst.x1, state->base.dst.y1,
  8930. drm_rect_width(&state->base.dst),
  8931. drm_rect_height(&state->base.dst));
  8932. }
  8933. }
  8934. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  8935. {
  8936. struct drm_device *dev = state->dev;
  8937. struct drm_connector *connector;
  8938. struct drm_connector_list_iter conn_iter;
  8939. unsigned int used_ports = 0;
  8940. unsigned int used_mst_ports = 0;
  8941. /*
  8942. * Walk the connector list instead of the encoder
  8943. * list to detect the problem on ddi platforms
  8944. * where there's just one encoder per digital port.
  8945. */
  8946. drm_connector_list_iter_begin(dev, &conn_iter);
  8947. drm_for_each_connector_iter(connector, &conn_iter) {
  8948. struct drm_connector_state *connector_state;
  8949. struct intel_encoder *encoder;
  8950. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  8951. if (!connector_state)
  8952. connector_state = connector->state;
  8953. if (!connector_state->best_encoder)
  8954. continue;
  8955. encoder = to_intel_encoder(connector_state->best_encoder);
  8956. WARN_ON(!connector_state->crtc);
  8957. switch (encoder->type) {
  8958. unsigned int port_mask;
  8959. case INTEL_OUTPUT_DDI:
  8960. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  8961. break;
  8962. case INTEL_OUTPUT_DP:
  8963. case INTEL_OUTPUT_HDMI:
  8964. case INTEL_OUTPUT_EDP:
  8965. port_mask = 1 << encoder->port;
  8966. /* the same port mustn't appear more than once */
  8967. if (used_ports & port_mask)
  8968. return false;
  8969. used_ports |= port_mask;
  8970. break;
  8971. case INTEL_OUTPUT_DP_MST:
  8972. used_mst_ports |=
  8973. 1 << encoder->port;
  8974. break;
  8975. default:
  8976. break;
  8977. }
  8978. }
  8979. drm_connector_list_iter_end(&conn_iter);
  8980. /* can't mix MST and SST/HDMI on the same port */
  8981. if (used_ports & used_mst_ports)
  8982. return false;
  8983. return true;
  8984. }
  8985. static void
  8986. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  8987. {
  8988. struct drm_i915_private *dev_priv =
  8989. to_i915(crtc_state->base.crtc->dev);
  8990. struct intel_crtc_scaler_state scaler_state;
  8991. struct intel_dpll_hw_state dpll_hw_state;
  8992. struct intel_shared_dpll *shared_dpll;
  8993. struct intel_crtc_wm_state wm_state;
  8994. bool force_thru, ips_force_disable;
  8995. /* FIXME: before the switch to atomic started, a new pipe_config was
  8996. * kzalloc'd. Code that depends on any field being zero should be
  8997. * fixed, so that the crtc_state can be safely duplicated. For now,
  8998. * only fields that are know to not cause problems are preserved. */
  8999. scaler_state = crtc_state->scaler_state;
  9000. shared_dpll = crtc_state->shared_dpll;
  9001. dpll_hw_state = crtc_state->dpll_hw_state;
  9002. force_thru = crtc_state->pch_pfit.force_thru;
  9003. ips_force_disable = crtc_state->ips_force_disable;
  9004. if (IS_G4X(dev_priv) ||
  9005. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9006. wm_state = crtc_state->wm;
  9007. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9008. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9009. memset(&crtc_state->base + 1, 0,
  9010. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9011. crtc_state->scaler_state = scaler_state;
  9012. crtc_state->shared_dpll = shared_dpll;
  9013. crtc_state->dpll_hw_state = dpll_hw_state;
  9014. crtc_state->pch_pfit.force_thru = force_thru;
  9015. crtc_state->ips_force_disable = ips_force_disable;
  9016. if (IS_G4X(dev_priv) ||
  9017. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9018. crtc_state->wm = wm_state;
  9019. }
  9020. static int
  9021. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9022. struct intel_crtc_state *pipe_config)
  9023. {
  9024. struct drm_atomic_state *state = pipe_config->base.state;
  9025. struct intel_encoder *encoder;
  9026. struct drm_connector *connector;
  9027. struct drm_connector_state *connector_state;
  9028. int base_bpp, ret = -EINVAL;
  9029. int i;
  9030. bool retry = true;
  9031. clear_intel_crtc_state(pipe_config);
  9032. pipe_config->cpu_transcoder =
  9033. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9034. /*
  9035. * Sanitize sync polarity flags based on requested ones. If neither
  9036. * positive or negative polarity is requested, treat this as meaning
  9037. * negative polarity.
  9038. */
  9039. if (!(pipe_config->base.adjusted_mode.flags &
  9040. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9041. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9042. if (!(pipe_config->base.adjusted_mode.flags &
  9043. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9044. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9045. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9046. pipe_config);
  9047. if (base_bpp < 0)
  9048. goto fail;
  9049. /*
  9050. * Determine the real pipe dimensions. Note that stereo modes can
  9051. * increase the actual pipe size due to the frame doubling and
  9052. * insertion of additional space for blanks between the frame. This
  9053. * is stored in the crtc timings. We use the requested mode to do this
  9054. * computation to clearly distinguish it from the adjusted mode, which
  9055. * can be changed by the connectors in the below retry loop.
  9056. */
  9057. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9058. &pipe_config->pipe_src_w,
  9059. &pipe_config->pipe_src_h);
  9060. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9061. if (connector_state->crtc != crtc)
  9062. continue;
  9063. encoder = to_intel_encoder(connector_state->best_encoder);
  9064. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9065. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9066. goto fail;
  9067. }
  9068. /*
  9069. * Determine output_types before calling the .compute_config()
  9070. * hooks so that the hooks can use this information safely.
  9071. */
  9072. if (encoder->compute_output_type)
  9073. pipe_config->output_types |=
  9074. BIT(encoder->compute_output_type(encoder, pipe_config,
  9075. connector_state));
  9076. else
  9077. pipe_config->output_types |= BIT(encoder->type);
  9078. }
  9079. encoder_retry:
  9080. /* Ensure the port clock defaults are reset when retrying. */
  9081. pipe_config->port_clock = 0;
  9082. pipe_config->pixel_multiplier = 1;
  9083. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9084. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9085. CRTC_STEREO_DOUBLE);
  9086. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9087. * adjust it according to limitations or connector properties, and also
  9088. * a chance to reject the mode entirely.
  9089. */
  9090. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9091. if (connector_state->crtc != crtc)
  9092. continue;
  9093. encoder = to_intel_encoder(connector_state->best_encoder);
  9094. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9095. DRM_DEBUG_KMS("Encoder config failure\n");
  9096. goto fail;
  9097. }
  9098. }
  9099. /* Set default port clock if not overwritten by the encoder. Needs to be
  9100. * done afterwards in case the encoder adjusts the mode. */
  9101. if (!pipe_config->port_clock)
  9102. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9103. * pipe_config->pixel_multiplier;
  9104. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9105. if (ret < 0) {
  9106. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9107. goto fail;
  9108. }
  9109. if (ret == RETRY) {
  9110. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9111. ret = -EINVAL;
  9112. goto fail;
  9113. }
  9114. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9115. retry = false;
  9116. goto encoder_retry;
  9117. }
  9118. /* Dithering seems to not pass-through bits correctly when it should, so
  9119. * only enable it on 6bpc panels and when its not a compliance
  9120. * test requesting 6bpc video pattern.
  9121. */
  9122. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9123. !pipe_config->dither_force_disable;
  9124. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9125. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9126. fail:
  9127. return ret;
  9128. }
  9129. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9130. {
  9131. int diff;
  9132. if (clock1 == clock2)
  9133. return true;
  9134. if (!clock1 || !clock2)
  9135. return false;
  9136. diff = abs(clock1 - clock2);
  9137. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9138. return true;
  9139. return false;
  9140. }
  9141. static bool
  9142. intel_compare_m_n(unsigned int m, unsigned int n,
  9143. unsigned int m2, unsigned int n2,
  9144. bool exact)
  9145. {
  9146. if (m == m2 && n == n2)
  9147. return true;
  9148. if (exact || !m || !n || !m2 || !n2)
  9149. return false;
  9150. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9151. if (n > n2) {
  9152. while (n > n2) {
  9153. m2 <<= 1;
  9154. n2 <<= 1;
  9155. }
  9156. } else if (n < n2) {
  9157. while (n < n2) {
  9158. m <<= 1;
  9159. n <<= 1;
  9160. }
  9161. }
  9162. if (n != n2)
  9163. return false;
  9164. return intel_fuzzy_clock_check(m, m2);
  9165. }
  9166. static bool
  9167. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9168. struct intel_link_m_n *m2_n2,
  9169. bool adjust)
  9170. {
  9171. if (m_n->tu == m2_n2->tu &&
  9172. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9173. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9174. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9175. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9176. if (adjust)
  9177. *m2_n2 = *m_n;
  9178. return true;
  9179. }
  9180. return false;
  9181. }
  9182. static void __printf(3, 4)
  9183. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9184. {
  9185. char *level;
  9186. unsigned int category;
  9187. struct va_format vaf;
  9188. va_list args;
  9189. if (adjust) {
  9190. level = KERN_DEBUG;
  9191. category = DRM_UT_KMS;
  9192. } else {
  9193. level = KERN_ERR;
  9194. category = DRM_UT_NONE;
  9195. }
  9196. va_start(args, format);
  9197. vaf.fmt = format;
  9198. vaf.va = &args;
  9199. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9200. va_end(args);
  9201. }
  9202. static bool
  9203. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9204. struct intel_crtc_state *current_config,
  9205. struct intel_crtc_state *pipe_config,
  9206. bool adjust)
  9207. {
  9208. bool ret = true;
  9209. bool fixup_inherited = adjust &&
  9210. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9211. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9212. #define PIPE_CONF_CHECK_X(name) \
  9213. if (current_config->name != pipe_config->name) { \
  9214. pipe_config_err(adjust, __stringify(name), \
  9215. "(expected 0x%08x, found 0x%08x)\n", \
  9216. current_config->name, \
  9217. pipe_config->name); \
  9218. ret = false; \
  9219. }
  9220. #define PIPE_CONF_CHECK_I(name) \
  9221. if (current_config->name != pipe_config->name) { \
  9222. pipe_config_err(adjust, __stringify(name), \
  9223. "(expected %i, found %i)\n", \
  9224. current_config->name, \
  9225. pipe_config->name); \
  9226. ret = false; \
  9227. }
  9228. #define PIPE_CONF_CHECK_BOOL(name) \
  9229. if (current_config->name != pipe_config->name) { \
  9230. pipe_config_err(adjust, __stringify(name), \
  9231. "(expected %s, found %s)\n", \
  9232. yesno(current_config->name), \
  9233. yesno(pipe_config->name)); \
  9234. ret = false; \
  9235. }
  9236. /*
  9237. * Checks state where we only read out the enabling, but not the entire
  9238. * state itself (like full infoframes or ELD for audio). These states
  9239. * require a full modeset on bootup to fix up.
  9240. */
  9241. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) \
  9242. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9243. PIPE_CONF_CHECK_BOOL(name); \
  9244. } else { \
  9245. pipe_config_err(adjust, __stringify(name), \
  9246. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9247. yesno(current_config->name), \
  9248. yesno(pipe_config->name)); \
  9249. ret = false; \
  9250. }
  9251. #define PIPE_CONF_CHECK_P(name) \
  9252. if (current_config->name != pipe_config->name) { \
  9253. pipe_config_err(adjust, __stringify(name), \
  9254. "(expected %p, found %p)\n", \
  9255. current_config->name, \
  9256. pipe_config->name); \
  9257. ret = false; \
  9258. }
  9259. #define PIPE_CONF_CHECK_M_N(name) \
  9260. if (!intel_compare_link_m_n(&current_config->name, \
  9261. &pipe_config->name,\
  9262. adjust)) { \
  9263. pipe_config_err(adjust, __stringify(name), \
  9264. "(expected tu %i gmch %i/%i link %i/%i, " \
  9265. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9266. current_config->name.tu, \
  9267. current_config->name.gmch_m, \
  9268. current_config->name.gmch_n, \
  9269. current_config->name.link_m, \
  9270. current_config->name.link_n, \
  9271. pipe_config->name.tu, \
  9272. pipe_config->name.gmch_m, \
  9273. pipe_config->name.gmch_n, \
  9274. pipe_config->name.link_m, \
  9275. pipe_config->name.link_n); \
  9276. ret = false; \
  9277. }
  9278. /* This is required for BDW+ where there is only one set of registers for
  9279. * switching between high and low RR.
  9280. * This macro can be used whenever a comparison has to be made between one
  9281. * hw state and multiple sw state variables.
  9282. */
  9283. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9284. if (!intel_compare_link_m_n(&current_config->name, \
  9285. &pipe_config->name, adjust) && \
  9286. !intel_compare_link_m_n(&current_config->alt_name, \
  9287. &pipe_config->name, adjust)) { \
  9288. pipe_config_err(adjust, __stringify(name), \
  9289. "(expected tu %i gmch %i/%i link %i/%i, " \
  9290. "or tu %i gmch %i/%i link %i/%i, " \
  9291. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9292. current_config->name.tu, \
  9293. current_config->name.gmch_m, \
  9294. current_config->name.gmch_n, \
  9295. current_config->name.link_m, \
  9296. current_config->name.link_n, \
  9297. current_config->alt_name.tu, \
  9298. current_config->alt_name.gmch_m, \
  9299. current_config->alt_name.gmch_n, \
  9300. current_config->alt_name.link_m, \
  9301. current_config->alt_name.link_n, \
  9302. pipe_config->name.tu, \
  9303. pipe_config->name.gmch_m, \
  9304. pipe_config->name.gmch_n, \
  9305. pipe_config->name.link_m, \
  9306. pipe_config->name.link_n); \
  9307. ret = false; \
  9308. }
  9309. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9310. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9311. pipe_config_err(adjust, __stringify(name), \
  9312. "(%x) (expected %i, found %i)\n", \
  9313. (mask), \
  9314. current_config->name & (mask), \
  9315. pipe_config->name & (mask)); \
  9316. ret = false; \
  9317. }
  9318. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9319. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9320. pipe_config_err(adjust, __stringify(name), \
  9321. "(expected %i, found %i)\n", \
  9322. current_config->name, \
  9323. pipe_config->name); \
  9324. ret = false; \
  9325. }
  9326. #define PIPE_CONF_QUIRK(quirk) \
  9327. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9328. PIPE_CONF_CHECK_I(cpu_transcoder);
  9329. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9330. PIPE_CONF_CHECK_I(fdi_lanes);
  9331. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9332. PIPE_CONF_CHECK_I(lane_count);
  9333. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9334. if (INTEL_GEN(dev_priv) < 8) {
  9335. PIPE_CONF_CHECK_M_N(dp_m_n);
  9336. if (current_config->has_drrs)
  9337. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9338. } else
  9339. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9340. PIPE_CONF_CHECK_X(output_types);
  9341. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9342. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9343. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9344. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9345. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9346. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9347. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9348. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9349. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9350. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9351. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9352. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9353. PIPE_CONF_CHECK_I(pixel_multiplier);
  9354. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9355. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9356. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9357. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9358. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9359. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9360. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9361. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9362. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9363. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9364. DRM_MODE_FLAG_INTERLACE);
  9365. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9366. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9367. DRM_MODE_FLAG_PHSYNC);
  9368. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9369. DRM_MODE_FLAG_NHSYNC);
  9370. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9371. DRM_MODE_FLAG_PVSYNC);
  9372. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9373. DRM_MODE_FLAG_NVSYNC);
  9374. }
  9375. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9376. /* pfit ratios are autocomputed by the hw on gen4+ */
  9377. if (INTEL_GEN(dev_priv) < 4)
  9378. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9379. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9380. if (!adjust) {
  9381. PIPE_CONF_CHECK_I(pipe_src_w);
  9382. PIPE_CONF_CHECK_I(pipe_src_h);
  9383. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9384. if (current_config->pch_pfit.enabled) {
  9385. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9386. PIPE_CONF_CHECK_X(pch_pfit.size);
  9387. }
  9388. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9389. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9390. }
  9391. PIPE_CONF_CHECK_BOOL(double_wide);
  9392. PIPE_CONF_CHECK_P(shared_dpll);
  9393. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9394. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9395. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9396. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9397. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9398. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9399. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9400. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9401. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9402. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9403. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9404. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9405. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9406. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9407. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9408. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9409. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9410. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9411. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9412. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9413. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9414. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9415. PIPE_CONF_CHECK_X(dsi_pll.div);
  9416. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9417. PIPE_CONF_CHECK_I(pipe_bpp);
  9418. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9419. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9420. PIPE_CONF_CHECK_I(min_voltage_level);
  9421. #undef PIPE_CONF_CHECK_X
  9422. #undef PIPE_CONF_CHECK_I
  9423. #undef PIPE_CONF_CHECK_BOOL
  9424. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9425. #undef PIPE_CONF_CHECK_P
  9426. #undef PIPE_CONF_CHECK_FLAGS
  9427. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9428. #undef PIPE_CONF_QUIRK
  9429. return ret;
  9430. }
  9431. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9432. const struct intel_crtc_state *pipe_config)
  9433. {
  9434. if (pipe_config->has_pch_encoder) {
  9435. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9436. &pipe_config->fdi_m_n);
  9437. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9438. /*
  9439. * FDI already provided one idea for the dotclock.
  9440. * Yell if the encoder disagrees.
  9441. */
  9442. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9443. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9444. fdi_dotclock, dotclock);
  9445. }
  9446. }
  9447. static void verify_wm_state(struct drm_crtc *crtc,
  9448. struct drm_crtc_state *new_state)
  9449. {
  9450. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9451. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9452. struct skl_pipe_wm hw_wm, *sw_wm;
  9453. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9454. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9455. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9456. const enum pipe pipe = intel_crtc->pipe;
  9457. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9458. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9459. return;
  9460. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9461. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9462. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9463. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9464. /* planes */
  9465. for_each_universal_plane(dev_priv, pipe, plane) {
  9466. hw_plane_wm = &hw_wm.planes[plane];
  9467. sw_plane_wm = &sw_wm->planes[plane];
  9468. /* Watermarks */
  9469. for (level = 0; level <= max_level; level++) {
  9470. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9471. &sw_plane_wm->wm[level]))
  9472. continue;
  9473. 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",
  9474. pipe_name(pipe), plane + 1, level,
  9475. sw_plane_wm->wm[level].plane_en,
  9476. sw_plane_wm->wm[level].plane_res_b,
  9477. sw_plane_wm->wm[level].plane_res_l,
  9478. hw_plane_wm->wm[level].plane_en,
  9479. hw_plane_wm->wm[level].plane_res_b,
  9480. hw_plane_wm->wm[level].plane_res_l);
  9481. }
  9482. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9483. &sw_plane_wm->trans_wm)) {
  9484. 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",
  9485. pipe_name(pipe), plane + 1,
  9486. sw_plane_wm->trans_wm.plane_en,
  9487. sw_plane_wm->trans_wm.plane_res_b,
  9488. sw_plane_wm->trans_wm.plane_res_l,
  9489. hw_plane_wm->trans_wm.plane_en,
  9490. hw_plane_wm->trans_wm.plane_res_b,
  9491. hw_plane_wm->trans_wm.plane_res_l);
  9492. }
  9493. /* DDB */
  9494. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9495. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9496. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9497. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9498. pipe_name(pipe), plane + 1,
  9499. sw_ddb_entry->start, sw_ddb_entry->end,
  9500. hw_ddb_entry->start, hw_ddb_entry->end);
  9501. }
  9502. }
  9503. /*
  9504. * cursor
  9505. * If the cursor plane isn't active, we may not have updated it's ddb
  9506. * allocation. In that case since the ddb allocation will be updated
  9507. * once the plane becomes visible, we can skip this check
  9508. */
  9509. if (1) {
  9510. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9511. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9512. /* Watermarks */
  9513. for (level = 0; level <= max_level; level++) {
  9514. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9515. &sw_plane_wm->wm[level]))
  9516. continue;
  9517. 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",
  9518. pipe_name(pipe), level,
  9519. sw_plane_wm->wm[level].plane_en,
  9520. sw_plane_wm->wm[level].plane_res_b,
  9521. sw_plane_wm->wm[level].plane_res_l,
  9522. hw_plane_wm->wm[level].plane_en,
  9523. hw_plane_wm->wm[level].plane_res_b,
  9524. hw_plane_wm->wm[level].plane_res_l);
  9525. }
  9526. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9527. &sw_plane_wm->trans_wm)) {
  9528. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9529. pipe_name(pipe),
  9530. sw_plane_wm->trans_wm.plane_en,
  9531. sw_plane_wm->trans_wm.plane_res_b,
  9532. sw_plane_wm->trans_wm.plane_res_l,
  9533. hw_plane_wm->trans_wm.plane_en,
  9534. hw_plane_wm->trans_wm.plane_res_b,
  9535. hw_plane_wm->trans_wm.plane_res_l);
  9536. }
  9537. /* DDB */
  9538. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9539. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9540. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9541. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9542. pipe_name(pipe),
  9543. sw_ddb_entry->start, sw_ddb_entry->end,
  9544. hw_ddb_entry->start, hw_ddb_entry->end);
  9545. }
  9546. }
  9547. }
  9548. static void
  9549. verify_connector_state(struct drm_device *dev,
  9550. struct drm_atomic_state *state,
  9551. struct drm_crtc *crtc)
  9552. {
  9553. struct drm_connector *connector;
  9554. struct drm_connector_state *new_conn_state;
  9555. int i;
  9556. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9557. struct drm_encoder *encoder = connector->encoder;
  9558. struct drm_crtc_state *crtc_state = NULL;
  9559. if (new_conn_state->crtc != crtc)
  9560. continue;
  9561. if (crtc)
  9562. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9563. intel_connector_verify_state(crtc_state, new_conn_state);
  9564. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9565. "connector's atomic encoder doesn't match legacy encoder\n");
  9566. }
  9567. }
  9568. static void
  9569. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9570. {
  9571. struct intel_encoder *encoder;
  9572. struct drm_connector *connector;
  9573. struct drm_connector_state *old_conn_state, *new_conn_state;
  9574. int i;
  9575. for_each_intel_encoder(dev, encoder) {
  9576. bool enabled = false, found = false;
  9577. enum pipe pipe;
  9578. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9579. encoder->base.base.id,
  9580. encoder->base.name);
  9581. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9582. new_conn_state, i) {
  9583. if (old_conn_state->best_encoder == &encoder->base)
  9584. found = true;
  9585. if (new_conn_state->best_encoder != &encoder->base)
  9586. continue;
  9587. found = enabled = true;
  9588. I915_STATE_WARN(new_conn_state->crtc !=
  9589. encoder->base.crtc,
  9590. "connector's crtc doesn't match encoder crtc\n");
  9591. }
  9592. if (!found)
  9593. continue;
  9594. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9595. "encoder's enabled state mismatch "
  9596. "(expected %i, found %i)\n",
  9597. !!encoder->base.crtc, enabled);
  9598. if (!encoder->base.crtc) {
  9599. bool active;
  9600. active = encoder->get_hw_state(encoder, &pipe);
  9601. I915_STATE_WARN(active,
  9602. "encoder detached but still enabled on pipe %c.\n",
  9603. pipe_name(pipe));
  9604. }
  9605. }
  9606. }
  9607. static void
  9608. verify_crtc_state(struct drm_crtc *crtc,
  9609. struct drm_crtc_state *old_crtc_state,
  9610. struct drm_crtc_state *new_crtc_state)
  9611. {
  9612. struct drm_device *dev = crtc->dev;
  9613. struct drm_i915_private *dev_priv = to_i915(dev);
  9614. struct intel_encoder *encoder;
  9615. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9616. struct intel_crtc_state *pipe_config, *sw_config;
  9617. struct drm_atomic_state *old_state;
  9618. bool active;
  9619. old_state = old_crtc_state->state;
  9620. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9621. pipe_config = to_intel_crtc_state(old_crtc_state);
  9622. memset(pipe_config, 0, sizeof(*pipe_config));
  9623. pipe_config->base.crtc = crtc;
  9624. pipe_config->base.state = old_state;
  9625. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9626. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9627. /* we keep both pipes enabled on 830 */
  9628. if (IS_I830(dev_priv))
  9629. active = new_crtc_state->active;
  9630. I915_STATE_WARN(new_crtc_state->active != active,
  9631. "crtc active state doesn't match with hw state "
  9632. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9633. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9634. "transitional active state does not match atomic hw state "
  9635. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9636. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9637. enum pipe pipe;
  9638. active = encoder->get_hw_state(encoder, &pipe);
  9639. I915_STATE_WARN(active != new_crtc_state->active,
  9640. "[ENCODER:%i] active %i with crtc active %i\n",
  9641. encoder->base.base.id, active, new_crtc_state->active);
  9642. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9643. "Encoder connected to wrong pipe %c\n",
  9644. pipe_name(pipe));
  9645. if (active)
  9646. encoder->get_config(encoder, pipe_config);
  9647. }
  9648. intel_crtc_compute_pixel_rate(pipe_config);
  9649. if (!new_crtc_state->active)
  9650. return;
  9651. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9652. sw_config = to_intel_crtc_state(new_crtc_state);
  9653. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9654. pipe_config, false)) {
  9655. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9656. intel_dump_pipe_config(intel_crtc, pipe_config,
  9657. "[hw state]");
  9658. intel_dump_pipe_config(intel_crtc, sw_config,
  9659. "[sw state]");
  9660. }
  9661. }
  9662. static void
  9663. intel_verify_planes(struct intel_atomic_state *state)
  9664. {
  9665. struct intel_plane *plane;
  9666. const struct intel_plane_state *plane_state;
  9667. int i;
  9668. for_each_new_intel_plane_in_state(state, plane,
  9669. plane_state, i)
  9670. assert_plane(plane, plane_state->base.visible);
  9671. }
  9672. static void
  9673. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9674. struct intel_shared_dpll *pll,
  9675. struct drm_crtc *crtc,
  9676. struct drm_crtc_state *new_state)
  9677. {
  9678. struct intel_dpll_hw_state dpll_hw_state;
  9679. unsigned crtc_mask;
  9680. bool active;
  9681. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9682. DRM_DEBUG_KMS("%s\n", pll->name);
  9683. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9684. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9685. I915_STATE_WARN(!pll->on && pll->active_mask,
  9686. "pll in active use but not on in sw tracking\n");
  9687. I915_STATE_WARN(pll->on && !pll->active_mask,
  9688. "pll is on but not used by any active crtc\n");
  9689. I915_STATE_WARN(pll->on != active,
  9690. "pll on state mismatch (expected %i, found %i)\n",
  9691. pll->on, active);
  9692. }
  9693. if (!crtc) {
  9694. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9695. "more active pll users than references: %x vs %x\n",
  9696. pll->active_mask, pll->state.crtc_mask);
  9697. return;
  9698. }
  9699. crtc_mask = 1 << drm_crtc_index(crtc);
  9700. if (new_state->active)
  9701. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9702. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9703. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9704. else
  9705. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9706. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9707. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9708. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9709. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9710. crtc_mask, pll->state.crtc_mask);
  9711. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9712. &dpll_hw_state,
  9713. sizeof(dpll_hw_state)),
  9714. "pll hw state mismatch\n");
  9715. }
  9716. static void
  9717. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9718. struct drm_crtc_state *old_crtc_state,
  9719. struct drm_crtc_state *new_crtc_state)
  9720. {
  9721. struct drm_i915_private *dev_priv = to_i915(dev);
  9722. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9723. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9724. if (new_state->shared_dpll)
  9725. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9726. if (old_state->shared_dpll &&
  9727. old_state->shared_dpll != new_state->shared_dpll) {
  9728. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9729. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9730. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9731. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9732. pipe_name(drm_crtc_index(crtc)));
  9733. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9734. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9735. pipe_name(drm_crtc_index(crtc)));
  9736. }
  9737. }
  9738. static void
  9739. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9740. struct drm_atomic_state *state,
  9741. struct drm_crtc_state *old_state,
  9742. struct drm_crtc_state *new_state)
  9743. {
  9744. if (!needs_modeset(new_state) &&
  9745. !to_intel_crtc_state(new_state)->update_pipe)
  9746. return;
  9747. verify_wm_state(crtc, new_state);
  9748. verify_connector_state(crtc->dev, state, crtc);
  9749. verify_crtc_state(crtc, old_state, new_state);
  9750. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9751. }
  9752. static void
  9753. verify_disabled_dpll_state(struct drm_device *dev)
  9754. {
  9755. struct drm_i915_private *dev_priv = to_i915(dev);
  9756. int i;
  9757. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9758. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9759. }
  9760. static void
  9761. intel_modeset_verify_disabled(struct drm_device *dev,
  9762. struct drm_atomic_state *state)
  9763. {
  9764. verify_encoder_state(dev, state);
  9765. verify_connector_state(dev, state, NULL);
  9766. verify_disabled_dpll_state(dev);
  9767. }
  9768. static void update_scanline_offset(struct intel_crtc *crtc)
  9769. {
  9770. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9771. /*
  9772. * The scanline counter increments at the leading edge of hsync.
  9773. *
  9774. * On most platforms it starts counting from vtotal-1 on the
  9775. * first active line. That means the scanline counter value is
  9776. * always one less than what we would expect. Ie. just after
  9777. * start of vblank, which also occurs at start of hsync (on the
  9778. * last active line), the scanline counter will read vblank_start-1.
  9779. *
  9780. * On gen2 the scanline counter starts counting from 1 instead
  9781. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9782. * to keep the value positive), instead of adding one.
  9783. *
  9784. * On HSW+ the behaviour of the scanline counter depends on the output
  9785. * type. For DP ports it behaves like most other platforms, but on HDMI
  9786. * there's an extra 1 line difference. So we need to add two instead of
  9787. * one to the value.
  9788. *
  9789. * On VLV/CHV DSI the scanline counter would appear to increment
  9790. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9791. * that means we can't tell whether we're in vblank or not while
  9792. * we're on that particular line. We must still set scanline_offset
  9793. * to 1 so that the vblank timestamps come out correct when we query
  9794. * the scanline counter from within the vblank interrupt handler.
  9795. * However if queried just before the start of vblank we'll get an
  9796. * answer that's slightly in the future.
  9797. */
  9798. if (IS_GEN2(dev_priv)) {
  9799. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9800. int vtotal;
  9801. vtotal = adjusted_mode->crtc_vtotal;
  9802. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9803. vtotal /= 2;
  9804. crtc->scanline_offset = vtotal - 1;
  9805. } else if (HAS_DDI(dev_priv) &&
  9806. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9807. crtc->scanline_offset = 2;
  9808. } else
  9809. crtc->scanline_offset = 1;
  9810. }
  9811. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9812. {
  9813. struct drm_device *dev = state->dev;
  9814. struct drm_i915_private *dev_priv = to_i915(dev);
  9815. struct drm_crtc *crtc;
  9816. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9817. int i;
  9818. if (!dev_priv->display.crtc_compute_clock)
  9819. return;
  9820. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9822. struct intel_shared_dpll *old_dpll =
  9823. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9824. if (!needs_modeset(new_crtc_state))
  9825. continue;
  9826. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9827. if (!old_dpll)
  9828. continue;
  9829. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9830. }
  9831. }
  9832. /*
  9833. * This implements the workaround described in the "notes" section of the mode
  9834. * set sequence documentation. When going from no pipes or single pipe to
  9835. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9836. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9837. */
  9838. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9839. {
  9840. struct drm_crtc_state *crtc_state;
  9841. struct intel_crtc *intel_crtc;
  9842. struct drm_crtc *crtc;
  9843. struct intel_crtc_state *first_crtc_state = NULL;
  9844. struct intel_crtc_state *other_crtc_state = NULL;
  9845. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9846. int i;
  9847. /* look at all crtc's that are going to be enabled in during modeset */
  9848. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9849. intel_crtc = to_intel_crtc(crtc);
  9850. if (!crtc_state->active || !needs_modeset(crtc_state))
  9851. continue;
  9852. if (first_crtc_state) {
  9853. other_crtc_state = to_intel_crtc_state(crtc_state);
  9854. break;
  9855. } else {
  9856. first_crtc_state = to_intel_crtc_state(crtc_state);
  9857. first_pipe = intel_crtc->pipe;
  9858. }
  9859. }
  9860. /* No workaround needed? */
  9861. if (!first_crtc_state)
  9862. return 0;
  9863. /* w/a possibly needed, check how many crtc's are already enabled. */
  9864. for_each_intel_crtc(state->dev, intel_crtc) {
  9865. struct intel_crtc_state *pipe_config;
  9866. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9867. if (IS_ERR(pipe_config))
  9868. return PTR_ERR(pipe_config);
  9869. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9870. if (!pipe_config->base.active ||
  9871. needs_modeset(&pipe_config->base))
  9872. continue;
  9873. /* 2 or more enabled crtcs means no need for w/a */
  9874. if (enabled_pipe != INVALID_PIPE)
  9875. return 0;
  9876. enabled_pipe = intel_crtc->pipe;
  9877. }
  9878. if (enabled_pipe != INVALID_PIPE)
  9879. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9880. else if (other_crtc_state)
  9881. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9882. return 0;
  9883. }
  9884. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9885. {
  9886. struct drm_crtc *crtc;
  9887. /* Add all pipes to the state */
  9888. for_each_crtc(state->dev, crtc) {
  9889. struct drm_crtc_state *crtc_state;
  9890. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9891. if (IS_ERR(crtc_state))
  9892. return PTR_ERR(crtc_state);
  9893. }
  9894. return 0;
  9895. }
  9896. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9897. {
  9898. struct drm_crtc *crtc;
  9899. /*
  9900. * Add all pipes to the state, and force
  9901. * a modeset on all the active ones.
  9902. */
  9903. for_each_crtc(state->dev, crtc) {
  9904. struct drm_crtc_state *crtc_state;
  9905. int ret;
  9906. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9907. if (IS_ERR(crtc_state))
  9908. return PTR_ERR(crtc_state);
  9909. if (!crtc_state->active || needs_modeset(crtc_state))
  9910. continue;
  9911. crtc_state->mode_changed = true;
  9912. ret = drm_atomic_add_affected_connectors(state, crtc);
  9913. if (ret)
  9914. return ret;
  9915. ret = drm_atomic_add_affected_planes(state, crtc);
  9916. if (ret)
  9917. return ret;
  9918. }
  9919. return 0;
  9920. }
  9921. static int intel_modeset_checks(struct drm_atomic_state *state)
  9922. {
  9923. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9924. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9925. struct drm_crtc *crtc;
  9926. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9927. int ret = 0, i;
  9928. if (!check_digital_port_conflicts(state)) {
  9929. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9930. return -EINVAL;
  9931. }
  9932. intel_state->modeset = true;
  9933. intel_state->active_crtcs = dev_priv->active_crtcs;
  9934. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9935. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  9936. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9937. if (new_crtc_state->active)
  9938. intel_state->active_crtcs |= 1 << i;
  9939. else
  9940. intel_state->active_crtcs &= ~(1 << i);
  9941. if (old_crtc_state->active != new_crtc_state->active)
  9942. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  9943. }
  9944. /*
  9945. * See if the config requires any additional preparation, e.g.
  9946. * to adjust global state with pipes off. We need to do this
  9947. * here so we can get the modeset_pipe updated config for the new
  9948. * mode set on this crtc. For other crtcs we need to use the
  9949. * adjusted_mode bits in the crtc directly.
  9950. */
  9951. if (dev_priv->display.modeset_calc_cdclk) {
  9952. ret = dev_priv->display.modeset_calc_cdclk(state);
  9953. if (ret < 0)
  9954. return ret;
  9955. /*
  9956. * Writes to dev_priv->cdclk.logical must protected by
  9957. * holding all the crtc locks, even if we don't end up
  9958. * touching the hardware
  9959. */
  9960. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  9961. &intel_state->cdclk.logical)) {
  9962. ret = intel_lock_all_pipes(state);
  9963. if (ret < 0)
  9964. return ret;
  9965. }
  9966. /* All pipes must be switched off while we change the cdclk. */
  9967. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  9968. &intel_state->cdclk.actual)) {
  9969. ret = intel_modeset_all_pipes(state);
  9970. if (ret < 0)
  9971. return ret;
  9972. }
  9973. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  9974. intel_state->cdclk.logical.cdclk,
  9975. intel_state->cdclk.actual.cdclk);
  9976. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  9977. intel_state->cdclk.logical.voltage_level,
  9978. intel_state->cdclk.actual.voltage_level);
  9979. } else {
  9980. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  9981. }
  9982. intel_modeset_clear_plls(state);
  9983. if (IS_HASWELL(dev_priv))
  9984. return haswell_mode_set_planes_workaround(state);
  9985. return 0;
  9986. }
  9987. /*
  9988. * Handle calculation of various watermark data at the end of the atomic check
  9989. * phase. The code here should be run after the per-crtc and per-plane 'check'
  9990. * handlers to ensure that all derived state has been updated.
  9991. */
  9992. static int calc_watermark_data(struct drm_atomic_state *state)
  9993. {
  9994. struct drm_device *dev = state->dev;
  9995. struct drm_i915_private *dev_priv = to_i915(dev);
  9996. /* Is there platform-specific watermark information to calculate? */
  9997. if (dev_priv->display.compute_global_watermarks)
  9998. return dev_priv->display.compute_global_watermarks(state);
  9999. return 0;
  10000. }
  10001. /**
  10002. * intel_atomic_check - validate state object
  10003. * @dev: drm device
  10004. * @state: state to validate
  10005. */
  10006. static int intel_atomic_check(struct drm_device *dev,
  10007. struct drm_atomic_state *state)
  10008. {
  10009. struct drm_i915_private *dev_priv = to_i915(dev);
  10010. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10011. struct drm_crtc *crtc;
  10012. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10013. int ret, i;
  10014. bool any_ms = false;
  10015. ret = drm_atomic_helper_check_modeset(dev, state);
  10016. if (ret)
  10017. return ret;
  10018. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10019. struct intel_crtc_state *pipe_config =
  10020. to_intel_crtc_state(crtc_state);
  10021. /* Catch I915_MODE_FLAG_INHERITED */
  10022. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10023. crtc_state->mode_changed = true;
  10024. if (!needs_modeset(crtc_state))
  10025. continue;
  10026. if (!crtc_state->enable) {
  10027. any_ms = true;
  10028. continue;
  10029. }
  10030. /* FIXME: For only active_changed we shouldn't need to do any
  10031. * state recomputation at all. */
  10032. ret = drm_atomic_add_affected_connectors(state, crtc);
  10033. if (ret)
  10034. return ret;
  10035. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10036. if (ret) {
  10037. intel_dump_pipe_config(to_intel_crtc(crtc),
  10038. pipe_config, "[failed]");
  10039. return ret;
  10040. }
  10041. if (i915_modparams.fastboot &&
  10042. intel_pipe_config_compare(dev_priv,
  10043. to_intel_crtc_state(old_crtc_state),
  10044. pipe_config, true)) {
  10045. crtc_state->mode_changed = false;
  10046. pipe_config->update_pipe = true;
  10047. }
  10048. if (needs_modeset(crtc_state))
  10049. any_ms = true;
  10050. ret = drm_atomic_add_affected_planes(state, crtc);
  10051. if (ret)
  10052. return ret;
  10053. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10054. needs_modeset(crtc_state) ?
  10055. "[modeset]" : "[fastset]");
  10056. }
  10057. if (any_ms) {
  10058. ret = intel_modeset_checks(state);
  10059. if (ret)
  10060. return ret;
  10061. } else {
  10062. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10063. }
  10064. ret = drm_atomic_helper_check_planes(dev, state);
  10065. if (ret)
  10066. return ret;
  10067. intel_fbc_choose_crtc(dev_priv, intel_state);
  10068. return calc_watermark_data(state);
  10069. }
  10070. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10071. struct drm_atomic_state *state)
  10072. {
  10073. return drm_atomic_helper_prepare_planes(dev, state);
  10074. }
  10075. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10076. {
  10077. struct drm_device *dev = crtc->base.dev;
  10078. if (!dev->max_vblank_count)
  10079. return drm_crtc_accurate_vblank_count(&crtc->base);
  10080. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10081. }
  10082. static void intel_update_crtc(struct drm_crtc *crtc,
  10083. struct drm_atomic_state *state,
  10084. struct drm_crtc_state *old_crtc_state,
  10085. struct drm_crtc_state *new_crtc_state)
  10086. {
  10087. struct drm_device *dev = crtc->dev;
  10088. struct drm_i915_private *dev_priv = to_i915(dev);
  10089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10090. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10091. bool modeset = needs_modeset(new_crtc_state);
  10092. if (modeset) {
  10093. update_scanline_offset(intel_crtc);
  10094. dev_priv->display.crtc_enable(pipe_config, state);
  10095. } else {
  10096. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10097. pipe_config);
  10098. }
  10099. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10100. intel_fbc_enable(
  10101. intel_crtc, pipe_config,
  10102. to_intel_plane_state(crtc->primary->state));
  10103. }
  10104. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10105. }
  10106. static void intel_update_crtcs(struct drm_atomic_state *state)
  10107. {
  10108. struct drm_crtc *crtc;
  10109. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10110. int i;
  10111. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10112. if (!new_crtc_state->active)
  10113. continue;
  10114. intel_update_crtc(crtc, state, old_crtc_state,
  10115. new_crtc_state);
  10116. }
  10117. }
  10118. static void skl_update_crtcs(struct drm_atomic_state *state)
  10119. {
  10120. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10121. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10122. struct drm_crtc *crtc;
  10123. struct intel_crtc *intel_crtc;
  10124. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10125. struct intel_crtc_state *cstate;
  10126. unsigned int updated = 0;
  10127. bool progress;
  10128. enum pipe pipe;
  10129. int i;
  10130. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10131. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10132. /* ignore allocations for crtc's that have been turned off. */
  10133. if (new_crtc_state->active)
  10134. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10135. /*
  10136. * Whenever the number of active pipes changes, we need to make sure we
  10137. * update the pipes in the right order so that their ddb allocations
  10138. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10139. * cause pipe underruns and other bad stuff.
  10140. */
  10141. do {
  10142. progress = false;
  10143. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10144. bool vbl_wait = false;
  10145. unsigned int cmask = drm_crtc_mask(crtc);
  10146. intel_crtc = to_intel_crtc(crtc);
  10147. cstate = to_intel_crtc_state(new_crtc_state);
  10148. pipe = intel_crtc->pipe;
  10149. if (updated & cmask || !cstate->base.active)
  10150. continue;
  10151. if (skl_ddb_allocation_overlaps(dev_priv,
  10152. entries,
  10153. &cstate->wm.skl.ddb,
  10154. i))
  10155. continue;
  10156. updated |= cmask;
  10157. entries[i] = &cstate->wm.skl.ddb;
  10158. /*
  10159. * If this is an already active pipe, it's DDB changed,
  10160. * and this isn't the last pipe that needs updating
  10161. * then we need to wait for a vblank to pass for the
  10162. * new ddb allocation to take effect.
  10163. */
  10164. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10165. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10166. !new_crtc_state->active_changed &&
  10167. intel_state->wm_results.dirty_pipes != updated)
  10168. vbl_wait = true;
  10169. intel_update_crtc(crtc, state, old_crtc_state,
  10170. new_crtc_state);
  10171. if (vbl_wait)
  10172. intel_wait_for_vblank(dev_priv, pipe);
  10173. progress = true;
  10174. }
  10175. } while (progress);
  10176. }
  10177. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10178. {
  10179. struct intel_atomic_state *state, *next;
  10180. struct llist_node *freed;
  10181. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10182. llist_for_each_entry_safe(state, next, freed, freed)
  10183. drm_atomic_state_put(&state->base);
  10184. }
  10185. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10186. {
  10187. struct drm_i915_private *dev_priv =
  10188. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10189. intel_atomic_helper_free_state(dev_priv);
  10190. }
  10191. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10192. {
  10193. struct wait_queue_entry wait_fence, wait_reset;
  10194. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10195. init_wait_entry(&wait_fence, 0);
  10196. init_wait_entry(&wait_reset, 0);
  10197. for (;;) {
  10198. prepare_to_wait(&intel_state->commit_ready.wait,
  10199. &wait_fence, TASK_UNINTERRUPTIBLE);
  10200. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10201. &wait_reset, TASK_UNINTERRUPTIBLE);
  10202. if (i915_sw_fence_done(&intel_state->commit_ready)
  10203. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10204. break;
  10205. schedule();
  10206. }
  10207. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10208. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10209. }
  10210. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10211. {
  10212. struct drm_device *dev = state->dev;
  10213. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10214. struct drm_i915_private *dev_priv = to_i915(dev);
  10215. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10216. struct drm_crtc *crtc;
  10217. struct intel_crtc_state *intel_cstate;
  10218. u64 put_domains[I915_MAX_PIPES] = {};
  10219. int i;
  10220. intel_atomic_commit_fence_wait(intel_state);
  10221. drm_atomic_helper_wait_for_dependencies(state);
  10222. if (intel_state->modeset)
  10223. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10224. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10226. if (needs_modeset(new_crtc_state) ||
  10227. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10228. put_domains[to_intel_crtc(crtc)->pipe] =
  10229. modeset_get_crtc_power_domains(crtc,
  10230. to_intel_crtc_state(new_crtc_state));
  10231. }
  10232. if (!needs_modeset(new_crtc_state))
  10233. continue;
  10234. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10235. to_intel_crtc_state(new_crtc_state));
  10236. if (old_crtc_state->active) {
  10237. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10238. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10239. intel_crtc->active = false;
  10240. intel_fbc_disable(intel_crtc);
  10241. intel_disable_shared_dpll(intel_crtc);
  10242. /*
  10243. * Underruns don't always raise
  10244. * interrupts, so check manually.
  10245. */
  10246. intel_check_cpu_fifo_underruns(dev_priv);
  10247. intel_check_pch_fifo_underruns(dev_priv);
  10248. if (!new_crtc_state->active) {
  10249. /*
  10250. * Make sure we don't call initial_watermarks
  10251. * for ILK-style watermark updates.
  10252. *
  10253. * No clue what this is supposed to achieve.
  10254. */
  10255. if (INTEL_GEN(dev_priv) >= 9)
  10256. dev_priv->display.initial_watermarks(intel_state,
  10257. to_intel_crtc_state(new_crtc_state));
  10258. }
  10259. }
  10260. }
  10261. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10262. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10263. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10264. if (intel_state->modeset) {
  10265. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10266. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10267. /*
  10268. * SKL workaround: bspec recommends we disable the SAGV when we
  10269. * have more then one pipe enabled
  10270. */
  10271. if (!intel_can_enable_sagv(state))
  10272. intel_disable_sagv(dev_priv);
  10273. intel_modeset_verify_disabled(dev, state);
  10274. }
  10275. /* Complete the events for pipes that have now been disabled */
  10276. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10277. bool modeset = needs_modeset(new_crtc_state);
  10278. /* Complete events for now disable pipes here. */
  10279. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10280. spin_lock_irq(&dev->event_lock);
  10281. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10282. spin_unlock_irq(&dev->event_lock);
  10283. new_crtc_state->event = NULL;
  10284. }
  10285. }
  10286. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10287. dev_priv->display.update_crtcs(state);
  10288. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10289. * already, but still need the state for the delayed optimization. To
  10290. * fix this:
  10291. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10292. * - schedule that vblank worker _before_ calling hw_done
  10293. * - at the start of commit_tail, cancel it _synchrously
  10294. * - switch over to the vblank wait helper in the core after that since
  10295. * we don't need out special handling any more.
  10296. */
  10297. drm_atomic_helper_wait_for_flip_done(dev, state);
  10298. /*
  10299. * Now that the vblank has passed, we can go ahead and program the
  10300. * optimal watermarks on platforms that need two-step watermark
  10301. * programming.
  10302. *
  10303. * TODO: Move this (and other cleanup) to an async worker eventually.
  10304. */
  10305. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10306. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10307. if (dev_priv->display.optimize_watermarks)
  10308. dev_priv->display.optimize_watermarks(intel_state,
  10309. intel_cstate);
  10310. }
  10311. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10312. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10313. if (put_domains[i])
  10314. modeset_put_power_domains(dev_priv, put_domains[i]);
  10315. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10316. }
  10317. if (intel_state->modeset)
  10318. intel_verify_planes(intel_state);
  10319. if (intel_state->modeset && intel_can_enable_sagv(state))
  10320. intel_enable_sagv(dev_priv);
  10321. drm_atomic_helper_commit_hw_done(state);
  10322. if (intel_state->modeset) {
  10323. /* As one of the primary mmio accessors, KMS has a high
  10324. * likelihood of triggering bugs in unclaimed access. After we
  10325. * finish modesetting, see if an error has been flagged, and if
  10326. * so enable debugging for the next modeset - and hope we catch
  10327. * the culprit.
  10328. */
  10329. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10330. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10331. }
  10332. drm_atomic_helper_cleanup_planes(dev, state);
  10333. drm_atomic_helper_commit_cleanup_done(state);
  10334. drm_atomic_state_put(state);
  10335. intel_atomic_helper_free_state(dev_priv);
  10336. }
  10337. static void intel_atomic_commit_work(struct work_struct *work)
  10338. {
  10339. struct drm_atomic_state *state =
  10340. container_of(work, struct drm_atomic_state, commit_work);
  10341. intel_atomic_commit_tail(state);
  10342. }
  10343. static int __i915_sw_fence_call
  10344. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10345. enum i915_sw_fence_notify notify)
  10346. {
  10347. struct intel_atomic_state *state =
  10348. container_of(fence, struct intel_atomic_state, commit_ready);
  10349. switch (notify) {
  10350. case FENCE_COMPLETE:
  10351. /* we do blocking waits in the worker, nothing to do here */
  10352. break;
  10353. case FENCE_FREE:
  10354. {
  10355. struct intel_atomic_helper *helper =
  10356. &to_i915(state->base.dev)->atomic_helper;
  10357. if (llist_add(&state->freed, &helper->free_list))
  10358. schedule_work(&helper->free_work);
  10359. break;
  10360. }
  10361. }
  10362. return NOTIFY_DONE;
  10363. }
  10364. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10365. {
  10366. struct drm_plane_state *old_plane_state, *new_plane_state;
  10367. struct drm_plane *plane;
  10368. int i;
  10369. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10370. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10371. intel_fb_obj(new_plane_state->fb),
  10372. to_intel_plane(plane)->frontbuffer_bit);
  10373. }
  10374. /**
  10375. * intel_atomic_commit - commit validated state object
  10376. * @dev: DRM device
  10377. * @state: the top-level driver state object
  10378. * @nonblock: nonblocking commit
  10379. *
  10380. * This function commits a top-level state object that has been validated
  10381. * with drm_atomic_helper_check().
  10382. *
  10383. * RETURNS
  10384. * Zero for success or -errno.
  10385. */
  10386. static int intel_atomic_commit(struct drm_device *dev,
  10387. struct drm_atomic_state *state,
  10388. bool nonblock)
  10389. {
  10390. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10391. struct drm_i915_private *dev_priv = to_i915(dev);
  10392. int ret = 0;
  10393. drm_atomic_state_get(state);
  10394. i915_sw_fence_init(&intel_state->commit_ready,
  10395. intel_atomic_commit_ready);
  10396. /*
  10397. * The intel_legacy_cursor_update() fast path takes care
  10398. * of avoiding the vblank waits for simple cursor
  10399. * movement and flips. For cursor on/off and size changes,
  10400. * we want to perform the vblank waits so that watermark
  10401. * updates happen during the correct frames. Gen9+ have
  10402. * double buffered watermarks and so shouldn't need this.
  10403. *
  10404. * Unset state->legacy_cursor_update before the call to
  10405. * drm_atomic_helper_setup_commit() because otherwise
  10406. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10407. * we get FIFO underruns because we didn't wait
  10408. * for vblank.
  10409. *
  10410. * FIXME doing watermarks and fb cleanup from a vblank worker
  10411. * (assuming we had any) would solve these problems.
  10412. */
  10413. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10414. struct intel_crtc_state *new_crtc_state;
  10415. struct intel_crtc *crtc;
  10416. int i;
  10417. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10418. if (new_crtc_state->wm.need_postvbl_update ||
  10419. new_crtc_state->update_wm_post)
  10420. state->legacy_cursor_update = false;
  10421. }
  10422. ret = intel_atomic_prepare_commit(dev, state);
  10423. if (ret) {
  10424. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10425. i915_sw_fence_commit(&intel_state->commit_ready);
  10426. return ret;
  10427. }
  10428. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10429. if (!ret)
  10430. ret = drm_atomic_helper_swap_state(state, true);
  10431. if (ret) {
  10432. i915_sw_fence_commit(&intel_state->commit_ready);
  10433. drm_atomic_helper_cleanup_planes(dev, state);
  10434. return ret;
  10435. }
  10436. dev_priv->wm.distrust_bios_wm = false;
  10437. intel_shared_dpll_swap_state(state);
  10438. intel_atomic_track_fbs(state);
  10439. if (intel_state->modeset) {
  10440. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10441. sizeof(intel_state->min_cdclk));
  10442. memcpy(dev_priv->min_voltage_level,
  10443. intel_state->min_voltage_level,
  10444. sizeof(intel_state->min_voltage_level));
  10445. dev_priv->active_crtcs = intel_state->active_crtcs;
  10446. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10447. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10448. }
  10449. drm_atomic_state_get(state);
  10450. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10451. i915_sw_fence_commit(&intel_state->commit_ready);
  10452. if (nonblock && intel_state->modeset) {
  10453. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10454. } else if (nonblock) {
  10455. queue_work(system_unbound_wq, &state->commit_work);
  10456. } else {
  10457. if (intel_state->modeset)
  10458. flush_workqueue(dev_priv->modeset_wq);
  10459. intel_atomic_commit_tail(state);
  10460. }
  10461. return 0;
  10462. }
  10463. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10464. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10465. .set_config = drm_atomic_helper_set_config,
  10466. .destroy = intel_crtc_destroy,
  10467. .page_flip = drm_atomic_helper_page_flip,
  10468. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10469. .atomic_destroy_state = intel_crtc_destroy_state,
  10470. .set_crc_source = intel_crtc_set_crc_source,
  10471. };
  10472. struct wait_rps_boost {
  10473. struct wait_queue_entry wait;
  10474. struct drm_crtc *crtc;
  10475. struct drm_i915_gem_request *request;
  10476. };
  10477. static int do_rps_boost(struct wait_queue_entry *_wait,
  10478. unsigned mode, int sync, void *key)
  10479. {
  10480. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10481. struct drm_i915_gem_request *rq = wait->request;
  10482. /*
  10483. * If we missed the vblank, but the request is already running it
  10484. * is reasonable to assume that it will complete before the next
  10485. * vblank without our intervention, so leave RPS alone.
  10486. */
  10487. if (!i915_gem_request_started(rq))
  10488. gen6_rps_boost(rq, NULL);
  10489. i915_gem_request_put(rq);
  10490. drm_crtc_vblank_put(wait->crtc);
  10491. list_del(&wait->wait.entry);
  10492. kfree(wait);
  10493. return 1;
  10494. }
  10495. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10496. struct dma_fence *fence)
  10497. {
  10498. struct wait_rps_boost *wait;
  10499. if (!dma_fence_is_i915(fence))
  10500. return;
  10501. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10502. return;
  10503. if (drm_crtc_vblank_get(crtc))
  10504. return;
  10505. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10506. if (!wait) {
  10507. drm_crtc_vblank_put(crtc);
  10508. return;
  10509. }
  10510. wait->request = to_request(dma_fence_get(fence));
  10511. wait->crtc = crtc;
  10512. wait->wait.func = do_rps_boost;
  10513. wait->wait.flags = 0;
  10514. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10515. }
  10516. /**
  10517. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10518. * @plane: drm plane to prepare for
  10519. * @fb: framebuffer to prepare for presentation
  10520. *
  10521. * Prepares a framebuffer for usage on a display plane. Generally this
  10522. * involves pinning the underlying object and updating the frontbuffer tracking
  10523. * bits. Some older platforms need special physical address handling for
  10524. * cursor planes.
  10525. *
  10526. * Must be called with struct_mutex held.
  10527. *
  10528. * Returns 0 on success, negative error code on failure.
  10529. */
  10530. int
  10531. intel_prepare_plane_fb(struct drm_plane *plane,
  10532. struct drm_plane_state *new_state)
  10533. {
  10534. struct intel_atomic_state *intel_state =
  10535. to_intel_atomic_state(new_state->state);
  10536. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10537. struct drm_framebuffer *fb = new_state->fb;
  10538. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10539. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10540. int ret;
  10541. if (old_obj) {
  10542. struct drm_crtc_state *crtc_state =
  10543. drm_atomic_get_existing_crtc_state(new_state->state,
  10544. plane->state->crtc);
  10545. /* Big Hammer, we also need to ensure that any pending
  10546. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10547. * current scanout is retired before unpinning the old
  10548. * framebuffer. Note that we rely on userspace rendering
  10549. * into the buffer attached to the pipe they are waiting
  10550. * on. If not, userspace generates a GPU hang with IPEHR
  10551. * point to the MI_WAIT_FOR_EVENT.
  10552. *
  10553. * This should only fail upon a hung GPU, in which case we
  10554. * can safely continue.
  10555. */
  10556. if (needs_modeset(crtc_state)) {
  10557. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10558. old_obj->resv, NULL,
  10559. false, 0,
  10560. GFP_KERNEL);
  10561. if (ret < 0)
  10562. return ret;
  10563. }
  10564. }
  10565. if (new_state->fence) { /* explicit fencing */
  10566. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10567. new_state->fence,
  10568. I915_FENCE_TIMEOUT,
  10569. GFP_KERNEL);
  10570. if (ret < 0)
  10571. return ret;
  10572. }
  10573. if (!obj)
  10574. return 0;
  10575. ret = i915_gem_object_pin_pages(obj);
  10576. if (ret)
  10577. return ret;
  10578. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10579. if (ret) {
  10580. i915_gem_object_unpin_pages(obj);
  10581. return ret;
  10582. }
  10583. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10584. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10585. const int align = intel_cursor_alignment(dev_priv);
  10586. ret = i915_gem_object_attach_phys(obj, align);
  10587. } else {
  10588. struct i915_vma *vma;
  10589. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10590. if (!IS_ERR(vma))
  10591. to_intel_plane_state(new_state)->vma = vma;
  10592. else
  10593. ret = PTR_ERR(vma);
  10594. }
  10595. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10596. mutex_unlock(&dev_priv->drm.struct_mutex);
  10597. i915_gem_object_unpin_pages(obj);
  10598. if (ret)
  10599. return ret;
  10600. if (!new_state->fence) { /* implicit fencing */
  10601. struct dma_fence *fence;
  10602. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10603. obj->resv, NULL,
  10604. false, I915_FENCE_TIMEOUT,
  10605. GFP_KERNEL);
  10606. if (ret < 0)
  10607. return ret;
  10608. fence = reservation_object_get_excl_rcu(obj->resv);
  10609. if (fence) {
  10610. add_rps_boost_after_vblank(new_state->crtc, fence);
  10611. dma_fence_put(fence);
  10612. }
  10613. } else {
  10614. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10615. }
  10616. return 0;
  10617. }
  10618. /**
  10619. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10620. * @plane: drm plane to clean up for
  10621. * @fb: old framebuffer that was on plane
  10622. *
  10623. * Cleans up a framebuffer that has just been removed from a plane.
  10624. *
  10625. * Must be called with struct_mutex held.
  10626. */
  10627. void
  10628. intel_cleanup_plane_fb(struct drm_plane *plane,
  10629. struct drm_plane_state *old_state)
  10630. {
  10631. struct i915_vma *vma;
  10632. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10633. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10634. if (vma) {
  10635. mutex_lock(&plane->dev->struct_mutex);
  10636. intel_unpin_fb_vma(vma);
  10637. mutex_unlock(&plane->dev->struct_mutex);
  10638. }
  10639. }
  10640. int
  10641. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10642. {
  10643. struct drm_i915_private *dev_priv;
  10644. int max_scale;
  10645. int crtc_clock, max_dotclk;
  10646. if (!intel_crtc || !crtc_state->base.enable)
  10647. return DRM_PLANE_HELPER_NO_SCALING;
  10648. dev_priv = to_i915(intel_crtc->base.dev);
  10649. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10650. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10651. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10652. max_dotclk *= 2;
  10653. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10654. return DRM_PLANE_HELPER_NO_SCALING;
  10655. /*
  10656. * skl max scale is lower of:
  10657. * close to 3 but not 3, -1 is for that purpose
  10658. * or
  10659. * cdclk/crtc_clock
  10660. */
  10661. max_scale = min((1 << 16) * 3 - 1,
  10662. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10663. return max_scale;
  10664. }
  10665. static int
  10666. intel_check_primary_plane(struct intel_plane *plane,
  10667. struct intel_crtc_state *crtc_state,
  10668. struct intel_plane_state *state)
  10669. {
  10670. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10671. struct drm_crtc *crtc = state->base.crtc;
  10672. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10673. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10674. bool can_position = false;
  10675. int ret;
  10676. if (INTEL_GEN(dev_priv) >= 9) {
  10677. /* use scaler when colorkey is not required */
  10678. if (!state->ckey.flags) {
  10679. min_scale = 1;
  10680. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10681. }
  10682. can_position = true;
  10683. }
  10684. ret = drm_atomic_helper_check_plane_state(&state->base,
  10685. &crtc_state->base,
  10686. &state->clip,
  10687. min_scale, max_scale,
  10688. can_position, true);
  10689. if (ret)
  10690. return ret;
  10691. if (!state->base.fb)
  10692. return 0;
  10693. if (INTEL_GEN(dev_priv) >= 9) {
  10694. ret = skl_check_plane_surface(crtc_state, state);
  10695. if (ret)
  10696. return ret;
  10697. state->ctl = skl_plane_ctl(crtc_state, state);
  10698. } else {
  10699. ret = i9xx_check_plane_surface(state);
  10700. if (ret)
  10701. return ret;
  10702. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10703. }
  10704. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10705. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10706. return 0;
  10707. }
  10708. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10709. struct drm_crtc_state *old_crtc_state)
  10710. {
  10711. struct drm_device *dev = crtc->dev;
  10712. struct drm_i915_private *dev_priv = to_i915(dev);
  10713. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10714. struct intel_crtc_state *old_intel_cstate =
  10715. to_intel_crtc_state(old_crtc_state);
  10716. struct intel_atomic_state *old_intel_state =
  10717. to_intel_atomic_state(old_crtc_state->state);
  10718. struct intel_crtc_state *intel_cstate =
  10719. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10720. bool modeset = needs_modeset(&intel_cstate->base);
  10721. if (!modeset &&
  10722. (intel_cstate->base.color_mgmt_changed ||
  10723. intel_cstate->update_pipe)) {
  10724. intel_color_set_csc(&intel_cstate->base);
  10725. intel_color_load_luts(&intel_cstate->base);
  10726. }
  10727. /* Perform vblank evasion around commit operation */
  10728. intel_pipe_update_start(intel_cstate);
  10729. if (modeset)
  10730. goto out;
  10731. if (intel_cstate->update_pipe)
  10732. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10733. else if (INTEL_GEN(dev_priv) >= 9)
  10734. skl_detach_scalers(intel_crtc);
  10735. out:
  10736. if (dev_priv->display.atomic_update_watermarks)
  10737. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10738. intel_cstate);
  10739. }
  10740. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10741. struct drm_crtc_state *old_crtc_state)
  10742. {
  10743. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10745. struct intel_atomic_state *old_intel_state =
  10746. to_intel_atomic_state(old_crtc_state->state);
  10747. struct intel_crtc_state *new_crtc_state =
  10748. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10749. intel_pipe_update_end(new_crtc_state);
  10750. if (new_crtc_state->update_pipe &&
  10751. !needs_modeset(&new_crtc_state->base) &&
  10752. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED) {
  10753. if (!IS_GEN2(dev_priv))
  10754. intel_set_cpu_fifo_underrun_reporting(dev_priv, intel_crtc->pipe, true);
  10755. if (new_crtc_state->has_pch_encoder) {
  10756. enum pipe pch_transcoder =
  10757. intel_crtc_pch_transcoder(intel_crtc);
  10758. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10759. }
  10760. }
  10761. }
  10762. /**
  10763. * intel_plane_destroy - destroy a plane
  10764. * @plane: plane to destroy
  10765. *
  10766. * Common destruction function for all types of planes (primary, cursor,
  10767. * sprite).
  10768. */
  10769. void intel_plane_destroy(struct drm_plane *plane)
  10770. {
  10771. drm_plane_cleanup(plane);
  10772. kfree(to_intel_plane(plane));
  10773. }
  10774. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10775. {
  10776. switch (format) {
  10777. case DRM_FORMAT_C8:
  10778. case DRM_FORMAT_RGB565:
  10779. case DRM_FORMAT_XRGB1555:
  10780. case DRM_FORMAT_XRGB8888:
  10781. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10782. modifier == I915_FORMAT_MOD_X_TILED;
  10783. default:
  10784. return false;
  10785. }
  10786. }
  10787. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10788. {
  10789. switch (format) {
  10790. case DRM_FORMAT_C8:
  10791. case DRM_FORMAT_RGB565:
  10792. case DRM_FORMAT_XRGB8888:
  10793. case DRM_FORMAT_XBGR8888:
  10794. case DRM_FORMAT_XRGB2101010:
  10795. case DRM_FORMAT_XBGR2101010:
  10796. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10797. modifier == I915_FORMAT_MOD_X_TILED;
  10798. default:
  10799. return false;
  10800. }
  10801. }
  10802. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10803. {
  10804. switch (format) {
  10805. case DRM_FORMAT_XRGB8888:
  10806. case DRM_FORMAT_XBGR8888:
  10807. case DRM_FORMAT_ARGB8888:
  10808. case DRM_FORMAT_ABGR8888:
  10809. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10810. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10811. return true;
  10812. /* fall through */
  10813. case DRM_FORMAT_RGB565:
  10814. case DRM_FORMAT_XRGB2101010:
  10815. case DRM_FORMAT_XBGR2101010:
  10816. case DRM_FORMAT_YUYV:
  10817. case DRM_FORMAT_YVYU:
  10818. case DRM_FORMAT_UYVY:
  10819. case DRM_FORMAT_VYUY:
  10820. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10821. return true;
  10822. /* fall through */
  10823. case DRM_FORMAT_C8:
  10824. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10825. modifier == I915_FORMAT_MOD_X_TILED ||
  10826. modifier == I915_FORMAT_MOD_Y_TILED)
  10827. return true;
  10828. /* fall through */
  10829. default:
  10830. return false;
  10831. }
  10832. }
  10833. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10834. uint32_t format,
  10835. uint64_t modifier)
  10836. {
  10837. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10838. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10839. return false;
  10840. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10841. modifier != DRM_FORMAT_MOD_LINEAR)
  10842. return false;
  10843. if (INTEL_GEN(dev_priv) >= 9)
  10844. return skl_mod_supported(format, modifier);
  10845. else if (INTEL_GEN(dev_priv) >= 4)
  10846. return i965_mod_supported(format, modifier);
  10847. else
  10848. return i8xx_mod_supported(format, modifier);
  10849. }
  10850. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10851. uint32_t format,
  10852. uint64_t modifier)
  10853. {
  10854. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10855. return false;
  10856. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10857. }
  10858. static struct drm_plane_funcs intel_plane_funcs = {
  10859. .update_plane = drm_atomic_helper_update_plane,
  10860. .disable_plane = drm_atomic_helper_disable_plane,
  10861. .destroy = intel_plane_destroy,
  10862. .atomic_get_property = intel_plane_atomic_get_property,
  10863. .atomic_set_property = intel_plane_atomic_set_property,
  10864. .atomic_duplicate_state = intel_plane_duplicate_state,
  10865. .atomic_destroy_state = intel_plane_destroy_state,
  10866. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10867. };
  10868. static int
  10869. intel_legacy_cursor_update(struct drm_plane *plane,
  10870. struct drm_crtc *crtc,
  10871. struct drm_framebuffer *fb,
  10872. int crtc_x, int crtc_y,
  10873. unsigned int crtc_w, unsigned int crtc_h,
  10874. uint32_t src_x, uint32_t src_y,
  10875. uint32_t src_w, uint32_t src_h,
  10876. struct drm_modeset_acquire_ctx *ctx)
  10877. {
  10878. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10879. int ret;
  10880. struct drm_plane_state *old_plane_state, *new_plane_state;
  10881. struct intel_plane *intel_plane = to_intel_plane(plane);
  10882. struct drm_framebuffer *old_fb;
  10883. struct drm_crtc_state *crtc_state = crtc->state;
  10884. struct i915_vma *old_vma, *vma;
  10885. /*
  10886. * When crtc is inactive or there is a modeset pending,
  10887. * wait for it to complete in the slowpath
  10888. */
  10889. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10890. to_intel_crtc_state(crtc_state)->update_pipe)
  10891. goto slow;
  10892. old_plane_state = plane->state;
  10893. /*
  10894. * Don't do an async update if there is an outstanding commit modifying
  10895. * the plane. This prevents our async update's changes from getting
  10896. * overridden by a previous synchronous update's state.
  10897. */
  10898. if (old_plane_state->commit &&
  10899. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  10900. goto slow;
  10901. /*
  10902. * If any parameters change that may affect watermarks,
  10903. * take the slowpath. Only changing fb or position should be
  10904. * in the fastpath.
  10905. */
  10906. if (old_plane_state->crtc != crtc ||
  10907. old_plane_state->src_w != src_w ||
  10908. old_plane_state->src_h != src_h ||
  10909. old_plane_state->crtc_w != crtc_w ||
  10910. old_plane_state->crtc_h != crtc_h ||
  10911. !old_plane_state->fb != !fb)
  10912. goto slow;
  10913. new_plane_state = intel_plane_duplicate_state(plane);
  10914. if (!new_plane_state)
  10915. return -ENOMEM;
  10916. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10917. new_plane_state->src_x = src_x;
  10918. new_plane_state->src_y = src_y;
  10919. new_plane_state->src_w = src_w;
  10920. new_plane_state->src_h = src_h;
  10921. new_plane_state->crtc_x = crtc_x;
  10922. new_plane_state->crtc_y = crtc_y;
  10923. new_plane_state->crtc_w = crtc_w;
  10924. new_plane_state->crtc_h = crtc_h;
  10925. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10926. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  10927. to_intel_plane_state(plane->state),
  10928. to_intel_plane_state(new_plane_state));
  10929. if (ret)
  10930. goto out_free;
  10931. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10932. if (ret)
  10933. goto out_free;
  10934. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10935. int align = intel_cursor_alignment(dev_priv);
  10936. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10937. if (ret) {
  10938. DRM_DEBUG_KMS("failed to attach phys object\n");
  10939. goto out_unlock;
  10940. }
  10941. } else {
  10942. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10943. if (IS_ERR(vma)) {
  10944. DRM_DEBUG_KMS("failed to pin object\n");
  10945. ret = PTR_ERR(vma);
  10946. goto out_unlock;
  10947. }
  10948. to_intel_plane_state(new_plane_state)->vma = vma;
  10949. }
  10950. old_fb = old_plane_state->fb;
  10951. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10952. intel_plane->frontbuffer_bit);
  10953. /* Swap plane state */
  10954. plane->state = new_plane_state;
  10955. if (plane->state->visible) {
  10956. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10957. intel_plane->update_plane(intel_plane,
  10958. to_intel_crtc_state(crtc->state),
  10959. to_intel_plane_state(plane->state));
  10960. } else {
  10961. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10962. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10963. }
  10964. old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
  10965. if (old_vma)
  10966. intel_unpin_fb_vma(old_vma);
  10967. out_unlock:
  10968. mutex_unlock(&dev_priv->drm.struct_mutex);
  10969. out_free:
  10970. if (ret)
  10971. intel_plane_destroy_state(plane, new_plane_state);
  10972. else
  10973. intel_plane_destroy_state(plane, old_plane_state);
  10974. return ret;
  10975. slow:
  10976. return drm_atomic_helper_update_plane(plane, crtc, fb,
  10977. crtc_x, crtc_y, crtc_w, crtc_h,
  10978. src_x, src_y, src_w, src_h, ctx);
  10979. }
  10980. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10981. .update_plane = intel_legacy_cursor_update,
  10982. .disable_plane = drm_atomic_helper_disable_plane,
  10983. .destroy = intel_plane_destroy,
  10984. .atomic_get_property = intel_plane_atomic_get_property,
  10985. .atomic_set_property = intel_plane_atomic_set_property,
  10986. .atomic_duplicate_state = intel_plane_duplicate_state,
  10987. .atomic_destroy_state = intel_plane_destroy_state,
  10988. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  10989. };
  10990. static struct intel_plane *
  10991. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  10992. {
  10993. struct intel_plane *primary = NULL;
  10994. struct intel_plane_state *state = NULL;
  10995. const uint32_t *intel_primary_formats;
  10996. unsigned int supported_rotations;
  10997. unsigned int num_formats;
  10998. const uint64_t *modifiers;
  10999. int ret;
  11000. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11001. if (!primary) {
  11002. ret = -ENOMEM;
  11003. goto fail;
  11004. }
  11005. state = intel_create_plane_state(&primary->base);
  11006. if (!state) {
  11007. ret = -ENOMEM;
  11008. goto fail;
  11009. }
  11010. primary->base.state = &state->base;
  11011. primary->can_scale = false;
  11012. primary->max_downscale = 1;
  11013. if (INTEL_GEN(dev_priv) >= 9) {
  11014. primary->can_scale = true;
  11015. state->scaler_id = -1;
  11016. }
  11017. primary->pipe = pipe;
  11018. /*
  11019. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11020. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11021. */
  11022. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11023. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11024. else
  11025. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11026. primary->id = PLANE_PRIMARY;
  11027. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11028. primary->check_plane = intel_check_primary_plane;
  11029. if (INTEL_GEN(dev_priv) >= 9) {
  11030. intel_primary_formats = skl_primary_formats;
  11031. num_formats = ARRAY_SIZE(skl_primary_formats);
  11032. if (skl_plane_has_ccs(dev_priv, pipe, PLANE_PRIMARY))
  11033. modifiers = skl_format_modifiers_ccs;
  11034. else
  11035. modifiers = skl_format_modifiers_noccs;
  11036. primary->update_plane = skl_update_plane;
  11037. primary->disable_plane = skl_disable_plane;
  11038. primary->get_hw_state = skl_plane_get_hw_state;
  11039. } else if (INTEL_GEN(dev_priv) >= 4) {
  11040. intel_primary_formats = i965_primary_formats;
  11041. num_formats = ARRAY_SIZE(i965_primary_formats);
  11042. modifiers = i9xx_format_modifiers;
  11043. primary->update_plane = i9xx_update_plane;
  11044. primary->disable_plane = i9xx_disable_plane;
  11045. primary->get_hw_state = i9xx_plane_get_hw_state;
  11046. } else {
  11047. intel_primary_formats = i8xx_primary_formats;
  11048. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11049. modifiers = i9xx_format_modifiers;
  11050. primary->update_plane = i9xx_update_plane;
  11051. primary->disable_plane = i9xx_disable_plane;
  11052. primary->get_hw_state = i9xx_plane_get_hw_state;
  11053. }
  11054. if (INTEL_GEN(dev_priv) >= 9)
  11055. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11056. 0, &intel_plane_funcs,
  11057. intel_primary_formats, num_formats,
  11058. modifiers,
  11059. DRM_PLANE_TYPE_PRIMARY,
  11060. "plane 1%c", pipe_name(pipe));
  11061. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11062. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11063. 0, &intel_plane_funcs,
  11064. intel_primary_formats, num_formats,
  11065. modifiers,
  11066. DRM_PLANE_TYPE_PRIMARY,
  11067. "primary %c", pipe_name(pipe));
  11068. else
  11069. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11070. 0, &intel_plane_funcs,
  11071. intel_primary_formats, num_formats,
  11072. modifiers,
  11073. DRM_PLANE_TYPE_PRIMARY,
  11074. "plane %c",
  11075. plane_name(primary->i9xx_plane));
  11076. if (ret)
  11077. goto fail;
  11078. if (INTEL_GEN(dev_priv) >= 10) {
  11079. supported_rotations =
  11080. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11081. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11082. DRM_MODE_REFLECT_X;
  11083. } else if (INTEL_GEN(dev_priv) >= 9) {
  11084. supported_rotations =
  11085. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11086. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11087. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11088. supported_rotations =
  11089. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11090. DRM_MODE_REFLECT_X;
  11091. } else if (INTEL_GEN(dev_priv) >= 4) {
  11092. supported_rotations =
  11093. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11094. } else {
  11095. supported_rotations = DRM_MODE_ROTATE_0;
  11096. }
  11097. if (INTEL_GEN(dev_priv) >= 4)
  11098. drm_plane_create_rotation_property(&primary->base,
  11099. DRM_MODE_ROTATE_0,
  11100. supported_rotations);
  11101. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11102. return primary;
  11103. fail:
  11104. kfree(state);
  11105. kfree(primary);
  11106. return ERR_PTR(ret);
  11107. }
  11108. static struct intel_plane *
  11109. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11110. enum pipe pipe)
  11111. {
  11112. struct intel_plane *cursor = NULL;
  11113. struct intel_plane_state *state = NULL;
  11114. int ret;
  11115. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11116. if (!cursor) {
  11117. ret = -ENOMEM;
  11118. goto fail;
  11119. }
  11120. state = intel_create_plane_state(&cursor->base);
  11121. if (!state) {
  11122. ret = -ENOMEM;
  11123. goto fail;
  11124. }
  11125. cursor->base.state = &state->base;
  11126. cursor->can_scale = false;
  11127. cursor->max_downscale = 1;
  11128. cursor->pipe = pipe;
  11129. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11130. cursor->id = PLANE_CURSOR;
  11131. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11132. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11133. cursor->update_plane = i845_update_cursor;
  11134. cursor->disable_plane = i845_disable_cursor;
  11135. cursor->get_hw_state = i845_cursor_get_hw_state;
  11136. cursor->check_plane = i845_check_cursor;
  11137. } else {
  11138. cursor->update_plane = i9xx_update_cursor;
  11139. cursor->disable_plane = i9xx_disable_cursor;
  11140. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11141. cursor->check_plane = i9xx_check_cursor;
  11142. }
  11143. cursor->cursor.base = ~0;
  11144. cursor->cursor.cntl = ~0;
  11145. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11146. cursor->cursor.size = ~0;
  11147. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11148. 0, &intel_cursor_plane_funcs,
  11149. intel_cursor_formats,
  11150. ARRAY_SIZE(intel_cursor_formats),
  11151. cursor_format_modifiers,
  11152. DRM_PLANE_TYPE_CURSOR,
  11153. "cursor %c", pipe_name(pipe));
  11154. if (ret)
  11155. goto fail;
  11156. if (INTEL_GEN(dev_priv) >= 4)
  11157. drm_plane_create_rotation_property(&cursor->base,
  11158. DRM_MODE_ROTATE_0,
  11159. DRM_MODE_ROTATE_0 |
  11160. DRM_MODE_ROTATE_180);
  11161. if (INTEL_GEN(dev_priv) >= 9)
  11162. state->scaler_id = -1;
  11163. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11164. return cursor;
  11165. fail:
  11166. kfree(state);
  11167. kfree(cursor);
  11168. return ERR_PTR(ret);
  11169. }
  11170. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11171. struct intel_crtc_state *crtc_state)
  11172. {
  11173. struct intel_crtc_scaler_state *scaler_state =
  11174. &crtc_state->scaler_state;
  11175. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11176. int i;
  11177. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11178. if (!crtc->num_scalers)
  11179. return;
  11180. for (i = 0; i < crtc->num_scalers; i++) {
  11181. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11182. scaler->in_use = 0;
  11183. scaler->mode = PS_SCALER_MODE_DYN;
  11184. }
  11185. scaler_state->scaler_id = -1;
  11186. }
  11187. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11188. {
  11189. struct intel_crtc *intel_crtc;
  11190. struct intel_crtc_state *crtc_state = NULL;
  11191. struct intel_plane *primary = NULL;
  11192. struct intel_plane *cursor = NULL;
  11193. int sprite, ret;
  11194. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11195. if (!intel_crtc)
  11196. return -ENOMEM;
  11197. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11198. if (!crtc_state) {
  11199. ret = -ENOMEM;
  11200. goto fail;
  11201. }
  11202. intel_crtc->config = crtc_state;
  11203. intel_crtc->base.state = &crtc_state->base;
  11204. crtc_state->base.crtc = &intel_crtc->base;
  11205. primary = intel_primary_plane_create(dev_priv, pipe);
  11206. if (IS_ERR(primary)) {
  11207. ret = PTR_ERR(primary);
  11208. goto fail;
  11209. }
  11210. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11211. for_each_sprite(dev_priv, pipe, sprite) {
  11212. struct intel_plane *plane;
  11213. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11214. if (IS_ERR(plane)) {
  11215. ret = PTR_ERR(plane);
  11216. goto fail;
  11217. }
  11218. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11219. }
  11220. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11221. if (IS_ERR(cursor)) {
  11222. ret = PTR_ERR(cursor);
  11223. goto fail;
  11224. }
  11225. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11226. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11227. &primary->base, &cursor->base,
  11228. &intel_crtc_funcs,
  11229. "pipe %c", pipe_name(pipe));
  11230. if (ret)
  11231. goto fail;
  11232. intel_crtc->pipe = pipe;
  11233. /* initialize shared scalers */
  11234. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11235. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11236. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] != NULL);
  11237. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] = intel_crtc;
  11238. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11239. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11240. intel_color_init(&intel_crtc->base);
  11241. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11242. return 0;
  11243. fail:
  11244. /*
  11245. * drm_mode_config_cleanup() will free up any
  11246. * crtcs/planes already initialized.
  11247. */
  11248. kfree(crtc_state);
  11249. kfree(intel_crtc);
  11250. return ret;
  11251. }
  11252. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11253. {
  11254. struct drm_device *dev = connector->base.dev;
  11255. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11256. if (!connector->base.state->crtc)
  11257. return INVALID_PIPE;
  11258. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11259. }
  11260. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11261. struct drm_file *file)
  11262. {
  11263. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11264. struct drm_crtc *drmmode_crtc;
  11265. struct intel_crtc *crtc;
  11266. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11267. if (!drmmode_crtc)
  11268. return -ENOENT;
  11269. crtc = to_intel_crtc(drmmode_crtc);
  11270. pipe_from_crtc_id->pipe = crtc->pipe;
  11271. return 0;
  11272. }
  11273. static int intel_encoder_clones(struct intel_encoder *encoder)
  11274. {
  11275. struct drm_device *dev = encoder->base.dev;
  11276. struct intel_encoder *source_encoder;
  11277. int index_mask = 0;
  11278. int entry = 0;
  11279. for_each_intel_encoder(dev, source_encoder) {
  11280. if (encoders_cloneable(encoder, source_encoder))
  11281. index_mask |= (1 << entry);
  11282. entry++;
  11283. }
  11284. return index_mask;
  11285. }
  11286. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11287. {
  11288. if (!IS_MOBILE(dev_priv))
  11289. return false;
  11290. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11291. return false;
  11292. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11293. return false;
  11294. return true;
  11295. }
  11296. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11297. {
  11298. if (INTEL_GEN(dev_priv) >= 9)
  11299. return false;
  11300. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11301. return false;
  11302. if (IS_CHERRYVIEW(dev_priv))
  11303. return false;
  11304. if (HAS_PCH_LPT_H(dev_priv) &&
  11305. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11306. return false;
  11307. /* DDI E can't be used if DDI A requires 4 lanes */
  11308. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11309. return false;
  11310. if (!dev_priv->vbt.int_crt_support)
  11311. return false;
  11312. return true;
  11313. }
  11314. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11315. {
  11316. int pps_num;
  11317. int pps_idx;
  11318. if (HAS_DDI(dev_priv))
  11319. return;
  11320. /*
  11321. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11322. * everywhere where registers can be write protected.
  11323. */
  11324. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11325. pps_num = 2;
  11326. else
  11327. pps_num = 1;
  11328. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11329. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11330. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11331. I915_WRITE(PP_CONTROL(pps_idx), val);
  11332. }
  11333. }
  11334. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11335. {
  11336. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11337. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11338. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11339. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11340. else
  11341. dev_priv->pps_mmio_base = PPS_BASE;
  11342. intel_pps_unlock_regs_wa(dev_priv);
  11343. }
  11344. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11345. {
  11346. struct intel_encoder *encoder;
  11347. bool dpd_is_edp = false;
  11348. intel_pps_init(dev_priv);
  11349. /*
  11350. * intel_edp_init_connector() depends on this completing first, to
  11351. * prevent the registeration of both eDP and LVDS and the incorrect
  11352. * sharing of the PPS.
  11353. */
  11354. intel_lvds_init(dev_priv);
  11355. if (intel_crt_present(dev_priv))
  11356. intel_crt_init(dev_priv);
  11357. if (IS_GEN9_LP(dev_priv)) {
  11358. /*
  11359. * FIXME: Broxton doesn't support port detection via the
  11360. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11361. * detect the ports.
  11362. */
  11363. intel_ddi_init(dev_priv, PORT_A);
  11364. intel_ddi_init(dev_priv, PORT_B);
  11365. intel_ddi_init(dev_priv, PORT_C);
  11366. intel_dsi_init(dev_priv);
  11367. } else if (HAS_DDI(dev_priv)) {
  11368. int found;
  11369. /*
  11370. * Haswell uses DDI functions to detect digital outputs.
  11371. * On SKL pre-D0 the strap isn't connected, so we assume
  11372. * it's there.
  11373. */
  11374. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11375. /* WaIgnoreDDIAStrap: skl */
  11376. if (found || IS_GEN9_BC(dev_priv))
  11377. intel_ddi_init(dev_priv, PORT_A);
  11378. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11379. * register */
  11380. found = I915_READ(SFUSE_STRAP);
  11381. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11382. intel_ddi_init(dev_priv, PORT_B);
  11383. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11384. intel_ddi_init(dev_priv, PORT_C);
  11385. if (found & SFUSE_STRAP_DDID_DETECTED)
  11386. intel_ddi_init(dev_priv, PORT_D);
  11387. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11388. intel_ddi_init(dev_priv, PORT_F);
  11389. /*
  11390. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11391. */
  11392. if (IS_GEN9_BC(dev_priv) &&
  11393. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11394. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11395. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11396. intel_ddi_init(dev_priv, PORT_E);
  11397. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11398. int found;
  11399. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11400. if (has_edp_a(dev_priv))
  11401. intel_dp_init(dev_priv, DP_A, PORT_A);
  11402. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11403. /* PCH SDVOB multiplex with HDMIB */
  11404. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11405. if (!found)
  11406. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11407. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11408. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11409. }
  11410. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11411. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11412. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11413. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11414. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11415. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11416. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11417. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11418. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11419. bool has_edp, has_port;
  11420. /*
  11421. * The DP_DETECTED bit is the latched state of the DDC
  11422. * SDA pin at boot. However since eDP doesn't require DDC
  11423. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11424. * eDP ports may have been muxed to an alternate function.
  11425. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11426. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11427. * detect eDP ports.
  11428. *
  11429. * Sadly the straps seem to be missing sometimes even for HDMI
  11430. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11431. * and VBT for the presence of the port. Additionally we can't
  11432. * trust the port type the VBT declares as we've seen at least
  11433. * HDMI ports that the VBT claim are DP or eDP.
  11434. */
  11435. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11436. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11437. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11438. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11439. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11440. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11441. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11442. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11443. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11444. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11445. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11446. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11447. if (IS_CHERRYVIEW(dev_priv)) {
  11448. /*
  11449. * eDP not supported on port D,
  11450. * so no need to worry about it
  11451. */
  11452. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11453. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11454. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11455. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11456. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11457. }
  11458. intel_dsi_init(dev_priv);
  11459. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11460. bool found = false;
  11461. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11462. DRM_DEBUG_KMS("probing SDVOB\n");
  11463. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11464. if (!found && IS_G4X(dev_priv)) {
  11465. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11466. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11467. }
  11468. if (!found && IS_G4X(dev_priv))
  11469. intel_dp_init(dev_priv, DP_B, PORT_B);
  11470. }
  11471. /* Before G4X SDVOC doesn't have its own detect register */
  11472. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11473. DRM_DEBUG_KMS("probing SDVOC\n");
  11474. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11475. }
  11476. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11477. if (IS_G4X(dev_priv)) {
  11478. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11479. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11480. }
  11481. if (IS_G4X(dev_priv))
  11482. intel_dp_init(dev_priv, DP_C, PORT_C);
  11483. }
  11484. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11485. intel_dp_init(dev_priv, DP_D, PORT_D);
  11486. } else if (IS_GEN2(dev_priv))
  11487. intel_dvo_init(dev_priv);
  11488. if (SUPPORTS_TV(dev_priv))
  11489. intel_tv_init(dev_priv);
  11490. intel_psr_init(dev_priv);
  11491. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11492. encoder->base.possible_crtcs = encoder->crtc_mask;
  11493. encoder->base.possible_clones =
  11494. intel_encoder_clones(encoder);
  11495. }
  11496. intel_init_pch_refclk(dev_priv);
  11497. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11498. }
  11499. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11500. {
  11501. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11502. drm_framebuffer_cleanup(fb);
  11503. i915_gem_object_lock(intel_fb->obj);
  11504. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11505. i915_gem_object_unlock(intel_fb->obj);
  11506. i915_gem_object_put(intel_fb->obj);
  11507. kfree(intel_fb);
  11508. }
  11509. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11510. struct drm_file *file,
  11511. unsigned int *handle)
  11512. {
  11513. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11514. struct drm_i915_gem_object *obj = intel_fb->obj;
  11515. if (obj->userptr.mm) {
  11516. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11517. return -EINVAL;
  11518. }
  11519. return drm_gem_handle_create(file, &obj->base, handle);
  11520. }
  11521. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11522. struct drm_file *file,
  11523. unsigned flags, unsigned color,
  11524. struct drm_clip_rect *clips,
  11525. unsigned num_clips)
  11526. {
  11527. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11528. i915_gem_object_flush_if_display(obj);
  11529. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11530. return 0;
  11531. }
  11532. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11533. .destroy = intel_user_framebuffer_destroy,
  11534. .create_handle = intel_user_framebuffer_create_handle,
  11535. .dirty = intel_user_framebuffer_dirty,
  11536. };
  11537. static
  11538. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11539. uint64_t fb_modifier, uint32_t pixel_format)
  11540. {
  11541. u32 gen = INTEL_GEN(dev_priv);
  11542. if (gen >= 9) {
  11543. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11544. /* "The stride in bytes must not exceed the of the size of 8K
  11545. * pixels and 32K bytes."
  11546. */
  11547. return min(8192 * cpp, 32768);
  11548. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11549. return 32*1024;
  11550. } else if (gen >= 4) {
  11551. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11552. return 16*1024;
  11553. else
  11554. return 32*1024;
  11555. } else if (gen >= 3) {
  11556. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11557. return 8*1024;
  11558. else
  11559. return 16*1024;
  11560. } else {
  11561. /* XXX DSPC is limited to 4k tiled */
  11562. return 8*1024;
  11563. }
  11564. }
  11565. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11566. struct drm_i915_gem_object *obj,
  11567. struct drm_mode_fb_cmd2 *mode_cmd)
  11568. {
  11569. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11570. struct drm_framebuffer *fb = &intel_fb->base;
  11571. struct drm_format_name_buf format_name;
  11572. u32 pitch_limit;
  11573. unsigned int tiling, stride;
  11574. int ret = -EINVAL;
  11575. int i;
  11576. i915_gem_object_lock(obj);
  11577. obj->framebuffer_references++;
  11578. tiling = i915_gem_object_get_tiling(obj);
  11579. stride = i915_gem_object_get_stride(obj);
  11580. i915_gem_object_unlock(obj);
  11581. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11582. /*
  11583. * If there's a fence, enforce that
  11584. * the fb modifier and tiling mode match.
  11585. */
  11586. if (tiling != I915_TILING_NONE &&
  11587. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11588. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11589. goto err;
  11590. }
  11591. } else {
  11592. if (tiling == I915_TILING_X) {
  11593. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11594. } else if (tiling == I915_TILING_Y) {
  11595. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11596. goto err;
  11597. }
  11598. }
  11599. /* Passed in modifier sanity checking. */
  11600. switch (mode_cmd->modifier[0]) {
  11601. case I915_FORMAT_MOD_Y_TILED_CCS:
  11602. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11603. switch (mode_cmd->pixel_format) {
  11604. case DRM_FORMAT_XBGR8888:
  11605. case DRM_FORMAT_ABGR8888:
  11606. case DRM_FORMAT_XRGB8888:
  11607. case DRM_FORMAT_ARGB8888:
  11608. break;
  11609. default:
  11610. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11611. goto err;
  11612. }
  11613. /* fall through */
  11614. case I915_FORMAT_MOD_Y_TILED:
  11615. case I915_FORMAT_MOD_Yf_TILED:
  11616. if (INTEL_GEN(dev_priv) < 9) {
  11617. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11618. mode_cmd->modifier[0]);
  11619. goto err;
  11620. }
  11621. case DRM_FORMAT_MOD_LINEAR:
  11622. case I915_FORMAT_MOD_X_TILED:
  11623. break;
  11624. default:
  11625. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11626. mode_cmd->modifier[0]);
  11627. goto err;
  11628. }
  11629. /*
  11630. * gen2/3 display engine uses the fence if present,
  11631. * so the tiling mode must match the fb modifier exactly.
  11632. */
  11633. if (INTEL_GEN(dev_priv) < 4 &&
  11634. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11635. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11636. goto err;
  11637. }
  11638. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11639. mode_cmd->pixel_format);
  11640. if (mode_cmd->pitches[0] > pitch_limit) {
  11641. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11642. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11643. "tiled" : "linear",
  11644. mode_cmd->pitches[0], pitch_limit);
  11645. goto err;
  11646. }
  11647. /*
  11648. * If there's a fence, enforce that
  11649. * the fb pitch and fence stride match.
  11650. */
  11651. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11652. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11653. mode_cmd->pitches[0], stride);
  11654. goto err;
  11655. }
  11656. /* Reject formats not supported by any plane early. */
  11657. switch (mode_cmd->pixel_format) {
  11658. case DRM_FORMAT_C8:
  11659. case DRM_FORMAT_RGB565:
  11660. case DRM_FORMAT_XRGB8888:
  11661. case DRM_FORMAT_ARGB8888:
  11662. break;
  11663. case DRM_FORMAT_XRGB1555:
  11664. if (INTEL_GEN(dev_priv) > 3) {
  11665. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11666. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11667. goto err;
  11668. }
  11669. break;
  11670. case DRM_FORMAT_ABGR8888:
  11671. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11672. INTEL_GEN(dev_priv) < 9) {
  11673. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11674. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11675. goto err;
  11676. }
  11677. break;
  11678. case DRM_FORMAT_XBGR8888:
  11679. case DRM_FORMAT_XRGB2101010:
  11680. case DRM_FORMAT_XBGR2101010:
  11681. if (INTEL_GEN(dev_priv) < 4) {
  11682. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11683. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11684. goto err;
  11685. }
  11686. break;
  11687. case DRM_FORMAT_ABGR2101010:
  11688. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11689. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11690. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11691. goto err;
  11692. }
  11693. break;
  11694. case DRM_FORMAT_YUYV:
  11695. case DRM_FORMAT_UYVY:
  11696. case DRM_FORMAT_YVYU:
  11697. case DRM_FORMAT_VYUY:
  11698. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11699. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11700. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11701. goto err;
  11702. }
  11703. break;
  11704. default:
  11705. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11706. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11707. goto err;
  11708. }
  11709. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11710. if (mode_cmd->offsets[0] != 0)
  11711. goto err;
  11712. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11713. for (i = 0; i < fb->format->num_planes; i++) {
  11714. u32 stride_alignment;
  11715. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11716. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11717. goto err;
  11718. }
  11719. stride_alignment = intel_fb_stride_alignment(fb, i);
  11720. /*
  11721. * Display WA #0531: skl,bxt,kbl,glk
  11722. *
  11723. * Render decompression and plane width > 3840
  11724. * combined with horizontal panning requires the
  11725. * plane stride to be a multiple of 4. We'll just
  11726. * require the entire fb to accommodate that to avoid
  11727. * potential runtime errors at plane configuration time.
  11728. */
  11729. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11730. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11731. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11732. stride_alignment *= 4;
  11733. if (fb->pitches[i] & (stride_alignment - 1)) {
  11734. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11735. i, fb->pitches[i], stride_alignment);
  11736. goto err;
  11737. }
  11738. }
  11739. intel_fb->obj = obj;
  11740. ret = intel_fill_fb_info(dev_priv, fb);
  11741. if (ret)
  11742. goto err;
  11743. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11744. if (ret) {
  11745. DRM_ERROR("framebuffer init failed %d\n", ret);
  11746. goto err;
  11747. }
  11748. return 0;
  11749. err:
  11750. i915_gem_object_lock(obj);
  11751. obj->framebuffer_references--;
  11752. i915_gem_object_unlock(obj);
  11753. return ret;
  11754. }
  11755. static struct drm_framebuffer *
  11756. intel_user_framebuffer_create(struct drm_device *dev,
  11757. struct drm_file *filp,
  11758. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11759. {
  11760. struct drm_framebuffer *fb;
  11761. struct drm_i915_gem_object *obj;
  11762. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11763. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11764. if (!obj)
  11765. return ERR_PTR(-ENOENT);
  11766. fb = intel_framebuffer_create(obj, &mode_cmd);
  11767. if (IS_ERR(fb))
  11768. i915_gem_object_put(obj);
  11769. return fb;
  11770. }
  11771. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11772. {
  11773. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11774. drm_atomic_state_default_release(state);
  11775. i915_sw_fence_fini(&intel_state->commit_ready);
  11776. kfree(state);
  11777. }
  11778. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11779. .fb_create = intel_user_framebuffer_create,
  11780. .get_format_info = intel_get_format_info,
  11781. .output_poll_changed = intel_fbdev_output_poll_changed,
  11782. .atomic_check = intel_atomic_check,
  11783. .atomic_commit = intel_atomic_commit,
  11784. .atomic_state_alloc = intel_atomic_state_alloc,
  11785. .atomic_state_clear = intel_atomic_state_clear,
  11786. .atomic_state_free = intel_atomic_state_free,
  11787. };
  11788. /**
  11789. * intel_init_display_hooks - initialize the display modesetting hooks
  11790. * @dev_priv: device private
  11791. */
  11792. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11793. {
  11794. intel_init_cdclk_hooks(dev_priv);
  11795. if (INTEL_GEN(dev_priv) >= 9) {
  11796. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11797. dev_priv->display.get_initial_plane_config =
  11798. skylake_get_initial_plane_config;
  11799. dev_priv->display.crtc_compute_clock =
  11800. haswell_crtc_compute_clock;
  11801. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11802. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11803. } else if (HAS_DDI(dev_priv)) {
  11804. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11805. dev_priv->display.get_initial_plane_config =
  11806. i9xx_get_initial_plane_config;
  11807. dev_priv->display.crtc_compute_clock =
  11808. haswell_crtc_compute_clock;
  11809. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11810. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11811. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11812. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11813. dev_priv->display.get_initial_plane_config =
  11814. i9xx_get_initial_plane_config;
  11815. dev_priv->display.crtc_compute_clock =
  11816. ironlake_crtc_compute_clock;
  11817. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11818. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11819. } else if (IS_CHERRYVIEW(dev_priv)) {
  11820. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11821. dev_priv->display.get_initial_plane_config =
  11822. i9xx_get_initial_plane_config;
  11823. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11824. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11825. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11826. } else if (IS_VALLEYVIEW(dev_priv)) {
  11827. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11828. dev_priv->display.get_initial_plane_config =
  11829. i9xx_get_initial_plane_config;
  11830. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11831. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11832. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11833. } else if (IS_G4X(dev_priv)) {
  11834. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11835. dev_priv->display.get_initial_plane_config =
  11836. i9xx_get_initial_plane_config;
  11837. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11838. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11839. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11840. } else if (IS_PINEVIEW(dev_priv)) {
  11841. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11842. dev_priv->display.get_initial_plane_config =
  11843. i9xx_get_initial_plane_config;
  11844. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11845. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11846. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11847. } else if (!IS_GEN2(dev_priv)) {
  11848. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11849. dev_priv->display.get_initial_plane_config =
  11850. i9xx_get_initial_plane_config;
  11851. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11852. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11853. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11854. } else {
  11855. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11856. dev_priv->display.get_initial_plane_config =
  11857. i9xx_get_initial_plane_config;
  11858. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11859. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11860. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11861. }
  11862. if (IS_GEN5(dev_priv)) {
  11863. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11864. } else if (IS_GEN6(dev_priv)) {
  11865. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11866. } else if (IS_IVYBRIDGE(dev_priv)) {
  11867. /* FIXME: detect B0+ stepping and use auto training */
  11868. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11869. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11870. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11871. }
  11872. if (INTEL_GEN(dev_priv) >= 9)
  11873. dev_priv->display.update_crtcs = skl_update_crtcs;
  11874. else
  11875. dev_priv->display.update_crtcs = intel_update_crtcs;
  11876. }
  11877. /*
  11878. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11879. */
  11880. static void quirk_ssc_force_disable(struct drm_device *dev)
  11881. {
  11882. struct drm_i915_private *dev_priv = to_i915(dev);
  11883. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11884. DRM_INFO("applying lvds SSC disable quirk\n");
  11885. }
  11886. /*
  11887. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11888. * brightness value
  11889. */
  11890. static void quirk_invert_brightness(struct drm_device *dev)
  11891. {
  11892. struct drm_i915_private *dev_priv = to_i915(dev);
  11893. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11894. DRM_INFO("applying inverted panel brightness quirk\n");
  11895. }
  11896. /* Some VBT's incorrectly indicate no backlight is present */
  11897. static void quirk_backlight_present(struct drm_device *dev)
  11898. {
  11899. struct drm_i915_private *dev_priv = to_i915(dev);
  11900. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11901. DRM_INFO("applying backlight present quirk\n");
  11902. }
  11903. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11904. * which is 300 ms greater than eDP spec T12 min.
  11905. */
  11906. static void quirk_increase_t12_delay(struct drm_device *dev)
  11907. {
  11908. struct drm_i915_private *dev_priv = to_i915(dev);
  11909. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11910. DRM_INFO("Applying T12 delay quirk\n");
  11911. }
  11912. struct intel_quirk {
  11913. int device;
  11914. int subsystem_vendor;
  11915. int subsystem_device;
  11916. void (*hook)(struct drm_device *dev);
  11917. };
  11918. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11919. struct intel_dmi_quirk {
  11920. void (*hook)(struct drm_device *dev);
  11921. const struct dmi_system_id (*dmi_id_list)[];
  11922. };
  11923. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11924. {
  11925. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11926. return 1;
  11927. }
  11928. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11929. {
  11930. .dmi_id_list = &(const struct dmi_system_id[]) {
  11931. {
  11932. .callback = intel_dmi_reverse_brightness,
  11933. .ident = "NCR Corporation",
  11934. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11935. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11936. },
  11937. },
  11938. { } /* terminating entry */
  11939. },
  11940. .hook = quirk_invert_brightness,
  11941. },
  11942. };
  11943. static struct intel_quirk intel_quirks[] = {
  11944. /* Lenovo U160 cannot use SSC on LVDS */
  11945. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11946. /* Sony Vaio Y cannot use SSC on LVDS */
  11947. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11948. /* Acer Aspire 5734Z must invert backlight brightness */
  11949. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11950. /* Acer/eMachines G725 */
  11951. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11952. /* Acer/eMachines e725 */
  11953. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11954. /* Acer/Packard Bell NCL20 */
  11955. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11956. /* Acer Aspire 4736Z */
  11957. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11958. /* Acer Aspire 5336 */
  11959. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11960. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11961. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11962. /* Acer C720 Chromebook (Core i3 4005U) */
  11963. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11964. /* Apple Macbook 2,1 (Core 2 T7400) */
  11965. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11966. /* Apple Macbook 4,1 */
  11967. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11968. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11969. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11970. /* HP Chromebook 14 (Celeron 2955U) */
  11971. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11972. /* Dell Chromebook 11 */
  11973. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11974. /* Dell Chromebook 11 (2015 version) */
  11975. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11976. /* Toshiba Satellite P50-C-18C */
  11977. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11978. };
  11979. static void intel_init_quirks(struct drm_device *dev)
  11980. {
  11981. struct pci_dev *d = dev->pdev;
  11982. int i;
  11983. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11984. struct intel_quirk *q = &intel_quirks[i];
  11985. if (d->device == q->device &&
  11986. (d->subsystem_vendor == q->subsystem_vendor ||
  11987. q->subsystem_vendor == PCI_ANY_ID) &&
  11988. (d->subsystem_device == q->subsystem_device ||
  11989. q->subsystem_device == PCI_ANY_ID))
  11990. q->hook(dev);
  11991. }
  11992. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11993. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11994. intel_dmi_quirks[i].hook(dev);
  11995. }
  11996. }
  11997. /* Disable the VGA plane that we never use */
  11998. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  11999. {
  12000. struct pci_dev *pdev = dev_priv->drm.pdev;
  12001. u8 sr1;
  12002. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12003. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12004. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12005. outb(SR01, VGA_SR_INDEX);
  12006. sr1 = inb(VGA_SR_DATA);
  12007. outb(sr1 | 1<<5, VGA_SR_DATA);
  12008. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12009. udelay(300);
  12010. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12011. POSTING_READ(vga_reg);
  12012. }
  12013. void intel_modeset_init_hw(struct drm_device *dev)
  12014. {
  12015. struct drm_i915_private *dev_priv = to_i915(dev);
  12016. intel_update_cdclk(dev_priv);
  12017. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12018. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12019. }
  12020. /*
  12021. * Calculate what we think the watermarks should be for the state we've read
  12022. * out of the hardware and then immediately program those watermarks so that
  12023. * we ensure the hardware settings match our internal state.
  12024. *
  12025. * We can calculate what we think WM's should be by creating a duplicate of the
  12026. * current state (which was constructed during hardware readout) and running it
  12027. * through the atomic check code to calculate new watermark values in the
  12028. * state object.
  12029. */
  12030. static void sanitize_watermarks(struct drm_device *dev)
  12031. {
  12032. struct drm_i915_private *dev_priv = to_i915(dev);
  12033. struct drm_atomic_state *state;
  12034. struct intel_atomic_state *intel_state;
  12035. struct drm_crtc *crtc;
  12036. struct drm_crtc_state *cstate;
  12037. struct drm_modeset_acquire_ctx ctx;
  12038. int ret;
  12039. int i;
  12040. /* Only supported on platforms that use atomic watermark design */
  12041. if (!dev_priv->display.optimize_watermarks)
  12042. return;
  12043. /*
  12044. * We need to hold connection_mutex before calling duplicate_state so
  12045. * that the connector loop is protected.
  12046. */
  12047. drm_modeset_acquire_init(&ctx, 0);
  12048. retry:
  12049. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12050. if (ret == -EDEADLK) {
  12051. drm_modeset_backoff(&ctx);
  12052. goto retry;
  12053. } else if (WARN_ON(ret)) {
  12054. goto fail;
  12055. }
  12056. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12057. if (WARN_ON(IS_ERR(state)))
  12058. goto fail;
  12059. intel_state = to_intel_atomic_state(state);
  12060. /*
  12061. * Hardware readout is the only time we don't want to calculate
  12062. * intermediate watermarks (since we don't trust the current
  12063. * watermarks).
  12064. */
  12065. if (!HAS_GMCH_DISPLAY(dev_priv))
  12066. intel_state->skip_intermediate_wm = true;
  12067. ret = intel_atomic_check(dev, state);
  12068. if (ret) {
  12069. /*
  12070. * If we fail here, it means that the hardware appears to be
  12071. * programmed in a way that shouldn't be possible, given our
  12072. * understanding of watermark requirements. This might mean a
  12073. * mistake in the hardware readout code or a mistake in the
  12074. * watermark calculations for a given platform. Raise a WARN
  12075. * so that this is noticeable.
  12076. *
  12077. * If this actually happens, we'll have to just leave the
  12078. * BIOS-programmed watermarks untouched and hope for the best.
  12079. */
  12080. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12081. goto put_state;
  12082. }
  12083. /* Write calculated watermark values back */
  12084. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12085. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12086. cs->wm.need_postvbl_update = true;
  12087. dev_priv->display.optimize_watermarks(intel_state, cs);
  12088. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12089. }
  12090. put_state:
  12091. drm_atomic_state_put(state);
  12092. fail:
  12093. drm_modeset_drop_locks(&ctx);
  12094. drm_modeset_acquire_fini(&ctx);
  12095. }
  12096. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12097. {
  12098. if (IS_GEN5(dev_priv)) {
  12099. u32 fdi_pll_clk =
  12100. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12101. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12102. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12103. dev_priv->fdi_pll_freq = 270000;
  12104. } else {
  12105. return;
  12106. }
  12107. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12108. }
  12109. int intel_modeset_init(struct drm_device *dev)
  12110. {
  12111. struct drm_i915_private *dev_priv = to_i915(dev);
  12112. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12113. enum pipe pipe;
  12114. struct intel_crtc *crtc;
  12115. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12116. drm_mode_config_init(dev);
  12117. dev->mode_config.min_width = 0;
  12118. dev->mode_config.min_height = 0;
  12119. dev->mode_config.preferred_depth = 24;
  12120. dev->mode_config.prefer_shadow = 1;
  12121. dev->mode_config.allow_fb_modifiers = true;
  12122. dev->mode_config.funcs = &intel_mode_funcs;
  12123. init_llist_head(&dev_priv->atomic_helper.free_list);
  12124. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12125. intel_atomic_helper_free_state_worker);
  12126. intel_init_quirks(dev);
  12127. intel_init_pm(dev_priv);
  12128. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12129. return 0;
  12130. /*
  12131. * There may be no VBT; and if the BIOS enabled SSC we can
  12132. * just keep using it to avoid unnecessary flicker. Whereas if the
  12133. * BIOS isn't using it, don't assume it will work even if the VBT
  12134. * indicates as much.
  12135. */
  12136. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12137. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12138. DREF_SSC1_ENABLE);
  12139. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12140. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12141. bios_lvds_use_ssc ? "en" : "dis",
  12142. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12143. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12144. }
  12145. }
  12146. if (IS_GEN2(dev_priv)) {
  12147. dev->mode_config.max_width = 2048;
  12148. dev->mode_config.max_height = 2048;
  12149. } else if (IS_GEN3(dev_priv)) {
  12150. dev->mode_config.max_width = 4096;
  12151. dev->mode_config.max_height = 4096;
  12152. } else {
  12153. dev->mode_config.max_width = 8192;
  12154. dev->mode_config.max_height = 8192;
  12155. }
  12156. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12157. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12158. dev->mode_config.cursor_height = 1023;
  12159. } else if (IS_GEN2(dev_priv)) {
  12160. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12161. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12162. } else {
  12163. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12164. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12165. }
  12166. dev->mode_config.fb_base = ggtt->gmadr.start;
  12167. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12168. INTEL_INFO(dev_priv)->num_pipes,
  12169. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12170. for_each_pipe(dev_priv, pipe) {
  12171. int ret;
  12172. ret = intel_crtc_init(dev_priv, pipe);
  12173. if (ret) {
  12174. drm_mode_config_cleanup(dev);
  12175. return ret;
  12176. }
  12177. }
  12178. intel_shared_dpll_init(dev);
  12179. intel_update_fdi_pll_freq(dev_priv);
  12180. intel_update_czclk(dev_priv);
  12181. intel_modeset_init_hw(dev);
  12182. if (dev_priv->max_cdclk_freq == 0)
  12183. intel_update_max_cdclk(dev_priv);
  12184. /* Just disable it once at startup */
  12185. i915_disable_vga(dev_priv);
  12186. intel_setup_outputs(dev_priv);
  12187. drm_modeset_lock_all(dev);
  12188. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12189. drm_modeset_unlock_all(dev);
  12190. for_each_intel_crtc(dev, crtc) {
  12191. struct intel_initial_plane_config plane_config = {};
  12192. if (!crtc->active)
  12193. continue;
  12194. /*
  12195. * Note that reserving the BIOS fb up front prevents us
  12196. * from stuffing other stolen allocations like the ring
  12197. * on top. This prevents some ugliness at boot time, and
  12198. * can even allow for smooth boot transitions if the BIOS
  12199. * fb is large enough for the active pipe configuration.
  12200. */
  12201. dev_priv->display.get_initial_plane_config(crtc,
  12202. &plane_config);
  12203. /*
  12204. * If the fb is shared between multiple heads, we'll
  12205. * just get the first one.
  12206. */
  12207. intel_find_initial_plane_obj(crtc, &plane_config);
  12208. }
  12209. /*
  12210. * Make sure hardware watermarks really match the state we read out.
  12211. * Note that we need to do this after reconstructing the BIOS fb's
  12212. * since the watermark calculation done here will use pstate->fb.
  12213. */
  12214. if (!HAS_GMCH_DISPLAY(dev_priv))
  12215. sanitize_watermarks(dev);
  12216. return 0;
  12217. }
  12218. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12219. {
  12220. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12221. /* 640x480@60Hz, ~25175 kHz */
  12222. struct dpll clock = {
  12223. .m1 = 18,
  12224. .m2 = 7,
  12225. .p1 = 13,
  12226. .p2 = 4,
  12227. .n = 2,
  12228. };
  12229. u32 dpll, fp;
  12230. int i;
  12231. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12232. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12233. pipe_name(pipe), clock.vco, clock.dot);
  12234. fp = i9xx_dpll_compute_fp(&clock);
  12235. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12236. DPLL_VGA_MODE_DIS |
  12237. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12238. PLL_P2_DIVIDE_BY_4 |
  12239. PLL_REF_INPUT_DREFCLK |
  12240. DPLL_VCO_ENABLE;
  12241. I915_WRITE(FP0(pipe), fp);
  12242. I915_WRITE(FP1(pipe), fp);
  12243. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12244. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12245. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12246. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12247. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12248. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12249. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12250. /*
  12251. * Apparently we need to have VGA mode enabled prior to changing
  12252. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12253. * dividers, even though the register value does change.
  12254. */
  12255. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12256. I915_WRITE(DPLL(pipe), dpll);
  12257. /* Wait for the clocks to stabilize. */
  12258. POSTING_READ(DPLL(pipe));
  12259. udelay(150);
  12260. /* The pixel multiplier can only be updated once the
  12261. * DPLL is enabled and the clocks are stable.
  12262. *
  12263. * So write it again.
  12264. */
  12265. I915_WRITE(DPLL(pipe), dpll);
  12266. /* We do this three times for luck */
  12267. for (i = 0; i < 3 ; i++) {
  12268. I915_WRITE(DPLL(pipe), dpll);
  12269. POSTING_READ(DPLL(pipe));
  12270. udelay(150); /* wait for warmup */
  12271. }
  12272. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12273. POSTING_READ(PIPECONF(pipe));
  12274. intel_wait_for_pipe_scanline_moving(crtc);
  12275. }
  12276. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12277. {
  12278. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12279. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12280. pipe_name(pipe));
  12281. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12282. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12283. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12284. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
  12285. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
  12286. I915_WRITE(PIPECONF(pipe), 0);
  12287. POSTING_READ(PIPECONF(pipe));
  12288. intel_wait_for_pipe_scanline_stopped(crtc);
  12289. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12290. POSTING_READ(DPLL(pipe));
  12291. }
  12292. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12293. struct intel_plane *plane)
  12294. {
  12295. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12296. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  12297. u32 val = I915_READ(DSPCNTR(i9xx_plane));
  12298. return (val & DISPLAY_PLANE_ENABLE) == 0 ||
  12299. (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
  12300. }
  12301. static void
  12302. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12303. {
  12304. struct intel_crtc *crtc;
  12305. if (INTEL_GEN(dev_priv) >= 4)
  12306. return;
  12307. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12308. struct intel_plane *plane =
  12309. to_intel_plane(crtc->base.primary);
  12310. if (intel_plane_mapping_ok(crtc, plane))
  12311. continue;
  12312. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12313. plane->base.name);
  12314. intel_plane_disable_noatomic(crtc, plane);
  12315. }
  12316. }
  12317. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12318. {
  12319. struct drm_device *dev = crtc->base.dev;
  12320. struct intel_encoder *encoder;
  12321. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12322. return true;
  12323. return false;
  12324. }
  12325. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12326. {
  12327. struct drm_device *dev = encoder->base.dev;
  12328. struct intel_connector *connector;
  12329. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12330. return connector;
  12331. return NULL;
  12332. }
  12333. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12334. enum pipe pch_transcoder)
  12335. {
  12336. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12337. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12338. }
  12339. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12340. struct drm_modeset_acquire_ctx *ctx)
  12341. {
  12342. struct drm_device *dev = crtc->base.dev;
  12343. struct drm_i915_private *dev_priv = to_i915(dev);
  12344. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12345. /* Clear any frame start delays used for debugging left by the BIOS */
  12346. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12347. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12348. I915_WRITE(reg,
  12349. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12350. }
  12351. /* restore vblank interrupts to correct state */
  12352. drm_crtc_vblank_reset(&crtc->base);
  12353. if (crtc->active) {
  12354. struct intel_plane *plane;
  12355. drm_crtc_vblank_on(&crtc->base);
  12356. /* Disable everything but the primary plane */
  12357. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12358. const struct intel_plane_state *plane_state =
  12359. to_intel_plane_state(plane->base.state);
  12360. if (plane_state->base.visible &&
  12361. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12362. intel_plane_disable_noatomic(crtc, plane);
  12363. }
  12364. }
  12365. /* Adjust the state of the output pipe according to whether we
  12366. * have active connectors/encoders. */
  12367. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12368. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12369. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12370. /*
  12371. * We start out with underrun reporting disabled to avoid races.
  12372. * For correct bookkeeping mark this on active crtcs.
  12373. *
  12374. * Also on gmch platforms we dont have any hardware bits to
  12375. * disable the underrun reporting. Which means we need to start
  12376. * out with underrun reporting disabled also on inactive pipes,
  12377. * since otherwise we'll complain about the garbage we read when
  12378. * e.g. coming up after runtime pm.
  12379. *
  12380. * No protection against concurrent access is required - at
  12381. * worst a fifo underrun happens which also sets this to false.
  12382. */
  12383. crtc->cpu_fifo_underrun_disabled = true;
  12384. /*
  12385. * We track the PCH trancoder underrun reporting state
  12386. * within the crtc. With crtc for pipe A housing the underrun
  12387. * reporting state for PCH transcoder A, crtc for pipe B housing
  12388. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12389. * and marking underrun reporting as disabled for the non-existing
  12390. * PCH transcoders B and C would prevent enabling the south
  12391. * error interrupt (see cpt_can_enable_serr_int()).
  12392. */
  12393. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12394. crtc->pch_fifo_underrun_disabled = true;
  12395. }
  12396. }
  12397. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12398. {
  12399. struct intel_connector *connector;
  12400. /* We need to check both for a crtc link (meaning that the
  12401. * encoder is active and trying to read from a pipe) and the
  12402. * pipe itself being active. */
  12403. bool has_active_crtc = encoder->base.crtc &&
  12404. to_intel_crtc(encoder->base.crtc)->active;
  12405. connector = intel_encoder_find_connector(encoder);
  12406. if (connector && !has_active_crtc) {
  12407. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12408. encoder->base.base.id,
  12409. encoder->base.name);
  12410. /* Connector is active, but has no active pipe. This is
  12411. * fallout from our resume register restoring. Disable
  12412. * the encoder manually again. */
  12413. if (encoder->base.crtc) {
  12414. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12415. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12416. encoder->base.base.id,
  12417. encoder->base.name);
  12418. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12419. if (encoder->post_disable)
  12420. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12421. }
  12422. encoder->base.crtc = NULL;
  12423. /* Inconsistent output/port/pipe state happens presumably due to
  12424. * a bug in one of the get_hw_state functions. Or someplace else
  12425. * in our code, like the register restore mess on resume. Clamp
  12426. * things to off as a safer default. */
  12427. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12428. connector->base.encoder = NULL;
  12429. }
  12430. }
  12431. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12432. {
  12433. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12434. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12435. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12436. i915_disable_vga(dev_priv);
  12437. }
  12438. }
  12439. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12440. {
  12441. /* This function can be called both from intel_modeset_setup_hw_state or
  12442. * at a very early point in our resume sequence, where the power well
  12443. * structures are not yet restored. Since this function is at a very
  12444. * paranoid "someone might have enabled VGA while we were not looking"
  12445. * level, just check if the power well is enabled instead of trying to
  12446. * follow the "don't touch the power well if we don't need it" policy
  12447. * the rest of the driver uses. */
  12448. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12449. return;
  12450. i915_redisable_vga_power_on(dev_priv);
  12451. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12452. }
  12453. /* FIXME read out full plane state for all planes */
  12454. static void readout_plane_state(struct intel_crtc *crtc)
  12455. {
  12456. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12457. struct intel_crtc_state *crtc_state =
  12458. to_intel_crtc_state(crtc->base.state);
  12459. struct intel_plane *plane;
  12460. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12461. struct intel_plane_state *plane_state =
  12462. to_intel_plane_state(plane->base.state);
  12463. bool visible = plane->get_hw_state(plane);
  12464. intel_set_plane_visible(crtc_state, plane_state, visible);
  12465. }
  12466. }
  12467. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12468. {
  12469. struct drm_i915_private *dev_priv = to_i915(dev);
  12470. enum pipe pipe;
  12471. struct intel_crtc *crtc;
  12472. struct intel_encoder *encoder;
  12473. struct intel_connector *connector;
  12474. struct drm_connector_list_iter conn_iter;
  12475. int i;
  12476. dev_priv->active_crtcs = 0;
  12477. for_each_intel_crtc(dev, crtc) {
  12478. struct intel_crtc_state *crtc_state =
  12479. to_intel_crtc_state(crtc->base.state);
  12480. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12481. memset(crtc_state, 0, sizeof(*crtc_state));
  12482. crtc_state->base.crtc = &crtc->base;
  12483. crtc_state->base.active = crtc_state->base.enable =
  12484. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12485. crtc->base.enabled = crtc_state->base.enable;
  12486. crtc->active = crtc_state->base.active;
  12487. if (crtc_state->base.active)
  12488. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12489. readout_plane_state(crtc);
  12490. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12491. crtc->base.base.id, crtc->base.name,
  12492. enableddisabled(crtc_state->base.active));
  12493. }
  12494. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12495. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12496. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12497. &pll->state.hw_state);
  12498. pll->state.crtc_mask = 0;
  12499. for_each_intel_crtc(dev, crtc) {
  12500. struct intel_crtc_state *crtc_state =
  12501. to_intel_crtc_state(crtc->base.state);
  12502. if (crtc_state->base.active &&
  12503. crtc_state->shared_dpll == pll)
  12504. pll->state.crtc_mask |= 1 << crtc->pipe;
  12505. }
  12506. pll->active_mask = pll->state.crtc_mask;
  12507. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12508. pll->name, pll->state.crtc_mask, pll->on);
  12509. }
  12510. for_each_intel_encoder(dev, encoder) {
  12511. pipe = 0;
  12512. if (encoder->get_hw_state(encoder, &pipe)) {
  12513. struct intel_crtc_state *crtc_state;
  12514. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12515. crtc_state = to_intel_crtc_state(crtc->base.state);
  12516. encoder->base.crtc = &crtc->base;
  12517. encoder->get_config(encoder, crtc_state);
  12518. } else {
  12519. encoder->base.crtc = NULL;
  12520. }
  12521. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12522. encoder->base.base.id, encoder->base.name,
  12523. enableddisabled(encoder->base.crtc),
  12524. pipe_name(pipe));
  12525. }
  12526. drm_connector_list_iter_begin(dev, &conn_iter);
  12527. for_each_intel_connector_iter(connector, &conn_iter) {
  12528. if (connector->get_hw_state(connector)) {
  12529. connector->base.dpms = DRM_MODE_DPMS_ON;
  12530. encoder = connector->encoder;
  12531. connector->base.encoder = &encoder->base;
  12532. if (encoder->base.crtc &&
  12533. encoder->base.crtc->state->active) {
  12534. /*
  12535. * This has to be done during hardware readout
  12536. * because anything calling .crtc_disable may
  12537. * rely on the connector_mask being accurate.
  12538. */
  12539. encoder->base.crtc->state->connector_mask |=
  12540. 1 << drm_connector_index(&connector->base);
  12541. encoder->base.crtc->state->encoder_mask |=
  12542. 1 << drm_encoder_index(&encoder->base);
  12543. }
  12544. } else {
  12545. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12546. connector->base.encoder = NULL;
  12547. }
  12548. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12549. connector->base.base.id, connector->base.name,
  12550. enableddisabled(connector->base.encoder));
  12551. }
  12552. drm_connector_list_iter_end(&conn_iter);
  12553. for_each_intel_crtc(dev, crtc) {
  12554. struct intel_crtc_state *crtc_state =
  12555. to_intel_crtc_state(crtc->base.state);
  12556. int min_cdclk = 0;
  12557. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12558. if (crtc_state->base.active) {
  12559. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12560. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12561. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12562. /*
  12563. * The initial mode needs to be set in order to keep
  12564. * the atomic core happy. It wants a valid mode if the
  12565. * crtc's enabled, so we do the above call.
  12566. *
  12567. * But we don't set all the derived state fully, hence
  12568. * set a flag to indicate that a full recalculation is
  12569. * needed on the next commit.
  12570. */
  12571. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12572. intel_crtc_compute_pixel_rate(crtc_state);
  12573. if (dev_priv->display.modeset_calc_cdclk) {
  12574. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12575. if (WARN_ON(min_cdclk < 0))
  12576. min_cdclk = 0;
  12577. }
  12578. drm_calc_timestamping_constants(&crtc->base,
  12579. &crtc_state->base.adjusted_mode);
  12580. update_scanline_offset(crtc);
  12581. }
  12582. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12583. dev_priv->min_voltage_level[crtc->pipe] =
  12584. crtc_state->min_voltage_level;
  12585. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12586. }
  12587. }
  12588. static void
  12589. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12590. {
  12591. struct intel_encoder *encoder;
  12592. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12593. u64 get_domains;
  12594. enum intel_display_power_domain domain;
  12595. if (!encoder->get_power_domains)
  12596. continue;
  12597. get_domains = encoder->get_power_domains(encoder);
  12598. for_each_power_domain(domain, get_domains)
  12599. intel_display_power_get(dev_priv, domain);
  12600. }
  12601. }
  12602. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12603. {
  12604. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12605. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12606. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12607. DARBF_GATING_DIS);
  12608. if (IS_HASWELL(dev_priv)) {
  12609. /*
  12610. * WaRsPkgCStateDisplayPMReq:hsw
  12611. * System hang if this isn't done before disabling all planes!
  12612. */
  12613. I915_WRITE(CHICKEN_PAR1_1,
  12614. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12615. }
  12616. }
  12617. /* Scan out the current hw modeset state,
  12618. * and sanitizes it to the current state
  12619. */
  12620. static void
  12621. intel_modeset_setup_hw_state(struct drm_device *dev,
  12622. struct drm_modeset_acquire_ctx *ctx)
  12623. {
  12624. struct drm_i915_private *dev_priv = to_i915(dev);
  12625. enum pipe pipe;
  12626. struct intel_crtc *crtc;
  12627. struct intel_encoder *encoder;
  12628. int i;
  12629. intel_early_display_was(dev_priv);
  12630. intel_modeset_readout_hw_state(dev);
  12631. /* HW state is read out, now we need to sanitize this mess. */
  12632. get_encoder_power_domains(dev_priv);
  12633. intel_sanitize_plane_mapping(dev_priv);
  12634. for_each_intel_encoder(dev, encoder) {
  12635. intel_sanitize_encoder(encoder);
  12636. }
  12637. for_each_pipe(dev_priv, pipe) {
  12638. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12639. intel_sanitize_crtc(crtc, ctx);
  12640. intel_dump_pipe_config(crtc, crtc->config,
  12641. "[setup_hw_state]");
  12642. }
  12643. intel_modeset_update_connector_atomic_state(dev);
  12644. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12645. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12646. if (!pll->on || pll->active_mask)
  12647. continue;
  12648. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12649. pll->funcs.disable(dev_priv, pll);
  12650. pll->on = false;
  12651. }
  12652. if (IS_G4X(dev_priv)) {
  12653. g4x_wm_get_hw_state(dev);
  12654. g4x_wm_sanitize(dev_priv);
  12655. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12656. vlv_wm_get_hw_state(dev);
  12657. vlv_wm_sanitize(dev_priv);
  12658. } else if (INTEL_GEN(dev_priv) >= 9) {
  12659. skl_wm_get_hw_state(dev);
  12660. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12661. ilk_wm_get_hw_state(dev);
  12662. }
  12663. for_each_intel_crtc(dev, crtc) {
  12664. u64 put_domains;
  12665. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12666. if (WARN_ON(put_domains))
  12667. modeset_put_power_domains(dev_priv, put_domains);
  12668. }
  12669. intel_display_set_init_power(dev_priv, false);
  12670. intel_power_domains_verify_state(dev_priv);
  12671. intel_fbc_init_pipe_state(dev_priv);
  12672. }
  12673. void intel_display_resume(struct drm_device *dev)
  12674. {
  12675. struct drm_i915_private *dev_priv = to_i915(dev);
  12676. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12677. struct drm_modeset_acquire_ctx ctx;
  12678. int ret;
  12679. dev_priv->modeset_restore_state = NULL;
  12680. if (state)
  12681. state->acquire_ctx = &ctx;
  12682. drm_modeset_acquire_init(&ctx, 0);
  12683. while (1) {
  12684. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12685. if (ret != -EDEADLK)
  12686. break;
  12687. drm_modeset_backoff(&ctx);
  12688. }
  12689. if (!ret)
  12690. ret = __intel_display_resume(dev, state, &ctx);
  12691. intel_enable_ipc(dev_priv);
  12692. drm_modeset_drop_locks(&ctx);
  12693. drm_modeset_acquire_fini(&ctx);
  12694. if (ret)
  12695. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12696. if (state)
  12697. drm_atomic_state_put(state);
  12698. }
  12699. int intel_connector_register(struct drm_connector *connector)
  12700. {
  12701. struct intel_connector *intel_connector = to_intel_connector(connector);
  12702. int ret;
  12703. ret = intel_backlight_device_register(intel_connector);
  12704. if (ret)
  12705. goto err;
  12706. return 0;
  12707. err:
  12708. return ret;
  12709. }
  12710. void intel_connector_unregister(struct drm_connector *connector)
  12711. {
  12712. struct intel_connector *intel_connector = to_intel_connector(connector);
  12713. intel_backlight_device_unregister(intel_connector);
  12714. intel_panel_destroy_backlight(connector);
  12715. }
  12716. static void intel_hpd_poll_fini(struct drm_device *dev)
  12717. {
  12718. struct intel_connector *connector;
  12719. struct drm_connector_list_iter conn_iter;
  12720. /* Kill all the work that may have been queued by hpd. */
  12721. drm_connector_list_iter_begin(dev, &conn_iter);
  12722. for_each_intel_connector_iter(connector, &conn_iter) {
  12723. if (connector->modeset_retry_work.func)
  12724. cancel_work_sync(&connector->modeset_retry_work);
  12725. }
  12726. drm_connector_list_iter_end(&conn_iter);
  12727. }
  12728. void intel_modeset_cleanup(struct drm_device *dev)
  12729. {
  12730. struct drm_i915_private *dev_priv = to_i915(dev);
  12731. flush_work(&dev_priv->atomic_helper.free_work);
  12732. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12733. intel_disable_gt_powersave(dev_priv);
  12734. /*
  12735. * Interrupts and polling as the first thing to avoid creating havoc.
  12736. * Too much stuff here (turning of connectors, ...) would
  12737. * experience fancy races otherwise.
  12738. */
  12739. intel_irq_uninstall(dev_priv);
  12740. /*
  12741. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12742. * poll handlers. Hence disable polling after hpd handling is shut down.
  12743. */
  12744. intel_hpd_poll_fini(dev);
  12745. /* poll work can call into fbdev, hence clean that up afterwards */
  12746. intel_fbdev_fini(dev_priv);
  12747. intel_unregister_dsm_handler();
  12748. intel_fbc_global_disable(dev_priv);
  12749. /* flush any delayed tasks or pending work */
  12750. flush_scheduled_work();
  12751. drm_mode_config_cleanup(dev);
  12752. intel_cleanup_overlay(dev_priv);
  12753. intel_cleanup_gt_powersave(dev_priv);
  12754. intel_teardown_gmbus(dev_priv);
  12755. destroy_workqueue(dev_priv->modeset_wq);
  12756. }
  12757. void intel_connector_attach_encoder(struct intel_connector *connector,
  12758. struct intel_encoder *encoder)
  12759. {
  12760. connector->encoder = encoder;
  12761. drm_mode_connector_attach_encoder(&connector->base,
  12762. &encoder->base);
  12763. }
  12764. /*
  12765. * set vga decode state - true == enable VGA decode
  12766. */
  12767. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12768. {
  12769. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12770. u16 gmch_ctrl;
  12771. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12772. DRM_ERROR("failed to read control word\n");
  12773. return -EIO;
  12774. }
  12775. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12776. return 0;
  12777. if (state)
  12778. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12779. else
  12780. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12781. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12782. DRM_ERROR("failed to write control word\n");
  12783. return -EIO;
  12784. }
  12785. return 0;
  12786. }
  12787. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12788. struct intel_display_error_state {
  12789. u32 power_well_driver;
  12790. int num_transcoders;
  12791. struct intel_cursor_error_state {
  12792. u32 control;
  12793. u32 position;
  12794. u32 base;
  12795. u32 size;
  12796. } cursor[I915_MAX_PIPES];
  12797. struct intel_pipe_error_state {
  12798. bool power_domain_on;
  12799. u32 source;
  12800. u32 stat;
  12801. } pipe[I915_MAX_PIPES];
  12802. struct intel_plane_error_state {
  12803. u32 control;
  12804. u32 stride;
  12805. u32 size;
  12806. u32 pos;
  12807. u32 addr;
  12808. u32 surface;
  12809. u32 tile_offset;
  12810. } plane[I915_MAX_PIPES];
  12811. struct intel_transcoder_error_state {
  12812. bool power_domain_on;
  12813. enum transcoder cpu_transcoder;
  12814. u32 conf;
  12815. u32 htotal;
  12816. u32 hblank;
  12817. u32 hsync;
  12818. u32 vtotal;
  12819. u32 vblank;
  12820. u32 vsync;
  12821. } transcoder[4];
  12822. };
  12823. struct intel_display_error_state *
  12824. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12825. {
  12826. struct intel_display_error_state *error;
  12827. int transcoders[] = {
  12828. TRANSCODER_A,
  12829. TRANSCODER_B,
  12830. TRANSCODER_C,
  12831. TRANSCODER_EDP,
  12832. };
  12833. int i;
  12834. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12835. return NULL;
  12836. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12837. if (error == NULL)
  12838. return NULL;
  12839. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12840. error->power_well_driver =
  12841. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12842. for_each_pipe(dev_priv, i) {
  12843. error->pipe[i].power_domain_on =
  12844. __intel_display_power_is_enabled(dev_priv,
  12845. POWER_DOMAIN_PIPE(i));
  12846. if (!error->pipe[i].power_domain_on)
  12847. continue;
  12848. error->cursor[i].control = I915_READ(CURCNTR(i));
  12849. error->cursor[i].position = I915_READ(CURPOS(i));
  12850. error->cursor[i].base = I915_READ(CURBASE(i));
  12851. error->plane[i].control = I915_READ(DSPCNTR(i));
  12852. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12853. if (INTEL_GEN(dev_priv) <= 3) {
  12854. error->plane[i].size = I915_READ(DSPSIZE(i));
  12855. error->plane[i].pos = I915_READ(DSPPOS(i));
  12856. }
  12857. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12858. error->plane[i].addr = I915_READ(DSPADDR(i));
  12859. if (INTEL_GEN(dev_priv) >= 4) {
  12860. error->plane[i].surface = I915_READ(DSPSURF(i));
  12861. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12862. }
  12863. error->pipe[i].source = I915_READ(PIPESRC(i));
  12864. if (HAS_GMCH_DISPLAY(dev_priv))
  12865. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12866. }
  12867. /* Note: this does not include DSI transcoders. */
  12868. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12869. if (HAS_DDI(dev_priv))
  12870. error->num_transcoders++; /* Account for eDP. */
  12871. for (i = 0; i < error->num_transcoders; i++) {
  12872. enum transcoder cpu_transcoder = transcoders[i];
  12873. error->transcoder[i].power_domain_on =
  12874. __intel_display_power_is_enabled(dev_priv,
  12875. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12876. if (!error->transcoder[i].power_domain_on)
  12877. continue;
  12878. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12879. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12880. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12881. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12882. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12883. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12884. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12885. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12886. }
  12887. return error;
  12888. }
  12889. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12890. void
  12891. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12892. struct intel_display_error_state *error)
  12893. {
  12894. struct drm_i915_private *dev_priv = m->i915;
  12895. int i;
  12896. if (!error)
  12897. return;
  12898. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12899. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12900. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12901. error->power_well_driver);
  12902. for_each_pipe(dev_priv, i) {
  12903. err_printf(m, "Pipe [%d]:\n", i);
  12904. err_printf(m, " Power: %s\n",
  12905. onoff(error->pipe[i].power_domain_on));
  12906. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12907. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12908. err_printf(m, "Plane [%d]:\n", i);
  12909. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12910. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12911. if (INTEL_GEN(dev_priv) <= 3) {
  12912. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12913. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12914. }
  12915. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12916. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12917. if (INTEL_GEN(dev_priv) >= 4) {
  12918. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12919. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12920. }
  12921. err_printf(m, "Cursor [%d]:\n", i);
  12922. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12923. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12924. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12925. }
  12926. for (i = 0; i < error->num_transcoders; i++) {
  12927. err_printf(m, "CPU transcoder: %s\n",
  12928. transcoder_name(error->transcoder[i].cpu_transcoder));
  12929. err_printf(m, " Power: %s\n",
  12930. onoff(error->transcoder[i].power_domain_on));
  12931. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12932. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12933. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12934. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12935. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12936. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12937. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12938. }
  12939. }
  12940. #endif