intel_display.c 439 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595
  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(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_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  882. {
  883. i915_reg_t reg = PIPEDSL(pipe);
  884. u32 line1, line2;
  885. u32 line_mask;
  886. if (IS_GEN2(dev_priv))
  887. line_mask = DSL_LINEMASK_GEN2;
  888. else
  889. line_mask = DSL_LINEMASK_GEN3;
  890. line1 = I915_READ(reg) & line_mask;
  891. msleep(5);
  892. line2 = I915_READ(reg) & line_mask;
  893. return line1 == line2;
  894. }
  895. /*
  896. * intel_wait_for_pipe_off - wait for pipe to turn off
  897. * @crtc: crtc whose pipe to wait for
  898. *
  899. * After disabling a pipe, we can't wait for vblank in the usual way,
  900. * spinning on the vblank interrupt status bit, since we won't actually
  901. * see an interrupt when the pipe is disabled.
  902. *
  903. * On Gen4 and above:
  904. * wait for the pipe register state bit to turn off
  905. *
  906. * Otherwise:
  907. * wait for the display line value to settle (it usually
  908. * ends up stopping at the start of the next frame).
  909. *
  910. */
  911. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  912. {
  913. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  914. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  915. enum pipe pipe = crtc->pipe;
  916. if (INTEL_GEN(dev_priv) >= 4) {
  917. i915_reg_t reg = PIPECONF(cpu_transcoder);
  918. /* Wait for the Pipe State to go off */
  919. if (intel_wait_for_register(dev_priv,
  920. reg, I965_PIPECONF_ACTIVE, 0,
  921. 100))
  922. WARN(1, "pipe_off wait timed out\n");
  923. } else {
  924. /* Wait for the display line to settle */
  925. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  926. WARN(1, "pipe_off wait timed out\n");
  927. }
  928. }
  929. /* Only for pre-ILK configs */
  930. void assert_pll(struct drm_i915_private *dev_priv,
  931. enum pipe pipe, bool state)
  932. {
  933. u32 val;
  934. bool cur_state;
  935. val = I915_READ(DPLL(pipe));
  936. cur_state = !!(val & DPLL_VCO_ENABLE);
  937. I915_STATE_WARN(cur_state != state,
  938. "PLL state assertion failure (expected %s, current %s)\n",
  939. onoff(state), onoff(cur_state));
  940. }
  941. /* XXX: the dsi pll is shared between MIPI DSI ports */
  942. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  943. {
  944. u32 val;
  945. bool cur_state;
  946. mutex_lock(&dev_priv->sb_lock);
  947. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  948. mutex_unlock(&dev_priv->sb_lock);
  949. cur_state = val & DSI_PLL_VCO_EN;
  950. I915_STATE_WARN(cur_state != state,
  951. "DSI PLL state assertion failure (expected %s, current %s)\n",
  952. onoff(state), onoff(cur_state));
  953. }
  954. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  955. enum pipe pipe, bool state)
  956. {
  957. bool cur_state;
  958. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  959. pipe);
  960. if (HAS_DDI(dev_priv)) {
  961. /* DDI does not have a specific FDI_TX register */
  962. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  963. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  964. } else {
  965. u32 val = I915_READ(FDI_TX_CTL(pipe));
  966. cur_state = !!(val & FDI_TX_ENABLE);
  967. }
  968. I915_STATE_WARN(cur_state != state,
  969. "FDI TX state assertion failure (expected %s, current %s)\n",
  970. onoff(state), onoff(cur_state));
  971. }
  972. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  973. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  974. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  975. enum pipe pipe, bool state)
  976. {
  977. u32 val;
  978. bool cur_state;
  979. val = I915_READ(FDI_RX_CTL(pipe));
  980. cur_state = !!(val & FDI_RX_ENABLE);
  981. I915_STATE_WARN(cur_state != state,
  982. "FDI RX state assertion failure (expected %s, current %s)\n",
  983. onoff(state), onoff(cur_state));
  984. }
  985. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  986. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  987. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  988. enum pipe pipe)
  989. {
  990. u32 val;
  991. /* ILK FDI PLL is always enabled */
  992. if (IS_GEN5(dev_priv))
  993. return;
  994. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  995. if (HAS_DDI(dev_priv))
  996. return;
  997. val = I915_READ(FDI_TX_CTL(pipe));
  998. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  999. }
  1000. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1001. enum pipe pipe, bool state)
  1002. {
  1003. u32 val;
  1004. bool cur_state;
  1005. val = I915_READ(FDI_RX_CTL(pipe));
  1006. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1007. I915_STATE_WARN(cur_state != state,
  1008. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1009. onoff(state), onoff(cur_state));
  1010. }
  1011. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1012. {
  1013. i915_reg_t pp_reg;
  1014. u32 val;
  1015. enum pipe panel_pipe = PIPE_A;
  1016. bool locked = true;
  1017. if (WARN_ON(HAS_DDI(dev_priv)))
  1018. return;
  1019. if (HAS_PCH_SPLIT(dev_priv)) {
  1020. u32 port_sel;
  1021. pp_reg = PP_CONTROL(0);
  1022. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1023. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1024. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1025. panel_pipe = PIPE_B;
  1026. /* XXX: else fix for eDP */
  1027. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1028. /* presumably write lock depends on pipe, not port select */
  1029. pp_reg = PP_CONTROL(pipe);
  1030. panel_pipe = pipe;
  1031. } else {
  1032. pp_reg = PP_CONTROL(0);
  1033. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1034. panel_pipe = PIPE_B;
  1035. }
  1036. val = I915_READ(pp_reg);
  1037. if (!(val & PANEL_POWER_ON) ||
  1038. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1039. locked = false;
  1040. I915_STATE_WARN(panel_pipe == pipe && locked,
  1041. "panel assertion failure, pipe %c regs locked\n",
  1042. pipe_name(pipe));
  1043. }
  1044. static void assert_cursor(struct drm_i915_private *dev_priv,
  1045. enum pipe pipe, bool state)
  1046. {
  1047. bool cur_state;
  1048. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1049. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1050. else
  1051. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1052. I915_STATE_WARN(cur_state != state,
  1053. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1054. pipe_name(pipe), onoff(state), onoff(cur_state));
  1055. }
  1056. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1057. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1058. void assert_pipe(struct drm_i915_private *dev_priv,
  1059. enum pipe pipe, bool state)
  1060. {
  1061. bool cur_state;
  1062. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1063. pipe);
  1064. enum intel_display_power_domain power_domain;
  1065. /* we keep both pipes enabled on 830 */
  1066. if (IS_I830(dev_priv))
  1067. state = true;
  1068. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1069. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1070. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1071. cur_state = !!(val & PIPECONF_ENABLE);
  1072. intel_display_power_put(dev_priv, power_domain);
  1073. } else {
  1074. cur_state = false;
  1075. }
  1076. I915_STATE_WARN(cur_state != state,
  1077. "pipe %c assertion failure (expected %s, current %s)\n",
  1078. pipe_name(pipe), onoff(state), onoff(cur_state));
  1079. }
  1080. static void assert_plane(struct drm_i915_private *dev_priv,
  1081. enum plane plane, bool state)
  1082. {
  1083. u32 val;
  1084. bool cur_state;
  1085. val = I915_READ(DSPCNTR(plane));
  1086. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1087. I915_STATE_WARN(cur_state != state,
  1088. "plane %c assertion failure (expected %s, current %s)\n",
  1089. plane_name(plane), onoff(state), onoff(cur_state));
  1090. }
  1091. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1092. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1093. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe)
  1095. {
  1096. int i;
  1097. /* Primary planes are fixed to pipes on gen4+ */
  1098. if (INTEL_GEN(dev_priv) >= 4) {
  1099. u32 val = I915_READ(DSPCNTR(pipe));
  1100. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1101. "plane %c assertion failure, should be disabled but not\n",
  1102. plane_name(pipe));
  1103. return;
  1104. }
  1105. /* Need to check both planes against the pipe */
  1106. for_each_pipe(dev_priv, i) {
  1107. u32 val = I915_READ(DSPCNTR(i));
  1108. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1109. DISPPLANE_SEL_PIPE_SHIFT;
  1110. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1111. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1112. plane_name(i), pipe_name(pipe));
  1113. }
  1114. }
  1115. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1116. enum pipe pipe)
  1117. {
  1118. int sprite;
  1119. if (INTEL_GEN(dev_priv) >= 9) {
  1120. for_each_sprite(dev_priv, pipe, sprite) {
  1121. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1122. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1123. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1124. sprite, pipe_name(pipe));
  1125. }
  1126. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1127. for_each_sprite(dev_priv, pipe, sprite) {
  1128. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1129. I915_STATE_WARN(val & SP_ENABLE,
  1130. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1131. sprite_name(pipe, sprite), pipe_name(pipe));
  1132. }
  1133. } else if (INTEL_GEN(dev_priv) >= 7) {
  1134. u32 val = I915_READ(SPRCTL(pipe));
  1135. I915_STATE_WARN(val & SPRITE_ENABLE,
  1136. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1137. plane_name(pipe), pipe_name(pipe));
  1138. } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  1139. u32 val = I915_READ(DVSCNTR(pipe));
  1140. I915_STATE_WARN(val & DVS_ENABLE,
  1141. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1142. plane_name(pipe), pipe_name(pipe));
  1143. }
  1144. }
  1145. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1146. {
  1147. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1148. drm_crtc_vblank_put(crtc);
  1149. }
  1150. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1151. enum pipe pipe)
  1152. {
  1153. u32 val;
  1154. bool enabled;
  1155. val = I915_READ(PCH_TRANSCONF(pipe));
  1156. enabled = !!(val & TRANS_ENABLE);
  1157. I915_STATE_WARN(enabled,
  1158. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1159. pipe_name(pipe));
  1160. }
  1161. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1162. enum pipe pipe, u32 port_sel, u32 val)
  1163. {
  1164. if ((val & DP_PORT_EN) == 0)
  1165. return false;
  1166. if (HAS_PCH_CPT(dev_priv)) {
  1167. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1168. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1169. return false;
  1170. } else if (IS_CHERRYVIEW(dev_priv)) {
  1171. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1172. return false;
  1173. } else {
  1174. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1175. return false;
  1176. }
  1177. return true;
  1178. }
  1179. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1180. enum pipe pipe, u32 val)
  1181. {
  1182. if ((val & SDVO_ENABLE) == 0)
  1183. return false;
  1184. if (HAS_PCH_CPT(dev_priv)) {
  1185. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1186. return false;
  1187. } else if (IS_CHERRYVIEW(dev_priv)) {
  1188. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1189. return false;
  1190. } else {
  1191. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1192. return false;
  1193. }
  1194. return true;
  1195. }
  1196. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 val)
  1198. {
  1199. if ((val & LVDS_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv)) {
  1202. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1203. return false;
  1204. } else {
  1205. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1206. return false;
  1207. }
  1208. return true;
  1209. }
  1210. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1211. enum pipe pipe, u32 val)
  1212. {
  1213. if ((val & ADPA_DAC_ENABLE) == 0)
  1214. return false;
  1215. if (HAS_PCH_CPT(dev_priv)) {
  1216. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1217. return false;
  1218. } else {
  1219. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1220. return false;
  1221. }
  1222. return true;
  1223. }
  1224. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1225. enum pipe pipe, i915_reg_t reg,
  1226. u32 port_sel)
  1227. {
  1228. u32 val = I915_READ(reg);
  1229. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1230. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1231. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1232. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1233. && (val & DP_PIPEB_SELECT),
  1234. "IBX PCH dp port still using transcoder B\n");
  1235. }
  1236. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1237. enum pipe pipe, i915_reg_t reg)
  1238. {
  1239. u32 val = I915_READ(reg);
  1240. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1241. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1242. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1243. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1244. && (val & SDVO_PIPE_B_SELECT),
  1245. "IBX PCH hdmi port still using transcoder B\n");
  1246. }
  1247. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1248. enum pipe pipe)
  1249. {
  1250. u32 val;
  1251. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1252. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1253. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1254. val = I915_READ(PCH_ADPA);
  1255. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1256. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1257. pipe_name(pipe));
  1258. val = I915_READ(PCH_LVDS);
  1259. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1260. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1261. pipe_name(pipe));
  1262. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1263. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1264. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1265. }
  1266. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1267. const struct intel_crtc_state *pipe_config)
  1268. {
  1269. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1270. enum pipe pipe = crtc->pipe;
  1271. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1272. POSTING_READ(DPLL(pipe));
  1273. udelay(150);
  1274. if (intel_wait_for_register(dev_priv,
  1275. DPLL(pipe),
  1276. DPLL_LOCK_VLV,
  1277. DPLL_LOCK_VLV,
  1278. 1))
  1279. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1280. }
  1281. static void vlv_enable_pll(struct intel_crtc *crtc,
  1282. const struct intel_crtc_state *pipe_config)
  1283. {
  1284. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1285. enum pipe pipe = crtc->pipe;
  1286. assert_pipe_disabled(dev_priv, pipe);
  1287. /* PLL is protected by panel, make sure we can write it */
  1288. assert_panel_unlocked(dev_priv, pipe);
  1289. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1290. _vlv_enable_pll(crtc, pipe_config);
  1291. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1292. POSTING_READ(DPLL_MD(pipe));
  1293. }
  1294. static void _chv_enable_pll(struct intel_crtc *crtc,
  1295. const struct intel_crtc_state *pipe_config)
  1296. {
  1297. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1298. enum pipe pipe = crtc->pipe;
  1299. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1300. u32 tmp;
  1301. mutex_lock(&dev_priv->sb_lock);
  1302. /* Enable back the 10bit clock to display controller */
  1303. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1304. tmp |= DPIO_DCLKP_EN;
  1305. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1306. mutex_unlock(&dev_priv->sb_lock);
  1307. /*
  1308. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1309. */
  1310. udelay(1);
  1311. /* Enable PLL */
  1312. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1313. /* Check PLL is locked */
  1314. if (intel_wait_for_register(dev_priv,
  1315. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1316. 1))
  1317. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1318. }
  1319. static void chv_enable_pll(struct intel_crtc *crtc,
  1320. const struct intel_crtc_state *pipe_config)
  1321. {
  1322. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1323. enum pipe pipe = crtc->pipe;
  1324. assert_pipe_disabled(dev_priv, pipe);
  1325. /* PLL is protected by panel, make sure we can write it */
  1326. assert_panel_unlocked(dev_priv, pipe);
  1327. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1328. _chv_enable_pll(crtc, pipe_config);
  1329. if (pipe != PIPE_A) {
  1330. /*
  1331. * WaPixelRepeatModeFixForC0:chv
  1332. *
  1333. * DPLLCMD is AWOL. Use chicken bits to propagate
  1334. * the value from DPLLBMD to either pipe B or C.
  1335. */
  1336. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1337. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1338. I915_WRITE(CBR4_VLV, 0);
  1339. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1340. /*
  1341. * DPLLB VGA mode also seems to cause problems.
  1342. * We should always have it disabled.
  1343. */
  1344. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1345. } else {
  1346. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1347. POSTING_READ(DPLL_MD(pipe));
  1348. }
  1349. }
  1350. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1351. {
  1352. struct intel_crtc *crtc;
  1353. int count = 0;
  1354. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1355. count += crtc->base.state->active &&
  1356. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1357. }
  1358. return count;
  1359. }
  1360. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1361. const struct intel_crtc_state *crtc_state)
  1362. {
  1363. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1364. i915_reg_t reg = DPLL(crtc->pipe);
  1365. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1366. int i;
  1367. assert_pipe_disabled(dev_priv, crtc->pipe);
  1368. /* PLL is protected by panel, make sure we can write it */
  1369. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1370. assert_panel_unlocked(dev_priv, crtc->pipe);
  1371. /* Enable DVO 2x clock on both PLLs if necessary */
  1372. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1373. /*
  1374. * It appears to be important that we don't enable this
  1375. * for the current pipe before otherwise configuring the
  1376. * PLL. No idea how this should be handled if multiple
  1377. * DVO outputs are enabled simultaneosly.
  1378. */
  1379. dpll |= DPLL_DVO_2X_MODE;
  1380. I915_WRITE(DPLL(!crtc->pipe),
  1381. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1382. }
  1383. /*
  1384. * Apparently we need to have VGA mode enabled prior to changing
  1385. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1386. * dividers, even though the register value does change.
  1387. */
  1388. I915_WRITE(reg, 0);
  1389. I915_WRITE(reg, dpll);
  1390. /* Wait for the clocks to stabilize. */
  1391. POSTING_READ(reg);
  1392. udelay(150);
  1393. if (INTEL_GEN(dev_priv) >= 4) {
  1394. I915_WRITE(DPLL_MD(crtc->pipe),
  1395. crtc_state->dpll_hw_state.dpll_md);
  1396. } else {
  1397. /* The pixel multiplier can only be updated once the
  1398. * DPLL is enabled and the clocks are stable.
  1399. *
  1400. * So write it again.
  1401. */
  1402. I915_WRITE(reg, dpll);
  1403. }
  1404. /* We do this three times for luck */
  1405. for (i = 0; i < 3; i++) {
  1406. I915_WRITE(reg, dpll);
  1407. POSTING_READ(reg);
  1408. udelay(150); /* wait for warmup */
  1409. }
  1410. }
  1411. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1412. {
  1413. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1414. enum pipe pipe = crtc->pipe;
  1415. /* Disable DVO 2x clock on both PLLs if necessary */
  1416. if (IS_I830(dev_priv) &&
  1417. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1418. !intel_num_dvo_pipes(dev_priv)) {
  1419. I915_WRITE(DPLL(PIPE_B),
  1420. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1421. I915_WRITE(DPLL(PIPE_A),
  1422. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1423. }
  1424. /* Don't disable pipe or pipe PLLs if needed */
  1425. if (IS_I830(dev_priv))
  1426. return;
  1427. /* Make sure the pipe isn't still relying on us */
  1428. assert_pipe_disabled(dev_priv, pipe);
  1429. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1430. POSTING_READ(DPLL(pipe));
  1431. }
  1432. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1433. {
  1434. u32 val;
  1435. /* Make sure the pipe isn't still relying on us */
  1436. assert_pipe_disabled(dev_priv, pipe);
  1437. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1438. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1439. if (pipe != PIPE_A)
  1440. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1441. I915_WRITE(DPLL(pipe), val);
  1442. POSTING_READ(DPLL(pipe));
  1443. }
  1444. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1445. {
  1446. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1447. u32 val;
  1448. /* Make sure the pipe isn't still relying on us */
  1449. assert_pipe_disabled(dev_priv, pipe);
  1450. val = DPLL_SSC_REF_CLK_CHV |
  1451. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1452. if (pipe != PIPE_A)
  1453. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1454. I915_WRITE(DPLL(pipe), val);
  1455. POSTING_READ(DPLL(pipe));
  1456. mutex_lock(&dev_priv->sb_lock);
  1457. /* Disable 10bit clock to display controller */
  1458. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1459. val &= ~DPIO_DCLKP_EN;
  1460. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1461. mutex_unlock(&dev_priv->sb_lock);
  1462. }
  1463. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1464. struct intel_digital_port *dport,
  1465. unsigned int expected_mask)
  1466. {
  1467. u32 port_mask;
  1468. i915_reg_t dpll_reg;
  1469. switch (dport->base.port) {
  1470. case PORT_B:
  1471. port_mask = DPLL_PORTB_READY_MASK;
  1472. dpll_reg = DPLL(0);
  1473. break;
  1474. case PORT_C:
  1475. port_mask = DPLL_PORTC_READY_MASK;
  1476. dpll_reg = DPLL(0);
  1477. expected_mask <<= 4;
  1478. break;
  1479. case PORT_D:
  1480. port_mask = DPLL_PORTD_READY_MASK;
  1481. dpll_reg = DPIO_PHY_STATUS;
  1482. break;
  1483. default:
  1484. BUG();
  1485. }
  1486. if (intel_wait_for_register(dev_priv,
  1487. dpll_reg, port_mask, expected_mask,
  1488. 1000))
  1489. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1490. port_name(dport->base.port),
  1491. I915_READ(dpll_reg) & port_mask, expected_mask);
  1492. }
  1493. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1494. enum pipe pipe)
  1495. {
  1496. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1497. pipe);
  1498. i915_reg_t reg;
  1499. uint32_t val, pipeconf_val;
  1500. /* Make sure PCH DPLL is enabled */
  1501. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1502. /* FDI must be feeding us bits for PCH ports */
  1503. assert_fdi_tx_enabled(dev_priv, pipe);
  1504. assert_fdi_rx_enabled(dev_priv, pipe);
  1505. if (HAS_PCH_CPT(dev_priv)) {
  1506. /* Workaround: Set the timing override bit before enabling the
  1507. * pch transcoder. */
  1508. reg = TRANS_CHICKEN2(pipe);
  1509. val = I915_READ(reg);
  1510. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1511. I915_WRITE(reg, val);
  1512. }
  1513. reg = PCH_TRANSCONF(pipe);
  1514. val = I915_READ(reg);
  1515. pipeconf_val = I915_READ(PIPECONF(pipe));
  1516. if (HAS_PCH_IBX(dev_priv)) {
  1517. /*
  1518. * Make the BPC in transcoder be consistent with
  1519. * that in pipeconf reg. For HDMI we must use 8bpc
  1520. * here for both 8bpc and 12bpc.
  1521. */
  1522. val &= ~PIPECONF_BPC_MASK;
  1523. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1524. val |= PIPECONF_8BPC;
  1525. else
  1526. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1527. }
  1528. val &= ~TRANS_INTERLACE_MASK;
  1529. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1530. if (HAS_PCH_IBX(dev_priv) &&
  1531. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1532. val |= TRANS_LEGACY_INTERLACED_ILK;
  1533. else
  1534. val |= TRANS_INTERLACED;
  1535. else
  1536. val |= TRANS_PROGRESSIVE;
  1537. I915_WRITE(reg, val | TRANS_ENABLE);
  1538. if (intel_wait_for_register(dev_priv,
  1539. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1540. 100))
  1541. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1542. }
  1543. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1544. enum transcoder cpu_transcoder)
  1545. {
  1546. u32 val, pipeconf_val;
  1547. /* FDI must be feeding us bits for PCH ports */
  1548. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1549. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1550. /* Workaround: set timing override bit. */
  1551. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1552. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1553. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1554. val = TRANS_ENABLE;
  1555. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1556. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1557. PIPECONF_INTERLACED_ILK)
  1558. val |= TRANS_INTERLACED;
  1559. else
  1560. val |= TRANS_PROGRESSIVE;
  1561. I915_WRITE(LPT_TRANSCONF, val);
  1562. if (intel_wait_for_register(dev_priv,
  1563. LPT_TRANSCONF,
  1564. TRANS_STATE_ENABLE,
  1565. TRANS_STATE_ENABLE,
  1566. 100))
  1567. DRM_ERROR("Failed to enable PCH transcoder\n");
  1568. }
  1569. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1570. enum pipe pipe)
  1571. {
  1572. i915_reg_t reg;
  1573. uint32_t val;
  1574. /* FDI relies on the transcoder */
  1575. assert_fdi_tx_disabled(dev_priv, pipe);
  1576. assert_fdi_rx_disabled(dev_priv, pipe);
  1577. /* Ports must be off as well */
  1578. assert_pch_ports_disabled(dev_priv, pipe);
  1579. reg = PCH_TRANSCONF(pipe);
  1580. val = I915_READ(reg);
  1581. val &= ~TRANS_ENABLE;
  1582. I915_WRITE(reg, val);
  1583. /* wait for PCH transcoder off, transcoder state */
  1584. if (intel_wait_for_register(dev_priv,
  1585. reg, TRANS_STATE_ENABLE, 0,
  1586. 50))
  1587. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1588. if (HAS_PCH_CPT(dev_priv)) {
  1589. /* Workaround: Clear the timing override chicken bit again. */
  1590. reg = TRANS_CHICKEN2(pipe);
  1591. val = I915_READ(reg);
  1592. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1593. I915_WRITE(reg, val);
  1594. }
  1595. }
  1596. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1597. {
  1598. u32 val;
  1599. val = I915_READ(LPT_TRANSCONF);
  1600. val &= ~TRANS_ENABLE;
  1601. I915_WRITE(LPT_TRANSCONF, val);
  1602. /* wait for PCH transcoder off, transcoder state */
  1603. if (intel_wait_for_register(dev_priv,
  1604. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1605. 50))
  1606. DRM_ERROR("Failed to disable PCH transcoder\n");
  1607. /* Workaround: clear timing override bit. */
  1608. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1609. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1610. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1611. }
  1612. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1613. {
  1614. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1615. if (HAS_PCH_LPT(dev_priv))
  1616. return PIPE_A;
  1617. else
  1618. return crtc->pipe;
  1619. }
  1620. /**
  1621. * intel_enable_pipe - enable a pipe, asserting requirements
  1622. * @crtc: crtc responsible for the pipe
  1623. *
  1624. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1625. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1626. */
  1627. static void intel_enable_pipe(struct intel_crtc *crtc)
  1628. {
  1629. struct drm_device *dev = crtc->base.dev;
  1630. struct drm_i915_private *dev_priv = to_i915(dev);
  1631. enum pipe pipe = crtc->pipe;
  1632. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1633. i915_reg_t reg;
  1634. u32 val;
  1635. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1636. assert_planes_disabled(dev_priv, pipe);
  1637. assert_cursor_disabled(dev_priv, pipe);
  1638. assert_sprites_disabled(dev_priv, pipe);
  1639. /*
  1640. * A pipe without a PLL won't actually be able to drive bits from
  1641. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1642. * need the check.
  1643. */
  1644. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1645. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1646. assert_dsi_pll_enabled(dev_priv);
  1647. else
  1648. assert_pll_enabled(dev_priv, pipe);
  1649. } else {
  1650. if (crtc->config->has_pch_encoder) {
  1651. /* if driving the PCH, we need FDI enabled */
  1652. assert_fdi_rx_pll_enabled(dev_priv,
  1653. intel_crtc_pch_transcoder(crtc));
  1654. assert_fdi_tx_pll_enabled(dev_priv,
  1655. (enum pipe) cpu_transcoder);
  1656. }
  1657. /* FIXME: assert CPU port conditions for SNB+ */
  1658. }
  1659. reg = PIPECONF(cpu_transcoder);
  1660. val = I915_READ(reg);
  1661. if (val & PIPECONF_ENABLE) {
  1662. /* we keep both pipes enabled on 830 */
  1663. WARN_ON(!IS_I830(dev_priv));
  1664. return;
  1665. }
  1666. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1667. POSTING_READ(reg);
  1668. /*
  1669. * Until the pipe starts DSL will read as 0, which would cause
  1670. * an apparent vblank timestamp jump, which messes up also the
  1671. * frame count when it's derived from the timestamps. So let's
  1672. * wait for the pipe to start properly before we call
  1673. * drm_crtc_vblank_on()
  1674. */
  1675. if (dev->max_vblank_count == 0 &&
  1676. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1677. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1678. }
  1679. /**
  1680. * intel_disable_pipe - disable a pipe, asserting requirements
  1681. * @crtc: crtc whose pipes is to be disabled
  1682. *
  1683. * Disable the pipe of @crtc, making sure that various hardware
  1684. * specific requirements are met, if applicable, e.g. plane
  1685. * disabled, panel fitter off, etc.
  1686. *
  1687. * Will wait until the pipe has shut down before returning.
  1688. */
  1689. static void intel_disable_pipe(struct intel_crtc *crtc)
  1690. {
  1691. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1692. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1693. enum pipe pipe = crtc->pipe;
  1694. i915_reg_t reg;
  1695. u32 val;
  1696. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1697. /*
  1698. * Make sure planes won't keep trying to pump pixels to us,
  1699. * or we might hang the display.
  1700. */
  1701. assert_planes_disabled(dev_priv, pipe);
  1702. assert_cursor_disabled(dev_priv, pipe);
  1703. assert_sprites_disabled(dev_priv, pipe);
  1704. reg = PIPECONF(cpu_transcoder);
  1705. val = I915_READ(reg);
  1706. if ((val & PIPECONF_ENABLE) == 0)
  1707. return;
  1708. /*
  1709. * Double wide has implications for planes
  1710. * so best keep it disabled when not needed.
  1711. */
  1712. if (crtc->config->double_wide)
  1713. val &= ~PIPECONF_DOUBLE_WIDE;
  1714. /* Don't disable pipe or pipe PLLs if needed */
  1715. if (!IS_I830(dev_priv))
  1716. val &= ~PIPECONF_ENABLE;
  1717. I915_WRITE(reg, val);
  1718. if ((val & PIPECONF_ENABLE) == 0)
  1719. intel_wait_for_pipe_off(crtc);
  1720. }
  1721. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1722. {
  1723. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1724. }
  1725. static unsigned int
  1726. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1727. {
  1728. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1729. unsigned int cpp = fb->format->cpp[plane];
  1730. switch (fb->modifier) {
  1731. case DRM_FORMAT_MOD_LINEAR:
  1732. return cpp;
  1733. case I915_FORMAT_MOD_X_TILED:
  1734. if (IS_GEN2(dev_priv))
  1735. return 128;
  1736. else
  1737. return 512;
  1738. case I915_FORMAT_MOD_Y_TILED_CCS:
  1739. if (plane == 1)
  1740. return 128;
  1741. /* fall through */
  1742. case I915_FORMAT_MOD_Y_TILED:
  1743. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1744. return 128;
  1745. else
  1746. return 512;
  1747. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1748. if (plane == 1)
  1749. return 128;
  1750. /* fall through */
  1751. case I915_FORMAT_MOD_Yf_TILED:
  1752. switch (cpp) {
  1753. case 1:
  1754. return 64;
  1755. case 2:
  1756. case 4:
  1757. return 128;
  1758. case 8:
  1759. case 16:
  1760. return 256;
  1761. default:
  1762. MISSING_CASE(cpp);
  1763. return cpp;
  1764. }
  1765. break;
  1766. default:
  1767. MISSING_CASE(fb->modifier);
  1768. return cpp;
  1769. }
  1770. }
  1771. static unsigned int
  1772. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1773. {
  1774. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1775. return 1;
  1776. else
  1777. return intel_tile_size(to_i915(fb->dev)) /
  1778. intel_tile_width_bytes(fb, plane);
  1779. }
  1780. /* Return the tile dimensions in pixel units */
  1781. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1782. unsigned int *tile_width,
  1783. unsigned int *tile_height)
  1784. {
  1785. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1786. unsigned int cpp = fb->format->cpp[plane];
  1787. *tile_width = tile_width_bytes / cpp;
  1788. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1789. }
  1790. unsigned int
  1791. intel_fb_align_height(const struct drm_framebuffer *fb,
  1792. int plane, unsigned int height)
  1793. {
  1794. unsigned int tile_height = intel_tile_height(fb, plane);
  1795. return ALIGN(height, tile_height);
  1796. }
  1797. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1798. {
  1799. unsigned int size = 0;
  1800. int i;
  1801. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1802. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1803. return size;
  1804. }
  1805. static void
  1806. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1807. const struct drm_framebuffer *fb,
  1808. unsigned int rotation)
  1809. {
  1810. view->type = I915_GGTT_VIEW_NORMAL;
  1811. if (drm_rotation_90_or_270(rotation)) {
  1812. view->type = I915_GGTT_VIEW_ROTATED;
  1813. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1814. }
  1815. }
  1816. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1817. {
  1818. if (IS_I830(dev_priv))
  1819. return 16 * 1024;
  1820. else if (IS_I85X(dev_priv))
  1821. return 256;
  1822. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1823. return 32;
  1824. else
  1825. return 4 * 1024;
  1826. }
  1827. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1828. {
  1829. if (INTEL_INFO(dev_priv)->gen >= 9)
  1830. return 256 * 1024;
  1831. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1832. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1833. return 128 * 1024;
  1834. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1835. return 4 * 1024;
  1836. else
  1837. return 0;
  1838. }
  1839. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1840. int plane)
  1841. {
  1842. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1843. /* AUX_DIST needs only 4K alignment */
  1844. if (plane == 1)
  1845. return 4096;
  1846. switch (fb->modifier) {
  1847. case DRM_FORMAT_MOD_LINEAR:
  1848. return intel_linear_alignment(dev_priv);
  1849. case I915_FORMAT_MOD_X_TILED:
  1850. if (INTEL_GEN(dev_priv) >= 9)
  1851. return 256 * 1024;
  1852. return 0;
  1853. case I915_FORMAT_MOD_Y_TILED_CCS:
  1854. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1855. case I915_FORMAT_MOD_Y_TILED:
  1856. case I915_FORMAT_MOD_Yf_TILED:
  1857. return 1 * 1024 * 1024;
  1858. default:
  1859. MISSING_CASE(fb->modifier);
  1860. return 0;
  1861. }
  1862. }
  1863. struct i915_vma *
  1864. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1865. {
  1866. struct drm_device *dev = fb->dev;
  1867. struct drm_i915_private *dev_priv = to_i915(dev);
  1868. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1869. struct i915_ggtt_view view;
  1870. struct i915_vma *vma;
  1871. u32 alignment;
  1872. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1873. alignment = intel_surf_alignment(fb, 0);
  1874. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1875. /* Note that the w/a also requires 64 PTE of padding following the
  1876. * bo. We currently fill all unused PTE with the shadow page and so
  1877. * we should always have valid PTE following the scanout preventing
  1878. * the VT-d warning.
  1879. */
  1880. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1881. alignment = 256 * 1024;
  1882. /*
  1883. * Global gtt pte registers are special registers which actually forward
  1884. * writes to a chunk of system memory. Which means that there is no risk
  1885. * that the register values disappear as soon as we call
  1886. * intel_runtime_pm_put(), so it is correct to wrap only the
  1887. * pin/unpin/fence and not more.
  1888. */
  1889. intel_runtime_pm_get(dev_priv);
  1890. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1891. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1892. if (IS_ERR(vma))
  1893. goto err;
  1894. if (i915_vma_is_map_and_fenceable(vma)) {
  1895. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1896. * fence, whereas 965+ only requires a fence if using
  1897. * framebuffer compression. For simplicity, we always, when
  1898. * possible, install a fence as the cost is not that onerous.
  1899. *
  1900. * If we fail to fence the tiled scanout, then either the
  1901. * modeset will reject the change (which is highly unlikely as
  1902. * the affected systems, all but one, do not have unmappable
  1903. * space) or we will not be able to enable full powersaving
  1904. * techniques (also likely not to apply due to various limits
  1905. * FBC and the like impose on the size of the buffer, which
  1906. * presumably we violated anyway with this unmappable buffer).
  1907. * Anyway, it is presumably better to stumble onwards with
  1908. * something and try to run the system in a "less than optimal"
  1909. * mode that matches the user configuration.
  1910. */
  1911. i915_vma_pin_fence(vma);
  1912. }
  1913. i915_vma_get(vma);
  1914. err:
  1915. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1916. intel_runtime_pm_put(dev_priv);
  1917. return vma;
  1918. }
  1919. void intel_unpin_fb_vma(struct i915_vma *vma)
  1920. {
  1921. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1922. i915_vma_unpin_fence(vma);
  1923. i915_gem_object_unpin_from_display_plane(vma);
  1924. i915_vma_put(vma);
  1925. }
  1926. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1927. unsigned int rotation)
  1928. {
  1929. if (drm_rotation_90_or_270(rotation))
  1930. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1931. else
  1932. return fb->pitches[plane];
  1933. }
  1934. /*
  1935. * Convert the x/y offsets into a linear offset.
  1936. * Only valid with 0/180 degree rotation, which is fine since linear
  1937. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1938. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1939. */
  1940. u32 intel_fb_xy_to_linear(int x, int y,
  1941. const struct intel_plane_state *state,
  1942. int plane)
  1943. {
  1944. const struct drm_framebuffer *fb = state->base.fb;
  1945. unsigned int cpp = fb->format->cpp[plane];
  1946. unsigned int pitch = fb->pitches[plane];
  1947. return y * pitch + x * cpp;
  1948. }
  1949. /*
  1950. * Add the x/y offsets derived from fb->offsets[] to the user
  1951. * specified plane src x/y offsets. The resulting x/y offsets
  1952. * specify the start of scanout from the beginning of the gtt mapping.
  1953. */
  1954. void intel_add_fb_offsets(int *x, int *y,
  1955. const struct intel_plane_state *state,
  1956. int plane)
  1957. {
  1958. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1959. unsigned int rotation = state->base.rotation;
  1960. if (drm_rotation_90_or_270(rotation)) {
  1961. *x += intel_fb->rotated[plane].x;
  1962. *y += intel_fb->rotated[plane].y;
  1963. } else {
  1964. *x += intel_fb->normal[plane].x;
  1965. *y += intel_fb->normal[plane].y;
  1966. }
  1967. }
  1968. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1969. unsigned int tile_width,
  1970. unsigned int tile_height,
  1971. unsigned int tile_size,
  1972. unsigned int pitch_tiles,
  1973. u32 old_offset,
  1974. u32 new_offset)
  1975. {
  1976. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1977. unsigned int tiles;
  1978. WARN_ON(old_offset & (tile_size - 1));
  1979. WARN_ON(new_offset & (tile_size - 1));
  1980. WARN_ON(new_offset > old_offset);
  1981. tiles = (old_offset - new_offset) / tile_size;
  1982. *y += tiles / pitch_tiles * tile_height;
  1983. *x += tiles % pitch_tiles * tile_width;
  1984. /* minimize x in case it got needlessly big */
  1985. *y += *x / pitch_pixels * tile_height;
  1986. *x %= pitch_pixels;
  1987. return new_offset;
  1988. }
  1989. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1990. const struct drm_framebuffer *fb, int plane,
  1991. unsigned int rotation,
  1992. u32 old_offset, u32 new_offset)
  1993. {
  1994. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1995. unsigned int cpp = fb->format->cpp[plane];
  1996. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1997. WARN_ON(new_offset > old_offset);
  1998. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1999. unsigned int tile_size, tile_width, tile_height;
  2000. unsigned int pitch_tiles;
  2001. tile_size = intel_tile_size(dev_priv);
  2002. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2003. if (drm_rotation_90_or_270(rotation)) {
  2004. pitch_tiles = pitch / tile_height;
  2005. swap(tile_width, tile_height);
  2006. } else {
  2007. pitch_tiles = pitch / (tile_width * cpp);
  2008. }
  2009. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2010. tile_size, pitch_tiles,
  2011. old_offset, new_offset);
  2012. } else {
  2013. old_offset += *y * pitch + *x * cpp;
  2014. *y = (old_offset - new_offset) / pitch;
  2015. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2016. }
  2017. return new_offset;
  2018. }
  2019. /*
  2020. * Adjust the tile offset by moving the difference into
  2021. * the x/y offsets.
  2022. */
  2023. static u32 intel_adjust_tile_offset(int *x, int *y,
  2024. const struct intel_plane_state *state, int plane,
  2025. u32 old_offset, u32 new_offset)
  2026. {
  2027. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  2028. state->base.rotation,
  2029. old_offset, new_offset);
  2030. }
  2031. /*
  2032. * Computes the linear offset to the base tile and adjusts
  2033. * x, y. bytes per pixel is assumed to be a power-of-two.
  2034. *
  2035. * In the 90/270 rotated case, x and y are assumed
  2036. * to be already rotated to match the rotated GTT view, and
  2037. * pitch is the tile_height aligned framebuffer height.
  2038. *
  2039. * This function is used when computing the derived information
  2040. * under intel_framebuffer, so using any of that information
  2041. * here is not allowed. Anything under drm_framebuffer can be
  2042. * used. This is why the user has to pass in the pitch since it
  2043. * is specified in the rotated orientation.
  2044. */
  2045. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2046. int *x, int *y,
  2047. const struct drm_framebuffer *fb, int plane,
  2048. unsigned int pitch,
  2049. unsigned int rotation,
  2050. u32 alignment)
  2051. {
  2052. uint64_t fb_modifier = fb->modifier;
  2053. unsigned int cpp = fb->format->cpp[plane];
  2054. u32 offset, offset_aligned;
  2055. if (alignment)
  2056. alignment--;
  2057. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2058. unsigned int tile_size, tile_width, tile_height;
  2059. unsigned int tile_rows, tiles, pitch_tiles;
  2060. tile_size = intel_tile_size(dev_priv);
  2061. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2062. if (drm_rotation_90_or_270(rotation)) {
  2063. pitch_tiles = pitch / tile_height;
  2064. swap(tile_width, tile_height);
  2065. } else {
  2066. pitch_tiles = pitch / (tile_width * cpp);
  2067. }
  2068. tile_rows = *y / tile_height;
  2069. *y %= tile_height;
  2070. tiles = *x / tile_width;
  2071. *x %= tile_width;
  2072. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2073. offset_aligned = offset & ~alignment;
  2074. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2075. tile_size, pitch_tiles,
  2076. offset, offset_aligned);
  2077. } else {
  2078. offset = *y * pitch + *x * cpp;
  2079. offset_aligned = offset & ~alignment;
  2080. *y = (offset & alignment) / pitch;
  2081. *x = ((offset & alignment) - *y * pitch) / cpp;
  2082. }
  2083. return offset_aligned;
  2084. }
  2085. u32 intel_compute_tile_offset(int *x, int *y,
  2086. const struct intel_plane_state *state,
  2087. int plane)
  2088. {
  2089. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2090. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2091. const struct drm_framebuffer *fb = state->base.fb;
  2092. unsigned int rotation = state->base.rotation;
  2093. int pitch = intel_fb_pitch(fb, plane, rotation);
  2094. u32 alignment;
  2095. if (intel_plane->id == PLANE_CURSOR)
  2096. alignment = intel_cursor_alignment(dev_priv);
  2097. else
  2098. alignment = intel_surf_alignment(fb, plane);
  2099. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2100. rotation, alignment);
  2101. }
  2102. /* Convert the fb->offset[] into x/y offsets */
  2103. static int intel_fb_offset_to_xy(int *x, int *y,
  2104. const struct drm_framebuffer *fb, int plane)
  2105. {
  2106. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2107. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2108. fb->offsets[plane] % intel_tile_size(dev_priv))
  2109. return -EINVAL;
  2110. *x = 0;
  2111. *y = 0;
  2112. _intel_adjust_tile_offset(x, y,
  2113. fb, plane, DRM_MODE_ROTATE_0,
  2114. fb->offsets[plane], 0);
  2115. return 0;
  2116. }
  2117. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2118. {
  2119. switch (fb_modifier) {
  2120. case I915_FORMAT_MOD_X_TILED:
  2121. return I915_TILING_X;
  2122. case I915_FORMAT_MOD_Y_TILED:
  2123. case I915_FORMAT_MOD_Y_TILED_CCS:
  2124. return I915_TILING_Y;
  2125. default:
  2126. return I915_TILING_NONE;
  2127. }
  2128. }
  2129. static const struct drm_format_info ccs_formats[] = {
  2130. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2131. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2132. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2133. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2134. };
  2135. static const struct drm_format_info *
  2136. lookup_format_info(const struct drm_format_info formats[],
  2137. int num_formats, u32 format)
  2138. {
  2139. int i;
  2140. for (i = 0; i < num_formats; i++) {
  2141. if (formats[i].format == format)
  2142. return &formats[i];
  2143. }
  2144. return NULL;
  2145. }
  2146. static const struct drm_format_info *
  2147. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2148. {
  2149. switch (cmd->modifier[0]) {
  2150. case I915_FORMAT_MOD_Y_TILED_CCS:
  2151. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2152. return lookup_format_info(ccs_formats,
  2153. ARRAY_SIZE(ccs_formats),
  2154. cmd->pixel_format);
  2155. default:
  2156. return NULL;
  2157. }
  2158. }
  2159. static int
  2160. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2161. struct drm_framebuffer *fb)
  2162. {
  2163. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2164. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2165. u32 gtt_offset_rotated = 0;
  2166. unsigned int max_size = 0;
  2167. int i, num_planes = fb->format->num_planes;
  2168. unsigned int tile_size = intel_tile_size(dev_priv);
  2169. for (i = 0; i < num_planes; i++) {
  2170. unsigned int width, height;
  2171. unsigned int cpp, size;
  2172. u32 offset;
  2173. int x, y;
  2174. int ret;
  2175. cpp = fb->format->cpp[i];
  2176. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2177. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2178. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2179. if (ret) {
  2180. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2181. i, fb->offsets[i]);
  2182. return ret;
  2183. }
  2184. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2185. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2186. int hsub = fb->format->hsub;
  2187. int vsub = fb->format->vsub;
  2188. int tile_width, tile_height;
  2189. int main_x, main_y;
  2190. int ccs_x, ccs_y;
  2191. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2192. tile_width *= hsub;
  2193. tile_height *= vsub;
  2194. ccs_x = (x * hsub) % tile_width;
  2195. ccs_y = (y * vsub) % tile_height;
  2196. main_x = intel_fb->normal[0].x % tile_width;
  2197. main_y = intel_fb->normal[0].y % tile_height;
  2198. /*
  2199. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2200. * x/y offsets must match between CCS and the main surface.
  2201. */
  2202. if (main_x != ccs_x || main_y != ccs_y) {
  2203. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2204. main_x, main_y,
  2205. ccs_x, ccs_y,
  2206. intel_fb->normal[0].x,
  2207. intel_fb->normal[0].y,
  2208. x, y);
  2209. return -EINVAL;
  2210. }
  2211. }
  2212. /*
  2213. * The fence (if used) is aligned to the start of the object
  2214. * so having the framebuffer wrap around across the edge of the
  2215. * fenced region doesn't really work. We have no API to configure
  2216. * the fence start offset within the object (nor could we probably
  2217. * on gen2/3). So it's just easier if we just require that the
  2218. * fb layout agrees with the fence layout. We already check that the
  2219. * fb stride matches the fence stride elsewhere.
  2220. */
  2221. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2222. (x + width) * cpp > fb->pitches[i]) {
  2223. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2224. i, fb->offsets[i]);
  2225. return -EINVAL;
  2226. }
  2227. /*
  2228. * First pixel of the framebuffer from
  2229. * the start of the normal gtt mapping.
  2230. */
  2231. intel_fb->normal[i].x = x;
  2232. intel_fb->normal[i].y = y;
  2233. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2234. fb, i, fb->pitches[i],
  2235. DRM_MODE_ROTATE_0, tile_size);
  2236. offset /= tile_size;
  2237. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2238. unsigned int tile_width, tile_height;
  2239. unsigned int pitch_tiles;
  2240. struct drm_rect r;
  2241. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2242. rot_info->plane[i].offset = offset;
  2243. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2244. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2245. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2246. intel_fb->rotated[i].pitch =
  2247. rot_info->plane[i].height * tile_height;
  2248. /* how many tiles does this plane need */
  2249. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2250. /*
  2251. * If the plane isn't horizontally tile aligned,
  2252. * we need one more tile.
  2253. */
  2254. if (x != 0)
  2255. size++;
  2256. /* rotate the x/y offsets to match the GTT view */
  2257. r.x1 = x;
  2258. r.y1 = y;
  2259. r.x2 = x + width;
  2260. r.y2 = y + height;
  2261. drm_rect_rotate(&r,
  2262. rot_info->plane[i].width * tile_width,
  2263. rot_info->plane[i].height * tile_height,
  2264. DRM_MODE_ROTATE_270);
  2265. x = r.x1;
  2266. y = r.y1;
  2267. /* rotate the tile dimensions to match the GTT view */
  2268. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2269. swap(tile_width, tile_height);
  2270. /*
  2271. * We only keep the x/y offsets, so push all of the
  2272. * gtt offset into the x/y offsets.
  2273. */
  2274. __intel_adjust_tile_offset(&x, &y,
  2275. tile_width, tile_height,
  2276. tile_size, pitch_tiles,
  2277. gtt_offset_rotated * tile_size, 0);
  2278. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2279. /*
  2280. * First pixel of the framebuffer from
  2281. * the start of the rotated gtt mapping.
  2282. */
  2283. intel_fb->rotated[i].x = x;
  2284. intel_fb->rotated[i].y = y;
  2285. } else {
  2286. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2287. x * cpp, tile_size);
  2288. }
  2289. /* how many tiles in total needed in the bo */
  2290. max_size = max(max_size, offset + size);
  2291. }
  2292. if (max_size * tile_size > intel_fb->obj->base.size) {
  2293. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2294. max_size * tile_size, intel_fb->obj->base.size);
  2295. return -EINVAL;
  2296. }
  2297. return 0;
  2298. }
  2299. static int i9xx_format_to_fourcc(int format)
  2300. {
  2301. switch (format) {
  2302. case DISPPLANE_8BPP:
  2303. return DRM_FORMAT_C8;
  2304. case DISPPLANE_BGRX555:
  2305. return DRM_FORMAT_XRGB1555;
  2306. case DISPPLANE_BGRX565:
  2307. return DRM_FORMAT_RGB565;
  2308. default:
  2309. case DISPPLANE_BGRX888:
  2310. return DRM_FORMAT_XRGB8888;
  2311. case DISPPLANE_RGBX888:
  2312. return DRM_FORMAT_XBGR8888;
  2313. case DISPPLANE_BGRX101010:
  2314. return DRM_FORMAT_XRGB2101010;
  2315. case DISPPLANE_RGBX101010:
  2316. return DRM_FORMAT_XBGR2101010;
  2317. }
  2318. }
  2319. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2320. {
  2321. switch (format) {
  2322. case PLANE_CTL_FORMAT_RGB_565:
  2323. return DRM_FORMAT_RGB565;
  2324. default:
  2325. case PLANE_CTL_FORMAT_XRGB_8888:
  2326. if (rgb_order) {
  2327. if (alpha)
  2328. return DRM_FORMAT_ABGR8888;
  2329. else
  2330. return DRM_FORMAT_XBGR8888;
  2331. } else {
  2332. if (alpha)
  2333. return DRM_FORMAT_ARGB8888;
  2334. else
  2335. return DRM_FORMAT_XRGB8888;
  2336. }
  2337. case PLANE_CTL_FORMAT_XRGB_2101010:
  2338. if (rgb_order)
  2339. return DRM_FORMAT_XBGR2101010;
  2340. else
  2341. return DRM_FORMAT_XRGB2101010;
  2342. }
  2343. }
  2344. static bool
  2345. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2346. struct intel_initial_plane_config *plane_config)
  2347. {
  2348. struct drm_device *dev = crtc->base.dev;
  2349. struct drm_i915_private *dev_priv = to_i915(dev);
  2350. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2351. struct drm_i915_gem_object *obj = NULL;
  2352. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2353. struct drm_framebuffer *fb = &plane_config->fb->base;
  2354. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2355. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2356. PAGE_SIZE);
  2357. size_aligned -= base_aligned;
  2358. if (plane_config->size == 0)
  2359. return false;
  2360. /* If the FB is too big, just don't use it since fbdev is not very
  2361. * important and we should probably use that space with FBC or other
  2362. * features. */
  2363. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2364. return false;
  2365. mutex_lock(&dev->struct_mutex);
  2366. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2367. base_aligned,
  2368. base_aligned,
  2369. size_aligned);
  2370. mutex_unlock(&dev->struct_mutex);
  2371. if (!obj)
  2372. return false;
  2373. if (plane_config->tiling == I915_TILING_X)
  2374. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2375. mode_cmd.pixel_format = fb->format->format;
  2376. mode_cmd.width = fb->width;
  2377. mode_cmd.height = fb->height;
  2378. mode_cmd.pitches[0] = fb->pitches[0];
  2379. mode_cmd.modifier[0] = fb->modifier;
  2380. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2381. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2382. DRM_DEBUG_KMS("intel fb init failed\n");
  2383. goto out_unref_obj;
  2384. }
  2385. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2386. return true;
  2387. out_unref_obj:
  2388. i915_gem_object_put(obj);
  2389. return false;
  2390. }
  2391. static void
  2392. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2393. struct intel_plane_state *plane_state,
  2394. bool visible)
  2395. {
  2396. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2397. plane_state->base.visible = visible;
  2398. /* FIXME pre-g4x don't work like this */
  2399. if (visible) {
  2400. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2401. crtc_state->active_planes |= BIT(plane->id);
  2402. } else {
  2403. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2404. crtc_state->active_planes &= ~BIT(plane->id);
  2405. }
  2406. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2407. crtc_state->base.crtc->name,
  2408. crtc_state->active_planes);
  2409. }
  2410. static void
  2411. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2412. struct intel_initial_plane_config *plane_config)
  2413. {
  2414. struct drm_device *dev = intel_crtc->base.dev;
  2415. struct drm_i915_private *dev_priv = to_i915(dev);
  2416. struct drm_crtc *c;
  2417. struct drm_i915_gem_object *obj;
  2418. struct drm_plane *primary = intel_crtc->base.primary;
  2419. struct drm_plane_state *plane_state = primary->state;
  2420. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2421. struct intel_plane *intel_plane = to_intel_plane(primary);
  2422. struct intel_plane_state *intel_state =
  2423. to_intel_plane_state(plane_state);
  2424. struct drm_framebuffer *fb;
  2425. if (!plane_config->fb)
  2426. return;
  2427. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2428. fb = &plane_config->fb->base;
  2429. goto valid_fb;
  2430. }
  2431. kfree(plane_config->fb);
  2432. /*
  2433. * Failed to alloc the obj, check to see if we should share
  2434. * an fb with another CRTC instead
  2435. */
  2436. for_each_crtc(dev, c) {
  2437. struct intel_plane_state *state;
  2438. if (c == &intel_crtc->base)
  2439. continue;
  2440. if (!to_intel_crtc(c)->active)
  2441. continue;
  2442. state = to_intel_plane_state(c->primary->state);
  2443. if (!state->vma)
  2444. continue;
  2445. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2446. fb = c->primary->fb;
  2447. drm_framebuffer_get(fb);
  2448. goto valid_fb;
  2449. }
  2450. }
  2451. /*
  2452. * We've failed to reconstruct the BIOS FB. Current display state
  2453. * indicates that the primary plane is visible, but has a NULL FB,
  2454. * which will lead to problems later if we don't fix it up. The
  2455. * simplest solution is to just disable the primary plane now and
  2456. * pretend the BIOS never had it enabled.
  2457. */
  2458. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2459. to_intel_plane_state(plane_state),
  2460. false);
  2461. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2462. trace_intel_disable_plane(primary, intel_crtc);
  2463. intel_plane->disable_plane(intel_plane, intel_crtc);
  2464. return;
  2465. valid_fb:
  2466. mutex_lock(&dev->struct_mutex);
  2467. intel_state->vma =
  2468. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2469. mutex_unlock(&dev->struct_mutex);
  2470. if (IS_ERR(intel_state->vma)) {
  2471. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2472. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2473. intel_state->vma = NULL;
  2474. drm_framebuffer_put(fb);
  2475. return;
  2476. }
  2477. plane_state->src_x = 0;
  2478. plane_state->src_y = 0;
  2479. plane_state->src_w = fb->width << 16;
  2480. plane_state->src_h = fb->height << 16;
  2481. plane_state->crtc_x = 0;
  2482. plane_state->crtc_y = 0;
  2483. plane_state->crtc_w = fb->width;
  2484. plane_state->crtc_h = fb->height;
  2485. intel_state->base.src = drm_plane_state_src(plane_state);
  2486. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2487. obj = intel_fb_obj(fb);
  2488. if (i915_gem_object_is_tiled(obj))
  2489. dev_priv->preserve_bios_swizzle = true;
  2490. drm_framebuffer_get(fb);
  2491. primary->fb = primary->state->fb = fb;
  2492. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2493. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2494. to_intel_plane_state(plane_state),
  2495. true);
  2496. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2497. &obj->frontbuffer_bits);
  2498. }
  2499. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2500. unsigned int rotation)
  2501. {
  2502. int cpp = fb->format->cpp[plane];
  2503. switch (fb->modifier) {
  2504. case DRM_FORMAT_MOD_LINEAR:
  2505. case I915_FORMAT_MOD_X_TILED:
  2506. switch (cpp) {
  2507. case 8:
  2508. return 4096;
  2509. case 4:
  2510. case 2:
  2511. case 1:
  2512. return 8192;
  2513. default:
  2514. MISSING_CASE(cpp);
  2515. break;
  2516. }
  2517. break;
  2518. case I915_FORMAT_MOD_Y_TILED_CCS:
  2519. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2520. /* FIXME AUX plane? */
  2521. case I915_FORMAT_MOD_Y_TILED:
  2522. case I915_FORMAT_MOD_Yf_TILED:
  2523. switch (cpp) {
  2524. case 8:
  2525. return 2048;
  2526. case 4:
  2527. return 4096;
  2528. case 2:
  2529. case 1:
  2530. return 8192;
  2531. default:
  2532. MISSING_CASE(cpp);
  2533. break;
  2534. }
  2535. break;
  2536. default:
  2537. MISSING_CASE(fb->modifier);
  2538. }
  2539. return 2048;
  2540. }
  2541. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2542. int main_x, int main_y, u32 main_offset)
  2543. {
  2544. const struct drm_framebuffer *fb = plane_state->base.fb;
  2545. int hsub = fb->format->hsub;
  2546. int vsub = fb->format->vsub;
  2547. int aux_x = plane_state->aux.x;
  2548. int aux_y = plane_state->aux.y;
  2549. u32 aux_offset = plane_state->aux.offset;
  2550. u32 alignment = intel_surf_alignment(fb, 1);
  2551. while (aux_offset >= main_offset && aux_y <= main_y) {
  2552. int x, y;
  2553. if (aux_x == main_x && aux_y == main_y)
  2554. break;
  2555. if (aux_offset == 0)
  2556. break;
  2557. x = aux_x / hsub;
  2558. y = aux_y / vsub;
  2559. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2560. aux_offset, aux_offset - alignment);
  2561. aux_x = x * hsub + aux_x % hsub;
  2562. aux_y = y * vsub + aux_y % vsub;
  2563. }
  2564. if (aux_x != main_x || aux_y != main_y)
  2565. return false;
  2566. plane_state->aux.offset = aux_offset;
  2567. plane_state->aux.x = aux_x;
  2568. plane_state->aux.y = aux_y;
  2569. return true;
  2570. }
  2571. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2572. {
  2573. const struct drm_framebuffer *fb = plane_state->base.fb;
  2574. unsigned int rotation = plane_state->base.rotation;
  2575. int x = plane_state->base.src.x1 >> 16;
  2576. int y = plane_state->base.src.y1 >> 16;
  2577. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2578. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2579. int max_width = skl_max_plane_width(fb, 0, rotation);
  2580. int max_height = 4096;
  2581. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2582. if (w > max_width || h > max_height) {
  2583. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2584. w, h, max_width, max_height);
  2585. return -EINVAL;
  2586. }
  2587. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2588. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2589. alignment = intel_surf_alignment(fb, 0);
  2590. /*
  2591. * AUX surface offset is specified as the distance from the
  2592. * main surface offset, and it must be non-negative. Make
  2593. * sure that is what we will get.
  2594. */
  2595. if (offset > aux_offset)
  2596. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2597. offset, aux_offset & ~(alignment - 1));
  2598. /*
  2599. * When using an X-tiled surface, the plane blows up
  2600. * if the x offset + width exceed the stride.
  2601. *
  2602. * TODO: linear and Y-tiled seem fine, Yf untested,
  2603. */
  2604. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2605. int cpp = fb->format->cpp[0];
  2606. while ((x + w) * cpp > fb->pitches[0]) {
  2607. if (offset == 0) {
  2608. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2609. return -EINVAL;
  2610. }
  2611. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2612. offset, offset - alignment);
  2613. }
  2614. }
  2615. /*
  2616. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2617. * they match with the main surface x/y offsets.
  2618. */
  2619. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2620. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2621. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2622. if (offset == 0)
  2623. break;
  2624. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2625. offset, offset - alignment);
  2626. }
  2627. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2628. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2629. return -EINVAL;
  2630. }
  2631. }
  2632. plane_state->main.offset = offset;
  2633. plane_state->main.x = x;
  2634. plane_state->main.y = y;
  2635. return 0;
  2636. }
  2637. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2638. {
  2639. const struct drm_framebuffer *fb = plane_state->base.fb;
  2640. unsigned int rotation = plane_state->base.rotation;
  2641. int max_width = skl_max_plane_width(fb, 1, rotation);
  2642. int max_height = 4096;
  2643. int x = plane_state->base.src.x1 >> 17;
  2644. int y = plane_state->base.src.y1 >> 17;
  2645. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2646. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2647. u32 offset;
  2648. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2649. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2650. /* FIXME not quite sure how/if these apply to the chroma plane */
  2651. if (w > max_width || h > max_height) {
  2652. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2653. w, h, max_width, max_height);
  2654. return -EINVAL;
  2655. }
  2656. plane_state->aux.offset = offset;
  2657. plane_state->aux.x = x;
  2658. plane_state->aux.y = y;
  2659. return 0;
  2660. }
  2661. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2662. {
  2663. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2664. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2665. const struct drm_framebuffer *fb = plane_state->base.fb;
  2666. int src_x = plane_state->base.src.x1 >> 16;
  2667. int src_y = plane_state->base.src.y1 >> 16;
  2668. int hsub = fb->format->hsub;
  2669. int vsub = fb->format->vsub;
  2670. int x = src_x / hsub;
  2671. int y = src_y / vsub;
  2672. u32 offset;
  2673. switch (plane->id) {
  2674. case PLANE_PRIMARY:
  2675. case PLANE_SPRITE0:
  2676. break;
  2677. default:
  2678. DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
  2679. return -EINVAL;
  2680. }
  2681. if (crtc->pipe == PIPE_C) {
  2682. DRM_DEBUG_KMS("No RC support on pipe C\n");
  2683. return -EINVAL;
  2684. }
  2685. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2686. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2687. plane_state->base.rotation);
  2688. return -EINVAL;
  2689. }
  2690. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2691. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2692. plane_state->aux.offset = offset;
  2693. plane_state->aux.x = x * hsub + src_x % hsub;
  2694. plane_state->aux.y = y * vsub + src_y % vsub;
  2695. return 0;
  2696. }
  2697. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2698. {
  2699. const struct drm_framebuffer *fb = plane_state->base.fb;
  2700. unsigned int rotation = plane_state->base.rotation;
  2701. int ret;
  2702. if (!plane_state->base.visible)
  2703. return 0;
  2704. /* Rotate src coordinates to match rotated GTT view */
  2705. if (drm_rotation_90_or_270(rotation))
  2706. drm_rect_rotate(&plane_state->base.src,
  2707. fb->width << 16, fb->height << 16,
  2708. DRM_MODE_ROTATE_270);
  2709. /*
  2710. * Handle the AUX surface first since
  2711. * the main surface setup depends on it.
  2712. */
  2713. if (fb->format->format == DRM_FORMAT_NV12) {
  2714. ret = skl_check_nv12_aux_surface(plane_state);
  2715. if (ret)
  2716. return ret;
  2717. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2718. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2719. ret = skl_check_ccs_aux_surface(plane_state);
  2720. if (ret)
  2721. return ret;
  2722. } else {
  2723. plane_state->aux.offset = ~0xfff;
  2724. plane_state->aux.x = 0;
  2725. plane_state->aux.y = 0;
  2726. }
  2727. ret = skl_check_main_surface(plane_state);
  2728. if (ret)
  2729. return ret;
  2730. return 0;
  2731. }
  2732. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2733. const struct intel_plane_state *plane_state)
  2734. {
  2735. struct drm_i915_private *dev_priv =
  2736. to_i915(plane_state->base.plane->dev);
  2737. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2738. const struct drm_framebuffer *fb = plane_state->base.fb;
  2739. unsigned int rotation = plane_state->base.rotation;
  2740. u32 dspcntr;
  2741. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2742. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2743. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2744. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2745. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2746. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2747. if (INTEL_GEN(dev_priv) < 4)
  2748. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2749. switch (fb->format->format) {
  2750. case DRM_FORMAT_C8:
  2751. dspcntr |= DISPPLANE_8BPP;
  2752. break;
  2753. case DRM_FORMAT_XRGB1555:
  2754. dspcntr |= DISPPLANE_BGRX555;
  2755. break;
  2756. case DRM_FORMAT_RGB565:
  2757. dspcntr |= DISPPLANE_BGRX565;
  2758. break;
  2759. case DRM_FORMAT_XRGB8888:
  2760. dspcntr |= DISPPLANE_BGRX888;
  2761. break;
  2762. case DRM_FORMAT_XBGR8888:
  2763. dspcntr |= DISPPLANE_RGBX888;
  2764. break;
  2765. case DRM_FORMAT_XRGB2101010:
  2766. dspcntr |= DISPPLANE_BGRX101010;
  2767. break;
  2768. case DRM_FORMAT_XBGR2101010:
  2769. dspcntr |= DISPPLANE_RGBX101010;
  2770. break;
  2771. default:
  2772. MISSING_CASE(fb->format->format);
  2773. return 0;
  2774. }
  2775. if (INTEL_GEN(dev_priv) >= 4 &&
  2776. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2777. dspcntr |= DISPPLANE_TILED;
  2778. if (rotation & DRM_MODE_ROTATE_180)
  2779. dspcntr |= DISPPLANE_ROTATE_180;
  2780. if (rotation & DRM_MODE_REFLECT_X)
  2781. dspcntr |= DISPPLANE_MIRROR;
  2782. return dspcntr;
  2783. }
  2784. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2785. {
  2786. struct drm_i915_private *dev_priv =
  2787. to_i915(plane_state->base.plane->dev);
  2788. int src_x = plane_state->base.src.x1 >> 16;
  2789. int src_y = plane_state->base.src.y1 >> 16;
  2790. u32 offset;
  2791. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2792. if (INTEL_GEN(dev_priv) >= 4)
  2793. offset = intel_compute_tile_offset(&src_x, &src_y,
  2794. plane_state, 0);
  2795. else
  2796. offset = 0;
  2797. /* HSW/BDW do this automagically in hardware */
  2798. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2799. unsigned int rotation = plane_state->base.rotation;
  2800. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2801. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2802. if (rotation & DRM_MODE_ROTATE_180) {
  2803. src_x += src_w - 1;
  2804. src_y += src_h - 1;
  2805. } else if (rotation & DRM_MODE_REFLECT_X) {
  2806. src_x += src_w - 1;
  2807. }
  2808. }
  2809. plane_state->main.offset = offset;
  2810. plane_state->main.x = src_x;
  2811. plane_state->main.y = src_y;
  2812. return 0;
  2813. }
  2814. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2815. const struct intel_crtc_state *crtc_state,
  2816. const struct intel_plane_state *plane_state)
  2817. {
  2818. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2819. const struct drm_framebuffer *fb = plane_state->base.fb;
  2820. enum plane plane = primary->plane;
  2821. u32 linear_offset;
  2822. u32 dspcntr = plane_state->ctl;
  2823. i915_reg_t reg = DSPCNTR(plane);
  2824. int x = plane_state->main.x;
  2825. int y = plane_state->main.y;
  2826. unsigned long irqflags;
  2827. u32 dspaddr_offset;
  2828. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2829. if (INTEL_GEN(dev_priv) >= 4)
  2830. dspaddr_offset = plane_state->main.offset;
  2831. else
  2832. dspaddr_offset = linear_offset;
  2833. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2834. if (INTEL_GEN(dev_priv) < 4) {
  2835. /* pipesrc and dspsize control the size that is scaled from,
  2836. * which should always be the user's requested size.
  2837. */
  2838. I915_WRITE_FW(DSPSIZE(plane),
  2839. ((crtc_state->pipe_src_h - 1) << 16) |
  2840. (crtc_state->pipe_src_w - 1));
  2841. I915_WRITE_FW(DSPPOS(plane), 0);
  2842. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2843. I915_WRITE_FW(PRIMSIZE(plane),
  2844. ((crtc_state->pipe_src_h - 1) << 16) |
  2845. (crtc_state->pipe_src_w - 1));
  2846. I915_WRITE_FW(PRIMPOS(plane), 0);
  2847. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2848. }
  2849. I915_WRITE_FW(reg, dspcntr);
  2850. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2851. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2852. I915_WRITE_FW(DSPSURF(plane),
  2853. intel_plane_ggtt_offset(plane_state) +
  2854. dspaddr_offset);
  2855. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2856. } else if (INTEL_GEN(dev_priv) >= 4) {
  2857. I915_WRITE_FW(DSPSURF(plane),
  2858. intel_plane_ggtt_offset(plane_state) +
  2859. dspaddr_offset);
  2860. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2861. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2862. } else {
  2863. I915_WRITE_FW(DSPADDR(plane),
  2864. intel_plane_ggtt_offset(plane_state) +
  2865. dspaddr_offset);
  2866. }
  2867. POSTING_READ_FW(reg);
  2868. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2869. }
  2870. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2871. struct intel_crtc *crtc)
  2872. {
  2873. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2874. enum plane plane = primary->plane;
  2875. unsigned long irqflags;
  2876. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2877. I915_WRITE_FW(DSPCNTR(plane), 0);
  2878. if (INTEL_INFO(dev_priv)->gen >= 4)
  2879. I915_WRITE_FW(DSPSURF(plane), 0);
  2880. else
  2881. I915_WRITE_FW(DSPADDR(plane), 0);
  2882. POSTING_READ_FW(DSPCNTR(plane));
  2883. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2884. }
  2885. static u32
  2886. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2887. {
  2888. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2889. return 64;
  2890. else
  2891. return intel_tile_width_bytes(fb, plane);
  2892. }
  2893. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2894. {
  2895. struct drm_device *dev = intel_crtc->base.dev;
  2896. struct drm_i915_private *dev_priv = to_i915(dev);
  2897. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2898. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2899. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2900. }
  2901. /*
  2902. * This function detaches (aka. unbinds) unused scalers in hardware
  2903. */
  2904. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2905. {
  2906. struct intel_crtc_scaler_state *scaler_state;
  2907. int i;
  2908. scaler_state = &intel_crtc->config->scaler_state;
  2909. /* loop through and disable scalers that aren't in use */
  2910. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2911. if (!scaler_state->scalers[i].in_use)
  2912. skl_detach_scaler(intel_crtc, i);
  2913. }
  2914. }
  2915. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2916. unsigned int rotation)
  2917. {
  2918. u32 stride;
  2919. if (plane >= fb->format->num_planes)
  2920. return 0;
  2921. stride = intel_fb_pitch(fb, plane, rotation);
  2922. /*
  2923. * The stride is either expressed as a multiple of 64 bytes chunks for
  2924. * linear buffers or in number of tiles for tiled buffers.
  2925. */
  2926. if (drm_rotation_90_or_270(rotation))
  2927. stride /= intel_tile_height(fb, plane);
  2928. else
  2929. stride /= intel_fb_stride_alignment(fb, plane);
  2930. return stride;
  2931. }
  2932. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2933. {
  2934. switch (pixel_format) {
  2935. case DRM_FORMAT_C8:
  2936. return PLANE_CTL_FORMAT_INDEXED;
  2937. case DRM_FORMAT_RGB565:
  2938. return PLANE_CTL_FORMAT_RGB_565;
  2939. case DRM_FORMAT_XBGR8888:
  2940. case DRM_FORMAT_ABGR8888:
  2941. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2942. case DRM_FORMAT_XRGB8888:
  2943. case DRM_FORMAT_ARGB8888:
  2944. return PLANE_CTL_FORMAT_XRGB_8888;
  2945. case DRM_FORMAT_XRGB2101010:
  2946. return PLANE_CTL_FORMAT_XRGB_2101010;
  2947. case DRM_FORMAT_XBGR2101010:
  2948. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2949. case DRM_FORMAT_YUYV:
  2950. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2951. case DRM_FORMAT_YVYU:
  2952. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2953. case DRM_FORMAT_UYVY:
  2954. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2955. case DRM_FORMAT_VYUY:
  2956. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2957. default:
  2958. MISSING_CASE(pixel_format);
  2959. }
  2960. return 0;
  2961. }
  2962. /*
  2963. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2964. * to be already pre-multiplied. We need to add a knob (or a different
  2965. * DRM_FORMAT) for user-space to configure that.
  2966. */
  2967. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  2968. {
  2969. switch (pixel_format) {
  2970. case DRM_FORMAT_ABGR8888:
  2971. case DRM_FORMAT_ARGB8888:
  2972. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2973. default:
  2974. return PLANE_CTL_ALPHA_DISABLE;
  2975. }
  2976. }
  2977. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  2978. {
  2979. switch (pixel_format) {
  2980. case DRM_FORMAT_ABGR8888:
  2981. case DRM_FORMAT_ARGB8888:
  2982. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  2983. default:
  2984. return PLANE_COLOR_ALPHA_DISABLE;
  2985. }
  2986. }
  2987. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2988. {
  2989. switch (fb_modifier) {
  2990. case DRM_FORMAT_MOD_LINEAR:
  2991. break;
  2992. case I915_FORMAT_MOD_X_TILED:
  2993. return PLANE_CTL_TILED_X;
  2994. case I915_FORMAT_MOD_Y_TILED:
  2995. return PLANE_CTL_TILED_Y;
  2996. case I915_FORMAT_MOD_Y_TILED_CCS:
  2997. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2998. case I915_FORMAT_MOD_Yf_TILED:
  2999. return PLANE_CTL_TILED_YF;
  3000. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3001. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  3002. default:
  3003. MISSING_CASE(fb_modifier);
  3004. }
  3005. return 0;
  3006. }
  3007. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  3008. {
  3009. switch (rotation) {
  3010. case DRM_MODE_ROTATE_0:
  3011. break;
  3012. /*
  3013. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3014. * while i915 HW rotation is clockwise, thats why this swapping.
  3015. */
  3016. case DRM_MODE_ROTATE_90:
  3017. return PLANE_CTL_ROTATE_270;
  3018. case DRM_MODE_ROTATE_180:
  3019. return PLANE_CTL_ROTATE_180;
  3020. case DRM_MODE_ROTATE_270:
  3021. return PLANE_CTL_ROTATE_90;
  3022. default:
  3023. MISSING_CASE(rotation);
  3024. }
  3025. return 0;
  3026. }
  3027. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3028. const struct intel_plane_state *plane_state)
  3029. {
  3030. struct drm_i915_private *dev_priv =
  3031. to_i915(plane_state->base.plane->dev);
  3032. const struct drm_framebuffer *fb = plane_state->base.fb;
  3033. unsigned int rotation = plane_state->base.rotation;
  3034. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3035. u32 plane_ctl;
  3036. plane_ctl = PLANE_CTL_ENABLE;
  3037. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3038. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3039. plane_ctl |=
  3040. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3041. PLANE_CTL_PIPE_CSC_ENABLE |
  3042. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3043. }
  3044. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3045. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3046. plane_ctl |= skl_plane_ctl_rotation(rotation);
  3047. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3048. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3049. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3050. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3051. return plane_ctl;
  3052. }
  3053. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3054. const struct intel_plane_state *plane_state)
  3055. {
  3056. const struct drm_framebuffer *fb = plane_state->base.fb;
  3057. u32 plane_color_ctl = 0;
  3058. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3059. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3060. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3061. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3062. return plane_color_ctl;
  3063. }
  3064. static int
  3065. __intel_display_resume(struct drm_device *dev,
  3066. struct drm_atomic_state *state,
  3067. struct drm_modeset_acquire_ctx *ctx)
  3068. {
  3069. struct drm_crtc_state *crtc_state;
  3070. struct drm_crtc *crtc;
  3071. int i, ret;
  3072. intel_modeset_setup_hw_state(dev, ctx);
  3073. i915_redisable_vga(to_i915(dev));
  3074. if (!state)
  3075. return 0;
  3076. /*
  3077. * We've duplicated the state, pointers to the old state are invalid.
  3078. *
  3079. * Don't attempt to use the old state until we commit the duplicated state.
  3080. */
  3081. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3082. /*
  3083. * Force recalculation even if we restore
  3084. * current state. With fast modeset this may not result
  3085. * in a modeset when the state is compatible.
  3086. */
  3087. crtc_state->mode_changed = true;
  3088. }
  3089. /* ignore any reset values/BIOS leftovers in the WM registers */
  3090. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3091. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3092. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3093. WARN_ON(ret == -EDEADLK);
  3094. return ret;
  3095. }
  3096. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3097. {
  3098. return intel_has_gpu_reset(dev_priv) &&
  3099. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3100. }
  3101. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3102. {
  3103. struct drm_device *dev = &dev_priv->drm;
  3104. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3105. struct drm_atomic_state *state;
  3106. int ret;
  3107. /* reset doesn't touch the display */
  3108. if (!i915_modparams.force_reset_modeset_test &&
  3109. !gpu_reset_clobbers_display(dev_priv))
  3110. return;
  3111. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3112. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3113. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3114. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3115. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3116. i915_gem_set_wedged(dev_priv);
  3117. }
  3118. /*
  3119. * Need mode_config.mutex so that we don't
  3120. * trample ongoing ->detect() and whatnot.
  3121. */
  3122. mutex_lock(&dev->mode_config.mutex);
  3123. drm_modeset_acquire_init(ctx, 0);
  3124. while (1) {
  3125. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3126. if (ret != -EDEADLK)
  3127. break;
  3128. drm_modeset_backoff(ctx);
  3129. }
  3130. /*
  3131. * Disabling the crtcs gracefully seems nicer. Also the
  3132. * g33 docs say we should at least disable all the planes.
  3133. */
  3134. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3135. if (IS_ERR(state)) {
  3136. ret = PTR_ERR(state);
  3137. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3138. return;
  3139. }
  3140. ret = drm_atomic_helper_disable_all(dev, ctx);
  3141. if (ret) {
  3142. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3143. drm_atomic_state_put(state);
  3144. return;
  3145. }
  3146. dev_priv->modeset_restore_state = state;
  3147. state->acquire_ctx = ctx;
  3148. }
  3149. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3150. {
  3151. struct drm_device *dev = &dev_priv->drm;
  3152. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3153. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3154. int ret;
  3155. /* reset doesn't touch the display */
  3156. if (!i915_modparams.force_reset_modeset_test &&
  3157. !gpu_reset_clobbers_display(dev_priv))
  3158. return;
  3159. if (!state)
  3160. goto unlock;
  3161. dev_priv->modeset_restore_state = NULL;
  3162. /* reset doesn't touch the display */
  3163. if (!gpu_reset_clobbers_display(dev_priv)) {
  3164. /* for testing only restore the display */
  3165. ret = __intel_display_resume(dev, state, ctx);
  3166. if (ret)
  3167. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3168. } else {
  3169. /*
  3170. * The display has been reset as well,
  3171. * so need a full re-initialization.
  3172. */
  3173. intel_runtime_pm_disable_interrupts(dev_priv);
  3174. intel_runtime_pm_enable_interrupts(dev_priv);
  3175. intel_pps_unlock_regs_wa(dev_priv);
  3176. intel_modeset_init_hw(dev);
  3177. intel_init_clock_gating(dev_priv);
  3178. spin_lock_irq(&dev_priv->irq_lock);
  3179. if (dev_priv->display.hpd_irq_setup)
  3180. dev_priv->display.hpd_irq_setup(dev_priv);
  3181. spin_unlock_irq(&dev_priv->irq_lock);
  3182. ret = __intel_display_resume(dev, state, ctx);
  3183. if (ret)
  3184. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3185. intel_hpd_init(dev_priv);
  3186. }
  3187. drm_atomic_state_put(state);
  3188. unlock:
  3189. drm_modeset_drop_locks(ctx);
  3190. drm_modeset_acquire_fini(ctx);
  3191. mutex_unlock(&dev->mode_config.mutex);
  3192. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3193. }
  3194. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3195. const struct intel_crtc_state *new_crtc_state)
  3196. {
  3197. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3198. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3199. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3200. crtc->base.mode = new_crtc_state->base.mode;
  3201. /*
  3202. * Update pipe size and adjust fitter if needed: the reason for this is
  3203. * that in compute_mode_changes we check the native mode (not the pfit
  3204. * mode) to see if we can flip rather than do a full mode set. In the
  3205. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3206. * pfit state, we'll end up with a big fb scanned out into the wrong
  3207. * sized surface.
  3208. */
  3209. I915_WRITE(PIPESRC(crtc->pipe),
  3210. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3211. (new_crtc_state->pipe_src_h - 1));
  3212. /* on skylake this is done by detaching scalers */
  3213. if (INTEL_GEN(dev_priv) >= 9) {
  3214. skl_detach_scalers(crtc);
  3215. if (new_crtc_state->pch_pfit.enabled)
  3216. skylake_pfit_enable(crtc);
  3217. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3218. if (new_crtc_state->pch_pfit.enabled)
  3219. ironlake_pfit_enable(crtc);
  3220. else if (old_crtc_state->pch_pfit.enabled)
  3221. ironlake_pfit_disable(crtc, true);
  3222. }
  3223. }
  3224. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3225. {
  3226. struct drm_device *dev = crtc->base.dev;
  3227. struct drm_i915_private *dev_priv = to_i915(dev);
  3228. int pipe = crtc->pipe;
  3229. i915_reg_t reg;
  3230. u32 temp;
  3231. /* enable normal train */
  3232. reg = FDI_TX_CTL(pipe);
  3233. temp = I915_READ(reg);
  3234. if (IS_IVYBRIDGE(dev_priv)) {
  3235. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3236. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3237. } else {
  3238. temp &= ~FDI_LINK_TRAIN_NONE;
  3239. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3240. }
  3241. I915_WRITE(reg, temp);
  3242. reg = FDI_RX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. if (HAS_PCH_CPT(dev_priv)) {
  3245. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3246. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3247. } else {
  3248. temp &= ~FDI_LINK_TRAIN_NONE;
  3249. temp |= FDI_LINK_TRAIN_NONE;
  3250. }
  3251. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3252. /* wait one idle pattern time */
  3253. POSTING_READ(reg);
  3254. udelay(1000);
  3255. /* IVB wants error correction enabled */
  3256. if (IS_IVYBRIDGE(dev_priv))
  3257. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3258. FDI_FE_ERRC_ENABLE);
  3259. }
  3260. /* The FDI link training functions for ILK/Ibexpeak. */
  3261. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3262. const struct intel_crtc_state *crtc_state)
  3263. {
  3264. struct drm_device *dev = crtc->base.dev;
  3265. struct drm_i915_private *dev_priv = to_i915(dev);
  3266. int pipe = crtc->pipe;
  3267. i915_reg_t reg;
  3268. u32 temp, tries;
  3269. /* FDI needs bits from pipe first */
  3270. assert_pipe_enabled(dev_priv, pipe);
  3271. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3272. for train result */
  3273. reg = FDI_RX_IMR(pipe);
  3274. temp = I915_READ(reg);
  3275. temp &= ~FDI_RX_SYMBOL_LOCK;
  3276. temp &= ~FDI_RX_BIT_LOCK;
  3277. I915_WRITE(reg, temp);
  3278. I915_READ(reg);
  3279. udelay(150);
  3280. /* enable CPU FDI TX and PCH FDI RX */
  3281. reg = FDI_TX_CTL(pipe);
  3282. temp = I915_READ(reg);
  3283. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3284. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3285. temp &= ~FDI_LINK_TRAIN_NONE;
  3286. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3287. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3288. reg = FDI_RX_CTL(pipe);
  3289. temp = I915_READ(reg);
  3290. temp &= ~FDI_LINK_TRAIN_NONE;
  3291. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3292. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3293. POSTING_READ(reg);
  3294. udelay(150);
  3295. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3296. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3297. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3298. FDI_RX_PHASE_SYNC_POINTER_EN);
  3299. reg = FDI_RX_IIR(pipe);
  3300. for (tries = 0; tries < 5; tries++) {
  3301. temp = I915_READ(reg);
  3302. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3303. if ((temp & FDI_RX_BIT_LOCK)) {
  3304. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3305. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3306. break;
  3307. }
  3308. }
  3309. if (tries == 5)
  3310. DRM_ERROR("FDI train 1 fail!\n");
  3311. /* Train 2 */
  3312. reg = FDI_TX_CTL(pipe);
  3313. temp = I915_READ(reg);
  3314. temp &= ~FDI_LINK_TRAIN_NONE;
  3315. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3316. I915_WRITE(reg, temp);
  3317. reg = FDI_RX_CTL(pipe);
  3318. temp = I915_READ(reg);
  3319. temp &= ~FDI_LINK_TRAIN_NONE;
  3320. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3321. I915_WRITE(reg, temp);
  3322. POSTING_READ(reg);
  3323. udelay(150);
  3324. reg = FDI_RX_IIR(pipe);
  3325. for (tries = 0; tries < 5; tries++) {
  3326. temp = I915_READ(reg);
  3327. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3328. if (temp & FDI_RX_SYMBOL_LOCK) {
  3329. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3330. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3331. break;
  3332. }
  3333. }
  3334. if (tries == 5)
  3335. DRM_ERROR("FDI train 2 fail!\n");
  3336. DRM_DEBUG_KMS("FDI train done\n");
  3337. }
  3338. static const int snb_b_fdi_train_param[] = {
  3339. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3340. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3341. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3342. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3343. };
  3344. /* The FDI link training functions for SNB/Cougarpoint. */
  3345. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3346. const struct intel_crtc_state *crtc_state)
  3347. {
  3348. struct drm_device *dev = crtc->base.dev;
  3349. struct drm_i915_private *dev_priv = to_i915(dev);
  3350. int pipe = crtc->pipe;
  3351. i915_reg_t reg;
  3352. u32 temp, i, retry;
  3353. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3354. for train result */
  3355. reg = FDI_RX_IMR(pipe);
  3356. temp = I915_READ(reg);
  3357. temp &= ~FDI_RX_SYMBOL_LOCK;
  3358. temp &= ~FDI_RX_BIT_LOCK;
  3359. I915_WRITE(reg, temp);
  3360. POSTING_READ(reg);
  3361. udelay(150);
  3362. /* enable CPU FDI TX and PCH FDI RX */
  3363. reg = FDI_TX_CTL(pipe);
  3364. temp = I915_READ(reg);
  3365. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3366. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3367. temp &= ~FDI_LINK_TRAIN_NONE;
  3368. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3369. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3370. /* SNB-B */
  3371. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3372. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3373. I915_WRITE(FDI_RX_MISC(pipe),
  3374. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3375. reg = FDI_RX_CTL(pipe);
  3376. temp = I915_READ(reg);
  3377. if (HAS_PCH_CPT(dev_priv)) {
  3378. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3379. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3380. } else {
  3381. temp &= ~FDI_LINK_TRAIN_NONE;
  3382. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3383. }
  3384. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3385. POSTING_READ(reg);
  3386. udelay(150);
  3387. for (i = 0; i < 4; i++) {
  3388. reg = FDI_TX_CTL(pipe);
  3389. temp = I915_READ(reg);
  3390. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3391. temp |= snb_b_fdi_train_param[i];
  3392. I915_WRITE(reg, temp);
  3393. POSTING_READ(reg);
  3394. udelay(500);
  3395. for (retry = 0; retry < 5; retry++) {
  3396. reg = FDI_RX_IIR(pipe);
  3397. temp = I915_READ(reg);
  3398. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3399. if (temp & FDI_RX_BIT_LOCK) {
  3400. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3401. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3402. break;
  3403. }
  3404. udelay(50);
  3405. }
  3406. if (retry < 5)
  3407. break;
  3408. }
  3409. if (i == 4)
  3410. DRM_ERROR("FDI train 1 fail!\n");
  3411. /* Train 2 */
  3412. reg = FDI_TX_CTL(pipe);
  3413. temp = I915_READ(reg);
  3414. temp &= ~FDI_LINK_TRAIN_NONE;
  3415. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3416. if (IS_GEN6(dev_priv)) {
  3417. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3418. /* SNB-B */
  3419. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3420. }
  3421. I915_WRITE(reg, temp);
  3422. reg = FDI_RX_CTL(pipe);
  3423. temp = I915_READ(reg);
  3424. if (HAS_PCH_CPT(dev_priv)) {
  3425. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3426. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3427. } else {
  3428. temp &= ~FDI_LINK_TRAIN_NONE;
  3429. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3430. }
  3431. I915_WRITE(reg, temp);
  3432. POSTING_READ(reg);
  3433. udelay(150);
  3434. for (i = 0; i < 4; i++) {
  3435. reg = FDI_TX_CTL(pipe);
  3436. temp = I915_READ(reg);
  3437. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3438. temp |= snb_b_fdi_train_param[i];
  3439. I915_WRITE(reg, temp);
  3440. POSTING_READ(reg);
  3441. udelay(500);
  3442. for (retry = 0; retry < 5; retry++) {
  3443. reg = FDI_RX_IIR(pipe);
  3444. temp = I915_READ(reg);
  3445. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3446. if (temp & FDI_RX_SYMBOL_LOCK) {
  3447. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3448. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3449. break;
  3450. }
  3451. udelay(50);
  3452. }
  3453. if (retry < 5)
  3454. break;
  3455. }
  3456. if (i == 4)
  3457. DRM_ERROR("FDI train 2 fail!\n");
  3458. DRM_DEBUG_KMS("FDI train done.\n");
  3459. }
  3460. /* Manual link training for Ivy Bridge A0 parts */
  3461. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3462. const struct intel_crtc_state *crtc_state)
  3463. {
  3464. struct drm_device *dev = crtc->base.dev;
  3465. struct drm_i915_private *dev_priv = to_i915(dev);
  3466. int pipe = crtc->pipe;
  3467. i915_reg_t reg;
  3468. u32 temp, i, j;
  3469. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3470. for train result */
  3471. reg = FDI_RX_IMR(pipe);
  3472. temp = I915_READ(reg);
  3473. temp &= ~FDI_RX_SYMBOL_LOCK;
  3474. temp &= ~FDI_RX_BIT_LOCK;
  3475. I915_WRITE(reg, temp);
  3476. POSTING_READ(reg);
  3477. udelay(150);
  3478. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3479. I915_READ(FDI_RX_IIR(pipe)));
  3480. /* Try each vswing and preemphasis setting twice before moving on */
  3481. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3482. /* disable first in case we need to retry */
  3483. reg = FDI_TX_CTL(pipe);
  3484. temp = I915_READ(reg);
  3485. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3486. temp &= ~FDI_TX_ENABLE;
  3487. I915_WRITE(reg, temp);
  3488. reg = FDI_RX_CTL(pipe);
  3489. temp = I915_READ(reg);
  3490. temp &= ~FDI_LINK_TRAIN_AUTO;
  3491. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3492. temp &= ~FDI_RX_ENABLE;
  3493. I915_WRITE(reg, temp);
  3494. /* enable CPU FDI TX and PCH FDI RX */
  3495. reg = FDI_TX_CTL(pipe);
  3496. temp = I915_READ(reg);
  3497. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3498. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3499. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3500. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3501. temp |= snb_b_fdi_train_param[j/2];
  3502. temp |= FDI_COMPOSITE_SYNC;
  3503. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3504. I915_WRITE(FDI_RX_MISC(pipe),
  3505. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3506. reg = FDI_RX_CTL(pipe);
  3507. temp = I915_READ(reg);
  3508. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3509. temp |= FDI_COMPOSITE_SYNC;
  3510. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3511. POSTING_READ(reg);
  3512. udelay(1); /* should be 0.5us */
  3513. for (i = 0; i < 4; i++) {
  3514. reg = FDI_RX_IIR(pipe);
  3515. temp = I915_READ(reg);
  3516. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3517. if (temp & FDI_RX_BIT_LOCK ||
  3518. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3519. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3520. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3521. i);
  3522. break;
  3523. }
  3524. udelay(1); /* should be 0.5us */
  3525. }
  3526. if (i == 4) {
  3527. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3528. continue;
  3529. }
  3530. /* Train 2 */
  3531. reg = FDI_TX_CTL(pipe);
  3532. temp = I915_READ(reg);
  3533. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3534. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3535. I915_WRITE(reg, temp);
  3536. reg = FDI_RX_CTL(pipe);
  3537. temp = I915_READ(reg);
  3538. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3539. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3540. I915_WRITE(reg, temp);
  3541. POSTING_READ(reg);
  3542. udelay(2); /* should be 1.5us */
  3543. for (i = 0; i < 4; i++) {
  3544. reg = FDI_RX_IIR(pipe);
  3545. temp = I915_READ(reg);
  3546. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3547. if (temp & FDI_RX_SYMBOL_LOCK ||
  3548. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3549. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3550. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3551. i);
  3552. goto train_done;
  3553. }
  3554. udelay(2); /* should be 1.5us */
  3555. }
  3556. if (i == 4)
  3557. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3558. }
  3559. train_done:
  3560. DRM_DEBUG_KMS("FDI train done.\n");
  3561. }
  3562. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3563. {
  3564. struct drm_device *dev = intel_crtc->base.dev;
  3565. struct drm_i915_private *dev_priv = to_i915(dev);
  3566. int pipe = intel_crtc->pipe;
  3567. i915_reg_t reg;
  3568. u32 temp;
  3569. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3570. reg = FDI_RX_CTL(pipe);
  3571. temp = I915_READ(reg);
  3572. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3573. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3574. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3575. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3576. POSTING_READ(reg);
  3577. udelay(200);
  3578. /* Switch from Rawclk to PCDclk */
  3579. temp = I915_READ(reg);
  3580. I915_WRITE(reg, temp | FDI_PCDCLK);
  3581. POSTING_READ(reg);
  3582. udelay(200);
  3583. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3584. reg = FDI_TX_CTL(pipe);
  3585. temp = I915_READ(reg);
  3586. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3587. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3588. POSTING_READ(reg);
  3589. udelay(100);
  3590. }
  3591. }
  3592. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3593. {
  3594. struct drm_device *dev = intel_crtc->base.dev;
  3595. struct drm_i915_private *dev_priv = to_i915(dev);
  3596. int pipe = intel_crtc->pipe;
  3597. i915_reg_t reg;
  3598. u32 temp;
  3599. /* Switch from PCDclk to Rawclk */
  3600. reg = FDI_RX_CTL(pipe);
  3601. temp = I915_READ(reg);
  3602. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3603. /* Disable CPU FDI TX PLL */
  3604. reg = FDI_TX_CTL(pipe);
  3605. temp = I915_READ(reg);
  3606. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3607. POSTING_READ(reg);
  3608. udelay(100);
  3609. reg = FDI_RX_CTL(pipe);
  3610. temp = I915_READ(reg);
  3611. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3612. /* Wait for the clocks to turn off. */
  3613. POSTING_READ(reg);
  3614. udelay(100);
  3615. }
  3616. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3617. {
  3618. struct drm_device *dev = crtc->dev;
  3619. struct drm_i915_private *dev_priv = to_i915(dev);
  3620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3621. int pipe = intel_crtc->pipe;
  3622. i915_reg_t reg;
  3623. u32 temp;
  3624. /* disable CPU FDI tx and PCH FDI rx */
  3625. reg = FDI_TX_CTL(pipe);
  3626. temp = I915_READ(reg);
  3627. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3628. POSTING_READ(reg);
  3629. reg = FDI_RX_CTL(pipe);
  3630. temp = I915_READ(reg);
  3631. temp &= ~(0x7 << 16);
  3632. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3633. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3634. POSTING_READ(reg);
  3635. udelay(100);
  3636. /* Ironlake workaround, disable clock pointer after downing FDI */
  3637. if (HAS_PCH_IBX(dev_priv))
  3638. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3639. /* still set train pattern 1 */
  3640. reg = FDI_TX_CTL(pipe);
  3641. temp = I915_READ(reg);
  3642. temp &= ~FDI_LINK_TRAIN_NONE;
  3643. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3644. I915_WRITE(reg, temp);
  3645. reg = FDI_RX_CTL(pipe);
  3646. temp = I915_READ(reg);
  3647. if (HAS_PCH_CPT(dev_priv)) {
  3648. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3649. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3650. } else {
  3651. temp &= ~FDI_LINK_TRAIN_NONE;
  3652. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3653. }
  3654. /* BPC in FDI rx is consistent with that in PIPECONF */
  3655. temp &= ~(0x07 << 16);
  3656. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3657. I915_WRITE(reg, temp);
  3658. POSTING_READ(reg);
  3659. udelay(100);
  3660. }
  3661. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3662. {
  3663. struct drm_crtc *crtc;
  3664. bool cleanup_done;
  3665. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3666. struct drm_crtc_commit *commit;
  3667. spin_lock(&crtc->commit_lock);
  3668. commit = list_first_entry_or_null(&crtc->commit_list,
  3669. struct drm_crtc_commit, commit_entry);
  3670. cleanup_done = commit ?
  3671. try_wait_for_completion(&commit->cleanup_done) : true;
  3672. spin_unlock(&crtc->commit_lock);
  3673. if (cleanup_done)
  3674. continue;
  3675. drm_crtc_wait_one_vblank(crtc);
  3676. return true;
  3677. }
  3678. return false;
  3679. }
  3680. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3681. {
  3682. u32 temp;
  3683. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3684. mutex_lock(&dev_priv->sb_lock);
  3685. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3686. temp |= SBI_SSCCTL_DISABLE;
  3687. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3688. mutex_unlock(&dev_priv->sb_lock);
  3689. }
  3690. /* Program iCLKIP clock to the desired frequency */
  3691. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3692. {
  3693. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3694. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3695. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3696. u32 temp;
  3697. lpt_disable_iclkip(dev_priv);
  3698. /* The iCLK virtual clock root frequency is in MHz,
  3699. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3700. * divisors, it is necessary to divide one by another, so we
  3701. * convert the virtual clock precision to KHz here for higher
  3702. * precision.
  3703. */
  3704. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3705. u32 iclk_virtual_root_freq = 172800 * 1000;
  3706. u32 iclk_pi_range = 64;
  3707. u32 desired_divisor;
  3708. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3709. clock << auxdiv);
  3710. divsel = (desired_divisor / iclk_pi_range) - 2;
  3711. phaseinc = desired_divisor % iclk_pi_range;
  3712. /*
  3713. * Near 20MHz is a corner case which is
  3714. * out of range for the 7-bit divisor
  3715. */
  3716. if (divsel <= 0x7f)
  3717. break;
  3718. }
  3719. /* This should not happen with any sane values */
  3720. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3721. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3722. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3723. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3724. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3725. clock,
  3726. auxdiv,
  3727. divsel,
  3728. phasedir,
  3729. phaseinc);
  3730. mutex_lock(&dev_priv->sb_lock);
  3731. /* Program SSCDIVINTPHASE6 */
  3732. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3733. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3734. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3735. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3736. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3737. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3738. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3739. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3740. /* Program SSCAUXDIV */
  3741. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3742. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3743. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3744. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3745. /* Enable modulator and associated divider */
  3746. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3747. temp &= ~SBI_SSCCTL_DISABLE;
  3748. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3749. mutex_unlock(&dev_priv->sb_lock);
  3750. /* Wait for initialization time */
  3751. udelay(24);
  3752. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3753. }
  3754. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3755. {
  3756. u32 divsel, phaseinc, auxdiv;
  3757. u32 iclk_virtual_root_freq = 172800 * 1000;
  3758. u32 iclk_pi_range = 64;
  3759. u32 desired_divisor;
  3760. u32 temp;
  3761. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3762. return 0;
  3763. mutex_lock(&dev_priv->sb_lock);
  3764. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3765. if (temp & SBI_SSCCTL_DISABLE) {
  3766. mutex_unlock(&dev_priv->sb_lock);
  3767. return 0;
  3768. }
  3769. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3770. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3771. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3772. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3773. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3774. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3775. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3776. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3777. mutex_unlock(&dev_priv->sb_lock);
  3778. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3779. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3780. desired_divisor << auxdiv);
  3781. }
  3782. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3783. enum pipe pch_transcoder)
  3784. {
  3785. struct drm_device *dev = crtc->base.dev;
  3786. struct drm_i915_private *dev_priv = to_i915(dev);
  3787. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3788. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3789. I915_READ(HTOTAL(cpu_transcoder)));
  3790. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3791. I915_READ(HBLANK(cpu_transcoder)));
  3792. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3793. I915_READ(HSYNC(cpu_transcoder)));
  3794. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3795. I915_READ(VTOTAL(cpu_transcoder)));
  3796. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3797. I915_READ(VBLANK(cpu_transcoder)));
  3798. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3799. I915_READ(VSYNC(cpu_transcoder)));
  3800. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3801. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3802. }
  3803. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3804. {
  3805. struct drm_i915_private *dev_priv = to_i915(dev);
  3806. uint32_t temp;
  3807. temp = I915_READ(SOUTH_CHICKEN1);
  3808. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3809. return;
  3810. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3811. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3812. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3813. if (enable)
  3814. temp |= FDI_BC_BIFURCATION_SELECT;
  3815. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3816. I915_WRITE(SOUTH_CHICKEN1, temp);
  3817. POSTING_READ(SOUTH_CHICKEN1);
  3818. }
  3819. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3820. {
  3821. struct drm_device *dev = intel_crtc->base.dev;
  3822. switch (intel_crtc->pipe) {
  3823. case PIPE_A:
  3824. break;
  3825. case PIPE_B:
  3826. if (intel_crtc->config->fdi_lanes > 2)
  3827. cpt_set_fdi_bc_bifurcation(dev, false);
  3828. else
  3829. cpt_set_fdi_bc_bifurcation(dev, true);
  3830. break;
  3831. case PIPE_C:
  3832. cpt_set_fdi_bc_bifurcation(dev, true);
  3833. break;
  3834. default:
  3835. BUG();
  3836. }
  3837. }
  3838. /* Return which DP Port should be selected for Transcoder DP control */
  3839. static enum port
  3840. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3841. {
  3842. struct drm_device *dev = crtc->base.dev;
  3843. struct intel_encoder *encoder;
  3844. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3845. if (encoder->type == INTEL_OUTPUT_DP ||
  3846. encoder->type == INTEL_OUTPUT_EDP)
  3847. return encoder->port;
  3848. }
  3849. return -1;
  3850. }
  3851. /*
  3852. * Enable PCH resources required for PCH ports:
  3853. * - PCH PLLs
  3854. * - FDI training & RX/TX
  3855. * - update transcoder timings
  3856. * - DP transcoding bits
  3857. * - transcoder
  3858. */
  3859. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3860. {
  3861. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3862. struct drm_device *dev = crtc->base.dev;
  3863. struct drm_i915_private *dev_priv = to_i915(dev);
  3864. int pipe = crtc->pipe;
  3865. u32 temp;
  3866. assert_pch_transcoder_disabled(dev_priv, pipe);
  3867. if (IS_IVYBRIDGE(dev_priv))
  3868. ivybridge_update_fdi_bc_bifurcation(crtc);
  3869. /* Write the TU size bits before fdi link training, so that error
  3870. * detection works. */
  3871. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3872. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3873. /* For PCH output, training FDI link */
  3874. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3875. /* We need to program the right clock selection before writing the pixel
  3876. * mutliplier into the DPLL. */
  3877. if (HAS_PCH_CPT(dev_priv)) {
  3878. u32 sel;
  3879. temp = I915_READ(PCH_DPLL_SEL);
  3880. temp |= TRANS_DPLL_ENABLE(pipe);
  3881. sel = TRANS_DPLLB_SEL(pipe);
  3882. if (crtc_state->shared_dpll ==
  3883. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3884. temp |= sel;
  3885. else
  3886. temp &= ~sel;
  3887. I915_WRITE(PCH_DPLL_SEL, temp);
  3888. }
  3889. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3890. * transcoder, and we actually should do this to not upset any PCH
  3891. * transcoder that already use the clock when we share it.
  3892. *
  3893. * Note that enable_shared_dpll tries to do the right thing, but
  3894. * get_shared_dpll unconditionally resets the pll - we need that to have
  3895. * the right LVDS enable sequence. */
  3896. intel_enable_shared_dpll(crtc);
  3897. /* set transcoder timing, panel must allow it */
  3898. assert_panel_unlocked(dev_priv, pipe);
  3899. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3900. intel_fdi_normal_train(crtc);
  3901. /* For PCH DP, enable TRANS_DP_CTL */
  3902. if (HAS_PCH_CPT(dev_priv) &&
  3903. intel_crtc_has_dp_encoder(crtc_state)) {
  3904. const struct drm_display_mode *adjusted_mode =
  3905. &crtc_state->base.adjusted_mode;
  3906. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3907. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3908. temp = I915_READ(reg);
  3909. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3910. TRANS_DP_SYNC_MASK |
  3911. TRANS_DP_BPC_MASK);
  3912. temp |= TRANS_DP_OUTPUT_ENABLE;
  3913. temp |= bpc << 9; /* same format but at 11:9 */
  3914. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3915. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3916. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3917. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3918. switch (intel_trans_dp_port_sel(crtc)) {
  3919. case PORT_B:
  3920. temp |= TRANS_DP_PORT_SEL_B;
  3921. break;
  3922. case PORT_C:
  3923. temp |= TRANS_DP_PORT_SEL_C;
  3924. break;
  3925. case PORT_D:
  3926. temp |= TRANS_DP_PORT_SEL_D;
  3927. break;
  3928. default:
  3929. BUG();
  3930. }
  3931. I915_WRITE(reg, temp);
  3932. }
  3933. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3934. }
  3935. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3936. {
  3937. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3938. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3939. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3940. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3941. lpt_program_iclkip(crtc);
  3942. /* Set transcoder timing. */
  3943. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3944. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3945. }
  3946. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3947. {
  3948. struct drm_i915_private *dev_priv = to_i915(dev);
  3949. i915_reg_t dslreg = PIPEDSL(pipe);
  3950. u32 temp;
  3951. temp = I915_READ(dslreg);
  3952. udelay(500);
  3953. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3954. if (wait_for(I915_READ(dslreg) != temp, 5))
  3955. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3956. }
  3957. }
  3958. static int
  3959. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3960. unsigned int scaler_user, int *scaler_id,
  3961. int src_w, int src_h, int dst_w, int dst_h)
  3962. {
  3963. struct intel_crtc_scaler_state *scaler_state =
  3964. &crtc_state->scaler_state;
  3965. struct intel_crtc *intel_crtc =
  3966. to_intel_crtc(crtc_state->base.crtc);
  3967. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3968. const struct drm_display_mode *adjusted_mode =
  3969. &crtc_state->base.adjusted_mode;
  3970. int need_scaling;
  3971. /*
  3972. * Src coordinates are already rotated by 270 degrees for
  3973. * the 90/270 degree plane rotation cases (to match the
  3974. * GTT mapping), hence no need to account for rotation here.
  3975. */
  3976. need_scaling = src_w != dst_w || src_h != dst_h;
  3977. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  3978. need_scaling = true;
  3979. /*
  3980. * Scaling/fitting not supported in IF-ID mode in GEN9+
  3981. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  3982. * Once NV12 is enabled, handle it here while allocating scaler
  3983. * for NV12.
  3984. */
  3985. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  3986. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3987. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  3988. return -EINVAL;
  3989. }
  3990. /*
  3991. * if plane is being disabled or scaler is no more required or force detach
  3992. * - free scaler binded to this plane/crtc
  3993. * - in order to do this, update crtc->scaler_usage
  3994. *
  3995. * Here scaler state in crtc_state is set free so that
  3996. * scaler can be assigned to other user. Actual register
  3997. * update to free the scaler is done in plane/panel-fit programming.
  3998. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3999. */
  4000. if (force_detach || !need_scaling) {
  4001. if (*scaler_id >= 0) {
  4002. scaler_state->scaler_users &= ~(1 << scaler_user);
  4003. scaler_state->scalers[*scaler_id].in_use = 0;
  4004. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4005. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4006. intel_crtc->pipe, scaler_user, *scaler_id,
  4007. scaler_state->scaler_users);
  4008. *scaler_id = -1;
  4009. }
  4010. return 0;
  4011. }
  4012. /* range checks */
  4013. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4014. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4015. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4016. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4017. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4018. "size is out of scaler range\n",
  4019. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4020. return -EINVAL;
  4021. }
  4022. /* mark this plane as a scaler user in crtc_state */
  4023. scaler_state->scaler_users |= (1 << scaler_user);
  4024. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4025. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4026. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4027. scaler_state->scaler_users);
  4028. return 0;
  4029. }
  4030. /**
  4031. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4032. *
  4033. * @state: crtc's scaler state
  4034. *
  4035. * Return
  4036. * 0 - scaler_usage updated successfully
  4037. * error - requested scaling cannot be supported or other error condition
  4038. */
  4039. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4040. {
  4041. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4042. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4043. &state->scaler_state.scaler_id,
  4044. state->pipe_src_w, state->pipe_src_h,
  4045. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4046. }
  4047. /**
  4048. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4049. *
  4050. * @state: crtc's scaler state
  4051. * @plane_state: atomic plane state to update
  4052. *
  4053. * Return
  4054. * 0 - scaler_usage updated successfully
  4055. * error - requested scaling cannot be supported or other error condition
  4056. */
  4057. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4058. struct intel_plane_state *plane_state)
  4059. {
  4060. struct intel_plane *intel_plane =
  4061. to_intel_plane(plane_state->base.plane);
  4062. struct drm_framebuffer *fb = plane_state->base.fb;
  4063. int ret;
  4064. bool force_detach = !fb || !plane_state->base.visible;
  4065. ret = skl_update_scaler(crtc_state, force_detach,
  4066. drm_plane_index(&intel_plane->base),
  4067. &plane_state->scaler_id,
  4068. drm_rect_width(&plane_state->base.src) >> 16,
  4069. drm_rect_height(&plane_state->base.src) >> 16,
  4070. drm_rect_width(&plane_state->base.dst),
  4071. drm_rect_height(&plane_state->base.dst));
  4072. if (ret || plane_state->scaler_id < 0)
  4073. return ret;
  4074. /* check colorkey */
  4075. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4076. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4077. intel_plane->base.base.id,
  4078. intel_plane->base.name);
  4079. return -EINVAL;
  4080. }
  4081. /* Check src format */
  4082. switch (fb->format->format) {
  4083. case DRM_FORMAT_RGB565:
  4084. case DRM_FORMAT_XBGR8888:
  4085. case DRM_FORMAT_XRGB8888:
  4086. case DRM_FORMAT_ABGR8888:
  4087. case DRM_FORMAT_ARGB8888:
  4088. case DRM_FORMAT_XRGB2101010:
  4089. case DRM_FORMAT_XBGR2101010:
  4090. case DRM_FORMAT_YUYV:
  4091. case DRM_FORMAT_YVYU:
  4092. case DRM_FORMAT_UYVY:
  4093. case DRM_FORMAT_VYUY:
  4094. break;
  4095. default:
  4096. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4097. intel_plane->base.base.id, intel_plane->base.name,
  4098. fb->base.id, fb->format->format);
  4099. return -EINVAL;
  4100. }
  4101. return 0;
  4102. }
  4103. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4104. {
  4105. int i;
  4106. for (i = 0; i < crtc->num_scalers; i++)
  4107. skl_detach_scaler(crtc, i);
  4108. }
  4109. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4110. {
  4111. struct drm_device *dev = crtc->base.dev;
  4112. struct drm_i915_private *dev_priv = to_i915(dev);
  4113. int pipe = crtc->pipe;
  4114. struct intel_crtc_scaler_state *scaler_state =
  4115. &crtc->config->scaler_state;
  4116. if (crtc->config->pch_pfit.enabled) {
  4117. int id;
  4118. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4119. return;
  4120. id = scaler_state->scaler_id;
  4121. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4122. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4123. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4124. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4125. }
  4126. }
  4127. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4128. {
  4129. struct drm_device *dev = crtc->base.dev;
  4130. struct drm_i915_private *dev_priv = to_i915(dev);
  4131. int pipe = crtc->pipe;
  4132. if (crtc->config->pch_pfit.enabled) {
  4133. /* Force use of hard-coded filter coefficients
  4134. * as some pre-programmed values are broken,
  4135. * e.g. x201.
  4136. */
  4137. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4138. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4139. PF_PIPE_SEL_IVB(pipe));
  4140. else
  4141. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4142. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4143. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4144. }
  4145. }
  4146. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4147. {
  4148. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4149. struct drm_device *dev = crtc->base.dev;
  4150. struct drm_i915_private *dev_priv = to_i915(dev);
  4151. if (!crtc->config->ips_enabled)
  4152. return;
  4153. /*
  4154. * We can only enable IPS after we enable a plane and wait for a vblank
  4155. * This function is called from post_plane_update, which is run after
  4156. * a vblank wait.
  4157. */
  4158. assert_plane_enabled(dev_priv, crtc->plane);
  4159. if (IS_BROADWELL(dev_priv)) {
  4160. mutex_lock(&dev_priv->pcu_lock);
  4161. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4162. IPS_ENABLE | IPS_PCODE_CONTROL));
  4163. mutex_unlock(&dev_priv->pcu_lock);
  4164. /* Quoting Art Runyan: "its not safe to expect any particular
  4165. * value in IPS_CTL bit 31 after enabling IPS through the
  4166. * mailbox." Moreover, the mailbox may return a bogus state,
  4167. * so we need to just enable it and continue on.
  4168. */
  4169. } else {
  4170. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4171. /* The bit only becomes 1 in the next vblank, so this wait here
  4172. * is essentially intel_wait_for_vblank. If we don't have this
  4173. * and don't wait for vblanks until the end of crtc_enable, then
  4174. * the HW state readout code will complain that the expected
  4175. * IPS_CTL value is not the one we read. */
  4176. if (intel_wait_for_register(dev_priv,
  4177. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4178. 50))
  4179. DRM_ERROR("Timed out waiting for IPS enable\n");
  4180. }
  4181. }
  4182. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4183. {
  4184. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4185. struct drm_device *dev = crtc->base.dev;
  4186. struct drm_i915_private *dev_priv = to_i915(dev);
  4187. if (!crtc_state->ips_enabled)
  4188. return;
  4189. assert_plane_enabled(dev_priv, crtc->plane);
  4190. if (IS_BROADWELL(dev_priv)) {
  4191. mutex_lock(&dev_priv->pcu_lock);
  4192. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4193. mutex_unlock(&dev_priv->pcu_lock);
  4194. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4195. if (intel_wait_for_register(dev_priv,
  4196. IPS_CTL, IPS_ENABLE, 0,
  4197. 42))
  4198. DRM_ERROR("Timed out waiting for IPS disable\n");
  4199. } else {
  4200. I915_WRITE(IPS_CTL, 0);
  4201. POSTING_READ(IPS_CTL);
  4202. }
  4203. /* We need to wait for a vblank before we can disable the plane. */
  4204. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4205. }
  4206. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4207. {
  4208. if (intel_crtc->overlay) {
  4209. struct drm_device *dev = intel_crtc->base.dev;
  4210. mutex_lock(&dev->struct_mutex);
  4211. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4212. mutex_unlock(&dev->struct_mutex);
  4213. }
  4214. /* Let userspace switch the overlay on again. In most cases userspace
  4215. * has to recompute where to put it anyway.
  4216. */
  4217. }
  4218. /**
  4219. * intel_post_enable_primary - Perform operations after enabling primary plane
  4220. * @crtc: the CRTC whose primary plane was just enabled
  4221. *
  4222. * Performs potentially sleeping operations that must be done after the primary
  4223. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4224. * called due to an explicit primary plane update, or due to an implicit
  4225. * re-enable that is caused when a sprite plane is updated to no longer
  4226. * completely hide the primary plane.
  4227. */
  4228. static void
  4229. intel_post_enable_primary(struct drm_crtc *crtc,
  4230. const struct intel_crtc_state *new_crtc_state)
  4231. {
  4232. struct drm_device *dev = crtc->dev;
  4233. struct drm_i915_private *dev_priv = to_i915(dev);
  4234. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4235. int pipe = intel_crtc->pipe;
  4236. /*
  4237. * FIXME IPS should be fine as long as one plane is
  4238. * enabled, but in practice it seems to have problems
  4239. * when going from primary only to sprite only and vice
  4240. * versa.
  4241. */
  4242. hsw_enable_ips(new_crtc_state);
  4243. /*
  4244. * Gen2 reports pipe underruns whenever all planes are disabled.
  4245. * So don't enable underrun reporting before at least some planes
  4246. * are enabled.
  4247. * FIXME: Need to fix the logic to work when we turn off all planes
  4248. * but leave the pipe running.
  4249. */
  4250. if (IS_GEN2(dev_priv))
  4251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4252. /* Underruns don't always raise interrupts, so check manually. */
  4253. intel_check_cpu_fifo_underruns(dev_priv);
  4254. intel_check_pch_fifo_underruns(dev_priv);
  4255. }
  4256. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4257. static void
  4258. intel_pre_disable_primary(struct drm_crtc *crtc,
  4259. const struct intel_crtc_state *old_crtc_state)
  4260. {
  4261. struct drm_device *dev = crtc->dev;
  4262. struct drm_i915_private *dev_priv = to_i915(dev);
  4263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4264. int pipe = intel_crtc->pipe;
  4265. /*
  4266. * Gen2 reports pipe underruns whenever all planes are disabled.
  4267. * So diasble underrun reporting before all the planes get disabled.
  4268. * FIXME: Need to fix the logic to work when we turn off all planes
  4269. * but leave the pipe running.
  4270. */
  4271. if (IS_GEN2(dev_priv))
  4272. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4273. /*
  4274. * FIXME IPS should be fine as long as one plane is
  4275. * enabled, but in practice it seems to have problems
  4276. * when going from primary only to sprite only and vice
  4277. * versa.
  4278. */
  4279. hsw_disable_ips(old_crtc_state);
  4280. }
  4281. /* FIXME get rid of this and use pre_plane_update */
  4282. static void
  4283. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4284. {
  4285. struct drm_device *dev = crtc->dev;
  4286. struct drm_i915_private *dev_priv = to_i915(dev);
  4287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4288. int pipe = intel_crtc->pipe;
  4289. intel_pre_disable_primary(crtc, to_intel_crtc_state(crtc->state));
  4290. /*
  4291. * Vblank time updates from the shadow to live plane control register
  4292. * are blocked if the memory self-refresh mode is active at that
  4293. * moment. So to make sure the plane gets truly disabled, disable
  4294. * first the self-refresh mode. The self-refresh enable bit in turn
  4295. * will be checked/applied by the HW only at the next frame start
  4296. * event which is after the vblank start event, so we need to have a
  4297. * wait-for-vblank between disabling the plane and the pipe.
  4298. */
  4299. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4300. intel_set_memory_cxsr(dev_priv, false))
  4301. intel_wait_for_vblank(dev_priv, pipe);
  4302. }
  4303. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4304. {
  4305. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4306. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4307. struct intel_crtc_state *pipe_config =
  4308. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4309. crtc);
  4310. struct drm_plane *primary = crtc->base.primary;
  4311. struct drm_plane_state *old_pri_state =
  4312. drm_atomic_get_existing_plane_state(old_state, primary);
  4313. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4314. if (pipe_config->update_wm_post && pipe_config->base.active)
  4315. intel_update_watermarks(crtc);
  4316. if (old_pri_state) {
  4317. struct intel_plane_state *primary_state =
  4318. intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
  4319. to_intel_plane(primary));
  4320. struct intel_plane_state *old_primary_state =
  4321. to_intel_plane_state(old_pri_state);
  4322. intel_fbc_post_update(crtc);
  4323. if (primary_state->base.visible &&
  4324. (needs_modeset(&pipe_config->base) ||
  4325. !old_primary_state->base.visible))
  4326. intel_post_enable_primary(&crtc->base, pipe_config);
  4327. }
  4328. }
  4329. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4330. struct intel_crtc_state *pipe_config)
  4331. {
  4332. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4333. struct drm_device *dev = crtc->base.dev;
  4334. struct drm_i915_private *dev_priv = to_i915(dev);
  4335. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4336. struct drm_plane *primary = crtc->base.primary;
  4337. struct drm_plane_state *old_pri_state =
  4338. drm_atomic_get_existing_plane_state(old_state, primary);
  4339. bool modeset = needs_modeset(&pipe_config->base);
  4340. struct intel_atomic_state *old_intel_state =
  4341. to_intel_atomic_state(old_state);
  4342. if (old_pri_state) {
  4343. struct intel_plane_state *primary_state =
  4344. intel_atomic_get_new_plane_state(old_intel_state,
  4345. to_intel_plane(primary));
  4346. struct intel_plane_state *old_primary_state =
  4347. to_intel_plane_state(old_pri_state);
  4348. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4349. if (old_primary_state->base.visible &&
  4350. (modeset || !primary_state->base.visible))
  4351. intel_pre_disable_primary(&crtc->base, old_crtc_state);
  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(intel_crtc);
  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 haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4599. struct drm_atomic_state *old_state)
  4600. {
  4601. struct drm_crtc *crtc = pipe_config->base.crtc;
  4602. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4604. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4605. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4606. struct intel_atomic_state *old_intel_state =
  4607. to_intel_atomic_state(old_state);
  4608. bool psl_clkgate_wa;
  4609. if (WARN_ON(intel_crtc->active))
  4610. return;
  4611. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4612. if (intel_crtc->config->shared_dpll)
  4613. intel_enable_shared_dpll(intel_crtc);
  4614. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4615. intel_dp_set_m_n(intel_crtc, M1_N1);
  4616. if (!transcoder_is_dsi(cpu_transcoder))
  4617. intel_set_pipe_timings(intel_crtc);
  4618. intel_set_pipe_src_size(intel_crtc);
  4619. if (cpu_transcoder != TRANSCODER_EDP &&
  4620. !transcoder_is_dsi(cpu_transcoder)) {
  4621. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4622. intel_crtc->config->pixel_multiplier - 1);
  4623. }
  4624. if (intel_crtc->config->has_pch_encoder) {
  4625. intel_cpu_transcoder_set_m_n(intel_crtc,
  4626. &intel_crtc->config->fdi_m_n, NULL);
  4627. }
  4628. if (!transcoder_is_dsi(cpu_transcoder))
  4629. haswell_set_pipeconf(crtc);
  4630. haswell_set_pipemisc(crtc);
  4631. intel_color_set_csc(&pipe_config->base);
  4632. intel_crtc->active = true;
  4633. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4634. if (!transcoder_is_dsi(cpu_transcoder))
  4635. intel_ddi_enable_pipe_clock(pipe_config);
  4636. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4637. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4638. intel_crtc->config->pch_pfit.enabled;
  4639. if (psl_clkgate_wa)
  4640. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4641. if (INTEL_GEN(dev_priv) >= 9)
  4642. skylake_pfit_enable(intel_crtc);
  4643. else
  4644. ironlake_pfit_enable(intel_crtc);
  4645. /*
  4646. * On ILK+ LUT must be loaded before the pipe is running but with
  4647. * clocks enabled
  4648. */
  4649. intel_color_load_luts(&pipe_config->base);
  4650. intel_ddi_set_pipe_settings(pipe_config);
  4651. if (!transcoder_is_dsi(cpu_transcoder))
  4652. intel_ddi_enable_transcoder_func(pipe_config);
  4653. if (dev_priv->display.initial_watermarks != NULL)
  4654. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4655. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4656. if (!transcoder_is_dsi(cpu_transcoder))
  4657. intel_enable_pipe(intel_crtc);
  4658. if (intel_crtc->config->has_pch_encoder)
  4659. lpt_pch_enable(pipe_config);
  4660. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4661. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4662. assert_vblank_disabled(crtc);
  4663. drm_crtc_vblank_on(crtc);
  4664. intel_encoders_enable(crtc, pipe_config, old_state);
  4665. if (psl_clkgate_wa) {
  4666. intel_wait_for_vblank(dev_priv, pipe);
  4667. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4668. }
  4669. /* If we change the relative order between pipe/planes enabling, we need
  4670. * to change the workaround. */
  4671. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4672. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4673. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4674. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4675. }
  4676. }
  4677. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4678. {
  4679. struct drm_device *dev = crtc->base.dev;
  4680. struct drm_i915_private *dev_priv = to_i915(dev);
  4681. int pipe = crtc->pipe;
  4682. /* To avoid upsetting the power well on haswell only disable the pfit if
  4683. * it's in use. The hw state code will make sure we get this right. */
  4684. if (force || crtc->config->pch_pfit.enabled) {
  4685. I915_WRITE(PF_CTL(pipe), 0);
  4686. I915_WRITE(PF_WIN_POS(pipe), 0);
  4687. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4688. }
  4689. }
  4690. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4691. struct drm_atomic_state *old_state)
  4692. {
  4693. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4694. struct drm_device *dev = crtc->dev;
  4695. struct drm_i915_private *dev_priv = to_i915(dev);
  4696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4697. int pipe = intel_crtc->pipe;
  4698. /*
  4699. * Sometimes spurious CPU pipe underruns happen when the
  4700. * pipe is already disabled, but FDI RX/TX is still enabled.
  4701. * Happens at least with VGA+HDMI cloning. Suppress them.
  4702. */
  4703. if (intel_crtc->config->has_pch_encoder) {
  4704. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4705. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4706. }
  4707. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4708. drm_crtc_vblank_off(crtc);
  4709. assert_vblank_disabled(crtc);
  4710. intel_disable_pipe(intel_crtc);
  4711. ironlake_pfit_disable(intel_crtc, false);
  4712. if (intel_crtc->config->has_pch_encoder)
  4713. ironlake_fdi_disable(crtc);
  4714. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4715. if (intel_crtc->config->has_pch_encoder) {
  4716. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4717. if (HAS_PCH_CPT(dev_priv)) {
  4718. i915_reg_t reg;
  4719. u32 temp;
  4720. /* disable TRANS_DP_CTL */
  4721. reg = TRANS_DP_CTL(pipe);
  4722. temp = I915_READ(reg);
  4723. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4724. TRANS_DP_PORT_SEL_MASK);
  4725. temp |= TRANS_DP_PORT_SEL_NONE;
  4726. I915_WRITE(reg, temp);
  4727. /* disable DPLL_SEL */
  4728. temp = I915_READ(PCH_DPLL_SEL);
  4729. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4730. I915_WRITE(PCH_DPLL_SEL, temp);
  4731. }
  4732. ironlake_fdi_pll_disable(intel_crtc);
  4733. }
  4734. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4735. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4736. }
  4737. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4738. struct drm_atomic_state *old_state)
  4739. {
  4740. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4741. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4743. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4744. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4745. drm_crtc_vblank_off(crtc);
  4746. assert_vblank_disabled(crtc);
  4747. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4748. if (!transcoder_is_dsi(cpu_transcoder))
  4749. intel_disable_pipe(intel_crtc);
  4750. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4751. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4752. if (!transcoder_is_dsi(cpu_transcoder))
  4753. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4754. if (INTEL_GEN(dev_priv) >= 9)
  4755. skylake_scaler_disable(intel_crtc);
  4756. else
  4757. ironlake_pfit_disable(intel_crtc, false);
  4758. if (!transcoder_is_dsi(cpu_transcoder))
  4759. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4760. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4761. }
  4762. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4763. {
  4764. struct drm_device *dev = crtc->base.dev;
  4765. struct drm_i915_private *dev_priv = to_i915(dev);
  4766. struct intel_crtc_state *pipe_config = crtc->config;
  4767. if (!pipe_config->gmch_pfit.control)
  4768. return;
  4769. /*
  4770. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4771. * according to register description and PRM.
  4772. */
  4773. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4774. assert_pipe_disabled(dev_priv, crtc->pipe);
  4775. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4776. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4777. /* Border color in case we don't scale up to the full screen. Black by
  4778. * default, change to something else for debugging. */
  4779. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4780. }
  4781. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4782. {
  4783. switch (port) {
  4784. case PORT_A:
  4785. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4786. case PORT_B:
  4787. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4788. case PORT_C:
  4789. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4790. case PORT_D:
  4791. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4792. case PORT_E:
  4793. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4794. default:
  4795. MISSING_CASE(port);
  4796. return POWER_DOMAIN_PORT_OTHER;
  4797. }
  4798. }
  4799. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4800. struct intel_crtc_state *crtc_state)
  4801. {
  4802. struct drm_device *dev = crtc->dev;
  4803. struct drm_i915_private *dev_priv = to_i915(dev);
  4804. struct drm_encoder *encoder;
  4805. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4806. enum pipe pipe = intel_crtc->pipe;
  4807. u64 mask;
  4808. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4809. if (!crtc_state->base.active)
  4810. return 0;
  4811. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4812. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4813. if (crtc_state->pch_pfit.enabled ||
  4814. crtc_state->pch_pfit.force_thru)
  4815. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4816. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4817. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4818. mask |= BIT_ULL(intel_encoder->power_domain);
  4819. }
  4820. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4821. mask |= BIT(POWER_DOMAIN_AUDIO);
  4822. if (crtc_state->shared_dpll)
  4823. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4824. return mask;
  4825. }
  4826. static u64
  4827. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4828. struct intel_crtc_state *crtc_state)
  4829. {
  4830. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4832. enum intel_display_power_domain domain;
  4833. u64 domains, new_domains, old_domains;
  4834. old_domains = intel_crtc->enabled_power_domains;
  4835. intel_crtc->enabled_power_domains = new_domains =
  4836. get_crtc_power_domains(crtc, crtc_state);
  4837. domains = new_domains & ~old_domains;
  4838. for_each_power_domain(domain, domains)
  4839. intel_display_power_get(dev_priv, domain);
  4840. return old_domains & ~new_domains;
  4841. }
  4842. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4843. u64 domains)
  4844. {
  4845. enum intel_display_power_domain domain;
  4846. for_each_power_domain(domain, domains)
  4847. intel_display_power_put(dev_priv, domain);
  4848. }
  4849. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4850. struct drm_atomic_state *old_state)
  4851. {
  4852. struct intel_atomic_state *old_intel_state =
  4853. to_intel_atomic_state(old_state);
  4854. struct drm_crtc *crtc = pipe_config->base.crtc;
  4855. struct drm_device *dev = crtc->dev;
  4856. struct drm_i915_private *dev_priv = to_i915(dev);
  4857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4858. int pipe = intel_crtc->pipe;
  4859. if (WARN_ON(intel_crtc->active))
  4860. return;
  4861. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4862. intel_dp_set_m_n(intel_crtc, M1_N1);
  4863. intel_set_pipe_timings(intel_crtc);
  4864. intel_set_pipe_src_size(intel_crtc);
  4865. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4866. struct drm_i915_private *dev_priv = to_i915(dev);
  4867. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4868. I915_WRITE(CHV_CANVAS(pipe), 0);
  4869. }
  4870. i9xx_set_pipeconf(intel_crtc);
  4871. intel_crtc->active = true;
  4872. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4873. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4874. if (IS_CHERRYVIEW(dev_priv)) {
  4875. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4876. chv_enable_pll(intel_crtc, intel_crtc->config);
  4877. } else {
  4878. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4879. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4880. }
  4881. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4882. i9xx_pfit_enable(intel_crtc);
  4883. intel_color_load_luts(&pipe_config->base);
  4884. dev_priv->display.initial_watermarks(old_intel_state,
  4885. pipe_config);
  4886. intel_enable_pipe(intel_crtc);
  4887. assert_vblank_disabled(crtc);
  4888. drm_crtc_vblank_on(crtc);
  4889. intel_encoders_enable(crtc, pipe_config, old_state);
  4890. }
  4891. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4892. {
  4893. struct drm_device *dev = crtc->base.dev;
  4894. struct drm_i915_private *dev_priv = to_i915(dev);
  4895. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4896. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4897. }
  4898. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4899. struct drm_atomic_state *old_state)
  4900. {
  4901. struct intel_atomic_state *old_intel_state =
  4902. to_intel_atomic_state(old_state);
  4903. struct drm_crtc *crtc = pipe_config->base.crtc;
  4904. struct drm_device *dev = crtc->dev;
  4905. struct drm_i915_private *dev_priv = to_i915(dev);
  4906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4907. enum pipe pipe = intel_crtc->pipe;
  4908. if (WARN_ON(intel_crtc->active))
  4909. return;
  4910. i9xx_set_pll_dividers(intel_crtc);
  4911. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4912. intel_dp_set_m_n(intel_crtc, M1_N1);
  4913. intel_set_pipe_timings(intel_crtc);
  4914. intel_set_pipe_src_size(intel_crtc);
  4915. i9xx_set_pipeconf(intel_crtc);
  4916. intel_crtc->active = true;
  4917. if (!IS_GEN2(dev_priv))
  4918. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4919. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4920. i9xx_enable_pll(intel_crtc, pipe_config);
  4921. i9xx_pfit_enable(intel_crtc);
  4922. intel_color_load_luts(&pipe_config->base);
  4923. if (dev_priv->display.initial_watermarks != NULL)
  4924. dev_priv->display.initial_watermarks(old_intel_state,
  4925. intel_crtc->config);
  4926. else
  4927. intel_update_watermarks(intel_crtc);
  4928. intel_enable_pipe(intel_crtc);
  4929. assert_vblank_disabled(crtc);
  4930. drm_crtc_vblank_on(crtc);
  4931. intel_encoders_enable(crtc, pipe_config, old_state);
  4932. }
  4933. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4934. {
  4935. struct drm_device *dev = crtc->base.dev;
  4936. struct drm_i915_private *dev_priv = to_i915(dev);
  4937. if (!crtc->config->gmch_pfit.control)
  4938. return;
  4939. assert_pipe_disabled(dev_priv, crtc->pipe);
  4940. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4941. I915_READ(PFIT_CONTROL));
  4942. I915_WRITE(PFIT_CONTROL, 0);
  4943. }
  4944. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4945. struct drm_atomic_state *old_state)
  4946. {
  4947. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4948. struct drm_device *dev = crtc->dev;
  4949. struct drm_i915_private *dev_priv = to_i915(dev);
  4950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4951. int pipe = intel_crtc->pipe;
  4952. /*
  4953. * On gen2 planes are double buffered but the pipe isn't, so we must
  4954. * wait for planes to fully turn off before disabling the pipe.
  4955. */
  4956. if (IS_GEN2(dev_priv))
  4957. intel_wait_for_vblank(dev_priv, pipe);
  4958. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4959. drm_crtc_vblank_off(crtc);
  4960. assert_vblank_disabled(crtc);
  4961. intel_disable_pipe(intel_crtc);
  4962. i9xx_pfit_disable(intel_crtc);
  4963. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4964. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4965. if (IS_CHERRYVIEW(dev_priv))
  4966. chv_disable_pll(dev_priv, pipe);
  4967. else if (IS_VALLEYVIEW(dev_priv))
  4968. vlv_disable_pll(dev_priv, pipe);
  4969. else
  4970. i9xx_disable_pll(intel_crtc);
  4971. }
  4972. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4973. if (!IS_GEN2(dev_priv))
  4974. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4975. if (!dev_priv->display.initial_watermarks)
  4976. intel_update_watermarks(intel_crtc);
  4977. /* clock the pipe down to 640x480@60 to potentially save power */
  4978. if (IS_I830(dev_priv))
  4979. i830_enable_pipe(dev_priv, pipe);
  4980. }
  4981. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  4982. struct drm_modeset_acquire_ctx *ctx)
  4983. {
  4984. struct intel_encoder *encoder;
  4985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4986. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4987. enum intel_display_power_domain domain;
  4988. u64 domains;
  4989. struct drm_atomic_state *state;
  4990. struct intel_crtc_state *crtc_state;
  4991. int ret;
  4992. if (!intel_crtc->active)
  4993. return;
  4994. if (crtc->primary->state->visible) {
  4995. intel_pre_disable_primary_noatomic(crtc);
  4996. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4997. crtc->primary->state->visible = false;
  4998. }
  4999. state = drm_atomic_state_alloc(crtc->dev);
  5000. if (!state) {
  5001. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5002. crtc->base.id, crtc->name);
  5003. return;
  5004. }
  5005. state->acquire_ctx = ctx;
  5006. /* Everything's already locked, -EDEADLK can't happen. */
  5007. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5008. ret = drm_atomic_add_affected_connectors(state, crtc);
  5009. WARN_ON(IS_ERR(crtc_state) || ret);
  5010. dev_priv->display.crtc_disable(crtc_state, state);
  5011. drm_atomic_state_put(state);
  5012. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5013. crtc->base.id, crtc->name);
  5014. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5015. crtc->state->active = false;
  5016. intel_crtc->active = false;
  5017. crtc->enabled = false;
  5018. crtc->state->connector_mask = 0;
  5019. crtc->state->encoder_mask = 0;
  5020. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5021. encoder->base.crtc = NULL;
  5022. intel_fbc_disable(intel_crtc);
  5023. intel_update_watermarks(intel_crtc);
  5024. intel_disable_shared_dpll(intel_crtc);
  5025. domains = intel_crtc->enabled_power_domains;
  5026. for_each_power_domain(domain, domains)
  5027. intel_display_power_put(dev_priv, domain);
  5028. intel_crtc->enabled_power_domains = 0;
  5029. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5030. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5031. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5032. }
  5033. /*
  5034. * turn all crtc's off, but do not adjust state
  5035. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5036. */
  5037. int intel_display_suspend(struct drm_device *dev)
  5038. {
  5039. struct drm_i915_private *dev_priv = to_i915(dev);
  5040. struct drm_atomic_state *state;
  5041. int ret;
  5042. state = drm_atomic_helper_suspend(dev);
  5043. ret = PTR_ERR_OR_ZERO(state);
  5044. if (ret)
  5045. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5046. else
  5047. dev_priv->modeset_restore_state = state;
  5048. return ret;
  5049. }
  5050. void intel_encoder_destroy(struct drm_encoder *encoder)
  5051. {
  5052. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5053. drm_encoder_cleanup(encoder);
  5054. kfree(intel_encoder);
  5055. }
  5056. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5057. * internal consistency). */
  5058. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5059. struct drm_connector_state *conn_state)
  5060. {
  5061. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5062. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5063. connector->base.base.id,
  5064. connector->base.name);
  5065. if (connector->get_hw_state(connector)) {
  5066. struct intel_encoder *encoder = connector->encoder;
  5067. I915_STATE_WARN(!crtc_state,
  5068. "connector enabled without attached crtc\n");
  5069. if (!crtc_state)
  5070. return;
  5071. I915_STATE_WARN(!crtc_state->active,
  5072. "connector is active, but attached crtc isn't\n");
  5073. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5074. return;
  5075. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5076. "atomic encoder doesn't match attached encoder\n");
  5077. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5078. "attached encoder crtc differs from connector crtc\n");
  5079. } else {
  5080. I915_STATE_WARN(crtc_state && crtc_state->active,
  5081. "attached crtc is active, but connector isn't\n");
  5082. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5083. "best encoder set without crtc!\n");
  5084. }
  5085. }
  5086. int intel_connector_init(struct intel_connector *connector)
  5087. {
  5088. struct intel_digital_connector_state *conn_state;
  5089. /*
  5090. * Allocate enough memory to hold intel_digital_connector_state,
  5091. * This might be a few bytes too many, but for connectors that don't
  5092. * need it we'll free the state and allocate a smaller one on the first
  5093. * succesful commit anyway.
  5094. */
  5095. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5096. if (!conn_state)
  5097. return -ENOMEM;
  5098. __drm_atomic_helper_connector_reset(&connector->base,
  5099. &conn_state->base);
  5100. return 0;
  5101. }
  5102. struct intel_connector *intel_connector_alloc(void)
  5103. {
  5104. struct intel_connector *connector;
  5105. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5106. if (!connector)
  5107. return NULL;
  5108. if (intel_connector_init(connector) < 0) {
  5109. kfree(connector);
  5110. return NULL;
  5111. }
  5112. return connector;
  5113. }
  5114. /*
  5115. * Free the bits allocated by intel_connector_alloc.
  5116. * This should only be used after intel_connector_alloc has returned
  5117. * successfully, and before drm_connector_init returns successfully.
  5118. * Otherwise the destroy callbacks for the connector and the state should
  5119. * take care of proper cleanup/free
  5120. */
  5121. void intel_connector_free(struct intel_connector *connector)
  5122. {
  5123. kfree(to_intel_digital_connector_state(connector->base.state));
  5124. kfree(connector);
  5125. }
  5126. /* Simple connector->get_hw_state implementation for encoders that support only
  5127. * one connector and no cloning and hence the encoder state determines the state
  5128. * of the connector. */
  5129. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5130. {
  5131. enum pipe pipe = 0;
  5132. struct intel_encoder *encoder = connector->encoder;
  5133. return encoder->get_hw_state(encoder, &pipe);
  5134. }
  5135. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5136. {
  5137. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5138. return crtc_state->fdi_lanes;
  5139. return 0;
  5140. }
  5141. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5142. struct intel_crtc_state *pipe_config)
  5143. {
  5144. struct drm_i915_private *dev_priv = to_i915(dev);
  5145. struct drm_atomic_state *state = pipe_config->base.state;
  5146. struct intel_crtc *other_crtc;
  5147. struct intel_crtc_state *other_crtc_state;
  5148. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5149. pipe_name(pipe), pipe_config->fdi_lanes);
  5150. if (pipe_config->fdi_lanes > 4) {
  5151. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5152. pipe_name(pipe), pipe_config->fdi_lanes);
  5153. return -EINVAL;
  5154. }
  5155. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5156. if (pipe_config->fdi_lanes > 2) {
  5157. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5158. pipe_config->fdi_lanes);
  5159. return -EINVAL;
  5160. } else {
  5161. return 0;
  5162. }
  5163. }
  5164. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5165. return 0;
  5166. /* Ivybridge 3 pipe is really complicated */
  5167. switch (pipe) {
  5168. case PIPE_A:
  5169. return 0;
  5170. case PIPE_B:
  5171. if (pipe_config->fdi_lanes <= 2)
  5172. return 0;
  5173. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5174. other_crtc_state =
  5175. intel_atomic_get_crtc_state(state, other_crtc);
  5176. if (IS_ERR(other_crtc_state))
  5177. return PTR_ERR(other_crtc_state);
  5178. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5179. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5180. pipe_name(pipe), pipe_config->fdi_lanes);
  5181. return -EINVAL;
  5182. }
  5183. return 0;
  5184. case PIPE_C:
  5185. if (pipe_config->fdi_lanes > 2) {
  5186. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5187. pipe_name(pipe), pipe_config->fdi_lanes);
  5188. return -EINVAL;
  5189. }
  5190. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5191. other_crtc_state =
  5192. intel_atomic_get_crtc_state(state, other_crtc);
  5193. if (IS_ERR(other_crtc_state))
  5194. return PTR_ERR(other_crtc_state);
  5195. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5196. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5197. return -EINVAL;
  5198. }
  5199. return 0;
  5200. default:
  5201. BUG();
  5202. }
  5203. }
  5204. #define RETRY 1
  5205. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5206. struct intel_crtc_state *pipe_config)
  5207. {
  5208. struct drm_device *dev = intel_crtc->base.dev;
  5209. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5210. int lane, link_bw, fdi_dotclock, ret;
  5211. bool needs_recompute = false;
  5212. retry:
  5213. /* FDI is a binary signal running at ~2.7GHz, encoding
  5214. * each output octet as 10 bits. The actual frequency
  5215. * is stored as a divider into a 100MHz clock, and the
  5216. * mode pixel clock is stored in units of 1KHz.
  5217. * Hence the bw of each lane in terms of the mode signal
  5218. * is:
  5219. */
  5220. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5221. fdi_dotclock = adjusted_mode->crtc_clock;
  5222. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5223. pipe_config->pipe_bpp);
  5224. pipe_config->fdi_lanes = lane;
  5225. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5226. link_bw, &pipe_config->fdi_m_n, false);
  5227. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5228. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5229. pipe_config->pipe_bpp -= 2*3;
  5230. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5231. pipe_config->pipe_bpp);
  5232. needs_recompute = true;
  5233. pipe_config->bw_constrained = true;
  5234. goto retry;
  5235. }
  5236. if (needs_recompute)
  5237. return RETRY;
  5238. return ret;
  5239. }
  5240. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5241. struct intel_crtc_state *pipe_config)
  5242. {
  5243. if (pipe_config->ips_force_disable)
  5244. return false;
  5245. if (pipe_config->pipe_bpp > 24)
  5246. return false;
  5247. /* HSW can handle pixel rate up to cdclk? */
  5248. if (IS_HASWELL(dev_priv))
  5249. return true;
  5250. /*
  5251. * We compare against max which means we must take
  5252. * the increased cdclk requirement into account when
  5253. * calculating the new cdclk.
  5254. *
  5255. * Should measure whether using a lower cdclk w/o IPS
  5256. */
  5257. return pipe_config->pixel_rate <=
  5258. dev_priv->max_cdclk_freq * 95 / 100;
  5259. }
  5260. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5261. struct intel_crtc_state *pipe_config)
  5262. {
  5263. struct drm_device *dev = crtc->base.dev;
  5264. struct drm_i915_private *dev_priv = to_i915(dev);
  5265. pipe_config->ips_enabled = i915_modparams.enable_ips &&
  5266. hsw_crtc_supports_ips(crtc) &&
  5267. pipe_config_supports_ips(dev_priv, pipe_config);
  5268. }
  5269. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5270. {
  5271. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5272. /* GDG double wide on either pipe, otherwise pipe A only */
  5273. return INTEL_INFO(dev_priv)->gen < 4 &&
  5274. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5275. }
  5276. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5277. {
  5278. uint32_t pixel_rate;
  5279. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5280. /*
  5281. * We only use IF-ID interlacing. If we ever use
  5282. * PF-ID we'll need to adjust the pixel_rate here.
  5283. */
  5284. if (pipe_config->pch_pfit.enabled) {
  5285. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5286. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5287. pipe_w = pipe_config->pipe_src_w;
  5288. pipe_h = pipe_config->pipe_src_h;
  5289. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5290. pfit_h = pfit_size & 0xFFFF;
  5291. if (pipe_w < pfit_w)
  5292. pipe_w = pfit_w;
  5293. if (pipe_h < pfit_h)
  5294. pipe_h = pfit_h;
  5295. if (WARN_ON(!pfit_w || !pfit_h))
  5296. return pixel_rate;
  5297. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5298. pfit_w * pfit_h);
  5299. }
  5300. return pixel_rate;
  5301. }
  5302. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5303. {
  5304. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5305. if (HAS_GMCH_DISPLAY(dev_priv))
  5306. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5307. crtc_state->pixel_rate =
  5308. crtc_state->base.adjusted_mode.crtc_clock;
  5309. else
  5310. crtc_state->pixel_rate =
  5311. ilk_pipe_pixel_rate(crtc_state);
  5312. }
  5313. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5314. struct intel_crtc_state *pipe_config)
  5315. {
  5316. struct drm_device *dev = crtc->base.dev;
  5317. struct drm_i915_private *dev_priv = to_i915(dev);
  5318. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5319. int clock_limit = dev_priv->max_dotclk_freq;
  5320. if (INTEL_GEN(dev_priv) < 4) {
  5321. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5322. /*
  5323. * Enable double wide mode when the dot clock
  5324. * is > 90% of the (display) core speed.
  5325. */
  5326. if (intel_crtc_supports_double_wide(crtc) &&
  5327. adjusted_mode->crtc_clock > clock_limit) {
  5328. clock_limit = dev_priv->max_dotclk_freq;
  5329. pipe_config->double_wide = true;
  5330. }
  5331. }
  5332. if (adjusted_mode->crtc_clock > clock_limit) {
  5333. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5334. adjusted_mode->crtc_clock, clock_limit,
  5335. yesno(pipe_config->double_wide));
  5336. return -EINVAL;
  5337. }
  5338. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5339. /*
  5340. * There is only one pipe CSC unit per pipe, and we need that
  5341. * for output conversion from RGB->YCBCR. So if CTM is already
  5342. * applied we can't support YCBCR420 output.
  5343. */
  5344. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5345. return -EINVAL;
  5346. }
  5347. /*
  5348. * Pipe horizontal size must be even in:
  5349. * - DVO ganged mode
  5350. * - LVDS dual channel mode
  5351. * - Double wide pipe
  5352. */
  5353. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5354. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5355. pipe_config->pipe_src_w &= ~1;
  5356. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5357. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5358. */
  5359. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5360. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5361. return -EINVAL;
  5362. intel_crtc_compute_pixel_rate(pipe_config);
  5363. if (HAS_IPS(dev_priv))
  5364. hsw_compute_ips_config(crtc, pipe_config);
  5365. if (pipe_config->has_pch_encoder)
  5366. return ironlake_fdi_compute_config(crtc, pipe_config);
  5367. return 0;
  5368. }
  5369. static void
  5370. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5371. {
  5372. while (*num > DATA_LINK_M_N_MASK ||
  5373. *den > DATA_LINK_M_N_MASK) {
  5374. *num >>= 1;
  5375. *den >>= 1;
  5376. }
  5377. }
  5378. static void compute_m_n(unsigned int m, unsigned int n,
  5379. uint32_t *ret_m, uint32_t *ret_n,
  5380. bool reduce_m_n)
  5381. {
  5382. /*
  5383. * Reduce M/N as much as possible without loss in precision. Several DP
  5384. * dongles in particular seem to be fussy about too large *link* M/N
  5385. * values. The passed in values are more likely to have the least
  5386. * significant bits zero than M after rounding below, so do this first.
  5387. */
  5388. if (reduce_m_n) {
  5389. while ((m & 1) == 0 && (n & 1) == 0) {
  5390. m >>= 1;
  5391. n >>= 1;
  5392. }
  5393. }
  5394. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5395. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5396. intel_reduce_m_n_ratio(ret_m, ret_n);
  5397. }
  5398. void
  5399. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5400. int pixel_clock, int link_clock,
  5401. struct intel_link_m_n *m_n,
  5402. bool reduce_m_n)
  5403. {
  5404. m_n->tu = 64;
  5405. compute_m_n(bits_per_pixel * pixel_clock,
  5406. link_clock * nlanes * 8,
  5407. &m_n->gmch_m, &m_n->gmch_n,
  5408. reduce_m_n);
  5409. compute_m_n(pixel_clock, link_clock,
  5410. &m_n->link_m, &m_n->link_n,
  5411. reduce_m_n);
  5412. }
  5413. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5414. {
  5415. if (i915_modparams.panel_use_ssc >= 0)
  5416. return i915_modparams.panel_use_ssc != 0;
  5417. return dev_priv->vbt.lvds_use_ssc
  5418. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5419. }
  5420. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5421. {
  5422. return (1 << dpll->n) << 16 | dpll->m2;
  5423. }
  5424. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5425. {
  5426. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5427. }
  5428. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5429. struct intel_crtc_state *crtc_state,
  5430. struct dpll *reduced_clock)
  5431. {
  5432. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5433. u32 fp, fp2 = 0;
  5434. if (IS_PINEVIEW(dev_priv)) {
  5435. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5436. if (reduced_clock)
  5437. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5438. } else {
  5439. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5440. if (reduced_clock)
  5441. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5442. }
  5443. crtc_state->dpll_hw_state.fp0 = fp;
  5444. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5445. reduced_clock) {
  5446. crtc_state->dpll_hw_state.fp1 = fp2;
  5447. } else {
  5448. crtc_state->dpll_hw_state.fp1 = fp;
  5449. }
  5450. }
  5451. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5452. pipe)
  5453. {
  5454. u32 reg_val;
  5455. /*
  5456. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5457. * and set it to a reasonable value instead.
  5458. */
  5459. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5460. reg_val &= 0xffffff00;
  5461. reg_val |= 0x00000030;
  5462. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5463. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5464. reg_val &= 0x00ffffff;
  5465. reg_val |= 0x8c000000;
  5466. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5467. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5468. reg_val &= 0xffffff00;
  5469. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5470. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5471. reg_val &= 0x00ffffff;
  5472. reg_val |= 0xb0000000;
  5473. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5474. }
  5475. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5476. struct intel_link_m_n *m_n)
  5477. {
  5478. struct drm_device *dev = crtc->base.dev;
  5479. struct drm_i915_private *dev_priv = to_i915(dev);
  5480. int pipe = crtc->pipe;
  5481. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5482. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5483. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5484. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5485. }
  5486. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5487. struct intel_link_m_n *m_n,
  5488. struct intel_link_m_n *m2_n2)
  5489. {
  5490. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5491. int pipe = crtc->pipe;
  5492. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5493. if (INTEL_GEN(dev_priv) >= 5) {
  5494. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5495. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5496. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5497. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5498. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5499. * for gen < 8) and if DRRS is supported (to make sure the
  5500. * registers are not unnecessarily accessed).
  5501. */
  5502. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5503. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5504. I915_WRITE(PIPE_DATA_M2(transcoder),
  5505. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5506. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5507. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5508. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5509. }
  5510. } else {
  5511. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5512. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5513. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5514. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5515. }
  5516. }
  5517. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5518. {
  5519. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5520. if (m_n == M1_N1) {
  5521. dp_m_n = &crtc->config->dp_m_n;
  5522. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5523. } else if (m_n == M2_N2) {
  5524. /*
  5525. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5526. * needs to be programmed into M1_N1.
  5527. */
  5528. dp_m_n = &crtc->config->dp_m2_n2;
  5529. } else {
  5530. DRM_ERROR("Unsupported divider value\n");
  5531. return;
  5532. }
  5533. if (crtc->config->has_pch_encoder)
  5534. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5535. else
  5536. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5537. }
  5538. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5539. struct intel_crtc_state *pipe_config)
  5540. {
  5541. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5542. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5543. if (crtc->pipe != PIPE_A)
  5544. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5545. /* DPLL not used with DSI, but still need the rest set up */
  5546. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5547. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5548. DPLL_EXT_BUFFER_ENABLE_VLV;
  5549. pipe_config->dpll_hw_state.dpll_md =
  5550. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5551. }
  5552. static void chv_compute_dpll(struct intel_crtc *crtc,
  5553. struct intel_crtc_state *pipe_config)
  5554. {
  5555. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5556. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5557. if (crtc->pipe != PIPE_A)
  5558. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5559. /* DPLL not used with DSI, but still need the rest set up */
  5560. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5561. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5562. pipe_config->dpll_hw_state.dpll_md =
  5563. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5564. }
  5565. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5566. const struct intel_crtc_state *pipe_config)
  5567. {
  5568. struct drm_device *dev = crtc->base.dev;
  5569. struct drm_i915_private *dev_priv = to_i915(dev);
  5570. enum pipe pipe = crtc->pipe;
  5571. u32 mdiv;
  5572. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5573. u32 coreclk, reg_val;
  5574. /* Enable Refclk */
  5575. I915_WRITE(DPLL(pipe),
  5576. pipe_config->dpll_hw_state.dpll &
  5577. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5578. /* No need to actually set up the DPLL with DSI */
  5579. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5580. return;
  5581. mutex_lock(&dev_priv->sb_lock);
  5582. bestn = pipe_config->dpll.n;
  5583. bestm1 = pipe_config->dpll.m1;
  5584. bestm2 = pipe_config->dpll.m2;
  5585. bestp1 = pipe_config->dpll.p1;
  5586. bestp2 = pipe_config->dpll.p2;
  5587. /* See eDP HDMI DPIO driver vbios notes doc */
  5588. /* PLL B needs special handling */
  5589. if (pipe == PIPE_B)
  5590. vlv_pllb_recal_opamp(dev_priv, pipe);
  5591. /* Set up Tx target for periodic Rcomp update */
  5592. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5593. /* Disable target IRef on PLL */
  5594. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5595. reg_val &= 0x00ffffff;
  5596. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5597. /* Disable fast lock */
  5598. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5599. /* Set idtafcrecal before PLL is enabled */
  5600. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5601. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5602. mdiv |= ((bestn << DPIO_N_SHIFT));
  5603. mdiv |= (1 << DPIO_K_SHIFT);
  5604. /*
  5605. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5606. * but we don't support that).
  5607. * Note: don't use the DAC post divider as it seems unstable.
  5608. */
  5609. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5610. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5611. mdiv |= DPIO_ENABLE_CALIBRATION;
  5612. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5613. /* Set HBR and RBR LPF coefficients */
  5614. if (pipe_config->port_clock == 162000 ||
  5615. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5616. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5617. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5618. 0x009f0003);
  5619. else
  5620. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5621. 0x00d0000f);
  5622. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5623. /* Use SSC source */
  5624. if (pipe == PIPE_A)
  5625. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5626. 0x0df40000);
  5627. else
  5628. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5629. 0x0df70000);
  5630. } else { /* HDMI or VGA */
  5631. /* Use bend source */
  5632. if (pipe == PIPE_A)
  5633. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5634. 0x0df70000);
  5635. else
  5636. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5637. 0x0df40000);
  5638. }
  5639. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5640. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5641. if (intel_crtc_has_dp_encoder(crtc->config))
  5642. coreclk |= 0x01000000;
  5643. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5644. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5645. mutex_unlock(&dev_priv->sb_lock);
  5646. }
  5647. static void chv_prepare_pll(struct intel_crtc *crtc,
  5648. const struct intel_crtc_state *pipe_config)
  5649. {
  5650. struct drm_device *dev = crtc->base.dev;
  5651. struct drm_i915_private *dev_priv = to_i915(dev);
  5652. enum pipe pipe = crtc->pipe;
  5653. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5654. u32 loopfilter, tribuf_calcntr;
  5655. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5656. u32 dpio_val;
  5657. int vco;
  5658. /* Enable Refclk and SSC */
  5659. I915_WRITE(DPLL(pipe),
  5660. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5661. /* No need to actually set up the DPLL with DSI */
  5662. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5663. return;
  5664. bestn = pipe_config->dpll.n;
  5665. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5666. bestm1 = pipe_config->dpll.m1;
  5667. bestm2 = pipe_config->dpll.m2 >> 22;
  5668. bestp1 = pipe_config->dpll.p1;
  5669. bestp2 = pipe_config->dpll.p2;
  5670. vco = pipe_config->dpll.vco;
  5671. dpio_val = 0;
  5672. loopfilter = 0;
  5673. mutex_lock(&dev_priv->sb_lock);
  5674. /* p1 and p2 divider */
  5675. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5676. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5677. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5678. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5679. 1 << DPIO_CHV_K_DIV_SHIFT);
  5680. /* Feedback post-divider - m2 */
  5681. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5682. /* Feedback refclk divider - n and m1 */
  5683. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5684. DPIO_CHV_M1_DIV_BY_2 |
  5685. 1 << DPIO_CHV_N_DIV_SHIFT);
  5686. /* M2 fraction division */
  5687. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5688. /* M2 fraction division enable */
  5689. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5690. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5691. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5692. if (bestm2_frac)
  5693. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5694. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5695. /* Program digital lock detect threshold */
  5696. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5697. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5698. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5699. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5700. if (!bestm2_frac)
  5701. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5702. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5703. /* Loop filter */
  5704. if (vco == 5400000) {
  5705. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5706. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5707. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5708. tribuf_calcntr = 0x9;
  5709. } else if (vco <= 6200000) {
  5710. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5711. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5712. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5713. tribuf_calcntr = 0x9;
  5714. } else if (vco <= 6480000) {
  5715. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5716. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5717. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5718. tribuf_calcntr = 0x8;
  5719. } else {
  5720. /* Not supported. Apply the same limits as in the max case */
  5721. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5722. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5723. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5724. tribuf_calcntr = 0;
  5725. }
  5726. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5727. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5728. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5729. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5730. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5731. /* AFC Recal */
  5732. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5733. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5734. DPIO_AFC_RECAL);
  5735. mutex_unlock(&dev_priv->sb_lock);
  5736. }
  5737. /**
  5738. * vlv_force_pll_on - forcibly enable just the PLL
  5739. * @dev_priv: i915 private structure
  5740. * @pipe: pipe PLL to enable
  5741. * @dpll: PLL configuration
  5742. *
  5743. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5744. * in cases where we need the PLL enabled even when @pipe is not going to
  5745. * be enabled.
  5746. */
  5747. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5748. const struct dpll *dpll)
  5749. {
  5750. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5751. struct intel_crtc_state *pipe_config;
  5752. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5753. if (!pipe_config)
  5754. return -ENOMEM;
  5755. pipe_config->base.crtc = &crtc->base;
  5756. pipe_config->pixel_multiplier = 1;
  5757. pipe_config->dpll = *dpll;
  5758. if (IS_CHERRYVIEW(dev_priv)) {
  5759. chv_compute_dpll(crtc, pipe_config);
  5760. chv_prepare_pll(crtc, pipe_config);
  5761. chv_enable_pll(crtc, pipe_config);
  5762. } else {
  5763. vlv_compute_dpll(crtc, pipe_config);
  5764. vlv_prepare_pll(crtc, pipe_config);
  5765. vlv_enable_pll(crtc, pipe_config);
  5766. }
  5767. kfree(pipe_config);
  5768. return 0;
  5769. }
  5770. /**
  5771. * vlv_force_pll_off - forcibly disable just the PLL
  5772. * @dev_priv: i915 private structure
  5773. * @pipe: pipe PLL to disable
  5774. *
  5775. * Disable the PLL for @pipe. To be used in cases where we need
  5776. * the PLL enabled even when @pipe is not going to be enabled.
  5777. */
  5778. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5779. {
  5780. if (IS_CHERRYVIEW(dev_priv))
  5781. chv_disable_pll(dev_priv, pipe);
  5782. else
  5783. vlv_disable_pll(dev_priv, pipe);
  5784. }
  5785. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5786. struct intel_crtc_state *crtc_state,
  5787. struct dpll *reduced_clock)
  5788. {
  5789. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5790. u32 dpll;
  5791. struct dpll *clock = &crtc_state->dpll;
  5792. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5793. dpll = DPLL_VGA_MODE_DIS;
  5794. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5795. dpll |= DPLLB_MODE_LVDS;
  5796. else
  5797. dpll |= DPLLB_MODE_DAC_SERIAL;
  5798. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5799. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5800. dpll |= (crtc_state->pixel_multiplier - 1)
  5801. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5802. }
  5803. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5804. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5805. dpll |= DPLL_SDVO_HIGH_SPEED;
  5806. if (intel_crtc_has_dp_encoder(crtc_state))
  5807. dpll |= DPLL_SDVO_HIGH_SPEED;
  5808. /* compute bitmask from p1 value */
  5809. if (IS_PINEVIEW(dev_priv))
  5810. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5811. else {
  5812. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5813. if (IS_G4X(dev_priv) && reduced_clock)
  5814. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5815. }
  5816. switch (clock->p2) {
  5817. case 5:
  5818. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5819. break;
  5820. case 7:
  5821. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5822. break;
  5823. case 10:
  5824. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5825. break;
  5826. case 14:
  5827. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5828. break;
  5829. }
  5830. if (INTEL_GEN(dev_priv) >= 4)
  5831. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5832. if (crtc_state->sdvo_tv_clock)
  5833. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5834. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5835. intel_panel_use_ssc(dev_priv))
  5836. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5837. else
  5838. dpll |= PLL_REF_INPUT_DREFCLK;
  5839. dpll |= DPLL_VCO_ENABLE;
  5840. crtc_state->dpll_hw_state.dpll = dpll;
  5841. if (INTEL_GEN(dev_priv) >= 4) {
  5842. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5843. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5844. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5845. }
  5846. }
  5847. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5848. struct intel_crtc_state *crtc_state,
  5849. struct dpll *reduced_clock)
  5850. {
  5851. struct drm_device *dev = crtc->base.dev;
  5852. struct drm_i915_private *dev_priv = to_i915(dev);
  5853. u32 dpll;
  5854. struct dpll *clock = &crtc_state->dpll;
  5855. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5856. dpll = DPLL_VGA_MODE_DIS;
  5857. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5858. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5859. } else {
  5860. if (clock->p1 == 2)
  5861. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5862. else
  5863. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5864. if (clock->p2 == 4)
  5865. dpll |= PLL_P2_DIVIDE_BY_4;
  5866. }
  5867. if (!IS_I830(dev_priv) &&
  5868. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5869. dpll |= DPLL_DVO_2X_MODE;
  5870. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5871. intel_panel_use_ssc(dev_priv))
  5872. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5873. else
  5874. dpll |= PLL_REF_INPUT_DREFCLK;
  5875. dpll |= DPLL_VCO_ENABLE;
  5876. crtc_state->dpll_hw_state.dpll = dpll;
  5877. }
  5878. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5879. {
  5880. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5881. enum pipe pipe = intel_crtc->pipe;
  5882. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5883. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5884. uint32_t crtc_vtotal, crtc_vblank_end;
  5885. int vsyncshift = 0;
  5886. /* We need to be careful not to changed the adjusted mode, for otherwise
  5887. * the hw state checker will get angry at the mismatch. */
  5888. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5889. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5890. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5891. /* the chip adds 2 halflines automatically */
  5892. crtc_vtotal -= 1;
  5893. crtc_vblank_end -= 1;
  5894. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5895. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5896. else
  5897. vsyncshift = adjusted_mode->crtc_hsync_start -
  5898. adjusted_mode->crtc_htotal / 2;
  5899. if (vsyncshift < 0)
  5900. vsyncshift += adjusted_mode->crtc_htotal;
  5901. }
  5902. if (INTEL_GEN(dev_priv) > 3)
  5903. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5904. I915_WRITE(HTOTAL(cpu_transcoder),
  5905. (adjusted_mode->crtc_hdisplay - 1) |
  5906. ((adjusted_mode->crtc_htotal - 1) << 16));
  5907. I915_WRITE(HBLANK(cpu_transcoder),
  5908. (adjusted_mode->crtc_hblank_start - 1) |
  5909. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5910. I915_WRITE(HSYNC(cpu_transcoder),
  5911. (adjusted_mode->crtc_hsync_start - 1) |
  5912. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5913. I915_WRITE(VTOTAL(cpu_transcoder),
  5914. (adjusted_mode->crtc_vdisplay - 1) |
  5915. ((crtc_vtotal - 1) << 16));
  5916. I915_WRITE(VBLANK(cpu_transcoder),
  5917. (adjusted_mode->crtc_vblank_start - 1) |
  5918. ((crtc_vblank_end - 1) << 16));
  5919. I915_WRITE(VSYNC(cpu_transcoder),
  5920. (adjusted_mode->crtc_vsync_start - 1) |
  5921. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5922. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5923. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5924. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5925. * bits. */
  5926. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5927. (pipe == PIPE_B || pipe == PIPE_C))
  5928. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5929. }
  5930. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5931. {
  5932. struct drm_device *dev = intel_crtc->base.dev;
  5933. struct drm_i915_private *dev_priv = to_i915(dev);
  5934. enum pipe pipe = intel_crtc->pipe;
  5935. /* pipesrc controls the size that is scaled from, which should
  5936. * always be the user's requested size.
  5937. */
  5938. I915_WRITE(PIPESRC(pipe),
  5939. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5940. (intel_crtc->config->pipe_src_h - 1));
  5941. }
  5942. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5943. struct intel_crtc_state *pipe_config)
  5944. {
  5945. struct drm_device *dev = crtc->base.dev;
  5946. struct drm_i915_private *dev_priv = to_i915(dev);
  5947. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5948. uint32_t tmp;
  5949. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5950. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5951. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5952. tmp = I915_READ(HBLANK(cpu_transcoder));
  5953. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5954. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5955. tmp = I915_READ(HSYNC(cpu_transcoder));
  5956. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5957. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5958. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5959. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5960. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5961. tmp = I915_READ(VBLANK(cpu_transcoder));
  5962. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5963. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5964. tmp = I915_READ(VSYNC(cpu_transcoder));
  5965. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5966. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5967. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5968. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5969. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5970. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5971. }
  5972. }
  5973. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5974. struct intel_crtc_state *pipe_config)
  5975. {
  5976. struct drm_device *dev = crtc->base.dev;
  5977. struct drm_i915_private *dev_priv = to_i915(dev);
  5978. u32 tmp;
  5979. tmp = I915_READ(PIPESRC(crtc->pipe));
  5980. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5981. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5982. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5983. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5984. }
  5985. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5986. struct intel_crtc_state *pipe_config)
  5987. {
  5988. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5989. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5990. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5991. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5992. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5993. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5994. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5995. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5996. mode->flags = pipe_config->base.adjusted_mode.flags;
  5997. mode->type = DRM_MODE_TYPE_DRIVER;
  5998. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5999. mode->hsync = drm_mode_hsync(mode);
  6000. mode->vrefresh = drm_mode_vrefresh(mode);
  6001. drm_mode_set_name(mode);
  6002. }
  6003. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6004. {
  6005. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6006. uint32_t pipeconf;
  6007. pipeconf = 0;
  6008. /* we keep both pipes enabled on 830 */
  6009. if (IS_I830(dev_priv))
  6010. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6011. if (intel_crtc->config->double_wide)
  6012. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6013. /* only g4x and later have fancy bpc/dither controls */
  6014. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6015. IS_CHERRYVIEW(dev_priv)) {
  6016. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6017. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6018. pipeconf |= PIPECONF_DITHER_EN |
  6019. PIPECONF_DITHER_TYPE_SP;
  6020. switch (intel_crtc->config->pipe_bpp) {
  6021. case 18:
  6022. pipeconf |= PIPECONF_6BPC;
  6023. break;
  6024. case 24:
  6025. pipeconf |= PIPECONF_8BPC;
  6026. break;
  6027. case 30:
  6028. pipeconf |= PIPECONF_10BPC;
  6029. break;
  6030. default:
  6031. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6032. BUG();
  6033. }
  6034. }
  6035. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6036. if (INTEL_GEN(dev_priv) < 4 ||
  6037. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6038. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6039. else
  6040. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6041. } else
  6042. pipeconf |= PIPECONF_PROGRESSIVE;
  6043. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6044. intel_crtc->config->limited_color_range)
  6045. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6046. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6047. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6048. }
  6049. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6050. struct intel_crtc_state *crtc_state)
  6051. {
  6052. struct drm_device *dev = crtc->base.dev;
  6053. struct drm_i915_private *dev_priv = to_i915(dev);
  6054. const struct intel_limit *limit;
  6055. int refclk = 48000;
  6056. memset(&crtc_state->dpll_hw_state, 0,
  6057. sizeof(crtc_state->dpll_hw_state));
  6058. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6059. if (intel_panel_use_ssc(dev_priv)) {
  6060. refclk = dev_priv->vbt.lvds_ssc_freq;
  6061. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6062. }
  6063. limit = &intel_limits_i8xx_lvds;
  6064. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6065. limit = &intel_limits_i8xx_dvo;
  6066. } else {
  6067. limit = &intel_limits_i8xx_dac;
  6068. }
  6069. if (!crtc_state->clock_set &&
  6070. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6071. refclk, NULL, &crtc_state->dpll)) {
  6072. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6073. return -EINVAL;
  6074. }
  6075. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6076. return 0;
  6077. }
  6078. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6079. struct intel_crtc_state *crtc_state)
  6080. {
  6081. struct drm_device *dev = crtc->base.dev;
  6082. struct drm_i915_private *dev_priv = to_i915(dev);
  6083. const struct intel_limit *limit;
  6084. int refclk = 96000;
  6085. memset(&crtc_state->dpll_hw_state, 0,
  6086. sizeof(crtc_state->dpll_hw_state));
  6087. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6088. if (intel_panel_use_ssc(dev_priv)) {
  6089. refclk = dev_priv->vbt.lvds_ssc_freq;
  6090. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6091. }
  6092. if (intel_is_dual_link_lvds(dev))
  6093. limit = &intel_limits_g4x_dual_channel_lvds;
  6094. else
  6095. limit = &intel_limits_g4x_single_channel_lvds;
  6096. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6097. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6098. limit = &intel_limits_g4x_hdmi;
  6099. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6100. limit = &intel_limits_g4x_sdvo;
  6101. } else {
  6102. /* The option is for other outputs */
  6103. limit = &intel_limits_i9xx_sdvo;
  6104. }
  6105. if (!crtc_state->clock_set &&
  6106. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6107. refclk, NULL, &crtc_state->dpll)) {
  6108. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6109. return -EINVAL;
  6110. }
  6111. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6112. return 0;
  6113. }
  6114. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6115. struct intel_crtc_state *crtc_state)
  6116. {
  6117. struct drm_device *dev = crtc->base.dev;
  6118. struct drm_i915_private *dev_priv = to_i915(dev);
  6119. const struct intel_limit *limit;
  6120. int refclk = 96000;
  6121. memset(&crtc_state->dpll_hw_state, 0,
  6122. sizeof(crtc_state->dpll_hw_state));
  6123. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6124. if (intel_panel_use_ssc(dev_priv)) {
  6125. refclk = dev_priv->vbt.lvds_ssc_freq;
  6126. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6127. }
  6128. limit = &intel_limits_pineview_lvds;
  6129. } else {
  6130. limit = &intel_limits_pineview_sdvo;
  6131. }
  6132. if (!crtc_state->clock_set &&
  6133. !pnv_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 i9xx_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_i9xx_lvds;
  6156. } else {
  6157. limit = &intel_limits_i9xx_sdvo;
  6158. }
  6159. if (!crtc_state->clock_set &&
  6160. !i9xx_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 chv_crtc_compute_clock(struct intel_crtc *crtc,
  6169. struct intel_crtc_state *crtc_state)
  6170. {
  6171. int refclk = 100000;
  6172. const struct intel_limit *limit = &intel_limits_chv;
  6173. memset(&crtc_state->dpll_hw_state, 0,
  6174. sizeof(crtc_state->dpll_hw_state));
  6175. if (!crtc_state->clock_set &&
  6176. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6177. refclk, NULL, &crtc_state->dpll)) {
  6178. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6179. return -EINVAL;
  6180. }
  6181. chv_compute_dpll(crtc, crtc_state);
  6182. return 0;
  6183. }
  6184. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6185. struct intel_crtc_state *crtc_state)
  6186. {
  6187. int refclk = 100000;
  6188. const struct intel_limit *limit = &intel_limits_vlv;
  6189. memset(&crtc_state->dpll_hw_state, 0,
  6190. sizeof(crtc_state->dpll_hw_state));
  6191. if (!crtc_state->clock_set &&
  6192. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6193. refclk, NULL, &crtc_state->dpll)) {
  6194. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6195. return -EINVAL;
  6196. }
  6197. vlv_compute_dpll(crtc, crtc_state);
  6198. return 0;
  6199. }
  6200. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6201. struct intel_crtc_state *pipe_config)
  6202. {
  6203. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6204. uint32_t tmp;
  6205. if (INTEL_GEN(dev_priv) <= 3 &&
  6206. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6207. return;
  6208. tmp = I915_READ(PFIT_CONTROL);
  6209. if (!(tmp & PFIT_ENABLE))
  6210. return;
  6211. /* Check whether the pfit is attached to our pipe. */
  6212. if (INTEL_GEN(dev_priv) < 4) {
  6213. if (crtc->pipe != PIPE_B)
  6214. return;
  6215. } else {
  6216. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6217. return;
  6218. }
  6219. pipe_config->gmch_pfit.control = tmp;
  6220. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6221. }
  6222. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6223. struct intel_crtc_state *pipe_config)
  6224. {
  6225. struct drm_device *dev = crtc->base.dev;
  6226. struct drm_i915_private *dev_priv = to_i915(dev);
  6227. int pipe = pipe_config->cpu_transcoder;
  6228. struct dpll clock;
  6229. u32 mdiv;
  6230. int refclk = 100000;
  6231. /* In case of DSI, DPLL will not be used */
  6232. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6233. return;
  6234. mutex_lock(&dev_priv->sb_lock);
  6235. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6236. mutex_unlock(&dev_priv->sb_lock);
  6237. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6238. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6239. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6240. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6241. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6242. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6243. }
  6244. static void
  6245. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6246. struct intel_initial_plane_config *plane_config)
  6247. {
  6248. struct drm_device *dev = crtc->base.dev;
  6249. struct drm_i915_private *dev_priv = to_i915(dev);
  6250. u32 val, base, offset;
  6251. int pipe = crtc->pipe, plane = crtc->plane;
  6252. int fourcc, pixel_format;
  6253. unsigned int aligned_height;
  6254. struct drm_framebuffer *fb;
  6255. struct intel_framebuffer *intel_fb;
  6256. val = I915_READ(DSPCNTR(plane));
  6257. if (!(val & DISPLAY_PLANE_ENABLE))
  6258. return;
  6259. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6260. if (!intel_fb) {
  6261. DRM_DEBUG_KMS("failed to alloc fb\n");
  6262. return;
  6263. }
  6264. fb = &intel_fb->base;
  6265. fb->dev = dev;
  6266. if (INTEL_GEN(dev_priv) >= 4) {
  6267. if (val & DISPPLANE_TILED) {
  6268. plane_config->tiling = I915_TILING_X;
  6269. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6270. }
  6271. }
  6272. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6273. fourcc = i9xx_format_to_fourcc(pixel_format);
  6274. fb->format = drm_format_info(fourcc);
  6275. if (INTEL_GEN(dev_priv) >= 4) {
  6276. if (plane_config->tiling)
  6277. offset = I915_READ(DSPTILEOFF(plane));
  6278. else
  6279. offset = I915_READ(DSPLINOFF(plane));
  6280. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6281. } else {
  6282. base = I915_READ(DSPADDR(plane));
  6283. }
  6284. plane_config->base = base;
  6285. val = I915_READ(PIPESRC(pipe));
  6286. fb->width = ((val >> 16) & 0xfff) + 1;
  6287. fb->height = ((val >> 0) & 0xfff) + 1;
  6288. val = I915_READ(DSPSTRIDE(pipe));
  6289. fb->pitches[0] = val & 0xffffffc0;
  6290. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6291. plane_config->size = fb->pitches[0] * aligned_height;
  6292. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6293. pipe_name(pipe), plane, fb->width, fb->height,
  6294. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6295. plane_config->size);
  6296. plane_config->fb = intel_fb;
  6297. }
  6298. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6299. struct intel_crtc_state *pipe_config)
  6300. {
  6301. struct drm_device *dev = crtc->base.dev;
  6302. struct drm_i915_private *dev_priv = to_i915(dev);
  6303. int pipe = pipe_config->cpu_transcoder;
  6304. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6305. struct dpll clock;
  6306. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6307. int refclk = 100000;
  6308. /* In case of DSI, DPLL will not be used */
  6309. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6310. return;
  6311. mutex_lock(&dev_priv->sb_lock);
  6312. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6313. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6314. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6315. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6316. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6317. mutex_unlock(&dev_priv->sb_lock);
  6318. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6319. clock.m2 = (pll_dw0 & 0xff) << 22;
  6320. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6321. clock.m2 |= pll_dw2 & 0x3fffff;
  6322. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6323. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6324. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6325. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6326. }
  6327. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6328. struct intel_crtc_state *pipe_config)
  6329. {
  6330. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6331. enum intel_display_power_domain power_domain;
  6332. uint32_t tmp;
  6333. bool ret;
  6334. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6335. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6336. return false;
  6337. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6338. pipe_config->shared_dpll = NULL;
  6339. ret = false;
  6340. tmp = I915_READ(PIPECONF(crtc->pipe));
  6341. if (!(tmp & PIPECONF_ENABLE))
  6342. goto out;
  6343. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6344. IS_CHERRYVIEW(dev_priv)) {
  6345. switch (tmp & PIPECONF_BPC_MASK) {
  6346. case PIPECONF_6BPC:
  6347. pipe_config->pipe_bpp = 18;
  6348. break;
  6349. case PIPECONF_8BPC:
  6350. pipe_config->pipe_bpp = 24;
  6351. break;
  6352. case PIPECONF_10BPC:
  6353. pipe_config->pipe_bpp = 30;
  6354. break;
  6355. default:
  6356. break;
  6357. }
  6358. }
  6359. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6360. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6361. pipe_config->limited_color_range = true;
  6362. if (INTEL_GEN(dev_priv) < 4)
  6363. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6364. intel_get_pipe_timings(crtc, pipe_config);
  6365. intel_get_pipe_src_size(crtc, pipe_config);
  6366. i9xx_get_pfit_config(crtc, pipe_config);
  6367. if (INTEL_GEN(dev_priv) >= 4) {
  6368. /* No way to read it out on pipes B and C */
  6369. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6370. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6371. else
  6372. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6373. pipe_config->pixel_multiplier =
  6374. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6375. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6376. pipe_config->dpll_hw_state.dpll_md = tmp;
  6377. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6378. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6379. tmp = I915_READ(DPLL(crtc->pipe));
  6380. pipe_config->pixel_multiplier =
  6381. ((tmp & SDVO_MULTIPLIER_MASK)
  6382. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6383. } else {
  6384. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6385. * port and will be fixed up in the encoder->get_config
  6386. * function. */
  6387. pipe_config->pixel_multiplier = 1;
  6388. }
  6389. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6390. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6391. /*
  6392. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6393. * on 830. Filter it out here so that we don't
  6394. * report errors due to that.
  6395. */
  6396. if (IS_I830(dev_priv))
  6397. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6398. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6399. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6400. } else {
  6401. /* Mask out read-only status bits. */
  6402. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6403. DPLL_PORTC_READY_MASK |
  6404. DPLL_PORTB_READY_MASK);
  6405. }
  6406. if (IS_CHERRYVIEW(dev_priv))
  6407. chv_crtc_clock_get(crtc, pipe_config);
  6408. else if (IS_VALLEYVIEW(dev_priv))
  6409. vlv_crtc_clock_get(crtc, pipe_config);
  6410. else
  6411. i9xx_crtc_clock_get(crtc, pipe_config);
  6412. /*
  6413. * Normally the dotclock is filled in by the encoder .get_config()
  6414. * but in case the pipe is enabled w/o any ports we need a sane
  6415. * default.
  6416. */
  6417. pipe_config->base.adjusted_mode.crtc_clock =
  6418. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6419. ret = true;
  6420. out:
  6421. intel_display_power_put(dev_priv, power_domain);
  6422. return ret;
  6423. }
  6424. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6425. {
  6426. struct intel_encoder *encoder;
  6427. int i;
  6428. u32 val, final;
  6429. bool has_lvds = false;
  6430. bool has_cpu_edp = false;
  6431. bool has_panel = false;
  6432. bool has_ck505 = false;
  6433. bool can_ssc = false;
  6434. bool using_ssc_source = false;
  6435. /* We need to take the global config into account */
  6436. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6437. switch (encoder->type) {
  6438. case INTEL_OUTPUT_LVDS:
  6439. has_panel = true;
  6440. has_lvds = true;
  6441. break;
  6442. case INTEL_OUTPUT_EDP:
  6443. has_panel = true;
  6444. if (encoder->port == PORT_A)
  6445. has_cpu_edp = true;
  6446. break;
  6447. default:
  6448. break;
  6449. }
  6450. }
  6451. if (HAS_PCH_IBX(dev_priv)) {
  6452. has_ck505 = dev_priv->vbt.display_clock_mode;
  6453. can_ssc = has_ck505;
  6454. } else {
  6455. has_ck505 = false;
  6456. can_ssc = true;
  6457. }
  6458. /* Check if any DPLLs are using the SSC source */
  6459. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6460. u32 temp = I915_READ(PCH_DPLL(i));
  6461. if (!(temp & DPLL_VCO_ENABLE))
  6462. continue;
  6463. if ((temp & PLL_REF_INPUT_MASK) ==
  6464. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6465. using_ssc_source = true;
  6466. break;
  6467. }
  6468. }
  6469. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6470. has_panel, has_lvds, has_ck505, using_ssc_source);
  6471. /* Ironlake: try to setup display ref clock before DPLL
  6472. * enabling. This is only under driver's control after
  6473. * PCH B stepping, previous chipset stepping should be
  6474. * ignoring this setting.
  6475. */
  6476. val = I915_READ(PCH_DREF_CONTROL);
  6477. /* As we must carefully and slowly disable/enable each source in turn,
  6478. * compute the final state we want first and check if we need to
  6479. * make any changes at all.
  6480. */
  6481. final = val;
  6482. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6483. if (has_ck505)
  6484. final |= DREF_NONSPREAD_CK505_ENABLE;
  6485. else
  6486. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6487. final &= ~DREF_SSC_SOURCE_MASK;
  6488. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6489. final &= ~DREF_SSC1_ENABLE;
  6490. if (has_panel) {
  6491. final |= DREF_SSC_SOURCE_ENABLE;
  6492. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6493. final |= DREF_SSC1_ENABLE;
  6494. if (has_cpu_edp) {
  6495. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6496. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6497. else
  6498. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6499. } else
  6500. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6501. } else if (using_ssc_source) {
  6502. final |= DREF_SSC_SOURCE_ENABLE;
  6503. final |= DREF_SSC1_ENABLE;
  6504. }
  6505. if (final == val)
  6506. return;
  6507. /* Always enable nonspread source */
  6508. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6509. if (has_ck505)
  6510. val |= DREF_NONSPREAD_CK505_ENABLE;
  6511. else
  6512. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6513. if (has_panel) {
  6514. val &= ~DREF_SSC_SOURCE_MASK;
  6515. val |= DREF_SSC_SOURCE_ENABLE;
  6516. /* SSC must be turned on before enabling the CPU output */
  6517. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6518. DRM_DEBUG_KMS("Using SSC on panel\n");
  6519. val |= DREF_SSC1_ENABLE;
  6520. } else
  6521. val &= ~DREF_SSC1_ENABLE;
  6522. /* Get SSC going before enabling the outputs */
  6523. I915_WRITE(PCH_DREF_CONTROL, val);
  6524. POSTING_READ(PCH_DREF_CONTROL);
  6525. udelay(200);
  6526. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6527. /* Enable CPU source on CPU attached eDP */
  6528. if (has_cpu_edp) {
  6529. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6530. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6531. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6532. } else
  6533. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6534. } else
  6535. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6536. I915_WRITE(PCH_DREF_CONTROL, val);
  6537. POSTING_READ(PCH_DREF_CONTROL);
  6538. udelay(200);
  6539. } else {
  6540. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6541. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6542. /* Turn off CPU output */
  6543. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6544. I915_WRITE(PCH_DREF_CONTROL, val);
  6545. POSTING_READ(PCH_DREF_CONTROL);
  6546. udelay(200);
  6547. if (!using_ssc_source) {
  6548. DRM_DEBUG_KMS("Disabling SSC source\n");
  6549. /* Turn off the SSC source */
  6550. val &= ~DREF_SSC_SOURCE_MASK;
  6551. val |= DREF_SSC_SOURCE_DISABLE;
  6552. /* Turn off SSC1 */
  6553. val &= ~DREF_SSC1_ENABLE;
  6554. I915_WRITE(PCH_DREF_CONTROL, val);
  6555. POSTING_READ(PCH_DREF_CONTROL);
  6556. udelay(200);
  6557. }
  6558. }
  6559. BUG_ON(val != final);
  6560. }
  6561. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6562. {
  6563. uint32_t tmp;
  6564. tmp = I915_READ(SOUTH_CHICKEN2);
  6565. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6566. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6567. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6568. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6569. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6570. tmp = I915_READ(SOUTH_CHICKEN2);
  6571. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6572. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6573. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6574. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6575. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6576. }
  6577. /* WaMPhyProgramming:hsw */
  6578. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6579. {
  6580. uint32_t tmp;
  6581. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6582. tmp &= ~(0xFF << 24);
  6583. tmp |= (0x12 << 24);
  6584. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6585. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6586. tmp |= (1 << 11);
  6587. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6588. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6589. tmp |= (1 << 11);
  6590. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6591. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6592. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6593. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6594. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6595. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6596. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6597. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6598. tmp &= ~(7 << 13);
  6599. tmp |= (5 << 13);
  6600. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6601. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6602. tmp &= ~(7 << 13);
  6603. tmp |= (5 << 13);
  6604. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6605. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6606. tmp &= ~0xFF;
  6607. tmp |= 0x1C;
  6608. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6609. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6610. tmp &= ~0xFF;
  6611. tmp |= 0x1C;
  6612. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6613. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6614. tmp &= ~(0xFF << 16);
  6615. tmp |= (0x1C << 16);
  6616. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6617. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6618. tmp &= ~(0xFF << 16);
  6619. tmp |= (0x1C << 16);
  6620. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6621. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6622. tmp |= (1 << 27);
  6623. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6624. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6625. tmp |= (1 << 27);
  6626. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6627. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6628. tmp &= ~(0xF << 28);
  6629. tmp |= (4 << 28);
  6630. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6631. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6632. tmp &= ~(0xF << 28);
  6633. tmp |= (4 << 28);
  6634. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6635. }
  6636. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6637. * Programming" based on the parameters passed:
  6638. * - Sequence to enable CLKOUT_DP
  6639. * - Sequence to enable CLKOUT_DP without spread
  6640. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6641. */
  6642. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6643. bool with_spread, bool with_fdi)
  6644. {
  6645. uint32_t reg, tmp;
  6646. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6647. with_spread = true;
  6648. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6649. with_fdi, "LP PCH doesn't have FDI\n"))
  6650. with_fdi = false;
  6651. mutex_lock(&dev_priv->sb_lock);
  6652. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6653. tmp &= ~SBI_SSCCTL_DISABLE;
  6654. tmp |= SBI_SSCCTL_PATHALT;
  6655. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6656. udelay(24);
  6657. if (with_spread) {
  6658. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6659. tmp &= ~SBI_SSCCTL_PATHALT;
  6660. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6661. if (with_fdi) {
  6662. lpt_reset_fdi_mphy(dev_priv);
  6663. lpt_program_fdi_mphy(dev_priv);
  6664. }
  6665. }
  6666. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6667. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6668. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6669. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6670. mutex_unlock(&dev_priv->sb_lock);
  6671. }
  6672. /* Sequence to disable CLKOUT_DP */
  6673. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6674. {
  6675. uint32_t reg, tmp;
  6676. mutex_lock(&dev_priv->sb_lock);
  6677. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6678. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6679. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6680. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6681. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6682. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6683. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6684. tmp |= SBI_SSCCTL_PATHALT;
  6685. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6686. udelay(32);
  6687. }
  6688. tmp |= SBI_SSCCTL_DISABLE;
  6689. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6690. }
  6691. mutex_unlock(&dev_priv->sb_lock);
  6692. }
  6693. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6694. static const uint16_t sscdivintphase[] = {
  6695. [BEND_IDX( 50)] = 0x3B23,
  6696. [BEND_IDX( 45)] = 0x3B23,
  6697. [BEND_IDX( 40)] = 0x3C23,
  6698. [BEND_IDX( 35)] = 0x3C23,
  6699. [BEND_IDX( 30)] = 0x3D23,
  6700. [BEND_IDX( 25)] = 0x3D23,
  6701. [BEND_IDX( 20)] = 0x3E23,
  6702. [BEND_IDX( 15)] = 0x3E23,
  6703. [BEND_IDX( 10)] = 0x3F23,
  6704. [BEND_IDX( 5)] = 0x3F23,
  6705. [BEND_IDX( 0)] = 0x0025,
  6706. [BEND_IDX( -5)] = 0x0025,
  6707. [BEND_IDX(-10)] = 0x0125,
  6708. [BEND_IDX(-15)] = 0x0125,
  6709. [BEND_IDX(-20)] = 0x0225,
  6710. [BEND_IDX(-25)] = 0x0225,
  6711. [BEND_IDX(-30)] = 0x0325,
  6712. [BEND_IDX(-35)] = 0x0325,
  6713. [BEND_IDX(-40)] = 0x0425,
  6714. [BEND_IDX(-45)] = 0x0425,
  6715. [BEND_IDX(-50)] = 0x0525,
  6716. };
  6717. /*
  6718. * Bend CLKOUT_DP
  6719. * steps -50 to 50 inclusive, in steps of 5
  6720. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6721. * change in clock period = -(steps / 10) * 5.787 ps
  6722. */
  6723. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6724. {
  6725. uint32_t tmp;
  6726. int idx = BEND_IDX(steps);
  6727. if (WARN_ON(steps % 5 != 0))
  6728. return;
  6729. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6730. return;
  6731. mutex_lock(&dev_priv->sb_lock);
  6732. if (steps % 10 != 0)
  6733. tmp = 0xAAAAAAAB;
  6734. else
  6735. tmp = 0x00000000;
  6736. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6737. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6738. tmp &= 0xffff0000;
  6739. tmp |= sscdivintphase[idx];
  6740. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6741. mutex_unlock(&dev_priv->sb_lock);
  6742. }
  6743. #undef BEND_IDX
  6744. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6745. {
  6746. struct intel_encoder *encoder;
  6747. bool has_vga = false;
  6748. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6749. switch (encoder->type) {
  6750. case INTEL_OUTPUT_ANALOG:
  6751. has_vga = true;
  6752. break;
  6753. default:
  6754. break;
  6755. }
  6756. }
  6757. if (has_vga) {
  6758. lpt_bend_clkout_dp(dev_priv, 0);
  6759. lpt_enable_clkout_dp(dev_priv, true, true);
  6760. } else {
  6761. lpt_disable_clkout_dp(dev_priv);
  6762. }
  6763. }
  6764. /*
  6765. * Initialize reference clocks when the driver loads
  6766. */
  6767. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6768. {
  6769. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6770. ironlake_init_pch_refclk(dev_priv);
  6771. else if (HAS_PCH_LPT(dev_priv))
  6772. lpt_init_pch_refclk(dev_priv);
  6773. }
  6774. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6775. {
  6776. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6778. int pipe = intel_crtc->pipe;
  6779. uint32_t val;
  6780. val = 0;
  6781. switch (intel_crtc->config->pipe_bpp) {
  6782. case 18:
  6783. val |= PIPECONF_6BPC;
  6784. break;
  6785. case 24:
  6786. val |= PIPECONF_8BPC;
  6787. break;
  6788. case 30:
  6789. val |= PIPECONF_10BPC;
  6790. break;
  6791. case 36:
  6792. val |= PIPECONF_12BPC;
  6793. break;
  6794. default:
  6795. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6796. BUG();
  6797. }
  6798. if (intel_crtc->config->dither)
  6799. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6800. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6801. val |= PIPECONF_INTERLACED_ILK;
  6802. else
  6803. val |= PIPECONF_PROGRESSIVE;
  6804. if (intel_crtc->config->limited_color_range)
  6805. val |= PIPECONF_COLOR_RANGE_SELECT;
  6806. I915_WRITE(PIPECONF(pipe), val);
  6807. POSTING_READ(PIPECONF(pipe));
  6808. }
  6809. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6810. {
  6811. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6813. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6814. u32 val = 0;
  6815. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6816. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6817. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6818. val |= PIPECONF_INTERLACED_ILK;
  6819. else
  6820. val |= PIPECONF_PROGRESSIVE;
  6821. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6822. POSTING_READ(PIPECONF(cpu_transcoder));
  6823. }
  6824. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6825. {
  6826. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6828. struct intel_crtc_state *config = intel_crtc->config;
  6829. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6830. u32 val = 0;
  6831. switch (intel_crtc->config->pipe_bpp) {
  6832. case 18:
  6833. val |= PIPEMISC_DITHER_6_BPC;
  6834. break;
  6835. case 24:
  6836. val |= PIPEMISC_DITHER_8_BPC;
  6837. break;
  6838. case 30:
  6839. val |= PIPEMISC_DITHER_10_BPC;
  6840. break;
  6841. case 36:
  6842. val |= PIPEMISC_DITHER_12_BPC;
  6843. break;
  6844. default:
  6845. /* Case prevented by pipe_config_set_bpp. */
  6846. BUG();
  6847. }
  6848. if (intel_crtc->config->dither)
  6849. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6850. if (config->ycbcr420) {
  6851. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6852. PIPEMISC_YUV420_ENABLE |
  6853. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6854. }
  6855. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6856. }
  6857. }
  6858. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6859. {
  6860. /*
  6861. * Account for spread spectrum to avoid
  6862. * oversubscribing the link. Max center spread
  6863. * is 2.5%; use 5% for safety's sake.
  6864. */
  6865. u32 bps = target_clock * bpp * 21 / 20;
  6866. return DIV_ROUND_UP(bps, link_bw * 8);
  6867. }
  6868. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6869. {
  6870. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6871. }
  6872. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6873. struct intel_crtc_state *crtc_state,
  6874. struct dpll *reduced_clock)
  6875. {
  6876. struct drm_crtc *crtc = &intel_crtc->base;
  6877. struct drm_device *dev = crtc->dev;
  6878. struct drm_i915_private *dev_priv = to_i915(dev);
  6879. u32 dpll, fp, fp2;
  6880. int factor;
  6881. /* Enable autotuning of the PLL clock (if permissible) */
  6882. factor = 21;
  6883. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6884. if ((intel_panel_use_ssc(dev_priv) &&
  6885. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6886. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6887. factor = 25;
  6888. } else if (crtc_state->sdvo_tv_clock)
  6889. factor = 20;
  6890. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6891. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6892. fp |= FP_CB_TUNE;
  6893. if (reduced_clock) {
  6894. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6895. if (reduced_clock->m < factor * reduced_clock->n)
  6896. fp2 |= FP_CB_TUNE;
  6897. } else {
  6898. fp2 = fp;
  6899. }
  6900. dpll = 0;
  6901. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6902. dpll |= DPLLB_MODE_LVDS;
  6903. else
  6904. dpll |= DPLLB_MODE_DAC_SERIAL;
  6905. dpll |= (crtc_state->pixel_multiplier - 1)
  6906. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6907. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6908. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6909. dpll |= DPLL_SDVO_HIGH_SPEED;
  6910. if (intel_crtc_has_dp_encoder(crtc_state))
  6911. dpll |= DPLL_SDVO_HIGH_SPEED;
  6912. /*
  6913. * The high speed IO clock is only really required for
  6914. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6915. * possible to share the DPLL between CRT and HDMI. Enabling
  6916. * the clock needlessly does no real harm, except use up a
  6917. * bit of power potentially.
  6918. *
  6919. * We'll limit this to IVB with 3 pipes, since it has only two
  6920. * DPLLs and so DPLL sharing is the only way to get three pipes
  6921. * driving PCH ports at the same time. On SNB we could do this,
  6922. * and potentially avoid enabling the second DPLL, but it's not
  6923. * clear if it''s a win or loss power wise. No point in doing
  6924. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6925. */
  6926. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6927. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6928. dpll |= DPLL_SDVO_HIGH_SPEED;
  6929. /* compute bitmask from p1 value */
  6930. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6931. /* also FPA1 */
  6932. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6933. switch (crtc_state->dpll.p2) {
  6934. case 5:
  6935. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6936. break;
  6937. case 7:
  6938. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6939. break;
  6940. case 10:
  6941. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6942. break;
  6943. case 14:
  6944. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6945. break;
  6946. }
  6947. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6948. intel_panel_use_ssc(dev_priv))
  6949. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6950. else
  6951. dpll |= PLL_REF_INPUT_DREFCLK;
  6952. dpll |= DPLL_VCO_ENABLE;
  6953. crtc_state->dpll_hw_state.dpll = dpll;
  6954. crtc_state->dpll_hw_state.fp0 = fp;
  6955. crtc_state->dpll_hw_state.fp1 = fp2;
  6956. }
  6957. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6958. struct intel_crtc_state *crtc_state)
  6959. {
  6960. struct drm_device *dev = crtc->base.dev;
  6961. struct drm_i915_private *dev_priv = to_i915(dev);
  6962. const struct intel_limit *limit;
  6963. int refclk = 120000;
  6964. memset(&crtc_state->dpll_hw_state, 0,
  6965. sizeof(crtc_state->dpll_hw_state));
  6966. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6967. if (!crtc_state->has_pch_encoder)
  6968. return 0;
  6969. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6970. if (intel_panel_use_ssc(dev_priv)) {
  6971. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6972. dev_priv->vbt.lvds_ssc_freq);
  6973. refclk = dev_priv->vbt.lvds_ssc_freq;
  6974. }
  6975. if (intel_is_dual_link_lvds(dev)) {
  6976. if (refclk == 100000)
  6977. limit = &intel_limits_ironlake_dual_lvds_100m;
  6978. else
  6979. limit = &intel_limits_ironlake_dual_lvds;
  6980. } else {
  6981. if (refclk == 100000)
  6982. limit = &intel_limits_ironlake_single_lvds_100m;
  6983. else
  6984. limit = &intel_limits_ironlake_single_lvds;
  6985. }
  6986. } else {
  6987. limit = &intel_limits_ironlake_dac;
  6988. }
  6989. if (!crtc_state->clock_set &&
  6990. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6991. refclk, NULL, &crtc_state->dpll)) {
  6992. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6993. return -EINVAL;
  6994. }
  6995. ironlake_compute_dpll(crtc, crtc_state, NULL);
  6996. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  6997. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6998. pipe_name(crtc->pipe));
  6999. return -EINVAL;
  7000. }
  7001. return 0;
  7002. }
  7003. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7004. struct intel_link_m_n *m_n)
  7005. {
  7006. struct drm_device *dev = crtc->base.dev;
  7007. struct drm_i915_private *dev_priv = to_i915(dev);
  7008. enum pipe pipe = crtc->pipe;
  7009. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7010. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7011. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7012. & ~TU_SIZE_MASK;
  7013. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7014. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7015. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7016. }
  7017. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7018. enum transcoder transcoder,
  7019. struct intel_link_m_n *m_n,
  7020. struct intel_link_m_n *m2_n2)
  7021. {
  7022. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7023. enum pipe pipe = crtc->pipe;
  7024. if (INTEL_GEN(dev_priv) >= 5) {
  7025. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7026. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7027. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7028. & ~TU_SIZE_MASK;
  7029. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7030. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7031. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7032. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7033. * gen < 8) and if DRRS is supported (to make sure the
  7034. * registers are not unnecessarily read).
  7035. */
  7036. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7037. crtc->config->has_drrs) {
  7038. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7039. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7040. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7041. & ~TU_SIZE_MASK;
  7042. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7043. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7044. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7045. }
  7046. } else {
  7047. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7048. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7049. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7050. & ~TU_SIZE_MASK;
  7051. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7052. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7053. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7054. }
  7055. }
  7056. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7057. struct intel_crtc_state *pipe_config)
  7058. {
  7059. if (pipe_config->has_pch_encoder)
  7060. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7061. else
  7062. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7063. &pipe_config->dp_m_n,
  7064. &pipe_config->dp_m2_n2);
  7065. }
  7066. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7067. struct intel_crtc_state *pipe_config)
  7068. {
  7069. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7070. &pipe_config->fdi_m_n, NULL);
  7071. }
  7072. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7073. struct intel_crtc_state *pipe_config)
  7074. {
  7075. struct drm_device *dev = crtc->base.dev;
  7076. struct drm_i915_private *dev_priv = to_i915(dev);
  7077. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7078. uint32_t ps_ctrl = 0;
  7079. int id = -1;
  7080. int i;
  7081. /* find scaler attached to this pipe */
  7082. for (i = 0; i < crtc->num_scalers; i++) {
  7083. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7084. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7085. id = i;
  7086. pipe_config->pch_pfit.enabled = true;
  7087. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7088. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7089. break;
  7090. }
  7091. }
  7092. scaler_state->scaler_id = id;
  7093. if (id >= 0) {
  7094. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7095. } else {
  7096. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7097. }
  7098. }
  7099. static void
  7100. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7101. struct intel_initial_plane_config *plane_config)
  7102. {
  7103. struct drm_device *dev = crtc->base.dev;
  7104. struct drm_i915_private *dev_priv = to_i915(dev);
  7105. u32 val, base, offset, stride_mult, tiling, alpha;
  7106. int pipe = crtc->pipe;
  7107. int fourcc, pixel_format;
  7108. unsigned int aligned_height;
  7109. struct drm_framebuffer *fb;
  7110. struct intel_framebuffer *intel_fb;
  7111. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7112. if (!intel_fb) {
  7113. DRM_DEBUG_KMS("failed to alloc fb\n");
  7114. return;
  7115. }
  7116. fb = &intel_fb->base;
  7117. fb->dev = dev;
  7118. val = I915_READ(PLANE_CTL(pipe, 0));
  7119. if (!(val & PLANE_CTL_ENABLE))
  7120. goto error;
  7121. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7122. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7123. alpha = I915_READ(PLANE_COLOR_CTL(pipe, 0));
  7124. alpha &= PLANE_COLOR_ALPHA_MASK;
  7125. } else {
  7126. alpha = val & PLANE_CTL_ALPHA_MASK;
  7127. }
  7128. fourcc = skl_format_to_fourcc(pixel_format,
  7129. val & PLANE_CTL_ORDER_RGBX, alpha);
  7130. fb->format = drm_format_info(fourcc);
  7131. tiling = val & PLANE_CTL_TILED_MASK;
  7132. switch (tiling) {
  7133. case PLANE_CTL_TILED_LINEAR:
  7134. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7135. break;
  7136. case PLANE_CTL_TILED_X:
  7137. plane_config->tiling = I915_TILING_X;
  7138. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7139. break;
  7140. case PLANE_CTL_TILED_Y:
  7141. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7142. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7143. else
  7144. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7145. break;
  7146. case PLANE_CTL_TILED_YF:
  7147. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7148. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7149. else
  7150. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7151. break;
  7152. default:
  7153. MISSING_CASE(tiling);
  7154. goto error;
  7155. }
  7156. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7157. plane_config->base = base;
  7158. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7159. val = I915_READ(PLANE_SIZE(pipe, 0));
  7160. fb->height = ((val >> 16) & 0xfff) + 1;
  7161. fb->width = ((val >> 0) & 0x1fff) + 1;
  7162. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7163. stride_mult = intel_fb_stride_alignment(fb, 0);
  7164. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7165. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7166. plane_config->size = fb->pitches[0] * aligned_height;
  7167. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7168. pipe_name(pipe), fb->width, fb->height,
  7169. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7170. plane_config->size);
  7171. plane_config->fb = intel_fb;
  7172. return;
  7173. error:
  7174. kfree(intel_fb);
  7175. }
  7176. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7177. struct intel_crtc_state *pipe_config)
  7178. {
  7179. struct drm_device *dev = crtc->base.dev;
  7180. struct drm_i915_private *dev_priv = to_i915(dev);
  7181. uint32_t tmp;
  7182. tmp = I915_READ(PF_CTL(crtc->pipe));
  7183. if (tmp & PF_ENABLE) {
  7184. pipe_config->pch_pfit.enabled = true;
  7185. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7186. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7187. /* We currently do not free assignements of panel fitters on
  7188. * ivb/hsw (since we don't use the higher upscaling modes which
  7189. * differentiates them) so just WARN about this case for now. */
  7190. if (IS_GEN7(dev_priv)) {
  7191. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7192. PF_PIPE_SEL_IVB(crtc->pipe));
  7193. }
  7194. }
  7195. }
  7196. static void
  7197. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7198. struct intel_initial_plane_config *plane_config)
  7199. {
  7200. struct drm_device *dev = crtc->base.dev;
  7201. struct drm_i915_private *dev_priv = to_i915(dev);
  7202. u32 val, base, offset;
  7203. int pipe = crtc->pipe;
  7204. int fourcc, pixel_format;
  7205. unsigned int aligned_height;
  7206. struct drm_framebuffer *fb;
  7207. struct intel_framebuffer *intel_fb;
  7208. val = I915_READ(DSPCNTR(pipe));
  7209. if (!(val & DISPLAY_PLANE_ENABLE))
  7210. return;
  7211. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7212. if (!intel_fb) {
  7213. DRM_DEBUG_KMS("failed to alloc fb\n");
  7214. return;
  7215. }
  7216. fb = &intel_fb->base;
  7217. fb->dev = dev;
  7218. if (INTEL_GEN(dev_priv) >= 4) {
  7219. if (val & DISPPLANE_TILED) {
  7220. plane_config->tiling = I915_TILING_X;
  7221. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7222. }
  7223. }
  7224. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7225. fourcc = i9xx_format_to_fourcc(pixel_format);
  7226. fb->format = drm_format_info(fourcc);
  7227. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7228. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7229. offset = I915_READ(DSPOFFSET(pipe));
  7230. } else {
  7231. if (plane_config->tiling)
  7232. offset = I915_READ(DSPTILEOFF(pipe));
  7233. else
  7234. offset = I915_READ(DSPLINOFF(pipe));
  7235. }
  7236. plane_config->base = base;
  7237. val = I915_READ(PIPESRC(pipe));
  7238. fb->width = ((val >> 16) & 0xfff) + 1;
  7239. fb->height = ((val >> 0) & 0xfff) + 1;
  7240. val = I915_READ(DSPSTRIDE(pipe));
  7241. fb->pitches[0] = val & 0xffffffc0;
  7242. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7243. plane_config->size = fb->pitches[0] * aligned_height;
  7244. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7245. pipe_name(pipe), fb->width, fb->height,
  7246. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7247. plane_config->size);
  7248. plane_config->fb = intel_fb;
  7249. }
  7250. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7251. struct intel_crtc_state *pipe_config)
  7252. {
  7253. struct drm_device *dev = crtc->base.dev;
  7254. struct drm_i915_private *dev_priv = to_i915(dev);
  7255. enum intel_display_power_domain power_domain;
  7256. uint32_t tmp;
  7257. bool ret;
  7258. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7259. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7260. return false;
  7261. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7262. pipe_config->shared_dpll = NULL;
  7263. ret = false;
  7264. tmp = I915_READ(PIPECONF(crtc->pipe));
  7265. if (!(tmp & PIPECONF_ENABLE))
  7266. goto out;
  7267. switch (tmp & PIPECONF_BPC_MASK) {
  7268. case PIPECONF_6BPC:
  7269. pipe_config->pipe_bpp = 18;
  7270. break;
  7271. case PIPECONF_8BPC:
  7272. pipe_config->pipe_bpp = 24;
  7273. break;
  7274. case PIPECONF_10BPC:
  7275. pipe_config->pipe_bpp = 30;
  7276. break;
  7277. case PIPECONF_12BPC:
  7278. pipe_config->pipe_bpp = 36;
  7279. break;
  7280. default:
  7281. break;
  7282. }
  7283. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7284. pipe_config->limited_color_range = true;
  7285. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7286. struct intel_shared_dpll *pll;
  7287. enum intel_dpll_id pll_id;
  7288. pipe_config->has_pch_encoder = true;
  7289. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7290. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7291. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7292. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7293. if (HAS_PCH_IBX(dev_priv)) {
  7294. /*
  7295. * The pipe->pch transcoder and pch transcoder->pll
  7296. * mapping is fixed.
  7297. */
  7298. pll_id = (enum intel_dpll_id) crtc->pipe;
  7299. } else {
  7300. tmp = I915_READ(PCH_DPLL_SEL);
  7301. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7302. pll_id = DPLL_ID_PCH_PLL_B;
  7303. else
  7304. pll_id= DPLL_ID_PCH_PLL_A;
  7305. }
  7306. pipe_config->shared_dpll =
  7307. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7308. pll = pipe_config->shared_dpll;
  7309. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7310. &pipe_config->dpll_hw_state));
  7311. tmp = pipe_config->dpll_hw_state.dpll;
  7312. pipe_config->pixel_multiplier =
  7313. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7314. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7315. ironlake_pch_clock_get(crtc, pipe_config);
  7316. } else {
  7317. pipe_config->pixel_multiplier = 1;
  7318. }
  7319. intel_get_pipe_timings(crtc, pipe_config);
  7320. intel_get_pipe_src_size(crtc, pipe_config);
  7321. ironlake_get_pfit_config(crtc, pipe_config);
  7322. ret = true;
  7323. out:
  7324. intel_display_power_put(dev_priv, power_domain);
  7325. return ret;
  7326. }
  7327. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7328. {
  7329. struct drm_device *dev = &dev_priv->drm;
  7330. struct intel_crtc *crtc;
  7331. for_each_intel_crtc(dev, crtc)
  7332. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7333. pipe_name(crtc->pipe));
  7334. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7335. "Display power well on\n");
  7336. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7337. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7338. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7339. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7340. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7341. "CPU PWM1 enabled\n");
  7342. if (IS_HASWELL(dev_priv))
  7343. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7344. "CPU PWM2 enabled\n");
  7345. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7346. "PCH PWM1 enabled\n");
  7347. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7348. "Utility pin enabled\n");
  7349. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7350. /*
  7351. * In theory we can still leave IRQs enabled, as long as only the HPD
  7352. * interrupts remain enabled. We used to check for that, but since it's
  7353. * gen-specific and since we only disable LCPLL after we fully disable
  7354. * the interrupts, the check below should be enough.
  7355. */
  7356. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7357. }
  7358. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7359. {
  7360. if (IS_HASWELL(dev_priv))
  7361. return I915_READ(D_COMP_HSW);
  7362. else
  7363. return I915_READ(D_COMP_BDW);
  7364. }
  7365. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7366. {
  7367. if (IS_HASWELL(dev_priv)) {
  7368. mutex_lock(&dev_priv->pcu_lock);
  7369. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7370. val))
  7371. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7372. mutex_unlock(&dev_priv->pcu_lock);
  7373. } else {
  7374. I915_WRITE(D_COMP_BDW, val);
  7375. POSTING_READ(D_COMP_BDW);
  7376. }
  7377. }
  7378. /*
  7379. * This function implements pieces of two sequences from BSpec:
  7380. * - Sequence for display software to disable LCPLL
  7381. * - Sequence for display software to allow package C8+
  7382. * The steps implemented here are just the steps that actually touch the LCPLL
  7383. * register. Callers should take care of disabling all the display engine
  7384. * functions, doing the mode unset, fixing interrupts, etc.
  7385. */
  7386. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7387. bool switch_to_fclk, bool allow_power_down)
  7388. {
  7389. uint32_t val;
  7390. assert_can_disable_lcpll(dev_priv);
  7391. val = I915_READ(LCPLL_CTL);
  7392. if (switch_to_fclk) {
  7393. val |= LCPLL_CD_SOURCE_FCLK;
  7394. I915_WRITE(LCPLL_CTL, val);
  7395. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7396. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7397. DRM_ERROR("Switching to FCLK failed\n");
  7398. val = I915_READ(LCPLL_CTL);
  7399. }
  7400. val |= LCPLL_PLL_DISABLE;
  7401. I915_WRITE(LCPLL_CTL, val);
  7402. POSTING_READ(LCPLL_CTL);
  7403. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7404. DRM_ERROR("LCPLL still locked\n");
  7405. val = hsw_read_dcomp(dev_priv);
  7406. val |= D_COMP_COMP_DISABLE;
  7407. hsw_write_dcomp(dev_priv, val);
  7408. ndelay(100);
  7409. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7410. 1))
  7411. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7412. if (allow_power_down) {
  7413. val = I915_READ(LCPLL_CTL);
  7414. val |= LCPLL_POWER_DOWN_ALLOW;
  7415. I915_WRITE(LCPLL_CTL, val);
  7416. POSTING_READ(LCPLL_CTL);
  7417. }
  7418. }
  7419. /*
  7420. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7421. * source.
  7422. */
  7423. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7424. {
  7425. uint32_t val;
  7426. val = I915_READ(LCPLL_CTL);
  7427. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7428. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7429. return;
  7430. /*
  7431. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7432. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7433. */
  7434. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7435. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7436. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7437. I915_WRITE(LCPLL_CTL, val);
  7438. POSTING_READ(LCPLL_CTL);
  7439. }
  7440. val = hsw_read_dcomp(dev_priv);
  7441. val |= D_COMP_COMP_FORCE;
  7442. val &= ~D_COMP_COMP_DISABLE;
  7443. hsw_write_dcomp(dev_priv, val);
  7444. val = I915_READ(LCPLL_CTL);
  7445. val &= ~LCPLL_PLL_DISABLE;
  7446. I915_WRITE(LCPLL_CTL, val);
  7447. if (intel_wait_for_register(dev_priv,
  7448. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7449. 5))
  7450. DRM_ERROR("LCPLL not locked yet\n");
  7451. if (val & LCPLL_CD_SOURCE_FCLK) {
  7452. val = I915_READ(LCPLL_CTL);
  7453. val &= ~LCPLL_CD_SOURCE_FCLK;
  7454. I915_WRITE(LCPLL_CTL, val);
  7455. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7456. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7457. DRM_ERROR("Switching back to LCPLL failed\n");
  7458. }
  7459. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7460. intel_update_cdclk(dev_priv);
  7461. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7462. }
  7463. /*
  7464. * Package states C8 and deeper are really deep PC states that can only be
  7465. * reached when all the devices on the system allow it, so even if the graphics
  7466. * device allows PC8+, it doesn't mean the system will actually get to these
  7467. * states. Our driver only allows PC8+ when going into runtime PM.
  7468. *
  7469. * The requirements for PC8+ are that all the outputs are disabled, the power
  7470. * well is disabled and most interrupts are disabled, and these are also
  7471. * requirements for runtime PM. When these conditions are met, we manually do
  7472. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7473. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7474. * hang the machine.
  7475. *
  7476. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7477. * the state of some registers, so when we come back from PC8+ we need to
  7478. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7479. * need to take care of the registers kept by RC6. Notice that this happens even
  7480. * if we don't put the device in PCI D3 state (which is what currently happens
  7481. * because of the runtime PM support).
  7482. *
  7483. * For more, read "Display Sequences for Package C8" on the hardware
  7484. * documentation.
  7485. */
  7486. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7487. {
  7488. uint32_t val;
  7489. DRM_DEBUG_KMS("Enabling package C8+\n");
  7490. if (HAS_PCH_LPT_LP(dev_priv)) {
  7491. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7492. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7493. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7494. }
  7495. lpt_disable_clkout_dp(dev_priv);
  7496. hsw_disable_lcpll(dev_priv, true, true);
  7497. }
  7498. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7499. {
  7500. uint32_t val;
  7501. DRM_DEBUG_KMS("Disabling package C8+\n");
  7502. hsw_restore_lcpll(dev_priv);
  7503. lpt_init_pch_refclk(dev_priv);
  7504. if (HAS_PCH_LPT_LP(dev_priv)) {
  7505. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7506. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7507. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7508. }
  7509. }
  7510. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7511. struct intel_crtc_state *crtc_state)
  7512. {
  7513. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7514. struct intel_encoder *encoder =
  7515. intel_ddi_get_crtc_new_encoder(crtc_state);
  7516. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7517. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7518. pipe_name(crtc->pipe));
  7519. return -EINVAL;
  7520. }
  7521. }
  7522. return 0;
  7523. }
  7524. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7525. enum port port,
  7526. struct intel_crtc_state *pipe_config)
  7527. {
  7528. enum intel_dpll_id id;
  7529. u32 temp;
  7530. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7531. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7532. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7533. return;
  7534. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7535. }
  7536. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7537. enum port port,
  7538. struct intel_crtc_state *pipe_config)
  7539. {
  7540. enum intel_dpll_id id;
  7541. switch (port) {
  7542. case PORT_A:
  7543. id = DPLL_ID_SKL_DPLL0;
  7544. break;
  7545. case PORT_B:
  7546. id = DPLL_ID_SKL_DPLL1;
  7547. break;
  7548. case PORT_C:
  7549. id = DPLL_ID_SKL_DPLL2;
  7550. break;
  7551. default:
  7552. DRM_ERROR("Incorrect port type\n");
  7553. return;
  7554. }
  7555. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7556. }
  7557. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7558. enum port port,
  7559. struct intel_crtc_state *pipe_config)
  7560. {
  7561. enum intel_dpll_id id;
  7562. u32 temp;
  7563. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7564. id = temp >> (port * 3 + 1);
  7565. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7566. return;
  7567. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7568. }
  7569. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7570. enum port port,
  7571. struct intel_crtc_state *pipe_config)
  7572. {
  7573. enum intel_dpll_id id;
  7574. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7575. switch (ddi_pll_sel) {
  7576. case PORT_CLK_SEL_WRPLL1:
  7577. id = DPLL_ID_WRPLL1;
  7578. break;
  7579. case PORT_CLK_SEL_WRPLL2:
  7580. id = DPLL_ID_WRPLL2;
  7581. break;
  7582. case PORT_CLK_SEL_SPLL:
  7583. id = DPLL_ID_SPLL;
  7584. break;
  7585. case PORT_CLK_SEL_LCPLL_810:
  7586. id = DPLL_ID_LCPLL_810;
  7587. break;
  7588. case PORT_CLK_SEL_LCPLL_1350:
  7589. id = DPLL_ID_LCPLL_1350;
  7590. break;
  7591. case PORT_CLK_SEL_LCPLL_2700:
  7592. id = DPLL_ID_LCPLL_2700;
  7593. break;
  7594. default:
  7595. MISSING_CASE(ddi_pll_sel);
  7596. /* fall through */
  7597. case PORT_CLK_SEL_NONE:
  7598. return;
  7599. }
  7600. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7601. }
  7602. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7603. struct intel_crtc_state *pipe_config,
  7604. u64 *power_domain_mask)
  7605. {
  7606. struct drm_device *dev = crtc->base.dev;
  7607. struct drm_i915_private *dev_priv = to_i915(dev);
  7608. enum intel_display_power_domain power_domain;
  7609. u32 tmp;
  7610. /*
  7611. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7612. * transcoder handled below.
  7613. */
  7614. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7615. /*
  7616. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7617. * consistency and less surprising code; it's in always on power).
  7618. */
  7619. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7620. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7621. enum pipe trans_edp_pipe;
  7622. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7623. default:
  7624. WARN(1, "unknown pipe linked to edp transcoder\n");
  7625. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7626. case TRANS_DDI_EDP_INPUT_A_ON:
  7627. trans_edp_pipe = PIPE_A;
  7628. break;
  7629. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7630. trans_edp_pipe = PIPE_B;
  7631. break;
  7632. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7633. trans_edp_pipe = PIPE_C;
  7634. break;
  7635. }
  7636. if (trans_edp_pipe == crtc->pipe)
  7637. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7638. }
  7639. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7640. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7641. return false;
  7642. *power_domain_mask |= BIT_ULL(power_domain);
  7643. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7644. return tmp & PIPECONF_ENABLE;
  7645. }
  7646. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7647. struct intel_crtc_state *pipe_config,
  7648. u64 *power_domain_mask)
  7649. {
  7650. struct drm_device *dev = crtc->base.dev;
  7651. struct drm_i915_private *dev_priv = to_i915(dev);
  7652. enum intel_display_power_domain power_domain;
  7653. enum port port;
  7654. enum transcoder cpu_transcoder;
  7655. u32 tmp;
  7656. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7657. if (port == PORT_A)
  7658. cpu_transcoder = TRANSCODER_DSI_A;
  7659. else
  7660. cpu_transcoder = TRANSCODER_DSI_C;
  7661. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7662. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7663. continue;
  7664. *power_domain_mask |= BIT_ULL(power_domain);
  7665. /*
  7666. * The PLL needs to be enabled with a valid divider
  7667. * configuration, otherwise accessing DSI registers will hang
  7668. * the machine. See BSpec North Display Engine
  7669. * registers/MIPI[BXT]. We can break out here early, since we
  7670. * need the same DSI PLL to be enabled for both DSI ports.
  7671. */
  7672. if (!intel_dsi_pll_is_enabled(dev_priv))
  7673. break;
  7674. /* XXX: this works for video mode only */
  7675. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7676. if (!(tmp & DPI_ENABLE))
  7677. continue;
  7678. tmp = I915_READ(MIPI_CTRL(port));
  7679. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7680. continue;
  7681. pipe_config->cpu_transcoder = cpu_transcoder;
  7682. break;
  7683. }
  7684. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7685. }
  7686. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7687. struct intel_crtc_state *pipe_config)
  7688. {
  7689. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7690. struct intel_shared_dpll *pll;
  7691. enum port port;
  7692. uint32_t tmp;
  7693. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7694. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7695. if (IS_CANNONLAKE(dev_priv))
  7696. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7697. else if (IS_GEN9_BC(dev_priv))
  7698. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7699. else if (IS_GEN9_LP(dev_priv))
  7700. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7701. else
  7702. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7703. pll = pipe_config->shared_dpll;
  7704. if (pll) {
  7705. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7706. &pipe_config->dpll_hw_state));
  7707. }
  7708. /*
  7709. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7710. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7711. * the PCH transcoder is on.
  7712. */
  7713. if (INTEL_GEN(dev_priv) < 9 &&
  7714. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7715. pipe_config->has_pch_encoder = true;
  7716. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7717. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7718. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7719. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7720. }
  7721. }
  7722. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7723. struct intel_crtc_state *pipe_config)
  7724. {
  7725. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7726. enum intel_display_power_domain power_domain;
  7727. u64 power_domain_mask;
  7728. bool active;
  7729. intel_crtc_init_scalers(crtc, pipe_config);
  7730. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7731. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7732. return false;
  7733. power_domain_mask = BIT_ULL(power_domain);
  7734. pipe_config->shared_dpll = NULL;
  7735. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7736. if (IS_GEN9_LP(dev_priv) &&
  7737. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7738. WARN_ON(active);
  7739. active = true;
  7740. }
  7741. if (!active)
  7742. goto out;
  7743. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7744. haswell_get_ddi_port_state(crtc, pipe_config);
  7745. intel_get_pipe_timings(crtc, pipe_config);
  7746. }
  7747. intel_get_pipe_src_size(crtc, pipe_config);
  7748. pipe_config->gamma_mode =
  7749. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7750. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7751. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7752. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7753. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7754. bool blend_mode_420 = tmp &
  7755. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7756. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7757. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7758. pipe_config->ycbcr420 != blend_mode_420)
  7759. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7760. } else if (clrspace_yuv) {
  7761. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7762. }
  7763. }
  7764. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7765. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7766. power_domain_mask |= BIT_ULL(power_domain);
  7767. if (INTEL_GEN(dev_priv) >= 9)
  7768. skylake_get_pfit_config(crtc, pipe_config);
  7769. else
  7770. ironlake_get_pfit_config(crtc, pipe_config);
  7771. }
  7772. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7773. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7774. pipe_config->pixel_multiplier =
  7775. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7776. } else {
  7777. pipe_config->pixel_multiplier = 1;
  7778. }
  7779. out:
  7780. for_each_power_domain(power_domain, power_domain_mask)
  7781. intel_display_power_put(dev_priv, power_domain);
  7782. return active;
  7783. }
  7784. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7785. {
  7786. struct drm_i915_private *dev_priv =
  7787. to_i915(plane_state->base.plane->dev);
  7788. const struct drm_framebuffer *fb = plane_state->base.fb;
  7789. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7790. u32 base;
  7791. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7792. base = obj->phys_handle->busaddr;
  7793. else
  7794. base = intel_plane_ggtt_offset(plane_state);
  7795. base += plane_state->main.offset;
  7796. /* ILK+ do this automagically */
  7797. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7798. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7799. base += (plane_state->base.crtc_h *
  7800. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7801. return base;
  7802. }
  7803. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7804. {
  7805. int x = plane_state->base.crtc_x;
  7806. int y = plane_state->base.crtc_y;
  7807. u32 pos = 0;
  7808. if (x < 0) {
  7809. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7810. x = -x;
  7811. }
  7812. pos |= x << CURSOR_X_SHIFT;
  7813. if (y < 0) {
  7814. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7815. y = -y;
  7816. }
  7817. pos |= y << CURSOR_Y_SHIFT;
  7818. return pos;
  7819. }
  7820. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7821. {
  7822. const struct drm_mode_config *config =
  7823. &plane_state->base.plane->dev->mode_config;
  7824. int width = plane_state->base.crtc_w;
  7825. int height = plane_state->base.crtc_h;
  7826. return width > 0 && width <= config->cursor_width &&
  7827. height > 0 && height <= config->cursor_height;
  7828. }
  7829. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7830. struct intel_plane_state *plane_state)
  7831. {
  7832. const struct drm_framebuffer *fb = plane_state->base.fb;
  7833. int src_x, src_y;
  7834. u32 offset;
  7835. int ret;
  7836. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7837. &crtc_state->base,
  7838. &plane_state->clip,
  7839. DRM_PLANE_HELPER_NO_SCALING,
  7840. DRM_PLANE_HELPER_NO_SCALING,
  7841. true, true);
  7842. if (ret)
  7843. return ret;
  7844. if (!fb)
  7845. return 0;
  7846. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7847. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7848. return -EINVAL;
  7849. }
  7850. src_x = plane_state->base.src_x >> 16;
  7851. src_y = plane_state->base.src_y >> 16;
  7852. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7853. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7854. if (src_x != 0 || src_y != 0) {
  7855. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7856. return -EINVAL;
  7857. }
  7858. plane_state->main.offset = offset;
  7859. return 0;
  7860. }
  7861. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7862. const struct intel_plane_state *plane_state)
  7863. {
  7864. const struct drm_framebuffer *fb = plane_state->base.fb;
  7865. return CURSOR_ENABLE |
  7866. CURSOR_GAMMA_ENABLE |
  7867. CURSOR_FORMAT_ARGB |
  7868. CURSOR_STRIDE(fb->pitches[0]);
  7869. }
  7870. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7871. {
  7872. int width = plane_state->base.crtc_w;
  7873. /*
  7874. * 845g/865g are only limited by the width of their cursors,
  7875. * the height is arbitrary up to the precision of the register.
  7876. */
  7877. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7878. }
  7879. static int i845_check_cursor(struct intel_plane *plane,
  7880. struct intel_crtc_state *crtc_state,
  7881. struct intel_plane_state *plane_state)
  7882. {
  7883. const struct drm_framebuffer *fb = plane_state->base.fb;
  7884. int ret;
  7885. ret = intel_check_cursor(crtc_state, plane_state);
  7886. if (ret)
  7887. return ret;
  7888. /* if we want to turn off the cursor ignore width and height */
  7889. if (!fb)
  7890. return 0;
  7891. /* Check for which cursor types we support */
  7892. if (!i845_cursor_size_ok(plane_state)) {
  7893. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7894. plane_state->base.crtc_w,
  7895. plane_state->base.crtc_h);
  7896. return -EINVAL;
  7897. }
  7898. switch (fb->pitches[0]) {
  7899. case 256:
  7900. case 512:
  7901. case 1024:
  7902. case 2048:
  7903. break;
  7904. default:
  7905. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7906. fb->pitches[0]);
  7907. return -EINVAL;
  7908. }
  7909. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7910. return 0;
  7911. }
  7912. static void i845_update_cursor(struct intel_plane *plane,
  7913. const struct intel_crtc_state *crtc_state,
  7914. const struct intel_plane_state *plane_state)
  7915. {
  7916. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7917. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7918. unsigned long irqflags;
  7919. if (plane_state && plane_state->base.visible) {
  7920. unsigned int width = plane_state->base.crtc_w;
  7921. unsigned int height = plane_state->base.crtc_h;
  7922. cntl = plane_state->ctl;
  7923. size = (height << 12) | width;
  7924. base = intel_cursor_base(plane_state);
  7925. pos = intel_cursor_position(plane_state);
  7926. }
  7927. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7928. /* On these chipsets we can only modify the base/size/stride
  7929. * whilst the cursor is disabled.
  7930. */
  7931. if (plane->cursor.base != base ||
  7932. plane->cursor.size != size ||
  7933. plane->cursor.cntl != cntl) {
  7934. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7935. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7936. I915_WRITE_FW(CURSIZE, size);
  7937. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7938. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7939. plane->cursor.base = base;
  7940. plane->cursor.size = size;
  7941. plane->cursor.cntl = cntl;
  7942. } else {
  7943. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7944. }
  7945. POSTING_READ_FW(CURCNTR(PIPE_A));
  7946. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7947. }
  7948. static void i845_disable_cursor(struct intel_plane *plane,
  7949. struct intel_crtc *crtc)
  7950. {
  7951. i845_update_cursor(plane, NULL, NULL);
  7952. }
  7953. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7954. const struct intel_plane_state *plane_state)
  7955. {
  7956. struct drm_i915_private *dev_priv =
  7957. to_i915(plane_state->base.plane->dev);
  7958. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7959. u32 cntl;
  7960. cntl = MCURSOR_GAMMA_ENABLE;
  7961. if (HAS_DDI(dev_priv))
  7962. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7963. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7964. switch (plane_state->base.crtc_w) {
  7965. case 64:
  7966. cntl |= CURSOR_MODE_64_ARGB_AX;
  7967. break;
  7968. case 128:
  7969. cntl |= CURSOR_MODE_128_ARGB_AX;
  7970. break;
  7971. case 256:
  7972. cntl |= CURSOR_MODE_256_ARGB_AX;
  7973. break;
  7974. default:
  7975. MISSING_CASE(plane_state->base.crtc_w);
  7976. return 0;
  7977. }
  7978. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7979. cntl |= CURSOR_ROTATE_180;
  7980. return cntl;
  7981. }
  7982. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  7983. {
  7984. struct drm_i915_private *dev_priv =
  7985. to_i915(plane_state->base.plane->dev);
  7986. int width = plane_state->base.crtc_w;
  7987. int height = plane_state->base.crtc_h;
  7988. if (!intel_cursor_size_ok(plane_state))
  7989. return false;
  7990. /* Cursor width is limited to a few power-of-two sizes */
  7991. switch (width) {
  7992. case 256:
  7993. case 128:
  7994. case 64:
  7995. break;
  7996. default:
  7997. return false;
  7998. }
  7999. /*
  8000. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8001. * height from 8 lines up to the cursor width, when the
  8002. * cursor is not rotated. Everything else requires square
  8003. * cursors.
  8004. */
  8005. if (HAS_CUR_FBC(dev_priv) &&
  8006. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8007. if (height < 8 || height > width)
  8008. return false;
  8009. } else {
  8010. if (height != width)
  8011. return false;
  8012. }
  8013. return true;
  8014. }
  8015. static int i9xx_check_cursor(struct intel_plane *plane,
  8016. struct intel_crtc_state *crtc_state,
  8017. struct intel_plane_state *plane_state)
  8018. {
  8019. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8020. const struct drm_framebuffer *fb = plane_state->base.fb;
  8021. enum pipe pipe = plane->pipe;
  8022. int ret;
  8023. ret = intel_check_cursor(crtc_state, plane_state);
  8024. if (ret)
  8025. return ret;
  8026. /* if we want to turn off the cursor ignore width and height */
  8027. if (!fb)
  8028. return 0;
  8029. /* Check for which cursor types we support */
  8030. if (!i9xx_cursor_size_ok(plane_state)) {
  8031. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8032. plane_state->base.crtc_w,
  8033. plane_state->base.crtc_h);
  8034. return -EINVAL;
  8035. }
  8036. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8037. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8038. fb->pitches[0], plane_state->base.crtc_w);
  8039. return -EINVAL;
  8040. }
  8041. /*
  8042. * There's something wrong with the cursor on CHV pipe C.
  8043. * If it straddles the left edge of the screen then
  8044. * moving it away from the edge or disabling it often
  8045. * results in a pipe underrun, and often that can lead to
  8046. * dead pipe (constant underrun reported, and it scans
  8047. * out just a solid color). To recover from that, the
  8048. * display power well must be turned off and on again.
  8049. * Refuse the put the cursor into that compromised position.
  8050. */
  8051. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8052. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8053. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8054. return -EINVAL;
  8055. }
  8056. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8057. return 0;
  8058. }
  8059. static void i9xx_update_cursor(struct intel_plane *plane,
  8060. const struct intel_crtc_state *crtc_state,
  8061. const struct intel_plane_state *plane_state)
  8062. {
  8063. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8064. enum pipe pipe = plane->pipe;
  8065. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8066. unsigned long irqflags;
  8067. if (plane_state && plane_state->base.visible) {
  8068. cntl = plane_state->ctl;
  8069. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8070. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8071. base = intel_cursor_base(plane_state);
  8072. pos = intel_cursor_position(plane_state);
  8073. }
  8074. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8075. /*
  8076. * On some platforms writing CURCNTR first will also
  8077. * cause CURPOS to be armed by the CURBASE write.
  8078. * Without the CURCNTR write the CURPOS write would
  8079. * arm itself. Thus we always start the full update
  8080. * with a CURCNTR write.
  8081. *
  8082. * On other platforms CURPOS always requires the
  8083. * CURBASE write to arm the update. Additonally
  8084. * a write to any of the cursor register will cancel
  8085. * an already armed cursor update. Thus leaving out
  8086. * the CURBASE write after CURPOS could lead to a
  8087. * cursor that doesn't appear to move, or even change
  8088. * shape. Thus we always write CURBASE.
  8089. *
  8090. * CURCNTR and CUR_FBC_CTL are always
  8091. * armed by the CURBASE write only.
  8092. */
  8093. if (plane->cursor.base != base ||
  8094. plane->cursor.size != fbc_ctl ||
  8095. plane->cursor.cntl != cntl) {
  8096. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8097. if (HAS_CUR_FBC(dev_priv))
  8098. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8099. I915_WRITE_FW(CURPOS(pipe), pos);
  8100. I915_WRITE_FW(CURBASE(pipe), base);
  8101. plane->cursor.base = base;
  8102. plane->cursor.size = fbc_ctl;
  8103. plane->cursor.cntl = cntl;
  8104. } else {
  8105. I915_WRITE_FW(CURPOS(pipe), pos);
  8106. I915_WRITE_FW(CURBASE(pipe), base);
  8107. }
  8108. POSTING_READ_FW(CURBASE(pipe));
  8109. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8110. }
  8111. static void i9xx_disable_cursor(struct intel_plane *plane,
  8112. struct intel_crtc *crtc)
  8113. {
  8114. i9xx_update_cursor(plane, NULL, NULL);
  8115. }
  8116. /* VESA 640x480x72Hz mode to set on the pipe */
  8117. static const struct drm_display_mode load_detect_mode = {
  8118. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8119. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8120. };
  8121. struct drm_framebuffer *
  8122. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8123. struct drm_mode_fb_cmd2 *mode_cmd)
  8124. {
  8125. struct intel_framebuffer *intel_fb;
  8126. int ret;
  8127. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8128. if (!intel_fb)
  8129. return ERR_PTR(-ENOMEM);
  8130. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8131. if (ret)
  8132. goto err;
  8133. return &intel_fb->base;
  8134. err:
  8135. kfree(intel_fb);
  8136. return ERR_PTR(ret);
  8137. }
  8138. static u32
  8139. intel_framebuffer_pitch_for_width(int width, int bpp)
  8140. {
  8141. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8142. return ALIGN(pitch, 64);
  8143. }
  8144. static u32
  8145. intel_framebuffer_size_for_mode(const struct drm_display_mode *mode, int bpp)
  8146. {
  8147. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8148. return PAGE_ALIGN(pitch * mode->vdisplay);
  8149. }
  8150. static struct drm_framebuffer *
  8151. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8152. const struct drm_display_mode *mode,
  8153. int depth, int bpp)
  8154. {
  8155. struct drm_framebuffer *fb;
  8156. struct drm_i915_gem_object *obj;
  8157. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8158. obj = i915_gem_object_create(to_i915(dev),
  8159. intel_framebuffer_size_for_mode(mode, bpp));
  8160. if (IS_ERR(obj))
  8161. return ERR_CAST(obj);
  8162. mode_cmd.width = mode->hdisplay;
  8163. mode_cmd.height = mode->vdisplay;
  8164. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8165. bpp);
  8166. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8167. fb = intel_framebuffer_create(obj, &mode_cmd);
  8168. if (IS_ERR(fb))
  8169. i915_gem_object_put(obj);
  8170. return fb;
  8171. }
  8172. static struct drm_framebuffer *
  8173. mode_fits_in_fbdev(struct drm_device *dev,
  8174. const struct drm_display_mode *mode)
  8175. {
  8176. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8177. struct drm_i915_private *dev_priv = to_i915(dev);
  8178. struct drm_i915_gem_object *obj;
  8179. struct drm_framebuffer *fb;
  8180. if (!dev_priv->fbdev)
  8181. return NULL;
  8182. if (!dev_priv->fbdev->fb)
  8183. return NULL;
  8184. obj = dev_priv->fbdev->fb->obj;
  8185. BUG_ON(!obj);
  8186. fb = &dev_priv->fbdev->fb->base;
  8187. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8188. fb->format->cpp[0] * 8))
  8189. return NULL;
  8190. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8191. return NULL;
  8192. drm_framebuffer_get(fb);
  8193. return fb;
  8194. #else
  8195. return NULL;
  8196. #endif
  8197. }
  8198. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8199. struct drm_crtc *crtc,
  8200. const struct drm_display_mode *mode,
  8201. struct drm_framebuffer *fb,
  8202. int x, int y)
  8203. {
  8204. struct drm_plane_state *plane_state;
  8205. int hdisplay, vdisplay;
  8206. int ret;
  8207. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8208. if (IS_ERR(plane_state))
  8209. return PTR_ERR(plane_state);
  8210. if (mode)
  8211. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  8212. else
  8213. hdisplay = vdisplay = 0;
  8214. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8215. if (ret)
  8216. return ret;
  8217. drm_atomic_set_fb_for_plane(plane_state, fb);
  8218. plane_state->crtc_x = 0;
  8219. plane_state->crtc_y = 0;
  8220. plane_state->crtc_w = hdisplay;
  8221. plane_state->crtc_h = vdisplay;
  8222. plane_state->src_x = x << 16;
  8223. plane_state->src_y = y << 16;
  8224. plane_state->src_w = hdisplay << 16;
  8225. plane_state->src_h = vdisplay << 16;
  8226. return 0;
  8227. }
  8228. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8229. const struct drm_display_mode *mode,
  8230. struct intel_load_detect_pipe *old,
  8231. struct drm_modeset_acquire_ctx *ctx)
  8232. {
  8233. struct intel_crtc *intel_crtc;
  8234. struct intel_encoder *intel_encoder =
  8235. intel_attached_encoder(connector);
  8236. struct drm_crtc *possible_crtc;
  8237. struct drm_encoder *encoder = &intel_encoder->base;
  8238. struct drm_crtc *crtc = NULL;
  8239. struct drm_device *dev = encoder->dev;
  8240. struct drm_i915_private *dev_priv = to_i915(dev);
  8241. struct drm_framebuffer *fb;
  8242. struct drm_mode_config *config = &dev->mode_config;
  8243. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8244. struct drm_connector_state *connector_state;
  8245. struct intel_crtc_state *crtc_state;
  8246. int ret, i = -1;
  8247. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8248. connector->base.id, connector->name,
  8249. encoder->base.id, encoder->name);
  8250. old->restore_state = NULL;
  8251. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8252. /*
  8253. * Algorithm gets a little messy:
  8254. *
  8255. * - if the connector already has an assigned crtc, use it (but make
  8256. * sure it's on first)
  8257. *
  8258. * - try to find the first unused crtc that can drive this connector,
  8259. * and use that if we find one
  8260. */
  8261. /* See if we already have a CRTC for this connector */
  8262. if (connector->state->crtc) {
  8263. crtc = connector->state->crtc;
  8264. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8265. if (ret)
  8266. goto fail;
  8267. /* Make sure the crtc and connector are running */
  8268. goto found;
  8269. }
  8270. /* Find an unused one (if possible) */
  8271. for_each_crtc(dev, possible_crtc) {
  8272. i++;
  8273. if (!(encoder->possible_crtcs & (1 << i)))
  8274. continue;
  8275. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8276. if (ret)
  8277. goto fail;
  8278. if (possible_crtc->state->enable) {
  8279. drm_modeset_unlock(&possible_crtc->mutex);
  8280. continue;
  8281. }
  8282. crtc = possible_crtc;
  8283. break;
  8284. }
  8285. /*
  8286. * If we didn't find an unused CRTC, don't use any.
  8287. */
  8288. if (!crtc) {
  8289. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8290. ret = -ENODEV;
  8291. goto fail;
  8292. }
  8293. found:
  8294. intel_crtc = to_intel_crtc(crtc);
  8295. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8296. if (ret)
  8297. goto fail;
  8298. state = drm_atomic_state_alloc(dev);
  8299. restore_state = drm_atomic_state_alloc(dev);
  8300. if (!state || !restore_state) {
  8301. ret = -ENOMEM;
  8302. goto fail;
  8303. }
  8304. state->acquire_ctx = ctx;
  8305. restore_state->acquire_ctx = ctx;
  8306. connector_state = drm_atomic_get_connector_state(state, connector);
  8307. if (IS_ERR(connector_state)) {
  8308. ret = PTR_ERR(connector_state);
  8309. goto fail;
  8310. }
  8311. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8312. if (ret)
  8313. goto fail;
  8314. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8315. if (IS_ERR(crtc_state)) {
  8316. ret = PTR_ERR(crtc_state);
  8317. goto fail;
  8318. }
  8319. crtc_state->base.active = crtc_state->base.enable = true;
  8320. if (!mode)
  8321. mode = &load_detect_mode;
  8322. /* We need a framebuffer large enough to accommodate all accesses
  8323. * that the plane may generate whilst we perform load detection.
  8324. * We can not rely on the fbcon either being present (we get called
  8325. * during its initialisation to detect all boot displays, or it may
  8326. * not even exist) or that it is large enough to satisfy the
  8327. * requested mode.
  8328. */
  8329. fb = mode_fits_in_fbdev(dev, mode);
  8330. if (fb == NULL) {
  8331. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8332. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8333. } else
  8334. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8335. if (IS_ERR(fb)) {
  8336. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8337. ret = PTR_ERR(fb);
  8338. goto fail;
  8339. }
  8340. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8341. if (ret)
  8342. goto fail;
  8343. drm_framebuffer_put(fb);
  8344. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8345. if (ret)
  8346. goto fail;
  8347. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8348. if (!ret)
  8349. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8350. if (!ret)
  8351. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8352. if (ret) {
  8353. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8354. goto fail;
  8355. }
  8356. ret = drm_atomic_commit(state);
  8357. if (ret) {
  8358. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8359. goto fail;
  8360. }
  8361. old->restore_state = restore_state;
  8362. drm_atomic_state_put(state);
  8363. /* let the connector get through one full cycle before testing */
  8364. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8365. return true;
  8366. fail:
  8367. if (state) {
  8368. drm_atomic_state_put(state);
  8369. state = NULL;
  8370. }
  8371. if (restore_state) {
  8372. drm_atomic_state_put(restore_state);
  8373. restore_state = NULL;
  8374. }
  8375. if (ret == -EDEADLK)
  8376. return ret;
  8377. return false;
  8378. }
  8379. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8380. struct intel_load_detect_pipe *old,
  8381. struct drm_modeset_acquire_ctx *ctx)
  8382. {
  8383. struct intel_encoder *intel_encoder =
  8384. intel_attached_encoder(connector);
  8385. struct drm_encoder *encoder = &intel_encoder->base;
  8386. struct drm_atomic_state *state = old->restore_state;
  8387. int ret;
  8388. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8389. connector->base.id, connector->name,
  8390. encoder->base.id, encoder->name);
  8391. if (!state)
  8392. return;
  8393. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8394. if (ret)
  8395. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8396. drm_atomic_state_put(state);
  8397. }
  8398. static int i9xx_pll_refclk(struct drm_device *dev,
  8399. const struct intel_crtc_state *pipe_config)
  8400. {
  8401. struct drm_i915_private *dev_priv = to_i915(dev);
  8402. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8403. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8404. return dev_priv->vbt.lvds_ssc_freq;
  8405. else if (HAS_PCH_SPLIT(dev_priv))
  8406. return 120000;
  8407. else if (!IS_GEN2(dev_priv))
  8408. return 96000;
  8409. else
  8410. return 48000;
  8411. }
  8412. /* Returns the clock of the currently programmed mode of the given pipe. */
  8413. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8414. struct intel_crtc_state *pipe_config)
  8415. {
  8416. struct drm_device *dev = crtc->base.dev;
  8417. struct drm_i915_private *dev_priv = to_i915(dev);
  8418. int pipe = pipe_config->cpu_transcoder;
  8419. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8420. u32 fp;
  8421. struct dpll clock;
  8422. int port_clock;
  8423. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8424. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8425. fp = pipe_config->dpll_hw_state.fp0;
  8426. else
  8427. fp = pipe_config->dpll_hw_state.fp1;
  8428. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8429. if (IS_PINEVIEW(dev_priv)) {
  8430. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8431. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8432. } else {
  8433. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8434. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8435. }
  8436. if (!IS_GEN2(dev_priv)) {
  8437. if (IS_PINEVIEW(dev_priv))
  8438. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8439. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8440. else
  8441. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8442. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8443. switch (dpll & DPLL_MODE_MASK) {
  8444. case DPLLB_MODE_DAC_SERIAL:
  8445. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8446. 5 : 10;
  8447. break;
  8448. case DPLLB_MODE_LVDS:
  8449. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8450. 7 : 14;
  8451. break;
  8452. default:
  8453. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8454. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8455. return;
  8456. }
  8457. if (IS_PINEVIEW(dev_priv))
  8458. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8459. else
  8460. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8461. } else {
  8462. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8463. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8464. if (is_lvds) {
  8465. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8466. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8467. if (lvds & LVDS_CLKB_POWER_UP)
  8468. clock.p2 = 7;
  8469. else
  8470. clock.p2 = 14;
  8471. } else {
  8472. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8473. clock.p1 = 2;
  8474. else {
  8475. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8476. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8477. }
  8478. if (dpll & PLL_P2_DIVIDE_BY_4)
  8479. clock.p2 = 4;
  8480. else
  8481. clock.p2 = 2;
  8482. }
  8483. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8484. }
  8485. /*
  8486. * This value includes pixel_multiplier. We will use
  8487. * port_clock to compute adjusted_mode.crtc_clock in the
  8488. * encoder's get_config() function.
  8489. */
  8490. pipe_config->port_clock = port_clock;
  8491. }
  8492. int intel_dotclock_calculate(int link_freq,
  8493. const struct intel_link_m_n *m_n)
  8494. {
  8495. /*
  8496. * The calculation for the data clock is:
  8497. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8498. * But we want to avoid losing precison if possible, so:
  8499. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8500. *
  8501. * and the link clock is simpler:
  8502. * link_clock = (m * link_clock) / n
  8503. */
  8504. if (!m_n->link_n)
  8505. return 0;
  8506. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8507. }
  8508. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8509. struct intel_crtc_state *pipe_config)
  8510. {
  8511. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8512. /* read out port_clock from the DPLL */
  8513. i9xx_crtc_clock_get(crtc, pipe_config);
  8514. /*
  8515. * In case there is an active pipe without active ports,
  8516. * we may need some idea for the dotclock anyway.
  8517. * Calculate one based on the FDI configuration.
  8518. */
  8519. pipe_config->base.adjusted_mode.crtc_clock =
  8520. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8521. &pipe_config->fdi_m_n);
  8522. }
  8523. /* Returns the currently programmed mode of the given encoder. */
  8524. struct drm_display_mode *
  8525. intel_encoder_current_mode(struct intel_encoder *encoder)
  8526. {
  8527. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8528. struct intel_crtc_state *crtc_state;
  8529. struct drm_display_mode *mode;
  8530. struct intel_crtc *crtc;
  8531. enum pipe pipe;
  8532. if (!encoder->get_hw_state(encoder, &pipe))
  8533. return NULL;
  8534. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8535. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8536. if (!mode)
  8537. return NULL;
  8538. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8539. if (!crtc_state) {
  8540. kfree(mode);
  8541. return NULL;
  8542. }
  8543. crtc_state->base.crtc = &crtc->base;
  8544. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8545. kfree(crtc_state);
  8546. kfree(mode);
  8547. return NULL;
  8548. }
  8549. encoder->get_config(encoder, crtc_state);
  8550. intel_mode_from_pipe_config(mode, crtc_state);
  8551. kfree(crtc_state);
  8552. return mode;
  8553. }
  8554. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8555. {
  8556. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8557. drm_crtc_cleanup(crtc);
  8558. kfree(intel_crtc);
  8559. }
  8560. /**
  8561. * intel_wm_need_update - Check whether watermarks need updating
  8562. * @plane: drm plane
  8563. * @state: new plane state
  8564. *
  8565. * Check current plane state versus the new one to determine whether
  8566. * watermarks need to be recalculated.
  8567. *
  8568. * Returns true or false.
  8569. */
  8570. static bool intel_wm_need_update(struct drm_plane *plane,
  8571. struct drm_plane_state *state)
  8572. {
  8573. struct intel_plane_state *new = to_intel_plane_state(state);
  8574. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8575. /* Update watermarks on tiling or size changes. */
  8576. if (new->base.visible != cur->base.visible)
  8577. return true;
  8578. if (!cur->base.fb || !new->base.fb)
  8579. return false;
  8580. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8581. cur->base.rotation != new->base.rotation ||
  8582. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8583. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8584. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8585. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8586. return true;
  8587. return false;
  8588. }
  8589. static bool needs_scaling(const struct intel_plane_state *state)
  8590. {
  8591. int src_w = drm_rect_width(&state->base.src) >> 16;
  8592. int src_h = drm_rect_height(&state->base.src) >> 16;
  8593. int dst_w = drm_rect_width(&state->base.dst);
  8594. int dst_h = drm_rect_height(&state->base.dst);
  8595. return (src_w != dst_w || src_h != dst_h);
  8596. }
  8597. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8598. struct drm_crtc_state *crtc_state,
  8599. const struct intel_plane_state *old_plane_state,
  8600. struct drm_plane_state *plane_state)
  8601. {
  8602. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8603. struct drm_crtc *crtc = crtc_state->crtc;
  8604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8605. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8606. struct drm_device *dev = crtc->dev;
  8607. struct drm_i915_private *dev_priv = to_i915(dev);
  8608. bool mode_changed = needs_modeset(crtc_state);
  8609. bool was_crtc_enabled = old_crtc_state->base.active;
  8610. bool is_crtc_enabled = crtc_state->active;
  8611. bool turn_off, turn_on, visible, was_visible;
  8612. struct drm_framebuffer *fb = plane_state->fb;
  8613. int ret;
  8614. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8615. ret = skl_update_scaler_plane(
  8616. to_intel_crtc_state(crtc_state),
  8617. to_intel_plane_state(plane_state));
  8618. if (ret)
  8619. return ret;
  8620. }
  8621. was_visible = old_plane_state->base.visible;
  8622. visible = plane_state->visible;
  8623. if (!was_crtc_enabled && WARN_ON(was_visible))
  8624. was_visible = false;
  8625. /*
  8626. * Visibility is calculated as if the crtc was on, but
  8627. * after scaler setup everything depends on it being off
  8628. * when the crtc isn't active.
  8629. *
  8630. * FIXME this is wrong for watermarks. Watermarks should also
  8631. * be computed as if the pipe would be active. Perhaps move
  8632. * per-plane wm computation to the .check_plane() hook, and
  8633. * only combine the results from all planes in the current place?
  8634. */
  8635. if (!is_crtc_enabled) {
  8636. plane_state->visible = visible = false;
  8637. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8638. }
  8639. if (!was_visible && !visible)
  8640. return 0;
  8641. if (fb != old_plane_state->base.fb)
  8642. pipe_config->fb_changed = true;
  8643. turn_off = was_visible && (!visible || mode_changed);
  8644. turn_on = visible && (!was_visible || mode_changed);
  8645. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8646. intel_crtc->base.base.id, intel_crtc->base.name,
  8647. plane->base.base.id, plane->base.name,
  8648. fb ? fb->base.id : -1);
  8649. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8650. plane->base.base.id, plane->base.name,
  8651. was_visible, visible,
  8652. turn_off, turn_on, mode_changed);
  8653. if (turn_on) {
  8654. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8655. pipe_config->update_wm_pre = true;
  8656. /* must disable cxsr around plane enable/disable */
  8657. if (plane->id != PLANE_CURSOR)
  8658. pipe_config->disable_cxsr = true;
  8659. } else if (turn_off) {
  8660. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8661. pipe_config->update_wm_post = true;
  8662. /* must disable cxsr around plane enable/disable */
  8663. if (plane->id != PLANE_CURSOR)
  8664. pipe_config->disable_cxsr = true;
  8665. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8666. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8667. /* FIXME bollocks */
  8668. pipe_config->update_wm_pre = true;
  8669. pipe_config->update_wm_post = true;
  8670. }
  8671. }
  8672. if (visible || was_visible)
  8673. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8674. /*
  8675. * WaCxSRDisabledForSpriteScaling:ivb
  8676. *
  8677. * cstate->update_wm was already set above, so this flag will
  8678. * take effect when we commit and program watermarks.
  8679. */
  8680. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8681. needs_scaling(to_intel_plane_state(plane_state)) &&
  8682. !needs_scaling(old_plane_state))
  8683. pipe_config->disable_lp_wm = true;
  8684. return 0;
  8685. }
  8686. static bool encoders_cloneable(const struct intel_encoder *a,
  8687. const struct intel_encoder *b)
  8688. {
  8689. /* masks could be asymmetric, so check both ways */
  8690. return a == b || (a->cloneable & (1 << b->type) &&
  8691. b->cloneable & (1 << a->type));
  8692. }
  8693. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8694. struct intel_crtc *crtc,
  8695. struct intel_encoder *encoder)
  8696. {
  8697. struct intel_encoder *source_encoder;
  8698. struct drm_connector *connector;
  8699. struct drm_connector_state *connector_state;
  8700. int i;
  8701. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8702. if (connector_state->crtc != &crtc->base)
  8703. continue;
  8704. source_encoder =
  8705. to_intel_encoder(connector_state->best_encoder);
  8706. if (!encoders_cloneable(encoder, source_encoder))
  8707. return false;
  8708. }
  8709. return true;
  8710. }
  8711. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8712. struct drm_crtc_state *crtc_state)
  8713. {
  8714. struct drm_device *dev = crtc->dev;
  8715. struct drm_i915_private *dev_priv = to_i915(dev);
  8716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8717. struct intel_crtc_state *pipe_config =
  8718. to_intel_crtc_state(crtc_state);
  8719. struct drm_atomic_state *state = crtc_state->state;
  8720. int ret;
  8721. bool mode_changed = needs_modeset(crtc_state);
  8722. if (mode_changed && !crtc_state->active)
  8723. pipe_config->update_wm_post = true;
  8724. if (mode_changed && crtc_state->enable &&
  8725. dev_priv->display.crtc_compute_clock &&
  8726. !WARN_ON(pipe_config->shared_dpll)) {
  8727. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8728. pipe_config);
  8729. if (ret)
  8730. return ret;
  8731. }
  8732. if (crtc_state->color_mgmt_changed) {
  8733. ret = intel_color_check(crtc, crtc_state);
  8734. if (ret)
  8735. return ret;
  8736. /*
  8737. * Changing color management on Intel hardware is
  8738. * handled as part of planes update.
  8739. */
  8740. crtc_state->planes_changed = true;
  8741. }
  8742. ret = 0;
  8743. if (dev_priv->display.compute_pipe_wm) {
  8744. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8745. if (ret) {
  8746. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8747. return ret;
  8748. }
  8749. }
  8750. if (dev_priv->display.compute_intermediate_wm &&
  8751. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8752. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8753. return 0;
  8754. /*
  8755. * Calculate 'intermediate' watermarks that satisfy both the
  8756. * old state and the new state. We can program these
  8757. * immediately.
  8758. */
  8759. ret = dev_priv->display.compute_intermediate_wm(dev,
  8760. intel_crtc,
  8761. pipe_config);
  8762. if (ret) {
  8763. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8764. return ret;
  8765. }
  8766. } else if (dev_priv->display.compute_intermediate_wm) {
  8767. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8768. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8769. }
  8770. if (INTEL_GEN(dev_priv) >= 9) {
  8771. if (mode_changed)
  8772. ret = skl_update_scaler_crtc(pipe_config);
  8773. if (!ret)
  8774. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8775. pipe_config);
  8776. if (!ret)
  8777. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8778. pipe_config);
  8779. }
  8780. return ret;
  8781. }
  8782. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8783. .atomic_begin = intel_begin_crtc_commit,
  8784. .atomic_flush = intel_finish_crtc_commit,
  8785. .atomic_check = intel_crtc_atomic_check,
  8786. };
  8787. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8788. {
  8789. struct intel_connector *connector;
  8790. struct drm_connector_list_iter conn_iter;
  8791. drm_connector_list_iter_begin(dev, &conn_iter);
  8792. for_each_intel_connector_iter(connector, &conn_iter) {
  8793. if (connector->base.state->crtc)
  8794. drm_connector_unreference(&connector->base);
  8795. if (connector->base.encoder) {
  8796. connector->base.state->best_encoder =
  8797. connector->base.encoder;
  8798. connector->base.state->crtc =
  8799. connector->base.encoder->crtc;
  8800. drm_connector_reference(&connector->base);
  8801. } else {
  8802. connector->base.state->best_encoder = NULL;
  8803. connector->base.state->crtc = NULL;
  8804. }
  8805. }
  8806. drm_connector_list_iter_end(&conn_iter);
  8807. }
  8808. static void
  8809. connected_sink_compute_bpp(struct intel_connector *connector,
  8810. struct intel_crtc_state *pipe_config)
  8811. {
  8812. const struct drm_display_info *info = &connector->base.display_info;
  8813. int bpp = pipe_config->pipe_bpp;
  8814. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8815. connector->base.base.id,
  8816. connector->base.name);
  8817. /* Don't use an invalid EDID bpc value */
  8818. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8819. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8820. bpp, info->bpc * 3);
  8821. pipe_config->pipe_bpp = info->bpc * 3;
  8822. }
  8823. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8824. if (info->bpc == 0 && bpp > 24) {
  8825. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8826. bpp);
  8827. pipe_config->pipe_bpp = 24;
  8828. }
  8829. }
  8830. static int
  8831. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8832. struct intel_crtc_state *pipe_config)
  8833. {
  8834. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8835. struct drm_atomic_state *state;
  8836. struct drm_connector *connector;
  8837. struct drm_connector_state *connector_state;
  8838. int bpp, i;
  8839. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8840. IS_CHERRYVIEW(dev_priv)))
  8841. bpp = 10*3;
  8842. else if (INTEL_GEN(dev_priv) >= 5)
  8843. bpp = 12*3;
  8844. else
  8845. bpp = 8*3;
  8846. pipe_config->pipe_bpp = bpp;
  8847. state = pipe_config->base.state;
  8848. /* Clamp display bpp to EDID value */
  8849. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8850. if (connector_state->crtc != &crtc->base)
  8851. continue;
  8852. connected_sink_compute_bpp(to_intel_connector(connector),
  8853. pipe_config);
  8854. }
  8855. return bpp;
  8856. }
  8857. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8858. {
  8859. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8860. "type: 0x%x flags: 0x%x\n",
  8861. mode->crtc_clock,
  8862. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8863. mode->crtc_hsync_end, mode->crtc_htotal,
  8864. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8865. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8866. }
  8867. static inline void
  8868. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8869. unsigned int lane_count, struct intel_link_m_n *m_n)
  8870. {
  8871. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8872. id, lane_count,
  8873. m_n->gmch_m, m_n->gmch_n,
  8874. m_n->link_m, m_n->link_n, m_n->tu);
  8875. }
  8876. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8877. static const char * const output_type_str[] = {
  8878. OUTPUT_TYPE(UNUSED),
  8879. OUTPUT_TYPE(ANALOG),
  8880. OUTPUT_TYPE(DVO),
  8881. OUTPUT_TYPE(SDVO),
  8882. OUTPUT_TYPE(LVDS),
  8883. OUTPUT_TYPE(TVOUT),
  8884. OUTPUT_TYPE(HDMI),
  8885. OUTPUT_TYPE(DP),
  8886. OUTPUT_TYPE(EDP),
  8887. OUTPUT_TYPE(DSI),
  8888. OUTPUT_TYPE(DDI),
  8889. OUTPUT_TYPE(DP_MST),
  8890. };
  8891. #undef OUTPUT_TYPE
  8892. static void snprintf_output_types(char *buf, size_t len,
  8893. unsigned int output_types)
  8894. {
  8895. char *str = buf;
  8896. int i;
  8897. str[0] = '\0';
  8898. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8899. int r;
  8900. if ((output_types & BIT(i)) == 0)
  8901. continue;
  8902. r = snprintf(str, len, "%s%s",
  8903. str != buf ? "," : "", output_type_str[i]);
  8904. if (r >= len)
  8905. break;
  8906. str += r;
  8907. len -= r;
  8908. output_types &= ~BIT(i);
  8909. }
  8910. WARN_ON_ONCE(output_types != 0);
  8911. }
  8912. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8913. struct intel_crtc_state *pipe_config,
  8914. const char *context)
  8915. {
  8916. struct drm_device *dev = crtc->base.dev;
  8917. struct drm_i915_private *dev_priv = to_i915(dev);
  8918. struct drm_plane *plane;
  8919. struct intel_plane *intel_plane;
  8920. struct intel_plane_state *state;
  8921. struct drm_framebuffer *fb;
  8922. char buf[64];
  8923. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8924. crtc->base.base.id, crtc->base.name, context);
  8925. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  8926. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  8927. buf, pipe_config->output_types);
  8928. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8929. transcoder_name(pipe_config->cpu_transcoder),
  8930. pipe_config->pipe_bpp, pipe_config->dither);
  8931. if (pipe_config->has_pch_encoder)
  8932. intel_dump_m_n_config(pipe_config, "fdi",
  8933. pipe_config->fdi_lanes,
  8934. &pipe_config->fdi_m_n);
  8935. if (pipe_config->ycbcr420)
  8936. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8937. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8938. intel_dump_m_n_config(pipe_config, "dp m_n",
  8939. pipe_config->lane_count, &pipe_config->dp_m_n);
  8940. if (pipe_config->has_drrs)
  8941. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8942. pipe_config->lane_count,
  8943. &pipe_config->dp_m2_n2);
  8944. }
  8945. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8946. pipe_config->has_audio, pipe_config->has_infoframe);
  8947. DRM_DEBUG_KMS("requested mode:\n");
  8948. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8949. DRM_DEBUG_KMS("adjusted mode:\n");
  8950. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8951. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8952. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8953. pipe_config->port_clock,
  8954. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8955. pipe_config->pixel_rate);
  8956. if (INTEL_GEN(dev_priv) >= 9)
  8957. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8958. crtc->num_scalers,
  8959. pipe_config->scaler_state.scaler_users,
  8960. pipe_config->scaler_state.scaler_id);
  8961. if (HAS_GMCH_DISPLAY(dev_priv))
  8962. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8963. pipe_config->gmch_pfit.control,
  8964. pipe_config->gmch_pfit.pgm_ratios,
  8965. pipe_config->gmch_pfit.lvds_border_bits);
  8966. else
  8967. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8968. pipe_config->pch_pfit.pos,
  8969. pipe_config->pch_pfit.size,
  8970. enableddisabled(pipe_config->pch_pfit.enabled));
  8971. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8972. pipe_config->ips_enabled, pipe_config->double_wide);
  8973. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8974. DRM_DEBUG_KMS("planes on this crtc\n");
  8975. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8976. struct drm_format_name_buf format_name;
  8977. intel_plane = to_intel_plane(plane);
  8978. if (intel_plane->pipe != crtc->pipe)
  8979. continue;
  8980. state = to_intel_plane_state(plane->state);
  8981. fb = state->base.fb;
  8982. if (!fb) {
  8983. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  8984. plane->base.id, plane->name, state->scaler_id);
  8985. continue;
  8986. }
  8987. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  8988. plane->base.id, plane->name,
  8989. fb->base.id, fb->width, fb->height,
  8990. drm_get_format_name(fb->format->format, &format_name));
  8991. if (INTEL_GEN(dev_priv) >= 9)
  8992. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  8993. state->scaler_id,
  8994. state->base.src.x1 >> 16,
  8995. state->base.src.y1 >> 16,
  8996. drm_rect_width(&state->base.src) >> 16,
  8997. drm_rect_height(&state->base.src) >> 16,
  8998. state->base.dst.x1, state->base.dst.y1,
  8999. drm_rect_width(&state->base.dst),
  9000. drm_rect_height(&state->base.dst));
  9001. }
  9002. }
  9003. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9004. {
  9005. struct drm_device *dev = state->dev;
  9006. struct drm_connector *connector;
  9007. struct drm_connector_list_iter conn_iter;
  9008. unsigned int used_ports = 0;
  9009. unsigned int used_mst_ports = 0;
  9010. /*
  9011. * Walk the connector list instead of the encoder
  9012. * list to detect the problem on ddi platforms
  9013. * where there's just one encoder per digital port.
  9014. */
  9015. drm_connector_list_iter_begin(dev, &conn_iter);
  9016. drm_for_each_connector_iter(connector, &conn_iter) {
  9017. struct drm_connector_state *connector_state;
  9018. struct intel_encoder *encoder;
  9019. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9020. if (!connector_state)
  9021. connector_state = connector->state;
  9022. if (!connector_state->best_encoder)
  9023. continue;
  9024. encoder = to_intel_encoder(connector_state->best_encoder);
  9025. WARN_ON(!connector_state->crtc);
  9026. switch (encoder->type) {
  9027. unsigned int port_mask;
  9028. case INTEL_OUTPUT_DDI:
  9029. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9030. break;
  9031. case INTEL_OUTPUT_DP:
  9032. case INTEL_OUTPUT_HDMI:
  9033. case INTEL_OUTPUT_EDP:
  9034. port_mask = 1 << encoder->port;
  9035. /* the same port mustn't appear more than once */
  9036. if (used_ports & port_mask)
  9037. return false;
  9038. used_ports |= port_mask;
  9039. break;
  9040. case INTEL_OUTPUT_DP_MST:
  9041. used_mst_ports |=
  9042. 1 << encoder->port;
  9043. break;
  9044. default:
  9045. break;
  9046. }
  9047. }
  9048. drm_connector_list_iter_end(&conn_iter);
  9049. /* can't mix MST and SST/HDMI on the same port */
  9050. if (used_ports & used_mst_ports)
  9051. return false;
  9052. return true;
  9053. }
  9054. static void
  9055. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9056. {
  9057. struct drm_i915_private *dev_priv =
  9058. to_i915(crtc_state->base.crtc->dev);
  9059. struct intel_crtc_scaler_state scaler_state;
  9060. struct intel_dpll_hw_state dpll_hw_state;
  9061. struct intel_shared_dpll *shared_dpll;
  9062. struct intel_crtc_wm_state wm_state;
  9063. bool force_thru, ips_force_disable;
  9064. /* FIXME: before the switch to atomic started, a new pipe_config was
  9065. * kzalloc'd. Code that depends on any field being zero should be
  9066. * fixed, so that the crtc_state can be safely duplicated. For now,
  9067. * only fields that are know to not cause problems are preserved. */
  9068. scaler_state = crtc_state->scaler_state;
  9069. shared_dpll = crtc_state->shared_dpll;
  9070. dpll_hw_state = crtc_state->dpll_hw_state;
  9071. force_thru = crtc_state->pch_pfit.force_thru;
  9072. ips_force_disable = crtc_state->ips_force_disable;
  9073. if (IS_G4X(dev_priv) ||
  9074. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9075. wm_state = crtc_state->wm;
  9076. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9077. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9078. memset(&crtc_state->base + 1, 0,
  9079. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9080. crtc_state->scaler_state = scaler_state;
  9081. crtc_state->shared_dpll = shared_dpll;
  9082. crtc_state->dpll_hw_state = dpll_hw_state;
  9083. crtc_state->pch_pfit.force_thru = force_thru;
  9084. crtc_state->ips_force_disable = ips_force_disable;
  9085. if (IS_G4X(dev_priv) ||
  9086. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9087. crtc_state->wm = wm_state;
  9088. }
  9089. static int
  9090. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9091. struct intel_crtc_state *pipe_config)
  9092. {
  9093. struct drm_atomic_state *state = pipe_config->base.state;
  9094. struct intel_encoder *encoder;
  9095. struct drm_connector *connector;
  9096. struct drm_connector_state *connector_state;
  9097. int base_bpp, ret = -EINVAL;
  9098. int i;
  9099. bool retry = true;
  9100. clear_intel_crtc_state(pipe_config);
  9101. pipe_config->cpu_transcoder =
  9102. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9103. /*
  9104. * Sanitize sync polarity flags based on requested ones. If neither
  9105. * positive or negative polarity is requested, treat this as meaning
  9106. * negative polarity.
  9107. */
  9108. if (!(pipe_config->base.adjusted_mode.flags &
  9109. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9110. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9111. if (!(pipe_config->base.adjusted_mode.flags &
  9112. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9113. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9114. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9115. pipe_config);
  9116. if (base_bpp < 0)
  9117. goto fail;
  9118. /*
  9119. * Determine the real pipe dimensions. Note that stereo modes can
  9120. * increase the actual pipe size due to the frame doubling and
  9121. * insertion of additional space for blanks between the frame. This
  9122. * is stored in the crtc timings. We use the requested mode to do this
  9123. * computation to clearly distinguish it from the adjusted mode, which
  9124. * can be changed by the connectors in the below retry loop.
  9125. */
  9126. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9127. &pipe_config->pipe_src_w,
  9128. &pipe_config->pipe_src_h);
  9129. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9130. if (connector_state->crtc != crtc)
  9131. continue;
  9132. encoder = to_intel_encoder(connector_state->best_encoder);
  9133. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9134. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9135. goto fail;
  9136. }
  9137. /*
  9138. * Determine output_types before calling the .compute_config()
  9139. * hooks so that the hooks can use this information safely.
  9140. */
  9141. if (encoder->compute_output_type)
  9142. pipe_config->output_types |=
  9143. BIT(encoder->compute_output_type(encoder, pipe_config,
  9144. connector_state));
  9145. else
  9146. pipe_config->output_types |= BIT(encoder->type);
  9147. }
  9148. encoder_retry:
  9149. /* Ensure the port clock defaults are reset when retrying. */
  9150. pipe_config->port_clock = 0;
  9151. pipe_config->pixel_multiplier = 1;
  9152. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9153. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9154. CRTC_STEREO_DOUBLE);
  9155. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9156. * adjust it according to limitations or connector properties, and also
  9157. * a chance to reject the mode entirely.
  9158. */
  9159. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9160. if (connector_state->crtc != crtc)
  9161. continue;
  9162. encoder = to_intel_encoder(connector_state->best_encoder);
  9163. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9164. DRM_DEBUG_KMS("Encoder config failure\n");
  9165. goto fail;
  9166. }
  9167. }
  9168. /* Set default port clock if not overwritten by the encoder. Needs to be
  9169. * done afterwards in case the encoder adjusts the mode. */
  9170. if (!pipe_config->port_clock)
  9171. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9172. * pipe_config->pixel_multiplier;
  9173. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9174. if (ret < 0) {
  9175. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9176. goto fail;
  9177. }
  9178. if (ret == RETRY) {
  9179. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9180. ret = -EINVAL;
  9181. goto fail;
  9182. }
  9183. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9184. retry = false;
  9185. goto encoder_retry;
  9186. }
  9187. /* Dithering seems to not pass-through bits correctly when it should, so
  9188. * only enable it on 6bpc panels and when its not a compliance
  9189. * test requesting 6bpc video pattern.
  9190. */
  9191. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9192. !pipe_config->dither_force_disable;
  9193. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9194. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9195. fail:
  9196. return ret;
  9197. }
  9198. static void
  9199. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9200. {
  9201. struct drm_crtc *crtc;
  9202. struct drm_crtc_state *new_crtc_state;
  9203. int i;
  9204. /* Double check state. */
  9205. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9206. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9207. /*
  9208. * Update legacy state to satisfy fbc code. This can
  9209. * be removed when fbc uses the atomic state.
  9210. */
  9211. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9212. struct drm_plane_state *plane_state = crtc->primary->state;
  9213. crtc->primary->fb = plane_state->fb;
  9214. crtc->x = plane_state->src_x >> 16;
  9215. crtc->y = plane_state->src_y >> 16;
  9216. }
  9217. }
  9218. }
  9219. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9220. {
  9221. int diff;
  9222. if (clock1 == clock2)
  9223. return true;
  9224. if (!clock1 || !clock2)
  9225. return false;
  9226. diff = abs(clock1 - clock2);
  9227. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9228. return true;
  9229. return false;
  9230. }
  9231. static bool
  9232. intel_compare_m_n(unsigned int m, unsigned int n,
  9233. unsigned int m2, unsigned int n2,
  9234. bool exact)
  9235. {
  9236. if (m == m2 && n == n2)
  9237. return true;
  9238. if (exact || !m || !n || !m2 || !n2)
  9239. return false;
  9240. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9241. if (n > n2) {
  9242. while (n > n2) {
  9243. m2 <<= 1;
  9244. n2 <<= 1;
  9245. }
  9246. } else if (n < n2) {
  9247. while (n < n2) {
  9248. m <<= 1;
  9249. n <<= 1;
  9250. }
  9251. }
  9252. if (n != n2)
  9253. return false;
  9254. return intel_fuzzy_clock_check(m, m2);
  9255. }
  9256. static bool
  9257. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9258. struct intel_link_m_n *m2_n2,
  9259. bool adjust)
  9260. {
  9261. if (m_n->tu == m2_n2->tu &&
  9262. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9263. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9264. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9265. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9266. if (adjust)
  9267. *m2_n2 = *m_n;
  9268. return true;
  9269. }
  9270. return false;
  9271. }
  9272. static void __printf(3, 4)
  9273. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9274. {
  9275. char *level;
  9276. unsigned int category;
  9277. struct va_format vaf;
  9278. va_list args;
  9279. if (adjust) {
  9280. level = KERN_DEBUG;
  9281. category = DRM_UT_KMS;
  9282. } else {
  9283. level = KERN_ERR;
  9284. category = DRM_UT_NONE;
  9285. }
  9286. va_start(args, format);
  9287. vaf.fmt = format;
  9288. vaf.va = &args;
  9289. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9290. va_end(args);
  9291. }
  9292. static bool
  9293. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9294. struct intel_crtc_state *current_config,
  9295. struct intel_crtc_state *pipe_config,
  9296. bool adjust)
  9297. {
  9298. bool ret = true;
  9299. bool fixup_inherited = adjust &&
  9300. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9301. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9302. #define PIPE_CONF_CHECK_X(name) \
  9303. if (current_config->name != pipe_config->name) { \
  9304. pipe_config_err(adjust, __stringify(name), \
  9305. "(expected 0x%08x, found 0x%08x)\n", \
  9306. current_config->name, \
  9307. pipe_config->name); \
  9308. ret = false; \
  9309. }
  9310. #define PIPE_CONF_CHECK_I(name) \
  9311. if (current_config->name != pipe_config->name) { \
  9312. pipe_config_err(adjust, __stringify(name), \
  9313. "(expected %i, found %i)\n", \
  9314. current_config->name, \
  9315. pipe_config->name); \
  9316. ret = false; \
  9317. }
  9318. #define PIPE_CONF_CHECK_BOOL(name) \
  9319. if (current_config->name != pipe_config->name) { \
  9320. pipe_config_err(adjust, __stringify(name), \
  9321. "(expected %s, found %s)\n", \
  9322. yesno(current_config->name), \
  9323. yesno(pipe_config->name)); \
  9324. ret = false; \
  9325. }
  9326. /*
  9327. * Checks state where we only read out the enabling, but not the entire
  9328. * state itself (like full infoframes or ELD for audio). These states
  9329. * require a full modeset on bootup to fix up.
  9330. */
  9331. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) \
  9332. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9333. PIPE_CONF_CHECK_BOOL(name); \
  9334. } else { \
  9335. pipe_config_err(adjust, __stringify(name), \
  9336. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9337. yesno(current_config->name), \
  9338. yesno(pipe_config->name)); \
  9339. ret = false; \
  9340. }
  9341. #define PIPE_CONF_CHECK_P(name) \
  9342. if (current_config->name != pipe_config->name) { \
  9343. pipe_config_err(adjust, __stringify(name), \
  9344. "(expected %p, found %p)\n", \
  9345. current_config->name, \
  9346. pipe_config->name); \
  9347. ret = false; \
  9348. }
  9349. #define PIPE_CONF_CHECK_M_N(name) \
  9350. if (!intel_compare_link_m_n(&current_config->name, \
  9351. &pipe_config->name,\
  9352. adjust)) { \
  9353. pipe_config_err(adjust, __stringify(name), \
  9354. "(expected tu %i gmch %i/%i link %i/%i, " \
  9355. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9356. current_config->name.tu, \
  9357. current_config->name.gmch_m, \
  9358. current_config->name.gmch_n, \
  9359. current_config->name.link_m, \
  9360. current_config->name.link_n, \
  9361. pipe_config->name.tu, \
  9362. pipe_config->name.gmch_m, \
  9363. pipe_config->name.gmch_n, \
  9364. pipe_config->name.link_m, \
  9365. pipe_config->name.link_n); \
  9366. ret = false; \
  9367. }
  9368. /* This is required for BDW+ where there is only one set of registers for
  9369. * switching between high and low RR.
  9370. * This macro can be used whenever a comparison has to be made between one
  9371. * hw state and multiple sw state variables.
  9372. */
  9373. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9374. if (!intel_compare_link_m_n(&current_config->name, \
  9375. &pipe_config->name, adjust) && \
  9376. !intel_compare_link_m_n(&current_config->alt_name, \
  9377. &pipe_config->name, adjust)) { \
  9378. pipe_config_err(adjust, __stringify(name), \
  9379. "(expected tu %i gmch %i/%i link %i/%i, " \
  9380. "or tu %i gmch %i/%i link %i/%i, " \
  9381. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9382. current_config->name.tu, \
  9383. current_config->name.gmch_m, \
  9384. current_config->name.gmch_n, \
  9385. current_config->name.link_m, \
  9386. current_config->name.link_n, \
  9387. current_config->alt_name.tu, \
  9388. current_config->alt_name.gmch_m, \
  9389. current_config->alt_name.gmch_n, \
  9390. current_config->alt_name.link_m, \
  9391. current_config->alt_name.link_n, \
  9392. pipe_config->name.tu, \
  9393. pipe_config->name.gmch_m, \
  9394. pipe_config->name.gmch_n, \
  9395. pipe_config->name.link_m, \
  9396. pipe_config->name.link_n); \
  9397. ret = false; \
  9398. }
  9399. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9400. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9401. pipe_config_err(adjust, __stringify(name), \
  9402. "(%x) (expected %i, found %i)\n", \
  9403. (mask), \
  9404. current_config->name & (mask), \
  9405. pipe_config->name & (mask)); \
  9406. ret = false; \
  9407. }
  9408. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9409. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9410. pipe_config_err(adjust, __stringify(name), \
  9411. "(expected %i, found %i)\n", \
  9412. current_config->name, \
  9413. pipe_config->name); \
  9414. ret = false; \
  9415. }
  9416. #define PIPE_CONF_QUIRK(quirk) \
  9417. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9418. PIPE_CONF_CHECK_I(cpu_transcoder);
  9419. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9420. PIPE_CONF_CHECK_I(fdi_lanes);
  9421. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9422. PIPE_CONF_CHECK_I(lane_count);
  9423. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9424. if (INTEL_GEN(dev_priv) < 8) {
  9425. PIPE_CONF_CHECK_M_N(dp_m_n);
  9426. if (current_config->has_drrs)
  9427. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9428. } else
  9429. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9430. PIPE_CONF_CHECK_X(output_types);
  9431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9432. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9433. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9434. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9435. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9436. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9437. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9438. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9439. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9440. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9441. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9442. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9443. PIPE_CONF_CHECK_I(pixel_multiplier);
  9444. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9445. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9446. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9447. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9448. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9449. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9450. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9451. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9452. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9453. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9454. DRM_MODE_FLAG_INTERLACE);
  9455. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9456. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9457. DRM_MODE_FLAG_PHSYNC);
  9458. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9459. DRM_MODE_FLAG_NHSYNC);
  9460. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9461. DRM_MODE_FLAG_PVSYNC);
  9462. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9463. DRM_MODE_FLAG_NVSYNC);
  9464. }
  9465. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9466. /* pfit ratios are autocomputed by the hw on gen4+ */
  9467. if (INTEL_GEN(dev_priv) < 4)
  9468. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9469. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9470. if (!adjust) {
  9471. PIPE_CONF_CHECK_I(pipe_src_w);
  9472. PIPE_CONF_CHECK_I(pipe_src_h);
  9473. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9474. if (current_config->pch_pfit.enabled) {
  9475. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9476. PIPE_CONF_CHECK_X(pch_pfit.size);
  9477. }
  9478. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9479. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9480. }
  9481. PIPE_CONF_CHECK_BOOL(double_wide);
  9482. PIPE_CONF_CHECK_P(shared_dpll);
  9483. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9484. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9485. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9486. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9487. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9488. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9489. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9490. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9491. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9492. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9493. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9494. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9495. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9496. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9497. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9498. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9499. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9500. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9501. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9502. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9503. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9504. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9505. PIPE_CONF_CHECK_X(dsi_pll.div);
  9506. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9507. PIPE_CONF_CHECK_I(pipe_bpp);
  9508. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9509. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9510. PIPE_CONF_CHECK_I(min_voltage_level);
  9511. #undef PIPE_CONF_CHECK_X
  9512. #undef PIPE_CONF_CHECK_I
  9513. #undef PIPE_CONF_CHECK_BOOL
  9514. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9515. #undef PIPE_CONF_CHECK_P
  9516. #undef PIPE_CONF_CHECK_FLAGS
  9517. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9518. #undef PIPE_CONF_QUIRK
  9519. return ret;
  9520. }
  9521. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9522. const struct intel_crtc_state *pipe_config)
  9523. {
  9524. if (pipe_config->has_pch_encoder) {
  9525. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9526. &pipe_config->fdi_m_n);
  9527. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9528. /*
  9529. * FDI already provided one idea for the dotclock.
  9530. * Yell if the encoder disagrees.
  9531. */
  9532. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9533. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9534. fdi_dotclock, dotclock);
  9535. }
  9536. }
  9537. static void verify_wm_state(struct drm_crtc *crtc,
  9538. struct drm_crtc_state *new_state)
  9539. {
  9540. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9541. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9542. struct skl_pipe_wm hw_wm, *sw_wm;
  9543. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9544. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9546. const enum pipe pipe = intel_crtc->pipe;
  9547. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9548. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9549. return;
  9550. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9551. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9552. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9553. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9554. /* planes */
  9555. for_each_universal_plane(dev_priv, pipe, plane) {
  9556. hw_plane_wm = &hw_wm.planes[plane];
  9557. sw_plane_wm = &sw_wm->planes[plane];
  9558. /* Watermarks */
  9559. for (level = 0; level <= max_level; level++) {
  9560. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9561. &sw_plane_wm->wm[level]))
  9562. continue;
  9563. 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",
  9564. pipe_name(pipe), plane + 1, level,
  9565. sw_plane_wm->wm[level].plane_en,
  9566. sw_plane_wm->wm[level].plane_res_b,
  9567. sw_plane_wm->wm[level].plane_res_l,
  9568. hw_plane_wm->wm[level].plane_en,
  9569. hw_plane_wm->wm[level].plane_res_b,
  9570. hw_plane_wm->wm[level].plane_res_l);
  9571. }
  9572. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9573. &sw_plane_wm->trans_wm)) {
  9574. 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",
  9575. pipe_name(pipe), plane + 1,
  9576. sw_plane_wm->trans_wm.plane_en,
  9577. sw_plane_wm->trans_wm.plane_res_b,
  9578. sw_plane_wm->trans_wm.plane_res_l,
  9579. hw_plane_wm->trans_wm.plane_en,
  9580. hw_plane_wm->trans_wm.plane_res_b,
  9581. hw_plane_wm->trans_wm.plane_res_l);
  9582. }
  9583. /* DDB */
  9584. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9585. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9586. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9587. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9588. pipe_name(pipe), plane + 1,
  9589. sw_ddb_entry->start, sw_ddb_entry->end,
  9590. hw_ddb_entry->start, hw_ddb_entry->end);
  9591. }
  9592. }
  9593. /*
  9594. * cursor
  9595. * If the cursor plane isn't active, we may not have updated it's ddb
  9596. * allocation. In that case since the ddb allocation will be updated
  9597. * once the plane becomes visible, we can skip this check
  9598. */
  9599. if (1) {
  9600. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9601. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9602. /* Watermarks */
  9603. for (level = 0; level <= max_level; level++) {
  9604. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9605. &sw_plane_wm->wm[level]))
  9606. continue;
  9607. 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",
  9608. pipe_name(pipe), level,
  9609. sw_plane_wm->wm[level].plane_en,
  9610. sw_plane_wm->wm[level].plane_res_b,
  9611. sw_plane_wm->wm[level].plane_res_l,
  9612. hw_plane_wm->wm[level].plane_en,
  9613. hw_plane_wm->wm[level].plane_res_b,
  9614. hw_plane_wm->wm[level].plane_res_l);
  9615. }
  9616. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9617. &sw_plane_wm->trans_wm)) {
  9618. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9619. pipe_name(pipe),
  9620. sw_plane_wm->trans_wm.plane_en,
  9621. sw_plane_wm->trans_wm.plane_res_b,
  9622. sw_plane_wm->trans_wm.plane_res_l,
  9623. hw_plane_wm->trans_wm.plane_en,
  9624. hw_plane_wm->trans_wm.plane_res_b,
  9625. hw_plane_wm->trans_wm.plane_res_l);
  9626. }
  9627. /* DDB */
  9628. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9629. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9630. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9631. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9632. pipe_name(pipe),
  9633. sw_ddb_entry->start, sw_ddb_entry->end,
  9634. hw_ddb_entry->start, hw_ddb_entry->end);
  9635. }
  9636. }
  9637. }
  9638. static void
  9639. verify_connector_state(struct drm_device *dev,
  9640. struct drm_atomic_state *state,
  9641. struct drm_crtc *crtc)
  9642. {
  9643. struct drm_connector *connector;
  9644. struct drm_connector_state *new_conn_state;
  9645. int i;
  9646. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9647. struct drm_encoder *encoder = connector->encoder;
  9648. struct drm_crtc_state *crtc_state = NULL;
  9649. if (new_conn_state->crtc != crtc)
  9650. continue;
  9651. if (crtc)
  9652. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9653. intel_connector_verify_state(crtc_state, new_conn_state);
  9654. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9655. "connector's atomic encoder doesn't match legacy encoder\n");
  9656. }
  9657. }
  9658. static void
  9659. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9660. {
  9661. struct intel_encoder *encoder;
  9662. struct drm_connector *connector;
  9663. struct drm_connector_state *old_conn_state, *new_conn_state;
  9664. int i;
  9665. for_each_intel_encoder(dev, encoder) {
  9666. bool enabled = false, found = false;
  9667. enum pipe pipe;
  9668. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9669. encoder->base.base.id,
  9670. encoder->base.name);
  9671. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9672. new_conn_state, i) {
  9673. if (old_conn_state->best_encoder == &encoder->base)
  9674. found = true;
  9675. if (new_conn_state->best_encoder != &encoder->base)
  9676. continue;
  9677. found = enabled = true;
  9678. I915_STATE_WARN(new_conn_state->crtc !=
  9679. encoder->base.crtc,
  9680. "connector's crtc doesn't match encoder crtc\n");
  9681. }
  9682. if (!found)
  9683. continue;
  9684. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9685. "encoder's enabled state mismatch "
  9686. "(expected %i, found %i)\n",
  9687. !!encoder->base.crtc, enabled);
  9688. if (!encoder->base.crtc) {
  9689. bool active;
  9690. active = encoder->get_hw_state(encoder, &pipe);
  9691. I915_STATE_WARN(active,
  9692. "encoder detached but still enabled on pipe %c.\n",
  9693. pipe_name(pipe));
  9694. }
  9695. }
  9696. }
  9697. static void
  9698. verify_crtc_state(struct drm_crtc *crtc,
  9699. struct drm_crtc_state *old_crtc_state,
  9700. struct drm_crtc_state *new_crtc_state)
  9701. {
  9702. struct drm_device *dev = crtc->dev;
  9703. struct drm_i915_private *dev_priv = to_i915(dev);
  9704. struct intel_encoder *encoder;
  9705. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9706. struct intel_crtc_state *pipe_config, *sw_config;
  9707. struct drm_atomic_state *old_state;
  9708. bool active;
  9709. old_state = old_crtc_state->state;
  9710. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9711. pipe_config = to_intel_crtc_state(old_crtc_state);
  9712. memset(pipe_config, 0, sizeof(*pipe_config));
  9713. pipe_config->base.crtc = crtc;
  9714. pipe_config->base.state = old_state;
  9715. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9716. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9717. /* we keep both pipes enabled on 830 */
  9718. if (IS_I830(dev_priv))
  9719. active = new_crtc_state->active;
  9720. I915_STATE_WARN(new_crtc_state->active != active,
  9721. "crtc active state doesn't match with hw state "
  9722. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9723. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9724. "transitional active state does not match atomic hw state "
  9725. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9726. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9727. enum pipe pipe;
  9728. active = encoder->get_hw_state(encoder, &pipe);
  9729. I915_STATE_WARN(active != new_crtc_state->active,
  9730. "[ENCODER:%i] active %i with crtc active %i\n",
  9731. encoder->base.base.id, active, new_crtc_state->active);
  9732. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9733. "Encoder connected to wrong pipe %c\n",
  9734. pipe_name(pipe));
  9735. if (active)
  9736. encoder->get_config(encoder, pipe_config);
  9737. }
  9738. intel_crtc_compute_pixel_rate(pipe_config);
  9739. if (!new_crtc_state->active)
  9740. return;
  9741. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9742. sw_config = to_intel_crtc_state(new_crtc_state);
  9743. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9744. pipe_config, false)) {
  9745. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9746. intel_dump_pipe_config(intel_crtc, pipe_config,
  9747. "[hw state]");
  9748. intel_dump_pipe_config(intel_crtc, sw_config,
  9749. "[sw state]");
  9750. }
  9751. }
  9752. static void
  9753. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9754. struct intel_shared_dpll *pll,
  9755. struct drm_crtc *crtc,
  9756. struct drm_crtc_state *new_state)
  9757. {
  9758. struct intel_dpll_hw_state dpll_hw_state;
  9759. unsigned crtc_mask;
  9760. bool active;
  9761. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9762. DRM_DEBUG_KMS("%s\n", pll->name);
  9763. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9764. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9765. I915_STATE_WARN(!pll->on && pll->active_mask,
  9766. "pll in active use but not on in sw tracking\n");
  9767. I915_STATE_WARN(pll->on && !pll->active_mask,
  9768. "pll is on but not used by any active crtc\n");
  9769. I915_STATE_WARN(pll->on != active,
  9770. "pll on state mismatch (expected %i, found %i)\n",
  9771. pll->on, active);
  9772. }
  9773. if (!crtc) {
  9774. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9775. "more active pll users than references: %x vs %x\n",
  9776. pll->active_mask, pll->state.crtc_mask);
  9777. return;
  9778. }
  9779. crtc_mask = 1 << drm_crtc_index(crtc);
  9780. if (new_state->active)
  9781. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9782. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9783. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9784. else
  9785. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9786. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9787. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9788. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9789. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9790. crtc_mask, pll->state.crtc_mask);
  9791. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9792. &dpll_hw_state,
  9793. sizeof(dpll_hw_state)),
  9794. "pll hw state mismatch\n");
  9795. }
  9796. static void
  9797. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9798. struct drm_crtc_state *old_crtc_state,
  9799. struct drm_crtc_state *new_crtc_state)
  9800. {
  9801. struct drm_i915_private *dev_priv = to_i915(dev);
  9802. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9803. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9804. if (new_state->shared_dpll)
  9805. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9806. if (old_state->shared_dpll &&
  9807. old_state->shared_dpll != new_state->shared_dpll) {
  9808. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9809. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9810. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9811. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9812. pipe_name(drm_crtc_index(crtc)));
  9813. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9814. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9815. pipe_name(drm_crtc_index(crtc)));
  9816. }
  9817. }
  9818. static void
  9819. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9820. struct drm_atomic_state *state,
  9821. struct drm_crtc_state *old_state,
  9822. struct drm_crtc_state *new_state)
  9823. {
  9824. if (!needs_modeset(new_state) &&
  9825. !to_intel_crtc_state(new_state)->update_pipe)
  9826. return;
  9827. verify_wm_state(crtc, new_state);
  9828. verify_connector_state(crtc->dev, state, crtc);
  9829. verify_crtc_state(crtc, old_state, new_state);
  9830. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9831. }
  9832. static void
  9833. verify_disabled_dpll_state(struct drm_device *dev)
  9834. {
  9835. struct drm_i915_private *dev_priv = to_i915(dev);
  9836. int i;
  9837. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9838. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9839. }
  9840. static void
  9841. intel_modeset_verify_disabled(struct drm_device *dev,
  9842. struct drm_atomic_state *state)
  9843. {
  9844. verify_encoder_state(dev, state);
  9845. verify_connector_state(dev, state, NULL);
  9846. verify_disabled_dpll_state(dev);
  9847. }
  9848. static void update_scanline_offset(struct intel_crtc *crtc)
  9849. {
  9850. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9851. /*
  9852. * The scanline counter increments at the leading edge of hsync.
  9853. *
  9854. * On most platforms it starts counting from vtotal-1 on the
  9855. * first active line. That means the scanline counter value is
  9856. * always one less than what we would expect. Ie. just after
  9857. * start of vblank, which also occurs at start of hsync (on the
  9858. * last active line), the scanline counter will read vblank_start-1.
  9859. *
  9860. * On gen2 the scanline counter starts counting from 1 instead
  9861. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9862. * to keep the value positive), instead of adding one.
  9863. *
  9864. * On HSW+ the behaviour of the scanline counter depends on the output
  9865. * type. For DP ports it behaves like most other platforms, but on HDMI
  9866. * there's an extra 1 line difference. So we need to add two instead of
  9867. * one to the value.
  9868. *
  9869. * On VLV/CHV DSI the scanline counter would appear to increment
  9870. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9871. * that means we can't tell whether we're in vblank or not while
  9872. * we're on that particular line. We must still set scanline_offset
  9873. * to 1 so that the vblank timestamps come out correct when we query
  9874. * the scanline counter from within the vblank interrupt handler.
  9875. * However if queried just before the start of vblank we'll get an
  9876. * answer that's slightly in the future.
  9877. */
  9878. if (IS_GEN2(dev_priv)) {
  9879. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9880. int vtotal;
  9881. vtotal = adjusted_mode->crtc_vtotal;
  9882. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9883. vtotal /= 2;
  9884. crtc->scanline_offset = vtotal - 1;
  9885. } else if (HAS_DDI(dev_priv) &&
  9886. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9887. crtc->scanline_offset = 2;
  9888. } else
  9889. crtc->scanline_offset = 1;
  9890. }
  9891. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9892. {
  9893. struct drm_device *dev = state->dev;
  9894. struct drm_i915_private *dev_priv = to_i915(dev);
  9895. struct drm_crtc *crtc;
  9896. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9897. int i;
  9898. if (!dev_priv->display.crtc_compute_clock)
  9899. return;
  9900. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9902. struct intel_shared_dpll *old_dpll =
  9903. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9904. if (!needs_modeset(new_crtc_state))
  9905. continue;
  9906. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9907. if (!old_dpll)
  9908. continue;
  9909. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9910. }
  9911. }
  9912. /*
  9913. * This implements the workaround described in the "notes" section of the mode
  9914. * set sequence documentation. When going from no pipes or single pipe to
  9915. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9916. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9917. */
  9918. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9919. {
  9920. struct drm_crtc_state *crtc_state;
  9921. struct intel_crtc *intel_crtc;
  9922. struct drm_crtc *crtc;
  9923. struct intel_crtc_state *first_crtc_state = NULL;
  9924. struct intel_crtc_state *other_crtc_state = NULL;
  9925. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9926. int i;
  9927. /* look at all crtc's that are going to be enabled in during modeset */
  9928. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9929. intel_crtc = to_intel_crtc(crtc);
  9930. if (!crtc_state->active || !needs_modeset(crtc_state))
  9931. continue;
  9932. if (first_crtc_state) {
  9933. other_crtc_state = to_intel_crtc_state(crtc_state);
  9934. break;
  9935. } else {
  9936. first_crtc_state = to_intel_crtc_state(crtc_state);
  9937. first_pipe = intel_crtc->pipe;
  9938. }
  9939. }
  9940. /* No workaround needed? */
  9941. if (!first_crtc_state)
  9942. return 0;
  9943. /* w/a possibly needed, check how many crtc's are already enabled. */
  9944. for_each_intel_crtc(state->dev, intel_crtc) {
  9945. struct intel_crtc_state *pipe_config;
  9946. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9947. if (IS_ERR(pipe_config))
  9948. return PTR_ERR(pipe_config);
  9949. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9950. if (!pipe_config->base.active ||
  9951. needs_modeset(&pipe_config->base))
  9952. continue;
  9953. /* 2 or more enabled crtcs means no need for w/a */
  9954. if (enabled_pipe != INVALID_PIPE)
  9955. return 0;
  9956. enabled_pipe = intel_crtc->pipe;
  9957. }
  9958. if (enabled_pipe != INVALID_PIPE)
  9959. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9960. else if (other_crtc_state)
  9961. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9962. return 0;
  9963. }
  9964. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9965. {
  9966. struct drm_crtc *crtc;
  9967. /* Add all pipes to the state */
  9968. for_each_crtc(state->dev, crtc) {
  9969. struct drm_crtc_state *crtc_state;
  9970. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9971. if (IS_ERR(crtc_state))
  9972. return PTR_ERR(crtc_state);
  9973. }
  9974. return 0;
  9975. }
  9976. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9977. {
  9978. struct drm_crtc *crtc;
  9979. /*
  9980. * Add all pipes to the state, and force
  9981. * a modeset on all the active ones.
  9982. */
  9983. for_each_crtc(state->dev, crtc) {
  9984. struct drm_crtc_state *crtc_state;
  9985. int ret;
  9986. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9987. if (IS_ERR(crtc_state))
  9988. return PTR_ERR(crtc_state);
  9989. if (!crtc_state->active || needs_modeset(crtc_state))
  9990. continue;
  9991. crtc_state->mode_changed = true;
  9992. ret = drm_atomic_add_affected_connectors(state, crtc);
  9993. if (ret)
  9994. return ret;
  9995. ret = drm_atomic_add_affected_planes(state, crtc);
  9996. if (ret)
  9997. return ret;
  9998. }
  9999. return 0;
  10000. }
  10001. static int intel_modeset_checks(struct drm_atomic_state *state)
  10002. {
  10003. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10004. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10005. struct drm_crtc *crtc;
  10006. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10007. int ret = 0, i;
  10008. if (!check_digital_port_conflicts(state)) {
  10009. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10010. return -EINVAL;
  10011. }
  10012. intel_state->modeset = true;
  10013. intel_state->active_crtcs = dev_priv->active_crtcs;
  10014. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10015. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10016. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10017. if (new_crtc_state->active)
  10018. intel_state->active_crtcs |= 1 << i;
  10019. else
  10020. intel_state->active_crtcs &= ~(1 << i);
  10021. if (old_crtc_state->active != new_crtc_state->active)
  10022. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10023. }
  10024. /*
  10025. * See if the config requires any additional preparation, e.g.
  10026. * to adjust global state with pipes off. We need to do this
  10027. * here so we can get the modeset_pipe updated config for the new
  10028. * mode set on this crtc. For other crtcs we need to use the
  10029. * adjusted_mode bits in the crtc directly.
  10030. */
  10031. if (dev_priv->display.modeset_calc_cdclk) {
  10032. ret = dev_priv->display.modeset_calc_cdclk(state);
  10033. if (ret < 0)
  10034. return ret;
  10035. /*
  10036. * Writes to dev_priv->cdclk.logical must protected by
  10037. * holding all the crtc locks, even if we don't end up
  10038. * touching the hardware
  10039. */
  10040. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10041. &intel_state->cdclk.logical)) {
  10042. ret = intel_lock_all_pipes(state);
  10043. if (ret < 0)
  10044. return ret;
  10045. }
  10046. /* All pipes must be switched off while we change the cdclk. */
  10047. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10048. &intel_state->cdclk.actual)) {
  10049. ret = intel_modeset_all_pipes(state);
  10050. if (ret < 0)
  10051. return ret;
  10052. }
  10053. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10054. intel_state->cdclk.logical.cdclk,
  10055. intel_state->cdclk.actual.cdclk);
  10056. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10057. intel_state->cdclk.logical.voltage_level,
  10058. intel_state->cdclk.actual.voltage_level);
  10059. } else {
  10060. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10061. }
  10062. intel_modeset_clear_plls(state);
  10063. if (IS_HASWELL(dev_priv))
  10064. return haswell_mode_set_planes_workaround(state);
  10065. return 0;
  10066. }
  10067. /*
  10068. * Handle calculation of various watermark data at the end of the atomic check
  10069. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10070. * handlers to ensure that all derived state has been updated.
  10071. */
  10072. static int calc_watermark_data(struct drm_atomic_state *state)
  10073. {
  10074. struct drm_device *dev = state->dev;
  10075. struct drm_i915_private *dev_priv = to_i915(dev);
  10076. /* Is there platform-specific watermark information to calculate? */
  10077. if (dev_priv->display.compute_global_watermarks)
  10078. return dev_priv->display.compute_global_watermarks(state);
  10079. return 0;
  10080. }
  10081. /**
  10082. * intel_atomic_check - validate state object
  10083. * @dev: drm device
  10084. * @state: state to validate
  10085. */
  10086. static int intel_atomic_check(struct drm_device *dev,
  10087. struct drm_atomic_state *state)
  10088. {
  10089. struct drm_i915_private *dev_priv = to_i915(dev);
  10090. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10091. struct drm_crtc *crtc;
  10092. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10093. int ret, i;
  10094. bool any_ms = false;
  10095. ret = drm_atomic_helper_check_modeset(dev, state);
  10096. if (ret)
  10097. return ret;
  10098. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10099. struct intel_crtc_state *pipe_config =
  10100. to_intel_crtc_state(crtc_state);
  10101. /* Catch I915_MODE_FLAG_INHERITED */
  10102. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10103. crtc_state->mode_changed = true;
  10104. if (!needs_modeset(crtc_state))
  10105. continue;
  10106. if (!crtc_state->enable) {
  10107. any_ms = true;
  10108. continue;
  10109. }
  10110. /* FIXME: For only active_changed we shouldn't need to do any
  10111. * state recomputation at all. */
  10112. ret = drm_atomic_add_affected_connectors(state, crtc);
  10113. if (ret)
  10114. return ret;
  10115. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10116. if (ret) {
  10117. intel_dump_pipe_config(to_intel_crtc(crtc),
  10118. pipe_config, "[failed]");
  10119. return ret;
  10120. }
  10121. if (i915_modparams.fastboot &&
  10122. intel_pipe_config_compare(dev_priv,
  10123. to_intel_crtc_state(old_crtc_state),
  10124. pipe_config, true)) {
  10125. crtc_state->mode_changed = false;
  10126. pipe_config->update_pipe = true;
  10127. }
  10128. if (needs_modeset(crtc_state))
  10129. any_ms = true;
  10130. ret = drm_atomic_add_affected_planes(state, crtc);
  10131. if (ret)
  10132. return ret;
  10133. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10134. needs_modeset(crtc_state) ?
  10135. "[modeset]" : "[fastset]");
  10136. }
  10137. if (any_ms) {
  10138. ret = intel_modeset_checks(state);
  10139. if (ret)
  10140. return ret;
  10141. } else {
  10142. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10143. }
  10144. ret = drm_atomic_helper_check_planes(dev, state);
  10145. if (ret)
  10146. return ret;
  10147. intel_fbc_choose_crtc(dev_priv, state);
  10148. return calc_watermark_data(state);
  10149. }
  10150. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10151. struct drm_atomic_state *state)
  10152. {
  10153. return drm_atomic_helper_prepare_planes(dev, state);
  10154. }
  10155. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10156. {
  10157. struct drm_device *dev = crtc->base.dev;
  10158. if (!dev->max_vblank_count)
  10159. return drm_crtc_accurate_vblank_count(&crtc->base);
  10160. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10161. }
  10162. static void intel_update_crtc(struct drm_crtc *crtc,
  10163. struct drm_atomic_state *state,
  10164. struct drm_crtc_state *old_crtc_state,
  10165. struct drm_crtc_state *new_crtc_state)
  10166. {
  10167. struct drm_device *dev = crtc->dev;
  10168. struct drm_i915_private *dev_priv = to_i915(dev);
  10169. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10170. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10171. bool modeset = needs_modeset(new_crtc_state);
  10172. if (modeset) {
  10173. update_scanline_offset(intel_crtc);
  10174. dev_priv->display.crtc_enable(pipe_config, state);
  10175. } else {
  10176. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10177. pipe_config);
  10178. }
  10179. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10180. intel_fbc_enable(
  10181. intel_crtc, pipe_config,
  10182. to_intel_plane_state(crtc->primary->state));
  10183. }
  10184. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10185. }
  10186. static void intel_update_crtcs(struct drm_atomic_state *state)
  10187. {
  10188. struct drm_crtc *crtc;
  10189. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10190. int i;
  10191. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10192. if (!new_crtc_state->active)
  10193. continue;
  10194. intel_update_crtc(crtc, state, old_crtc_state,
  10195. new_crtc_state);
  10196. }
  10197. }
  10198. static void skl_update_crtcs(struct drm_atomic_state *state)
  10199. {
  10200. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10201. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10202. struct drm_crtc *crtc;
  10203. struct intel_crtc *intel_crtc;
  10204. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10205. struct intel_crtc_state *cstate;
  10206. unsigned int updated = 0;
  10207. bool progress;
  10208. enum pipe pipe;
  10209. int i;
  10210. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10211. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10212. /* ignore allocations for crtc's that have been turned off. */
  10213. if (new_crtc_state->active)
  10214. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10215. /*
  10216. * Whenever the number of active pipes changes, we need to make sure we
  10217. * update the pipes in the right order so that their ddb allocations
  10218. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10219. * cause pipe underruns and other bad stuff.
  10220. */
  10221. do {
  10222. progress = false;
  10223. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10224. bool vbl_wait = false;
  10225. unsigned int cmask = drm_crtc_mask(crtc);
  10226. intel_crtc = to_intel_crtc(crtc);
  10227. cstate = to_intel_crtc_state(new_crtc_state);
  10228. pipe = intel_crtc->pipe;
  10229. if (updated & cmask || !cstate->base.active)
  10230. continue;
  10231. if (skl_ddb_allocation_overlaps(dev_priv,
  10232. entries,
  10233. &cstate->wm.skl.ddb,
  10234. i))
  10235. continue;
  10236. updated |= cmask;
  10237. entries[i] = &cstate->wm.skl.ddb;
  10238. /*
  10239. * If this is an already active pipe, it's DDB changed,
  10240. * and this isn't the last pipe that needs updating
  10241. * then we need to wait for a vblank to pass for the
  10242. * new ddb allocation to take effect.
  10243. */
  10244. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10245. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10246. !new_crtc_state->active_changed &&
  10247. intel_state->wm_results.dirty_pipes != updated)
  10248. vbl_wait = true;
  10249. intel_update_crtc(crtc, state, old_crtc_state,
  10250. new_crtc_state);
  10251. if (vbl_wait)
  10252. intel_wait_for_vblank(dev_priv, pipe);
  10253. progress = true;
  10254. }
  10255. } while (progress);
  10256. }
  10257. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10258. {
  10259. struct intel_atomic_state *state, *next;
  10260. struct llist_node *freed;
  10261. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10262. llist_for_each_entry_safe(state, next, freed, freed)
  10263. drm_atomic_state_put(&state->base);
  10264. }
  10265. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10266. {
  10267. struct drm_i915_private *dev_priv =
  10268. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10269. intel_atomic_helper_free_state(dev_priv);
  10270. }
  10271. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10272. {
  10273. struct wait_queue_entry wait_fence, wait_reset;
  10274. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10275. init_wait_entry(&wait_fence, 0);
  10276. init_wait_entry(&wait_reset, 0);
  10277. for (;;) {
  10278. prepare_to_wait(&intel_state->commit_ready.wait,
  10279. &wait_fence, TASK_UNINTERRUPTIBLE);
  10280. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10281. &wait_reset, TASK_UNINTERRUPTIBLE);
  10282. if (i915_sw_fence_done(&intel_state->commit_ready)
  10283. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10284. break;
  10285. schedule();
  10286. }
  10287. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10288. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10289. }
  10290. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10291. {
  10292. struct drm_device *dev = state->dev;
  10293. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10294. struct drm_i915_private *dev_priv = to_i915(dev);
  10295. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10296. struct drm_crtc *crtc;
  10297. struct intel_crtc_state *intel_cstate;
  10298. u64 put_domains[I915_MAX_PIPES] = {};
  10299. int i;
  10300. intel_atomic_commit_fence_wait(intel_state);
  10301. drm_atomic_helper_wait_for_dependencies(state);
  10302. if (intel_state->modeset)
  10303. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10304. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10305. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10306. if (needs_modeset(new_crtc_state) ||
  10307. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10308. put_domains[to_intel_crtc(crtc)->pipe] =
  10309. modeset_get_crtc_power_domains(crtc,
  10310. to_intel_crtc_state(new_crtc_state));
  10311. }
  10312. if (!needs_modeset(new_crtc_state))
  10313. continue;
  10314. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10315. to_intel_crtc_state(new_crtc_state));
  10316. if (old_crtc_state->active) {
  10317. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10318. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10319. intel_crtc->active = false;
  10320. intel_fbc_disable(intel_crtc);
  10321. intel_disable_shared_dpll(intel_crtc);
  10322. /*
  10323. * Underruns don't always raise
  10324. * interrupts, so check manually.
  10325. */
  10326. intel_check_cpu_fifo_underruns(dev_priv);
  10327. intel_check_pch_fifo_underruns(dev_priv);
  10328. if (!new_crtc_state->active) {
  10329. /*
  10330. * Make sure we don't call initial_watermarks
  10331. * for ILK-style watermark updates.
  10332. *
  10333. * No clue what this is supposed to achieve.
  10334. */
  10335. if (INTEL_GEN(dev_priv) >= 9)
  10336. dev_priv->display.initial_watermarks(intel_state,
  10337. to_intel_crtc_state(new_crtc_state));
  10338. }
  10339. }
  10340. }
  10341. /* Only after disabling all output pipelines that will be changed can we
  10342. * update the the output configuration. */
  10343. intel_modeset_update_crtc_state(state);
  10344. if (intel_state->modeset) {
  10345. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10346. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10347. /*
  10348. * SKL workaround: bspec recommends we disable the SAGV when we
  10349. * have more then one pipe enabled
  10350. */
  10351. if (!intel_can_enable_sagv(state))
  10352. intel_disable_sagv(dev_priv);
  10353. intel_modeset_verify_disabled(dev, state);
  10354. }
  10355. /* Complete the events for pipes that have now been disabled */
  10356. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10357. bool modeset = needs_modeset(new_crtc_state);
  10358. /* Complete events for now disable pipes here. */
  10359. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10360. spin_lock_irq(&dev->event_lock);
  10361. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10362. spin_unlock_irq(&dev->event_lock);
  10363. new_crtc_state->event = NULL;
  10364. }
  10365. }
  10366. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10367. dev_priv->display.update_crtcs(state);
  10368. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10369. * already, but still need the state for the delayed optimization. To
  10370. * fix this:
  10371. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10372. * - schedule that vblank worker _before_ calling hw_done
  10373. * - at the start of commit_tail, cancel it _synchrously
  10374. * - switch over to the vblank wait helper in the core after that since
  10375. * we don't need out special handling any more.
  10376. */
  10377. drm_atomic_helper_wait_for_flip_done(dev, state);
  10378. /*
  10379. * Now that the vblank has passed, we can go ahead and program the
  10380. * optimal watermarks on platforms that need two-step watermark
  10381. * programming.
  10382. *
  10383. * TODO: Move this (and other cleanup) to an async worker eventually.
  10384. */
  10385. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10386. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10387. if (dev_priv->display.optimize_watermarks)
  10388. dev_priv->display.optimize_watermarks(intel_state,
  10389. intel_cstate);
  10390. }
  10391. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10392. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10393. if (put_domains[i])
  10394. modeset_put_power_domains(dev_priv, put_domains[i]);
  10395. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10396. }
  10397. if (intel_state->modeset && intel_can_enable_sagv(state))
  10398. intel_enable_sagv(dev_priv);
  10399. drm_atomic_helper_commit_hw_done(state);
  10400. if (intel_state->modeset) {
  10401. /* As one of the primary mmio accessors, KMS has a high
  10402. * likelihood of triggering bugs in unclaimed access. After we
  10403. * finish modesetting, see if an error has been flagged, and if
  10404. * so enable debugging for the next modeset - and hope we catch
  10405. * the culprit.
  10406. */
  10407. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10408. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10409. }
  10410. drm_atomic_helper_cleanup_planes(dev, state);
  10411. drm_atomic_helper_commit_cleanup_done(state);
  10412. drm_atomic_state_put(state);
  10413. intel_atomic_helper_free_state(dev_priv);
  10414. }
  10415. static void intel_atomic_commit_work(struct work_struct *work)
  10416. {
  10417. struct drm_atomic_state *state =
  10418. container_of(work, struct drm_atomic_state, commit_work);
  10419. intel_atomic_commit_tail(state);
  10420. }
  10421. static int __i915_sw_fence_call
  10422. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10423. enum i915_sw_fence_notify notify)
  10424. {
  10425. struct intel_atomic_state *state =
  10426. container_of(fence, struct intel_atomic_state, commit_ready);
  10427. switch (notify) {
  10428. case FENCE_COMPLETE:
  10429. /* we do blocking waits in the worker, nothing to do here */
  10430. break;
  10431. case FENCE_FREE:
  10432. {
  10433. struct intel_atomic_helper *helper =
  10434. &to_i915(state->base.dev)->atomic_helper;
  10435. if (llist_add(&state->freed, &helper->free_list))
  10436. schedule_work(&helper->free_work);
  10437. break;
  10438. }
  10439. }
  10440. return NOTIFY_DONE;
  10441. }
  10442. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10443. {
  10444. struct drm_plane_state *old_plane_state, *new_plane_state;
  10445. struct drm_plane *plane;
  10446. int i;
  10447. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10448. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10449. intel_fb_obj(new_plane_state->fb),
  10450. to_intel_plane(plane)->frontbuffer_bit);
  10451. }
  10452. /**
  10453. * intel_atomic_commit - commit validated state object
  10454. * @dev: DRM device
  10455. * @state: the top-level driver state object
  10456. * @nonblock: nonblocking commit
  10457. *
  10458. * This function commits a top-level state object that has been validated
  10459. * with drm_atomic_helper_check().
  10460. *
  10461. * RETURNS
  10462. * Zero for success or -errno.
  10463. */
  10464. static int intel_atomic_commit(struct drm_device *dev,
  10465. struct drm_atomic_state *state,
  10466. bool nonblock)
  10467. {
  10468. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10469. struct drm_i915_private *dev_priv = to_i915(dev);
  10470. int ret = 0;
  10471. drm_atomic_state_get(state);
  10472. i915_sw_fence_init(&intel_state->commit_ready,
  10473. intel_atomic_commit_ready);
  10474. /*
  10475. * The intel_legacy_cursor_update() fast path takes care
  10476. * of avoiding the vblank waits for simple cursor
  10477. * movement and flips. For cursor on/off and size changes,
  10478. * we want to perform the vblank waits so that watermark
  10479. * updates happen during the correct frames. Gen9+ have
  10480. * double buffered watermarks and so shouldn't need this.
  10481. *
  10482. * Unset state->legacy_cursor_update before the call to
  10483. * drm_atomic_helper_setup_commit() because otherwise
  10484. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10485. * we get FIFO underruns because we didn't wait
  10486. * for vblank.
  10487. *
  10488. * FIXME doing watermarks and fb cleanup from a vblank worker
  10489. * (assuming we had any) would solve these problems.
  10490. */
  10491. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10492. struct intel_crtc_state *new_crtc_state;
  10493. struct intel_crtc *crtc;
  10494. int i;
  10495. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10496. if (new_crtc_state->wm.need_postvbl_update ||
  10497. new_crtc_state->update_wm_post)
  10498. state->legacy_cursor_update = false;
  10499. }
  10500. ret = intel_atomic_prepare_commit(dev, state);
  10501. if (ret) {
  10502. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10503. i915_sw_fence_commit(&intel_state->commit_ready);
  10504. return ret;
  10505. }
  10506. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10507. if (!ret)
  10508. ret = drm_atomic_helper_swap_state(state, true);
  10509. if (ret) {
  10510. i915_sw_fence_commit(&intel_state->commit_ready);
  10511. drm_atomic_helper_cleanup_planes(dev, state);
  10512. return ret;
  10513. }
  10514. dev_priv->wm.distrust_bios_wm = false;
  10515. intel_shared_dpll_swap_state(state);
  10516. intel_atomic_track_fbs(state);
  10517. if (intel_state->modeset) {
  10518. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10519. sizeof(intel_state->min_cdclk));
  10520. memcpy(dev_priv->min_voltage_level,
  10521. intel_state->min_voltage_level,
  10522. sizeof(intel_state->min_voltage_level));
  10523. dev_priv->active_crtcs = intel_state->active_crtcs;
  10524. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10525. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10526. }
  10527. drm_atomic_state_get(state);
  10528. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10529. i915_sw_fence_commit(&intel_state->commit_ready);
  10530. if (nonblock)
  10531. queue_work(system_unbound_wq, &state->commit_work);
  10532. else
  10533. intel_atomic_commit_tail(state);
  10534. return 0;
  10535. }
  10536. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10537. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10538. .set_config = drm_atomic_helper_set_config,
  10539. .destroy = intel_crtc_destroy,
  10540. .page_flip = drm_atomic_helper_page_flip,
  10541. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10542. .atomic_destroy_state = intel_crtc_destroy_state,
  10543. .set_crc_source = intel_crtc_set_crc_source,
  10544. };
  10545. struct wait_rps_boost {
  10546. struct wait_queue_entry wait;
  10547. struct drm_crtc *crtc;
  10548. struct drm_i915_gem_request *request;
  10549. };
  10550. static int do_rps_boost(struct wait_queue_entry *_wait,
  10551. unsigned mode, int sync, void *key)
  10552. {
  10553. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10554. struct drm_i915_gem_request *rq = wait->request;
  10555. gen6_rps_boost(rq, NULL);
  10556. i915_gem_request_put(rq);
  10557. drm_crtc_vblank_put(wait->crtc);
  10558. list_del(&wait->wait.entry);
  10559. kfree(wait);
  10560. return 1;
  10561. }
  10562. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10563. struct dma_fence *fence)
  10564. {
  10565. struct wait_rps_boost *wait;
  10566. if (!dma_fence_is_i915(fence))
  10567. return;
  10568. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10569. return;
  10570. if (drm_crtc_vblank_get(crtc))
  10571. return;
  10572. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10573. if (!wait) {
  10574. drm_crtc_vblank_put(crtc);
  10575. return;
  10576. }
  10577. wait->request = to_request(dma_fence_get(fence));
  10578. wait->crtc = crtc;
  10579. wait->wait.func = do_rps_boost;
  10580. wait->wait.flags = 0;
  10581. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10582. }
  10583. /**
  10584. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10585. * @plane: drm plane to prepare for
  10586. * @fb: framebuffer to prepare for presentation
  10587. *
  10588. * Prepares a framebuffer for usage on a display plane. Generally this
  10589. * involves pinning the underlying object and updating the frontbuffer tracking
  10590. * bits. Some older platforms need special physical address handling for
  10591. * cursor planes.
  10592. *
  10593. * Must be called with struct_mutex held.
  10594. *
  10595. * Returns 0 on success, negative error code on failure.
  10596. */
  10597. int
  10598. intel_prepare_plane_fb(struct drm_plane *plane,
  10599. struct drm_plane_state *new_state)
  10600. {
  10601. struct intel_atomic_state *intel_state =
  10602. to_intel_atomic_state(new_state->state);
  10603. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10604. struct drm_framebuffer *fb = new_state->fb;
  10605. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10606. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10607. int ret;
  10608. if (old_obj) {
  10609. struct drm_crtc_state *crtc_state =
  10610. drm_atomic_get_existing_crtc_state(new_state->state,
  10611. plane->state->crtc);
  10612. /* Big Hammer, we also need to ensure that any pending
  10613. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10614. * current scanout is retired before unpinning the old
  10615. * framebuffer. Note that we rely on userspace rendering
  10616. * into the buffer attached to the pipe they are waiting
  10617. * on. If not, userspace generates a GPU hang with IPEHR
  10618. * point to the MI_WAIT_FOR_EVENT.
  10619. *
  10620. * This should only fail upon a hung GPU, in which case we
  10621. * can safely continue.
  10622. */
  10623. if (needs_modeset(crtc_state)) {
  10624. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10625. old_obj->resv, NULL,
  10626. false, 0,
  10627. GFP_KERNEL);
  10628. if (ret < 0)
  10629. return ret;
  10630. }
  10631. }
  10632. if (new_state->fence) { /* explicit fencing */
  10633. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10634. new_state->fence,
  10635. I915_FENCE_TIMEOUT,
  10636. GFP_KERNEL);
  10637. if (ret < 0)
  10638. return ret;
  10639. }
  10640. if (!obj)
  10641. return 0;
  10642. ret = i915_gem_object_pin_pages(obj);
  10643. if (ret)
  10644. return ret;
  10645. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10646. if (ret) {
  10647. i915_gem_object_unpin_pages(obj);
  10648. return ret;
  10649. }
  10650. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10651. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10652. const int align = intel_cursor_alignment(dev_priv);
  10653. ret = i915_gem_object_attach_phys(obj, align);
  10654. } else {
  10655. struct i915_vma *vma;
  10656. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10657. if (!IS_ERR(vma))
  10658. to_intel_plane_state(new_state)->vma = vma;
  10659. else
  10660. ret = PTR_ERR(vma);
  10661. }
  10662. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10663. mutex_unlock(&dev_priv->drm.struct_mutex);
  10664. i915_gem_object_unpin_pages(obj);
  10665. if (ret)
  10666. return ret;
  10667. if (!new_state->fence) { /* implicit fencing */
  10668. struct dma_fence *fence;
  10669. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10670. obj->resv, NULL,
  10671. false, I915_FENCE_TIMEOUT,
  10672. GFP_KERNEL);
  10673. if (ret < 0)
  10674. return ret;
  10675. fence = reservation_object_get_excl_rcu(obj->resv);
  10676. if (fence) {
  10677. add_rps_boost_after_vblank(new_state->crtc, fence);
  10678. dma_fence_put(fence);
  10679. }
  10680. } else {
  10681. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10682. }
  10683. return 0;
  10684. }
  10685. /**
  10686. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10687. * @plane: drm plane to clean up for
  10688. * @fb: old framebuffer that was on plane
  10689. *
  10690. * Cleans up a framebuffer that has just been removed from a plane.
  10691. *
  10692. * Must be called with struct_mutex held.
  10693. */
  10694. void
  10695. intel_cleanup_plane_fb(struct drm_plane *plane,
  10696. struct drm_plane_state *old_state)
  10697. {
  10698. struct i915_vma *vma;
  10699. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10700. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10701. if (vma) {
  10702. mutex_lock(&plane->dev->struct_mutex);
  10703. intel_unpin_fb_vma(vma);
  10704. mutex_unlock(&plane->dev->struct_mutex);
  10705. }
  10706. }
  10707. int
  10708. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10709. {
  10710. struct drm_i915_private *dev_priv;
  10711. int max_scale;
  10712. int crtc_clock, max_dotclk;
  10713. if (!intel_crtc || !crtc_state->base.enable)
  10714. return DRM_PLANE_HELPER_NO_SCALING;
  10715. dev_priv = to_i915(intel_crtc->base.dev);
  10716. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10717. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10718. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10719. max_dotclk *= 2;
  10720. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10721. return DRM_PLANE_HELPER_NO_SCALING;
  10722. /*
  10723. * skl max scale is lower of:
  10724. * close to 3 but not 3, -1 is for that purpose
  10725. * or
  10726. * cdclk/crtc_clock
  10727. */
  10728. max_scale = min((1 << 16) * 3 - 1,
  10729. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10730. return max_scale;
  10731. }
  10732. static int
  10733. intel_check_primary_plane(struct intel_plane *plane,
  10734. struct intel_crtc_state *crtc_state,
  10735. struct intel_plane_state *state)
  10736. {
  10737. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10738. struct drm_crtc *crtc = state->base.crtc;
  10739. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10740. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10741. bool can_position = false;
  10742. int ret;
  10743. if (INTEL_GEN(dev_priv) >= 9) {
  10744. /* use scaler when colorkey is not required */
  10745. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10746. min_scale = 1;
  10747. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10748. }
  10749. can_position = true;
  10750. }
  10751. ret = drm_atomic_helper_check_plane_state(&state->base,
  10752. &crtc_state->base,
  10753. &state->clip,
  10754. min_scale, max_scale,
  10755. can_position, true);
  10756. if (ret)
  10757. return ret;
  10758. if (!state->base.fb)
  10759. return 0;
  10760. if (INTEL_GEN(dev_priv) >= 9) {
  10761. ret = skl_check_plane_surface(state);
  10762. if (ret)
  10763. return ret;
  10764. state->ctl = skl_plane_ctl(crtc_state, state);
  10765. } else {
  10766. ret = i9xx_check_plane_surface(state);
  10767. if (ret)
  10768. return ret;
  10769. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10770. }
  10771. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10772. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10773. return 0;
  10774. }
  10775. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10776. struct drm_crtc_state *old_crtc_state)
  10777. {
  10778. struct drm_device *dev = crtc->dev;
  10779. struct drm_i915_private *dev_priv = to_i915(dev);
  10780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10781. struct intel_crtc_state *old_intel_cstate =
  10782. to_intel_crtc_state(old_crtc_state);
  10783. struct intel_atomic_state *old_intel_state =
  10784. to_intel_atomic_state(old_crtc_state->state);
  10785. struct intel_crtc_state *intel_cstate =
  10786. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10787. bool modeset = needs_modeset(&intel_cstate->base);
  10788. if (!modeset &&
  10789. (intel_cstate->base.color_mgmt_changed ||
  10790. intel_cstate->update_pipe)) {
  10791. intel_color_set_csc(&intel_cstate->base);
  10792. intel_color_load_luts(&intel_cstate->base);
  10793. }
  10794. /* Perform vblank evasion around commit operation */
  10795. intel_pipe_update_start(intel_cstate);
  10796. if (modeset)
  10797. goto out;
  10798. if (intel_cstate->update_pipe)
  10799. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10800. else if (INTEL_GEN(dev_priv) >= 9)
  10801. skl_detach_scalers(intel_crtc);
  10802. out:
  10803. if (dev_priv->display.atomic_update_watermarks)
  10804. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10805. intel_cstate);
  10806. }
  10807. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10808. struct drm_crtc_state *old_crtc_state)
  10809. {
  10810. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10811. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10812. struct intel_atomic_state *old_intel_state =
  10813. to_intel_atomic_state(old_crtc_state->state);
  10814. struct intel_crtc_state *new_crtc_state =
  10815. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10816. intel_pipe_update_end(new_crtc_state);
  10817. if (new_crtc_state->update_pipe &&
  10818. !needs_modeset(&new_crtc_state->base) &&
  10819. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED) {
  10820. if (!IS_GEN2(dev_priv))
  10821. intel_set_cpu_fifo_underrun_reporting(dev_priv, intel_crtc->pipe, true);
  10822. if (new_crtc_state->has_pch_encoder) {
  10823. enum pipe pch_transcoder =
  10824. intel_crtc_pch_transcoder(intel_crtc);
  10825. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10826. }
  10827. }
  10828. }
  10829. /**
  10830. * intel_plane_destroy - destroy a plane
  10831. * @plane: plane to destroy
  10832. *
  10833. * Common destruction function for all types of planes (primary, cursor,
  10834. * sprite).
  10835. */
  10836. void intel_plane_destroy(struct drm_plane *plane)
  10837. {
  10838. drm_plane_cleanup(plane);
  10839. kfree(to_intel_plane(plane));
  10840. }
  10841. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10842. {
  10843. switch (format) {
  10844. case DRM_FORMAT_C8:
  10845. case DRM_FORMAT_RGB565:
  10846. case DRM_FORMAT_XRGB1555:
  10847. case DRM_FORMAT_XRGB8888:
  10848. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10849. modifier == I915_FORMAT_MOD_X_TILED;
  10850. default:
  10851. return false;
  10852. }
  10853. }
  10854. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10855. {
  10856. switch (format) {
  10857. case DRM_FORMAT_C8:
  10858. case DRM_FORMAT_RGB565:
  10859. case DRM_FORMAT_XRGB8888:
  10860. case DRM_FORMAT_XBGR8888:
  10861. case DRM_FORMAT_XRGB2101010:
  10862. case DRM_FORMAT_XBGR2101010:
  10863. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10864. modifier == I915_FORMAT_MOD_X_TILED;
  10865. default:
  10866. return false;
  10867. }
  10868. }
  10869. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10870. {
  10871. switch (format) {
  10872. case DRM_FORMAT_XRGB8888:
  10873. case DRM_FORMAT_XBGR8888:
  10874. case DRM_FORMAT_ARGB8888:
  10875. case DRM_FORMAT_ABGR8888:
  10876. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10877. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10878. return true;
  10879. /* fall through */
  10880. case DRM_FORMAT_RGB565:
  10881. case DRM_FORMAT_XRGB2101010:
  10882. case DRM_FORMAT_XBGR2101010:
  10883. case DRM_FORMAT_YUYV:
  10884. case DRM_FORMAT_YVYU:
  10885. case DRM_FORMAT_UYVY:
  10886. case DRM_FORMAT_VYUY:
  10887. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10888. return true;
  10889. /* fall through */
  10890. case DRM_FORMAT_C8:
  10891. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10892. modifier == I915_FORMAT_MOD_X_TILED ||
  10893. modifier == I915_FORMAT_MOD_Y_TILED)
  10894. return true;
  10895. /* fall through */
  10896. default:
  10897. return false;
  10898. }
  10899. }
  10900. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10901. uint32_t format,
  10902. uint64_t modifier)
  10903. {
  10904. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10905. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10906. return false;
  10907. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10908. modifier != DRM_FORMAT_MOD_LINEAR)
  10909. return false;
  10910. if (INTEL_GEN(dev_priv) >= 9)
  10911. return skl_mod_supported(format, modifier);
  10912. else if (INTEL_GEN(dev_priv) >= 4)
  10913. return i965_mod_supported(format, modifier);
  10914. else
  10915. return i8xx_mod_supported(format, modifier);
  10916. unreachable();
  10917. }
  10918. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10919. uint32_t format,
  10920. uint64_t modifier)
  10921. {
  10922. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10923. return false;
  10924. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10925. }
  10926. static struct drm_plane_funcs intel_plane_funcs = {
  10927. .update_plane = drm_atomic_helper_update_plane,
  10928. .disable_plane = drm_atomic_helper_disable_plane,
  10929. .destroy = intel_plane_destroy,
  10930. .atomic_get_property = intel_plane_atomic_get_property,
  10931. .atomic_set_property = intel_plane_atomic_set_property,
  10932. .atomic_duplicate_state = intel_plane_duplicate_state,
  10933. .atomic_destroy_state = intel_plane_destroy_state,
  10934. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10935. };
  10936. static int
  10937. intel_legacy_cursor_update(struct drm_plane *plane,
  10938. struct drm_crtc *crtc,
  10939. struct drm_framebuffer *fb,
  10940. int crtc_x, int crtc_y,
  10941. unsigned int crtc_w, unsigned int crtc_h,
  10942. uint32_t src_x, uint32_t src_y,
  10943. uint32_t src_w, uint32_t src_h,
  10944. struct drm_modeset_acquire_ctx *ctx)
  10945. {
  10946. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10947. int ret;
  10948. struct drm_plane_state *old_plane_state, *new_plane_state;
  10949. struct intel_plane *intel_plane = to_intel_plane(plane);
  10950. struct drm_framebuffer *old_fb;
  10951. struct drm_crtc_state *crtc_state = crtc->state;
  10952. struct i915_vma *old_vma, *vma;
  10953. /*
  10954. * When crtc is inactive or there is a modeset pending,
  10955. * wait for it to complete in the slowpath
  10956. */
  10957. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10958. to_intel_crtc_state(crtc_state)->update_pipe)
  10959. goto slow;
  10960. old_plane_state = plane->state;
  10961. /*
  10962. * Don't do an async update if there is an outstanding commit modifying
  10963. * the plane. This prevents our async update's changes from getting
  10964. * overridden by a previous synchronous update's state.
  10965. */
  10966. if (old_plane_state->commit &&
  10967. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  10968. goto slow;
  10969. /*
  10970. * If any parameters change that may affect watermarks,
  10971. * take the slowpath. Only changing fb or position should be
  10972. * in the fastpath.
  10973. */
  10974. if (old_plane_state->crtc != crtc ||
  10975. old_plane_state->src_w != src_w ||
  10976. old_plane_state->src_h != src_h ||
  10977. old_plane_state->crtc_w != crtc_w ||
  10978. old_plane_state->crtc_h != crtc_h ||
  10979. !old_plane_state->fb != !fb)
  10980. goto slow;
  10981. new_plane_state = intel_plane_duplicate_state(plane);
  10982. if (!new_plane_state)
  10983. return -ENOMEM;
  10984. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10985. new_plane_state->src_x = src_x;
  10986. new_plane_state->src_y = src_y;
  10987. new_plane_state->src_w = src_w;
  10988. new_plane_state->src_h = src_h;
  10989. new_plane_state->crtc_x = crtc_x;
  10990. new_plane_state->crtc_y = crtc_y;
  10991. new_plane_state->crtc_w = crtc_w;
  10992. new_plane_state->crtc_h = crtc_h;
  10993. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10994. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  10995. to_intel_plane_state(plane->state),
  10996. to_intel_plane_state(new_plane_state));
  10997. if (ret)
  10998. goto out_free;
  10999. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11000. if (ret)
  11001. goto out_free;
  11002. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11003. int align = intel_cursor_alignment(dev_priv);
  11004. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11005. if (ret) {
  11006. DRM_DEBUG_KMS("failed to attach phys object\n");
  11007. goto out_unlock;
  11008. }
  11009. } else {
  11010. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11011. if (IS_ERR(vma)) {
  11012. DRM_DEBUG_KMS("failed to pin object\n");
  11013. ret = PTR_ERR(vma);
  11014. goto out_unlock;
  11015. }
  11016. to_intel_plane_state(new_plane_state)->vma = vma;
  11017. }
  11018. old_fb = old_plane_state->fb;
  11019. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11020. intel_plane->frontbuffer_bit);
  11021. /* Swap plane state */
  11022. plane->state = new_plane_state;
  11023. if (plane->state->visible) {
  11024. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11025. intel_plane->update_plane(intel_plane,
  11026. to_intel_crtc_state(crtc->state),
  11027. to_intel_plane_state(plane->state));
  11028. } else {
  11029. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11030. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11031. }
  11032. old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
  11033. if (old_vma)
  11034. intel_unpin_fb_vma(old_vma);
  11035. out_unlock:
  11036. mutex_unlock(&dev_priv->drm.struct_mutex);
  11037. out_free:
  11038. if (ret)
  11039. intel_plane_destroy_state(plane, new_plane_state);
  11040. else
  11041. intel_plane_destroy_state(plane, old_plane_state);
  11042. return ret;
  11043. slow:
  11044. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11045. crtc_x, crtc_y, crtc_w, crtc_h,
  11046. src_x, src_y, src_w, src_h, ctx);
  11047. }
  11048. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11049. .update_plane = intel_legacy_cursor_update,
  11050. .disable_plane = drm_atomic_helper_disable_plane,
  11051. .destroy = intel_plane_destroy,
  11052. .atomic_get_property = intel_plane_atomic_get_property,
  11053. .atomic_set_property = intel_plane_atomic_set_property,
  11054. .atomic_duplicate_state = intel_plane_duplicate_state,
  11055. .atomic_destroy_state = intel_plane_destroy_state,
  11056. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  11057. };
  11058. static struct intel_plane *
  11059. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11060. {
  11061. struct intel_plane *primary = NULL;
  11062. struct intel_plane_state *state = NULL;
  11063. const uint32_t *intel_primary_formats;
  11064. unsigned int supported_rotations;
  11065. unsigned int num_formats;
  11066. const uint64_t *modifiers;
  11067. int ret;
  11068. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11069. if (!primary) {
  11070. ret = -ENOMEM;
  11071. goto fail;
  11072. }
  11073. state = intel_create_plane_state(&primary->base);
  11074. if (!state) {
  11075. ret = -ENOMEM;
  11076. goto fail;
  11077. }
  11078. primary->base.state = &state->base;
  11079. primary->can_scale = false;
  11080. primary->max_downscale = 1;
  11081. if (INTEL_GEN(dev_priv) >= 9) {
  11082. primary->can_scale = true;
  11083. state->scaler_id = -1;
  11084. }
  11085. primary->pipe = pipe;
  11086. /*
  11087. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11088. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11089. */
  11090. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11091. primary->plane = (enum plane) !pipe;
  11092. else
  11093. primary->plane = (enum plane) pipe;
  11094. primary->id = PLANE_PRIMARY;
  11095. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11096. primary->check_plane = intel_check_primary_plane;
  11097. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  11098. intel_primary_formats = skl_primary_formats;
  11099. num_formats = ARRAY_SIZE(skl_primary_formats);
  11100. modifiers = skl_format_modifiers_ccs;
  11101. primary->update_plane = skl_update_plane;
  11102. primary->disable_plane = skl_disable_plane;
  11103. } else if (INTEL_GEN(dev_priv) >= 9) {
  11104. intel_primary_formats = skl_primary_formats;
  11105. num_formats = ARRAY_SIZE(skl_primary_formats);
  11106. if (pipe < PIPE_C)
  11107. modifiers = skl_format_modifiers_ccs;
  11108. else
  11109. modifiers = skl_format_modifiers_noccs;
  11110. primary->update_plane = skl_update_plane;
  11111. primary->disable_plane = skl_disable_plane;
  11112. } else if (INTEL_GEN(dev_priv) >= 4) {
  11113. intel_primary_formats = i965_primary_formats;
  11114. num_formats = ARRAY_SIZE(i965_primary_formats);
  11115. modifiers = i9xx_format_modifiers;
  11116. primary->update_plane = i9xx_update_primary_plane;
  11117. primary->disable_plane = i9xx_disable_primary_plane;
  11118. } else {
  11119. intel_primary_formats = i8xx_primary_formats;
  11120. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11121. modifiers = i9xx_format_modifiers;
  11122. primary->update_plane = i9xx_update_primary_plane;
  11123. primary->disable_plane = i9xx_disable_primary_plane;
  11124. }
  11125. if (INTEL_GEN(dev_priv) >= 9)
  11126. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11127. 0, &intel_plane_funcs,
  11128. intel_primary_formats, num_formats,
  11129. modifiers,
  11130. DRM_PLANE_TYPE_PRIMARY,
  11131. "plane 1%c", pipe_name(pipe));
  11132. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11133. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11134. 0, &intel_plane_funcs,
  11135. intel_primary_formats, num_formats,
  11136. modifiers,
  11137. DRM_PLANE_TYPE_PRIMARY,
  11138. "primary %c", pipe_name(pipe));
  11139. else
  11140. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11141. 0, &intel_plane_funcs,
  11142. intel_primary_formats, num_formats,
  11143. modifiers,
  11144. DRM_PLANE_TYPE_PRIMARY,
  11145. "plane %c", plane_name(primary->plane));
  11146. if (ret)
  11147. goto fail;
  11148. if (INTEL_GEN(dev_priv) >= 9) {
  11149. supported_rotations =
  11150. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11151. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11152. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11153. supported_rotations =
  11154. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11155. DRM_MODE_REFLECT_X;
  11156. } else if (INTEL_GEN(dev_priv) >= 4) {
  11157. supported_rotations =
  11158. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11159. } else {
  11160. supported_rotations = DRM_MODE_ROTATE_0;
  11161. }
  11162. if (INTEL_GEN(dev_priv) >= 4)
  11163. drm_plane_create_rotation_property(&primary->base,
  11164. DRM_MODE_ROTATE_0,
  11165. supported_rotations);
  11166. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11167. return primary;
  11168. fail:
  11169. kfree(state);
  11170. kfree(primary);
  11171. return ERR_PTR(ret);
  11172. }
  11173. static struct intel_plane *
  11174. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11175. enum pipe pipe)
  11176. {
  11177. struct intel_plane *cursor = NULL;
  11178. struct intel_plane_state *state = NULL;
  11179. int ret;
  11180. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11181. if (!cursor) {
  11182. ret = -ENOMEM;
  11183. goto fail;
  11184. }
  11185. state = intel_create_plane_state(&cursor->base);
  11186. if (!state) {
  11187. ret = -ENOMEM;
  11188. goto fail;
  11189. }
  11190. cursor->base.state = &state->base;
  11191. cursor->can_scale = false;
  11192. cursor->max_downscale = 1;
  11193. cursor->pipe = pipe;
  11194. cursor->plane = pipe;
  11195. cursor->id = PLANE_CURSOR;
  11196. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11197. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11198. cursor->update_plane = i845_update_cursor;
  11199. cursor->disable_plane = i845_disable_cursor;
  11200. cursor->check_plane = i845_check_cursor;
  11201. } else {
  11202. cursor->update_plane = i9xx_update_cursor;
  11203. cursor->disable_plane = i9xx_disable_cursor;
  11204. cursor->check_plane = i9xx_check_cursor;
  11205. }
  11206. cursor->cursor.base = ~0;
  11207. cursor->cursor.cntl = ~0;
  11208. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11209. cursor->cursor.size = ~0;
  11210. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11211. 0, &intel_cursor_plane_funcs,
  11212. intel_cursor_formats,
  11213. ARRAY_SIZE(intel_cursor_formats),
  11214. cursor_format_modifiers,
  11215. DRM_PLANE_TYPE_CURSOR,
  11216. "cursor %c", pipe_name(pipe));
  11217. if (ret)
  11218. goto fail;
  11219. if (INTEL_GEN(dev_priv) >= 4)
  11220. drm_plane_create_rotation_property(&cursor->base,
  11221. DRM_MODE_ROTATE_0,
  11222. DRM_MODE_ROTATE_0 |
  11223. DRM_MODE_ROTATE_180);
  11224. if (INTEL_GEN(dev_priv) >= 9)
  11225. state->scaler_id = -1;
  11226. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11227. return cursor;
  11228. fail:
  11229. kfree(state);
  11230. kfree(cursor);
  11231. return ERR_PTR(ret);
  11232. }
  11233. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11234. struct intel_crtc_state *crtc_state)
  11235. {
  11236. struct intel_crtc_scaler_state *scaler_state =
  11237. &crtc_state->scaler_state;
  11238. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11239. int i;
  11240. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11241. if (!crtc->num_scalers)
  11242. return;
  11243. for (i = 0; i < crtc->num_scalers; i++) {
  11244. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11245. scaler->in_use = 0;
  11246. scaler->mode = PS_SCALER_MODE_DYN;
  11247. }
  11248. scaler_state->scaler_id = -1;
  11249. }
  11250. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11251. {
  11252. struct intel_crtc *intel_crtc;
  11253. struct intel_crtc_state *crtc_state = NULL;
  11254. struct intel_plane *primary = NULL;
  11255. struct intel_plane *cursor = NULL;
  11256. int sprite, ret;
  11257. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11258. if (!intel_crtc)
  11259. return -ENOMEM;
  11260. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11261. if (!crtc_state) {
  11262. ret = -ENOMEM;
  11263. goto fail;
  11264. }
  11265. intel_crtc->config = crtc_state;
  11266. intel_crtc->base.state = &crtc_state->base;
  11267. crtc_state->base.crtc = &intel_crtc->base;
  11268. primary = intel_primary_plane_create(dev_priv, pipe);
  11269. if (IS_ERR(primary)) {
  11270. ret = PTR_ERR(primary);
  11271. goto fail;
  11272. }
  11273. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11274. for_each_sprite(dev_priv, pipe, sprite) {
  11275. struct intel_plane *plane;
  11276. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11277. if (IS_ERR(plane)) {
  11278. ret = PTR_ERR(plane);
  11279. goto fail;
  11280. }
  11281. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11282. }
  11283. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11284. if (IS_ERR(cursor)) {
  11285. ret = PTR_ERR(cursor);
  11286. goto fail;
  11287. }
  11288. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11289. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11290. &primary->base, &cursor->base,
  11291. &intel_crtc_funcs,
  11292. "pipe %c", pipe_name(pipe));
  11293. if (ret)
  11294. goto fail;
  11295. intel_crtc->pipe = pipe;
  11296. intel_crtc->plane = primary->plane;
  11297. /* initialize shared scalers */
  11298. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11299. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11300. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11301. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11302. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11303. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11304. intel_color_init(&intel_crtc->base);
  11305. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11306. return 0;
  11307. fail:
  11308. /*
  11309. * drm_mode_config_cleanup() will free up any
  11310. * crtcs/planes already initialized.
  11311. */
  11312. kfree(crtc_state);
  11313. kfree(intel_crtc);
  11314. return ret;
  11315. }
  11316. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11317. {
  11318. struct drm_device *dev = connector->base.dev;
  11319. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11320. if (!connector->base.state->crtc)
  11321. return INVALID_PIPE;
  11322. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11323. }
  11324. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11325. struct drm_file *file)
  11326. {
  11327. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11328. struct drm_crtc *drmmode_crtc;
  11329. struct intel_crtc *crtc;
  11330. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11331. if (!drmmode_crtc)
  11332. return -ENOENT;
  11333. crtc = to_intel_crtc(drmmode_crtc);
  11334. pipe_from_crtc_id->pipe = crtc->pipe;
  11335. return 0;
  11336. }
  11337. static int intel_encoder_clones(struct intel_encoder *encoder)
  11338. {
  11339. struct drm_device *dev = encoder->base.dev;
  11340. struct intel_encoder *source_encoder;
  11341. int index_mask = 0;
  11342. int entry = 0;
  11343. for_each_intel_encoder(dev, source_encoder) {
  11344. if (encoders_cloneable(encoder, source_encoder))
  11345. index_mask |= (1 << entry);
  11346. entry++;
  11347. }
  11348. return index_mask;
  11349. }
  11350. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11351. {
  11352. if (!IS_MOBILE(dev_priv))
  11353. return false;
  11354. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11355. return false;
  11356. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11357. return false;
  11358. return true;
  11359. }
  11360. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11361. {
  11362. if (INTEL_GEN(dev_priv) >= 9)
  11363. return false;
  11364. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11365. return false;
  11366. if (IS_CHERRYVIEW(dev_priv))
  11367. return false;
  11368. if (HAS_PCH_LPT_H(dev_priv) &&
  11369. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11370. return false;
  11371. /* DDI E can't be used if DDI A requires 4 lanes */
  11372. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11373. return false;
  11374. if (!dev_priv->vbt.int_crt_support)
  11375. return false;
  11376. return true;
  11377. }
  11378. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11379. {
  11380. int pps_num;
  11381. int pps_idx;
  11382. if (HAS_DDI(dev_priv))
  11383. return;
  11384. /*
  11385. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11386. * everywhere where registers can be write protected.
  11387. */
  11388. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11389. pps_num = 2;
  11390. else
  11391. pps_num = 1;
  11392. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11393. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11394. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11395. I915_WRITE(PP_CONTROL(pps_idx), val);
  11396. }
  11397. }
  11398. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11399. {
  11400. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11401. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11402. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11403. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11404. else
  11405. dev_priv->pps_mmio_base = PPS_BASE;
  11406. intel_pps_unlock_regs_wa(dev_priv);
  11407. }
  11408. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11409. {
  11410. struct intel_encoder *encoder;
  11411. bool dpd_is_edp = false;
  11412. intel_pps_init(dev_priv);
  11413. /*
  11414. * intel_edp_init_connector() depends on this completing first, to
  11415. * prevent the registeration of both eDP and LVDS and the incorrect
  11416. * sharing of the PPS.
  11417. */
  11418. intel_lvds_init(dev_priv);
  11419. if (intel_crt_present(dev_priv))
  11420. intel_crt_init(dev_priv);
  11421. if (IS_GEN9_LP(dev_priv)) {
  11422. /*
  11423. * FIXME: Broxton doesn't support port detection via the
  11424. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11425. * detect the ports.
  11426. */
  11427. intel_ddi_init(dev_priv, PORT_A);
  11428. intel_ddi_init(dev_priv, PORT_B);
  11429. intel_ddi_init(dev_priv, PORT_C);
  11430. intel_dsi_init(dev_priv);
  11431. } else if (HAS_DDI(dev_priv)) {
  11432. int found;
  11433. /*
  11434. * Haswell uses DDI functions to detect digital outputs.
  11435. * On SKL pre-D0 the strap isn't connected, so we assume
  11436. * it's there.
  11437. */
  11438. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11439. /* WaIgnoreDDIAStrap: skl */
  11440. if (found || IS_GEN9_BC(dev_priv))
  11441. intel_ddi_init(dev_priv, PORT_A);
  11442. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11443. * register */
  11444. found = I915_READ(SFUSE_STRAP);
  11445. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11446. intel_ddi_init(dev_priv, PORT_B);
  11447. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11448. intel_ddi_init(dev_priv, PORT_C);
  11449. if (found & SFUSE_STRAP_DDID_DETECTED)
  11450. intel_ddi_init(dev_priv, PORT_D);
  11451. /*
  11452. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11453. */
  11454. if (IS_GEN9_BC(dev_priv) &&
  11455. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11456. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11457. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11458. intel_ddi_init(dev_priv, PORT_E);
  11459. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11460. int found;
  11461. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11462. if (has_edp_a(dev_priv))
  11463. intel_dp_init(dev_priv, DP_A, PORT_A);
  11464. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11465. /* PCH SDVOB multiplex with HDMIB */
  11466. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11467. if (!found)
  11468. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11469. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11470. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11471. }
  11472. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11473. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11474. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11475. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11476. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11477. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11478. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11479. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11480. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11481. bool has_edp, has_port;
  11482. /*
  11483. * The DP_DETECTED bit is the latched state of the DDC
  11484. * SDA pin at boot. However since eDP doesn't require DDC
  11485. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11486. * eDP ports may have been muxed to an alternate function.
  11487. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11488. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11489. * detect eDP ports.
  11490. *
  11491. * Sadly the straps seem to be missing sometimes even for HDMI
  11492. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11493. * and VBT for the presence of the port. Additionally we can't
  11494. * trust the port type the VBT declares as we've seen at least
  11495. * HDMI ports that the VBT claim are DP or eDP.
  11496. */
  11497. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11498. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11499. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11500. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11501. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11502. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11503. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11504. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11505. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11506. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11507. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11508. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11509. if (IS_CHERRYVIEW(dev_priv)) {
  11510. /*
  11511. * eDP not supported on port D,
  11512. * so no need to worry about it
  11513. */
  11514. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11515. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11516. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11517. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11518. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11519. }
  11520. intel_dsi_init(dev_priv);
  11521. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11522. bool found = false;
  11523. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11524. DRM_DEBUG_KMS("probing SDVOB\n");
  11525. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11526. if (!found && IS_G4X(dev_priv)) {
  11527. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11528. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11529. }
  11530. if (!found && IS_G4X(dev_priv))
  11531. intel_dp_init(dev_priv, DP_B, PORT_B);
  11532. }
  11533. /* Before G4X SDVOC doesn't have its own detect register */
  11534. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11535. DRM_DEBUG_KMS("probing SDVOC\n");
  11536. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11537. }
  11538. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11539. if (IS_G4X(dev_priv)) {
  11540. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11541. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11542. }
  11543. if (IS_G4X(dev_priv))
  11544. intel_dp_init(dev_priv, DP_C, PORT_C);
  11545. }
  11546. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11547. intel_dp_init(dev_priv, DP_D, PORT_D);
  11548. } else if (IS_GEN2(dev_priv))
  11549. intel_dvo_init(dev_priv);
  11550. if (SUPPORTS_TV(dev_priv))
  11551. intel_tv_init(dev_priv);
  11552. intel_psr_init(dev_priv);
  11553. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11554. encoder->base.possible_crtcs = encoder->crtc_mask;
  11555. encoder->base.possible_clones =
  11556. intel_encoder_clones(encoder);
  11557. }
  11558. intel_init_pch_refclk(dev_priv);
  11559. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11560. }
  11561. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11562. {
  11563. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11564. drm_framebuffer_cleanup(fb);
  11565. i915_gem_object_lock(intel_fb->obj);
  11566. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11567. i915_gem_object_unlock(intel_fb->obj);
  11568. i915_gem_object_put(intel_fb->obj);
  11569. kfree(intel_fb);
  11570. }
  11571. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11572. struct drm_file *file,
  11573. unsigned int *handle)
  11574. {
  11575. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11576. struct drm_i915_gem_object *obj = intel_fb->obj;
  11577. if (obj->userptr.mm) {
  11578. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11579. return -EINVAL;
  11580. }
  11581. return drm_gem_handle_create(file, &obj->base, handle);
  11582. }
  11583. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11584. struct drm_file *file,
  11585. unsigned flags, unsigned color,
  11586. struct drm_clip_rect *clips,
  11587. unsigned num_clips)
  11588. {
  11589. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11590. i915_gem_object_flush_if_display(obj);
  11591. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11592. return 0;
  11593. }
  11594. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11595. .destroy = intel_user_framebuffer_destroy,
  11596. .create_handle = intel_user_framebuffer_create_handle,
  11597. .dirty = intel_user_framebuffer_dirty,
  11598. };
  11599. static
  11600. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11601. uint64_t fb_modifier, uint32_t pixel_format)
  11602. {
  11603. u32 gen = INTEL_GEN(dev_priv);
  11604. if (gen >= 9) {
  11605. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11606. /* "The stride in bytes must not exceed the of the size of 8K
  11607. * pixels and 32K bytes."
  11608. */
  11609. return min(8192 * cpp, 32768);
  11610. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11611. return 32*1024;
  11612. } else if (gen >= 4) {
  11613. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11614. return 16*1024;
  11615. else
  11616. return 32*1024;
  11617. } else if (gen >= 3) {
  11618. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11619. return 8*1024;
  11620. else
  11621. return 16*1024;
  11622. } else {
  11623. /* XXX DSPC is limited to 4k tiled */
  11624. return 8*1024;
  11625. }
  11626. }
  11627. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11628. struct drm_i915_gem_object *obj,
  11629. struct drm_mode_fb_cmd2 *mode_cmd)
  11630. {
  11631. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11632. struct drm_framebuffer *fb = &intel_fb->base;
  11633. struct drm_format_name_buf format_name;
  11634. u32 pitch_limit;
  11635. unsigned int tiling, stride;
  11636. int ret = -EINVAL;
  11637. int i;
  11638. i915_gem_object_lock(obj);
  11639. obj->framebuffer_references++;
  11640. tiling = i915_gem_object_get_tiling(obj);
  11641. stride = i915_gem_object_get_stride(obj);
  11642. i915_gem_object_unlock(obj);
  11643. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11644. /*
  11645. * If there's a fence, enforce that
  11646. * the fb modifier and tiling mode match.
  11647. */
  11648. if (tiling != I915_TILING_NONE &&
  11649. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11650. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11651. goto err;
  11652. }
  11653. } else {
  11654. if (tiling == I915_TILING_X) {
  11655. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11656. } else if (tiling == I915_TILING_Y) {
  11657. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11658. goto err;
  11659. }
  11660. }
  11661. /* Passed in modifier sanity checking. */
  11662. switch (mode_cmd->modifier[0]) {
  11663. case I915_FORMAT_MOD_Y_TILED_CCS:
  11664. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11665. switch (mode_cmd->pixel_format) {
  11666. case DRM_FORMAT_XBGR8888:
  11667. case DRM_FORMAT_ABGR8888:
  11668. case DRM_FORMAT_XRGB8888:
  11669. case DRM_FORMAT_ARGB8888:
  11670. break;
  11671. default:
  11672. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11673. goto err;
  11674. }
  11675. /* fall through */
  11676. case I915_FORMAT_MOD_Y_TILED:
  11677. case I915_FORMAT_MOD_Yf_TILED:
  11678. if (INTEL_GEN(dev_priv) < 9) {
  11679. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11680. mode_cmd->modifier[0]);
  11681. goto err;
  11682. }
  11683. case DRM_FORMAT_MOD_LINEAR:
  11684. case I915_FORMAT_MOD_X_TILED:
  11685. break;
  11686. default:
  11687. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11688. mode_cmd->modifier[0]);
  11689. goto err;
  11690. }
  11691. /*
  11692. * gen2/3 display engine uses the fence if present,
  11693. * so the tiling mode must match the fb modifier exactly.
  11694. */
  11695. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11696. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11697. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11698. goto err;
  11699. }
  11700. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11701. mode_cmd->pixel_format);
  11702. if (mode_cmd->pitches[0] > pitch_limit) {
  11703. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11704. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11705. "tiled" : "linear",
  11706. mode_cmd->pitches[0], pitch_limit);
  11707. goto err;
  11708. }
  11709. /*
  11710. * If there's a fence, enforce that
  11711. * the fb pitch and fence stride match.
  11712. */
  11713. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11714. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11715. mode_cmd->pitches[0], stride);
  11716. goto err;
  11717. }
  11718. /* Reject formats not supported by any plane early. */
  11719. switch (mode_cmd->pixel_format) {
  11720. case DRM_FORMAT_C8:
  11721. case DRM_FORMAT_RGB565:
  11722. case DRM_FORMAT_XRGB8888:
  11723. case DRM_FORMAT_ARGB8888:
  11724. break;
  11725. case DRM_FORMAT_XRGB1555:
  11726. if (INTEL_GEN(dev_priv) > 3) {
  11727. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11728. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11729. goto err;
  11730. }
  11731. break;
  11732. case DRM_FORMAT_ABGR8888:
  11733. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11734. INTEL_GEN(dev_priv) < 9) {
  11735. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11736. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11737. goto err;
  11738. }
  11739. break;
  11740. case DRM_FORMAT_XBGR8888:
  11741. case DRM_FORMAT_XRGB2101010:
  11742. case DRM_FORMAT_XBGR2101010:
  11743. if (INTEL_GEN(dev_priv) < 4) {
  11744. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11745. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11746. goto err;
  11747. }
  11748. break;
  11749. case DRM_FORMAT_ABGR2101010:
  11750. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11751. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11752. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11753. goto err;
  11754. }
  11755. break;
  11756. case DRM_FORMAT_YUYV:
  11757. case DRM_FORMAT_UYVY:
  11758. case DRM_FORMAT_YVYU:
  11759. case DRM_FORMAT_VYUY:
  11760. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11761. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11762. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11763. goto err;
  11764. }
  11765. break;
  11766. default:
  11767. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11768. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11769. goto err;
  11770. }
  11771. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11772. if (mode_cmd->offsets[0] != 0)
  11773. goto err;
  11774. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11775. for (i = 0; i < fb->format->num_planes; i++) {
  11776. u32 stride_alignment;
  11777. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11778. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11779. goto err;
  11780. }
  11781. stride_alignment = intel_fb_stride_alignment(fb, i);
  11782. /*
  11783. * Display WA #0531: skl,bxt,kbl,glk
  11784. *
  11785. * Render decompression and plane width > 3840
  11786. * combined with horizontal panning requires the
  11787. * plane stride to be a multiple of 4. We'll just
  11788. * require the entire fb to accommodate that to avoid
  11789. * potential runtime errors at plane configuration time.
  11790. */
  11791. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11792. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11793. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11794. stride_alignment *= 4;
  11795. if (fb->pitches[i] & (stride_alignment - 1)) {
  11796. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11797. i, fb->pitches[i], stride_alignment);
  11798. goto err;
  11799. }
  11800. }
  11801. intel_fb->obj = obj;
  11802. ret = intel_fill_fb_info(dev_priv, fb);
  11803. if (ret)
  11804. goto err;
  11805. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11806. if (ret) {
  11807. DRM_ERROR("framebuffer init failed %d\n", ret);
  11808. goto err;
  11809. }
  11810. return 0;
  11811. err:
  11812. i915_gem_object_lock(obj);
  11813. obj->framebuffer_references--;
  11814. i915_gem_object_unlock(obj);
  11815. return ret;
  11816. }
  11817. static struct drm_framebuffer *
  11818. intel_user_framebuffer_create(struct drm_device *dev,
  11819. struct drm_file *filp,
  11820. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11821. {
  11822. struct drm_framebuffer *fb;
  11823. struct drm_i915_gem_object *obj;
  11824. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11825. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11826. if (!obj)
  11827. return ERR_PTR(-ENOENT);
  11828. fb = intel_framebuffer_create(obj, &mode_cmd);
  11829. if (IS_ERR(fb))
  11830. i915_gem_object_put(obj);
  11831. return fb;
  11832. }
  11833. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11834. {
  11835. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11836. drm_atomic_state_default_release(state);
  11837. i915_sw_fence_fini(&intel_state->commit_ready);
  11838. kfree(state);
  11839. }
  11840. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11841. .fb_create = intel_user_framebuffer_create,
  11842. .get_format_info = intel_get_format_info,
  11843. .output_poll_changed = intel_fbdev_output_poll_changed,
  11844. .atomic_check = intel_atomic_check,
  11845. .atomic_commit = intel_atomic_commit,
  11846. .atomic_state_alloc = intel_atomic_state_alloc,
  11847. .atomic_state_clear = intel_atomic_state_clear,
  11848. .atomic_state_free = intel_atomic_state_free,
  11849. };
  11850. /**
  11851. * intel_init_display_hooks - initialize the display modesetting hooks
  11852. * @dev_priv: device private
  11853. */
  11854. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11855. {
  11856. intel_init_cdclk_hooks(dev_priv);
  11857. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11858. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11859. dev_priv->display.get_initial_plane_config =
  11860. skylake_get_initial_plane_config;
  11861. dev_priv->display.crtc_compute_clock =
  11862. haswell_crtc_compute_clock;
  11863. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11864. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11865. } else if (HAS_DDI(dev_priv)) {
  11866. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11867. dev_priv->display.get_initial_plane_config =
  11868. ironlake_get_initial_plane_config;
  11869. dev_priv->display.crtc_compute_clock =
  11870. haswell_crtc_compute_clock;
  11871. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11872. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11873. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11874. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11875. dev_priv->display.get_initial_plane_config =
  11876. ironlake_get_initial_plane_config;
  11877. dev_priv->display.crtc_compute_clock =
  11878. ironlake_crtc_compute_clock;
  11879. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11880. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11881. } else if (IS_CHERRYVIEW(dev_priv)) {
  11882. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11883. dev_priv->display.get_initial_plane_config =
  11884. i9xx_get_initial_plane_config;
  11885. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11886. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11887. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11888. } else if (IS_VALLEYVIEW(dev_priv)) {
  11889. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11890. dev_priv->display.get_initial_plane_config =
  11891. i9xx_get_initial_plane_config;
  11892. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11893. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11894. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11895. } else if (IS_G4X(dev_priv)) {
  11896. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11897. dev_priv->display.get_initial_plane_config =
  11898. i9xx_get_initial_plane_config;
  11899. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11900. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11901. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11902. } else if (IS_PINEVIEW(dev_priv)) {
  11903. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11904. dev_priv->display.get_initial_plane_config =
  11905. i9xx_get_initial_plane_config;
  11906. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11907. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11908. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11909. } else if (!IS_GEN2(dev_priv)) {
  11910. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11911. dev_priv->display.get_initial_plane_config =
  11912. i9xx_get_initial_plane_config;
  11913. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11914. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11915. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11916. } else {
  11917. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11918. dev_priv->display.get_initial_plane_config =
  11919. i9xx_get_initial_plane_config;
  11920. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11921. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11922. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11923. }
  11924. if (IS_GEN5(dev_priv)) {
  11925. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11926. } else if (IS_GEN6(dev_priv)) {
  11927. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11928. } else if (IS_IVYBRIDGE(dev_priv)) {
  11929. /* FIXME: detect B0+ stepping and use auto training */
  11930. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11931. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11932. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11933. }
  11934. if (INTEL_GEN(dev_priv) >= 9)
  11935. dev_priv->display.update_crtcs = skl_update_crtcs;
  11936. else
  11937. dev_priv->display.update_crtcs = intel_update_crtcs;
  11938. }
  11939. /*
  11940. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11941. */
  11942. static void quirk_ssc_force_disable(struct drm_device *dev)
  11943. {
  11944. struct drm_i915_private *dev_priv = to_i915(dev);
  11945. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11946. DRM_INFO("applying lvds SSC disable quirk\n");
  11947. }
  11948. /*
  11949. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11950. * brightness value
  11951. */
  11952. static void quirk_invert_brightness(struct drm_device *dev)
  11953. {
  11954. struct drm_i915_private *dev_priv = to_i915(dev);
  11955. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11956. DRM_INFO("applying inverted panel brightness quirk\n");
  11957. }
  11958. /* Some VBT's incorrectly indicate no backlight is present */
  11959. static void quirk_backlight_present(struct drm_device *dev)
  11960. {
  11961. struct drm_i915_private *dev_priv = to_i915(dev);
  11962. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11963. DRM_INFO("applying backlight present quirk\n");
  11964. }
  11965. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11966. * which is 300 ms greater than eDP spec T12 min.
  11967. */
  11968. static void quirk_increase_t12_delay(struct drm_device *dev)
  11969. {
  11970. struct drm_i915_private *dev_priv = to_i915(dev);
  11971. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11972. DRM_INFO("Applying T12 delay quirk\n");
  11973. }
  11974. struct intel_quirk {
  11975. int device;
  11976. int subsystem_vendor;
  11977. int subsystem_device;
  11978. void (*hook)(struct drm_device *dev);
  11979. };
  11980. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11981. struct intel_dmi_quirk {
  11982. void (*hook)(struct drm_device *dev);
  11983. const struct dmi_system_id (*dmi_id_list)[];
  11984. };
  11985. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11986. {
  11987. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11988. return 1;
  11989. }
  11990. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11991. {
  11992. .dmi_id_list = &(const struct dmi_system_id[]) {
  11993. {
  11994. .callback = intel_dmi_reverse_brightness,
  11995. .ident = "NCR Corporation",
  11996. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11997. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11998. },
  11999. },
  12000. { } /* terminating entry */
  12001. },
  12002. .hook = quirk_invert_brightness,
  12003. },
  12004. };
  12005. static struct intel_quirk intel_quirks[] = {
  12006. /* Lenovo U160 cannot use SSC on LVDS */
  12007. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12008. /* Sony Vaio Y cannot use SSC on LVDS */
  12009. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12010. /* Acer Aspire 5734Z must invert backlight brightness */
  12011. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12012. /* Acer/eMachines G725 */
  12013. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12014. /* Acer/eMachines e725 */
  12015. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12016. /* Acer/Packard Bell NCL20 */
  12017. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12018. /* Acer Aspire 4736Z */
  12019. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12020. /* Acer Aspire 5336 */
  12021. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12022. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12023. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12024. /* Acer C720 Chromebook (Core i3 4005U) */
  12025. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12026. /* Apple Macbook 2,1 (Core 2 T7400) */
  12027. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12028. /* Apple Macbook 4,1 */
  12029. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12030. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12031. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12032. /* HP Chromebook 14 (Celeron 2955U) */
  12033. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12034. /* Dell Chromebook 11 */
  12035. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12036. /* Dell Chromebook 11 (2015 version) */
  12037. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12038. /* Toshiba Satellite P50-C-18C */
  12039. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12040. };
  12041. static void intel_init_quirks(struct drm_device *dev)
  12042. {
  12043. struct pci_dev *d = dev->pdev;
  12044. int i;
  12045. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12046. struct intel_quirk *q = &intel_quirks[i];
  12047. if (d->device == q->device &&
  12048. (d->subsystem_vendor == q->subsystem_vendor ||
  12049. q->subsystem_vendor == PCI_ANY_ID) &&
  12050. (d->subsystem_device == q->subsystem_device ||
  12051. q->subsystem_device == PCI_ANY_ID))
  12052. q->hook(dev);
  12053. }
  12054. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12055. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12056. intel_dmi_quirks[i].hook(dev);
  12057. }
  12058. }
  12059. /* Disable the VGA plane that we never use */
  12060. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12061. {
  12062. struct pci_dev *pdev = dev_priv->drm.pdev;
  12063. u8 sr1;
  12064. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12065. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12066. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12067. outb(SR01, VGA_SR_INDEX);
  12068. sr1 = inb(VGA_SR_DATA);
  12069. outb(sr1 | 1<<5, VGA_SR_DATA);
  12070. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12071. udelay(300);
  12072. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12073. POSTING_READ(vga_reg);
  12074. }
  12075. void intel_modeset_init_hw(struct drm_device *dev)
  12076. {
  12077. struct drm_i915_private *dev_priv = to_i915(dev);
  12078. intel_update_cdclk(dev_priv);
  12079. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12080. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12081. }
  12082. /*
  12083. * Calculate what we think the watermarks should be for the state we've read
  12084. * out of the hardware and then immediately program those watermarks so that
  12085. * we ensure the hardware settings match our internal state.
  12086. *
  12087. * We can calculate what we think WM's should be by creating a duplicate of the
  12088. * current state (which was constructed during hardware readout) and running it
  12089. * through the atomic check code to calculate new watermark values in the
  12090. * state object.
  12091. */
  12092. static void sanitize_watermarks(struct drm_device *dev)
  12093. {
  12094. struct drm_i915_private *dev_priv = to_i915(dev);
  12095. struct drm_atomic_state *state;
  12096. struct intel_atomic_state *intel_state;
  12097. struct drm_crtc *crtc;
  12098. struct drm_crtc_state *cstate;
  12099. struct drm_modeset_acquire_ctx ctx;
  12100. int ret;
  12101. int i;
  12102. /* Only supported on platforms that use atomic watermark design */
  12103. if (!dev_priv->display.optimize_watermarks)
  12104. return;
  12105. /*
  12106. * We need to hold connection_mutex before calling duplicate_state so
  12107. * that the connector loop is protected.
  12108. */
  12109. drm_modeset_acquire_init(&ctx, 0);
  12110. retry:
  12111. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12112. if (ret == -EDEADLK) {
  12113. drm_modeset_backoff(&ctx);
  12114. goto retry;
  12115. } else if (WARN_ON(ret)) {
  12116. goto fail;
  12117. }
  12118. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12119. if (WARN_ON(IS_ERR(state)))
  12120. goto fail;
  12121. intel_state = to_intel_atomic_state(state);
  12122. /*
  12123. * Hardware readout is the only time we don't want to calculate
  12124. * intermediate watermarks (since we don't trust the current
  12125. * watermarks).
  12126. */
  12127. if (!HAS_GMCH_DISPLAY(dev_priv))
  12128. intel_state->skip_intermediate_wm = true;
  12129. ret = intel_atomic_check(dev, state);
  12130. if (ret) {
  12131. /*
  12132. * If we fail here, it means that the hardware appears to be
  12133. * programmed in a way that shouldn't be possible, given our
  12134. * understanding of watermark requirements. This might mean a
  12135. * mistake in the hardware readout code or a mistake in the
  12136. * watermark calculations for a given platform. Raise a WARN
  12137. * so that this is noticeable.
  12138. *
  12139. * If this actually happens, we'll have to just leave the
  12140. * BIOS-programmed watermarks untouched and hope for the best.
  12141. */
  12142. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12143. goto put_state;
  12144. }
  12145. /* Write calculated watermark values back */
  12146. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12147. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12148. cs->wm.need_postvbl_update = true;
  12149. dev_priv->display.optimize_watermarks(intel_state, cs);
  12150. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12151. }
  12152. put_state:
  12153. drm_atomic_state_put(state);
  12154. fail:
  12155. drm_modeset_drop_locks(&ctx);
  12156. drm_modeset_acquire_fini(&ctx);
  12157. }
  12158. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12159. {
  12160. if (IS_GEN5(dev_priv)) {
  12161. u32 fdi_pll_clk =
  12162. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12163. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12164. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12165. dev_priv->fdi_pll_freq = 270000;
  12166. } else {
  12167. return;
  12168. }
  12169. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12170. }
  12171. int intel_modeset_init(struct drm_device *dev)
  12172. {
  12173. struct drm_i915_private *dev_priv = to_i915(dev);
  12174. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12175. enum pipe pipe;
  12176. struct intel_crtc *crtc;
  12177. drm_mode_config_init(dev);
  12178. dev->mode_config.min_width = 0;
  12179. dev->mode_config.min_height = 0;
  12180. dev->mode_config.preferred_depth = 24;
  12181. dev->mode_config.prefer_shadow = 1;
  12182. dev->mode_config.allow_fb_modifiers = true;
  12183. dev->mode_config.funcs = &intel_mode_funcs;
  12184. init_llist_head(&dev_priv->atomic_helper.free_list);
  12185. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12186. intel_atomic_helper_free_state_worker);
  12187. intel_init_quirks(dev);
  12188. intel_init_pm(dev_priv);
  12189. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12190. return 0;
  12191. /*
  12192. * There may be no VBT; and if the BIOS enabled SSC we can
  12193. * just keep using it to avoid unnecessary flicker. Whereas if the
  12194. * BIOS isn't using it, don't assume it will work even if the VBT
  12195. * indicates as much.
  12196. */
  12197. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12198. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12199. DREF_SSC1_ENABLE);
  12200. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12201. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12202. bios_lvds_use_ssc ? "en" : "dis",
  12203. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12204. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12205. }
  12206. }
  12207. if (IS_GEN2(dev_priv)) {
  12208. dev->mode_config.max_width = 2048;
  12209. dev->mode_config.max_height = 2048;
  12210. } else if (IS_GEN3(dev_priv)) {
  12211. dev->mode_config.max_width = 4096;
  12212. dev->mode_config.max_height = 4096;
  12213. } else {
  12214. dev->mode_config.max_width = 8192;
  12215. dev->mode_config.max_height = 8192;
  12216. }
  12217. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12218. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12219. dev->mode_config.cursor_height = 1023;
  12220. } else if (IS_GEN2(dev_priv)) {
  12221. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12222. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12223. } else {
  12224. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12225. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12226. }
  12227. dev->mode_config.fb_base = ggtt->mappable_base;
  12228. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12229. INTEL_INFO(dev_priv)->num_pipes,
  12230. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12231. for_each_pipe(dev_priv, pipe) {
  12232. int ret;
  12233. ret = intel_crtc_init(dev_priv, pipe);
  12234. if (ret) {
  12235. drm_mode_config_cleanup(dev);
  12236. return ret;
  12237. }
  12238. }
  12239. intel_shared_dpll_init(dev);
  12240. intel_update_fdi_pll_freq(dev_priv);
  12241. intel_update_czclk(dev_priv);
  12242. intel_modeset_init_hw(dev);
  12243. if (dev_priv->max_cdclk_freq == 0)
  12244. intel_update_max_cdclk(dev_priv);
  12245. /* Just disable it once at startup */
  12246. i915_disable_vga(dev_priv);
  12247. intel_setup_outputs(dev_priv);
  12248. drm_modeset_lock_all(dev);
  12249. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12250. drm_modeset_unlock_all(dev);
  12251. for_each_intel_crtc(dev, crtc) {
  12252. struct intel_initial_plane_config plane_config = {};
  12253. if (!crtc->active)
  12254. continue;
  12255. /*
  12256. * Note that reserving the BIOS fb up front prevents us
  12257. * from stuffing other stolen allocations like the ring
  12258. * on top. This prevents some ugliness at boot time, and
  12259. * can even allow for smooth boot transitions if the BIOS
  12260. * fb is large enough for the active pipe configuration.
  12261. */
  12262. dev_priv->display.get_initial_plane_config(crtc,
  12263. &plane_config);
  12264. /*
  12265. * If the fb is shared between multiple heads, we'll
  12266. * just get the first one.
  12267. */
  12268. intel_find_initial_plane_obj(crtc, &plane_config);
  12269. }
  12270. /*
  12271. * Make sure hardware watermarks really match the state we read out.
  12272. * Note that we need to do this after reconstructing the BIOS fb's
  12273. * since the watermark calculation done here will use pstate->fb.
  12274. */
  12275. if (!HAS_GMCH_DISPLAY(dev_priv))
  12276. sanitize_watermarks(dev);
  12277. return 0;
  12278. }
  12279. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12280. {
  12281. /* 640x480@60Hz, ~25175 kHz */
  12282. struct dpll clock = {
  12283. .m1 = 18,
  12284. .m2 = 7,
  12285. .p1 = 13,
  12286. .p2 = 4,
  12287. .n = 2,
  12288. };
  12289. u32 dpll, fp;
  12290. int i;
  12291. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12292. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12293. pipe_name(pipe), clock.vco, clock.dot);
  12294. fp = i9xx_dpll_compute_fp(&clock);
  12295. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12296. DPLL_VGA_MODE_DIS |
  12297. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12298. PLL_P2_DIVIDE_BY_4 |
  12299. PLL_REF_INPUT_DREFCLK |
  12300. DPLL_VCO_ENABLE;
  12301. I915_WRITE(FP0(pipe), fp);
  12302. I915_WRITE(FP1(pipe), fp);
  12303. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12304. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12305. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12306. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12307. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12308. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12309. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12310. /*
  12311. * Apparently we need to have VGA mode enabled prior to changing
  12312. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12313. * dividers, even though the register value does change.
  12314. */
  12315. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12316. I915_WRITE(DPLL(pipe), dpll);
  12317. /* Wait for the clocks to stabilize. */
  12318. POSTING_READ(DPLL(pipe));
  12319. udelay(150);
  12320. /* The pixel multiplier can only be updated once the
  12321. * DPLL is enabled and the clocks are stable.
  12322. *
  12323. * So write it again.
  12324. */
  12325. I915_WRITE(DPLL(pipe), dpll);
  12326. /* We do this three times for luck */
  12327. for (i = 0; i < 3 ; i++) {
  12328. I915_WRITE(DPLL(pipe), dpll);
  12329. POSTING_READ(DPLL(pipe));
  12330. udelay(150); /* wait for warmup */
  12331. }
  12332. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12333. POSTING_READ(PIPECONF(pipe));
  12334. }
  12335. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12336. {
  12337. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12338. pipe_name(pipe));
  12339. assert_plane_disabled(dev_priv, PLANE_A);
  12340. assert_plane_disabled(dev_priv, PLANE_B);
  12341. I915_WRITE(PIPECONF(pipe), 0);
  12342. POSTING_READ(PIPECONF(pipe));
  12343. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  12344. DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
  12345. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12346. POSTING_READ(DPLL(pipe));
  12347. }
  12348. static bool
  12349. intel_check_plane_mapping(struct intel_crtc *crtc)
  12350. {
  12351. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12352. u32 val;
  12353. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12354. return true;
  12355. val = I915_READ(DSPCNTR(!crtc->plane));
  12356. if ((val & DISPLAY_PLANE_ENABLE) &&
  12357. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12358. return false;
  12359. return true;
  12360. }
  12361. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12362. {
  12363. struct drm_device *dev = crtc->base.dev;
  12364. struct intel_encoder *encoder;
  12365. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12366. return true;
  12367. return false;
  12368. }
  12369. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12370. {
  12371. struct drm_device *dev = encoder->base.dev;
  12372. struct intel_connector *connector;
  12373. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12374. return connector;
  12375. return NULL;
  12376. }
  12377. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12378. enum pipe pch_transcoder)
  12379. {
  12380. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12381. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12382. }
  12383. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12384. struct drm_modeset_acquire_ctx *ctx)
  12385. {
  12386. struct drm_device *dev = crtc->base.dev;
  12387. struct drm_i915_private *dev_priv = to_i915(dev);
  12388. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12389. /* Clear any frame start delays used for debugging left by the BIOS */
  12390. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12391. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12392. I915_WRITE(reg,
  12393. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12394. }
  12395. /* restore vblank interrupts to correct state */
  12396. drm_crtc_vblank_reset(&crtc->base);
  12397. if (crtc->active) {
  12398. struct intel_plane *plane;
  12399. drm_crtc_vblank_on(&crtc->base);
  12400. /* Disable everything but the primary plane */
  12401. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12402. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12403. continue;
  12404. trace_intel_disable_plane(&plane->base, crtc);
  12405. plane->disable_plane(plane, crtc);
  12406. }
  12407. }
  12408. /* We need to sanitize the plane -> pipe mapping first because this will
  12409. * disable the crtc (and hence change the state) if it is wrong. Note
  12410. * that gen4+ has a fixed plane -> pipe mapping. */
  12411. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12412. bool plane;
  12413. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12414. crtc->base.base.id, crtc->base.name);
  12415. /* Pipe has the wrong plane attached and the plane is active.
  12416. * Temporarily change the plane mapping and disable everything
  12417. * ... */
  12418. plane = crtc->plane;
  12419. crtc->base.primary->state->visible = true;
  12420. crtc->plane = !plane;
  12421. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12422. crtc->plane = plane;
  12423. }
  12424. /* Adjust the state of the output pipe according to whether we
  12425. * have active connectors/encoders. */
  12426. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12427. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12428. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12429. /*
  12430. * We start out with underrun reporting disabled to avoid races.
  12431. * For correct bookkeeping mark this on active crtcs.
  12432. *
  12433. * Also on gmch platforms we dont have any hardware bits to
  12434. * disable the underrun reporting. Which means we need to start
  12435. * out with underrun reporting disabled also on inactive pipes,
  12436. * since otherwise we'll complain about the garbage we read when
  12437. * e.g. coming up after runtime pm.
  12438. *
  12439. * No protection against concurrent access is required - at
  12440. * worst a fifo underrun happens which also sets this to false.
  12441. */
  12442. crtc->cpu_fifo_underrun_disabled = true;
  12443. /*
  12444. * We track the PCH trancoder underrun reporting state
  12445. * within the crtc. With crtc for pipe A housing the underrun
  12446. * reporting state for PCH transcoder A, crtc for pipe B housing
  12447. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12448. * and marking underrun reporting as disabled for the non-existing
  12449. * PCH transcoders B and C would prevent enabling the south
  12450. * error interrupt (see cpt_can_enable_serr_int()).
  12451. */
  12452. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12453. crtc->pch_fifo_underrun_disabled = true;
  12454. }
  12455. }
  12456. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12457. {
  12458. struct intel_connector *connector;
  12459. /* We need to check both for a crtc link (meaning that the
  12460. * encoder is active and trying to read from a pipe) and the
  12461. * pipe itself being active. */
  12462. bool has_active_crtc = encoder->base.crtc &&
  12463. to_intel_crtc(encoder->base.crtc)->active;
  12464. connector = intel_encoder_find_connector(encoder);
  12465. if (connector && !has_active_crtc) {
  12466. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12467. encoder->base.base.id,
  12468. encoder->base.name);
  12469. /* Connector is active, but has no active pipe. This is
  12470. * fallout from our resume register restoring. Disable
  12471. * the encoder manually again. */
  12472. if (encoder->base.crtc) {
  12473. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12474. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12475. encoder->base.base.id,
  12476. encoder->base.name);
  12477. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12478. if (encoder->post_disable)
  12479. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12480. }
  12481. encoder->base.crtc = NULL;
  12482. /* Inconsistent output/port/pipe state happens presumably due to
  12483. * a bug in one of the get_hw_state functions. Or someplace else
  12484. * in our code, like the register restore mess on resume. Clamp
  12485. * things to off as a safer default. */
  12486. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12487. connector->base.encoder = NULL;
  12488. }
  12489. /* Enabled encoders without active connectors will be fixed in
  12490. * the crtc fixup. */
  12491. }
  12492. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12493. {
  12494. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12495. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12496. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12497. i915_disable_vga(dev_priv);
  12498. }
  12499. }
  12500. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12501. {
  12502. /* This function can be called both from intel_modeset_setup_hw_state or
  12503. * at a very early point in our resume sequence, where the power well
  12504. * structures are not yet restored. Since this function is at a very
  12505. * paranoid "someone might have enabled VGA while we were not looking"
  12506. * level, just check if the power well is enabled instead of trying to
  12507. * follow the "don't touch the power well if we don't need it" policy
  12508. * the rest of the driver uses. */
  12509. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12510. return;
  12511. i915_redisable_vga_power_on(dev_priv);
  12512. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12513. }
  12514. static bool primary_get_hw_state(struct intel_plane *plane)
  12515. {
  12516. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12517. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12518. }
  12519. /* FIXME read out full plane state for all planes */
  12520. static void readout_plane_state(struct intel_crtc *crtc)
  12521. {
  12522. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12523. bool visible;
  12524. visible = crtc->active && primary_get_hw_state(primary);
  12525. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12526. to_intel_plane_state(primary->base.state),
  12527. visible);
  12528. }
  12529. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12530. {
  12531. struct drm_i915_private *dev_priv = to_i915(dev);
  12532. enum pipe pipe;
  12533. struct intel_crtc *crtc;
  12534. struct intel_encoder *encoder;
  12535. struct intel_connector *connector;
  12536. struct drm_connector_list_iter conn_iter;
  12537. int i;
  12538. dev_priv->active_crtcs = 0;
  12539. for_each_intel_crtc(dev, crtc) {
  12540. struct intel_crtc_state *crtc_state =
  12541. to_intel_crtc_state(crtc->base.state);
  12542. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12543. memset(crtc_state, 0, sizeof(*crtc_state));
  12544. crtc_state->base.crtc = &crtc->base;
  12545. crtc_state->base.active = crtc_state->base.enable =
  12546. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12547. crtc->base.enabled = crtc_state->base.enable;
  12548. crtc->active = crtc_state->base.active;
  12549. if (crtc_state->base.active)
  12550. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12551. readout_plane_state(crtc);
  12552. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12553. crtc->base.base.id, crtc->base.name,
  12554. enableddisabled(crtc_state->base.active));
  12555. }
  12556. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12557. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12558. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12559. &pll->state.hw_state);
  12560. pll->state.crtc_mask = 0;
  12561. for_each_intel_crtc(dev, crtc) {
  12562. struct intel_crtc_state *crtc_state =
  12563. to_intel_crtc_state(crtc->base.state);
  12564. if (crtc_state->base.active &&
  12565. crtc_state->shared_dpll == pll)
  12566. pll->state.crtc_mask |= 1 << crtc->pipe;
  12567. }
  12568. pll->active_mask = pll->state.crtc_mask;
  12569. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12570. pll->name, pll->state.crtc_mask, pll->on);
  12571. }
  12572. for_each_intel_encoder(dev, encoder) {
  12573. pipe = 0;
  12574. if (encoder->get_hw_state(encoder, &pipe)) {
  12575. struct intel_crtc_state *crtc_state;
  12576. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12577. crtc_state = to_intel_crtc_state(crtc->base.state);
  12578. encoder->base.crtc = &crtc->base;
  12579. encoder->get_config(encoder, crtc_state);
  12580. } else {
  12581. encoder->base.crtc = NULL;
  12582. }
  12583. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12584. encoder->base.base.id, encoder->base.name,
  12585. enableddisabled(encoder->base.crtc),
  12586. pipe_name(pipe));
  12587. }
  12588. drm_connector_list_iter_begin(dev, &conn_iter);
  12589. for_each_intel_connector_iter(connector, &conn_iter) {
  12590. if (connector->get_hw_state(connector)) {
  12591. connector->base.dpms = DRM_MODE_DPMS_ON;
  12592. encoder = connector->encoder;
  12593. connector->base.encoder = &encoder->base;
  12594. if (encoder->base.crtc &&
  12595. encoder->base.crtc->state->active) {
  12596. /*
  12597. * This has to be done during hardware readout
  12598. * because anything calling .crtc_disable may
  12599. * rely on the connector_mask being accurate.
  12600. */
  12601. encoder->base.crtc->state->connector_mask |=
  12602. 1 << drm_connector_index(&connector->base);
  12603. encoder->base.crtc->state->encoder_mask |=
  12604. 1 << drm_encoder_index(&encoder->base);
  12605. }
  12606. } else {
  12607. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12608. connector->base.encoder = NULL;
  12609. }
  12610. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12611. connector->base.base.id, connector->base.name,
  12612. enableddisabled(connector->base.encoder));
  12613. }
  12614. drm_connector_list_iter_end(&conn_iter);
  12615. for_each_intel_crtc(dev, crtc) {
  12616. struct intel_crtc_state *crtc_state =
  12617. to_intel_crtc_state(crtc->base.state);
  12618. int min_cdclk = 0;
  12619. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12620. if (crtc_state->base.active) {
  12621. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12622. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12623. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12624. /*
  12625. * The initial mode needs to be set in order to keep
  12626. * the atomic core happy. It wants a valid mode if the
  12627. * crtc's enabled, so we do the above call.
  12628. *
  12629. * But we don't set all the derived state fully, hence
  12630. * set a flag to indicate that a full recalculation is
  12631. * needed on the next commit.
  12632. */
  12633. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12634. intel_crtc_compute_pixel_rate(crtc_state);
  12635. if (dev_priv->display.modeset_calc_cdclk) {
  12636. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12637. if (WARN_ON(min_cdclk < 0))
  12638. min_cdclk = 0;
  12639. }
  12640. drm_calc_timestamping_constants(&crtc->base,
  12641. &crtc_state->base.adjusted_mode);
  12642. update_scanline_offset(crtc);
  12643. }
  12644. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12645. dev_priv->min_voltage_level[crtc->pipe] =
  12646. crtc_state->min_voltage_level;
  12647. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12648. }
  12649. }
  12650. static void
  12651. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12652. {
  12653. struct intel_encoder *encoder;
  12654. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12655. u64 get_domains;
  12656. enum intel_display_power_domain domain;
  12657. if (!encoder->get_power_domains)
  12658. continue;
  12659. get_domains = encoder->get_power_domains(encoder);
  12660. for_each_power_domain(domain, get_domains)
  12661. intel_display_power_get(dev_priv, domain);
  12662. }
  12663. }
  12664. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12665. {
  12666. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12667. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12668. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12669. DARBF_GATING_DIS);
  12670. if (IS_HASWELL(dev_priv)) {
  12671. /*
  12672. * WaRsPkgCStateDisplayPMReq:hsw
  12673. * System hang if this isn't done before disabling all planes!
  12674. */
  12675. I915_WRITE(CHICKEN_PAR1_1,
  12676. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12677. }
  12678. }
  12679. /* Scan out the current hw modeset state,
  12680. * and sanitizes it to the current state
  12681. */
  12682. static void
  12683. intel_modeset_setup_hw_state(struct drm_device *dev,
  12684. struct drm_modeset_acquire_ctx *ctx)
  12685. {
  12686. struct drm_i915_private *dev_priv = to_i915(dev);
  12687. enum pipe pipe;
  12688. struct intel_crtc *crtc;
  12689. struct intel_encoder *encoder;
  12690. int i;
  12691. intel_early_display_was(dev_priv);
  12692. intel_modeset_readout_hw_state(dev);
  12693. /* HW state is read out, now we need to sanitize this mess. */
  12694. get_encoder_power_domains(dev_priv);
  12695. for_each_intel_encoder(dev, encoder) {
  12696. intel_sanitize_encoder(encoder);
  12697. }
  12698. for_each_pipe(dev_priv, pipe) {
  12699. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12700. intel_sanitize_crtc(crtc, ctx);
  12701. intel_dump_pipe_config(crtc, crtc->config,
  12702. "[setup_hw_state]");
  12703. }
  12704. intel_modeset_update_connector_atomic_state(dev);
  12705. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12706. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12707. if (!pll->on || pll->active_mask)
  12708. continue;
  12709. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12710. pll->funcs.disable(dev_priv, pll);
  12711. pll->on = false;
  12712. }
  12713. if (IS_G4X(dev_priv)) {
  12714. g4x_wm_get_hw_state(dev);
  12715. g4x_wm_sanitize(dev_priv);
  12716. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12717. vlv_wm_get_hw_state(dev);
  12718. vlv_wm_sanitize(dev_priv);
  12719. } else if (INTEL_GEN(dev_priv) >= 9) {
  12720. skl_wm_get_hw_state(dev);
  12721. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12722. ilk_wm_get_hw_state(dev);
  12723. }
  12724. for_each_intel_crtc(dev, crtc) {
  12725. u64 put_domains;
  12726. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12727. if (WARN_ON(put_domains))
  12728. modeset_put_power_domains(dev_priv, put_domains);
  12729. }
  12730. intel_display_set_init_power(dev_priv, false);
  12731. intel_power_domains_verify_state(dev_priv);
  12732. intel_fbc_init_pipe_state(dev_priv);
  12733. }
  12734. void intel_display_resume(struct drm_device *dev)
  12735. {
  12736. struct drm_i915_private *dev_priv = to_i915(dev);
  12737. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12738. struct drm_modeset_acquire_ctx ctx;
  12739. int ret;
  12740. dev_priv->modeset_restore_state = NULL;
  12741. if (state)
  12742. state->acquire_ctx = &ctx;
  12743. drm_modeset_acquire_init(&ctx, 0);
  12744. while (1) {
  12745. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12746. if (ret != -EDEADLK)
  12747. break;
  12748. drm_modeset_backoff(&ctx);
  12749. }
  12750. if (!ret)
  12751. ret = __intel_display_resume(dev, state, &ctx);
  12752. intel_enable_ipc(dev_priv);
  12753. drm_modeset_drop_locks(&ctx);
  12754. drm_modeset_acquire_fini(&ctx);
  12755. if (ret)
  12756. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12757. if (state)
  12758. drm_atomic_state_put(state);
  12759. }
  12760. int intel_connector_register(struct drm_connector *connector)
  12761. {
  12762. struct intel_connector *intel_connector = to_intel_connector(connector);
  12763. int ret;
  12764. ret = intel_backlight_device_register(intel_connector);
  12765. if (ret)
  12766. goto err;
  12767. return 0;
  12768. err:
  12769. return ret;
  12770. }
  12771. void intel_connector_unregister(struct drm_connector *connector)
  12772. {
  12773. struct intel_connector *intel_connector = to_intel_connector(connector);
  12774. intel_backlight_device_unregister(intel_connector);
  12775. intel_panel_destroy_backlight(connector);
  12776. }
  12777. static void intel_hpd_poll_fini(struct drm_device *dev)
  12778. {
  12779. struct intel_connector *connector;
  12780. struct drm_connector_list_iter conn_iter;
  12781. /* First disable polling... */
  12782. drm_kms_helper_poll_fini(dev);
  12783. /* Then kill the work that may have been queued by hpd. */
  12784. drm_connector_list_iter_begin(dev, &conn_iter);
  12785. for_each_intel_connector_iter(connector, &conn_iter) {
  12786. if (connector->modeset_retry_work.func)
  12787. cancel_work_sync(&connector->modeset_retry_work);
  12788. }
  12789. drm_connector_list_iter_end(&conn_iter);
  12790. }
  12791. void intel_modeset_cleanup(struct drm_device *dev)
  12792. {
  12793. struct drm_i915_private *dev_priv = to_i915(dev);
  12794. flush_work(&dev_priv->atomic_helper.free_work);
  12795. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12796. intel_disable_gt_powersave(dev_priv);
  12797. /*
  12798. * Interrupts and polling as the first thing to avoid creating havoc.
  12799. * Too much stuff here (turning of connectors, ...) would
  12800. * experience fancy races otherwise.
  12801. */
  12802. intel_irq_uninstall(dev_priv);
  12803. /*
  12804. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12805. * poll handlers. Hence disable polling after hpd handling is shut down.
  12806. */
  12807. intel_hpd_poll_fini(dev);
  12808. /* poll work can call into fbdev, hence clean that up afterwards */
  12809. intel_fbdev_fini(dev_priv);
  12810. intel_unregister_dsm_handler();
  12811. intel_fbc_global_disable(dev_priv);
  12812. /* flush any delayed tasks or pending work */
  12813. flush_scheduled_work();
  12814. drm_mode_config_cleanup(dev);
  12815. intel_cleanup_overlay(dev_priv);
  12816. intel_cleanup_gt_powersave(dev_priv);
  12817. intel_teardown_gmbus(dev_priv);
  12818. }
  12819. void intel_connector_attach_encoder(struct intel_connector *connector,
  12820. struct intel_encoder *encoder)
  12821. {
  12822. connector->encoder = encoder;
  12823. drm_mode_connector_attach_encoder(&connector->base,
  12824. &encoder->base);
  12825. }
  12826. /*
  12827. * set vga decode state - true == enable VGA decode
  12828. */
  12829. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12830. {
  12831. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12832. u16 gmch_ctrl;
  12833. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12834. DRM_ERROR("failed to read control word\n");
  12835. return -EIO;
  12836. }
  12837. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12838. return 0;
  12839. if (state)
  12840. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12841. else
  12842. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12843. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12844. DRM_ERROR("failed to write control word\n");
  12845. return -EIO;
  12846. }
  12847. return 0;
  12848. }
  12849. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12850. struct intel_display_error_state {
  12851. u32 power_well_driver;
  12852. int num_transcoders;
  12853. struct intel_cursor_error_state {
  12854. u32 control;
  12855. u32 position;
  12856. u32 base;
  12857. u32 size;
  12858. } cursor[I915_MAX_PIPES];
  12859. struct intel_pipe_error_state {
  12860. bool power_domain_on;
  12861. u32 source;
  12862. u32 stat;
  12863. } pipe[I915_MAX_PIPES];
  12864. struct intel_plane_error_state {
  12865. u32 control;
  12866. u32 stride;
  12867. u32 size;
  12868. u32 pos;
  12869. u32 addr;
  12870. u32 surface;
  12871. u32 tile_offset;
  12872. } plane[I915_MAX_PIPES];
  12873. struct intel_transcoder_error_state {
  12874. bool power_domain_on;
  12875. enum transcoder cpu_transcoder;
  12876. u32 conf;
  12877. u32 htotal;
  12878. u32 hblank;
  12879. u32 hsync;
  12880. u32 vtotal;
  12881. u32 vblank;
  12882. u32 vsync;
  12883. } transcoder[4];
  12884. };
  12885. struct intel_display_error_state *
  12886. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12887. {
  12888. struct intel_display_error_state *error;
  12889. int transcoders[] = {
  12890. TRANSCODER_A,
  12891. TRANSCODER_B,
  12892. TRANSCODER_C,
  12893. TRANSCODER_EDP,
  12894. };
  12895. int i;
  12896. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12897. return NULL;
  12898. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12899. if (error == NULL)
  12900. return NULL;
  12901. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12902. error->power_well_driver =
  12903. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12904. for_each_pipe(dev_priv, i) {
  12905. error->pipe[i].power_domain_on =
  12906. __intel_display_power_is_enabled(dev_priv,
  12907. POWER_DOMAIN_PIPE(i));
  12908. if (!error->pipe[i].power_domain_on)
  12909. continue;
  12910. error->cursor[i].control = I915_READ(CURCNTR(i));
  12911. error->cursor[i].position = I915_READ(CURPOS(i));
  12912. error->cursor[i].base = I915_READ(CURBASE(i));
  12913. error->plane[i].control = I915_READ(DSPCNTR(i));
  12914. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12915. if (INTEL_GEN(dev_priv) <= 3) {
  12916. error->plane[i].size = I915_READ(DSPSIZE(i));
  12917. error->plane[i].pos = I915_READ(DSPPOS(i));
  12918. }
  12919. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12920. error->plane[i].addr = I915_READ(DSPADDR(i));
  12921. if (INTEL_GEN(dev_priv) >= 4) {
  12922. error->plane[i].surface = I915_READ(DSPSURF(i));
  12923. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12924. }
  12925. error->pipe[i].source = I915_READ(PIPESRC(i));
  12926. if (HAS_GMCH_DISPLAY(dev_priv))
  12927. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12928. }
  12929. /* Note: this does not include DSI transcoders. */
  12930. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12931. if (HAS_DDI(dev_priv))
  12932. error->num_transcoders++; /* Account for eDP. */
  12933. for (i = 0; i < error->num_transcoders; i++) {
  12934. enum transcoder cpu_transcoder = transcoders[i];
  12935. error->transcoder[i].power_domain_on =
  12936. __intel_display_power_is_enabled(dev_priv,
  12937. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12938. if (!error->transcoder[i].power_domain_on)
  12939. continue;
  12940. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12941. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12942. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12943. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12944. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12945. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12946. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12947. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12948. }
  12949. return error;
  12950. }
  12951. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12952. void
  12953. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12954. struct intel_display_error_state *error)
  12955. {
  12956. struct drm_i915_private *dev_priv = m->i915;
  12957. int i;
  12958. if (!error)
  12959. return;
  12960. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12961. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12962. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12963. error->power_well_driver);
  12964. for_each_pipe(dev_priv, i) {
  12965. err_printf(m, "Pipe [%d]:\n", i);
  12966. err_printf(m, " Power: %s\n",
  12967. onoff(error->pipe[i].power_domain_on));
  12968. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12969. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12970. err_printf(m, "Plane [%d]:\n", i);
  12971. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12972. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12973. if (INTEL_GEN(dev_priv) <= 3) {
  12974. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12975. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12976. }
  12977. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12978. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12979. if (INTEL_GEN(dev_priv) >= 4) {
  12980. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12981. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12982. }
  12983. err_printf(m, "Cursor [%d]:\n", i);
  12984. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12985. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12986. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12987. }
  12988. for (i = 0; i < error->num_transcoders; i++) {
  12989. err_printf(m, "CPU transcoder: %s\n",
  12990. transcoder_name(error->transcoder[i].cpu_transcoder));
  12991. err_printf(m, " Power: %s\n",
  12992. onoff(error->transcoder[i].power_domain_on));
  12993. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12994. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12995. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12996. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12997. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12998. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12999. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13000. }
  13001. }
  13002. #endif