intel_display.c 436 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint64_t skl_format_modifiers_noccs[] = {
  86. I915_FORMAT_MOD_Yf_TILED,
  87. I915_FORMAT_MOD_Y_TILED,
  88. I915_FORMAT_MOD_X_TILED,
  89. DRM_FORMAT_MOD_LINEAR,
  90. DRM_FORMAT_MOD_INVALID
  91. };
  92. static const uint64_t skl_format_modifiers_ccs[] = {
  93. I915_FORMAT_MOD_Yf_TILED_CCS,
  94. I915_FORMAT_MOD_Y_TILED_CCS,
  95. I915_FORMAT_MOD_Yf_TILED,
  96. I915_FORMAT_MOD_Y_TILED,
  97. I915_FORMAT_MOD_X_TILED,
  98. DRM_FORMAT_MOD_LINEAR,
  99. DRM_FORMAT_MOD_INVALID
  100. };
  101. /* Cursor formats */
  102. static const uint32_t intel_cursor_formats[] = {
  103. DRM_FORMAT_ARGB8888,
  104. };
  105. static const uint64_t cursor_format_modifiers[] = {
  106. DRM_FORMAT_MOD_LINEAR,
  107. DRM_FORMAT_MOD_INVALID
  108. };
  109. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  110. struct intel_crtc_state *pipe_config);
  111. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  112. struct intel_crtc_state *pipe_config);
  113. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  114. struct drm_i915_gem_object *obj,
  115. struct drm_mode_fb_cmd2 *mode_cmd);
  116. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  117. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  118. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  119. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  120. struct intel_link_m_n *m_n,
  121. struct intel_link_m_n *m2_n2);
  122. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  123. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  124. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  125. static void vlv_prepare_pll(struct intel_crtc *crtc,
  126. const struct intel_crtc_state *pipe_config);
  127. static void chv_prepare_pll(struct intel_crtc *crtc,
  128. const struct intel_crtc_state *pipe_config);
  129. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  130. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  131. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  132. struct intel_crtc_state *crtc_state);
  133. static void skylake_pfit_enable(struct intel_crtc *crtc);
  134. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  135. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  136. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  137. struct drm_modeset_acquire_ctx *ctx);
  138. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  139. struct intel_limit {
  140. struct {
  141. int min, max;
  142. } dot, vco, n, m, m1, m2, p, p1;
  143. struct {
  144. int dot_limit;
  145. int p2_slow, p2_fast;
  146. } p2;
  147. };
  148. /* returns HPLL frequency in kHz */
  149. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  150. {
  151. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  152. /* Obtain SKU information */
  153. mutex_lock(&dev_priv->sb_lock);
  154. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  155. CCK_FUSE_HPLL_FREQ_MASK;
  156. mutex_unlock(&dev_priv->sb_lock);
  157. return vco_freq[hpll_freq] * 1000;
  158. }
  159. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  160. const char *name, u32 reg, int ref_freq)
  161. {
  162. u32 val;
  163. int divider;
  164. mutex_lock(&dev_priv->sb_lock);
  165. val = vlv_cck_read(dev_priv, reg);
  166. mutex_unlock(&dev_priv->sb_lock);
  167. divider = val & CCK_FREQUENCY_VALUES;
  168. WARN((val & CCK_FREQUENCY_STATUS) !=
  169. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  170. "%s change in progress\n", name);
  171. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  172. }
  173. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  174. const char *name, u32 reg)
  175. {
  176. if (dev_priv->hpll_freq == 0)
  177. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  178. return vlv_get_cck_clock(dev_priv, name, reg,
  179. dev_priv->hpll_freq);
  180. }
  181. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  182. {
  183. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  184. return;
  185. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  186. CCK_CZ_CLOCK_CONTROL);
  187. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  188. }
  189. static inline u32 /* units of 100MHz */
  190. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  191. const struct intel_crtc_state *pipe_config)
  192. {
  193. if (HAS_DDI(dev_priv))
  194. return pipe_config->port_clock; /* SPLL */
  195. else
  196. return dev_priv->fdi_pll_freq;
  197. }
  198. static const struct intel_limit intel_limits_i8xx_dac = {
  199. .dot = { .min = 25000, .max = 350000 },
  200. .vco = { .min = 908000, .max = 1512000 },
  201. .n = { .min = 2, .max = 16 },
  202. .m = { .min = 96, .max = 140 },
  203. .m1 = { .min = 18, .max = 26 },
  204. .m2 = { .min = 6, .max = 16 },
  205. .p = { .min = 4, .max = 128 },
  206. .p1 = { .min = 2, .max = 33 },
  207. .p2 = { .dot_limit = 165000,
  208. .p2_slow = 4, .p2_fast = 2 },
  209. };
  210. static const struct intel_limit intel_limits_i8xx_dvo = {
  211. .dot = { .min = 25000, .max = 350000 },
  212. .vco = { .min = 908000, .max = 1512000 },
  213. .n = { .min = 2, .max = 16 },
  214. .m = { .min = 96, .max = 140 },
  215. .m1 = { .min = 18, .max = 26 },
  216. .m2 = { .min = 6, .max = 16 },
  217. .p = { .min = 4, .max = 128 },
  218. .p1 = { .min = 2, .max = 33 },
  219. .p2 = { .dot_limit = 165000,
  220. .p2_slow = 4, .p2_fast = 4 },
  221. };
  222. static const struct intel_limit intel_limits_i8xx_lvds = {
  223. .dot = { .min = 25000, .max = 350000 },
  224. .vco = { .min = 908000, .max = 1512000 },
  225. .n = { .min = 2, .max = 16 },
  226. .m = { .min = 96, .max = 140 },
  227. .m1 = { .min = 18, .max = 26 },
  228. .m2 = { .min = 6, .max = 16 },
  229. .p = { .min = 4, .max = 128 },
  230. .p1 = { .min = 1, .max = 6 },
  231. .p2 = { .dot_limit = 165000,
  232. .p2_slow = 14, .p2_fast = 7 },
  233. };
  234. static const struct intel_limit intel_limits_i9xx_sdvo = {
  235. .dot = { .min = 20000, .max = 400000 },
  236. .vco = { .min = 1400000, .max = 2800000 },
  237. .n = { .min = 1, .max = 6 },
  238. .m = { .min = 70, .max = 120 },
  239. .m1 = { .min = 8, .max = 18 },
  240. .m2 = { .min = 3, .max = 7 },
  241. .p = { .min = 5, .max = 80 },
  242. .p1 = { .min = 1, .max = 8 },
  243. .p2 = { .dot_limit = 200000,
  244. .p2_slow = 10, .p2_fast = 5 },
  245. };
  246. static const struct intel_limit intel_limits_i9xx_lvds = {
  247. .dot = { .min = 20000, .max = 400000 },
  248. .vco = { .min = 1400000, .max = 2800000 },
  249. .n = { .min = 1, .max = 6 },
  250. .m = { .min = 70, .max = 120 },
  251. .m1 = { .min = 8, .max = 18 },
  252. .m2 = { .min = 3, .max = 7 },
  253. .p = { .min = 7, .max = 98 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 112000,
  256. .p2_slow = 14, .p2_fast = 7 },
  257. };
  258. static const struct intel_limit intel_limits_g4x_sdvo = {
  259. .dot = { .min = 25000, .max = 270000 },
  260. .vco = { .min = 1750000, .max = 3500000},
  261. .n = { .min = 1, .max = 4 },
  262. .m = { .min = 104, .max = 138 },
  263. .m1 = { .min = 17, .max = 23 },
  264. .m2 = { .min = 5, .max = 11 },
  265. .p = { .min = 10, .max = 30 },
  266. .p1 = { .min = 1, .max = 3},
  267. .p2 = { .dot_limit = 270000,
  268. .p2_slow = 10,
  269. .p2_fast = 10
  270. },
  271. };
  272. static const struct intel_limit intel_limits_g4x_hdmi = {
  273. .dot = { .min = 22000, .max = 400000 },
  274. .vco = { .min = 1750000, .max = 3500000},
  275. .n = { .min = 1, .max = 4 },
  276. .m = { .min = 104, .max = 138 },
  277. .m1 = { .min = 16, .max = 23 },
  278. .m2 = { .min = 5, .max = 11 },
  279. .p = { .min = 5, .max = 80 },
  280. .p1 = { .min = 1, .max = 8},
  281. .p2 = { .dot_limit = 165000,
  282. .p2_slow = 10, .p2_fast = 5 },
  283. };
  284. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  285. .dot = { .min = 20000, .max = 115000 },
  286. .vco = { .min = 1750000, .max = 3500000 },
  287. .n = { .min = 1, .max = 3 },
  288. .m = { .min = 104, .max = 138 },
  289. .m1 = { .min = 17, .max = 23 },
  290. .m2 = { .min = 5, .max = 11 },
  291. .p = { .min = 28, .max = 112 },
  292. .p1 = { .min = 2, .max = 8 },
  293. .p2 = { .dot_limit = 0,
  294. .p2_slow = 14, .p2_fast = 14
  295. },
  296. };
  297. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  298. .dot = { .min = 80000, .max = 224000 },
  299. .vco = { .min = 1750000, .max = 3500000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 104, .max = 138 },
  302. .m1 = { .min = 17, .max = 23 },
  303. .m2 = { .min = 5, .max = 11 },
  304. .p = { .min = 14, .max = 42 },
  305. .p1 = { .min = 2, .max = 6 },
  306. .p2 = { .dot_limit = 0,
  307. .p2_slow = 7, .p2_fast = 7
  308. },
  309. };
  310. static const struct intel_limit intel_limits_pineview_sdvo = {
  311. .dot = { .min = 20000, .max = 400000},
  312. .vco = { .min = 1700000, .max = 3500000 },
  313. /* Pineview's Ncounter is a ring counter */
  314. .n = { .min = 3, .max = 6 },
  315. .m = { .min = 2, .max = 256 },
  316. /* Pineview only has one combined m divider, which we treat as m2. */
  317. .m1 = { .min = 0, .max = 0 },
  318. .m2 = { .min = 0, .max = 254 },
  319. .p = { .min = 5, .max = 80 },
  320. .p1 = { .min = 1, .max = 8 },
  321. .p2 = { .dot_limit = 200000,
  322. .p2_slow = 10, .p2_fast = 5 },
  323. };
  324. static const struct intel_limit intel_limits_pineview_lvds = {
  325. .dot = { .min = 20000, .max = 400000 },
  326. .vco = { .min = 1700000, .max = 3500000 },
  327. .n = { .min = 3, .max = 6 },
  328. .m = { .min = 2, .max = 256 },
  329. .m1 = { .min = 0, .max = 0 },
  330. .m2 = { .min = 0, .max = 254 },
  331. .p = { .min = 7, .max = 112 },
  332. .p1 = { .min = 1, .max = 8 },
  333. .p2 = { .dot_limit = 112000,
  334. .p2_slow = 14, .p2_fast = 14 },
  335. };
  336. /* Ironlake / Sandybridge
  337. *
  338. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  339. * the range value for them is (actual_value - 2).
  340. */
  341. static const struct intel_limit intel_limits_ironlake_dac = {
  342. .dot = { .min = 25000, .max = 350000 },
  343. .vco = { .min = 1760000, .max = 3510000 },
  344. .n = { .min = 1, .max = 5 },
  345. .m = { .min = 79, .max = 127 },
  346. .m1 = { .min = 12, .max = 22 },
  347. .m2 = { .min = 5, .max = 9 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 225000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  354. .dot = { .min = 25000, .max = 350000 },
  355. .vco = { .min = 1760000, .max = 3510000 },
  356. .n = { .min = 1, .max = 3 },
  357. .m = { .min = 79, .max = 118 },
  358. .m1 = { .min = 12, .max = 22 },
  359. .m2 = { .min = 5, .max = 9 },
  360. .p = { .min = 28, .max = 112 },
  361. .p1 = { .min = 2, .max = 8 },
  362. .p2 = { .dot_limit = 225000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  366. .dot = { .min = 25000, .max = 350000 },
  367. .vco = { .min = 1760000, .max = 3510000 },
  368. .n = { .min = 1, .max = 3 },
  369. .m = { .min = 79, .max = 127 },
  370. .m1 = { .min = 12, .max = 22 },
  371. .m2 = { .min = 5, .max = 9 },
  372. .p = { .min = 14, .max = 56 },
  373. .p1 = { .min = 2, .max = 8 },
  374. .p2 = { .dot_limit = 225000,
  375. .p2_slow = 7, .p2_fast = 7 },
  376. };
  377. /* LVDS 100mhz refclk limits. */
  378. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  379. .dot = { .min = 25000, .max = 350000 },
  380. .vco = { .min = 1760000, .max = 3510000 },
  381. .n = { .min = 1, .max = 2 },
  382. .m = { .min = 79, .max = 126 },
  383. .m1 = { .min = 12, .max = 22 },
  384. .m2 = { .min = 5, .max = 9 },
  385. .p = { .min = 28, .max = 112 },
  386. .p1 = { .min = 2, .max = 8 },
  387. .p2 = { .dot_limit = 225000,
  388. .p2_slow = 14, .p2_fast = 14 },
  389. };
  390. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  391. .dot = { .min = 25000, .max = 350000 },
  392. .vco = { .min = 1760000, .max = 3510000 },
  393. .n = { .min = 1, .max = 3 },
  394. .m = { .min = 79, .max = 126 },
  395. .m1 = { .min = 12, .max = 22 },
  396. .m2 = { .min = 5, .max = 9 },
  397. .p = { .min = 14, .max = 42 },
  398. .p1 = { .min = 2, .max = 6 },
  399. .p2 = { .dot_limit = 225000,
  400. .p2_slow = 7, .p2_fast = 7 },
  401. };
  402. static const struct intel_limit intel_limits_vlv = {
  403. /*
  404. * These are the data rate limits (measured in fast clocks)
  405. * since those are the strictest limits we have. The fast
  406. * clock and actual rate limits are more relaxed, so checking
  407. * them would make no difference.
  408. */
  409. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  410. .vco = { .min = 4000000, .max = 6000000 },
  411. .n = { .min = 1, .max = 7 },
  412. .m1 = { .min = 2, .max = 3 },
  413. .m2 = { .min = 11, .max = 156 },
  414. .p1 = { .min = 2, .max = 3 },
  415. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  416. };
  417. static const struct intel_limit intel_limits_chv = {
  418. /*
  419. * These are the data rate limits (measured in fast clocks)
  420. * since those are the strictest limits we have. The fast
  421. * clock and actual rate limits are more relaxed, so checking
  422. * them would make no difference.
  423. */
  424. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  425. .vco = { .min = 4800000, .max = 6480000 },
  426. .n = { .min = 1, .max = 1 },
  427. .m1 = { .min = 2, .max = 2 },
  428. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  429. .p1 = { .min = 2, .max = 4 },
  430. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  431. };
  432. static const struct intel_limit intel_limits_bxt = {
  433. /* FIXME: find real dot limits */
  434. .dot = { .min = 0, .max = INT_MAX },
  435. .vco = { .min = 4800000, .max = 6700000 },
  436. .n = { .min = 1, .max = 1 },
  437. .m1 = { .min = 2, .max = 2 },
  438. /* FIXME: find real m2 limits */
  439. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  440. .p1 = { .min = 2, .max = 4 },
  441. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  442. };
  443. static bool
  444. needs_modeset(const struct drm_crtc_state *state)
  445. {
  446. return drm_atomic_crtc_needs_modeset(state);
  447. }
  448. /*
  449. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  450. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  451. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  452. * The helpers' return value is the rate of the clock that is fed to the
  453. * display engine's pipe which can be the above fast dot clock rate or a
  454. * divided-down version of it.
  455. */
  456. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  457. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  458. {
  459. clock->m = clock->m2 + 2;
  460. clock->p = clock->p1 * clock->p2;
  461. if (WARN_ON(clock->n == 0 || clock->p == 0))
  462. return 0;
  463. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  464. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  465. return clock->dot;
  466. }
  467. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  468. {
  469. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  470. }
  471. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = i9xx_dpll_compute_m(clock);
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  478. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  479. return clock->dot;
  480. }
  481. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  482. {
  483. clock->m = clock->m1 * clock->m2;
  484. clock->p = clock->p1 * clock->p2;
  485. if (WARN_ON(clock->n == 0 || clock->p == 0))
  486. return 0;
  487. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  488. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  489. return clock->dot / 5;
  490. }
  491. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m1 * clock->m2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  498. clock->n << 22);
  499. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  500. return clock->dot / 5;
  501. }
  502. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  503. /**
  504. * Returns whether the given set of divisors are valid for a given refclk with
  505. * the given connectors.
  506. */
  507. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  508. const struct intel_limit *limit,
  509. const struct dpll *clock)
  510. {
  511. if (clock->n < limit->n.min || limit->n.max < clock->n)
  512. INTELPllInvalid("n out of range\n");
  513. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  514. INTELPllInvalid("p1 out of range\n");
  515. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  516. INTELPllInvalid("m2 out of range\n");
  517. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  518. INTELPllInvalid("m1 out of range\n");
  519. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  520. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  521. if (clock->m1 <= clock->m2)
  522. INTELPllInvalid("m1 <= m2\n");
  523. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  524. !IS_GEN9_LP(dev_priv)) {
  525. if (clock->p < limit->p.min || limit->p.max < clock->p)
  526. INTELPllInvalid("p out of range\n");
  527. if (clock->m < limit->m.min || limit->m.max < clock->m)
  528. INTELPllInvalid("m out of range\n");
  529. }
  530. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  531. INTELPllInvalid("vco out of range\n");
  532. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  533. * connector, etc., rather than just a single range.
  534. */
  535. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  536. INTELPllInvalid("dot out of range\n");
  537. return true;
  538. }
  539. static int
  540. i9xx_select_p2_div(const struct intel_limit *limit,
  541. const struct intel_crtc_state *crtc_state,
  542. int target)
  543. {
  544. struct drm_device *dev = crtc_state->base.crtc->dev;
  545. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  546. /*
  547. * For LVDS just rely on its current settings for dual-channel.
  548. * We haven't figured out how to reliably set up different
  549. * single/dual channel state, if we even can.
  550. */
  551. if (intel_is_dual_link_lvds(dev))
  552. return limit->p2.p2_fast;
  553. else
  554. return limit->p2.p2_slow;
  555. } else {
  556. if (target < limit->p2.dot_limit)
  557. return limit->p2.p2_slow;
  558. else
  559. return limit->p2.p2_fast;
  560. }
  561. }
  562. /*
  563. * Returns a set of divisors for the desired target clock with the given
  564. * refclk, or FALSE. The returned values represent the clock equation:
  565. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  566. *
  567. * Target and reference clocks are specified in kHz.
  568. *
  569. * If match_clock is provided, then best_clock P divider must match the P
  570. * divider from @match_clock used for LVDS downclocking.
  571. */
  572. static bool
  573. i9xx_find_best_dpll(const struct intel_limit *limit,
  574. struct intel_crtc_state *crtc_state,
  575. int target, int refclk, struct dpll *match_clock,
  576. struct dpll *best_clock)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. struct dpll clock;
  580. int err = target;
  581. memset(best_clock, 0, sizeof(*best_clock));
  582. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  583. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  584. clock.m1++) {
  585. for (clock.m2 = limit->m2.min;
  586. clock.m2 <= limit->m2.max; clock.m2++) {
  587. if (clock.m2 >= clock.m1)
  588. break;
  589. for (clock.n = limit->n.min;
  590. clock.n <= limit->n.max; clock.n++) {
  591. for (clock.p1 = limit->p1.min;
  592. clock.p1 <= limit->p1.max; clock.p1++) {
  593. int this_err;
  594. i9xx_calc_dpll_params(refclk, &clock);
  595. if (!intel_PLL_is_valid(to_i915(dev),
  596. limit,
  597. &clock))
  598. continue;
  599. if (match_clock &&
  600. clock.p != match_clock->p)
  601. continue;
  602. this_err = abs(clock.dot - target);
  603. if (this_err < err) {
  604. *best_clock = clock;
  605. err = this_err;
  606. }
  607. }
  608. }
  609. }
  610. }
  611. return (err != target);
  612. }
  613. /*
  614. * Returns a set of divisors for the desired target clock with the given
  615. * refclk, or FALSE. The returned values represent the clock equation:
  616. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  617. *
  618. * Target and reference clocks are specified in kHz.
  619. *
  620. * If match_clock is provided, then best_clock P divider must match the P
  621. * divider from @match_clock used for LVDS downclocking.
  622. */
  623. static bool
  624. pnv_find_best_dpll(const struct intel_limit *limit,
  625. struct intel_crtc_state *crtc_state,
  626. int target, int refclk, struct dpll *match_clock,
  627. struct dpll *best_clock)
  628. {
  629. struct drm_device *dev = crtc_state->base.crtc->dev;
  630. struct dpll clock;
  631. int err = target;
  632. memset(best_clock, 0, sizeof(*best_clock));
  633. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  634. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  635. clock.m1++) {
  636. for (clock.m2 = limit->m2.min;
  637. clock.m2 <= limit->m2.max; clock.m2++) {
  638. for (clock.n = limit->n.min;
  639. clock.n <= limit->n.max; clock.n++) {
  640. for (clock.p1 = limit->p1.min;
  641. clock.p1 <= limit->p1.max; clock.p1++) {
  642. int this_err;
  643. pnv_calc_dpll_params(refclk, &clock);
  644. if (!intel_PLL_is_valid(to_i915(dev),
  645. limit,
  646. &clock))
  647. continue;
  648. if (match_clock &&
  649. clock.p != match_clock->p)
  650. continue;
  651. this_err = abs(clock.dot - target);
  652. if (this_err < err) {
  653. *best_clock = clock;
  654. err = this_err;
  655. }
  656. }
  657. }
  658. }
  659. }
  660. return (err != target);
  661. }
  662. /*
  663. * Returns a set of divisors for the desired target clock with the given
  664. * refclk, or FALSE. The returned values represent the clock equation:
  665. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  666. *
  667. * Target and reference clocks are specified in kHz.
  668. *
  669. * If match_clock is provided, then best_clock P divider must match the P
  670. * divider from @match_clock used for LVDS downclocking.
  671. */
  672. static bool
  673. g4x_find_best_dpll(const struct intel_limit *limit,
  674. struct intel_crtc_state *crtc_state,
  675. int target, int refclk, struct dpll *match_clock,
  676. struct dpll *best_clock)
  677. {
  678. struct drm_device *dev = crtc_state->base.crtc->dev;
  679. struct dpll clock;
  680. int max_n;
  681. bool found = false;
  682. /* approximately equals target * 0.00585 */
  683. int err_most = (target >> 8) + (target >> 9);
  684. memset(best_clock, 0, sizeof(*best_clock));
  685. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  686. max_n = limit->n.max;
  687. /* based on hardware requirement, prefer smaller n to precision */
  688. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  689. /* based on hardware requirement, prefere larger m1,m2 */
  690. for (clock.m1 = limit->m1.max;
  691. clock.m1 >= limit->m1.min; clock.m1--) {
  692. for (clock.m2 = limit->m2.max;
  693. clock.m2 >= limit->m2.min; clock.m2--) {
  694. for (clock.p1 = limit->p1.max;
  695. clock.p1 >= limit->p1.min; clock.p1--) {
  696. int this_err;
  697. i9xx_calc_dpll_params(refclk, &clock);
  698. if (!intel_PLL_is_valid(to_i915(dev),
  699. limit,
  700. &clock))
  701. continue;
  702. this_err = abs(clock.dot - target);
  703. if (this_err < err_most) {
  704. *best_clock = clock;
  705. err_most = this_err;
  706. max_n = clock.n;
  707. found = true;
  708. }
  709. }
  710. }
  711. }
  712. }
  713. return found;
  714. }
  715. /*
  716. * Check if the calculated PLL configuration is more optimal compared to the
  717. * best configuration and error found so far. Return the calculated error.
  718. */
  719. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  720. const struct dpll *calculated_clock,
  721. const struct dpll *best_clock,
  722. unsigned int best_error_ppm,
  723. unsigned int *error_ppm)
  724. {
  725. /*
  726. * For CHV ignore the error and consider only the P value.
  727. * Prefer a bigger P value based on HW requirements.
  728. */
  729. if (IS_CHERRYVIEW(to_i915(dev))) {
  730. *error_ppm = 0;
  731. return calculated_clock->p > best_clock->p;
  732. }
  733. if (WARN_ON_ONCE(!target_freq))
  734. return false;
  735. *error_ppm = div_u64(1000000ULL *
  736. abs(target_freq - calculated_clock->dot),
  737. target_freq);
  738. /*
  739. * Prefer a better P value over a better (smaller) error if the error
  740. * is small. Ensure this preference for future configurations too by
  741. * setting the error to 0.
  742. */
  743. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  744. *error_ppm = 0;
  745. return true;
  746. }
  747. return *error_ppm + 10 < best_error_ppm;
  748. }
  749. /*
  750. * Returns a set of divisors for the desired target clock with the given
  751. * refclk, or FALSE. The returned values represent the clock equation:
  752. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  753. */
  754. static bool
  755. vlv_find_best_dpll(const struct intel_limit *limit,
  756. struct intel_crtc_state *crtc_state,
  757. int target, int refclk, struct dpll *match_clock,
  758. struct dpll *best_clock)
  759. {
  760. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  761. struct drm_device *dev = crtc->base.dev;
  762. struct dpll clock;
  763. unsigned int bestppm = 1000000;
  764. /* min update 19.2 MHz */
  765. int max_n = min(limit->n.max, refclk / 19200);
  766. bool found = false;
  767. target *= 5; /* fast clock */
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. /* based on hardware requirement, prefer smaller n to precision */
  770. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  771. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  772. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  773. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  774. clock.p = clock.p1 * clock.p2;
  775. /* based on hardware requirement, prefer bigger m1,m2 values */
  776. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  777. unsigned int ppm;
  778. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  779. refclk * clock.m1);
  780. vlv_calc_dpll_params(refclk, &clock);
  781. if (!intel_PLL_is_valid(to_i915(dev),
  782. limit,
  783. &clock))
  784. continue;
  785. if (!vlv_PLL_is_optimal(dev, target,
  786. &clock,
  787. best_clock,
  788. bestppm, &ppm))
  789. continue;
  790. *best_clock = clock;
  791. bestppm = ppm;
  792. found = true;
  793. }
  794. }
  795. }
  796. }
  797. return found;
  798. }
  799. /*
  800. * Returns a set of divisors for the desired target clock with the given
  801. * refclk, or FALSE. The returned values represent the clock equation:
  802. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  803. */
  804. static bool
  805. chv_find_best_dpll(const struct intel_limit *limit,
  806. struct intel_crtc_state *crtc_state,
  807. int target, int refclk, struct dpll *match_clock,
  808. struct dpll *best_clock)
  809. {
  810. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  811. struct drm_device *dev = crtc->base.dev;
  812. unsigned int best_error_ppm;
  813. struct dpll clock;
  814. uint64_t m2;
  815. int found = false;
  816. memset(best_clock, 0, sizeof(*best_clock));
  817. best_error_ppm = 1000000;
  818. /*
  819. * Based on hardware doc, the n always set to 1, and m1 always
  820. * set to 2. If requires to support 200Mhz refclk, we need to
  821. * revisit this because n may not 1 anymore.
  822. */
  823. clock.n = 1, clock.m1 = 2;
  824. target *= 5; /* fast clock */
  825. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  826. for (clock.p2 = limit->p2.p2_fast;
  827. clock.p2 >= limit->p2.p2_slow;
  828. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  829. unsigned int error_ppm;
  830. clock.p = clock.p1 * clock.p2;
  831. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  832. clock.n) << 22, refclk * clock.m1);
  833. if (m2 > INT_MAX/clock.m1)
  834. continue;
  835. clock.m2 = m2;
  836. chv_calc_dpll_params(refclk, &clock);
  837. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  838. continue;
  839. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  840. best_error_ppm, &error_ppm))
  841. continue;
  842. *best_clock = clock;
  843. best_error_ppm = error_ppm;
  844. found = true;
  845. }
  846. }
  847. return found;
  848. }
  849. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  850. struct dpll *best_clock)
  851. {
  852. int refclk = 100000;
  853. const struct intel_limit *limit = &intel_limits_bxt;
  854. return chv_find_best_dpll(limit, crtc_state,
  855. target_clock, refclk, NULL, best_clock);
  856. }
  857. bool intel_crtc_active(struct intel_crtc *crtc)
  858. {
  859. /* Be paranoid as we can arrive here with only partial
  860. * state retrieved from the hardware during setup.
  861. *
  862. * We can ditch the adjusted_mode.crtc_clock check as soon
  863. * as Haswell has gained clock readout/fastboot support.
  864. *
  865. * We can ditch the crtc->primary->fb check as soon as we can
  866. * properly reconstruct framebuffers.
  867. *
  868. * FIXME: The intel_crtc->active here should be switched to
  869. * crtc->state->active once we have proper CRTC states wired up
  870. * for atomic.
  871. */
  872. return crtc->active && crtc->base.primary->state->fb &&
  873. crtc->config->base.adjusted_mode.crtc_clock;
  874. }
  875. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  876. enum pipe pipe)
  877. {
  878. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  879. return crtc->config->cpu_transcoder;
  880. }
  881. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  882. enum pipe pipe)
  883. {
  884. i915_reg_t reg = PIPEDSL(pipe);
  885. u32 line1, line2;
  886. u32 line_mask;
  887. if (IS_GEN2(dev_priv))
  888. line_mask = DSL_LINEMASK_GEN2;
  889. else
  890. line_mask = DSL_LINEMASK_GEN3;
  891. line1 = I915_READ(reg) & line_mask;
  892. msleep(5);
  893. line2 = I915_READ(reg) & line_mask;
  894. return line1 != line2;
  895. }
  896. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  897. {
  898. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  899. enum pipe pipe = crtc->pipe;
  900. /* Wait for the display line to settle/start moving */
  901. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  902. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  903. pipe_name(pipe), onoff(state));
  904. }
  905. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  906. {
  907. wait_for_pipe_scanline_moving(crtc, false);
  908. }
  909. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  910. {
  911. wait_for_pipe_scanline_moving(crtc, true);
  912. }
  913. static void
  914. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  915. {
  916. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  917. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  918. if (INTEL_GEN(dev_priv) >= 4) {
  919. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  920. i915_reg_t reg = PIPECONF(cpu_transcoder);
  921. /* Wait for the Pipe State to go off */
  922. if (intel_wait_for_register(dev_priv,
  923. reg, I965_PIPECONF_ACTIVE, 0,
  924. 100))
  925. WARN(1, "pipe_off wait timed out\n");
  926. } else {
  927. intel_wait_for_pipe_scanline_stopped(crtc);
  928. }
  929. }
  930. /* Only for pre-ILK configs */
  931. void assert_pll(struct drm_i915_private *dev_priv,
  932. enum pipe pipe, bool state)
  933. {
  934. u32 val;
  935. bool cur_state;
  936. val = I915_READ(DPLL(pipe));
  937. cur_state = !!(val & DPLL_VCO_ENABLE);
  938. I915_STATE_WARN(cur_state != state,
  939. "PLL state assertion failure (expected %s, current %s)\n",
  940. onoff(state), onoff(cur_state));
  941. }
  942. /* XXX: the dsi pll is shared between MIPI DSI ports */
  943. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  944. {
  945. u32 val;
  946. bool cur_state;
  947. mutex_lock(&dev_priv->sb_lock);
  948. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  949. mutex_unlock(&dev_priv->sb_lock);
  950. cur_state = val & DSI_PLL_VCO_EN;
  951. I915_STATE_WARN(cur_state != state,
  952. "DSI PLL state assertion failure (expected %s, current %s)\n",
  953. onoff(state), onoff(cur_state));
  954. }
  955. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  956. enum pipe pipe, bool state)
  957. {
  958. bool cur_state;
  959. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  960. pipe);
  961. if (HAS_DDI(dev_priv)) {
  962. /* DDI does not have a specific FDI_TX register */
  963. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  964. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  965. } else {
  966. u32 val = I915_READ(FDI_TX_CTL(pipe));
  967. cur_state = !!(val & FDI_TX_ENABLE);
  968. }
  969. I915_STATE_WARN(cur_state != state,
  970. "FDI TX state assertion failure (expected %s, current %s)\n",
  971. onoff(state), onoff(cur_state));
  972. }
  973. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  974. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  975. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  976. enum pipe pipe, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. val = I915_READ(FDI_RX_CTL(pipe));
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. I915_STATE_WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. onoff(state), onoff(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. u32 val;
  992. /* ILK FDI PLL is always enabled */
  993. if (IS_GEN5(dev_priv))
  994. return;
  995. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  996. if (HAS_DDI(dev_priv))
  997. return;
  998. val = I915_READ(FDI_TX_CTL(pipe));
  999. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1000. }
  1001. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1002. enum pipe pipe, bool state)
  1003. {
  1004. u32 val;
  1005. bool cur_state;
  1006. val = I915_READ(FDI_RX_CTL(pipe));
  1007. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1008. I915_STATE_WARN(cur_state != state,
  1009. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1010. onoff(state), onoff(cur_state));
  1011. }
  1012. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1013. {
  1014. i915_reg_t pp_reg;
  1015. u32 val;
  1016. enum pipe panel_pipe = PIPE_A;
  1017. bool locked = true;
  1018. if (WARN_ON(HAS_DDI(dev_priv)))
  1019. return;
  1020. if (HAS_PCH_SPLIT(dev_priv)) {
  1021. u32 port_sel;
  1022. pp_reg = PP_CONTROL(0);
  1023. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1024. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1025. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1026. panel_pipe = PIPE_B;
  1027. /* XXX: else fix for eDP */
  1028. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1029. /* presumably write lock depends on pipe, not port select */
  1030. pp_reg = PP_CONTROL(pipe);
  1031. panel_pipe = pipe;
  1032. } else {
  1033. pp_reg = PP_CONTROL(0);
  1034. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. }
  1037. val = I915_READ(pp_reg);
  1038. if (!(val & PANEL_POWER_ON) ||
  1039. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1040. locked = false;
  1041. I915_STATE_WARN(panel_pipe == pipe && locked,
  1042. "panel assertion failure, pipe %c regs locked\n",
  1043. pipe_name(pipe));
  1044. }
  1045. void assert_pipe(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, bool state)
  1047. {
  1048. bool cur_state;
  1049. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1050. pipe);
  1051. enum intel_display_power_domain power_domain;
  1052. /* we keep both pipes enabled on 830 */
  1053. if (IS_I830(dev_priv))
  1054. state = true;
  1055. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1056. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1057. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1058. cur_state = !!(val & PIPECONF_ENABLE);
  1059. intel_display_power_put(dev_priv, power_domain);
  1060. } else {
  1061. cur_state = false;
  1062. }
  1063. I915_STATE_WARN(cur_state != state,
  1064. "pipe %c assertion failure (expected %s, current %s)\n",
  1065. pipe_name(pipe), onoff(state), onoff(cur_state));
  1066. }
  1067. static void assert_plane(struct intel_plane *plane, bool state)
  1068. {
  1069. bool cur_state = plane->get_hw_state(plane);
  1070. I915_STATE_WARN(cur_state != state,
  1071. "%s assertion failure (expected %s, current %s)\n",
  1072. plane->base.name, onoff(state), onoff(cur_state));
  1073. }
  1074. #define assert_plane_enabled(p) assert_plane(p, true)
  1075. #define assert_plane_disabled(p) assert_plane(p, false)
  1076. static void assert_planes_disabled(struct intel_crtc *crtc)
  1077. {
  1078. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1079. struct intel_plane *plane;
  1080. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1081. assert_plane_disabled(plane);
  1082. }
  1083. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1084. {
  1085. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1086. drm_crtc_vblank_put(crtc);
  1087. }
  1088. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. u32 val;
  1092. bool enabled;
  1093. val = I915_READ(PCH_TRANSCONF(pipe));
  1094. enabled = !!(val & TRANS_ENABLE);
  1095. I915_STATE_WARN(enabled,
  1096. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1097. pipe_name(pipe));
  1098. }
  1099. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1100. enum pipe pipe, u32 port_sel, u32 val)
  1101. {
  1102. if ((val & DP_PORT_EN) == 0)
  1103. return false;
  1104. if (HAS_PCH_CPT(dev_priv)) {
  1105. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1106. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1107. return false;
  1108. } else if (IS_CHERRYVIEW(dev_priv)) {
  1109. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1110. return false;
  1111. } else {
  1112. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1113. return false;
  1114. }
  1115. return true;
  1116. }
  1117. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe, u32 val)
  1119. {
  1120. if ((val & SDVO_ENABLE) == 0)
  1121. return false;
  1122. if (HAS_PCH_CPT(dev_priv)) {
  1123. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1124. return false;
  1125. } else if (IS_CHERRYVIEW(dev_priv)) {
  1126. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1127. return false;
  1128. } else {
  1129. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1130. return false;
  1131. }
  1132. return true;
  1133. }
  1134. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1135. enum pipe pipe, u32 val)
  1136. {
  1137. if ((val & LVDS_PORT_EN) == 0)
  1138. return false;
  1139. if (HAS_PCH_CPT(dev_priv)) {
  1140. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1141. return false;
  1142. } else {
  1143. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1144. return false;
  1145. }
  1146. return true;
  1147. }
  1148. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1149. enum pipe pipe, u32 val)
  1150. {
  1151. if ((val & ADPA_DAC_ENABLE) == 0)
  1152. return false;
  1153. if (HAS_PCH_CPT(dev_priv)) {
  1154. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1155. return false;
  1156. } else {
  1157. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1158. return false;
  1159. }
  1160. return true;
  1161. }
  1162. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe, i915_reg_t reg,
  1164. u32 port_sel)
  1165. {
  1166. u32 val = I915_READ(reg);
  1167. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1168. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1169. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1170. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1171. && (val & DP_PIPEB_SELECT),
  1172. "IBX PCH dp port still using transcoder B\n");
  1173. }
  1174. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1175. enum pipe pipe, i915_reg_t reg)
  1176. {
  1177. u32 val = I915_READ(reg);
  1178. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1179. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1180. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1181. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1182. && (val & SDVO_PIPE_B_SELECT),
  1183. "IBX PCH hdmi port still using transcoder B\n");
  1184. }
  1185. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1190. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1191. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1192. val = I915_READ(PCH_ADPA);
  1193. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1194. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1195. pipe_name(pipe));
  1196. val = I915_READ(PCH_LVDS);
  1197. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1198. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1199. pipe_name(pipe));
  1200. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1201. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1202. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1203. }
  1204. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1205. const struct intel_crtc_state *pipe_config)
  1206. {
  1207. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1208. enum pipe pipe = crtc->pipe;
  1209. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1210. POSTING_READ(DPLL(pipe));
  1211. udelay(150);
  1212. if (intel_wait_for_register(dev_priv,
  1213. DPLL(pipe),
  1214. DPLL_LOCK_VLV,
  1215. DPLL_LOCK_VLV,
  1216. 1))
  1217. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1218. }
  1219. static void vlv_enable_pll(struct intel_crtc *crtc,
  1220. const struct intel_crtc_state *pipe_config)
  1221. {
  1222. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1223. enum pipe pipe = crtc->pipe;
  1224. assert_pipe_disabled(dev_priv, pipe);
  1225. /* PLL is protected by panel, make sure we can write it */
  1226. assert_panel_unlocked(dev_priv, pipe);
  1227. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1228. _vlv_enable_pll(crtc, pipe_config);
  1229. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1230. POSTING_READ(DPLL_MD(pipe));
  1231. }
  1232. static void _chv_enable_pll(struct intel_crtc *crtc,
  1233. const struct intel_crtc_state *pipe_config)
  1234. {
  1235. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1236. enum pipe pipe = crtc->pipe;
  1237. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1238. u32 tmp;
  1239. mutex_lock(&dev_priv->sb_lock);
  1240. /* Enable back the 10bit clock to display controller */
  1241. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1242. tmp |= DPIO_DCLKP_EN;
  1243. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1244. mutex_unlock(&dev_priv->sb_lock);
  1245. /*
  1246. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1247. */
  1248. udelay(1);
  1249. /* Enable PLL */
  1250. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1251. /* Check PLL is locked */
  1252. if (intel_wait_for_register(dev_priv,
  1253. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1254. 1))
  1255. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1256. }
  1257. static void chv_enable_pll(struct intel_crtc *crtc,
  1258. const struct intel_crtc_state *pipe_config)
  1259. {
  1260. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1261. enum pipe pipe = crtc->pipe;
  1262. assert_pipe_disabled(dev_priv, pipe);
  1263. /* PLL is protected by panel, make sure we can write it */
  1264. assert_panel_unlocked(dev_priv, pipe);
  1265. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1266. _chv_enable_pll(crtc, pipe_config);
  1267. if (pipe != PIPE_A) {
  1268. /*
  1269. * WaPixelRepeatModeFixForC0:chv
  1270. *
  1271. * DPLLCMD is AWOL. Use chicken bits to propagate
  1272. * the value from DPLLBMD to either pipe B or C.
  1273. */
  1274. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1275. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1276. I915_WRITE(CBR4_VLV, 0);
  1277. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1278. /*
  1279. * DPLLB VGA mode also seems to cause problems.
  1280. * We should always have it disabled.
  1281. */
  1282. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1283. } else {
  1284. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1285. POSTING_READ(DPLL_MD(pipe));
  1286. }
  1287. }
  1288. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1289. {
  1290. struct intel_crtc *crtc;
  1291. int count = 0;
  1292. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1293. count += crtc->base.state->active &&
  1294. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1295. }
  1296. return count;
  1297. }
  1298. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1299. const struct intel_crtc_state *crtc_state)
  1300. {
  1301. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1302. i915_reg_t reg = DPLL(crtc->pipe);
  1303. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1304. int i;
  1305. assert_pipe_disabled(dev_priv, crtc->pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1308. assert_panel_unlocked(dev_priv, crtc->pipe);
  1309. /* Enable DVO 2x clock on both PLLs if necessary */
  1310. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1311. /*
  1312. * It appears to be important that we don't enable this
  1313. * for the current pipe before otherwise configuring the
  1314. * PLL. No idea how this should be handled if multiple
  1315. * DVO outputs are enabled simultaneosly.
  1316. */
  1317. dpll |= DPLL_DVO_2X_MODE;
  1318. I915_WRITE(DPLL(!crtc->pipe),
  1319. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1320. }
  1321. /*
  1322. * Apparently we need to have VGA mode enabled prior to changing
  1323. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1324. * dividers, even though the register value does change.
  1325. */
  1326. I915_WRITE(reg, 0);
  1327. I915_WRITE(reg, dpll);
  1328. /* Wait for the clocks to stabilize. */
  1329. POSTING_READ(reg);
  1330. udelay(150);
  1331. if (INTEL_GEN(dev_priv) >= 4) {
  1332. I915_WRITE(DPLL_MD(crtc->pipe),
  1333. crtc_state->dpll_hw_state.dpll_md);
  1334. } else {
  1335. /* The pixel multiplier can only be updated once the
  1336. * DPLL is enabled and the clocks are stable.
  1337. *
  1338. * So write it again.
  1339. */
  1340. I915_WRITE(reg, dpll);
  1341. }
  1342. /* We do this three times for luck */
  1343. for (i = 0; i < 3; i++) {
  1344. I915_WRITE(reg, dpll);
  1345. POSTING_READ(reg);
  1346. udelay(150); /* wait for warmup */
  1347. }
  1348. }
  1349. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1350. {
  1351. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1352. enum pipe pipe = crtc->pipe;
  1353. /* Disable DVO 2x clock on both PLLs if necessary */
  1354. if (IS_I830(dev_priv) &&
  1355. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1356. !intel_num_dvo_pipes(dev_priv)) {
  1357. I915_WRITE(DPLL(PIPE_B),
  1358. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1359. I915_WRITE(DPLL(PIPE_A),
  1360. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1361. }
  1362. /* Don't disable pipe or pipe PLLs if needed */
  1363. if (IS_I830(dev_priv))
  1364. return;
  1365. /* Make sure the pipe isn't still relying on us */
  1366. assert_pipe_disabled(dev_priv, pipe);
  1367. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1368. POSTING_READ(DPLL(pipe));
  1369. }
  1370. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1371. {
  1372. u32 val;
  1373. /* Make sure the pipe isn't still relying on us */
  1374. assert_pipe_disabled(dev_priv, pipe);
  1375. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1376. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1377. if (pipe != PIPE_A)
  1378. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1379. I915_WRITE(DPLL(pipe), val);
  1380. POSTING_READ(DPLL(pipe));
  1381. }
  1382. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1383. {
  1384. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1385. u32 val;
  1386. /* Make sure the pipe isn't still relying on us */
  1387. assert_pipe_disabled(dev_priv, pipe);
  1388. val = DPLL_SSC_REF_CLK_CHV |
  1389. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1390. if (pipe != PIPE_A)
  1391. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1392. I915_WRITE(DPLL(pipe), val);
  1393. POSTING_READ(DPLL(pipe));
  1394. mutex_lock(&dev_priv->sb_lock);
  1395. /* Disable 10bit clock to display controller */
  1396. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1397. val &= ~DPIO_DCLKP_EN;
  1398. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1399. mutex_unlock(&dev_priv->sb_lock);
  1400. }
  1401. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1402. struct intel_digital_port *dport,
  1403. unsigned int expected_mask)
  1404. {
  1405. u32 port_mask;
  1406. i915_reg_t dpll_reg;
  1407. switch (dport->base.port) {
  1408. case PORT_B:
  1409. port_mask = DPLL_PORTB_READY_MASK;
  1410. dpll_reg = DPLL(0);
  1411. break;
  1412. case PORT_C:
  1413. port_mask = DPLL_PORTC_READY_MASK;
  1414. dpll_reg = DPLL(0);
  1415. expected_mask <<= 4;
  1416. break;
  1417. case PORT_D:
  1418. port_mask = DPLL_PORTD_READY_MASK;
  1419. dpll_reg = DPIO_PHY_STATUS;
  1420. break;
  1421. default:
  1422. BUG();
  1423. }
  1424. if (intel_wait_for_register(dev_priv,
  1425. dpll_reg, port_mask, expected_mask,
  1426. 1000))
  1427. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1428. port_name(dport->base.port),
  1429. I915_READ(dpll_reg) & port_mask, expected_mask);
  1430. }
  1431. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1432. enum pipe pipe)
  1433. {
  1434. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1435. pipe);
  1436. i915_reg_t reg;
  1437. uint32_t val, pipeconf_val;
  1438. /* Make sure PCH DPLL is enabled */
  1439. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1440. /* FDI must be feeding us bits for PCH ports */
  1441. assert_fdi_tx_enabled(dev_priv, pipe);
  1442. assert_fdi_rx_enabled(dev_priv, pipe);
  1443. if (HAS_PCH_CPT(dev_priv)) {
  1444. /* Workaround: Set the timing override bit before enabling the
  1445. * pch transcoder. */
  1446. reg = TRANS_CHICKEN2(pipe);
  1447. val = I915_READ(reg);
  1448. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1449. I915_WRITE(reg, val);
  1450. }
  1451. reg = PCH_TRANSCONF(pipe);
  1452. val = I915_READ(reg);
  1453. pipeconf_val = I915_READ(PIPECONF(pipe));
  1454. if (HAS_PCH_IBX(dev_priv)) {
  1455. /*
  1456. * Make the BPC in transcoder be consistent with
  1457. * that in pipeconf reg. For HDMI we must use 8bpc
  1458. * here for both 8bpc and 12bpc.
  1459. */
  1460. val &= ~PIPECONF_BPC_MASK;
  1461. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1462. val |= PIPECONF_8BPC;
  1463. else
  1464. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1465. }
  1466. val &= ~TRANS_INTERLACE_MASK;
  1467. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1468. if (HAS_PCH_IBX(dev_priv) &&
  1469. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1470. val |= TRANS_LEGACY_INTERLACED_ILK;
  1471. else
  1472. val |= TRANS_INTERLACED;
  1473. else
  1474. val |= TRANS_PROGRESSIVE;
  1475. I915_WRITE(reg, val | TRANS_ENABLE);
  1476. if (intel_wait_for_register(dev_priv,
  1477. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1478. 100))
  1479. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1480. }
  1481. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1482. enum transcoder cpu_transcoder)
  1483. {
  1484. u32 val, pipeconf_val;
  1485. /* FDI must be feeding us bits for PCH ports */
  1486. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1487. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1488. /* Workaround: set timing override bit. */
  1489. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1490. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1491. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1492. val = TRANS_ENABLE;
  1493. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1494. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1495. PIPECONF_INTERLACED_ILK)
  1496. val |= TRANS_INTERLACED;
  1497. else
  1498. val |= TRANS_PROGRESSIVE;
  1499. I915_WRITE(LPT_TRANSCONF, val);
  1500. if (intel_wait_for_register(dev_priv,
  1501. LPT_TRANSCONF,
  1502. TRANS_STATE_ENABLE,
  1503. TRANS_STATE_ENABLE,
  1504. 100))
  1505. DRM_ERROR("Failed to enable PCH transcoder\n");
  1506. }
  1507. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1508. enum pipe pipe)
  1509. {
  1510. i915_reg_t reg;
  1511. uint32_t val;
  1512. /* FDI relies on the transcoder */
  1513. assert_fdi_tx_disabled(dev_priv, pipe);
  1514. assert_fdi_rx_disabled(dev_priv, pipe);
  1515. /* Ports must be off as well */
  1516. assert_pch_ports_disabled(dev_priv, pipe);
  1517. reg = PCH_TRANSCONF(pipe);
  1518. val = I915_READ(reg);
  1519. val &= ~TRANS_ENABLE;
  1520. I915_WRITE(reg, val);
  1521. /* wait for PCH transcoder off, transcoder state */
  1522. if (intel_wait_for_register(dev_priv,
  1523. reg, TRANS_STATE_ENABLE, 0,
  1524. 50))
  1525. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1526. if (HAS_PCH_CPT(dev_priv)) {
  1527. /* Workaround: Clear the timing override chicken bit again. */
  1528. reg = TRANS_CHICKEN2(pipe);
  1529. val = I915_READ(reg);
  1530. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1531. I915_WRITE(reg, val);
  1532. }
  1533. }
  1534. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1535. {
  1536. u32 val;
  1537. val = I915_READ(LPT_TRANSCONF);
  1538. val &= ~TRANS_ENABLE;
  1539. I915_WRITE(LPT_TRANSCONF, val);
  1540. /* wait for PCH transcoder off, transcoder state */
  1541. if (intel_wait_for_register(dev_priv,
  1542. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1543. 50))
  1544. DRM_ERROR("Failed to disable PCH transcoder\n");
  1545. /* Workaround: clear timing override bit. */
  1546. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1547. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1548. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1549. }
  1550. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1551. {
  1552. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1553. if (HAS_PCH_LPT(dev_priv))
  1554. return PIPE_A;
  1555. else
  1556. return crtc->pipe;
  1557. }
  1558. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1559. {
  1560. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1562. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1563. enum pipe pipe = crtc->pipe;
  1564. i915_reg_t reg;
  1565. u32 val;
  1566. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1567. assert_planes_disabled(crtc);
  1568. /*
  1569. * A pipe without a PLL won't actually be able to drive bits from
  1570. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1571. * need the check.
  1572. */
  1573. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1574. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1575. assert_dsi_pll_enabled(dev_priv);
  1576. else
  1577. assert_pll_enabled(dev_priv, pipe);
  1578. } else {
  1579. if (new_crtc_state->has_pch_encoder) {
  1580. /* if driving the PCH, we need FDI enabled */
  1581. assert_fdi_rx_pll_enabled(dev_priv,
  1582. intel_crtc_pch_transcoder(crtc));
  1583. assert_fdi_tx_pll_enabled(dev_priv,
  1584. (enum pipe) cpu_transcoder);
  1585. }
  1586. /* FIXME: assert CPU port conditions for SNB+ */
  1587. }
  1588. reg = PIPECONF(cpu_transcoder);
  1589. val = I915_READ(reg);
  1590. if (val & PIPECONF_ENABLE) {
  1591. /* we keep both pipes enabled on 830 */
  1592. WARN_ON(!IS_I830(dev_priv));
  1593. return;
  1594. }
  1595. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1596. POSTING_READ(reg);
  1597. /*
  1598. * Until the pipe starts PIPEDSL reads will return a stale value,
  1599. * which causes an apparent vblank timestamp jump when PIPEDSL
  1600. * resets to its proper value. That also messes up the frame count
  1601. * when it's derived from the timestamps. So let's wait for the
  1602. * pipe to start properly before we call drm_crtc_vblank_on()
  1603. */
  1604. if (dev_priv->drm.max_vblank_count == 0)
  1605. intel_wait_for_pipe_scanline_moving(crtc);
  1606. }
  1607. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1608. {
  1609. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1610. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1611. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1612. enum pipe pipe = crtc->pipe;
  1613. i915_reg_t reg;
  1614. u32 val;
  1615. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1616. /*
  1617. * Make sure planes won't keep trying to pump pixels to us,
  1618. * or we might hang the display.
  1619. */
  1620. assert_planes_disabled(crtc);
  1621. reg = PIPECONF(cpu_transcoder);
  1622. val = I915_READ(reg);
  1623. if ((val & PIPECONF_ENABLE) == 0)
  1624. return;
  1625. /*
  1626. * Double wide has implications for planes
  1627. * so best keep it disabled when not needed.
  1628. */
  1629. if (old_crtc_state->double_wide)
  1630. val &= ~PIPECONF_DOUBLE_WIDE;
  1631. /* Don't disable pipe or pipe PLLs if needed */
  1632. if (!IS_I830(dev_priv))
  1633. val &= ~PIPECONF_ENABLE;
  1634. I915_WRITE(reg, val);
  1635. if ((val & PIPECONF_ENABLE) == 0)
  1636. intel_wait_for_pipe_off(old_crtc_state);
  1637. }
  1638. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1639. {
  1640. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1641. }
  1642. static unsigned int
  1643. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1644. {
  1645. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1646. unsigned int cpp = fb->format->cpp[plane];
  1647. switch (fb->modifier) {
  1648. case DRM_FORMAT_MOD_LINEAR:
  1649. return cpp;
  1650. case I915_FORMAT_MOD_X_TILED:
  1651. if (IS_GEN2(dev_priv))
  1652. return 128;
  1653. else
  1654. return 512;
  1655. case I915_FORMAT_MOD_Y_TILED_CCS:
  1656. if (plane == 1)
  1657. return 128;
  1658. /* fall through */
  1659. case I915_FORMAT_MOD_Y_TILED:
  1660. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1661. return 128;
  1662. else
  1663. return 512;
  1664. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1665. if (plane == 1)
  1666. return 128;
  1667. /* fall through */
  1668. case I915_FORMAT_MOD_Yf_TILED:
  1669. switch (cpp) {
  1670. case 1:
  1671. return 64;
  1672. case 2:
  1673. case 4:
  1674. return 128;
  1675. case 8:
  1676. case 16:
  1677. return 256;
  1678. default:
  1679. MISSING_CASE(cpp);
  1680. return cpp;
  1681. }
  1682. break;
  1683. default:
  1684. MISSING_CASE(fb->modifier);
  1685. return cpp;
  1686. }
  1687. }
  1688. static unsigned int
  1689. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1690. {
  1691. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1692. return 1;
  1693. else
  1694. return intel_tile_size(to_i915(fb->dev)) /
  1695. intel_tile_width_bytes(fb, plane);
  1696. }
  1697. /* Return the tile dimensions in pixel units */
  1698. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1699. unsigned int *tile_width,
  1700. unsigned int *tile_height)
  1701. {
  1702. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1703. unsigned int cpp = fb->format->cpp[plane];
  1704. *tile_width = tile_width_bytes / cpp;
  1705. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1706. }
  1707. unsigned int
  1708. intel_fb_align_height(const struct drm_framebuffer *fb,
  1709. int plane, unsigned int height)
  1710. {
  1711. unsigned int tile_height = intel_tile_height(fb, plane);
  1712. return ALIGN(height, tile_height);
  1713. }
  1714. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1715. {
  1716. unsigned int size = 0;
  1717. int i;
  1718. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1719. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1720. return size;
  1721. }
  1722. static void
  1723. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1724. const struct drm_framebuffer *fb,
  1725. unsigned int rotation)
  1726. {
  1727. view->type = I915_GGTT_VIEW_NORMAL;
  1728. if (drm_rotation_90_or_270(rotation)) {
  1729. view->type = I915_GGTT_VIEW_ROTATED;
  1730. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1731. }
  1732. }
  1733. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1734. {
  1735. if (IS_I830(dev_priv))
  1736. return 16 * 1024;
  1737. else if (IS_I85X(dev_priv))
  1738. return 256;
  1739. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1740. return 32;
  1741. else
  1742. return 4 * 1024;
  1743. }
  1744. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1745. {
  1746. if (INTEL_INFO(dev_priv)->gen >= 9)
  1747. return 256 * 1024;
  1748. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1749. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1750. return 128 * 1024;
  1751. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1752. return 4 * 1024;
  1753. else
  1754. return 0;
  1755. }
  1756. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1757. int plane)
  1758. {
  1759. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1760. /* AUX_DIST needs only 4K alignment */
  1761. if (plane == 1)
  1762. return 4096;
  1763. switch (fb->modifier) {
  1764. case DRM_FORMAT_MOD_LINEAR:
  1765. return intel_linear_alignment(dev_priv);
  1766. case I915_FORMAT_MOD_X_TILED:
  1767. if (INTEL_GEN(dev_priv) >= 9)
  1768. return 256 * 1024;
  1769. return 0;
  1770. case I915_FORMAT_MOD_Y_TILED_CCS:
  1771. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1772. case I915_FORMAT_MOD_Y_TILED:
  1773. case I915_FORMAT_MOD_Yf_TILED:
  1774. return 1 * 1024 * 1024;
  1775. default:
  1776. MISSING_CASE(fb->modifier);
  1777. return 0;
  1778. }
  1779. }
  1780. struct i915_vma *
  1781. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1782. {
  1783. struct drm_device *dev = fb->dev;
  1784. struct drm_i915_private *dev_priv = to_i915(dev);
  1785. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1786. struct i915_ggtt_view view;
  1787. struct i915_vma *vma;
  1788. u32 alignment;
  1789. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1790. alignment = intel_surf_alignment(fb, 0);
  1791. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1792. /* Note that the w/a also requires 64 PTE of padding following the
  1793. * bo. We currently fill all unused PTE with the shadow page and so
  1794. * we should always have valid PTE following the scanout preventing
  1795. * the VT-d warning.
  1796. */
  1797. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1798. alignment = 256 * 1024;
  1799. /*
  1800. * Global gtt pte registers are special registers which actually forward
  1801. * writes to a chunk of system memory. Which means that there is no risk
  1802. * that the register values disappear as soon as we call
  1803. * intel_runtime_pm_put(), so it is correct to wrap only the
  1804. * pin/unpin/fence and not more.
  1805. */
  1806. intel_runtime_pm_get(dev_priv);
  1807. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1808. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1809. if (IS_ERR(vma))
  1810. goto err;
  1811. if (i915_vma_is_map_and_fenceable(vma)) {
  1812. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1813. * fence, whereas 965+ only requires a fence if using
  1814. * framebuffer compression. For simplicity, we always, when
  1815. * possible, install a fence as the cost is not that onerous.
  1816. *
  1817. * If we fail to fence the tiled scanout, then either the
  1818. * modeset will reject the change (which is highly unlikely as
  1819. * the affected systems, all but one, do not have unmappable
  1820. * space) or we will not be able to enable full powersaving
  1821. * techniques (also likely not to apply due to various limits
  1822. * FBC and the like impose on the size of the buffer, which
  1823. * presumably we violated anyway with this unmappable buffer).
  1824. * Anyway, it is presumably better to stumble onwards with
  1825. * something and try to run the system in a "less than optimal"
  1826. * mode that matches the user configuration.
  1827. */
  1828. i915_vma_pin_fence(vma);
  1829. }
  1830. i915_vma_get(vma);
  1831. err:
  1832. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1833. intel_runtime_pm_put(dev_priv);
  1834. return vma;
  1835. }
  1836. void intel_unpin_fb_vma(struct i915_vma *vma)
  1837. {
  1838. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1839. i915_vma_unpin_fence(vma);
  1840. i915_gem_object_unpin_from_display_plane(vma);
  1841. i915_vma_put(vma);
  1842. }
  1843. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1844. unsigned int rotation)
  1845. {
  1846. if (drm_rotation_90_or_270(rotation))
  1847. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1848. else
  1849. return fb->pitches[plane];
  1850. }
  1851. /*
  1852. * Convert the x/y offsets into a linear offset.
  1853. * Only valid with 0/180 degree rotation, which is fine since linear
  1854. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1855. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1856. */
  1857. u32 intel_fb_xy_to_linear(int x, int y,
  1858. const struct intel_plane_state *state,
  1859. int plane)
  1860. {
  1861. const struct drm_framebuffer *fb = state->base.fb;
  1862. unsigned int cpp = fb->format->cpp[plane];
  1863. unsigned int pitch = fb->pitches[plane];
  1864. return y * pitch + x * cpp;
  1865. }
  1866. /*
  1867. * Add the x/y offsets derived from fb->offsets[] to the user
  1868. * specified plane src x/y offsets. The resulting x/y offsets
  1869. * specify the start of scanout from the beginning of the gtt mapping.
  1870. */
  1871. void intel_add_fb_offsets(int *x, int *y,
  1872. const struct intel_plane_state *state,
  1873. int plane)
  1874. {
  1875. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1876. unsigned int rotation = state->base.rotation;
  1877. if (drm_rotation_90_or_270(rotation)) {
  1878. *x += intel_fb->rotated[plane].x;
  1879. *y += intel_fb->rotated[plane].y;
  1880. } else {
  1881. *x += intel_fb->normal[plane].x;
  1882. *y += intel_fb->normal[plane].y;
  1883. }
  1884. }
  1885. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1886. unsigned int tile_width,
  1887. unsigned int tile_height,
  1888. unsigned int tile_size,
  1889. unsigned int pitch_tiles,
  1890. u32 old_offset,
  1891. u32 new_offset)
  1892. {
  1893. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1894. unsigned int tiles;
  1895. WARN_ON(old_offset & (tile_size - 1));
  1896. WARN_ON(new_offset & (tile_size - 1));
  1897. WARN_ON(new_offset > old_offset);
  1898. tiles = (old_offset - new_offset) / tile_size;
  1899. *y += tiles / pitch_tiles * tile_height;
  1900. *x += tiles % pitch_tiles * tile_width;
  1901. /* minimize x in case it got needlessly big */
  1902. *y += *x / pitch_pixels * tile_height;
  1903. *x %= pitch_pixels;
  1904. return new_offset;
  1905. }
  1906. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1907. const struct drm_framebuffer *fb, int plane,
  1908. unsigned int rotation,
  1909. u32 old_offset, u32 new_offset)
  1910. {
  1911. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1912. unsigned int cpp = fb->format->cpp[plane];
  1913. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1914. WARN_ON(new_offset > old_offset);
  1915. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1916. unsigned int tile_size, tile_width, tile_height;
  1917. unsigned int pitch_tiles;
  1918. tile_size = intel_tile_size(dev_priv);
  1919. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1920. if (drm_rotation_90_or_270(rotation)) {
  1921. pitch_tiles = pitch / tile_height;
  1922. swap(tile_width, tile_height);
  1923. } else {
  1924. pitch_tiles = pitch / (tile_width * cpp);
  1925. }
  1926. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1927. tile_size, pitch_tiles,
  1928. old_offset, new_offset);
  1929. } else {
  1930. old_offset += *y * pitch + *x * cpp;
  1931. *y = (old_offset - new_offset) / pitch;
  1932. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1933. }
  1934. return new_offset;
  1935. }
  1936. /*
  1937. * Adjust the tile offset by moving the difference into
  1938. * the x/y offsets.
  1939. */
  1940. static u32 intel_adjust_tile_offset(int *x, int *y,
  1941. const struct intel_plane_state *state, int plane,
  1942. u32 old_offset, u32 new_offset)
  1943. {
  1944. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  1945. state->base.rotation,
  1946. old_offset, new_offset);
  1947. }
  1948. /*
  1949. * Computes the linear offset to the base tile and adjusts
  1950. * x, y. bytes per pixel is assumed to be a power-of-two.
  1951. *
  1952. * In the 90/270 rotated case, x and y are assumed
  1953. * to be already rotated to match the rotated GTT view, and
  1954. * pitch is the tile_height aligned framebuffer height.
  1955. *
  1956. * This function is used when computing the derived information
  1957. * under intel_framebuffer, so using any of that information
  1958. * here is not allowed. Anything under drm_framebuffer can be
  1959. * used. This is why the user has to pass in the pitch since it
  1960. * is specified in the rotated orientation.
  1961. */
  1962. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  1963. int *x, int *y,
  1964. const struct drm_framebuffer *fb, int plane,
  1965. unsigned int pitch,
  1966. unsigned int rotation,
  1967. u32 alignment)
  1968. {
  1969. uint64_t fb_modifier = fb->modifier;
  1970. unsigned int cpp = fb->format->cpp[plane];
  1971. u32 offset, offset_aligned;
  1972. if (alignment)
  1973. alignment--;
  1974. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  1975. unsigned int tile_size, tile_width, tile_height;
  1976. unsigned int tile_rows, tiles, pitch_tiles;
  1977. tile_size = intel_tile_size(dev_priv);
  1978. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1979. if (drm_rotation_90_or_270(rotation)) {
  1980. pitch_tiles = pitch / tile_height;
  1981. swap(tile_width, tile_height);
  1982. } else {
  1983. pitch_tiles = pitch / (tile_width * cpp);
  1984. }
  1985. tile_rows = *y / tile_height;
  1986. *y %= tile_height;
  1987. tiles = *x / tile_width;
  1988. *x %= tile_width;
  1989. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  1990. offset_aligned = offset & ~alignment;
  1991. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1992. tile_size, pitch_tiles,
  1993. offset, offset_aligned);
  1994. } else {
  1995. offset = *y * pitch + *x * cpp;
  1996. offset_aligned = offset & ~alignment;
  1997. *y = (offset & alignment) / pitch;
  1998. *x = ((offset & alignment) - *y * pitch) / cpp;
  1999. }
  2000. return offset_aligned;
  2001. }
  2002. u32 intel_compute_tile_offset(int *x, int *y,
  2003. const struct intel_plane_state *state,
  2004. int plane)
  2005. {
  2006. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2007. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2008. const struct drm_framebuffer *fb = state->base.fb;
  2009. unsigned int rotation = state->base.rotation;
  2010. int pitch = intel_fb_pitch(fb, plane, rotation);
  2011. u32 alignment;
  2012. if (intel_plane->id == PLANE_CURSOR)
  2013. alignment = intel_cursor_alignment(dev_priv);
  2014. else
  2015. alignment = intel_surf_alignment(fb, plane);
  2016. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2017. rotation, alignment);
  2018. }
  2019. /* Convert the fb->offset[] into x/y offsets */
  2020. static int intel_fb_offset_to_xy(int *x, int *y,
  2021. const struct drm_framebuffer *fb, int plane)
  2022. {
  2023. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2024. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2025. fb->offsets[plane] % intel_tile_size(dev_priv))
  2026. return -EINVAL;
  2027. *x = 0;
  2028. *y = 0;
  2029. _intel_adjust_tile_offset(x, y,
  2030. fb, plane, DRM_MODE_ROTATE_0,
  2031. fb->offsets[plane], 0);
  2032. return 0;
  2033. }
  2034. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2035. {
  2036. switch (fb_modifier) {
  2037. case I915_FORMAT_MOD_X_TILED:
  2038. return I915_TILING_X;
  2039. case I915_FORMAT_MOD_Y_TILED:
  2040. case I915_FORMAT_MOD_Y_TILED_CCS:
  2041. return I915_TILING_Y;
  2042. default:
  2043. return I915_TILING_NONE;
  2044. }
  2045. }
  2046. static const struct drm_format_info ccs_formats[] = {
  2047. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2048. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2049. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2050. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2051. };
  2052. static const struct drm_format_info *
  2053. lookup_format_info(const struct drm_format_info formats[],
  2054. int num_formats, u32 format)
  2055. {
  2056. int i;
  2057. for (i = 0; i < num_formats; i++) {
  2058. if (formats[i].format == format)
  2059. return &formats[i];
  2060. }
  2061. return NULL;
  2062. }
  2063. static const struct drm_format_info *
  2064. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2065. {
  2066. switch (cmd->modifier[0]) {
  2067. case I915_FORMAT_MOD_Y_TILED_CCS:
  2068. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2069. return lookup_format_info(ccs_formats,
  2070. ARRAY_SIZE(ccs_formats),
  2071. cmd->pixel_format);
  2072. default:
  2073. return NULL;
  2074. }
  2075. }
  2076. static int
  2077. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2078. struct drm_framebuffer *fb)
  2079. {
  2080. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2081. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2082. u32 gtt_offset_rotated = 0;
  2083. unsigned int max_size = 0;
  2084. int i, num_planes = fb->format->num_planes;
  2085. unsigned int tile_size = intel_tile_size(dev_priv);
  2086. for (i = 0; i < num_planes; i++) {
  2087. unsigned int width, height;
  2088. unsigned int cpp, size;
  2089. u32 offset;
  2090. int x, y;
  2091. int ret;
  2092. cpp = fb->format->cpp[i];
  2093. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2094. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2095. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2096. if (ret) {
  2097. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2098. i, fb->offsets[i]);
  2099. return ret;
  2100. }
  2101. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2102. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2103. int hsub = fb->format->hsub;
  2104. int vsub = fb->format->vsub;
  2105. int tile_width, tile_height;
  2106. int main_x, main_y;
  2107. int ccs_x, ccs_y;
  2108. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2109. tile_width *= hsub;
  2110. tile_height *= vsub;
  2111. ccs_x = (x * hsub) % tile_width;
  2112. ccs_y = (y * vsub) % tile_height;
  2113. main_x = intel_fb->normal[0].x % tile_width;
  2114. main_y = intel_fb->normal[0].y % tile_height;
  2115. /*
  2116. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2117. * x/y offsets must match between CCS and the main surface.
  2118. */
  2119. if (main_x != ccs_x || main_y != ccs_y) {
  2120. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2121. main_x, main_y,
  2122. ccs_x, ccs_y,
  2123. intel_fb->normal[0].x,
  2124. intel_fb->normal[0].y,
  2125. x, y);
  2126. return -EINVAL;
  2127. }
  2128. }
  2129. /*
  2130. * The fence (if used) is aligned to the start of the object
  2131. * so having the framebuffer wrap around across the edge of the
  2132. * fenced region doesn't really work. We have no API to configure
  2133. * the fence start offset within the object (nor could we probably
  2134. * on gen2/3). So it's just easier if we just require that the
  2135. * fb layout agrees with the fence layout. We already check that the
  2136. * fb stride matches the fence stride elsewhere.
  2137. */
  2138. if (i == 0 && i915_gem_object_is_tiled(intel_fb->obj) &&
  2139. (x + width) * cpp > fb->pitches[i]) {
  2140. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2141. i, fb->offsets[i]);
  2142. return -EINVAL;
  2143. }
  2144. /*
  2145. * First pixel of the framebuffer from
  2146. * the start of the normal gtt mapping.
  2147. */
  2148. intel_fb->normal[i].x = x;
  2149. intel_fb->normal[i].y = y;
  2150. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2151. fb, i, fb->pitches[i],
  2152. DRM_MODE_ROTATE_0, tile_size);
  2153. offset /= tile_size;
  2154. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2155. unsigned int tile_width, tile_height;
  2156. unsigned int pitch_tiles;
  2157. struct drm_rect r;
  2158. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2159. rot_info->plane[i].offset = offset;
  2160. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2161. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2162. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2163. intel_fb->rotated[i].pitch =
  2164. rot_info->plane[i].height * tile_height;
  2165. /* how many tiles does this plane need */
  2166. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2167. /*
  2168. * If the plane isn't horizontally tile aligned,
  2169. * we need one more tile.
  2170. */
  2171. if (x != 0)
  2172. size++;
  2173. /* rotate the x/y offsets to match the GTT view */
  2174. r.x1 = x;
  2175. r.y1 = y;
  2176. r.x2 = x + width;
  2177. r.y2 = y + height;
  2178. drm_rect_rotate(&r,
  2179. rot_info->plane[i].width * tile_width,
  2180. rot_info->plane[i].height * tile_height,
  2181. DRM_MODE_ROTATE_270);
  2182. x = r.x1;
  2183. y = r.y1;
  2184. /* rotate the tile dimensions to match the GTT view */
  2185. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2186. swap(tile_width, tile_height);
  2187. /*
  2188. * We only keep the x/y offsets, so push all of the
  2189. * gtt offset into the x/y offsets.
  2190. */
  2191. __intel_adjust_tile_offset(&x, &y,
  2192. tile_width, tile_height,
  2193. tile_size, pitch_tiles,
  2194. gtt_offset_rotated * tile_size, 0);
  2195. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2196. /*
  2197. * First pixel of the framebuffer from
  2198. * the start of the rotated gtt mapping.
  2199. */
  2200. intel_fb->rotated[i].x = x;
  2201. intel_fb->rotated[i].y = y;
  2202. } else {
  2203. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2204. x * cpp, tile_size);
  2205. }
  2206. /* how many tiles in total needed in the bo */
  2207. max_size = max(max_size, offset + size);
  2208. }
  2209. if (max_size * tile_size > intel_fb->obj->base.size) {
  2210. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2211. max_size * tile_size, intel_fb->obj->base.size);
  2212. return -EINVAL;
  2213. }
  2214. return 0;
  2215. }
  2216. static int i9xx_format_to_fourcc(int format)
  2217. {
  2218. switch (format) {
  2219. case DISPPLANE_8BPP:
  2220. return DRM_FORMAT_C8;
  2221. case DISPPLANE_BGRX555:
  2222. return DRM_FORMAT_XRGB1555;
  2223. case DISPPLANE_BGRX565:
  2224. return DRM_FORMAT_RGB565;
  2225. default:
  2226. case DISPPLANE_BGRX888:
  2227. return DRM_FORMAT_XRGB8888;
  2228. case DISPPLANE_RGBX888:
  2229. return DRM_FORMAT_XBGR8888;
  2230. case DISPPLANE_BGRX101010:
  2231. return DRM_FORMAT_XRGB2101010;
  2232. case DISPPLANE_RGBX101010:
  2233. return DRM_FORMAT_XBGR2101010;
  2234. }
  2235. }
  2236. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2237. {
  2238. switch (format) {
  2239. case PLANE_CTL_FORMAT_RGB_565:
  2240. return DRM_FORMAT_RGB565;
  2241. default:
  2242. case PLANE_CTL_FORMAT_XRGB_8888:
  2243. if (rgb_order) {
  2244. if (alpha)
  2245. return DRM_FORMAT_ABGR8888;
  2246. else
  2247. return DRM_FORMAT_XBGR8888;
  2248. } else {
  2249. if (alpha)
  2250. return DRM_FORMAT_ARGB8888;
  2251. else
  2252. return DRM_FORMAT_XRGB8888;
  2253. }
  2254. case PLANE_CTL_FORMAT_XRGB_2101010:
  2255. if (rgb_order)
  2256. return DRM_FORMAT_XBGR2101010;
  2257. else
  2258. return DRM_FORMAT_XRGB2101010;
  2259. }
  2260. }
  2261. static bool
  2262. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2263. struct intel_initial_plane_config *plane_config)
  2264. {
  2265. struct drm_device *dev = crtc->base.dev;
  2266. struct drm_i915_private *dev_priv = to_i915(dev);
  2267. struct drm_i915_gem_object *obj = NULL;
  2268. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2269. struct drm_framebuffer *fb = &plane_config->fb->base;
  2270. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2271. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2272. PAGE_SIZE);
  2273. size_aligned -= base_aligned;
  2274. if (plane_config->size == 0)
  2275. return false;
  2276. /* If the FB is too big, just don't use it since fbdev is not very
  2277. * important and we should probably use that space with FBC or other
  2278. * features. */
  2279. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2280. return false;
  2281. mutex_lock(&dev->struct_mutex);
  2282. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2283. base_aligned,
  2284. base_aligned,
  2285. size_aligned);
  2286. mutex_unlock(&dev->struct_mutex);
  2287. if (!obj)
  2288. return false;
  2289. if (plane_config->tiling == I915_TILING_X)
  2290. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2291. mode_cmd.pixel_format = fb->format->format;
  2292. mode_cmd.width = fb->width;
  2293. mode_cmd.height = fb->height;
  2294. mode_cmd.pitches[0] = fb->pitches[0];
  2295. mode_cmd.modifier[0] = fb->modifier;
  2296. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2297. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2298. DRM_DEBUG_KMS("intel fb init failed\n");
  2299. goto out_unref_obj;
  2300. }
  2301. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2302. return true;
  2303. out_unref_obj:
  2304. i915_gem_object_put(obj);
  2305. return false;
  2306. }
  2307. static void
  2308. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2309. struct intel_plane_state *plane_state,
  2310. bool visible)
  2311. {
  2312. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2313. plane_state->base.visible = visible;
  2314. /* FIXME pre-g4x don't work like this */
  2315. if (visible) {
  2316. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2317. crtc_state->active_planes |= BIT(plane->id);
  2318. } else {
  2319. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2320. crtc_state->active_planes &= ~BIT(plane->id);
  2321. }
  2322. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2323. crtc_state->base.crtc->name,
  2324. crtc_state->active_planes);
  2325. }
  2326. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2327. struct intel_plane *plane)
  2328. {
  2329. struct intel_crtc_state *crtc_state =
  2330. to_intel_crtc_state(crtc->base.state);
  2331. struct intel_plane_state *plane_state =
  2332. to_intel_plane_state(plane->base.state);
  2333. intel_set_plane_visible(crtc_state, plane_state, false);
  2334. if (plane->id == PLANE_PRIMARY)
  2335. intel_pre_disable_primary_noatomic(&crtc->base);
  2336. trace_intel_disable_plane(&plane->base, crtc);
  2337. plane->disable_plane(plane, crtc);
  2338. }
  2339. static void
  2340. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2341. struct intel_initial_plane_config *plane_config)
  2342. {
  2343. struct drm_device *dev = intel_crtc->base.dev;
  2344. struct drm_i915_private *dev_priv = to_i915(dev);
  2345. struct drm_crtc *c;
  2346. struct drm_i915_gem_object *obj;
  2347. struct drm_plane *primary = intel_crtc->base.primary;
  2348. struct drm_plane_state *plane_state = primary->state;
  2349. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2350. struct intel_plane *intel_plane = to_intel_plane(primary);
  2351. struct intel_plane_state *intel_state =
  2352. to_intel_plane_state(plane_state);
  2353. struct drm_framebuffer *fb;
  2354. if (!plane_config->fb)
  2355. return;
  2356. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2357. fb = &plane_config->fb->base;
  2358. goto valid_fb;
  2359. }
  2360. kfree(plane_config->fb);
  2361. /*
  2362. * Failed to alloc the obj, check to see if we should share
  2363. * an fb with another CRTC instead
  2364. */
  2365. for_each_crtc(dev, c) {
  2366. struct intel_plane_state *state;
  2367. if (c == &intel_crtc->base)
  2368. continue;
  2369. if (!to_intel_crtc(c)->active)
  2370. continue;
  2371. state = to_intel_plane_state(c->primary->state);
  2372. if (!state->vma)
  2373. continue;
  2374. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2375. fb = c->primary->fb;
  2376. drm_framebuffer_get(fb);
  2377. goto valid_fb;
  2378. }
  2379. }
  2380. /*
  2381. * We've failed to reconstruct the BIOS FB. Current display state
  2382. * indicates that the primary plane is visible, but has a NULL FB,
  2383. * which will lead to problems later if we don't fix it up. The
  2384. * simplest solution is to just disable the primary plane now and
  2385. * pretend the BIOS never had it enabled.
  2386. */
  2387. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2388. return;
  2389. valid_fb:
  2390. mutex_lock(&dev->struct_mutex);
  2391. intel_state->vma =
  2392. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2393. mutex_unlock(&dev->struct_mutex);
  2394. if (IS_ERR(intel_state->vma)) {
  2395. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2396. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2397. intel_state->vma = NULL;
  2398. drm_framebuffer_put(fb);
  2399. return;
  2400. }
  2401. plane_state->src_x = 0;
  2402. plane_state->src_y = 0;
  2403. plane_state->src_w = fb->width << 16;
  2404. plane_state->src_h = fb->height << 16;
  2405. plane_state->crtc_x = 0;
  2406. plane_state->crtc_y = 0;
  2407. plane_state->crtc_w = fb->width;
  2408. plane_state->crtc_h = fb->height;
  2409. intel_state->base.src = drm_plane_state_src(plane_state);
  2410. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2411. obj = intel_fb_obj(fb);
  2412. if (i915_gem_object_is_tiled(obj))
  2413. dev_priv->preserve_bios_swizzle = true;
  2414. drm_framebuffer_get(fb);
  2415. primary->fb = primary->state->fb = fb;
  2416. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2417. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2418. to_intel_plane_state(plane_state),
  2419. true);
  2420. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2421. &obj->frontbuffer_bits);
  2422. }
  2423. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2424. unsigned int rotation)
  2425. {
  2426. int cpp = fb->format->cpp[plane];
  2427. switch (fb->modifier) {
  2428. case DRM_FORMAT_MOD_LINEAR:
  2429. case I915_FORMAT_MOD_X_TILED:
  2430. switch (cpp) {
  2431. case 8:
  2432. return 4096;
  2433. case 4:
  2434. case 2:
  2435. case 1:
  2436. return 8192;
  2437. default:
  2438. MISSING_CASE(cpp);
  2439. break;
  2440. }
  2441. break;
  2442. case I915_FORMAT_MOD_Y_TILED_CCS:
  2443. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2444. /* FIXME AUX plane? */
  2445. case I915_FORMAT_MOD_Y_TILED:
  2446. case I915_FORMAT_MOD_Yf_TILED:
  2447. switch (cpp) {
  2448. case 8:
  2449. return 2048;
  2450. case 4:
  2451. return 4096;
  2452. case 2:
  2453. case 1:
  2454. return 8192;
  2455. default:
  2456. MISSING_CASE(cpp);
  2457. break;
  2458. }
  2459. break;
  2460. default:
  2461. MISSING_CASE(fb->modifier);
  2462. }
  2463. return 2048;
  2464. }
  2465. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2466. int main_x, int main_y, u32 main_offset)
  2467. {
  2468. const struct drm_framebuffer *fb = plane_state->base.fb;
  2469. int hsub = fb->format->hsub;
  2470. int vsub = fb->format->vsub;
  2471. int aux_x = plane_state->aux.x;
  2472. int aux_y = plane_state->aux.y;
  2473. u32 aux_offset = plane_state->aux.offset;
  2474. u32 alignment = intel_surf_alignment(fb, 1);
  2475. while (aux_offset >= main_offset && aux_y <= main_y) {
  2476. int x, y;
  2477. if (aux_x == main_x && aux_y == main_y)
  2478. break;
  2479. if (aux_offset == 0)
  2480. break;
  2481. x = aux_x / hsub;
  2482. y = aux_y / vsub;
  2483. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2484. aux_offset, aux_offset - alignment);
  2485. aux_x = x * hsub + aux_x % hsub;
  2486. aux_y = y * vsub + aux_y % vsub;
  2487. }
  2488. if (aux_x != main_x || aux_y != main_y)
  2489. return false;
  2490. plane_state->aux.offset = aux_offset;
  2491. plane_state->aux.x = aux_x;
  2492. plane_state->aux.y = aux_y;
  2493. return true;
  2494. }
  2495. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2496. {
  2497. const struct drm_framebuffer *fb = plane_state->base.fb;
  2498. unsigned int rotation = plane_state->base.rotation;
  2499. int x = plane_state->base.src.x1 >> 16;
  2500. int y = plane_state->base.src.y1 >> 16;
  2501. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2502. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2503. int max_width = skl_max_plane_width(fb, 0, rotation);
  2504. int max_height = 4096;
  2505. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2506. if (w > max_width || h > max_height) {
  2507. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2508. w, h, max_width, max_height);
  2509. return -EINVAL;
  2510. }
  2511. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2512. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2513. alignment = intel_surf_alignment(fb, 0);
  2514. /*
  2515. * AUX surface offset is specified as the distance from the
  2516. * main surface offset, and it must be non-negative. Make
  2517. * sure that is what we will get.
  2518. */
  2519. if (offset > aux_offset)
  2520. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2521. offset, aux_offset & ~(alignment - 1));
  2522. /*
  2523. * When using an X-tiled surface, the plane blows up
  2524. * if the x offset + width exceed the stride.
  2525. *
  2526. * TODO: linear and Y-tiled seem fine, Yf untested,
  2527. */
  2528. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2529. int cpp = fb->format->cpp[0];
  2530. while ((x + w) * cpp > fb->pitches[0]) {
  2531. if (offset == 0) {
  2532. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2533. return -EINVAL;
  2534. }
  2535. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2536. offset, offset - alignment);
  2537. }
  2538. }
  2539. /*
  2540. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2541. * they match with the main surface x/y offsets.
  2542. */
  2543. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2544. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2545. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2546. if (offset == 0)
  2547. break;
  2548. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2549. offset, offset - alignment);
  2550. }
  2551. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2552. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2553. return -EINVAL;
  2554. }
  2555. }
  2556. plane_state->main.offset = offset;
  2557. plane_state->main.x = x;
  2558. plane_state->main.y = y;
  2559. return 0;
  2560. }
  2561. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2562. {
  2563. const struct drm_framebuffer *fb = plane_state->base.fb;
  2564. unsigned int rotation = plane_state->base.rotation;
  2565. int max_width = skl_max_plane_width(fb, 1, rotation);
  2566. int max_height = 4096;
  2567. int x = plane_state->base.src.x1 >> 17;
  2568. int y = plane_state->base.src.y1 >> 17;
  2569. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2570. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2571. u32 offset;
  2572. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2573. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2574. /* FIXME not quite sure how/if these apply to the chroma plane */
  2575. if (w > max_width || h > max_height) {
  2576. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2577. w, h, max_width, max_height);
  2578. return -EINVAL;
  2579. }
  2580. plane_state->aux.offset = offset;
  2581. plane_state->aux.x = x;
  2582. plane_state->aux.y = y;
  2583. return 0;
  2584. }
  2585. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2586. {
  2587. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2588. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2589. const struct drm_framebuffer *fb = plane_state->base.fb;
  2590. int src_x = plane_state->base.src.x1 >> 16;
  2591. int src_y = plane_state->base.src.y1 >> 16;
  2592. int hsub = fb->format->hsub;
  2593. int vsub = fb->format->vsub;
  2594. int x = src_x / hsub;
  2595. int y = src_y / vsub;
  2596. u32 offset;
  2597. switch (plane->id) {
  2598. case PLANE_PRIMARY:
  2599. case PLANE_SPRITE0:
  2600. break;
  2601. default:
  2602. DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
  2603. return -EINVAL;
  2604. }
  2605. if (crtc->pipe == PIPE_C) {
  2606. DRM_DEBUG_KMS("No RC support on pipe C\n");
  2607. return -EINVAL;
  2608. }
  2609. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2610. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2611. plane_state->base.rotation);
  2612. return -EINVAL;
  2613. }
  2614. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2615. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2616. plane_state->aux.offset = offset;
  2617. plane_state->aux.x = x * hsub + src_x % hsub;
  2618. plane_state->aux.y = y * vsub + src_y % vsub;
  2619. return 0;
  2620. }
  2621. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2622. {
  2623. const struct drm_framebuffer *fb = plane_state->base.fb;
  2624. unsigned int rotation = plane_state->base.rotation;
  2625. int ret;
  2626. if (rotation & DRM_MODE_REFLECT_X &&
  2627. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2628. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2629. return -EINVAL;
  2630. }
  2631. if (!plane_state->base.visible)
  2632. return 0;
  2633. /* Rotate src coordinates to match rotated GTT view */
  2634. if (drm_rotation_90_or_270(rotation))
  2635. drm_rect_rotate(&plane_state->base.src,
  2636. fb->width << 16, fb->height << 16,
  2637. DRM_MODE_ROTATE_270);
  2638. /*
  2639. * Handle the AUX surface first since
  2640. * the main surface setup depends on it.
  2641. */
  2642. if (fb->format->format == DRM_FORMAT_NV12) {
  2643. ret = skl_check_nv12_aux_surface(plane_state);
  2644. if (ret)
  2645. return ret;
  2646. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2647. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2648. ret = skl_check_ccs_aux_surface(plane_state);
  2649. if (ret)
  2650. return ret;
  2651. } else {
  2652. plane_state->aux.offset = ~0xfff;
  2653. plane_state->aux.x = 0;
  2654. plane_state->aux.y = 0;
  2655. }
  2656. ret = skl_check_main_surface(plane_state);
  2657. if (ret)
  2658. return ret;
  2659. return 0;
  2660. }
  2661. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2662. const struct intel_plane_state *plane_state)
  2663. {
  2664. struct drm_i915_private *dev_priv =
  2665. to_i915(plane_state->base.plane->dev);
  2666. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2667. const struct drm_framebuffer *fb = plane_state->base.fb;
  2668. unsigned int rotation = plane_state->base.rotation;
  2669. u32 dspcntr;
  2670. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2671. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2672. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2673. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2674. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2675. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2676. if (INTEL_GEN(dev_priv) < 4)
  2677. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2678. switch (fb->format->format) {
  2679. case DRM_FORMAT_C8:
  2680. dspcntr |= DISPPLANE_8BPP;
  2681. break;
  2682. case DRM_FORMAT_XRGB1555:
  2683. dspcntr |= DISPPLANE_BGRX555;
  2684. break;
  2685. case DRM_FORMAT_RGB565:
  2686. dspcntr |= DISPPLANE_BGRX565;
  2687. break;
  2688. case DRM_FORMAT_XRGB8888:
  2689. dspcntr |= DISPPLANE_BGRX888;
  2690. break;
  2691. case DRM_FORMAT_XBGR8888:
  2692. dspcntr |= DISPPLANE_RGBX888;
  2693. break;
  2694. case DRM_FORMAT_XRGB2101010:
  2695. dspcntr |= DISPPLANE_BGRX101010;
  2696. break;
  2697. case DRM_FORMAT_XBGR2101010:
  2698. dspcntr |= DISPPLANE_RGBX101010;
  2699. break;
  2700. default:
  2701. MISSING_CASE(fb->format->format);
  2702. return 0;
  2703. }
  2704. if (INTEL_GEN(dev_priv) >= 4 &&
  2705. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2706. dspcntr |= DISPPLANE_TILED;
  2707. if (rotation & DRM_MODE_ROTATE_180)
  2708. dspcntr |= DISPPLANE_ROTATE_180;
  2709. if (rotation & DRM_MODE_REFLECT_X)
  2710. dspcntr |= DISPPLANE_MIRROR;
  2711. return dspcntr;
  2712. }
  2713. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2714. {
  2715. struct drm_i915_private *dev_priv =
  2716. to_i915(plane_state->base.plane->dev);
  2717. int src_x = plane_state->base.src.x1 >> 16;
  2718. int src_y = plane_state->base.src.y1 >> 16;
  2719. u32 offset;
  2720. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2721. if (INTEL_GEN(dev_priv) >= 4)
  2722. offset = intel_compute_tile_offset(&src_x, &src_y,
  2723. plane_state, 0);
  2724. else
  2725. offset = 0;
  2726. /* HSW/BDW do this automagically in hardware */
  2727. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2728. unsigned int rotation = plane_state->base.rotation;
  2729. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2730. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2731. if (rotation & DRM_MODE_ROTATE_180) {
  2732. src_x += src_w - 1;
  2733. src_y += src_h - 1;
  2734. } else if (rotation & DRM_MODE_REFLECT_X) {
  2735. src_x += src_w - 1;
  2736. }
  2737. }
  2738. plane_state->main.offset = offset;
  2739. plane_state->main.x = src_x;
  2740. plane_state->main.y = src_y;
  2741. return 0;
  2742. }
  2743. static void i9xx_update_plane(struct intel_plane *plane,
  2744. const struct intel_crtc_state *crtc_state,
  2745. const struct intel_plane_state *plane_state)
  2746. {
  2747. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2748. const struct drm_framebuffer *fb = plane_state->base.fb;
  2749. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2750. u32 linear_offset;
  2751. u32 dspcntr = plane_state->ctl;
  2752. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2753. int x = plane_state->main.x;
  2754. int y = plane_state->main.y;
  2755. unsigned long irqflags;
  2756. u32 dspaddr_offset;
  2757. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2758. if (INTEL_GEN(dev_priv) >= 4)
  2759. dspaddr_offset = plane_state->main.offset;
  2760. else
  2761. dspaddr_offset = linear_offset;
  2762. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2763. if (INTEL_GEN(dev_priv) < 4) {
  2764. /* pipesrc and dspsize control the size that is scaled from,
  2765. * which should always be the user's requested size.
  2766. */
  2767. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2768. ((crtc_state->pipe_src_h - 1) << 16) |
  2769. (crtc_state->pipe_src_w - 1));
  2770. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2771. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2772. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2773. ((crtc_state->pipe_src_h - 1) << 16) |
  2774. (crtc_state->pipe_src_w - 1));
  2775. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2776. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2777. }
  2778. I915_WRITE_FW(reg, dspcntr);
  2779. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2780. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2781. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2782. intel_plane_ggtt_offset(plane_state) +
  2783. dspaddr_offset);
  2784. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2785. } else if (INTEL_GEN(dev_priv) >= 4) {
  2786. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2787. intel_plane_ggtt_offset(plane_state) +
  2788. dspaddr_offset);
  2789. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2790. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2791. } else {
  2792. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2793. intel_plane_ggtt_offset(plane_state) +
  2794. dspaddr_offset);
  2795. }
  2796. POSTING_READ_FW(reg);
  2797. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2798. }
  2799. static void i9xx_disable_plane(struct intel_plane *plane,
  2800. struct intel_crtc *crtc)
  2801. {
  2802. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2803. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2804. unsigned long irqflags;
  2805. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2806. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2807. if (INTEL_GEN(dev_priv) >= 4)
  2808. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2809. else
  2810. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2811. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2812. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2813. }
  2814. static bool i9xx_plane_get_hw_state(struct intel_plane *plane)
  2815. {
  2816. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2817. enum intel_display_power_domain power_domain;
  2818. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2819. enum pipe pipe = plane->pipe;
  2820. bool ret;
  2821. /*
  2822. * Not 100% correct for planes that can move between pipes,
  2823. * but that's only the case for gen2-4 which don't have any
  2824. * display power wells.
  2825. */
  2826. power_domain = POWER_DOMAIN_PIPE(pipe);
  2827. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2828. return false;
  2829. ret = I915_READ(DSPCNTR(i9xx_plane)) & DISPLAY_PLANE_ENABLE;
  2830. intel_display_power_put(dev_priv, power_domain);
  2831. return ret;
  2832. }
  2833. static u32
  2834. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2835. {
  2836. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2837. return 64;
  2838. else
  2839. return intel_tile_width_bytes(fb, plane);
  2840. }
  2841. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2842. {
  2843. struct drm_device *dev = intel_crtc->base.dev;
  2844. struct drm_i915_private *dev_priv = to_i915(dev);
  2845. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2846. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2847. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2848. }
  2849. /*
  2850. * This function detaches (aka. unbinds) unused scalers in hardware
  2851. */
  2852. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2853. {
  2854. struct intel_crtc_scaler_state *scaler_state;
  2855. int i;
  2856. scaler_state = &intel_crtc->config->scaler_state;
  2857. /* loop through and disable scalers that aren't in use */
  2858. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2859. if (!scaler_state->scalers[i].in_use)
  2860. skl_detach_scaler(intel_crtc, i);
  2861. }
  2862. }
  2863. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2864. unsigned int rotation)
  2865. {
  2866. u32 stride;
  2867. if (plane >= fb->format->num_planes)
  2868. return 0;
  2869. stride = intel_fb_pitch(fb, plane, rotation);
  2870. /*
  2871. * The stride is either expressed as a multiple of 64 bytes chunks for
  2872. * linear buffers or in number of tiles for tiled buffers.
  2873. */
  2874. if (drm_rotation_90_or_270(rotation))
  2875. stride /= intel_tile_height(fb, plane);
  2876. else
  2877. stride /= intel_fb_stride_alignment(fb, plane);
  2878. return stride;
  2879. }
  2880. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2881. {
  2882. switch (pixel_format) {
  2883. case DRM_FORMAT_C8:
  2884. return PLANE_CTL_FORMAT_INDEXED;
  2885. case DRM_FORMAT_RGB565:
  2886. return PLANE_CTL_FORMAT_RGB_565;
  2887. case DRM_FORMAT_XBGR8888:
  2888. case DRM_FORMAT_ABGR8888:
  2889. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2890. case DRM_FORMAT_XRGB8888:
  2891. case DRM_FORMAT_ARGB8888:
  2892. return PLANE_CTL_FORMAT_XRGB_8888;
  2893. case DRM_FORMAT_XRGB2101010:
  2894. return PLANE_CTL_FORMAT_XRGB_2101010;
  2895. case DRM_FORMAT_XBGR2101010:
  2896. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2897. case DRM_FORMAT_YUYV:
  2898. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2899. case DRM_FORMAT_YVYU:
  2900. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2901. case DRM_FORMAT_UYVY:
  2902. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2903. case DRM_FORMAT_VYUY:
  2904. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2905. default:
  2906. MISSING_CASE(pixel_format);
  2907. }
  2908. return 0;
  2909. }
  2910. /*
  2911. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2912. * to be already pre-multiplied. We need to add a knob (or a different
  2913. * DRM_FORMAT) for user-space to configure that.
  2914. */
  2915. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  2916. {
  2917. switch (pixel_format) {
  2918. case DRM_FORMAT_ABGR8888:
  2919. case DRM_FORMAT_ARGB8888:
  2920. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2921. default:
  2922. return PLANE_CTL_ALPHA_DISABLE;
  2923. }
  2924. }
  2925. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  2926. {
  2927. switch (pixel_format) {
  2928. case DRM_FORMAT_ABGR8888:
  2929. case DRM_FORMAT_ARGB8888:
  2930. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  2931. default:
  2932. return PLANE_COLOR_ALPHA_DISABLE;
  2933. }
  2934. }
  2935. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2936. {
  2937. switch (fb_modifier) {
  2938. case DRM_FORMAT_MOD_LINEAR:
  2939. break;
  2940. case I915_FORMAT_MOD_X_TILED:
  2941. return PLANE_CTL_TILED_X;
  2942. case I915_FORMAT_MOD_Y_TILED:
  2943. return PLANE_CTL_TILED_Y;
  2944. case I915_FORMAT_MOD_Y_TILED_CCS:
  2945. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2946. case I915_FORMAT_MOD_Yf_TILED:
  2947. return PLANE_CTL_TILED_YF;
  2948. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2949. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  2950. default:
  2951. MISSING_CASE(fb_modifier);
  2952. }
  2953. return 0;
  2954. }
  2955. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  2956. {
  2957. switch (rotate) {
  2958. case DRM_MODE_ROTATE_0:
  2959. break;
  2960. /*
  2961. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2962. * while i915 HW rotation is clockwise, thats why this swapping.
  2963. */
  2964. case DRM_MODE_ROTATE_90:
  2965. return PLANE_CTL_ROTATE_270;
  2966. case DRM_MODE_ROTATE_180:
  2967. return PLANE_CTL_ROTATE_180;
  2968. case DRM_MODE_ROTATE_270:
  2969. return PLANE_CTL_ROTATE_90;
  2970. default:
  2971. MISSING_CASE(rotate);
  2972. }
  2973. return 0;
  2974. }
  2975. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  2976. {
  2977. switch (reflect) {
  2978. case 0:
  2979. break;
  2980. case DRM_MODE_REFLECT_X:
  2981. return PLANE_CTL_FLIP_HORIZONTAL;
  2982. case DRM_MODE_REFLECT_Y:
  2983. default:
  2984. MISSING_CASE(reflect);
  2985. }
  2986. return 0;
  2987. }
  2988. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  2989. const struct intel_plane_state *plane_state)
  2990. {
  2991. struct drm_i915_private *dev_priv =
  2992. to_i915(plane_state->base.plane->dev);
  2993. const struct drm_framebuffer *fb = plane_state->base.fb;
  2994. unsigned int rotation = plane_state->base.rotation;
  2995. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  2996. u32 plane_ctl;
  2997. plane_ctl = PLANE_CTL_ENABLE;
  2998. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  2999. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3000. plane_ctl |=
  3001. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3002. PLANE_CTL_PIPE_CSC_ENABLE |
  3003. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3004. }
  3005. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3006. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3007. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3008. if (INTEL_GEN(dev_priv) >= 10)
  3009. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3010. DRM_MODE_REFLECT_MASK);
  3011. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3012. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3013. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3014. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3015. return plane_ctl;
  3016. }
  3017. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3018. const struct intel_plane_state *plane_state)
  3019. {
  3020. const struct drm_framebuffer *fb = plane_state->base.fb;
  3021. u32 plane_color_ctl = 0;
  3022. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3023. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3024. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3025. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3026. return plane_color_ctl;
  3027. }
  3028. static int
  3029. __intel_display_resume(struct drm_device *dev,
  3030. struct drm_atomic_state *state,
  3031. struct drm_modeset_acquire_ctx *ctx)
  3032. {
  3033. struct drm_crtc_state *crtc_state;
  3034. struct drm_crtc *crtc;
  3035. int i, ret;
  3036. intel_modeset_setup_hw_state(dev, ctx);
  3037. i915_redisable_vga(to_i915(dev));
  3038. if (!state)
  3039. return 0;
  3040. /*
  3041. * We've duplicated the state, pointers to the old state are invalid.
  3042. *
  3043. * Don't attempt to use the old state until we commit the duplicated state.
  3044. */
  3045. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3046. /*
  3047. * Force recalculation even if we restore
  3048. * current state. With fast modeset this may not result
  3049. * in a modeset when the state is compatible.
  3050. */
  3051. crtc_state->mode_changed = true;
  3052. }
  3053. /* ignore any reset values/BIOS leftovers in the WM registers */
  3054. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3055. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3056. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3057. WARN_ON(ret == -EDEADLK);
  3058. return ret;
  3059. }
  3060. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3061. {
  3062. return intel_has_gpu_reset(dev_priv) &&
  3063. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3064. }
  3065. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3066. {
  3067. struct drm_device *dev = &dev_priv->drm;
  3068. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3069. struct drm_atomic_state *state;
  3070. int ret;
  3071. /* reset doesn't touch the display */
  3072. if (!i915_modparams.force_reset_modeset_test &&
  3073. !gpu_reset_clobbers_display(dev_priv))
  3074. return;
  3075. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3076. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3077. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3078. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3079. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3080. i915_gem_set_wedged(dev_priv);
  3081. }
  3082. /*
  3083. * Need mode_config.mutex so that we don't
  3084. * trample ongoing ->detect() and whatnot.
  3085. */
  3086. mutex_lock(&dev->mode_config.mutex);
  3087. drm_modeset_acquire_init(ctx, 0);
  3088. while (1) {
  3089. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3090. if (ret != -EDEADLK)
  3091. break;
  3092. drm_modeset_backoff(ctx);
  3093. }
  3094. /*
  3095. * Disabling the crtcs gracefully seems nicer. Also the
  3096. * g33 docs say we should at least disable all the planes.
  3097. */
  3098. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3099. if (IS_ERR(state)) {
  3100. ret = PTR_ERR(state);
  3101. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3102. return;
  3103. }
  3104. ret = drm_atomic_helper_disable_all(dev, ctx);
  3105. if (ret) {
  3106. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3107. drm_atomic_state_put(state);
  3108. return;
  3109. }
  3110. dev_priv->modeset_restore_state = state;
  3111. state->acquire_ctx = ctx;
  3112. }
  3113. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3114. {
  3115. struct drm_device *dev = &dev_priv->drm;
  3116. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3117. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3118. int ret;
  3119. /* reset doesn't touch the display */
  3120. if (!i915_modparams.force_reset_modeset_test &&
  3121. !gpu_reset_clobbers_display(dev_priv))
  3122. return;
  3123. if (!state)
  3124. goto unlock;
  3125. dev_priv->modeset_restore_state = NULL;
  3126. /* reset doesn't touch the display */
  3127. if (!gpu_reset_clobbers_display(dev_priv)) {
  3128. /* for testing only restore the display */
  3129. ret = __intel_display_resume(dev, state, ctx);
  3130. if (ret)
  3131. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3132. } else {
  3133. /*
  3134. * The display has been reset as well,
  3135. * so need a full re-initialization.
  3136. */
  3137. intel_runtime_pm_disable_interrupts(dev_priv);
  3138. intel_runtime_pm_enable_interrupts(dev_priv);
  3139. intel_pps_unlock_regs_wa(dev_priv);
  3140. intel_modeset_init_hw(dev);
  3141. intel_init_clock_gating(dev_priv);
  3142. spin_lock_irq(&dev_priv->irq_lock);
  3143. if (dev_priv->display.hpd_irq_setup)
  3144. dev_priv->display.hpd_irq_setup(dev_priv);
  3145. spin_unlock_irq(&dev_priv->irq_lock);
  3146. ret = __intel_display_resume(dev, state, ctx);
  3147. if (ret)
  3148. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3149. intel_hpd_init(dev_priv);
  3150. }
  3151. drm_atomic_state_put(state);
  3152. unlock:
  3153. drm_modeset_drop_locks(ctx);
  3154. drm_modeset_acquire_fini(ctx);
  3155. mutex_unlock(&dev->mode_config.mutex);
  3156. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3157. }
  3158. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3159. const struct intel_crtc_state *new_crtc_state)
  3160. {
  3161. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3162. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3163. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3164. crtc->base.mode = new_crtc_state->base.mode;
  3165. /*
  3166. * Update pipe size and adjust fitter if needed: the reason for this is
  3167. * that in compute_mode_changes we check the native mode (not the pfit
  3168. * mode) to see if we can flip rather than do a full mode set. In the
  3169. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3170. * pfit state, we'll end up with a big fb scanned out into the wrong
  3171. * sized surface.
  3172. */
  3173. I915_WRITE(PIPESRC(crtc->pipe),
  3174. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3175. (new_crtc_state->pipe_src_h - 1));
  3176. /* on skylake this is done by detaching scalers */
  3177. if (INTEL_GEN(dev_priv) >= 9) {
  3178. skl_detach_scalers(crtc);
  3179. if (new_crtc_state->pch_pfit.enabled)
  3180. skylake_pfit_enable(crtc);
  3181. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3182. if (new_crtc_state->pch_pfit.enabled)
  3183. ironlake_pfit_enable(crtc);
  3184. else if (old_crtc_state->pch_pfit.enabled)
  3185. ironlake_pfit_disable(crtc, true);
  3186. }
  3187. }
  3188. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3189. {
  3190. struct drm_device *dev = crtc->base.dev;
  3191. struct drm_i915_private *dev_priv = to_i915(dev);
  3192. int pipe = crtc->pipe;
  3193. i915_reg_t reg;
  3194. u32 temp;
  3195. /* enable normal train */
  3196. reg = FDI_TX_CTL(pipe);
  3197. temp = I915_READ(reg);
  3198. if (IS_IVYBRIDGE(dev_priv)) {
  3199. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3200. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3201. } else {
  3202. temp &= ~FDI_LINK_TRAIN_NONE;
  3203. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3204. }
  3205. I915_WRITE(reg, temp);
  3206. reg = FDI_RX_CTL(pipe);
  3207. temp = I915_READ(reg);
  3208. if (HAS_PCH_CPT(dev_priv)) {
  3209. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3210. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3211. } else {
  3212. temp &= ~FDI_LINK_TRAIN_NONE;
  3213. temp |= FDI_LINK_TRAIN_NONE;
  3214. }
  3215. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3216. /* wait one idle pattern time */
  3217. POSTING_READ(reg);
  3218. udelay(1000);
  3219. /* IVB wants error correction enabled */
  3220. if (IS_IVYBRIDGE(dev_priv))
  3221. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3222. FDI_FE_ERRC_ENABLE);
  3223. }
  3224. /* The FDI link training functions for ILK/Ibexpeak. */
  3225. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3226. const struct intel_crtc_state *crtc_state)
  3227. {
  3228. struct drm_device *dev = crtc->base.dev;
  3229. struct drm_i915_private *dev_priv = to_i915(dev);
  3230. int pipe = crtc->pipe;
  3231. i915_reg_t reg;
  3232. u32 temp, tries;
  3233. /* FDI needs bits from pipe first */
  3234. assert_pipe_enabled(dev_priv, pipe);
  3235. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3236. for train result */
  3237. reg = FDI_RX_IMR(pipe);
  3238. temp = I915_READ(reg);
  3239. temp &= ~FDI_RX_SYMBOL_LOCK;
  3240. temp &= ~FDI_RX_BIT_LOCK;
  3241. I915_WRITE(reg, temp);
  3242. I915_READ(reg);
  3243. udelay(150);
  3244. /* enable CPU FDI TX and PCH FDI RX */
  3245. reg = FDI_TX_CTL(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3248. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3249. temp &= ~FDI_LINK_TRAIN_NONE;
  3250. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3251. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3252. reg = FDI_RX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. temp &= ~FDI_LINK_TRAIN_NONE;
  3255. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3256. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3257. POSTING_READ(reg);
  3258. udelay(150);
  3259. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3260. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3261. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3262. FDI_RX_PHASE_SYNC_POINTER_EN);
  3263. reg = FDI_RX_IIR(pipe);
  3264. for (tries = 0; tries < 5; tries++) {
  3265. temp = I915_READ(reg);
  3266. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3267. if ((temp & FDI_RX_BIT_LOCK)) {
  3268. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3269. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3270. break;
  3271. }
  3272. }
  3273. if (tries == 5)
  3274. DRM_ERROR("FDI train 1 fail!\n");
  3275. /* Train 2 */
  3276. reg = FDI_TX_CTL(pipe);
  3277. temp = I915_READ(reg);
  3278. temp &= ~FDI_LINK_TRAIN_NONE;
  3279. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3280. I915_WRITE(reg, temp);
  3281. reg = FDI_RX_CTL(pipe);
  3282. temp = I915_READ(reg);
  3283. temp &= ~FDI_LINK_TRAIN_NONE;
  3284. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3285. I915_WRITE(reg, temp);
  3286. POSTING_READ(reg);
  3287. udelay(150);
  3288. reg = FDI_RX_IIR(pipe);
  3289. for (tries = 0; tries < 5; tries++) {
  3290. temp = I915_READ(reg);
  3291. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3292. if (temp & FDI_RX_SYMBOL_LOCK) {
  3293. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3294. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3295. break;
  3296. }
  3297. }
  3298. if (tries == 5)
  3299. DRM_ERROR("FDI train 2 fail!\n");
  3300. DRM_DEBUG_KMS("FDI train done\n");
  3301. }
  3302. static const int snb_b_fdi_train_param[] = {
  3303. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3304. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3305. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3306. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3307. };
  3308. /* The FDI link training functions for SNB/Cougarpoint. */
  3309. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3310. const struct intel_crtc_state *crtc_state)
  3311. {
  3312. struct drm_device *dev = crtc->base.dev;
  3313. struct drm_i915_private *dev_priv = to_i915(dev);
  3314. int pipe = crtc->pipe;
  3315. i915_reg_t reg;
  3316. u32 temp, i, retry;
  3317. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3318. for train result */
  3319. reg = FDI_RX_IMR(pipe);
  3320. temp = I915_READ(reg);
  3321. temp &= ~FDI_RX_SYMBOL_LOCK;
  3322. temp &= ~FDI_RX_BIT_LOCK;
  3323. I915_WRITE(reg, temp);
  3324. POSTING_READ(reg);
  3325. udelay(150);
  3326. /* enable CPU FDI TX and PCH FDI RX */
  3327. reg = FDI_TX_CTL(pipe);
  3328. temp = I915_READ(reg);
  3329. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3330. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3331. temp &= ~FDI_LINK_TRAIN_NONE;
  3332. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3333. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3334. /* SNB-B */
  3335. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3336. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3337. I915_WRITE(FDI_RX_MISC(pipe),
  3338. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3339. reg = FDI_RX_CTL(pipe);
  3340. temp = I915_READ(reg);
  3341. if (HAS_PCH_CPT(dev_priv)) {
  3342. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3343. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3344. } else {
  3345. temp &= ~FDI_LINK_TRAIN_NONE;
  3346. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3347. }
  3348. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3349. POSTING_READ(reg);
  3350. udelay(150);
  3351. for (i = 0; i < 4; i++) {
  3352. reg = FDI_TX_CTL(pipe);
  3353. temp = I915_READ(reg);
  3354. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3355. temp |= snb_b_fdi_train_param[i];
  3356. I915_WRITE(reg, temp);
  3357. POSTING_READ(reg);
  3358. udelay(500);
  3359. for (retry = 0; retry < 5; retry++) {
  3360. reg = FDI_RX_IIR(pipe);
  3361. temp = I915_READ(reg);
  3362. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3363. if (temp & FDI_RX_BIT_LOCK) {
  3364. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3365. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3366. break;
  3367. }
  3368. udelay(50);
  3369. }
  3370. if (retry < 5)
  3371. break;
  3372. }
  3373. if (i == 4)
  3374. DRM_ERROR("FDI train 1 fail!\n");
  3375. /* Train 2 */
  3376. reg = FDI_TX_CTL(pipe);
  3377. temp = I915_READ(reg);
  3378. temp &= ~FDI_LINK_TRAIN_NONE;
  3379. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3380. if (IS_GEN6(dev_priv)) {
  3381. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3382. /* SNB-B */
  3383. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3384. }
  3385. I915_WRITE(reg, temp);
  3386. reg = FDI_RX_CTL(pipe);
  3387. temp = I915_READ(reg);
  3388. if (HAS_PCH_CPT(dev_priv)) {
  3389. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3390. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3391. } else {
  3392. temp &= ~FDI_LINK_TRAIN_NONE;
  3393. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3394. }
  3395. I915_WRITE(reg, temp);
  3396. POSTING_READ(reg);
  3397. udelay(150);
  3398. for (i = 0; i < 4; i++) {
  3399. reg = FDI_TX_CTL(pipe);
  3400. temp = I915_READ(reg);
  3401. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3402. temp |= snb_b_fdi_train_param[i];
  3403. I915_WRITE(reg, temp);
  3404. POSTING_READ(reg);
  3405. udelay(500);
  3406. for (retry = 0; retry < 5; retry++) {
  3407. reg = FDI_RX_IIR(pipe);
  3408. temp = I915_READ(reg);
  3409. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3410. if (temp & FDI_RX_SYMBOL_LOCK) {
  3411. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3412. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3413. break;
  3414. }
  3415. udelay(50);
  3416. }
  3417. if (retry < 5)
  3418. break;
  3419. }
  3420. if (i == 4)
  3421. DRM_ERROR("FDI train 2 fail!\n");
  3422. DRM_DEBUG_KMS("FDI train done.\n");
  3423. }
  3424. /* Manual link training for Ivy Bridge A0 parts */
  3425. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3426. const struct intel_crtc_state *crtc_state)
  3427. {
  3428. struct drm_device *dev = crtc->base.dev;
  3429. struct drm_i915_private *dev_priv = to_i915(dev);
  3430. int pipe = crtc->pipe;
  3431. i915_reg_t reg;
  3432. u32 temp, i, j;
  3433. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3434. for train result */
  3435. reg = FDI_RX_IMR(pipe);
  3436. temp = I915_READ(reg);
  3437. temp &= ~FDI_RX_SYMBOL_LOCK;
  3438. temp &= ~FDI_RX_BIT_LOCK;
  3439. I915_WRITE(reg, temp);
  3440. POSTING_READ(reg);
  3441. udelay(150);
  3442. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3443. I915_READ(FDI_RX_IIR(pipe)));
  3444. /* Try each vswing and preemphasis setting twice before moving on */
  3445. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3446. /* disable first in case we need to retry */
  3447. reg = FDI_TX_CTL(pipe);
  3448. temp = I915_READ(reg);
  3449. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3450. temp &= ~FDI_TX_ENABLE;
  3451. I915_WRITE(reg, temp);
  3452. reg = FDI_RX_CTL(pipe);
  3453. temp = I915_READ(reg);
  3454. temp &= ~FDI_LINK_TRAIN_AUTO;
  3455. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3456. temp &= ~FDI_RX_ENABLE;
  3457. I915_WRITE(reg, temp);
  3458. /* enable CPU FDI TX and PCH FDI RX */
  3459. reg = FDI_TX_CTL(pipe);
  3460. temp = I915_READ(reg);
  3461. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3462. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3463. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3464. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3465. temp |= snb_b_fdi_train_param[j/2];
  3466. temp |= FDI_COMPOSITE_SYNC;
  3467. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3468. I915_WRITE(FDI_RX_MISC(pipe),
  3469. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3470. reg = FDI_RX_CTL(pipe);
  3471. temp = I915_READ(reg);
  3472. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3473. temp |= FDI_COMPOSITE_SYNC;
  3474. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3475. POSTING_READ(reg);
  3476. udelay(1); /* should be 0.5us */
  3477. for (i = 0; i < 4; i++) {
  3478. reg = FDI_RX_IIR(pipe);
  3479. temp = I915_READ(reg);
  3480. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3481. if (temp & FDI_RX_BIT_LOCK ||
  3482. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3483. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3484. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3485. i);
  3486. break;
  3487. }
  3488. udelay(1); /* should be 0.5us */
  3489. }
  3490. if (i == 4) {
  3491. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3492. continue;
  3493. }
  3494. /* Train 2 */
  3495. reg = FDI_TX_CTL(pipe);
  3496. temp = I915_READ(reg);
  3497. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3498. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3499. I915_WRITE(reg, temp);
  3500. reg = FDI_RX_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3503. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3504. I915_WRITE(reg, temp);
  3505. POSTING_READ(reg);
  3506. udelay(2); /* should be 1.5us */
  3507. for (i = 0; i < 4; i++) {
  3508. reg = FDI_RX_IIR(pipe);
  3509. temp = I915_READ(reg);
  3510. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3511. if (temp & FDI_RX_SYMBOL_LOCK ||
  3512. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3513. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3514. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3515. i);
  3516. goto train_done;
  3517. }
  3518. udelay(2); /* should be 1.5us */
  3519. }
  3520. if (i == 4)
  3521. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3522. }
  3523. train_done:
  3524. DRM_DEBUG_KMS("FDI train done.\n");
  3525. }
  3526. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3527. {
  3528. struct drm_device *dev = intel_crtc->base.dev;
  3529. struct drm_i915_private *dev_priv = to_i915(dev);
  3530. int pipe = intel_crtc->pipe;
  3531. i915_reg_t reg;
  3532. u32 temp;
  3533. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3534. reg = FDI_RX_CTL(pipe);
  3535. temp = I915_READ(reg);
  3536. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3537. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3538. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3539. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3540. POSTING_READ(reg);
  3541. udelay(200);
  3542. /* Switch from Rawclk to PCDclk */
  3543. temp = I915_READ(reg);
  3544. I915_WRITE(reg, temp | FDI_PCDCLK);
  3545. POSTING_READ(reg);
  3546. udelay(200);
  3547. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3548. reg = FDI_TX_CTL(pipe);
  3549. temp = I915_READ(reg);
  3550. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3551. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3552. POSTING_READ(reg);
  3553. udelay(100);
  3554. }
  3555. }
  3556. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3557. {
  3558. struct drm_device *dev = intel_crtc->base.dev;
  3559. struct drm_i915_private *dev_priv = to_i915(dev);
  3560. int pipe = intel_crtc->pipe;
  3561. i915_reg_t reg;
  3562. u32 temp;
  3563. /* Switch from PCDclk to Rawclk */
  3564. reg = FDI_RX_CTL(pipe);
  3565. temp = I915_READ(reg);
  3566. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3567. /* Disable CPU FDI TX PLL */
  3568. reg = FDI_TX_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3571. POSTING_READ(reg);
  3572. udelay(100);
  3573. reg = FDI_RX_CTL(pipe);
  3574. temp = I915_READ(reg);
  3575. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3576. /* Wait for the clocks to turn off. */
  3577. POSTING_READ(reg);
  3578. udelay(100);
  3579. }
  3580. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3581. {
  3582. struct drm_device *dev = crtc->dev;
  3583. struct drm_i915_private *dev_priv = to_i915(dev);
  3584. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3585. int pipe = intel_crtc->pipe;
  3586. i915_reg_t reg;
  3587. u32 temp;
  3588. /* disable CPU FDI tx and PCH FDI rx */
  3589. reg = FDI_TX_CTL(pipe);
  3590. temp = I915_READ(reg);
  3591. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3592. POSTING_READ(reg);
  3593. reg = FDI_RX_CTL(pipe);
  3594. temp = I915_READ(reg);
  3595. temp &= ~(0x7 << 16);
  3596. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3597. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3598. POSTING_READ(reg);
  3599. udelay(100);
  3600. /* Ironlake workaround, disable clock pointer after downing FDI */
  3601. if (HAS_PCH_IBX(dev_priv))
  3602. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3603. /* still set train pattern 1 */
  3604. reg = FDI_TX_CTL(pipe);
  3605. temp = I915_READ(reg);
  3606. temp &= ~FDI_LINK_TRAIN_NONE;
  3607. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3608. I915_WRITE(reg, temp);
  3609. reg = FDI_RX_CTL(pipe);
  3610. temp = I915_READ(reg);
  3611. if (HAS_PCH_CPT(dev_priv)) {
  3612. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3613. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3614. } else {
  3615. temp &= ~FDI_LINK_TRAIN_NONE;
  3616. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3617. }
  3618. /* BPC in FDI rx is consistent with that in PIPECONF */
  3619. temp &= ~(0x07 << 16);
  3620. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3621. I915_WRITE(reg, temp);
  3622. POSTING_READ(reg);
  3623. udelay(100);
  3624. }
  3625. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3626. {
  3627. struct drm_crtc *crtc;
  3628. bool cleanup_done;
  3629. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3630. struct drm_crtc_commit *commit;
  3631. spin_lock(&crtc->commit_lock);
  3632. commit = list_first_entry_or_null(&crtc->commit_list,
  3633. struct drm_crtc_commit, commit_entry);
  3634. cleanup_done = commit ?
  3635. try_wait_for_completion(&commit->cleanup_done) : true;
  3636. spin_unlock(&crtc->commit_lock);
  3637. if (cleanup_done)
  3638. continue;
  3639. drm_crtc_wait_one_vblank(crtc);
  3640. return true;
  3641. }
  3642. return false;
  3643. }
  3644. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3645. {
  3646. u32 temp;
  3647. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3648. mutex_lock(&dev_priv->sb_lock);
  3649. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3650. temp |= SBI_SSCCTL_DISABLE;
  3651. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3652. mutex_unlock(&dev_priv->sb_lock);
  3653. }
  3654. /* Program iCLKIP clock to the desired frequency */
  3655. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3656. {
  3657. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3658. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3659. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3660. u32 temp;
  3661. lpt_disable_iclkip(dev_priv);
  3662. /* The iCLK virtual clock root frequency is in MHz,
  3663. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3664. * divisors, it is necessary to divide one by another, so we
  3665. * convert the virtual clock precision to KHz here for higher
  3666. * precision.
  3667. */
  3668. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3669. u32 iclk_virtual_root_freq = 172800 * 1000;
  3670. u32 iclk_pi_range = 64;
  3671. u32 desired_divisor;
  3672. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3673. clock << auxdiv);
  3674. divsel = (desired_divisor / iclk_pi_range) - 2;
  3675. phaseinc = desired_divisor % iclk_pi_range;
  3676. /*
  3677. * Near 20MHz is a corner case which is
  3678. * out of range for the 7-bit divisor
  3679. */
  3680. if (divsel <= 0x7f)
  3681. break;
  3682. }
  3683. /* This should not happen with any sane values */
  3684. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3685. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3686. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3687. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3688. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3689. clock,
  3690. auxdiv,
  3691. divsel,
  3692. phasedir,
  3693. phaseinc);
  3694. mutex_lock(&dev_priv->sb_lock);
  3695. /* Program SSCDIVINTPHASE6 */
  3696. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3697. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3698. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3699. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3700. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3701. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3702. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3703. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3704. /* Program SSCAUXDIV */
  3705. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3706. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3707. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3708. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3709. /* Enable modulator and associated divider */
  3710. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3711. temp &= ~SBI_SSCCTL_DISABLE;
  3712. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3713. mutex_unlock(&dev_priv->sb_lock);
  3714. /* Wait for initialization time */
  3715. udelay(24);
  3716. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3717. }
  3718. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3719. {
  3720. u32 divsel, phaseinc, auxdiv;
  3721. u32 iclk_virtual_root_freq = 172800 * 1000;
  3722. u32 iclk_pi_range = 64;
  3723. u32 desired_divisor;
  3724. u32 temp;
  3725. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3726. return 0;
  3727. mutex_lock(&dev_priv->sb_lock);
  3728. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3729. if (temp & SBI_SSCCTL_DISABLE) {
  3730. mutex_unlock(&dev_priv->sb_lock);
  3731. return 0;
  3732. }
  3733. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3734. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3735. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3736. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3737. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3738. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3739. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3740. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3741. mutex_unlock(&dev_priv->sb_lock);
  3742. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3743. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3744. desired_divisor << auxdiv);
  3745. }
  3746. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3747. enum pipe pch_transcoder)
  3748. {
  3749. struct drm_device *dev = crtc->base.dev;
  3750. struct drm_i915_private *dev_priv = to_i915(dev);
  3751. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3752. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3753. I915_READ(HTOTAL(cpu_transcoder)));
  3754. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3755. I915_READ(HBLANK(cpu_transcoder)));
  3756. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3757. I915_READ(HSYNC(cpu_transcoder)));
  3758. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3759. I915_READ(VTOTAL(cpu_transcoder)));
  3760. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3761. I915_READ(VBLANK(cpu_transcoder)));
  3762. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3763. I915_READ(VSYNC(cpu_transcoder)));
  3764. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3765. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3766. }
  3767. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3768. {
  3769. struct drm_i915_private *dev_priv = to_i915(dev);
  3770. uint32_t temp;
  3771. temp = I915_READ(SOUTH_CHICKEN1);
  3772. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3773. return;
  3774. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3775. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3776. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3777. if (enable)
  3778. temp |= FDI_BC_BIFURCATION_SELECT;
  3779. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3780. I915_WRITE(SOUTH_CHICKEN1, temp);
  3781. POSTING_READ(SOUTH_CHICKEN1);
  3782. }
  3783. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3784. {
  3785. struct drm_device *dev = intel_crtc->base.dev;
  3786. switch (intel_crtc->pipe) {
  3787. case PIPE_A:
  3788. break;
  3789. case PIPE_B:
  3790. if (intel_crtc->config->fdi_lanes > 2)
  3791. cpt_set_fdi_bc_bifurcation(dev, false);
  3792. else
  3793. cpt_set_fdi_bc_bifurcation(dev, true);
  3794. break;
  3795. case PIPE_C:
  3796. cpt_set_fdi_bc_bifurcation(dev, true);
  3797. break;
  3798. default:
  3799. BUG();
  3800. }
  3801. }
  3802. /* Return which DP Port should be selected for Transcoder DP control */
  3803. static enum port
  3804. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3805. {
  3806. struct drm_device *dev = crtc->base.dev;
  3807. struct intel_encoder *encoder;
  3808. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3809. if (encoder->type == INTEL_OUTPUT_DP ||
  3810. encoder->type == INTEL_OUTPUT_EDP)
  3811. return encoder->port;
  3812. }
  3813. return -1;
  3814. }
  3815. /*
  3816. * Enable PCH resources required for PCH ports:
  3817. * - PCH PLLs
  3818. * - FDI training & RX/TX
  3819. * - update transcoder timings
  3820. * - DP transcoding bits
  3821. * - transcoder
  3822. */
  3823. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3824. {
  3825. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3826. struct drm_device *dev = crtc->base.dev;
  3827. struct drm_i915_private *dev_priv = to_i915(dev);
  3828. int pipe = crtc->pipe;
  3829. u32 temp;
  3830. assert_pch_transcoder_disabled(dev_priv, pipe);
  3831. if (IS_IVYBRIDGE(dev_priv))
  3832. ivybridge_update_fdi_bc_bifurcation(crtc);
  3833. /* Write the TU size bits before fdi link training, so that error
  3834. * detection works. */
  3835. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3836. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3837. /* For PCH output, training FDI link */
  3838. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3839. /* We need to program the right clock selection before writing the pixel
  3840. * mutliplier into the DPLL. */
  3841. if (HAS_PCH_CPT(dev_priv)) {
  3842. u32 sel;
  3843. temp = I915_READ(PCH_DPLL_SEL);
  3844. temp |= TRANS_DPLL_ENABLE(pipe);
  3845. sel = TRANS_DPLLB_SEL(pipe);
  3846. if (crtc_state->shared_dpll ==
  3847. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3848. temp |= sel;
  3849. else
  3850. temp &= ~sel;
  3851. I915_WRITE(PCH_DPLL_SEL, temp);
  3852. }
  3853. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3854. * transcoder, and we actually should do this to not upset any PCH
  3855. * transcoder that already use the clock when we share it.
  3856. *
  3857. * Note that enable_shared_dpll tries to do the right thing, but
  3858. * get_shared_dpll unconditionally resets the pll - we need that to have
  3859. * the right LVDS enable sequence. */
  3860. intel_enable_shared_dpll(crtc);
  3861. /* set transcoder timing, panel must allow it */
  3862. assert_panel_unlocked(dev_priv, pipe);
  3863. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3864. intel_fdi_normal_train(crtc);
  3865. /* For PCH DP, enable TRANS_DP_CTL */
  3866. if (HAS_PCH_CPT(dev_priv) &&
  3867. intel_crtc_has_dp_encoder(crtc_state)) {
  3868. const struct drm_display_mode *adjusted_mode =
  3869. &crtc_state->base.adjusted_mode;
  3870. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3871. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3872. temp = I915_READ(reg);
  3873. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3874. TRANS_DP_SYNC_MASK |
  3875. TRANS_DP_BPC_MASK);
  3876. temp |= TRANS_DP_OUTPUT_ENABLE;
  3877. temp |= bpc << 9; /* same format but at 11:9 */
  3878. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3879. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3880. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3881. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3882. switch (intel_trans_dp_port_sel(crtc)) {
  3883. case PORT_B:
  3884. temp |= TRANS_DP_PORT_SEL_B;
  3885. break;
  3886. case PORT_C:
  3887. temp |= TRANS_DP_PORT_SEL_C;
  3888. break;
  3889. case PORT_D:
  3890. temp |= TRANS_DP_PORT_SEL_D;
  3891. break;
  3892. default:
  3893. BUG();
  3894. }
  3895. I915_WRITE(reg, temp);
  3896. }
  3897. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3898. }
  3899. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3900. {
  3901. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3902. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3903. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3904. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3905. lpt_program_iclkip(crtc);
  3906. /* Set transcoder timing. */
  3907. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3908. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3909. }
  3910. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3911. {
  3912. struct drm_i915_private *dev_priv = to_i915(dev);
  3913. i915_reg_t dslreg = PIPEDSL(pipe);
  3914. u32 temp;
  3915. temp = I915_READ(dslreg);
  3916. udelay(500);
  3917. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3918. if (wait_for(I915_READ(dslreg) != temp, 5))
  3919. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3920. }
  3921. }
  3922. static int
  3923. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3924. unsigned int scaler_user, int *scaler_id,
  3925. int src_w, int src_h, int dst_w, int dst_h)
  3926. {
  3927. struct intel_crtc_scaler_state *scaler_state =
  3928. &crtc_state->scaler_state;
  3929. struct intel_crtc *intel_crtc =
  3930. to_intel_crtc(crtc_state->base.crtc);
  3931. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3932. const struct drm_display_mode *adjusted_mode =
  3933. &crtc_state->base.adjusted_mode;
  3934. int need_scaling;
  3935. /*
  3936. * Src coordinates are already rotated by 270 degrees for
  3937. * the 90/270 degree plane rotation cases (to match the
  3938. * GTT mapping), hence no need to account for rotation here.
  3939. */
  3940. need_scaling = src_w != dst_w || src_h != dst_h;
  3941. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  3942. need_scaling = true;
  3943. /*
  3944. * Scaling/fitting not supported in IF-ID mode in GEN9+
  3945. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  3946. * Once NV12 is enabled, handle it here while allocating scaler
  3947. * for NV12.
  3948. */
  3949. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  3950. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3951. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  3952. return -EINVAL;
  3953. }
  3954. /*
  3955. * if plane is being disabled or scaler is no more required or force detach
  3956. * - free scaler binded to this plane/crtc
  3957. * - in order to do this, update crtc->scaler_usage
  3958. *
  3959. * Here scaler state in crtc_state is set free so that
  3960. * scaler can be assigned to other user. Actual register
  3961. * update to free the scaler is done in plane/panel-fit programming.
  3962. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3963. */
  3964. if (force_detach || !need_scaling) {
  3965. if (*scaler_id >= 0) {
  3966. scaler_state->scaler_users &= ~(1 << scaler_user);
  3967. scaler_state->scalers[*scaler_id].in_use = 0;
  3968. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3969. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3970. intel_crtc->pipe, scaler_user, *scaler_id,
  3971. scaler_state->scaler_users);
  3972. *scaler_id = -1;
  3973. }
  3974. return 0;
  3975. }
  3976. /* range checks */
  3977. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3978. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3979. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3980. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3981. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3982. "size is out of scaler range\n",
  3983. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3984. return -EINVAL;
  3985. }
  3986. /* mark this plane as a scaler user in crtc_state */
  3987. scaler_state->scaler_users |= (1 << scaler_user);
  3988. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3989. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3990. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3991. scaler_state->scaler_users);
  3992. return 0;
  3993. }
  3994. /**
  3995. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3996. *
  3997. * @state: crtc's scaler state
  3998. *
  3999. * Return
  4000. * 0 - scaler_usage updated successfully
  4001. * error - requested scaling cannot be supported or other error condition
  4002. */
  4003. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4004. {
  4005. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4006. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4007. &state->scaler_state.scaler_id,
  4008. state->pipe_src_w, state->pipe_src_h,
  4009. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4010. }
  4011. /**
  4012. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4013. *
  4014. * @state: crtc's scaler state
  4015. * @plane_state: atomic plane state to update
  4016. *
  4017. * Return
  4018. * 0 - scaler_usage updated successfully
  4019. * error - requested scaling cannot be supported or other error condition
  4020. */
  4021. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4022. struct intel_plane_state *plane_state)
  4023. {
  4024. struct intel_plane *intel_plane =
  4025. to_intel_plane(plane_state->base.plane);
  4026. struct drm_framebuffer *fb = plane_state->base.fb;
  4027. int ret;
  4028. bool force_detach = !fb || !plane_state->base.visible;
  4029. ret = skl_update_scaler(crtc_state, force_detach,
  4030. drm_plane_index(&intel_plane->base),
  4031. &plane_state->scaler_id,
  4032. drm_rect_width(&plane_state->base.src) >> 16,
  4033. drm_rect_height(&plane_state->base.src) >> 16,
  4034. drm_rect_width(&plane_state->base.dst),
  4035. drm_rect_height(&plane_state->base.dst));
  4036. if (ret || plane_state->scaler_id < 0)
  4037. return ret;
  4038. /* check colorkey */
  4039. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4040. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4041. intel_plane->base.base.id,
  4042. intel_plane->base.name);
  4043. return -EINVAL;
  4044. }
  4045. /* Check src format */
  4046. switch (fb->format->format) {
  4047. case DRM_FORMAT_RGB565:
  4048. case DRM_FORMAT_XBGR8888:
  4049. case DRM_FORMAT_XRGB8888:
  4050. case DRM_FORMAT_ABGR8888:
  4051. case DRM_FORMAT_ARGB8888:
  4052. case DRM_FORMAT_XRGB2101010:
  4053. case DRM_FORMAT_XBGR2101010:
  4054. case DRM_FORMAT_YUYV:
  4055. case DRM_FORMAT_YVYU:
  4056. case DRM_FORMAT_UYVY:
  4057. case DRM_FORMAT_VYUY:
  4058. break;
  4059. default:
  4060. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4061. intel_plane->base.base.id, intel_plane->base.name,
  4062. fb->base.id, fb->format->format);
  4063. return -EINVAL;
  4064. }
  4065. return 0;
  4066. }
  4067. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4068. {
  4069. int i;
  4070. for (i = 0; i < crtc->num_scalers; i++)
  4071. skl_detach_scaler(crtc, i);
  4072. }
  4073. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4074. {
  4075. struct drm_device *dev = crtc->base.dev;
  4076. struct drm_i915_private *dev_priv = to_i915(dev);
  4077. int pipe = crtc->pipe;
  4078. struct intel_crtc_scaler_state *scaler_state =
  4079. &crtc->config->scaler_state;
  4080. if (crtc->config->pch_pfit.enabled) {
  4081. int id;
  4082. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4083. return;
  4084. id = scaler_state->scaler_id;
  4085. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4086. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4087. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4088. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4089. }
  4090. }
  4091. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4092. {
  4093. struct drm_device *dev = crtc->base.dev;
  4094. struct drm_i915_private *dev_priv = to_i915(dev);
  4095. int pipe = crtc->pipe;
  4096. if (crtc->config->pch_pfit.enabled) {
  4097. /* Force use of hard-coded filter coefficients
  4098. * as some pre-programmed values are broken,
  4099. * e.g. x201.
  4100. */
  4101. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4102. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4103. PF_PIPE_SEL_IVB(pipe));
  4104. else
  4105. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4106. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4107. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4108. }
  4109. }
  4110. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4111. {
  4112. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4113. struct drm_device *dev = crtc->base.dev;
  4114. struct drm_i915_private *dev_priv = to_i915(dev);
  4115. if (!crtc_state->ips_enabled)
  4116. return;
  4117. /*
  4118. * We can only enable IPS after we enable a plane and wait for a vblank
  4119. * This function is called from post_plane_update, which is run after
  4120. * a vblank wait.
  4121. */
  4122. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4123. if (IS_BROADWELL(dev_priv)) {
  4124. mutex_lock(&dev_priv->pcu_lock);
  4125. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4126. IPS_ENABLE | IPS_PCODE_CONTROL));
  4127. mutex_unlock(&dev_priv->pcu_lock);
  4128. /* Quoting Art Runyan: "its not safe to expect any particular
  4129. * value in IPS_CTL bit 31 after enabling IPS through the
  4130. * mailbox." Moreover, the mailbox may return a bogus state,
  4131. * so we need to just enable it and continue on.
  4132. */
  4133. } else {
  4134. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4135. /* The bit only becomes 1 in the next vblank, so this wait here
  4136. * is essentially intel_wait_for_vblank. If we don't have this
  4137. * and don't wait for vblanks until the end of crtc_enable, then
  4138. * the HW state readout code will complain that the expected
  4139. * IPS_CTL value is not the one we read. */
  4140. if (intel_wait_for_register(dev_priv,
  4141. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4142. 50))
  4143. DRM_ERROR("Timed out waiting for IPS enable\n");
  4144. }
  4145. }
  4146. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4147. {
  4148. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4149. struct drm_device *dev = crtc->base.dev;
  4150. struct drm_i915_private *dev_priv = to_i915(dev);
  4151. if (!crtc_state->ips_enabled)
  4152. return;
  4153. if (IS_BROADWELL(dev_priv)) {
  4154. mutex_lock(&dev_priv->pcu_lock);
  4155. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4156. mutex_unlock(&dev_priv->pcu_lock);
  4157. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4158. if (intel_wait_for_register(dev_priv,
  4159. IPS_CTL, IPS_ENABLE, 0,
  4160. 42))
  4161. DRM_ERROR("Timed out waiting for IPS disable\n");
  4162. } else {
  4163. I915_WRITE(IPS_CTL, 0);
  4164. POSTING_READ(IPS_CTL);
  4165. }
  4166. /* We need to wait for a vblank before we can disable the plane. */
  4167. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4168. }
  4169. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4170. {
  4171. if (intel_crtc->overlay) {
  4172. struct drm_device *dev = intel_crtc->base.dev;
  4173. mutex_lock(&dev->struct_mutex);
  4174. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4175. mutex_unlock(&dev->struct_mutex);
  4176. }
  4177. /* Let userspace switch the overlay on again. In most cases userspace
  4178. * has to recompute where to put it anyway.
  4179. */
  4180. }
  4181. /**
  4182. * intel_post_enable_primary - Perform operations after enabling primary plane
  4183. * @crtc: the CRTC whose primary plane was just enabled
  4184. *
  4185. * Performs potentially sleeping operations that must be done after the primary
  4186. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4187. * called due to an explicit primary plane update, or due to an implicit
  4188. * re-enable that is caused when a sprite plane is updated to no longer
  4189. * completely hide the primary plane.
  4190. */
  4191. static void
  4192. intel_post_enable_primary(struct drm_crtc *crtc,
  4193. const struct intel_crtc_state *new_crtc_state)
  4194. {
  4195. struct drm_device *dev = crtc->dev;
  4196. struct drm_i915_private *dev_priv = to_i915(dev);
  4197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4198. int pipe = intel_crtc->pipe;
  4199. /*
  4200. * Gen2 reports pipe underruns whenever all planes are disabled.
  4201. * So don't enable underrun reporting before at least some planes
  4202. * are enabled.
  4203. * FIXME: Need to fix the logic to work when we turn off all planes
  4204. * but leave the pipe running.
  4205. */
  4206. if (IS_GEN2(dev_priv))
  4207. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4208. /* Underruns don't always raise interrupts, so check manually. */
  4209. intel_check_cpu_fifo_underruns(dev_priv);
  4210. intel_check_pch_fifo_underruns(dev_priv);
  4211. }
  4212. /* FIXME get rid of this and use pre_plane_update */
  4213. static void
  4214. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4215. {
  4216. struct drm_device *dev = crtc->dev;
  4217. struct drm_i915_private *dev_priv = to_i915(dev);
  4218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4219. int pipe = intel_crtc->pipe;
  4220. /*
  4221. * Gen2 reports pipe underruns whenever all planes are disabled.
  4222. * So disable underrun reporting before all the planes get disabled.
  4223. */
  4224. if (IS_GEN2(dev_priv))
  4225. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4226. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4227. /*
  4228. * Vblank time updates from the shadow to live plane control register
  4229. * are blocked if the memory self-refresh mode is active at that
  4230. * moment. So to make sure the plane gets truly disabled, disable
  4231. * first the self-refresh mode. The self-refresh enable bit in turn
  4232. * will be checked/applied by the HW only at the next frame start
  4233. * event which is after the vblank start event, so we need to have a
  4234. * wait-for-vblank between disabling the plane and the pipe.
  4235. */
  4236. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4237. intel_set_memory_cxsr(dev_priv, false))
  4238. intel_wait_for_vblank(dev_priv, pipe);
  4239. }
  4240. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4241. const struct intel_crtc_state *new_crtc_state)
  4242. {
  4243. if (!old_crtc_state->ips_enabled)
  4244. return false;
  4245. if (needs_modeset(&new_crtc_state->base))
  4246. return true;
  4247. return !new_crtc_state->ips_enabled;
  4248. }
  4249. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4250. const struct intel_crtc_state *new_crtc_state)
  4251. {
  4252. if (!new_crtc_state->ips_enabled)
  4253. return false;
  4254. if (needs_modeset(&new_crtc_state->base))
  4255. return true;
  4256. /*
  4257. * We can't read out IPS on broadwell, assume the worst and
  4258. * forcibly enable IPS on the first fastset.
  4259. */
  4260. if (new_crtc_state->update_pipe &&
  4261. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4262. return true;
  4263. return !old_crtc_state->ips_enabled;
  4264. }
  4265. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4266. {
  4267. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4268. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4269. struct intel_crtc_state *pipe_config =
  4270. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4271. crtc);
  4272. struct drm_plane *primary = crtc->base.primary;
  4273. struct drm_plane_state *old_pri_state =
  4274. drm_atomic_get_existing_plane_state(old_state, primary);
  4275. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4276. if (pipe_config->update_wm_post && pipe_config->base.active)
  4277. intel_update_watermarks(crtc);
  4278. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4279. hsw_enable_ips(pipe_config);
  4280. if (old_pri_state) {
  4281. struct intel_plane_state *primary_state =
  4282. intel_atomic_get_new_plane_state(to_intel_atomic_state(old_state),
  4283. to_intel_plane(primary));
  4284. struct intel_plane_state *old_primary_state =
  4285. to_intel_plane_state(old_pri_state);
  4286. intel_fbc_post_update(crtc);
  4287. if (primary_state->base.visible &&
  4288. (needs_modeset(&pipe_config->base) ||
  4289. !old_primary_state->base.visible))
  4290. intel_post_enable_primary(&crtc->base, pipe_config);
  4291. }
  4292. }
  4293. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4294. struct intel_crtc_state *pipe_config)
  4295. {
  4296. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4297. struct drm_device *dev = crtc->base.dev;
  4298. struct drm_i915_private *dev_priv = to_i915(dev);
  4299. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4300. struct drm_plane *primary = crtc->base.primary;
  4301. struct drm_plane_state *old_pri_state =
  4302. drm_atomic_get_existing_plane_state(old_state, primary);
  4303. bool modeset = needs_modeset(&pipe_config->base);
  4304. struct intel_atomic_state *old_intel_state =
  4305. to_intel_atomic_state(old_state);
  4306. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4307. hsw_disable_ips(old_crtc_state);
  4308. if (old_pri_state) {
  4309. struct intel_plane_state *primary_state =
  4310. intel_atomic_get_new_plane_state(old_intel_state,
  4311. to_intel_plane(primary));
  4312. struct intel_plane_state *old_primary_state =
  4313. to_intel_plane_state(old_pri_state);
  4314. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4315. /*
  4316. * Gen2 reports pipe underruns whenever all planes are disabled.
  4317. * So disable underrun reporting before all the planes get disabled.
  4318. */
  4319. if (IS_GEN2(dev_priv) && old_primary_state->base.visible &&
  4320. (modeset || !primary_state->base.visible))
  4321. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4322. }
  4323. /*
  4324. * Vblank time updates from the shadow to live plane control register
  4325. * are blocked if the memory self-refresh mode is active at that
  4326. * moment. So to make sure the plane gets truly disabled, disable
  4327. * first the self-refresh mode. The self-refresh enable bit in turn
  4328. * will be checked/applied by the HW only at the next frame start
  4329. * event which is after the vblank start event, so we need to have a
  4330. * wait-for-vblank between disabling the plane and the pipe.
  4331. */
  4332. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4333. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4334. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4335. /*
  4336. * IVB workaround: must disable low power watermarks for at least
  4337. * one frame before enabling scaling. LP watermarks can be re-enabled
  4338. * when scaling is disabled.
  4339. *
  4340. * WaCxSRDisabledForSpriteScaling:ivb
  4341. */
  4342. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4343. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4344. /*
  4345. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4346. * watermark programming here.
  4347. */
  4348. if (needs_modeset(&pipe_config->base))
  4349. return;
  4350. /*
  4351. * For platforms that support atomic watermarks, program the
  4352. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4353. * will be the intermediate values that are safe for both pre- and
  4354. * post- vblank; when vblank happens, the 'active' values will be set
  4355. * to the final 'target' values and we'll do this again to get the
  4356. * optimal watermarks. For gen9+ platforms, the values we program here
  4357. * will be the final target values which will get automatically latched
  4358. * at vblank time; no further programming will be necessary.
  4359. *
  4360. * If a platform hasn't been transitioned to atomic watermarks yet,
  4361. * we'll continue to update watermarks the old way, if flags tell
  4362. * us to.
  4363. */
  4364. if (dev_priv->display.initial_watermarks != NULL)
  4365. dev_priv->display.initial_watermarks(old_intel_state,
  4366. pipe_config);
  4367. else if (pipe_config->update_wm_pre)
  4368. intel_update_watermarks(crtc);
  4369. }
  4370. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4371. {
  4372. struct drm_device *dev = crtc->dev;
  4373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4374. struct drm_plane *p;
  4375. int pipe = intel_crtc->pipe;
  4376. intel_crtc_dpms_overlay_disable(intel_crtc);
  4377. drm_for_each_plane_mask(p, dev, plane_mask)
  4378. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4379. /*
  4380. * FIXME: Once we grow proper nuclear flip support out of this we need
  4381. * to compute the mask of flip planes precisely. For the time being
  4382. * consider this a flip to a NULL plane.
  4383. */
  4384. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4385. }
  4386. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4387. struct intel_crtc_state *crtc_state,
  4388. struct drm_atomic_state *old_state)
  4389. {
  4390. struct drm_connector_state *conn_state;
  4391. struct drm_connector *conn;
  4392. int i;
  4393. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4394. struct intel_encoder *encoder =
  4395. to_intel_encoder(conn_state->best_encoder);
  4396. if (conn_state->crtc != crtc)
  4397. continue;
  4398. if (encoder->pre_pll_enable)
  4399. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4400. }
  4401. }
  4402. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4403. struct intel_crtc_state *crtc_state,
  4404. struct drm_atomic_state *old_state)
  4405. {
  4406. struct drm_connector_state *conn_state;
  4407. struct drm_connector *conn;
  4408. int i;
  4409. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4410. struct intel_encoder *encoder =
  4411. to_intel_encoder(conn_state->best_encoder);
  4412. if (conn_state->crtc != crtc)
  4413. continue;
  4414. if (encoder->pre_enable)
  4415. encoder->pre_enable(encoder, crtc_state, conn_state);
  4416. }
  4417. }
  4418. static void intel_encoders_enable(struct drm_crtc *crtc,
  4419. struct intel_crtc_state *crtc_state,
  4420. struct drm_atomic_state *old_state)
  4421. {
  4422. struct drm_connector_state *conn_state;
  4423. struct drm_connector *conn;
  4424. int i;
  4425. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4426. struct intel_encoder *encoder =
  4427. to_intel_encoder(conn_state->best_encoder);
  4428. if (conn_state->crtc != crtc)
  4429. continue;
  4430. encoder->enable(encoder, crtc_state, conn_state);
  4431. intel_opregion_notify_encoder(encoder, true);
  4432. }
  4433. }
  4434. static void intel_encoders_disable(struct drm_crtc *crtc,
  4435. struct intel_crtc_state *old_crtc_state,
  4436. struct drm_atomic_state *old_state)
  4437. {
  4438. struct drm_connector_state *old_conn_state;
  4439. struct drm_connector *conn;
  4440. int i;
  4441. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4442. struct intel_encoder *encoder =
  4443. to_intel_encoder(old_conn_state->best_encoder);
  4444. if (old_conn_state->crtc != crtc)
  4445. continue;
  4446. intel_opregion_notify_encoder(encoder, false);
  4447. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4448. }
  4449. }
  4450. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4451. struct intel_crtc_state *old_crtc_state,
  4452. struct drm_atomic_state *old_state)
  4453. {
  4454. struct drm_connector_state *old_conn_state;
  4455. struct drm_connector *conn;
  4456. int i;
  4457. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4458. struct intel_encoder *encoder =
  4459. to_intel_encoder(old_conn_state->best_encoder);
  4460. if (old_conn_state->crtc != crtc)
  4461. continue;
  4462. if (encoder->post_disable)
  4463. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4464. }
  4465. }
  4466. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4467. struct intel_crtc_state *old_crtc_state,
  4468. struct drm_atomic_state *old_state)
  4469. {
  4470. struct drm_connector_state *old_conn_state;
  4471. struct drm_connector *conn;
  4472. int i;
  4473. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4474. struct intel_encoder *encoder =
  4475. to_intel_encoder(old_conn_state->best_encoder);
  4476. if (old_conn_state->crtc != crtc)
  4477. continue;
  4478. if (encoder->post_pll_disable)
  4479. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4480. }
  4481. }
  4482. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4483. struct drm_atomic_state *old_state)
  4484. {
  4485. struct drm_crtc *crtc = pipe_config->base.crtc;
  4486. struct drm_device *dev = crtc->dev;
  4487. struct drm_i915_private *dev_priv = to_i915(dev);
  4488. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4489. int pipe = intel_crtc->pipe;
  4490. struct intel_atomic_state *old_intel_state =
  4491. to_intel_atomic_state(old_state);
  4492. if (WARN_ON(intel_crtc->active))
  4493. return;
  4494. /*
  4495. * Sometimes spurious CPU pipe underruns happen during FDI
  4496. * training, at least with VGA+HDMI cloning. Suppress them.
  4497. *
  4498. * On ILK we get an occasional spurious CPU pipe underruns
  4499. * between eDP port A enable and vdd enable. Also PCH port
  4500. * enable seems to result in the occasional CPU pipe underrun.
  4501. *
  4502. * Spurious PCH underruns also occur during PCH enabling.
  4503. */
  4504. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4505. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4506. if (intel_crtc->config->has_pch_encoder)
  4507. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4508. if (intel_crtc->config->has_pch_encoder)
  4509. intel_prepare_shared_dpll(intel_crtc);
  4510. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4511. intel_dp_set_m_n(intel_crtc, M1_N1);
  4512. intel_set_pipe_timings(intel_crtc);
  4513. intel_set_pipe_src_size(intel_crtc);
  4514. if (intel_crtc->config->has_pch_encoder) {
  4515. intel_cpu_transcoder_set_m_n(intel_crtc,
  4516. &intel_crtc->config->fdi_m_n, NULL);
  4517. }
  4518. ironlake_set_pipeconf(crtc);
  4519. intel_crtc->active = true;
  4520. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4521. if (intel_crtc->config->has_pch_encoder) {
  4522. /* Note: FDI PLL enabling _must_ be done before we enable the
  4523. * cpu pipes, hence this is separate from all the other fdi/pch
  4524. * enabling. */
  4525. ironlake_fdi_pll_enable(intel_crtc);
  4526. } else {
  4527. assert_fdi_tx_disabled(dev_priv, pipe);
  4528. assert_fdi_rx_disabled(dev_priv, pipe);
  4529. }
  4530. ironlake_pfit_enable(intel_crtc);
  4531. /*
  4532. * On ILK+ LUT must be loaded before the pipe is running but with
  4533. * clocks enabled
  4534. */
  4535. intel_color_load_luts(&pipe_config->base);
  4536. if (dev_priv->display.initial_watermarks != NULL)
  4537. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4538. intel_enable_pipe(pipe_config);
  4539. if (intel_crtc->config->has_pch_encoder)
  4540. ironlake_pch_enable(pipe_config);
  4541. assert_vblank_disabled(crtc);
  4542. drm_crtc_vblank_on(crtc);
  4543. intel_encoders_enable(crtc, pipe_config, old_state);
  4544. if (HAS_PCH_CPT(dev_priv))
  4545. cpt_verify_modeset(dev, intel_crtc->pipe);
  4546. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4547. if (intel_crtc->config->has_pch_encoder)
  4548. intel_wait_for_vblank(dev_priv, pipe);
  4549. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4550. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4551. }
  4552. /* IPS only exists on ULT machines and is tied to pipe A. */
  4553. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4554. {
  4555. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4556. }
  4557. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4558. enum pipe pipe, bool apply)
  4559. {
  4560. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4561. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4562. if (apply)
  4563. val |= mask;
  4564. else
  4565. val &= ~mask;
  4566. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4567. }
  4568. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4569. struct drm_atomic_state *old_state)
  4570. {
  4571. struct drm_crtc *crtc = pipe_config->base.crtc;
  4572. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4574. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4575. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4576. struct intel_atomic_state *old_intel_state =
  4577. to_intel_atomic_state(old_state);
  4578. bool psl_clkgate_wa;
  4579. if (WARN_ON(intel_crtc->active))
  4580. return;
  4581. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4582. if (intel_crtc->config->shared_dpll)
  4583. intel_enable_shared_dpll(intel_crtc);
  4584. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4585. intel_dp_set_m_n(intel_crtc, M1_N1);
  4586. if (!transcoder_is_dsi(cpu_transcoder))
  4587. intel_set_pipe_timings(intel_crtc);
  4588. intel_set_pipe_src_size(intel_crtc);
  4589. if (cpu_transcoder != TRANSCODER_EDP &&
  4590. !transcoder_is_dsi(cpu_transcoder)) {
  4591. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4592. intel_crtc->config->pixel_multiplier - 1);
  4593. }
  4594. if (intel_crtc->config->has_pch_encoder) {
  4595. intel_cpu_transcoder_set_m_n(intel_crtc,
  4596. &intel_crtc->config->fdi_m_n, NULL);
  4597. }
  4598. if (!transcoder_is_dsi(cpu_transcoder))
  4599. haswell_set_pipeconf(crtc);
  4600. haswell_set_pipemisc(crtc);
  4601. intel_color_set_csc(&pipe_config->base);
  4602. intel_crtc->active = true;
  4603. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4604. if (!transcoder_is_dsi(cpu_transcoder))
  4605. intel_ddi_enable_pipe_clock(pipe_config);
  4606. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4607. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4608. intel_crtc->config->pch_pfit.enabled;
  4609. if (psl_clkgate_wa)
  4610. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4611. if (INTEL_GEN(dev_priv) >= 9)
  4612. skylake_pfit_enable(intel_crtc);
  4613. else
  4614. ironlake_pfit_enable(intel_crtc);
  4615. /*
  4616. * On ILK+ LUT must be loaded before the pipe is running but with
  4617. * clocks enabled
  4618. */
  4619. intel_color_load_luts(&pipe_config->base);
  4620. intel_ddi_set_pipe_settings(pipe_config);
  4621. if (!transcoder_is_dsi(cpu_transcoder))
  4622. intel_ddi_enable_transcoder_func(pipe_config);
  4623. if (dev_priv->display.initial_watermarks != NULL)
  4624. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4625. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4626. if (!transcoder_is_dsi(cpu_transcoder))
  4627. intel_enable_pipe(pipe_config);
  4628. if (intel_crtc->config->has_pch_encoder)
  4629. lpt_pch_enable(pipe_config);
  4630. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4631. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4632. assert_vblank_disabled(crtc);
  4633. drm_crtc_vblank_on(crtc);
  4634. intel_encoders_enable(crtc, pipe_config, old_state);
  4635. if (psl_clkgate_wa) {
  4636. intel_wait_for_vblank(dev_priv, pipe);
  4637. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4638. }
  4639. /* If we change the relative order between pipe/planes enabling, we need
  4640. * to change the workaround. */
  4641. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4642. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4643. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4644. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4645. }
  4646. }
  4647. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4648. {
  4649. struct drm_device *dev = crtc->base.dev;
  4650. struct drm_i915_private *dev_priv = to_i915(dev);
  4651. int pipe = crtc->pipe;
  4652. /* To avoid upsetting the power well on haswell only disable the pfit if
  4653. * it's in use. The hw state code will make sure we get this right. */
  4654. if (force || crtc->config->pch_pfit.enabled) {
  4655. I915_WRITE(PF_CTL(pipe), 0);
  4656. I915_WRITE(PF_WIN_POS(pipe), 0);
  4657. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4658. }
  4659. }
  4660. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4661. struct drm_atomic_state *old_state)
  4662. {
  4663. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4664. struct drm_device *dev = crtc->dev;
  4665. struct drm_i915_private *dev_priv = to_i915(dev);
  4666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4667. int pipe = intel_crtc->pipe;
  4668. /*
  4669. * Sometimes spurious CPU pipe underruns happen when the
  4670. * pipe is already disabled, but FDI RX/TX is still enabled.
  4671. * Happens at least with VGA+HDMI cloning. Suppress them.
  4672. */
  4673. if (intel_crtc->config->has_pch_encoder) {
  4674. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4675. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4676. }
  4677. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4678. drm_crtc_vblank_off(crtc);
  4679. assert_vblank_disabled(crtc);
  4680. intel_disable_pipe(old_crtc_state);
  4681. ironlake_pfit_disable(intel_crtc, false);
  4682. if (intel_crtc->config->has_pch_encoder)
  4683. ironlake_fdi_disable(crtc);
  4684. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4685. if (intel_crtc->config->has_pch_encoder) {
  4686. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4687. if (HAS_PCH_CPT(dev_priv)) {
  4688. i915_reg_t reg;
  4689. u32 temp;
  4690. /* disable TRANS_DP_CTL */
  4691. reg = TRANS_DP_CTL(pipe);
  4692. temp = I915_READ(reg);
  4693. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4694. TRANS_DP_PORT_SEL_MASK);
  4695. temp |= TRANS_DP_PORT_SEL_NONE;
  4696. I915_WRITE(reg, temp);
  4697. /* disable DPLL_SEL */
  4698. temp = I915_READ(PCH_DPLL_SEL);
  4699. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4700. I915_WRITE(PCH_DPLL_SEL, temp);
  4701. }
  4702. ironlake_fdi_pll_disable(intel_crtc);
  4703. }
  4704. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4705. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4706. }
  4707. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4708. struct drm_atomic_state *old_state)
  4709. {
  4710. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4711. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4713. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4714. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4715. drm_crtc_vblank_off(crtc);
  4716. assert_vblank_disabled(crtc);
  4717. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4718. if (!transcoder_is_dsi(cpu_transcoder))
  4719. intel_disable_pipe(old_crtc_state);
  4720. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4721. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4722. if (!transcoder_is_dsi(cpu_transcoder))
  4723. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4724. if (INTEL_GEN(dev_priv) >= 9)
  4725. skylake_scaler_disable(intel_crtc);
  4726. else
  4727. ironlake_pfit_disable(intel_crtc, false);
  4728. if (!transcoder_is_dsi(cpu_transcoder))
  4729. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4730. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4731. }
  4732. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4733. {
  4734. struct drm_device *dev = crtc->base.dev;
  4735. struct drm_i915_private *dev_priv = to_i915(dev);
  4736. struct intel_crtc_state *pipe_config = crtc->config;
  4737. if (!pipe_config->gmch_pfit.control)
  4738. return;
  4739. /*
  4740. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4741. * according to register description and PRM.
  4742. */
  4743. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4744. assert_pipe_disabled(dev_priv, crtc->pipe);
  4745. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4746. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4747. /* Border color in case we don't scale up to the full screen. Black by
  4748. * default, change to something else for debugging. */
  4749. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4750. }
  4751. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4752. {
  4753. switch (port) {
  4754. case PORT_A:
  4755. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4756. case PORT_B:
  4757. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4758. case PORT_C:
  4759. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4760. case PORT_D:
  4761. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4762. case PORT_E:
  4763. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4764. default:
  4765. MISSING_CASE(port);
  4766. return POWER_DOMAIN_PORT_OTHER;
  4767. }
  4768. }
  4769. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4770. struct intel_crtc_state *crtc_state)
  4771. {
  4772. struct drm_device *dev = crtc->dev;
  4773. struct drm_i915_private *dev_priv = to_i915(dev);
  4774. struct drm_encoder *encoder;
  4775. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4776. enum pipe pipe = intel_crtc->pipe;
  4777. u64 mask;
  4778. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4779. if (!crtc_state->base.active)
  4780. return 0;
  4781. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4782. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4783. if (crtc_state->pch_pfit.enabled ||
  4784. crtc_state->pch_pfit.force_thru)
  4785. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4786. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4787. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4788. mask |= BIT_ULL(intel_encoder->power_domain);
  4789. }
  4790. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4791. mask |= BIT(POWER_DOMAIN_AUDIO);
  4792. if (crtc_state->shared_dpll)
  4793. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4794. return mask;
  4795. }
  4796. static u64
  4797. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4798. struct intel_crtc_state *crtc_state)
  4799. {
  4800. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4801. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4802. enum intel_display_power_domain domain;
  4803. u64 domains, new_domains, old_domains;
  4804. old_domains = intel_crtc->enabled_power_domains;
  4805. intel_crtc->enabled_power_domains = new_domains =
  4806. get_crtc_power_domains(crtc, crtc_state);
  4807. domains = new_domains & ~old_domains;
  4808. for_each_power_domain(domain, domains)
  4809. intel_display_power_get(dev_priv, domain);
  4810. return old_domains & ~new_domains;
  4811. }
  4812. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4813. u64 domains)
  4814. {
  4815. enum intel_display_power_domain domain;
  4816. for_each_power_domain(domain, domains)
  4817. intel_display_power_put(dev_priv, domain);
  4818. }
  4819. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4820. struct drm_atomic_state *old_state)
  4821. {
  4822. struct intel_atomic_state *old_intel_state =
  4823. to_intel_atomic_state(old_state);
  4824. struct drm_crtc *crtc = pipe_config->base.crtc;
  4825. struct drm_device *dev = crtc->dev;
  4826. struct drm_i915_private *dev_priv = to_i915(dev);
  4827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4828. int pipe = intel_crtc->pipe;
  4829. if (WARN_ON(intel_crtc->active))
  4830. return;
  4831. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4832. intel_dp_set_m_n(intel_crtc, M1_N1);
  4833. intel_set_pipe_timings(intel_crtc);
  4834. intel_set_pipe_src_size(intel_crtc);
  4835. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4836. struct drm_i915_private *dev_priv = to_i915(dev);
  4837. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4838. I915_WRITE(CHV_CANVAS(pipe), 0);
  4839. }
  4840. i9xx_set_pipeconf(intel_crtc);
  4841. intel_crtc->active = true;
  4842. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4843. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4844. if (IS_CHERRYVIEW(dev_priv)) {
  4845. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4846. chv_enable_pll(intel_crtc, intel_crtc->config);
  4847. } else {
  4848. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4849. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4850. }
  4851. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4852. i9xx_pfit_enable(intel_crtc);
  4853. intel_color_load_luts(&pipe_config->base);
  4854. dev_priv->display.initial_watermarks(old_intel_state,
  4855. pipe_config);
  4856. intel_enable_pipe(pipe_config);
  4857. assert_vblank_disabled(crtc);
  4858. drm_crtc_vblank_on(crtc);
  4859. intel_encoders_enable(crtc, pipe_config, old_state);
  4860. }
  4861. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4862. {
  4863. struct drm_device *dev = crtc->base.dev;
  4864. struct drm_i915_private *dev_priv = to_i915(dev);
  4865. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4866. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4867. }
  4868. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4869. struct drm_atomic_state *old_state)
  4870. {
  4871. struct intel_atomic_state *old_intel_state =
  4872. to_intel_atomic_state(old_state);
  4873. struct drm_crtc *crtc = pipe_config->base.crtc;
  4874. struct drm_device *dev = crtc->dev;
  4875. struct drm_i915_private *dev_priv = to_i915(dev);
  4876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4877. enum pipe pipe = intel_crtc->pipe;
  4878. if (WARN_ON(intel_crtc->active))
  4879. return;
  4880. i9xx_set_pll_dividers(intel_crtc);
  4881. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4882. intel_dp_set_m_n(intel_crtc, M1_N1);
  4883. intel_set_pipe_timings(intel_crtc);
  4884. intel_set_pipe_src_size(intel_crtc);
  4885. i9xx_set_pipeconf(intel_crtc);
  4886. intel_crtc->active = true;
  4887. if (!IS_GEN2(dev_priv))
  4888. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4889. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4890. i9xx_enable_pll(intel_crtc, pipe_config);
  4891. i9xx_pfit_enable(intel_crtc);
  4892. intel_color_load_luts(&pipe_config->base);
  4893. if (dev_priv->display.initial_watermarks != NULL)
  4894. dev_priv->display.initial_watermarks(old_intel_state,
  4895. intel_crtc->config);
  4896. else
  4897. intel_update_watermarks(intel_crtc);
  4898. intel_enable_pipe(pipe_config);
  4899. assert_vblank_disabled(crtc);
  4900. drm_crtc_vblank_on(crtc);
  4901. intel_encoders_enable(crtc, pipe_config, old_state);
  4902. }
  4903. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4904. {
  4905. struct drm_device *dev = crtc->base.dev;
  4906. struct drm_i915_private *dev_priv = to_i915(dev);
  4907. if (!crtc->config->gmch_pfit.control)
  4908. return;
  4909. assert_pipe_disabled(dev_priv, crtc->pipe);
  4910. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4911. I915_READ(PFIT_CONTROL));
  4912. I915_WRITE(PFIT_CONTROL, 0);
  4913. }
  4914. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4915. struct drm_atomic_state *old_state)
  4916. {
  4917. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4918. struct drm_device *dev = crtc->dev;
  4919. struct drm_i915_private *dev_priv = to_i915(dev);
  4920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4921. int pipe = intel_crtc->pipe;
  4922. /*
  4923. * On gen2 planes are double buffered but the pipe isn't, so we must
  4924. * wait for planes to fully turn off before disabling the pipe.
  4925. */
  4926. if (IS_GEN2(dev_priv))
  4927. intel_wait_for_vblank(dev_priv, pipe);
  4928. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4929. drm_crtc_vblank_off(crtc);
  4930. assert_vblank_disabled(crtc);
  4931. intel_disable_pipe(old_crtc_state);
  4932. i9xx_pfit_disable(intel_crtc);
  4933. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4934. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4935. if (IS_CHERRYVIEW(dev_priv))
  4936. chv_disable_pll(dev_priv, pipe);
  4937. else if (IS_VALLEYVIEW(dev_priv))
  4938. vlv_disable_pll(dev_priv, pipe);
  4939. else
  4940. i9xx_disable_pll(intel_crtc);
  4941. }
  4942. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4943. if (!IS_GEN2(dev_priv))
  4944. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4945. if (!dev_priv->display.initial_watermarks)
  4946. intel_update_watermarks(intel_crtc);
  4947. /* clock the pipe down to 640x480@60 to potentially save power */
  4948. if (IS_I830(dev_priv))
  4949. i830_enable_pipe(dev_priv, pipe);
  4950. }
  4951. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  4952. struct drm_modeset_acquire_ctx *ctx)
  4953. {
  4954. struct intel_encoder *encoder;
  4955. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4956. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4957. enum intel_display_power_domain domain;
  4958. struct intel_plane *plane;
  4959. u64 domains;
  4960. struct drm_atomic_state *state;
  4961. struct intel_crtc_state *crtc_state;
  4962. int ret;
  4963. if (!intel_crtc->active)
  4964. return;
  4965. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  4966. const struct intel_plane_state *plane_state =
  4967. to_intel_plane_state(plane->base.state);
  4968. if (plane_state->base.visible)
  4969. intel_plane_disable_noatomic(intel_crtc, plane);
  4970. }
  4971. state = drm_atomic_state_alloc(crtc->dev);
  4972. if (!state) {
  4973. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4974. crtc->base.id, crtc->name);
  4975. return;
  4976. }
  4977. state->acquire_ctx = ctx;
  4978. /* Everything's already locked, -EDEADLK can't happen. */
  4979. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4980. ret = drm_atomic_add_affected_connectors(state, crtc);
  4981. WARN_ON(IS_ERR(crtc_state) || ret);
  4982. dev_priv->display.crtc_disable(crtc_state, state);
  4983. drm_atomic_state_put(state);
  4984. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4985. crtc->base.id, crtc->name);
  4986. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4987. crtc->state->active = false;
  4988. intel_crtc->active = false;
  4989. crtc->enabled = false;
  4990. crtc->state->connector_mask = 0;
  4991. crtc->state->encoder_mask = 0;
  4992. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4993. encoder->base.crtc = NULL;
  4994. intel_fbc_disable(intel_crtc);
  4995. intel_update_watermarks(intel_crtc);
  4996. intel_disable_shared_dpll(intel_crtc);
  4997. domains = intel_crtc->enabled_power_domains;
  4998. for_each_power_domain(domain, domains)
  4999. intel_display_power_put(dev_priv, domain);
  5000. intel_crtc->enabled_power_domains = 0;
  5001. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5002. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5003. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5004. }
  5005. /*
  5006. * turn all crtc's off, but do not adjust state
  5007. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5008. */
  5009. int intel_display_suspend(struct drm_device *dev)
  5010. {
  5011. struct drm_i915_private *dev_priv = to_i915(dev);
  5012. struct drm_atomic_state *state;
  5013. int ret;
  5014. state = drm_atomic_helper_suspend(dev);
  5015. ret = PTR_ERR_OR_ZERO(state);
  5016. if (ret)
  5017. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5018. else
  5019. dev_priv->modeset_restore_state = state;
  5020. return ret;
  5021. }
  5022. void intel_encoder_destroy(struct drm_encoder *encoder)
  5023. {
  5024. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5025. drm_encoder_cleanup(encoder);
  5026. kfree(intel_encoder);
  5027. }
  5028. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5029. * internal consistency). */
  5030. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5031. struct drm_connector_state *conn_state)
  5032. {
  5033. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5034. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5035. connector->base.base.id,
  5036. connector->base.name);
  5037. if (connector->get_hw_state(connector)) {
  5038. struct intel_encoder *encoder = connector->encoder;
  5039. I915_STATE_WARN(!crtc_state,
  5040. "connector enabled without attached crtc\n");
  5041. if (!crtc_state)
  5042. return;
  5043. I915_STATE_WARN(!crtc_state->active,
  5044. "connector is active, but attached crtc isn't\n");
  5045. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5046. return;
  5047. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5048. "atomic encoder doesn't match attached encoder\n");
  5049. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5050. "attached encoder crtc differs from connector crtc\n");
  5051. } else {
  5052. I915_STATE_WARN(crtc_state && crtc_state->active,
  5053. "attached crtc is active, but connector isn't\n");
  5054. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5055. "best encoder set without crtc!\n");
  5056. }
  5057. }
  5058. int intel_connector_init(struct intel_connector *connector)
  5059. {
  5060. struct intel_digital_connector_state *conn_state;
  5061. /*
  5062. * Allocate enough memory to hold intel_digital_connector_state,
  5063. * This might be a few bytes too many, but for connectors that don't
  5064. * need it we'll free the state and allocate a smaller one on the first
  5065. * succesful commit anyway.
  5066. */
  5067. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5068. if (!conn_state)
  5069. return -ENOMEM;
  5070. __drm_atomic_helper_connector_reset(&connector->base,
  5071. &conn_state->base);
  5072. return 0;
  5073. }
  5074. struct intel_connector *intel_connector_alloc(void)
  5075. {
  5076. struct intel_connector *connector;
  5077. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5078. if (!connector)
  5079. return NULL;
  5080. if (intel_connector_init(connector) < 0) {
  5081. kfree(connector);
  5082. return NULL;
  5083. }
  5084. return connector;
  5085. }
  5086. /*
  5087. * Free the bits allocated by intel_connector_alloc.
  5088. * This should only be used after intel_connector_alloc has returned
  5089. * successfully, and before drm_connector_init returns successfully.
  5090. * Otherwise the destroy callbacks for the connector and the state should
  5091. * take care of proper cleanup/free
  5092. */
  5093. void intel_connector_free(struct intel_connector *connector)
  5094. {
  5095. kfree(to_intel_digital_connector_state(connector->base.state));
  5096. kfree(connector);
  5097. }
  5098. /* Simple connector->get_hw_state implementation for encoders that support only
  5099. * one connector and no cloning and hence the encoder state determines the state
  5100. * of the connector. */
  5101. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5102. {
  5103. enum pipe pipe = 0;
  5104. struct intel_encoder *encoder = connector->encoder;
  5105. return encoder->get_hw_state(encoder, &pipe);
  5106. }
  5107. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5108. {
  5109. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5110. return crtc_state->fdi_lanes;
  5111. return 0;
  5112. }
  5113. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5114. struct intel_crtc_state *pipe_config)
  5115. {
  5116. struct drm_i915_private *dev_priv = to_i915(dev);
  5117. struct drm_atomic_state *state = pipe_config->base.state;
  5118. struct intel_crtc *other_crtc;
  5119. struct intel_crtc_state *other_crtc_state;
  5120. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5121. pipe_name(pipe), pipe_config->fdi_lanes);
  5122. if (pipe_config->fdi_lanes > 4) {
  5123. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5124. pipe_name(pipe), pipe_config->fdi_lanes);
  5125. return -EINVAL;
  5126. }
  5127. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5128. if (pipe_config->fdi_lanes > 2) {
  5129. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5130. pipe_config->fdi_lanes);
  5131. return -EINVAL;
  5132. } else {
  5133. return 0;
  5134. }
  5135. }
  5136. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5137. return 0;
  5138. /* Ivybridge 3 pipe is really complicated */
  5139. switch (pipe) {
  5140. case PIPE_A:
  5141. return 0;
  5142. case PIPE_B:
  5143. if (pipe_config->fdi_lanes <= 2)
  5144. return 0;
  5145. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5146. other_crtc_state =
  5147. intel_atomic_get_crtc_state(state, other_crtc);
  5148. if (IS_ERR(other_crtc_state))
  5149. return PTR_ERR(other_crtc_state);
  5150. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5151. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5152. pipe_name(pipe), pipe_config->fdi_lanes);
  5153. return -EINVAL;
  5154. }
  5155. return 0;
  5156. case PIPE_C:
  5157. if (pipe_config->fdi_lanes > 2) {
  5158. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5159. pipe_name(pipe), pipe_config->fdi_lanes);
  5160. return -EINVAL;
  5161. }
  5162. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5163. other_crtc_state =
  5164. intel_atomic_get_crtc_state(state, other_crtc);
  5165. if (IS_ERR(other_crtc_state))
  5166. return PTR_ERR(other_crtc_state);
  5167. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5168. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5169. return -EINVAL;
  5170. }
  5171. return 0;
  5172. default:
  5173. BUG();
  5174. }
  5175. }
  5176. #define RETRY 1
  5177. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5178. struct intel_crtc_state *pipe_config)
  5179. {
  5180. struct drm_device *dev = intel_crtc->base.dev;
  5181. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5182. int lane, link_bw, fdi_dotclock, ret;
  5183. bool needs_recompute = false;
  5184. retry:
  5185. /* FDI is a binary signal running at ~2.7GHz, encoding
  5186. * each output octet as 10 bits. The actual frequency
  5187. * is stored as a divider into a 100MHz clock, and the
  5188. * mode pixel clock is stored in units of 1KHz.
  5189. * Hence the bw of each lane in terms of the mode signal
  5190. * is:
  5191. */
  5192. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5193. fdi_dotclock = adjusted_mode->crtc_clock;
  5194. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5195. pipe_config->pipe_bpp);
  5196. pipe_config->fdi_lanes = lane;
  5197. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5198. link_bw, &pipe_config->fdi_m_n, false);
  5199. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5200. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5201. pipe_config->pipe_bpp -= 2*3;
  5202. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5203. pipe_config->pipe_bpp);
  5204. needs_recompute = true;
  5205. pipe_config->bw_constrained = true;
  5206. goto retry;
  5207. }
  5208. if (needs_recompute)
  5209. return RETRY;
  5210. return ret;
  5211. }
  5212. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5213. {
  5214. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5215. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5216. /* IPS only exists on ULT machines and is tied to pipe A. */
  5217. if (!hsw_crtc_supports_ips(crtc))
  5218. return false;
  5219. if (!i915_modparams.enable_ips)
  5220. return false;
  5221. if (crtc_state->pipe_bpp > 24)
  5222. return false;
  5223. /*
  5224. * We compare against max which means we must take
  5225. * the increased cdclk requirement into account when
  5226. * calculating the new cdclk.
  5227. *
  5228. * Should measure whether using a lower cdclk w/o IPS
  5229. */
  5230. if (IS_BROADWELL(dev_priv) &&
  5231. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5232. return false;
  5233. return true;
  5234. }
  5235. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5236. {
  5237. struct drm_i915_private *dev_priv =
  5238. to_i915(crtc_state->base.crtc->dev);
  5239. struct intel_atomic_state *intel_state =
  5240. to_intel_atomic_state(crtc_state->base.state);
  5241. if (!hsw_crtc_state_ips_capable(crtc_state))
  5242. return false;
  5243. if (crtc_state->ips_force_disable)
  5244. return false;
  5245. /* IPS should be fine as long as at least one plane is enabled. */
  5246. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5247. return false;
  5248. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5249. if (IS_BROADWELL(dev_priv) &&
  5250. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5251. return false;
  5252. return true;
  5253. }
  5254. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5255. {
  5256. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5257. /* GDG double wide on either pipe, otherwise pipe A only */
  5258. return INTEL_INFO(dev_priv)->gen < 4 &&
  5259. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5260. }
  5261. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5262. {
  5263. uint32_t pixel_rate;
  5264. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5265. /*
  5266. * We only use IF-ID interlacing. If we ever use
  5267. * PF-ID we'll need to adjust the pixel_rate here.
  5268. */
  5269. if (pipe_config->pch_pfit.enabled) {
  5270. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5271. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5272. pipe_w = pipe_config->pipe_src_w;
  5273. pipe_h = pipe_config->pipe_src_h;
  5274. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5275. pfit_h = pfit_size & 0xFFFF;
  5276. if (pipe_w < pfit_w)
  5277. pipe_w = pfit_w;
  5278. if (pipe_h < pfit_h)
  5279. pipe_h = pfit_h;
  5280. if (WARN_ON(!pfit_w || !pfit_h))
  5281. return pixel_rate;
  5282. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5283. pfit_w * pfit_h);
  5284. }
  5285. return pixel_rate;
  5286. }
  5287. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5288. {
  5289. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5290. if (HAS_GMCH_DISPLAY(dev_priv))
  5291. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5292. crtc_state->pixel_rate =
  5293. crtc_state->base.adjusted_mode.crtc_clock;
  5294. else
  5295. crtc_state->pixel_rate =
  5296. ilk_pipe_pixel_rate(crtc_state);
  5297. }
  5298. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5299. struct intel_crtc_state *pipe_config)
  5300. {
  5301. struct drm_device *dev = crtc->base.dev;
  5302. struct drm_i915_private *dev_priv = to_i915(dev);
  5303. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5304. int clock_limit = dev_priv->max_dotclk_freq;
  5305. if (INTEL_GEN(dev_priv) < 4) {
  5306. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5307. /*
  5308. * Enable double wide mode when the dot clock
  5309. * is > 90% of the (display) core speed.
  5310. */
  5311. if (intel_crtc_supports_double_wide(crtc) &&
  5312. adjusted_mode->crtc_clock > clock_limit) {
  5313. clock_limit = dev_priv->max_dotclk_freq;
  5314. pipe_config->double_wide = true;
  5315. }
  5316. }
  5317. if (adjusted_mode->crtc_clock > clock_limit) {
  5318. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5319. adjusted_mode->crtc_clock, clock_limit,
  5320. yesno(pipe_config->double_wide));
  5321. return -EINVAL;
  5322. }
  5323. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5324. /*
  5325. * There is only one pipe CSC unit per pipe, and we need that
  5326. * for output conversion from RGB->YCBCR. So if CTM is already
  5327. * applied we can't support YCBCR420 output.
  5328. */
  5329. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5330. return -EINVAL;
  5331. }
  5332. /*
  5333. * Pipe horizontal size must be even in:
  5334. * - DVO ganged mode
  5335. * - LVDS dual channel mode
  5336. * - Double wide pipe
  5337. */
  5338. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5339. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5340. pipe_config->pipe_src_w &= ~1;
  5341. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5342. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5343. */
  5344. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5345. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5346. return -EINVAL;
  5347. intel_crtc_compute_pixel_rate(pipe_config);
  5348. if (pipe_config->has_pch_encoder)
  5349. return ironlake_fdi_compute_config(crtc, pipe_config);
  5350. return 0;
  5351. }
  5352. static void
  5353. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5354. {
  5355. while (*num > DATA_LINK_M_N_MASK ||
  5356. *den > DATA_LINK_M_N_MASK) {
  5357. *num >>= 1;
  5358. *den >>= 1;
  5359. }
  5360. }
  5361. static void compute_m_n(unsigned int m, unsigned int n,
  5362. uint32_t *ret_m, uint32_t *ret_n,
  5363. bool reduce_m_n)
  5364. {
  5365. /*
  5366. * Reduce M/N as much as possible without loss in precision. Several DP
  5367. * dongles in particular seem to be fussy about too large *link* M/N
  5368. * values. The passed in values are more likely to have the least
  5369. * significant bits zero than M after rounding below, so do this first.
  5370. */
  5371. if (reduce_m_n) {
  5372. while ((m & 1) == 0 && (n & 1) == 0) {
  5373. m >>= 1;
  5374. n >>= 1;
  5375. }
  5376. }
  5377. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5378. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5379. intel_reduce_m_n_ratio(ret_m, ret_n);
  5380. }
  5381. void
  5382. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5383. int pixel_clock, int link_clock,
  5384. struct intel_link_m_n *m_n,
  5385. bool reduce_m_n)
  5386. {
  5387. m_n->tu = 64;
  5388. compute_m_n(bits_per_pixel * pixel_clock,
  5389. link_clock * nlanes * 8,
  5390. &m_n->gmch_m, &m_n->gmch_n,
  5391. reduce_m_n);
  5392. compute_m_n(pixel_clock, link_clock,
  5393. &m_n->link_m, &m_n->link_n,
  5394. reduce_m_n);
  5395. }
  5396. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5397. {
  5398. if (i915_modparams.panel_use_ssc >= 0)
  5399. return i915_modparams.panel_use_ssc != 0;
  5400. return dev_priv->vbt.lvds_use_ssc
  5401. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5402. }
  5403. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5404. {
  5405. return (1 << dpll->n) << 16 | dpll->m2;
  5406. }
  5407. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5408. {
  5409. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5410. }
  5411. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5412. struct intel_crtc_state *crtc_state,
  5413. struct dpll *reduced_clock)
  5414. {
  5415. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5416. u32 fp, fp2 = 0;
  5417. if (IS_PINEVIEW(dev_priv)) {
  5418. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5419. if (reduced_clock)
  5420. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5421. } else {
  5422. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5423. if (reduced_clock)
  5424. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5425. }
  5426. crtc_state->dpll_hw_state.fp0 = fp;
  5427. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5428. reduced_clock) {
  5429. crtc_state->dpll_hw_state.fp1 = fp2;
  5430. } else {
  5431. crtc_state->dpll_hw_state.fp1 = fp;
  5432. }
  5433. }
  5434. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5435. pipe)
  5436. {
  5437. u32 reg_val;
  5438. /*
  5439. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5440. * and set it to a reasonable value instead.
  5441. */
  5442. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5443. reg_val &= 0xffffff00;
  5444. reg_val |= 0x00000030;
  5445. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5446. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5447. reg_val &= 0x00ffffff;
  5448. reg_val |= 0x8c000000;
  5449. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5450. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5451. reg_val &= 0xffffff00;
  5452. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5453. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5454. reg_val &= 0x00ffffff;
  5455. reg_val |= 0xb0000000;
  5456. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5457. }
  5458. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5459. struct intel_link_m_n *m_n)
  5460. {
  5461. struct drm_device *dev = crtc->base.dev;
  5462. struct drm_i915_private *dev_priv = to_i915(dev);
  5463. int pipe = crtc->pipe;
  5464. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5465. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5466. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5467. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5468. }
  5469. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5470. struct intel_link_m_n *m_n,
  5471. struct intel_link_m_n *m2_n2)
  5472. {
  5473. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5474. int pipe = crtc->pipe;
  5475. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5476. if (INTEL_GEN(dev_priv) >= 5) {
  5477. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5478. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5479. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5480. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5481. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5482. * for gen < 8) and if DRRS is supported (to make sure the
  5483. * registers are not unnecessarily accessed).
  5484. */
  5485. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5486. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5487. I915_WRITE(PIPE_DATA_M2(transcoder),
  5488. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5489. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5490. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5491. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5492. }
  5493. } else {
  5494. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5495. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5496. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5497. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5498. }
  5499. }
  5500. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5501. {
  5502. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5503. if (m_n == M1_N1) {
  5504. dp_m_n = &crtc->config->dp_m_n;
  5505. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5506. } else if (m_n == M2_N2) {
  5507. /*
  5508. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5509. * needs to be programmed into M1_N1.
  5510. */
  5511. dp_m_n = &crtc->config->dp_m2_n2;
  5512. } else {
  5513. DRM_ERROR("Unsupported divider value\n");
  5514. return;
  5515. }
  5516. if (crtc->config->has_pch_encoder)
  5517. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5518. else
  5519. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5520. }
  5521. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5522. struct intel_crtc_state *pipe_config)
  5523. {
  5524. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5525. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5526. if (crtc->pipe != PIPE_A)
  5527. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5528. /* DPLL not used with DSI, but still need the rest set up */
  5529. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5530. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5531. DPLL_EXT_BUFFER_ENABLE_VLV;
  5532. pipe_config->dpll_hw_state.dpll_md =
  5533. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5534. }
  5535. static void chv_compute_dpll(struct intel_crtc *crtc,
  5536. struct intel_crtc_state *pipe_config)
  5537. {
  5538. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5539. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5540. if (crtc->pipe != PIPE_A)
  5541. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5542. /* DPLL not used with DSI, but still need the rest set up */
  5543. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5544. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5545. pipe_config->dpll_hw_state.dpll_md =
  5546. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5547. }
  5548. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5549. const struct intel_crtc_state *pipe_config)
  5550. {
  5551. struct drm_device *dev = crtc->base.dev;
  5552. struct drm_i915_private *dev_priv = to_i915(dev);
  5553. enum pipe pipe = crtc->pipe;
  5554. u32 mdiv;
  5555. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5556. u32 coreclk, reg_val;
  5557. /* Enable Refclk */
  5558. I915_WRITE(DPLL(pipe),
  5559. pipe_config->dpll_hw_state.dpll &
  5560. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5561. /* No need to actually set up the DPLL with DSI */
  5562. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5563. return;
  5564. mutex_lock(&dev_priv->sb_lock);
  5565. bestn = pipe_config->dpll.n;
  5566. bestm1 = pipe_config->dpll.m1;
  5567. bestm2 = pipe_config->dpll.m2;
  5568. bestp1 = pipe_config->dpll.p1;
  5569. bestp2 = pipe_config->dpll.p2;
  5570. /* See eDP HDMI DPIO driver vbios notes doc */
  5571. /* PLL B needs special handling */
  5572. if (pipe == PIPE_B)
  5573. vlv_pllb_recal_opamp(dev_priv, pipe);
  5574. /* Set up Tx target for periodic Rcomp update */
  5575. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5576. /* Disable target IRef on PLL */
  5577. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5578. reg_val &= 0x00ffffff;
  5579. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5580. /* Disable fast lock */
  5581. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5582. /* Set idtafcrecal before PLL is enabled */
  5583. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5584. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5585. mdiv |= ((bestn << DPIO_N_SHIFT));
  5586. mdiv |= (1 << DPIO_K_SHIFT);
  5587. /*
  5588. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5589. * but we don't support that).
  5590. * Note: don't use the DAC post divider as it seems unstable.
  5591. */
  5592. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5593. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5594. mdiv |= DPIO_ENABLE_CALIBRATION;
  5595. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5596. /* Set HBR and RBR LPF coefficients */
  5597. if (pipe_config->port_clock == 162000 ||
  5598. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5599. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5600. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5601. 0x009f0003);
  5602. else
  5603. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5604. 0x00d0000f);
  5605. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5606. /* Use SSC source */
  5607. if (pipe == PIPE_A)
  5608. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5609. 0x0df40000);
  5610. else
  5611. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5612. 0x0df70000);
  5613. } else { /* HDMI or VGA */
  5614. /* Use bend source */
  5615. if (pipe == PIPE_A)
  5616. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5617. 0x0df70000);
  5618. else
  5619. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5620. 0x0df40000);
  5621. }
  5622. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5623. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5624. if (intel_crtc_has_dp_encoder(crtc->config))
  5625. coreclk |= 0x01000000;
  5626. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5627. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5628. mutex_unlock(&dev_priv->sb_lock);
  5629. }
  5630. static void chv_prepare_pll(struct intel_crtc *crtc,
  5631. const struct intel_crtc_state *pipe_config)
  5632. {
  5633. struct drm_device *dev = crtc->base.dev;
  5634. struct drm_i915_private *dev_priv = to_i915(dev);
  5635. enum pipe pipe = crtc->pipe;
  5636. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5637. u32 loopfilter, tribuf_calcntr;
  5638. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5639. u32 dpio_val;
  5640. int vco;
  5641. /* Enable Refclk and SSC */
  5642. I915_WRITE(DPLL(pipe),
  5643. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5644. /* No need to actually set up the DPLL with DSI */
  5645. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5646. return;
  5647. bestn = pipe_config->dpll.n;
  5648. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5649. bestm1 = pipe_config->dpll.m1;
  5650. bestm2 = pipe_config->dpll.m2 >> 22;
  5651. bestp1 = pipe_config->dpll.p1;
  5652. bestp2 = pipe_config->dpll.p2;
  5653. vco = pipe_config->dpll.vco;
  5654. dpio_val = 0;
  5655. loopfilter = 0;
  5656. mutex_lock(&dev_priv->sb_lock);
  5657. /* p1 and p2 divider */
  5658. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5659. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5660. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5661. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5662. 1 << DPIO_CHV_K_DIV_SHIFT);
  5663. /* Feedback post-divider - m2 */
  5664. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5665. /* Feedback refclk divider - n and m1 */
  5666. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5667. DPIO_CHV_M1_DIV_BY_2 |
  5668. 1 << DPIO_CHV_N_DIV_SHIFT);
  5669. /* M2 fraction division */
  5670. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5671. /* M2 fraction division enable */
  5672. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5673. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5674. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5675. if (bestm2_frac)
  5676. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5677. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5678. /* Program digital lock detect threshold */
  5679. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5680. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5681. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5682. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5683. if (!bestm2_frac)
  5684. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5685. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5686. /* Loop filter */
  5687. if (vco == 5400000) {
  5688. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5689. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5690. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5691. tribuf_calcntr = 0x9;
  5692. } else if (vco <= 6200000) {
  5693. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5694. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5695. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5696. tribuf_calcntr = 0x9;
  5697. } else if (vco <= 6480000) {
  5698. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5699. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5700. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5701. tribuf_calcntr = 0x8;
  5702. } else {
  5703. /* Not supported. Apply the same limits as in the max case */
  5704. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5705. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5706. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5707. tribuf_calcntr = 0;
  5708. }
  5709. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5710. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5711. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5712. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5713. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5714. /* AFC Recal */
  5715. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5716. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5717. DPIO_AFC_RECAL);
  5718. mutex_unlock(&dev_priv->sb_lock);
  5719. }
  5720. /**
  5721. * vlv_force_pll_on - forcibly enable just the PLL
  5722. * @dev_priv: i915 private structure
  5723. * @pipe: pipe PLL to enable
  5724. * @dpll: PLL configuration
  5725. *
  5726. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5727. * in cases where we need the PLL enabled even when @pipe is not going to
  5728. * be enabled.
  5729. */
  5730. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5731. const struct dpll *dpll)
  5732. {
  5733. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5734. struct intel_crtc_state *pipe_config;
  5735. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5736. if (!pipe_config)
  5737. return -ENOMEM;
  5738. pipe_config->base.crtc = &crtc->base;
  5739. pipe_config->pixel_multiplier = 1;
  5740. pipe_config->dpll = *dpll;
  5741. if (IS_CHERRYVIEW(dev_priv)) {
  5742. chv_compute_dpll(crtc, pipe_config);
  5743. chv_prepare_pll(crtc, pipe_config);
  5744. chv_enable_pll(crtc, pipe_config);
  5745. } else {
  5746. vlv_compute_dpll(crtc, pipe_config);
  5747. vlv_prepare_pll(crtc, pipe_config);
  5748. vlv_enable_pll(crtc, pipe_config);
  5749. }
  5750. kfree(pipe_config);
  5751. return 0;
  5752. }
  5753. /**
  5754. * vlv_force_pll_off - forcibly disable just the PLL
  5755. * @dev_priv: i915 private structure
  5756. * @pipe: pipe PLL to disable
  5757. *
  5758. * Disable the PLL for @pipe. To be used in cases where we need
  5759. * the PLL enabled even when @pipe is not going to be enabled.
  5760. */
  5761. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5762. {
  5763. if (IS_CHERRYVIEW(dev_priv))
  5764. chv_disable_pll(dev_priv, pipe);
  5765. else
  5766. vlv_disable_pll(dev_priv, pipe);
  5767. }
  5768. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5769. struct intel_crtc_state *crtc_state,
  5770. struct dpll *reduced_clock)
  5771. {
  5772. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5773. u32 dpll;
  5774. struct dpll *clock = &crtc_state->dpll;
  5775. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5776. dpll = DPLL_VGA_MODE_DIS;
  5777. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5778. dpll |= DPLLB_MODE_LVDS;
  5779. else
  5780. dpll |= DPLLB_MODE_DAC_SERIAL;
  5781. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5782. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5783. dpll |= (crtc_state->pixel_multiplier - 1)
  5784. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5785. }
  5786. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5787. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5788. dpll |= DPLL_SDVO_HIGH_SPEED;
  5789. if (intel_crtc_has_dp_encoder(crtc_state))
  5790. dpll |= DPLL_SDVO_HIGH_SPEED;
  5791. /* compute bitmask from p1 value */
  5792. if (IS_PINEVIEW(dev_priv))
  5793. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5794. else {
  5795. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5796. if (IS_G4X(dev_priv) && reduced_clock)
  5797. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5798. }
  5799. switch (clock->p2) {
  5800. case 5:
  5801. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5802. break;
  5803. case 7:
  5804. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5805. break;
  5806. case 10:
  5807. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5808. break;
  5809. case 14:
  5810. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5811. break;
  5812. }
  5813. if (INTEL_GEN(dev_priv) >= 4)
  5814. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5815. if (crtc_state->sdvo_tv_clock)
  5816. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5817. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5818. intel_panel_use_ssc(dev_priv))
  5819. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5820. else
  5821. dpll |= PLL_REF_INPUT_DREFCLK;
  5822. dpll |= DPLL_VCO_ENABLE;
  5823. crtc_state->dpll_hw_state.dpll = dpll;
  5824. if (INTEL_GEN(dev_priv) >= 4) {
  5825. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5826. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5827. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5828. }
  5829. }
  5830. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5831. struct intel_crtc_state *crtc_state,
  5832. struct dpll *reduced_clock)
  5833. {
  5834. struct drm_device *dev = crtc->base.dev;
  5835. struct drm_i915_private *dev_priv = to_i915(dev);
  5836. u32 dpll;
  5837. struct dpll *clock = &crtc_state->dpll;
  5838. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5839. dpll = DPLL_VGA_MODE_DIS;
  5840. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5841. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5842. } else {
  5843. if (clock->p1 == 2)
  5844. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5845. else
  5846. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5847. if (clock->p2 == 4)
  5848. dpll |= PLL_P2_DIVIDE_BY_4;
  5849. }
  5850. if (!IS_I830(dev_priv) &&
  5851. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5852. dpll |= DPLL_DVO_2X_MODE;
  5853. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5854. intel_panel_use_ssc(dev_priv))
  5855. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5856. else
  5857. dpll |= PLL_REF_INPUT_DREFCLK;
  5858. dpll |= DPLL_VCO_ENABLE;
  5859. crtc_state->dpll_hw_state.dpll = dpll;
  5860. }
  5861. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5862. {
  5863. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5864. enum pipe pipe = intel_crtc->pipe;
  5865. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5866. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5867. uint32_t crtc_vtotal, crtc_vblank_end;
  5868. int vsyncshift = 0;
  5869. /* We need to be careful not to changed the adjusted mode, for otherwise
  5870. * the hw state checker will get angry at the mismatch. */
  5871. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5872. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5873. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5874. /* the chip adds 2 halflines automatically */
  5875. crtc_vtotal -= 1;
  5876. crtc_vblank_end -= 1;
  5877. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5878. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5879. else
  5880. vsyncshift = adjusted_mode->crtc_hsync_start -
  5881. adjusted_mode->crtc_htotal / 2;
  5882. if (vsyncshift < 0)
  5883. vsyncshift += adjusted_mode->crtc_htotal;
  5884. }
  5885. if (INTEL_GEN(dev_priv) > 3)
  5886. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5887. I915_WRITE(HTOTAL(cpu_transcoder),
  5888. (adjusted_mode->crtc_hdisplay - 1) |
  5889. ((adjusted_mode->crtc_htotal - 1) << 16));
  5890. I915_WRITE(HBLANK(cpu_transcoder),
  5891. (adjusted_mode->crtc_hblank_start - 1) |
  5892. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5893. I915_WRITE(HSYNC(cpu_transcoder),
  5894. (adjusted_mode->crtc_hsync_start - 1) |
  5895. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5896. I915_WRITE(VTOTAL(cpu_transcoder),
  5897. (adjusted_mode->crtc_vdisplay - 1) |
  5898. ((crtc_vtotal - 1) << 16));
  5899. I915_WRITE(VBLANK(cpu_transcoder),
  5900. (adjusted_mode->crtc_vblank_start - 1) |
  5901. ((crtc_vblank_end - 1) << 16));
  5902. I915_WRITE(VSYNC(cpu_transcoder),
  5903. (adjusted_mode->crtc_vsync_start - 1) |
  5904. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5905. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5906. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5907. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5908. * bits. */
  5909. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5910. (pipe == PIPE_B || pipe == PIPE_C))
  5911. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5912. }
  5913. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5914. {
  5915. struct drm_device *dev = intel_crtc->base.dev;
  5916. struct drm_i915_private *dev_priv = to_i915(dev);
  5917. enum pipe pipe = intel_crtc->pipe;
  5918. /* pipesrc controls the size that is scaled from, which should
  5919. * always be the user's requested size.
  5920. */
  5921. I915_WRITE(PIPESRC(pipe),
  5922. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5923. (intel_crtc->config->pipe_src_h - 1));
  5924. }
  5925. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5926. struct intel_crtc_state *pipe_config)
  5927. {
  5928. struct drm_device *dev = crtc->base.dev;
  5929. struct drm_i915_private *dev_priv = to_i915(dev);
  5930. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5931. uint32_t tmp;
  5932. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5933. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5934. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5935. tmp = I915_READ(HBLANK(cpu_transcoder));
  5936. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5937. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5938. tmp = I915_READ(HSYNC(cpu_transcoder));
  5939. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5940. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5941. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5942. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5943. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5944. tmp = I915_READ(VBLANK(cpu_transcoder));
  5945. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5946. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5947. tmp = I915_READ(VSYNC(cpu_transcoder));
  5948. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5949. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5950. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5951. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5952. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5953. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5954. }
  5955. }
  5956. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5957. struct intel_crtc_state *pipe_config)
  5958. {
  5959. struct drm_device *dev = crtc->base.dev;
  5960. struct drm_i915_private *dev_priv = to_i915(dev);
  5961. u32 tmp;
  5962. tmp = I915_READ(PIPESRC(crtc->pipe));
  5963. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5964. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5965. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5966. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5967. }
  5968. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5969. struct intel_crtc_state *pipe_config)
  5970. {
  5971. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5972. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5973. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5974. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5975. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5976. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5977. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5978. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5979. mode->flags = pipe_config->base.adjusted_mode.flags;
  5980. mode->type = DRM_MODE_TYPE_DRIVER;
  5981. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5982. mode->hsync = drm_mode_hsync(mode);
  5983. mode->vrefresh = drm_mode_vrefresh(mode);
  5984. drm_mode_set_name(mode);
  5985. }
  5986. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5987. {
  5988. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5989. uint32_t pipeconf;
  5990. pipeconf = 0;
  5991. /* we keep both pipes enabled on 830 */
  5992. if (IS_I830(dev_priv))
  5993. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5994. if (intel_crtc->config->double_wide)
  5995. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5996. /* only g4x and later have fancy bpc/dither controls */
  5997. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5998. IS_CHERRYVIEW(dev_priv)) {
  5999. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6000. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6001. pipeconf |= PIPECONF_DITHER_EN |
  6002. PIPECONF_DITHER_TYPE_SP;
  6003. switch (intel_crtc->config->pipe_bpp) {
  6004. case 18:
  6005. pipeconf |= PIPECONF_6BPC;
  6006. break;
  6007. case 24:
  6008. pipeconf |= PIPECONF_8BPC;
  6009. break;
  6010. case 30:
  6011. pipeconf |= PIPECONF_10BPC;
  6012. break;
  6013. default:
  6014. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6015. BUG();
  6016. }
  6017. }
  6018. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6019. if (INTEL_GEN(dev_priv) < 4 ||
  6020. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6021. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6022. else
  6023. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6024. } else
  6025. pipeconf |= PIPECONF_PROGRESSIVE;
  6026. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6027. intel_crtc->config->limited_color_range)
  6028. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6029. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6030. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6031. }
  6032. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6033. struct intel_crtc_state *crtc_state)
  6034. {
  6035. struct drm_device *dev = crtc->base.dev;
  6036. struct drm_i915_private *dev_priv = to_i915(dev);
  6037. const struct intel_limit *limit;
  6038. int refclk = 48000;
  6039. memset(&crtc_state->dpll_hw_state, 0,
  6040. sizeof(crtc_state->dpll_hw_state));
  6041. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6042. if (intel_panel_use_ssc(dev_priv)) {
  6043. refclk = dev_priv->vbt.lvds_ssc_freq;
  6044. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6045. }
  6046. limit = &intel_limits_i8xx_lvds;
  6047. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6048. limit = &intel_limits_i8xx_dvo;
  6049. } else {
  6050. limit = &intel_limits_i8xx_dac;
  6051. }
  6052. if (!crtc_state->clock_set &&
  6053. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6054. refclk, NULL, &crtc_state->dpll)) {
  6055. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6056. return -EINVAL;
  6057. }
  6058. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6059. return 0;
  6060. }
  6061. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6062. struct intel_crtc_state *crtc_state)
  6063. {
  6064. struct drm_device *dev = crtc->base.dev;
  6065. struct drm_i915_private *dev_priv = to_i915(dev);
  6066. const struct intel_limit *limit;
  6067. int refclk = 96000;
  6068. memset(&crtc_state->dpll_hw_state, 0,
  6069. sizeof(crtc_state->dpll_hw_state));
  6070. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6071. if (intel_panel_use_ssc(dev_priv)) {
  6072. refclk = dev_priv->vbt.lvds_ssc_freq;
  6073. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6074. }
  6075. if (intel_is_dual_link_lvds(dev))
  6076. limit = &intel_limits_g4x_dual_channel_lvds;
  6077. else
  6078. limit = &intel_limits_g4x_single_channel_lvds;
  6079. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6080. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6081. limit = &intel_limits_g4x_hdmi;
  6082. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6083. limit = &intel_limits_g4x_sdvo;
  6084. } else {
  6085. /* The option is for other outputs */
  6086. limit = &intel_limits_i9xx_sdvo;
  6087. }
  6088. if (!crtc_state->clock_set &&
  6089. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6090. refclk, NULL, &crtc_state->dpll)) {
  6091. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6092. return -EINVAL;
  6093. }
  6094. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6095. return 0;
  6096. }
  6097. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6098. struct intel_crtc_state *crtc_state)
  6099. {
  6100. struct drm_device *dev = crtc->base.dev;
  6101. struct drm_i915_private *dev_priv = to_i915(dev);
  6102. const struct intel_limit *limit;
  6103. int refclk = 96000;
  6104. memset(&crtc_state->dpll_hw_state, 0,
  6105. sizeof(crtc_state->dpll_hw_state));
  6106. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6107. if (intel_panel_use_ssc(dev_priv)) {
  6108. refclk = dev_priv->vbt.lvds_ssc_freq;
  6109. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6110. }
  6111. limit = &intel_limits_pineview_lvds;
  6112. } else {
  6113. limit = &intel_limits_pineview_sdvo;
  6114. }
  6115. if (!crtc_state->clock_set &&
  6116. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6117. refclk, NULL, &crtc_state->dpll)) {
  6118. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6119. return -EINVAL;
  6120. }
  6121. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6122. return 0;
  6123. }
  6124. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6125. struct intel_crtc_state *crtc_state)
  6126. {
  6127. struct drm_device *dev = crtc->base.dev;
  6128. struct drm_i915_private *dev_priv = to_i915(dev);
  6129. const struct intel_limit *limit;
  6130. int refclk = 96000;
  6131. memset(&crtc_state->dpll_hw_state, 0,
  6132. sizeof(crtc_state->dpll_hw_state));
  6133. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6134. if (intel_panel_use_ssc(dev_priv)) {
  6135. refclk = dev_priv->vbt.lvds_ssc_freq;
  6136. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6137. }
  6138. limit = &intel_limits_i9xx_lvds;
  6139. } else {
  6140. limit = &intel_limits_i9xx_sdvo;
  6141. }
  6142. if (!crtc_state->clock_set &&
  6143. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6144. refclk, NULL, &crtc_state->dpll)) {
  6145. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6146. return -EINVAL;
  6147. }
  6148. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6149. return 0;
  6150. }
  6151. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6152. struct intel_crtc_state *crtc_state)
  6153. {
  6154. int refclk = 100000;
  6155. const struct intel_limit *limit = &intel_limits_chv;
  6156. memset(&crtc_state->dpll_hw_state, 0,
  6157. sizeof(crtc_state->dpll_hw_state));
  6158. if (!crtc_state->clock_set &&
  6159. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6160. refclk, NULL, &crtc_state->dpll)) {
  6161. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6162. return -EINVAL;
  6163. }
  6164. chv_compute_dpll(crtc, crtc_state);
  6165. return 0;
  6166. }
  6167. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6168. struct intel_crtc_state *crtc_state)
  6169. {
  6170. int refclk = 100000;
  6171. const struct intel_limit *limit = &intel_limits_vlv;
  6172. memset(&crtc_state->dpll_hw_state, 0,
  6173. sizeof(crtc_state->dpll_hw_state));
  6174. if (!crtc_state->clock_set &&
  6175. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6176. refclk, NULL, &crtc_state->dpll)) {
  6177. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6178. return -EINVAL;
  6179. }
  6180. vlv_compute_dpll(crtc, crtc_state);
  6181. return 0;
  6182. }
  6183. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6184. struct intel_crtc_state *pipe_config)
  6185. {
  6186. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6187. uint32_t tmp;
  6188. if (INTEL_GEN(dev_priv) <= 3 &&
  6189. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6190. return;
  6191. tmp = I915_READ(PFIT_CONTROL);
  6192. if (!(tmp & PFIT_ENABLE))
  6193. return;
  6194. /* Check whether the pfit is attached to our pipe. */
  6195. if (INTEL_GEN(dev_priv) < 4) {
  6196. if (crtc->pipe != PIPE_B)
  6197. return;
  6198. } else {
  6199. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6200. return;
  6201. }
  6202. pipe_config->gmch_pfit.control = tmp;
  6203. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6204. }
  6205. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6206. struct intel_crtc_state *pipe_config)
  6207. {
  6208. struct drm_device *dev = crtc->base.dev;
  6209. struct drm_i915_private *dev_priv = to_i915(dev);
  6210. int pipe = pipe_config->cpu_transcoder;
  6211. struct dpll clock;
  6212. u32 mdiv;
  6213. int refclk = 100000;
  6214. /* In case of DSI, DPLL will not be used */
  6215. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6216. return;
  6217. mutex_lock(&dev_priv->sb_lock);
  6218. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6219. mutex_unlock(&dev_priv->sb_lock);
  6220. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6221. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6222. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6223. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6224. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6225. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6226. }
  6227. static void
  6228. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6229. struct intel_initial_plane_config *plane_config)
  6230. {
  6231. struct drm_device *dev = crtc->base.dev;
  6232. struct drm_i915_private *dev_priv = to_i915(dev);
  6233. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6234. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6235. enum pipe pipe = crtc->pipe;
  6236. u32 val, base, offset;
  6237. int fourcc, pixel_format;
  6238. unsigned int aligned_height;
  6239. struct drm_framebuffer *fb;
  6240. struct intel_framebuffer *intel_fb;
  6241. if (!plane->get_hw_state(plane))
  6242. return;
  6243. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6244. if (!intel_fb) {
  6245. DRM_DEBUG_KMS("failed to alloc fb\n");
  6246. return;
  6247. }
  6248. fb = &intel_fb->base;
  6249. fb->dev = dev;
  6250. val = I915_READ(DSPCNTR(i9xx_plane));
  6251. if (INTEL_GEN(dev_priv) >= 4) {
  6252. if (val & DISPPLANE_TILED) {
  6253. plane_config->tiling = I915_TILING_X;
  6254. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6255. }
  6256. }
  6257. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6258. fourcc = i9xx_format_to_fourcc(pixel_format);
  6259. fb->format = drm_format_info(fourcc);
  6260. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6261. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6262. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6263. } else if (INTEL_GEN(dev_priv) >= 4) {
  6264. if (plane_config->tiling)
  6265. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6266. else
  6267. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6268. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6269. } else {
  6270. base = I915_READ(DSPADDR(i9xx_plane));
  6271. }
  6272. plane_config->base = base;
  6273. val = I915_READ(PIPESRC(pipe));
  6274. fb->width = ((val >> 16) & 0xfff) + 1;
  6275. fb->height = ((val >> 0) & 0xfff) + 1;
  6276. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6277. fb->pitches[0] = val & 0xffffffc0;
  6278. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6279. plane_config->size = fb->pitches[0] * aligned_height;
  6280. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6281. crtc->base.name, plane->base.name, fb->width, fb->height,
  6282. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6283. plane_config->size);
  6284. plane_config->fb = intel_fb;
  6285. }
  6286. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6287. struct intel_crtc_state *pipe_config)
  6288. {
  6289. struct drm_device *dev = crtc->base.dev;
  6290. struct drm_i915_private *dev_priv = to_i915(dev);
  6291. int pipe = pipe_config->cpu_transcoder;
  6292. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6293. struct dpll clock;
  6294. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6295. int refclk = 100000;
  6296. /* In case of DSI, DPLL will not be used */
  6297. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6298. return;
  6299. mutex_lock(&dev_priv->sb_lock);
  6300. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6301. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6302. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6303. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6304. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6305. mutex_unlock(&dev_priv->sb_lock);
  6306. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6307. clock.m2 = (pll_dw0 & 0xff) << 22;
  6308. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6309. clock.m2 |= pll_dw2 & 0x3fffff;
  6310. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6311. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6312. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6313. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6314. }
  6315. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6316. struct intel_crtc_state *pipe_config)
  6317. {
  6318. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6319. enum intel_display_power_domain power_domain;
  6320. uint32_t tmp;
  6321. bool ret;
  6322. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6323. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6324. return false;
  6325. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6326. pipe_config->shared_dpll = NULL;
  6327. ret = false;
  6328. tmp = I915_READ(PIPECONF(crtc->pipe));
  6329. if (!(tmp & PIPECONF_ENABLE))
  6330. goto out;
  6331. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6332. IS_CHERRYVIEW(dev_priv)) {
  6333. switch (tmp & PIPECONF_BPC_MASK) {
  6334. case PIPECONF_6BPC:
  6335. pipe_config->pipe_bpp = 18;
  6336. break;
  6337. case PIPECONF_8BPC:
  6338. pipe_config->pipe_bpp = 24;
  6339. break;
  6340. case PIPECONF_10BPC:
  6341. pipe_config->pipe_bpp = 30;
  6342. break;
  6343. default:
  6344. break;
  6345. }
  6346. }
  6347. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6348. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6349. pipe_config->limited_color_range = true;
  6350. if (INTEL_GEN(dev_priv) < 4)
  6351. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6352. intel_get_pipe_timings(crtc, pipe_config);
  6353. intel_get_pipe_src_size(crtc, pipe_config);
  6354. i9xx_get_pfit_config(crtc, pipe_config);
  6355. if (INTEL_GEN(dev_priv) >= 4) {
  6356. /* No way to read it out on pipes B and C */
  6357. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6358. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6359. else
  6360. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6361. pipe_config->pixel_multiplier =
  6362. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6363. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6364. pipe_config->dpll_hw_state.dpll_md = tmp;
  6365. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6366. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6367. tmp = I915_READ(DPLL(crtc->pipe));
  6368. pipe_config->pixel_multiplier =
  6369. ((tmp & SDVO_MULTIPLIER_MASK)
  6370. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6371. } else {
  6372. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6373. * port and will be fixed up in the encoder->get_config
  6374. * function. */
  6375. pipe_config->pixel_multiplier = 1;
  6376. }
  6377. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6378. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6379. /*
  6380. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6381. * on 830. Filter it out here so that we don't
  6382. * report errors due to that.
  6383. */
  6384. if (IS_I830(dev_priv))
  6385. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6386. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6387. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6388. } else {
  6389. /* Mask out read-only status bits. */
  6390. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6391. DPLL_PORTC_READY_MASK |
  6392. DPLL_PORTB_READY_MASK);
  6393. }
  6394. if (IS_CHERRYVIEW(dev_priv))
  6395. chv_crtc_clock_get(crtc, pipe_config);
  6396. else if (IS_VALLEYVIEW(dev_priv))
  6397. vlv_crtc_clock_get(crtc, pipe_config);
  6398. else
  6399. i9xx_crtc_clock_get(crtc, pipe_config);
  6400. /*
  6401. * Normally the dotclock is filled in by the encoder .get_config()
  6402. * but in case the pipe is enabled w/o any ports we need a sane
  6403. * default.
  6404. */
  6405. pipe_config->base.adjusted_mode.crtc_clock =
  6406. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6407. ret = true;
  6408. out:
  6409. intel_display_power_put(dev_priv, power_domain);
  6410. return ret;
  6411. }
  6412. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6413. {
  6414. struct intel_encoder *encoder;
  6415. int i;
  6416. u32 val, final;
  6417. bool has_lvds = false;
  6418. bool has_cpu_edp = false;
  6419. bool has_panel = false;
  6420. bool has_ck505 = false;
  6421. bool can_ssc = false;
  6422. bool using_ssc_source = false;
  6423. /* We need to take the global config into account */
  6424. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6425. switch (encoder->type) {
  6426. case INTEL_OUTPUT_LVDS:
  6427. has_panel = true;
  6428. has_lvds = true;
  6429. break;
  6430. case INTEL_OUTPUT_EDP:
  6431. has_panel = true;
  6432. if (encoder->port == PORT_A)
  6433. has_cpu_edp = true;
  6434. break;
  6435. default:
  6436. break;
  6437. }
  6438. }
  6439. if (HAS_PCH_IBX(dev_priv)) {
  6440. has_ck505 = dev_priv->vbt.display_clock_mode;
  6441. can_ssc = has_ck505;
  6442. } else {
  6443. has_ck505 = false;
  6444. can_ssc = true;
  6445. }
  6446. /* Check if any DPLLs are using the SSC source */
  6447. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6448. u32 temp = I915_READ(PCH_DPLL(i));
  6449. if (!(temp & DPLL_VCO_ENABLE))
  6450. continue;
  6451. if ((temp & PLL_REF_INPUT_MASK) ==
  6452. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6453. using_ssc_source = true;
  6454. break;
  6455. }
  6456. }
  6457. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6458. has_panel, has_lvds, has_ck505, using_ssc_source);
  6459. /* Ironlake: try to setup display ref clock before DPLL
  6460. * enabling. This is only under driver's control after
  6461. * PCH B stepping, previous chipset stepping should be
  6462. * ignoring this setting.
  6463. */
  6464. val = I915_READ(PCH_DREF_CONTROL);
  6465. /* As we must carefully and slowly disable/enable each source in turn,
  6466. * compute the final state we want first and check if we need to
  6467. * make any changes at all.
  6468. */
  6469. final = val;
  6470. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6471. if (has_ck505)
  6472. final |= DREF_NONSPREAD_CK505_ENABLE;
  6473. else
  6474. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6475. final &= ~DREF_SSC_SOURCE_MASK;
  6476. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6477. final &= ~DREF_SSC1_ENABLE;
  6478. if (has_panel) {
  6479. final |= DREF_SSC_SOURCE_ENABLE;
  6480. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6481. final |= DREF_SSC1_ENABLE;
  6482. if (has_cpu_edp) {
  6483. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6484. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6485. else
  6486. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6487. } else
  6488. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6489. } else if (using_ssc_source) {
  6490. final |= DREF_SSC_SOURCE_ENABLE;
  6491. final |= DREF_SSC1_ENABLE;
  6492. }
  6493. if (final == val)
  6494. return;
  6495. /* Always enable nonspread source */
  6496. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6497. if (has_ck505)
  6498. val |= DREF_NONSPREAD_CK505_ENABLE;
  6499. else
  6500. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6501. if (has_panel) {
  6502. val &= ~DREF_SSC_SOURCE_MASK;
  6503. val |= DREF_SSC_SOURCE_ENABLE;
  6504. /* SSC must be turned on before enabling the CPU output */
  6505. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6506. DRM_DEBUG_KMS("Using SSC on panel\n");
  6507. val |= DREF_SSC1_ENABLE;
  6508. } else
  6509. val &= ~DREF_SSC1_ENABLE;
  6510. /* Get SSC going before enabling the outputs */
  6511. I915_WRITE(PCH_DREF_CONTROL, val);
  6512. POSTING_READ(PCH_DREF_CONTROL);
  6513. udelay(200);
  6514. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6515. /* Enable CPU source on CPU attached eDP */
  6516. if (has_cpu_edp) {
  6517. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6518. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6519. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6520. } else
  6521. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6522. } else
  6523. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6524. I915_WRITE(PCH_DREF_CONTROL, val);
  6525. POSTING_READ(PCH_DREF_CONTROL);
  6526. udelay(200);
  6527. } else {
  6528. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6529. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6530. /* Turn off CPU output */
  6531. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6532. I915_WRITE(PCH_DREF_CONTROL, val);
  6533. POSTING_READ(PCH_DREF_CONTROL);
  6534. udelay(200);
  6535. if (!using_ssc_source) {
  6536. DRM_DEBUG_KMS("Disabling SSC source\n");
  6537. /* Turn off the SSC source */
  6538. val &= ~DREF_SSC_SOURCE_MASK;
  6539. val |= DREF_SSC_SOURCE_DISABLE;
  6540. /* Turn off SSC1 */
  6541. val &= ~DREF_SSC1_ENABLE;
  6542. I915_WRITE(PCH_DREF_CONTROL, val);
  6543. POSTING_READ(PCH_DREF_CONTROL);
  6544. udelay(200);
  6545. }
  6546. }
  6547. BUG_ON(val != final);
  6548. }
  6549. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6550. {
  6551. uint32_t tmp;
  6552. tmp = I915_READ(SOUTH_CHICKEN2);
  6553. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6554. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6555. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6556. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6557. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6558. tmp = I915_READ(SOUTH_CHICKEN2);
  6559. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6560. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6561. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6562. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6563. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6564. }
  6565. /* WaMPhyProgramming:hsw */
  6566. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6567. {
  6568. uint32_t tmp;
  6569. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6570. tmp &= ~(0xFF << 24);
  6571. tmp |= (0x12 << 24);
  6572. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6573. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6574. tmp |= (1 << 11);
  6575. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6576. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6577. tmp |= (1 << 11);
  6578. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6579. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6580. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6581. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6582. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6583. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6584. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6585. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6586. tmp &= ~(7 << 13);
  6587. tmp |= (5 << 13);
  6588. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6589. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6590. tmp &= ~(7 << 13);
  6591. tmp |= (5 << 13);
  6592. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6593. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6594. tmp &= ~0xFF;
  6595. tmp |= 0x1C;
  6596. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6597. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6598. tmp &= ~0xFF;
  6599. tmp |= 0x1C;
  6600. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6601. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6602. tmp &= ~(0xFF << 16);
  6603. tmp |= (0x1C << 16);
  6604. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6605. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6606. tmp &= ~(0xFF << 16);
  6607. tmp |= (0x1C << 16);
  6608. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6609. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6610. tmp |= (1 << 27);
  6611. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6612. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6613. tmp |= (1 << 27);
  6614. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6615. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6616. tmp &= ~(0xF << 28);
  6617. tmp |= (4 << 28);
  6618. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6619. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6620. tmp &= ~(0xF << 28);
  6621. tmp |= (4 << 28);
  6622. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6623. }
  6624. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6625. * Programming" based on the parameters passed:
  6626. * - Sequence to enable CLKOUT_DP
  6627. * - Sequence to enable CLKOUT_DP without spread
  6628. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6629. */
  6630. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6631. bool with_spread, bool with_fdi)
  6632. {
  6633. uint32_t reg, tmp;
  6634. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6635. with_spread = true;
  6636. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6637. with_fdi, "LP PCH doesn't have FDI\n"))
  6638. with_fdi = false;
  6639. mutex_lock(&dev_priv->sb_lock);
  6640. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6641. tmp &= ~SBI_SSCCTL_DISABLE;
  6642. tmp |= SBI_SSCCTL_PATHALT;
  6643. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6644. udelay(24);
  6645. if (with_spread) {
  6646. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6647. tmp &= ~SBI_SSCCTL_PATHALT;
  6648. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6649. if (with_fdi) {
  6650. lpt_reset_fdi_mphy(dev_priv);
  6651. lpt_program_fdi_mphy(dev_priv);
  6652. }
  6653. }
  6654. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6655. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6656. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6657. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6658. mutex_unlock(&dev_priv->sb_lock);
  6659. }
  6660. /* Sequence to disable CLKOUT_DP */
  6661. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6662. {
  6663. uint32_t reg, tmp;
  6664. mutex_lock(&dev_priv->sb_lock);
  6665. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6666. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6667. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6668. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6669. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6670. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6671. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6672. tmp |= SBI_SSCCTL_PATHALT;
  6673. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6674. udelay(32);
  6675. }
  6676. tmp |= SBI_SSCCTL_DISABLE;
  6677. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6678. }
  6679. mutex_unlock(&dev_priv->sb_lock);
  6680. }
  6681. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6682. static const uint16_t sscdivintphase[] = {
  6683. [BEND_IDX( 50)] = 0x3B23,
  6684. [BEND_IDX( 45)] = 0x3B23,
  6685. [BEND_IDX( 40)] = 0x3C23,
  6686. [BEND_IDX( 35)] = 0x3C23,
  6687. [BEND_IDX( 30)] = 0x3D23,
  6688. [BEND_IDX( 25)] = 0x3D23,
  6689. [BEND_IDX( 20)] = 0x3E23,
  6690. [BEND_IDX( 15)] = 0x3E23,
  6691. [BEND_IDX( 10)] = 0x3F23,
  6692. [BEND_IDX( 5)] = 0x3F23,
  6693. [BEND_IDX( 0)] = 0x0025,
  6694. [BEND_IDX( -5)] = 0x0025,
  6695. [BEND_IDX(-10)] = 0x0125,
  6696. [BEND_IDX(-15)] = 0x0125,
  6697. [BEND_IDX(-20)] = 0x0225,
  6698. [BEND_IDX(-25)] = 0x0225,
  6699. [BEND_IDX(-30)] = 0x0325,
  6700. [BEND_IDX(-35)] = 0x0325,
  6701. [BEND_IDX(-40)] = 0x0425,
  6702. [BEND_IDX(-45)] = 0x0425,
  6703. [BEND_IDX(-50)] = 0x0525,
  6704. };
  6705. /*
  6706. * Bend CLKOUT_DP
  6707. * steps -50 to 50 inclusive, in steps of 5
  6708. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6709. * change in clock period = -(steps / 10) * 5.787 ps
  6710. */
  6711. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6712. {
  6713. uint32_t tmp;
  6714. int idx = BEND_IDX(steps);
  6715. if (WARN_ON(steps % 5 != 0))
  6716. return;
  6717. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6718. return;
  6719. mutex_lock(&dev_priv->sb_lock);
  6720. if (steps % 10 != 0)
  6721. tmp = 0xAAAAAAAB;
  6722. else
  6723. tmp = 0x00000000;
  6724. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6725. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6726. tmp &= 0xffff0000;
  6727. tmp |= sscdivintphase[idx];
  6728. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6729. mutex_unlock(&dev_priv->sb_lock);
  6730. }
  6731. #undef BEND_IDX
  6732. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6733. {
  6734. struct intel_encoder *encoder;
  6735. bool has_vga = false;
  6736. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6737. switch (encoder->type) {
  6738. case INTEL_OUTPUT_ANALOG:
  6739. has_vga = true;
  6740. break;
  6741. default:
  6742. break;
  6743. }
  6744. }
  6745. if (has_vga) {
  6746. lpt_bend_clkout_dp(dev_priv, 0);
  6747. lpt_enable_clkout_dp(dev_priv, true, true);
  6748. } else {
  6749. lpt_disable_clkout_dp(dev_priv);
  6750. }
  6751. }
  6752. /*
  6753. * Initialize reference clocks when the driver loads
  6754. */
  6755. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6756. {
  6757. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6758. ironlake_init_pch_refclk(dev_priv);
  6759. else if (HAS_PCH_LPT(dev_priv))
  6760. lpt_init_pch_refclk(dev_priv);
  6761. }
  6762. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6763. {
  6764. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6766. int pipe = intel_crtc->pipe;
  6767. uint32_t val;
  6768. val = 0;
  6769. switch (intel_crtc->config->pipe_bpp) {
  6770. case 18:
  6771. val |= PIPECONF_6BPC;
  6772. break;
  6773. case 24:
  6774. val |= PIPECONF_8BPC;
  6775. break;
  6776. case 30:
  6777. val |= PIPECONF_10BPC;
  6778. break;
  6779. case 36:
  6780. val |= PIPECONF_12BPC;
  6781. break;
  6782. default:
  6783. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6784. BUG();
  6785. }
  6786. if (intel_crtc->config->dither)
  6787. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6788. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6789. val |= PIPECONF_INTERLACED_ILK;
  6790. else
  6791. val |= PIPECONF_PROGRESSIVE;
  6792. if (intel_crtc->config->limited_color_range)
  6793. val |= PIPECONF_COLOR_RANGE_SELECT;
  6794. I915_WRITE(PIPECONF(pipe), val);
  6795. POSTING_READ(PIPECONF(pipe));
  6796. }
  6797. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6798. {
  6799. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6801. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6802. u32 val = 0;
  6803. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6804. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6805. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6806. val |= PIPECONF_INTERLACED_ILK;
  6807. else
  6808. val |= PIPECONF_PROGRESSIVE;
  6809. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6810. POSTING_READ(PIPECONF(cpu_transcoder));
  6811. }
  6812. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6813. {
  6814. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6816. struct intel_crtc_state *config = intel_crtc->config;
  6817. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6818. u32 val = 0;
  6819. switch (intel_crtc->config->pipe_bpp) {
  6820. case 18:
  6821. val |= PIPEMISC_DITHER_6_BPC;
  6822. break;
  6823. case 24:
  6824. val |= PIPEMISC_DITHER_8_BPC;
  6825. break;
  6826. case 30:
  6827. val |= PIPEMISC_DITHER_10_BPC;
  6828. break;
  6829. case 36:
  6830. val |= PIPEMISC_DITHER_12_BPC;
  6831. break;
  6832. default:
  6833. /* Case prevented by pipe_config_set_bpp. */
  6834. BUG();
  6835. }
  6836. if (intel_crtc->config->dither)
  6837. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6838. if (config->ycbcr420) {
  6839. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6840. PIPEMISC_YUV420_ENABLE |
  6841. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6842. }
  6843. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6844. }
  6845. }
  6846. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6847. {
  6848. /*
  6849. * Account for spread spectrum to avoid
  6850. * oversubscribing the link. Max center spread
  6851. * is 2.5%; use 5% for safety's sake.
  6852. */
  6853. u32 bps = target_clock * bpp * 21 / 20;
  6854. return DIV_ROUND_UP(bps, link_bw * 8);
  6855. }
  6856. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6857. {
  6858. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6859. }
  6860. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6861. struct intel_crtc_state *crtc_state,
  6862. struct dpll *reduced_clock)
  6863. {
  6864. struct drm_crtc *crtc = &intel_crtc->base;
  6865. struct drm_device *dev = crtc->dev;
  6866. struct drm_i915_private *dev_priv = to_i915(dev);
  6867. u32 dpll, fp, fp2;
  6868. int factor;
  6869. /* Enable autotuning of the PLL clock (if permissible) */
  6870. factor = 21;
  6871. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6872. if ((intel_panel_use_ssc(dev_priv) &&
  6873. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6874. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6875. factor = 25;
  6876. } else if (crtc_state->sdvo_tv_clock)
  6877. factor = 20;
  6878. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6879. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6880. fp |= FP_CB_TUNE;
  6881. if (reduced_clock) {
  6882. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6883. if (reduced_clock->m < factor * reduced_clock->n)
  6884. fp2 |= FP_CB_TUNE;
  6885. } else {
  6886. fp2 = fp;
  6887. }
  6888. dpll = 0;
  6889. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6890. dpll |= DPLLB_MODE_LVDS;
  6891. else
  6892. dpll |= DPLLB_MODE_DAC_SERIAL;
  6893. dpll |= (crtc_state->pixel_multiplier - 1)
  6894. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6895. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6896. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6897. dpll |= DPLL_SDVO_HIGH_SPEED;
  6898. if (intel_crtc_has_dp_encoder(crtc_state))
  6899. dpll |= DPLL_SDVO_HIGH_SPEED;
  6900. /*
  6901. * The high speed IO clock is only really required for
  6902. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6903. * possible to share the DPLL between CRT and HDMI. Enabling
  6904. * the clock needlessly does no real harm, except use up a
  6905. * bit of power potentially.
  6906. *
  6907. * We'll limit this to IVB with 3 pipes, since it has only two
  6908. * DPLLs and so DPLL sharing is the only way to get three pipes
  6909. * driving PCH ports at the same time. On SNB we could do this,
  6910. * and potentially avoid enabling the second DPLL, but it's not
  6911. * clear if it''s a win or loss power wise. No point in doing
  6912. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6913. */
  6914. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6915. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6916. dpll |= DPLL_SDVO_HIGH_SPEED;
  6917. /* compute bitmask from p1 value */
  6918. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6919. /* also FPA1 */
  6920. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6921. switch (crtc_state->dpll.p2) {
  6922. case 5:
  6923. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6924. break;
  6925. case 7:
  6926. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6927. break;
  6928. case 10:
  6929. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6930. break;
  6931. case 14:
  6932. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6933. break;
  6934. }
  6935. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6936. intel_panel_use_ssc(dev_priv))
  6937. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6938. else
  6939. dpll |= PLL_REF_INPUT_DREFCLK;
  6940. dpll |= DPLL_VCO_ENABLE;
  6941. crtc_state->dpll_hw_state.dpll = dpll;
  6942. crtc_state->dpll_hw_state.fp0 = fp;
  6943. crtc_state->dpll_hw_state.fp1 = fp2;
  6944. }
  6945. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6946. struct intel_crtc_state *crtc_state)
  6947. {
  6948. struct drm_device *dev = crtc->base.dev;
  6949. struct drm_i915_private *dev_priv = to_i915(dev);
  6950. const struct intel_limit *limit;
  6951. int refclk = 120000;
  6952. memset(&crtc_state->dpll_hw_state, 0,
  6953. sizeof(crtc_state->dpll_hw_state));
  6954. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6955. if (!crtc_state->has_pch_encoder)
  6956. return 0;
  6957. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6958. if (intel_panel_use_ssc(dev_priv)) {
  6959. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6960. dev_priv->vbt.lvds_ssc_freq);
  6961. refclk = dev_priv->vbt.lvds_ssc_freq;
  6962. }
  6963. if (intel_is_dual_link_lvds(dev)) {
  6964. if (refclk == 100000)
  6965. limit = &intel_limits_ironlake_dual_lvds_100m;
  6966. else
  6967. limit = &intel_limits_ironlake_dual_lvds;
  6968. } else {
  6969. if (refclk == 100000)
  6970. limit = &intel_limits_ironlake_single_lvds_100m;
  6971. else
  6972. limit = &intel_limits_ironlake_single_lvds;
  6973. }
  6974. } else {
  6975. limit = &intel_limits_ironlake_dac;
  6976. }
  6977. if (!crtc_state->clock_set &&
  6978. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6979. refclk, NULL, &crtc_state->dpll)) {
  6980. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6981. return -EINVAL;
  6982. }
  6983. ironlake_compute_dpll(crtc, crtc_state, NULL);
  6984. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  6985. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6986. pipe_name(crtc->pipe));
  6987. return -EINVAL;
  6988. }
  6989. return 0;
  6990. }
  6991. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6992. struct intel_link_m_n *m_n)
  6993. {
  6994. struct drm_device *dev = crtc->base.dev;
  6995. struct drm_i915_private *dev_priv = to_i915(dev);
  6996. enum pipe pipe = crtc->pipe;
  6997. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6998. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6999. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7000. & ~TU_SIZE_MASK;
  7001. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7002. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7003. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7004. }
  7005. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7006. enum transcoder transcoder,
  7007. struct intel_link_m_n *m_n,
  7008. struct intel_link_m_n *m2_n2)
  7009. {
  7010. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7011. enum pipe pipe = crtc->pipe;
  7012. if (INTEL_GEN(dev_priv) >= 5) {
  7013. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7014. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7015. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7016. & ~TU_SIZE_MASK;
  7017. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7018. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7019. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7020. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7021. * gen < 8) and if DRRS is supported (to make sure the
  7022. * registers are not unnecessarily read).
  7023. */
  7024. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7025. crtc->config->has_drrs) {
  7026. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7027. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7028. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7029. & ~TU_SIZE_MASK;
  7030. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7031. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7032. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7033. }
  7034. } else {
  7035. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7036. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7037. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7038. & ~TU_SIZE_MASK;
  7039. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7040. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7041. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7042. }
  7043. }
  7044. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7045. struct intel_crtc_state *pipe_config)
  7046. {
  7047. if (pipe_config->has_pch_encoder)
  7048. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7049. else
  7050. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7051. &pipe_config->dp_m_n,
  7052. &pipe_config->dp_m2_n2);
  7053. }
  7054. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7055. struct intel_crtc_state *pipe_config)
  7056. {
  7057. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7058. &pipe_config->fdi_m_n, NULL);
  7059. }
  7060. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7061. struct intel_crtc_state *pipe_config)
  7062. {
  7063. struct drm_device *dev = crtc->base.dev;
  7064. struct drm_i915_private *dev_priv = to_i915(dev);
  7065. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7066. uint32_t ps_ctrl = 0;
  7067. int id = -1;
  7068. int i;
  7069. /* find scaler attached to this pipe */
  7070. for (i = 0; i < crtc->num_scalers; i++) {
  7071. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7072. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7073. id = i;
  7074. pipe_config->pch_pfit.enabled = true;
  7075. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7076. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7077. break;
  7078. }
  7079. }
  7080. scaler_state->scaler_id = id;
  7081. if (id >= 0) {
  7082. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7083. } else {
  7084. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7085. }
  7086. }
  7087. static void
  7088. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7089. struct intel_initial_plane_config *plane_config)
  7090. {
  7091. struct drm_device *dev = crtc->base.dev;
  7092. struct drm_i915_private *dev_priv = to_i915(dev);
  7093. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7094. enum plane_id plane_id = plane->id;
  7095. enum pipe pipe = crtc->pipe;
  7096. u32 val, base, offset, stride_mult, tiling, alpha;
  7097. int fourcc, pixel_format;
  7098. unsigned int aligned_height;
  7099. struct drm_framebuffer *fb;
  7100. struct intel_framebuffer *intel_fb;
  7101. if (!plane->get_hw_state(plane))
  7102. return;
  7103. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7104. if (!intel_fb) {
  7105. DRM_DEBUG_KMS("failed to alloc fb\n");
  7106. return;
  7107. }
  7108. fb = &intel_fb->base;
  7109. fb->dev = dev;
  7110. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7111. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7112. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7113. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7114. alpha &= PLANE_COLOR_ALPHA_MASK;
  7115. } else {
  7116. alpha = val & PLANE_CTL_ALPHA_MASK;
  7117. }
  7118. fourcc = skl_format_to_fourcc(pixel_format,
  7119. val & PLANE_CTL_ORDER_RGBX, alpha);
  7120. fb->format = drm_format_info(fourcc);
  7121. tiling = val & PLANE_CTL_TILED_MASK;
  7122. switch (tiling) {
  7123. case PLANE_CTL_TILED_LINEAR:
  7124. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7125. break;
  7126. case PLANE_CTL_TILED_X:
  7127. plane_config->tiling = I915_TILING_X;
  7128. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7129. break;
  7130. case PLANE_CTL_TILED_Y:
  7131. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7132. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7133. else
  7134. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7135. break;
  7136. case PLANE_CTL_TILED_YF:
  7137. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7138. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7139. else
  7140. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7141. break;
  7142. default:
  7143. MISSING_CASE(tiling);
  7144. goto error;
  7145. }
  7146. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7147. plane_config->base = base;
  7148. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7149. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7150. fb->height = ((val >> 16) & 0xfff) + 1;
  7151. fb->width = ((val >> 0) & 0x1fff) + 1;
  7152. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7153. stride_mult = intel_fb_stride_alignment(fb, 0);
  7154. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7155. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7156. plane_config->size = fb->pitches[0] * aligned_height;
  7157. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7158. crtc->base.name, plane->base.name, fb->width, fb->height,
  7159. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7160. plane_config->size);
  7161. plane_config->fb = intel_fb;
  7162. return;
  7163. error:
  7164. kfree(intel_fb);
  7165. }
  7166. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7167. struct intel_crtc_state *pipe_config)
  7168. {
  7169. struct drm_device *dev = crtc->base.dev;
  7170. struct drm_i915_private *dev_priv = to_i915(dev);
  7171. uint32_t tmp;
  7172. tmp = I915_READ(PF_CTL(crtc->pipe));
  7173. if (tmp & PF_ENABLE) {
  7174. pipe_config->pch_pfit.enabled = true;
  7175. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7176. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7177. /* We currently do not free assignements of panel fitters on
  7178. * ivb/hsw (since we don't use the higher upscaling modes which
  7179. * differentiates them) so just WARN about this case for now. */
  7180. if (IS_GEN7(dev_priv)) {
  7181. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7182. PF_PIPE_SEL_IVB(crtc->pipe));
  7183. }
  7184. }
  7185. }
  7186. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7187. struct intel_crtc_state *pipe_config)
  7188. {
  7189. struct drm_device *dev = crtc->base.dev;
  7190. struct drm_i915_private *dev_priv = to_i915(dev);
  7191. enum intel_display_power_domain power_domain;
  7192. uint32_t tmp;
  7193. bool ret;
  7194. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7195. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7196. return false;
  7197. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7198. pipe_config->shared_dpll = NULL;
  7199. ret = false;
  7200. tmp = I915_READ(PIPECONF(crtc->pipe));
  7201. if (!(tmp & PIPECONF_ENABLE))
  7202. goto out;
  7203. switch (tmp & PIPECONF_BPC_MASK) {
  7204. case PIPECONF_6BPC:
  7205. pipe_config->pipe_bpp = 18;
  7206. break;
  7207. case PIPECONF_8BPC:
  7208. pipe_config->pipe_bpp = 24;
  7209. break;
  7210. case PIPECONF_10BPC:
  7211. pipe_config->pipe_bpp = 30;
  7212. break;
  7213. case PIPECONF_12BPC:
  7214. pipe_config->pipe_bpp = 36;
  7215. break;
  7216. default:
  7217. break;
  7218. }
  7219. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7220. pipe_config->limited_color_range = true;
  7221. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7222. struct intel_shared_dpll *pll;
  7223. enum intel_dpll_id pll_id;
  7224. pipe_config->has_pch_encoder = true;
  7225. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7226. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7227. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7228. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7229. if (HAS_PCH_IBX(dev_priv)) {
  7230. /*
  7231. * The pipe->pch transcoder and pch transcoder->pll
  7232. * mapping is fixed.
  7233. */
  7234. pll_id = (enum intel_dpll_id) crtc->pipe;
  7235. } else {
  7236. tmp = I915_READ(PCH_DPLL_SEL);
  7237. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7238. pll_id = DPLL_ID_PCH_PLL_B;
  7239. else
  7240. pll_id= DPLL_ID_PCH_PLL_A;
  7241. }
  7242. pipe_config->shared_dpll =
  7243. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7244. pll = pipe_config->shared_dpll;
  7245. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7246. &pipe_config->dpll_hw_state));
  7247. tmp = pipe_config->dpll_hw_state.dpll;
  7248. pipe_config->pixel_multiplier =
  7249. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7250. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7251. ironlake_pch_clock_get(crtc, pipe_config);
  7252. } else {
  7253. pipe_config->pixel_multiplier = 1;
  7254. }
  7255. intel_get_pipe_timings(crtc, pipe_config);
  7256. intel_get_pipe_src_size(crtc, pipe_config);
  7257. ironlake_get_pfit_config(crtc, pipe_config);
  7258. ret = true;
  7259. out:
  7260. intel_display_power_put(dev_priv, power_domain);
  7261. return ret;
  7262. }
  7263. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7264. {
  7265. struct drm_device *dev = &dev_priv->drm;
  7266. struct intel_crtc *crtc;
  7267. for_each_intel_crtc(dev, crtc)
  7268. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7269. pipe_name(crtc->pipe));
  7270. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7271. "Display power well on\n");
  7272. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7273. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7274. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7275. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7276. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7277. "CPU PWM1 enabled\n");
  7278. if (IS_HASWELL(dev_priv))
  7279. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7280. "CPU PWM2 enabled\n");
  7281. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7282. "PCH PWM1 enabled\n");
  7283. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7284. "Utility pin enabled\n");
  7285. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7286. /*
  7287. * In theory we can still leave IRQs enabled, as long as only the HPD
  7288. * interrupts remain enabled. We used to check for that, but since it's
  7289. * gen-specific and since we only disable LCPLL after we fully disable
  7290. * the interrupts, the check below should be enough.
  7291. */
  7292. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7293. }
  7294. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7295. {
  7296. if (IS_HASWELL(dev_priv))
  7297. return I915_READ(D_COMP_HSW);
  7298. else
  7299. return I915_READ(D_COMP_BDW);
  7300. }
  7301. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7302. {
  7303. if (IS_HASWELL(dev_priv)) {
  7304. mutex_lock(&dev_priv->pcu_lock);
  7305. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7306. val))
  7307. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7308. mutex_unlock(&dev_priv->pcu_lock);
  7309. } else {
  7310. I915_WRITE(D_COMP_BDW, val);
  7311. POSTING_READ(D_COMP_BDW);
  7312. }
  7313. }
  7314. /*
  7315. * This function implements pieces of two sequences from BSpec:
  7316. * - Sequence for display software to disable LCPLL
  7317. * - Sequence for display software to allow package C8+
  7318. * The steps implemented here are just the steps that actually touch the LCPLL
  7319. * register. Callers should take care of disabling all the display engine
  7320. * functions, doing the mode unset, fixing interrupts, etc.
  7321. */
  7322. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7323. bool switch_to_fclk, bool allow_power_down)
  7324. {
  7325. uint32_t val;
  7326. assert_can_disable_lcpll(dev_priv);
  7327. val = I915_READ(LCPLL_CTL);
  7328. if (switch_to_fclk) {
  7329. val |= LCPLL_CD_SOURCE_FCLK;
  7330. I915_WRITE(LCPLL_CTL, val);
  7331. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7332. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7333. DRM_ERROR("Switching to FCLK failed\n");
  7334. val = I915_READ(LCPLL_CTL);
  7335. }
  7336. val |= LCPLL_PLL_DISABLE;
  7337. I915_WRITE(LCPLL_CTL, val);
  7338. POSTING_READ(LCPLL_CTL);
  7339. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7340. DRM_ERROR("LCPLL still locked\n");
  7341. val = hsw_read_dcomp(dev_priv);
  7342. val |= D_COMP_COMP_DISABLE;
  7343. hsw_write_dcomp(dev_priv, val);
  7344. ndelay(100);
  7345. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7346. 1))
  7347. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7348. if (allow_power_down) {
  7349. val = I915_READ(LCPLL_CTL);
  7350. val |= LCPLL_POWER_DOWN_ALLOW;
  7351. I915_WRITE(LCPLL_CTL, val);
  7352. POSTING_READ(LCPLL_CTL);
  7353. }
  7354. }
  7355. /*
  7356. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7357. * source.
  7358. */
  7359. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7360. {
  7361. uint32_t val;
  7362. val = I915_READ(LCPLL_CTL);
  7363. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7364. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7365. return;
  7366. /*
  7367. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7368. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7369. */
  7370. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7371. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7372. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7373. I915_WRITE(LCPLL_CTL, val);
  7374. POSTING_READ(LCPLL_CTL);
  7375. }
  7376. val = hsw_read_dcomp(dev_priv);
  7377. val |= D_COMP_COMP_FORCE;
  7378. val &= ~D_COMP_COMP_DISABLE;
  7379. hsw_write_dcomp(dev_priv, val);
  7380. val = I915_READ(LCPLL_CTL);
  7381. val &= ~LCPLL_PLL_DISABLE;
  7382. I915_WRITE(LCPLL_CTL, val);
  7383. if (intel_wait_for_register(dev_priv,
  7384. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7385. 5))
  7386. DRM_ERROR("LCPLL not locked yet\n");
  7387. if (val & LCPLL_CD_SOURCE_FCLK) {
  7388. val = I915_READ(LCPLL_CTL);
  7389. val &= ~LCPLL_CD_SOURCE_FCLK;
  7390. I915_WRITE(LCPLL_CTL, val);
  7391. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7392. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7393. DRM_ERROR("Switching back to LCPLL failed\n");
  7394. }
  7395. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7396. intel_update_cdclk(dev_priv);
  7397. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7398. }
  7399. /*
  7400. * Package states C8 and deeper are really deep PC states that can only be
  7401. * reached when all the devices on the system allow it, so even if the graphics
  7402. * device allows PC8+, it doesn't mean the system will actually get to these
  7403. * states. Our driver only allows PC8+ when going into runtime PM.
  7404. *
  7405. * The requirements for PC8+ are that all the outputs are disabled, the power
  7406. * well is disabled and most interrupts are disabled, and these are also
  7407. * requirements for runtime PM. When these conditions are met, we manually do
  7408. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7409. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7410. * hang the machine.
  7411. *
  7412. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7413. * the state of some registers, so when we come back from PC8+ we need to
  7414. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7415. * need to take care of the registers kept by RC6. Notice that this happens even
  7416. * if we don't put the device in PCI D3 state (which is what currently happens
  7417. * because of the runtime PM support).
  7418. *
  7419. * For more, read "Display Sequences for Package C8" on the hardware
  7420. * documentation.
  7421. */
  7422. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7423. {
  7424. uint32_t val;
  7425. DRM_DEBUG_KMS("Enabling package C8+\n");
  7426. if (HAS_PCH_LPT_LP(dev_priv)) {
  7427. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7428. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7429. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7430. }
  7431. lpt_disable_clkout_dp(dev_priv);
  7432. hsw_disable_lcpll(dev_priv, true, true);
  7433. }
  7434. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7435. {
  7436. uint32_t val;
  7437. DRM_DEBUG_KMS("Disabling package C8+\n");
  7438. hsw_restore_lcpll(dev_priv);
  7439. lpt_init_pch_refclk(dev_priv);
  7440. if (HAS_PCH_LPT_LP(dev_priv)) {
  7441. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7442. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7443. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7444. }
  7445. }
  7446. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7447. struct intel_crtc_state *crtc_state)
  7448. {
  7449. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7450. struct intel_encoder *encoder =
  7451. intel_ddi_get_crtc_new_encoder(crtc_state);
  7452. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7453. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7454. pipe_name(crtc->pipe));
  7455. return -EINVAL;
  7456. }
  7457. }
  7458. return 0;
  7459. }
  7460. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7461. enum port port,
  7462. struct intel_crtc_state *pipe_config)
  7463. {
  7464. enum intel_dpll_id id;
  7465. u32 temp;
  7466. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7467. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7468. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7469. return;
  7470. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7471. }
  7472. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7473. enum port port,
  7474. struct intel_crtc_state *pipe_config)
  7475. {
  7476. enum intel_dpll_id id;
  7477. switch (port) {
  7478. case PORT_A:
  7479. id = DPLL_ID_SKL_DPLL0;
  7480. break;
  7481. case PORT_B:
  7482. id = DPLL_ID_SKL_DPLL1;
  7483. break;
  7484. case PORT_C:
  7485. id = DPLL_ID_SKL_DPLL2;
  7486. break;
  7487. default:
  7488. DRM_ERROR("Incorrect port type\n");
  7489. return;
  7490. }
  7491. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7492. }
  7493. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7494. enum port port,
  7495. struct intel_crtc_state *pipe_config)
  7496. {
  7497. enum intel_dpll_id id;
  7498. u32 temp;
  7499. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7500. id = temp >> (port * 3 + 1);
  7501. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7502. return;
  7503. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7504. }
  7505. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7506. enum port port,
  7507. struct intel_crtc_state *pipe_config)
  7508. {
  7509. enum intel_dpll_id id;
  7510. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7511. switch (ddi_pll_sel) {
  7512. case PORT_CLK_SEL_WRPLL1:
  7513. id = DPLL_ID_WRPLL1;
  7514. break;
  7515. case PORT_CLK_SEL_WRPLL2:
  7516. id = DPLL_ID_WRPLL2;
  7517. break;
  7518. case PORT_CLK_SEL_SPLL:
  7519. id = DPLL_ID_SPLL;
  7520. break;
  7521. case PORT_CLK_SEL_LCPLL_810:
  7522. id = DPLL_ID_LCPLL_810;
  7523. break;
  7524. case PORT_CLK_SEL_LCPLL_1350:
  7525. id = DPLL_ID_LCPLL_1350;
  7526. break;
  7527. case PORT_CLK_SEL_LCPLL_2700:
  7528. id = DPLL_ID_LCPLL_2700;
  7529. break;
  7530. default:
  7531. MISSING_CASE(ddi_pll_sel);
  7532. /* fall through */
  7533. case PORT_CLK_SEL_NONE:
  7534. return;
  7535. }
  7536. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7537. }
  7538. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7539. struct intel_crtc_state *pipe_config,
  7540. u64 *power_domain_mask)
  7541. {
  7542. struct drm_device *dev = crtc->base.dev;
  7543. struct drm_i915_private *dev_priv = to_i915(dev);
  7544. enum intel_display_power_domain power_domain;
  7545. u32 tmp;
  7546. /*
  7547. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7548. * transcoder handled below.
  7549. */
  7550. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7551. /*
  7552. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7553. * consistency and less surprising code; it's in always on power).
  7554. */
  7555. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7556. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7557. enum pipe trans_edp_pipe;
  7558. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7559. default:
  7560. WARN(1, "unknown pipe linked to edp transcoder\n");
  7561. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7562. case TRANS_DDI_EDP_INPUT_A_ON:
  7563. trans_edp_pipe = PIPE_A;
  7564. break;
  7565. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7566. trans_edp_pipe = PIPE_B;
  7567. break;
  7568. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7569. trans_edp_pipe = PIPE_C;
  7570. break;
  7571. }
  7572. if (trans_edp_pipe == crtc->pipe)
  7573. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7574. }
  7575. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7576. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7577. return false;
  7578. *power_domain_mask |= BIT_ULL(power_domain);
  7579. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7580. return tmp & PIPECONF_ENABLE;
  7581. }
  7582. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7583. struct intel_crtc_state *pipe_config,
  7584. u64 *power_domain_mask)
  7585. {
  7586. struct drm_device *dev = crtc->base.dev;
  7587. struct drm_i915_private *dev_priv = to_i915(dev);
  7588. enum intel_display_power_domain power_domain;
  7589. enum port port;
  7590. enum transcoder cpu_transcoder;
  7591. u32 tmp;
  7592. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7593. if (port == PORT_A)
  7594. cpu_transcoder = TRANSCODER_DSI_A;
  7595. else
  7596. cpu_transcoder = TRANSCODER_DSI_C;
  7597. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7598. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7599. continue;
  7600. *power_domain_mask |= BIT_ULL(power_domain);
  7601. /*
  7602. * The PLL needs to be enabled with a valid divider
  7603. * configuration, otherwise accessing DSI registers will hang
  7604. * the machine. See BSpec North Display Engine
  7605. * registers/MIPI[BXT]. We can break out here early, since we
  7606. * need the same DSI PLL to be enabled for both DSI ports.
  7607. */
  7608. if (!intel_dsi_pll_is_enabled(dev_priv))
  7609. break;
  7610. /* XXX: this works for video mode only */
  7611. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7612. if (!(tmp & DPI_ENABLE))
  7613. continue;
  7614. tmp = I915_READ(MIPI_CTRL(port));
  7615. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7616. continue;
  7617. pipe_config->cpu_transcoder = cpu_transcoder;
  7618. break;
  7619. }
  7620. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7621. }
  7622. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7623. struct intel_crtc_state *pipe_config)
  7624. {
  7625. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7626. struct intel_shared_dpll *pll;
  7627. enum port port;
  7628. uint32_t tmp;
  7629. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7630. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7631. if (IS_CANNONLAKE(dev_priv))
  7632. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7633. else if (IS_GEN9_BC(dev_priv))
  7634. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7635. else if (IS_GEN9_LP(dev_priv))
  7636. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7637. else
  7638. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7639. pll = pipe_config->shared_dpll;
  7640. if (pll) {
  7641. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7642. &pipe_config->dpll_hw_state));
  7643. }
  7644. /*
  7645. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7646. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7647. * the PCH transcoder is on.
  7648. */
  7649. if (INTEL_GEN(dev_priv) < 9 &&
  7650. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7651. pipe_config->has_pch_encoder = true;
  7652. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7653. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7654. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7655. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7656. }
  7657. }
  7658. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7659. struct intel_crtc_state *pipe_config)
  7660. {
  7661. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7662. enum intel_display_power_domain power_domain;
  7663. u64 power_domain_mask;
  7664. bool active;
  7665. intel_crtc_init_scalers(crtc, pipe_config);
  7666. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7667. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7668. return false;
  7669. power_domain_mask = BIT_ULL(power_domain);
  7670. pipe_config->shared_dpll = NULL;
  7671. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7672. if (IS_GEN9_LP(dev_priv) &&
  7673. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7674. WARN_ON(active);
  7675. active = true;
  7676. }
  7677. if (!active)
  7678. goto out;
  7679. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7680. haswell_get_ddi_port_state(crtc, pipe_config);
  7681. intel_get_pipe_timings(crtc, pipe_config);
  7682. }
  7683. intel_get_pipe_src_size(crtc, pipe_config);
  7684. pipe_config->gamma_mode =
  7685. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7686. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7687. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7688. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7689. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7690. bool blend_mode_420 = tmp &
  7691. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7692. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7693. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7694. pipe_config->ycbcr420 != blend_mode_420)
  7695. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7696. } else if (clrspace_yuv) {
  7697. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7698. }
  7699. }
  7700. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7701. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7702. power_domain_mask |= BIT_ULL(power_domain);
  7703. if (INTEL_GEN(dev_priv) >= 9)
  7704. skylake_get_pfit_config(crtc, pipe_config);
  7705. else
  7706. ironlake_get_pfit_config(crtc, pipe_config);
  7707. }
  7708. if (hsw_crtc_supports_ips(crtc)) {
  7709. if (IS_HASWELL(dev_priv))
  7710. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7711. else {
  7712. /*
  7713. * We cannot readout IPS state on broadwell, set to
  7714. * true so we can set it to a defined state on first
  7715. * commit.
  7716. */
  7717. pipe_config->ips_enabled = true;
  7718. }
  7719. }
  7720. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7721. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7722. pipe_config->pixel_multiplier =
  7723. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7724. } else {
  7725. pipe_config->pixel_multiplier = 1;
  7726. }
  7727. out:
  7728. for_each_power_domain(power_domain, power_domain_mask)
  7729. intel_display_power_put(dev_priv, power_domain);
  7730. return active;
  7731. }
  7732. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7733. {
  7734. struct drm_i915_private *dev_priv =
  7735. to_i915(plane_state->base.plane->dev);
  7736. const struct drm_framebuffer *fb = plane_state->base.fb;
  7737. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7738. u32 base;
  7739. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7740. base = obj->phys_handle->busaddr;
  7741. else
  7742. base = intel_plane_ggtt_offset(plane_state);
  7743. base += plane_state->main.offset;
  7744. /* ILK+ do this automagically */
  7745. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7746. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7747. base += (plane_state->base.crtc_h *
  7748. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7749. return base;
  7750. }
  7751. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7752. {
  7753. int x = plane_state->base.crtc_x;
  7754. int y = plane_state->base.crtc_y;
  7755. u32 pos = 0;
  7756. if (x < 0) {
  7757. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7758. x = -x;
  7759. }
  7760. pos |= x << CURSOR_X_SHIFT;
  7761. if (y < 0) {
  7762. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7763. y = -y;
  7764. }
  7765. pos |= y << CURSOR_Y_SHIFT;
  7766. return pos;
  7767. }
  7768. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7769. {
  7770. const struct drm_mode_config *config =
  7771. &plane_state->base.plane->dev->mode_config;
  7772. int width = plane_state->base.crtc_w;
  7773. int height = plane_state->base.crtc_h;
  7774. return width > 0 && width <= config->cursor_width &&
  7775. height > 0 && height <= config->cursor_height;
  7776. }
  7777. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7778. struct intel_plane_state *plane_state)
  7779. {
  7780. const struct drm_framebuffer *fb = plane_state->base.fb;
  7781. int src_x, src_y;
  7782. u32 offset;
  7783. int ret;
  7784. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  7785. &crtc_state->base,
  7786. &plane_state->clip,
  7787. DRM_PLANE_HELPER_NO_SCALING,
  7788. DRM_PLANE_HELPER_NO_SCALING,
  7789. true, true);
  7790. if (ret)
  7791. return ret;
  7792. if (!fb)
  7793. return 0;
  7794. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7795. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7796. return -EINVAL;
  7797. }
  7798. src_x = plane_state->base.src_x >> 16;
  7799. src_y = plane_state->base.src_y >> 16;
  7800. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7801. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7802. if (src_x != 0 || src_y != 0) {
  7803. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7804. return -EINVAL;
  7805. }
  7806. plane_state->main.offset = offset;
  7807. return 0;
  7808. }
  7809. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7810. const struct intel_plane_state *plane_state)
  7811. {
  7812. const struct drm_framebuffer *fb = plane_state->base.fb;
  7813. return CURSOR_ENABLE |
  7814. CURSOR_GAMMA_ENABLE |
  7815. CURSOR_FORMAT_ARGB |
  7816. CURSOR_STRIDE(fb->pitches[0]);
  7817. }
  7818. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7819. {
  7820. int width = plane_state->base.crtc_w;
  7821. /*
  7822. * 845g/865g are only limited by the width of their cursors,
  7823. * the height is arbitrary up to the precision of the register.
  7824. */
  7825. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7826. }
  7827. static int i845_check_cursor(struct intel_plane *plane,
  7828. struct intel_crtc_state *crtc_state,
  7829. struct intel_plane_state *plane_state)
  7830. {
  7831. const struct drm_framebuffer *fb = plane_state->base.fb;
  7832. int ret;
  7833. ret = intel_check_cursor(crtc_state, plane_state);
  7834. if (ret)
  7835. return ret;
  7836. /* if we want to turn off the cursor ignore width and height */
  7837. if (!fb)
  7838. return 0;
  7839. /* Check for which cursor types we support */
  7840. if (!i845_cursor_size_ok(plane_state)) {
  7841. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7842. plane_state->base.crtc_w,
  7843. plane_state->base.crtc_h);
  7844. return -EINVAL;
  7845. }
  7846. switch (fb->pitches[0]) {
  7847. case 256:
  7848. case 512:
  7849. case 1024:
  7850. case 2048:
  7851. break;
  7852. default:
  7853. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7854. fb->pitches[0]);
  7855. return -EINVAL;
  7856. }
  7857. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7858. return 0;
  7859. }
  7860. static void i845_update_cursor(struct intel_plane *plane,
  7861. const struct intel_crtc_state *crtc_state,
  7862. const struct intel_plane_state *plane_state)
  7863. {
  7864. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7865. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7866. unsigned long irqflags;
  7867. if (plane_state && plane_state->base.visible) {
  7868. unsigned int width = plane_state->base.crtc_w;
  7869. unsigned int height = plane_state->base.crtc_h;
  7870. cntl = plane_state->ctl;
  7871. size = (height << 12) | width;
  7872. base = intel_cursor_base(plane_state);
  7873. pos = intel_cursor_position(plane_state);
  7874. }
  7875. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7876. /* On these chipsets we can only modify the base/size/stride
  7877. * whilst the cursor is disabled.
  7878. */
  7879. if (plane->cursor.base != base ||
  7880. plane->cursor.size != size ||
  7881. plane->cursor.cntl != cntl) {
  7882. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7883. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7884. I915_WRITE_FW(CURSIZE, size);
  7885. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7886. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7887. plane->cursor.base = base;
  7888. plane->cursor.size = size;
  7889. plane->cursor.cntl = cntl;
  7890. } else {
  7891. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7892. }
  7893. POSTING_READ_FW(CURCNTR(PIPE_A));
  7894. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7895. }
  7896. static void i845_disable_cursor(struct intel_plane *plane,
  7897. struct intel_crtc *crtc)
  7898. {
  7899. i845_update_cursor(plane, NULL, NULL);
  7900. }
  7901. static bool i845_cursor_get_hw_state(struct intel_plane *plane)
  7902. {
  7903. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7904. enum intel_display_power_domain power_domain;
  7905. bool ret;
  7906. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  7907. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7908. return false;
  7909. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  7910. intel_display_power_put(dev_priv, power_domain);
  7911. return ret;
  7912. }
  7913. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7914. const struct intel_plane_state *plane_state)
  7915. {
  7916. struct drm_i915_private *dev_priv =
  7917. to_i915(plane_state->base.plane->dev);
  7918. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7919. u32 cntl;
  7920. cntl = MCURSOR_GAMMA_ENABLE;
  7921. if (HAS_DDI(dev_priv))
  7922. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7923. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7924. switch (plane_state->base.crtc_w) {
  7925. case 64:
  7926. cntl |= CURSOR_MODE_64_ARGB_AX;
  7927. break;
  7928. case 128:
  7929. cntl |= CURSOR_MODE_128_ARGB_AX;
  7930. break;
  7931. case 256:
  7932. cntl |= CURSOR_MODE_256_ARGB_AX;
  7933. break;
  7934. default:
  7935. MISSING_CASE(plane_state->base.crtc_w);
  7936. return 0;
  7937. }
  7938. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7939. cntl |= CURSOR_ROTATE_180;
  7940. return cntl;
  7941. }
  7942. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  7943. {
  7944. struct drm_i915_private *dev_priv =
  7945. to_i915(plane_state->base.plane->dev);
  7946. int width = plane_state->base.crtc_w;
  7947. int height = plane_state->base.crtc_h;
  7948. if (!intel_cursor_size_ok(plane_state))
  7949. return false;
  7950. /* Cursor width is limited to a few power-of-two sizes */
  7951. switch (width) {
  7952. case 256:
  7953. case 128:
  7954. case 64:
  7955. break;
  7956. default:
  7957. return false;
  7958. }
  7959. /*
  7960. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  7961. * height from 8 lines up to the cursor width, when the
  7962. * cursor is not rotated. Everything else requires square
  7963. * cursors.
  7964. */
  7965. if (HAS_CUR_FBC(dev_priv) &&
  7966. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  7967. if (height < 8 || height > width)
  7968. return false;
  7969. } else {
  7970. if (height != width)
  7971. return false;
  7972. }
  7973. return true;
  7974. }
  7975. static int i9xx_check_cursor(struct intel_plane *plane,
  7976. struct intel_crtc_state *crtc_state,
  7977. struct intel_plane_state *plane_state)
  7978. {
  7979. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7980. const struct drm_framebuffer *fb = plane_state->base.fb;
  7981. enum pipe pipe = plane->pipe;
  7982. int ret;
  7983. ret = intel_check_cursor(crtc_state, plane_state);
  7984. if (ret)
  7985. return ret;
  7986. /* if we want to turn off the cursor ignore width and height */
  7987. if (!fb)
  7988. return 0;
  7989. /* Check for which cursor types we support */
  7990. if (!i9xx_cursor_size_ok(plane_state)) {
  7991. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7992. plane_state->base.crtc_w,
  7993. plane_state->base.crtc_h);
  7994. return -EINVAL;
  7995. }
  7996. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  7997. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  7998. fb->pitches[0], plane_state->base.crtc_w);
  7999. return -EINVAL;
  8000. }
  8001. /*
  8002. * There's something wrong with the cursor on CHV pipe C.
  8003. * If it straddles the left edge of the screen then
  8004. * moving it away from the edge or disabling it often
  8005. * results in a pipe underrun, and often that can lead to
  8006. * dead pipe (constant underrun reported, and it scans
  8007. * out just a solid color). To recover from that, the
  8008. * display power well must be turned off and on again.
  8009. * Refuse the put the cursor into that compromised position.
  8010. */
  8011. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8012. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8013. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8014. return -EINVAL;
  8015. }
  8016. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8017. return 0;
  8018. }
  8019. static void i9xx_update_cursor(struct intel_plane *plane,
  8020. const struct intel_crtc_state *crtc_state,
  8021. const struct intel_plane_state *plane_state)
  8022. {
  8023. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8024. enum pipe pipe = plane->pipe;
  8025. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8026. unsigned long irqflags;
  8027. if (plane_state && plane_state->base.visible) {
  8028. cntl = plane_state->ctl;
  8029. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8030. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8031. base = intel_cursor_base(plane_state);
  8032. pos = intel_cursor_position(plane_state);
  8033. }
  8034. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8035. /*
  8036. * On some platforms writing CURCNTR first will also
  8037. * cause CURPOS to be armed by the CURBASE write.
  8038. * Without the CURCNTR write the CURPOS write would
  8039. * arm itself. Thus we always start the full update
  8040. * with a CURCNTR write.
  8041. *
  8042. * On other platforms CURPOS always requires the
  8043. * CURBASE write to arm the update. Additonally
  8044. * a write to any of the cursor register will cancel
  8045. * an already armed cursor update. Thus leaving out
  8046. * the CURBASE write after CURPOS could lead to a
  8047. * cursor that doesn't appear to move, or even change
  8048. * shape. Thus we always write CURBASE.
  8049. *
  8050. * CURCNTR and CUR_FBC_CTL are always
  8051. * armed by the CURBASE write only.
  8052. */
  8053. if (plane->cursor.base != base ||
  8054. plane->cursor.size != fbc_ctl ||
  8055. plane->cursor.cntl != cntl) {
  8056. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8057. if (HAS_CUR_FBC(dev_priv))
  8058. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8059. I915_WRITE_FW(CURPOS(pipe), pos);
  8060. I915_WRITE_FW(CURBASE(pipe), base);
  8061. plane->cursor.base = base;
  8062. plane->cursor.size = fbc_ctl;
  8063. plane->cursor.cntl = cntl;
  8064. } else {
  8065. I915_WRITE_FW(CURPOS(pipe), pos);
  8066. I915_WRITE_FW(CURBASE(pipe), base);
  8067. }
  8068. POSTING_READ_FW(CURBASE(pipe));
  8069. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8070. }
  8071. static void i9xx_disable_cursor(struct intel_plane *plane,
  8072. struct intel_crtc *crtc)
  8073. {
  8074. i9xx_update_cursor(plane, NULL, NULL);
  8075. }
  8076. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane)
  8077. {
  8078. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8079. enum intel_display_power_domain power_domain;
  8080. enum pipe pipe = plane->pipe;
  8081. bool ret;
  8082. /*
  8083. * Not 100% correct for planes that can move between pipes,
  8084. * but that's only the case for gen2-3 which don't have any
  8085. * display power wells.
  8086. */
  8087. power_domain = POWER_DOMAIN_PIPE(pipe);
  8088. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8089. return false;
  8090. ret = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  8091. intel_display_power_put(dev_priv, power_domain);
  8092. return ret;
  8093. }
  8094. /* VESA 640x480x72Hz mode to set on the pipe */
  8095. static const struct drm_display_mode load_detect_mode = {
  8096. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8097. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8098. };
  8099. struct drm_framebuffer *
  8100. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8101. struct drm_mode_fb_cmd2 *mode_cmd)
  8102. {
  8103. struct intel_framebuffer *intel_fb;
  8104. int ret;
  8105. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8106. if (!intel_fb)
  8107. return ERR_PTR(-ENOMEM);
  8108. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8109. if (ret)
  8110. goto err;
  8111. return &intel_fb->base;
  8112. err:
  8113. kfree(intel_fb);
  8114. return ERR_PTR(ret);
  8115. }
  8116. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8117. struct drm_crtc *crtc)
  8118. {
  8119. struct drm_plane *plane;
  8120. struct drm_plane_state *plane_state;
  8121. int ret, i;
  8122. ret = drm_atomic_add_affected_planes(state, crtc);
  8123. if (ret)
  8124. return ret;
  8125. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8126. if (plane_state->crtc != crtc)
  8127. continue;
  8128. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8129. if (ret)
  8130. return ret;
  8131. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8132. }
  8133. return 0;
  8134. }
  8135. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8136. const struct drm_display_mode *mode,
  8137. struct intel_load_detect_pipe *old,
  8138. struct drm_modeset_acquire_ctx *ctx)
  8139. {
  8140. struct intel_crtc *intel_crtc;
  8141. struct intel_encoder *intel_encoder =
  8142. intel_attached_encoder(connector);
  8143. struct drm_crtc *possible_crtc;
  8144. struct drm_encoder *encoder = &intel_encoder->base;
  8145. struct drm_crtc *crtc = NULL;
  8146. struct drm_device *dev = encoder->dev;
  8147. struct drm_i915_private *dev_priv = to_i915(dev);
  8148. struct drm_mode_config *config = &dev->mode_config;
  8149. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8150. struct drm_connector_state *connector_state;
  8151. struct intel_crtc_state *crtc_state;
  8152. int ret, i = -1;
  8153. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8154. connector->base.id, connector->name,
  8155. encoder->base.id, encoder->name);
  8156. old->restore_state = NULL;
  8157. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8158. /*
  8159. * Algorithm gets a little messy:
  8160. *
  8161. * - if the connector already has an assigned crtc, use it (but make
  8162. * sure it's on first)
  8163. *
  8164. * - try to find the first unused crtc that can drive this connector,
  8165. * and use that if we find one
  8166. */
  8167. /* See if we already have a CRTC for this connector */
  8168. if (connector->state->crtc) {
  8169. crtc = connector->state->crtc;
  8170. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8171. if (ret)
  8172. goto fail;
  8173. /* Make sure the crtc and connector are running */
  8174. goto found;
  8175. }
  8176. /* Find an unused one (if possible) */
  8177. for_each_crtc(dev, possible_crtc) {
  8178. i++;
  8179. if (!(encoder->possible_crtcs & (1 << i)))
  8180. continue;
  8181. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8182. if (ret)
  8183. goto fail;
  8184. if (possible_crtc->state->enable) {
  8185. drm_modeset_unlock(&possible_crtc->mutex);
  8186. continue;
  8187. }
  8188. crtc = possible_crtc;
  8189. break;
  8190. }
  8191. /*
  8192. * If we didn't find an unused CRTC, don't use any.
  8193. */
  8194. if (!crtc) {
  8195. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8196. ret = -ENODEV;
  8197. goto fail;
  8198. }
  8199. found:
  8200. intel_crtc = to_intel_crtc(crtc);
  8201. state = drm_atomic_state_alloc(dev);
  8202. restore_state = drm_atomic_state_alloc(dev);
  8203. if (!state || !restore_state) {
  8204. ret = -ENOMEM;
  8205. goto fail;
  8206. }
  8207. state->acquire_ctx = ctx;
  8208. restore_state->acquire_ctx = ctx;
  8209. connector_state = drm_atomic_get_connector_state(state, connector);
  8210. if (IS_ERR(connector_state)) {
  8211. ret = PTR_ERR(connector_state);
  8212. goto fail;
  8213. }
  8214. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8215. if (ret)
  8216. goto fail;
  8217. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8218. if (IS_ERR(crtc_state)) {
  8219. ret = PTR_ERR(crtc_state);
  8220. goto fail;
  8221. }
  8222. crtc_state->base.active = crtc_state->base.enable = true;
  8223. if (!mode)
  8224. mode = &load_detect_mode;
  8225. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8226. if (ret)
  8227. goto fail;
  8228. ret = intel_modeset_disable_planes(state, crtc);
  8229. if (ret)
  8230. goto fail;
  8231. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8232. if (!ret)
  8233. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8234. if (ret) {
  8235. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8236. goto fail;
  8237. }
  8238. ret = drm_atomic_commit(state);
  8239. if (ret) {
  8240. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8241. goto fail;
  8242. }
  8243. old->restore_state = restore_state;
  8244. drm_atomic_state_put(state);
  8245. /* let the connector get through one full cycle before testing */
  8246. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8247. return true;
  8248. fail:
  8249. if (state) {
  8250. drm_atomic_state_put(state);
  8251. state = NULL;
  8252. }
  8253. if (restore_state) {
  8254. drm_atomic_state_put(restore_state);
  8255. restore_state = NULL;
  8256. }
  8257. if (ret == -EDEADLK)
  8258. return ret;
  8259. return false;
  8260. }
  8261. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8262. struct intel_load_detect_pipe *old,
  8263. struct drm_modeset_acquire_ctx *ctx)
  8264. {
  8265. struct intel_encoder *intel_encoder =
  8266. intel_attached_encoder(connector);
  8267. struct drm_encoder *encoder = &intel_encoder->base;
  8268. struct drm_atomic_state *state = old->restore_state;
  8269. int ret;
  8270. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8271. connector->base.id, connector->name,
  8272. encoder->base.id, encoder->name);
  8273. if (!state)
  8274. return;
  8275. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8276. if (ret)
  8277. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8278. drm_atomic_state_put(state);
  8279. }
  8280. static int i9xx_pll_refclk(struct drm_device *dev,
  8281. const struct intel_crtc_state *pipe_config)
  8282. {
  8283. struct drm_i915_private *dev_priv = to_i915(dev);
  8284. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8285. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8286. return dev_priv->vbt.lvds_ssc_freq;
  8287. else if (HAS_PCH_SPLIT(dev_priv))
  8288. return 120000;
  8289. else if (!IS_GEN2(dev_priv))
  8290. return 96000;
  8291. else
  8292. return 48000;
  8293. }
  8294. /* Returns the clock of the currently programmed mode of the given pipe. */
  8295. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8296. struct intel_crtc_state *pipe_config)
  8297. {
  8298. struct drm_device *dev = crtc->base.dev;
  8299. struct drm_i915_private *dev_priv = to_i915(dev);
  8300. int pipe = pipe_config->cpu_transcoder;
  8301. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8302. u32 fp;
  8303. struct dpll clock;
  8304. int port_clock;
  8305. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8306. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8307. fp = pipe_config->dpll_hw_state.fp0;
  8308. else
  8309. fp = pipe_config->dpll_hw_state.fp1;
  8310. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8311. if (IS_PINEVIEW(dev_priv)) {
  8312. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8313. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8314. } else {
  8315. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8316. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8317. }
  8318. if (!IS_GEN2(dev_priv)) {
  8319. if (IS_PINEVIEW(dev_priv))
  8320. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8321. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8322. else
  8323. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8324. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8325. switch (dpll & DPLL_MODE_MASK) {
  8326. case DPLLB_MODE_DAC_SERIAL:
  8327. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8328. 5 : 10;
  8329. break;
  8330. case DPLLB_MODE_LVDS:
  8331. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8332. 7 : 14;
  8333. break;
  8334. default:
  8335. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8336. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8337. return;
  8338. }
  8339. if (IS_PINEVIEW(dev_priv))
  8340. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8341. else
  8342. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8343. } else {
  8344. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8345. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8346. if (is_lvds) {
  8347. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8348. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8349. if (lvds & LVDS_CLKB_POWER_UP)
  8350. clock.p2 = 7;
  8351. else
  8352. clock.p2 = 14;
  8353. } else {
  8354. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8355. clock.p1 = 2;
  8356. else {
  8357. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8358. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8359. }
  8360. if (dpll & PLL_P2_DIVIDE_BY_4)
  8361. clock.p2 = 4;
  8362. else
  8363. clock.p2 = 2;
  8364. }
  8365. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8366. }
  8367. /*
  8368. * This value includes pixel_multiplier. We will use
  8369. * port_clock to compute adjusted_mode.crtc_clock in the
  8370. * encoder's get_config() function.
  8371. */
  8372. pipe_config->port_clock = port_clock;
  8373. }
  8374. int intel_dotclock_calculate(int link_freq,
  8375. const struct intel_link_m_n *m_n)
  8376. {
  8377. /*
  8378. * The calculation for the data clock is:
  8379. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8380. * But we want to avoid losing precison if possible, so:
  8381. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8382. *
  8383. * and the link clock is simpler:
  8384. * link_clock = (m * link_clock) / n
  8385. */
  8386. if (!m_n->link_n)
  8387. return 0;
  8388. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8389. }
  8390. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8391. struct intel_crtc_state *pipe_config)
  8392. {
  8393. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8394. /* read out port_clock from the DPLL */
  8395. i9xx_crtc_clock_get(crtc, pipe_config);
  8396. /*
  8397. * In case there is an active pipe without active ports,
  8398. * we may need some idea for the dotclock anyway.
  8399. * Calculate one based on the FDI configuration.
  8400. */
  8401. pipe_config->base.adjusted_mode.crtc_clock =
  8402. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8403. &pipe_config->fdi_m_n);
  8404. }
  8405. /* Returns the currently programmed mode of the given encoder. */
  8406. struct drm_display_mode *
  8407. intel_encoder_current_mode(struct intel_encoder *encoder)
  8408. {
  8409. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8410. struct intel_crtc_state *crtc_state;
  8411. struct drm_display_mode *mode;
  8412. struct intel_crtc *crtc;
  8413. enum pipe pipe;
  8414. if (!encoder->get_hw_state(encoder, &pipe))
  8415. return NULL;
  8416. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8417. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8418. if (!mode)
  8419. return NULL;
  8420. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8421. if (!crtc_state) {
  8422. kfree(mode);
  8423. return NULL;
  8424. }
  8425. crtc_state->base.crtc = &crtc->base;
  8426. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8427. kfree(crtc_state);
  8428. kfree(mode);
  8429. return NULL;
  8430. }
  8431. encoder->get_config(encoder, crtc_state);
  8432. intel_mode_from_pipe_config(mode, crtc_state);
  8433. kfree(crtc_state);
  8434. return mode;
  8435. }
  8436. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8437. {
  8438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8439. drm_crtc_cleanup(crtc);
  8440. kfree(intel_crtc);
  8441. }
  8442. /**
  8443. * intel_wm_need_update - Check whether watermarks need updating
  8444. * @plane: drm plane
  8445. * @state: new plane state
  8446. *
  8447. * Check current plane state versus the new one to determine whether
  8448. * watermarks need to be recalculated.
  8449. *
  8450. * Returns true or false.
  8451. */
  8452. static bool intel_wm_need_update(struct drm_plane *plane,
  8453. struct drm_plane_state *state)
  8454. {
  8455. struct intel_plane_state *new = to_intel_plane_state(state);
  8456. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8457. /* Update watermarks on tiling or size changes. */
  8458. if (new->base.visible != cur->base.visible)
  8459. return true;
  8460. if (!cur->base.fb || !new->base.fb)
  8461. return false;
  8462. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8463. cur->base.rotation != new->base.rotation ||
  8464. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8465. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8466. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8467. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8468. return true;
  8469. return false;
  8470. }
  8471. static bool needs_scaling(const struct intel_plane_state *state)
  8472. {
  8473. int src_w = drm_rect_width(&state->base.src) >> 16;
  8474. int src_h = drm_rect_height(&state->base.src) >> 16;
  8475. int dst_w = drm_rect_width(&state->base.dst);
  8476. int dst_h = drm_rect_height(&state->base.dst);
  8477. return (src_w != dst_w || src_h != dst_h);
  8478. }
  8479. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8480. struct drm_crtc_state *crtc_state,
  8481. const struct intel_plane_state *old_plane_state,
  8482. struct drm_plane_state *plane_state)
  8483. {
  8484. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8485. struct drm_crtc *crtc = crtc_state->crtc;
  8486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8487. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8488. struct drm_device *dev = crtc->dev;
  8489. struct drm_i915_private *dev_priv = to_i915(dev);
  8490. bool mode_changed = needs_modeset(crtc_state);
  8491. bool was_crtc_enabled = old_crtc_state->base.active;
  8492. bool is_crtc_enabled = crtc_state->active;
  8493. bool turn_off, turn_on, visible, was_visible;
  8494. struct drm_framebuffer *fb = plane_state->fb;
  8495. int ret;
  8496. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8497. ret = skl_update_scaler_plane(
  8498. to_intel_crtc_state(crtc_state),
  8499. to_intel_plane_state(plane_state));
  8500. if (ret)
  8501. return ret;
  8502. }
  8503. was_visible = old_plane_state->base.visible;
  8504. visible = plane_state->visible;
  8505. if (!was_crtc_enabled && WARN_ON(was_visible))
  8506. was_visible = false;
  8507. /*
  8508. * Visibility is calculated as if the crtc was on, but
  8509. * after scaler setup everything depends on it being off
  8510. * when the crtc isn't active.
  8511. *
  8512. * FIXME this is wrong for watermarks. Watermarks should also
  8513. * be computed as if the pipe would be active. Perhaps move
  8514. * per-plane wm computation to the .check_plane() hook, and
  8515. * only combine the results from all planes in the current place?
  8516. */
  8517. if (!is_crtc_enabled) {
  8518. plane_state->visible = visible = false;
  8519. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8520. }
  8521. if (!was_visible && !visible)
  8522. return 0;
  8523. if (fb != old_plane_state->base.fb)
  8524. pipe_config->fb_changed = true;
  8525. turn_off = was_visible && (!visible || mode_changed);
  8526. turn_on = visible && (!was_visible || mode_changed);
  8527. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8528. intel_crtc->base.base.id, intel_crtc->base.name,
  8529. plane->base.base.id, plane->base.name,
  8530. fb ? fb->base.id : -1);
  8531. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8532. plane->base.base.id, plane->base.name,
  8533. was_visible, visible,
  8534. turn_off, turn_on, mode_changed);
  8535. if (turn_on) {
  8536. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8537. pipe_config->update_wm_pre = true;
  8538. /* must disable cxsr around plane enable/disable */
  8539. if (plane->id != PLANE_CURSOR)
  8540. pipe_config->disable_cxsr = true;
  8541. } else if (turn_off) {
  8542. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8543. pipe_config->update_wm_post = true;
  8544. /* must disable cxsr around plane enable/disable */
  8545. if (plane->id != PLANE_CURSOR)
  8546. pipe_config->disable_cxsr = true;
  8547. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8548. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8549. /* FIXME bollocks */
  8550. pipe_config->update_wm_pre = true;
  8551. pipe_config->update_wm_post = true;
  8552. }
  8553. }
  8554. if (visible || was_visible)
  8555. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8556. /*
  8557. * WaCxSRDisabledForSpriteScaling:ivb
  8558. *
  8559. * cstate->update_wm was already set above, so this flag will
  8560. * take effect when we commit and program watermarks.
  8561. */
  8562. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8563. needs_scaling(to_intel_plane_state(plane_state)) &&
  8564. !needs_scaling(old_plane_state))
  8565. pipe_config->disable_lp_wm = true;
  8566. return 0;
  8567. }
  8568. static bool encoders_cloneable(const struct intel_encoder *a,
  8569. const struct intel_encoder *b)
  8570. {
  8571. /* masks could be asymmetric, so check both ways */
  8572. return a == b || (a->cloneable & (1 << b->type) &&
  8573. b->cloneable & (1 << a->type));
  8574. }
  8575. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8576. struct intel_crtc *crtc,
  8577. struct intel_encoder *encoder)
  8578. {
  8579. struct intel_encoder *source_encoder;
  8580. struct drm_connector *connector;
  8581. struct drm_connector_state *connector_state;
  8582. int i;
  8583. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8584. if (connector_state->crtc != &crtc->base)
  8585. continue;
  8586. source_encoder =
  8587. to_intel_encoder(connector_state->best_encoder);
  8588. if (!encoders_cloneable(encoder, source_encoder))
  8589. return false;
  8590. }
  8591. return true;
  8592. }
  8593. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8594. struct drm_crtc_state *crtc_state)
  8595. {
  8596. struct drm_device *dev = crtc->dev;
  8597. struct drm_i915_private *dev_priv = to_i915(dev);
  8598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8599. struct intel_crtc_state *pipe_config =
  8600. to_intel_crtc_state(crtc_state);
  8601. struct drm_atomic_state *state = crtc_state->state;
  8602. int ret;
  8603. bool mode_changed = needs_modeset(crtc_state);
  8604. if (mode_changed && !crtc_state->active)
  8605. pipe_config->update_wm_post = true;
  8606. if (mode_changed && crtc_state->enable &&
  8607. dev_priv->display.crtc_compute_clock &&
  8608. !WARN_ON(pipe_config->shared_dpll)) {
  8609. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8610. pipe_config);
  8611. if (ret)
  8612. return ret;
  8613. }
  8614. if (crtc_state->color_mgmt_changed) {
  8615. ret = intel_color_check(crtc, crtc_state);
  8616. if (ret)
  8617. return ret;
  8618. /*
  8619. * Changing color management on Intel hardware is
  8620. * handled as part of planes update.
  8621. */
  8622. crtc_state->planes_changed = true;
  8623. }
  8624. ret = 0;
  8625. if (dev_priv->display.compute_pipe_wm) {
  8626. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8627. if (ret) {
  8628. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8629. return ret;
  8630. }
  8631. }
  8632. if (dev_priv->display.compute_intermediate_wm &&
  8633. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8634. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8635. return 0;
  8636. /*
  8637. * Calculate 'intermediate' watermarks that satisfy both the
  8638. * old state and the new state. We can program these
  8639. * immediately.
  8640. */
  8641. ret = dev_priv->display.compute_intermediate_wm(dev,
  8642. intel_crtc,
  8643. pipe_config);
  8644. if (ret) {
  8645. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8646. return ret;
  8647. }
  8648. } else if (dev_priv->display.compute_intermediate_wm) {
  8649. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8650. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8651. }
  8652. if (INTEL_GEN(dev_priv) >= 9) {
  8653. if (mode_changed)
  8654. ret = skl_update_scaler_crtc(pipe_config);
  8655. if (!ret)
  8656. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8657. pipe_config);
  8658. if (!ret)
  8659. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8660. pipe_config);
  8661. }
  8662. if (HAS_IPS(dev_priv))
  8663. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8664. return ret;
  8665. }
  8666. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8667. .atomic_begin = intel_begin_crtc_commit,
  8668. .atomic_flush = intel_finish_crtc_commit,
  8669. .atomic_check = intel_crtc_atomic_check,
  8670. };
  8671. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8672. {
  8673. struct intel_connector *connector;
  8674. struct drm_connector_list_iter conn_iter;
  8675. drm_connector_list_iter_begin(dev, &conn_iter);
  8676. for_each_intel_connector_iter(connector, &conn_iter) {
  8677. if (connector->base.state->crtc)
  8678. drm_connector_unreference(&connector->base);
  8679. if (connector->base.encoder) {
  8680. connector->base.state->best_encoder =
  8681. connector->base.encoder;
  8682. connector->base.state->crtc =
  8683. connector->base.encoder->crtc;
  8684. drm_connector_reference(&connector->base);
  8685. } else {
  8686. connector->base.state->best_encoder = NULL;
  8687. connector->base.state->crtc = NULL;
  8688. }
  8689. }
  8690. drm_connector_list_iter_end(&conn_iter);
  8691. }
  8692. static void
  8693. connected_sink_compute_bpp(struct intel_connector *connector,
  8694. struct intel_crtc_state *pipe_config)
  8695. {
  8696. const struct drm_display_info *info = &connector->base.display_info;
  8697. int bpp = pipe_config->pipe_bpp;
  8698. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8699. connector->base.base.id,
  8700. connector->base.name);
  8701. /* Don't use an invalid EDID bpc value */
  8702. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8703. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8704. bpp, info->bpc * 3);
  8705. pipe_config->pipe_bpp = info->bpc * 3;
  8706. }
  8707. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8708. if (info->bpc == 0 && bpp > 24) {
  8709. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8710. bpp);
  8711. pipe_config->pipe_bpp = 24;
  8712. }
  8713. }
  8714. static int
  8715. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8716. struct intel_crtc_state *pipe_config)
  8717. {
  8718. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8719. struct drm_atomic_state *state;
  8720. struct drm_connector *connector;
  8721. struct drm_connector_state *connector_state;
  8722. int bpp, i;
  8723. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8724. IS_CHERRYVIEW(dev_priv)))
  8725. bpp = 10*3;
  8726. else if (INTEL_GEN(dev_priv) >= 5)
  8727. bpp = 12*3;
  8728. else
  8729. bpp = 8*3;
  8730. pipe_config->pipe_bpp = bpp;
  8731. state = pipe_config->base.state;
  8732. /* Clamp display bpp to EDID value */
  8733. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8734. if (connector_state->crtc != &crtc->base)
  8735. continue;
  8736. connected_sink_compute_bpp(to_intel_connector(connector),
  8737. pipe_config);
  8738. }
  8739. return bpp;
  8740. }
  8741. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8742. {
  8743. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8744. "type: 0x%x flags: 0x%x\n",
  8745. mode->crtc_clock,
  8746. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8747. mode->crtc_hsync_end, mode->crtc_htotal,
  8748. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8749. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8750. }
  8751. static inline void
  8752. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8753. unsigned int lane_count, struct intel_link_m_n *m_n)
  8754. {
  8755. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8756. id, lane_count,
  8757. m_n->gmch_m, m_n->gmch_n,
  8758. m_n->link_m, m_n->link_n, m_n->tu);
  8759. }
  8760. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  8761. static const char * const output_type_str[] = {
  8762. OUTPUT_TYPE(UNUSED),
  8763. OUTPUT_TYPE(ANALOG),
  8764. OUTPUT_TYPE(DVO),
  8765. OUTPUT_TYPE(SDVO),
  8766. OUTPUT_TYPE(LVDS),
  8767. OUTPUT_TYPE(TVOUT),
  8768. OUTPUT_TYPE(HDMI),
  8769. OUTPUT_TYPE(DP),
  8770. OUTPUT_TYPE(EDP),
  8771. OUTPUT_TYPE(DSI),
  8772. OUTPUT_TYPE(DDI),
  8773. OUTPUT_TYPE(DP_MST),
  8774. };
  8775. #undef OUTPUT_TYPE
  8776. static void snprintf_output_types(char *buf, size_t len,
  8777. unsigned int output_types)
  8778. {
  8779. char *str = buf;
  8780. int i;
  8781. str[0] = '\0';
  8782. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  8783. int r;
  8784. if ((output_types & BIT(i)) == 0)
  8785. continue;
  8786. r = snprintf(str, len, "%s%s",
  8787. str != buf ? "," : "", output_type_str[i]);
  8788. if (r >= len)
  8789. break;
  8790. str += r;
  8791. len -= r;
  8792. output_types &= ~BIT(i);
  8793. }
  8794. WARN_ON_ONCE(output_types != 0);
  8795. }
  8796. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8797. struct intel_crtc_state *pipe_config,
  8798. const char *context)
  8799. {
  8800. struct drm_device *dev = crtc->base.dev;
  8801. struct drm_i915_private *dev_priv = to_i915(dev);
  8802. struct drm_plane *plane;
  8803. struct intel_plane *intel_plane;
  8804. struct intel_plane_state *state;
  8805. struct drm_framebuffer *fb;
  8806. char buf[64];
  8807. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8808. crtc->base.base.id, crtc->base.name, context);
  8809. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  8810. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  8811. buf, pipe_config->output_types);
  8812. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8813. transcoder_name(pipe_config->cpu_transcoder),
  8814. pipe_config->pipe_bpp, pipe_config->dither);
  8815. if (pipe_config->has_pch_encoder)
  8816. intel_dump_m_n_config(pipe_config, "fdi",
  8817. pipe_config->fdi_lanes,
  8818. &pipe_config->fdi_m_n);
  8819. if (pipe_config->ycbcr420)
  8820. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8821. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8822. intel_dump_m_n_config(pipe_config, "dp m_n",
  8823. pipe_config->lane_count, &pipe_config->dp_m_n);
  8824. if (pipe_config->has_drrs)
  8825. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8826. pipe_config->lane_count,
  8827. &pipe_config->dp_m2_n2);
  8828. }
  8829. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8830. pipe_config->has_audio, pipe_config->has_infoframe);
  8831. DRM_DEBUG_KMS("requested mode:\n");
  8832. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8833. DRM_DEBUG_KMS("adjusted mode:\n");
  8834. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8835. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8836. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8837. pipe_config->port_clock,
  8838. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8839. pipe_config->pixel_rate);
  8840. if (INTEL_GEN(dev_priv) >= 9)
  8841. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8842. crtc->num_scalers,
  8843. pipe_config->scaler_state.scaler_users,
  8844. pipe_config->scaler_state.scaler_id);
  8845. if (HAS_GMCH_DISPLAY(dev_priv))
  8846. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8847. pipe_config->gmch_pfit.control,
  8848. pipe_config->gmch_pfit.pgm_ratios,
  8849. pipe_config->gmch_pfit.lvds_border_bits);
  8850. else
  8851. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8852. pipe_config->pch_pfit.pos,
  8853. pipe_config->pch_pfit.size,
  8854. enableddisabled(pipe_config->pch_pfit.enabled));
  8855. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8856. pipe_config->ips_enabled, pipe_config->double_wide);
  8857. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8858. DRM_DEBUG_KMS("planes on this crtc\n");
  8859. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8860. struct drm_format_name_buf format_name;
  8861. intel_plane = to_intel_plane(plane);
  8862. if (intel_plane->pipe != crtc->pipe)
  8863. continue;
  8864. state = to_intel_plane_state(plane->state);
  8865. fb = state->base.fb;
  8866. if (!fb) {
  8867. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  8868. plane->base.id, plane->name, state->scaler_id);
  8869. continue;
  8870. }
  8871. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  8872. plane->base.id, plane->name,
  8873. fb->base.id, fb->width, fb->height,
  8874. drm_get_format_name(fb->format->format, &format_name));
  8875. if (INTEL_GEN(dev_priv) >= 9)
  8876. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  8877. state->scaler_id,
  8878. state->base.src.x1 >> 16,
  8879. state->base.src.y1 >> 16,
  8880. drm_rect_width(&state->base.src) >> 16,
  8881. drm_rect_height(&state->base.src) >> 16,
  8882. state->base.dst.x1, state->base.dst.y1,
  8883. drm_rect_width(&state->base.dst),
  8884. drm_rect_height(&state->base.dst));
  8885. }
  8886. }
  8887. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  8888. {
  8889. struct drm_device *dev = state->dev;
  8890. struct drm_connector *connector;
  8891. struct drm_connector_list_iter conn_iter;
  8892. unsigned int used_ports = 0;
  8893. unsigned int used_mst_ports = 0;
  8894. /*
  8895. * Walk the connector list instead of the encoder
  8896. * list to detect the problem on ddi platforms
  8897. * where there's just one encoder per digital port.
  8898. */
  8899. drm_connector_list_iter_begin(dev, &conn_iter);
  8900. drm_for_each_connector_iter(connector, &conn_iter) {
  8901. struct drm_connector_state *connector_state;
  8902. struct intel_encoder *encoder;
  8903. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  8904. if (!connector_state)
  8905. connector_state = connector->state;
  8906. if (!connector_state->best_encoder)
  8907. continue;
  8908. encoder = to_intel_encoder(connector_state->best_encoder);
  8909. WARN_ON(!connector_state->crtc);
  8910. switch (encoder->type) {
  8911. unsigned int port_mask;
  8912. case INTEL_OUTPUT_DDI:
  8913. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  8914. break;
  8915. case INTEL_OUTPUT_DP:
  8916. case INTEL_OUTPUT_HDMI:
  8917. case INTEL_OUTPUT_EDP:
  8918. port_mask = 1 << encoder->port;
  8919. /* the same port mustn't appear more than once */
  8920. if (used_ports & port_mask)
  8921. return false;
  8922. used_ports |= port_mask;
  8923. break;
  8924. case INTEL_OUTPUT_DP_MST:
  8925. used_mst_ports |=
  8926. 1 << encoder->port;
  8927. break;
  8928. default:
  8929. break;
  8930. }
  8931. }
  8932. drm_connector_list_iter_end(&conn_iter);
  8933. /* can't mix MST and SST/HDMI on the same port */
  8934. if (used_ports & used_mst_ports)
  8935. return false;
  8936. return true;
  8937. }
  8938. static void
  8939. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  8940. {
  8941. struct drm_i915_private *dev_priv =
  8942. to_i915(crtc_state->base.crtc->dev);
  8943. struct intel_crtc_scaler_state scaler_state;
  8944. struct intel_dpll_hw_state dpll_hw_state;
  8945. struct intel_shared_dpll *shared_dpll;
  8946. struct intel_crtc_wm_state wm_state;
  8947. bool force_thru, ips_force_disable;
  8948. /* FIXME: before the switch to atomic started, a new pipe_config was
  8949. * kzalloc'd. Code that depends on any field being zero should be
  8950. * fixed, so that the crtc_state can be safely duplicated. For now,
  8951. * only fields that are know to not cause problems are preserved. */
  8952. scaler_state = crtc_state->scaler_state;
  8953. shared_dpll = crtc_state->shared_dpll;
  8954. dpll_hw_state = crtc_state->dpll_hw_state;
  8955. force_thru = crtc_state->pch_pfit.force_thru;
  8956. ips_force_disable = crtc_state->ips_force_disable;
  8957. if (IS_G4X(dev_priv) ||
  8958. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  8959. wm_state = crtc_state->wm;
  8960. /* Keep base drm_crtc_state intact, only clear our extended struct */
  8961. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  8962. memset(&crtc_state->base + 1, 0,
  8963. sizeof(*crtc_state) - sizeof(crtc_state->base));
  8964. crtc_state->scaler_state = scaler_state;
  8965. crtc_state->shared_dpll = shared_dpll;
  8966. crtc_state->dpll_hw_state = dpll_hw_state;
  8967. crtc_state->pch_pfit.force_thru = force_thru;
  8968. crtc_state->ips_force_disable = ips_force_disable;
  8969. if (IS_G4X(dev_priv) ||
  8970. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  8971. crtc_state->wm = wm_state;
  8972. }
  8973. static int
  8974. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8975. struct intel_crtc_state *pipe_config)
  8976. {
  8977. struct drm_atomic_state *state = pipe_config->base.state;
  8978. struct intel_encoder *encoder;
  8979. struct drm_connector *connector;
  8980. struct drm_connector_state *connector_state;
  8981. int base_bpp, ret = -EINVAL;
  8982. int i;
  8983. bool retry = true;
  8984. clear_intel_crtc_state(pipe_config);
  8985. pipe_config->cpu_transcoder =
  8986. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8987. /*
  8988. * Sanitize sync polarity flags based on requested ones. If neither
  8989. * positive or negative polarity is requested, treat this as meaning
  8990. * negative polarity.
  8991. */
  8992. if (!(pipe_config->base.adjusted_mode.flags &
  8993. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8994. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8995. if (!(pipe_config->base.adjusted_mode.flags &
  8996. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8997. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8998. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8999. pipe_config);
  9000. if (base_bpp < 0)
  9001. goto fail;
  9002. /*
  9003. * Determine the real pipe dimensions. Note that stereo modes can
  9004. * increase the actual pipe size due to the frame doubling and
  9005. * insertion of additional space for blanks between the frame. This
  9006. * is stored in the crtc timings. We use the requested mode to do this
  9007. * computation to clearly distinguish it from the adjusted mode, which
  9008. * can be changed by the connectors in the below retry loop.
  9009. */
  9010. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9011. &pipe_config->pipe_src_w,
  9012. &pipe_config->pipe_src_h);
  9013. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9014. if (connector_state->crtc != crtc)
  9015. continue;
  9016. encoder = to_intel_encoder(connector_state->best_encoder);
  9017. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9018. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9019. goto fail;
  9020. }
  9021. /*
  9022. * Determine output_types before calling the .compute_config()
  9023. * hooks so that the hooks can use this information safely.
  9024. */
  9025. if (encoder->compute_output_type)
  9026. pipe_config->output_types |=
  9027. BIT(encoder->compute_output_type(encoder, pipe_config,
  9028. connector_state));
  9029. else
  9030. pipe_config->output_types |= BIT(encoder->type);
  9031. }
  9032. encoder_retry:
  9033. /* Ensure the port clock defaults are reset when retrying. */
  9034. pipe_config->port_clock = 0;
  9035. pipe_config->pixel_multiplier = 1;
  9036. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9037. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9038. CRTC_STEREO_DOUBLE);
  9039. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9040. * adjust it according to limitations or connector properties, and also
  9041. * a chance to reject the mode entirely.
  9042. */
  9043. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9044. if (connector_state->crtc != crtc)
  9045. continue;
  9046. encoder = to_intel_encoder(connector_state->best_encoder);
  9047. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9048. DRM_DEBUG_KMS("Encoder config failure\n");
  9049. goto fail;
  9050. }
  9051. }
  9052. /* Set default port clock if not overwritten by the encoder. Needs to be
  9053. * done afterwards in case the encoder adjusts the mode. */
  9054. if (!pipe_config->port_clock)
  9055. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9056. * pipe_config->pixel_multiplier;
  9057. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9058. if (ret < 0) {
  9059. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9060. goto fail;
  9061. }
  9062. if (ret == RETRY) {
  9063. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9064. ret = -EINVAL;
  9065. goto fail;
  9066. }
  9067. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9068. retry = false;
  9069. goto encoder_retry;
  9070. }
  9071. /* Dithering seems to not pass-through bits correctly when it should, so
  9072. * only enable it on 6bpc panels and when its not a compliance
  9073. * test requesting 6bpc video pattern.
  9074. */
  9075. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9076. !pipe_config->dither_force_disable;
  9077. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9078. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9079. fail:
  9080. return ret;
  9081. }
  9082. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9083. {
  9084. int diff;
  9085. if (clock1 == clock2)
  9086. return true;
  9087. if (!clock1 || !clock2)
  9088. return false;
  9089. diff = abs(clock1 - clock2);
  9090. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9091. return true;
  9092. return false;
  9093. }
  9094. static bool
  9095. intel_compare_m_n(unsigned int m, unsigned int n,
  9096. unsigned int m2, unsigned int n2,
  9097. bool exact)
  9098. {
  9099. if (m == m2 && n == n2)
  9100. return true;
  9101. if (exact || !m || !n || !m2 || !n2)
  9102. return false;
  9103. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9104. if (n > n2) {
  9105. while (n > n2) {
  9106. m2 <<= 1;
  9107. n2 <<= 1;
  9108. }
  9109. } else if (n < n2) {
  9110. while (n < n2) {
  9111. m <<= 1;
  9112. n <<= 1;
  9113. }
  9114. }
  9115. if (n != n2)
  9116. return false;
  9117. return intel_fuzzy_clock_check(m, m2);
  9118. }
  9119. static bool
  9120. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9121. struct intel_link_m_n *m2_n2,
  9122. bool adjust)
  9123. {
  9124. if (m_n->tu == m2_n2->tu &&
  9125. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9126. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9127. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9128. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9129. if (adjust)
  9130. *m2_n2 = *m_n;
  9131. return true;
  9132. }
  9133. return false;
  9134. }
  9135. static void __printf(3, 4)
  9136. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9137. {
  9138. char *level;
  9139. unsigned int category;
  9140. struct va_format vaf;
  9141. va_list args;
  9142. if (adjust) {
  9143. level = KERN_DEBUG;
  9144. category = DRM_UT_KMS;
  9145. } else {
  9146. level = KERN_ERR;
  9147. category = DRM_UT_NONE;
  9148. }
  9149. va_start(args, format);
  9150. vaf.fmt = format;
  9151. vaf.va = &args;
  9152. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9153. va_end(args);
  9154. }
  9155. static bool
  9156. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9157. struct intel_crtc_state *current_config,
  9158. struct intel_crtc_state *pipe_config,
  9159. bool adjust)
  9160. {
  9161. bool ret = true;
  9162. bool fixup_inherited = adjust &&
  9163. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9164. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9165. #define PIPE_CONF_CHECK_X(name) \
  9166. if (current_config->name != pipe_config->name) { \
  9167. pipe_config_err(adjust, __stringify(name), \
  9168. "(expected 0x%08x, found 0x%08x)\n", \
  9169. current_config->name, \
  9170. pipe_config->name); \
  9171. ret = false; \
  9172. }
  9173. #define PIPE_CONF_CHECK_I(name) \
  9174. if (current_config->name != pipe_config->name) { \
  9175. pipe_config_err(adjust, __stringify(name), \
  9176. "(expected %i, found %i)\n", \
  9177. current_config->name, \
  9178. pipe_config->name); \
  9179. ret = false; \
  9180. }
  9181. #define PIPE_CONF_CHECK_BOOL(name) \
  9182. if (current_config->name != pipe_config->name) { \
  9183. pipe_config_err(adjust, __stringify(name), \
  9184. "(expected %s, found %s)\n", \
  9185. yesno(current_config->name), \
  9186. yesno(pipe_config->name)); \
  9187. ret = false; \
  9188. }
  9189. /*
  9190. * Checks state where we only read out the enabling, but not the entire
  9191. * state itself (like full infoframes or ELD for audio). These states
  9192. * require a full modeset on bootup to fix up.
  9193. */
  9194. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) \
  9195. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9196. PIPE_CONF_CHECK_BOOL(name); \
  9197. } else { \
  9198. pipe_config_err(adjust, __stringify(name), \
  9199. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9200. yesno(current_config->name), \
  9201. yesno(pipe_config->name)); \
  9202. ret = false; \
  9203. }
  9204. #define PIPE_CONF_CHECK_P(name) \
  9205. if (current_config->name != pipe_config->name) { \
  9206. pipe_config_err(adjust, __stringify(name), \
  9207. "(expected %p, found %p)\n", \
  9208. current_config->name, \
  9209. pipe_config->name); \
  9210. ret = false; \
  9211. }
  9212. #define PIPE_CONF_CHECK_M_N(name) \
  9213. if (!intel_compare_link_m_n(&current_config->name, \
  9214. &pipe_config->name,\
  9215. adjust)) { \
  9216. pipe_config_err(adjust, __stringify(name), \
  9217. "(expected tu %i gmch %i/%i link %i/%i, " \
  9218. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9219. current_config->name.tu, \
  9220. current_config->name.gmch_m, \
  9221. current_config->name.gmch_n, \
  9222. current_config->name.link_m, \
  9223. current_config->name.link_n, \
  9224. pipe_config->name.tu, \
  9225. pipe_config->name.gmch_m, \
  9226. pipe_config->name.gmch_n, \
  9227. pipe_config->name.link_m, \
  9228. pipe_config->name.link_n); \
  9229. ret = false; \
  9230. }
  9231. /* This is required for BDW+ where there is only one set of registers for
  9232. * switching between high and low RR.
  9233. * This macro can be used whenever a comparison has to be made between one
  9234. * hw state and multiple sw state variables.
  9235. */
  9236. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9237. if (!intel_compare_link_m_n(&current_config->name, \
  9238. &pipe_config->name, adjust) && \
  9239. !intel_compare_link_m_n(&current_config->alt_name, \
  9240. &pipe_config->name, adjust)) { \
  9241. pipe_config_err(adjust, __stringify(name), \
  9242. "(expected tu %i gmch %i/%i link %i/%i, " \
  9243. "or tu %i gmch %i/%i link %i/%i, " \
  9244. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9245. current_config->name.tu, \
  9246. current_config->name.gmch_m, \
  9247. current_config->name.gmch_n, \
  9248. current_config->name.link_m, \
  9249. current_config->name.link_n, \
  9250. current_config->alt_name.tu, \
  9251. current_config->alt_name.gmch_m, \
  9252. current_config->alt_name.gmch_n, \
  9253. current_config->alt_name.link_m, \
  9254. current_config->alt_name.link_n, \
  9255. pipe_config->name.tu, \
  9256. pipe_config->name.gmch_m, \
  9257. pipe_config->name.gmch_n, \
  9258. pipe_config->name.link_m, \
  9259. pipe_config->name.link_n); \
  9260. ret = false; \
  9261. }
  9262. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9263. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9264. pipe_config_err(adjust, __stringify(name), \
  9265. "(%x) (expected %i, found %i)\n", \
  9266. (mask), \
  9267. current_config->name & (mask), \
  9268. pipe_config->name & (mask)); \
  9269. ret = false; \
  9270. }
  9271. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9272. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9273. pipe_config_err(adjust, __stringify(name), \
  9274. "(expected %i, found %i)\n", \
  9275. current_config->name, \
  9276. pipe_config->name); \
  9277. ret = false; \
  9278. }
  9279. #define PIPE_CONF_QUIRK(quirk) \
  9280. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9281. PIPE_CONF_CHECK_I(cpu_transcoder);
  9282. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9283. PIPE_CONF_CHECK_I(fdi_lanes);
  9284. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9285. PIPE_CONF_CHECK_I(lane_count);
  9286. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9287. if (INTEL_GEN(dev_priv) < 8) {
  9288. PIPE_CONF_CHECK_M_N(dp_m_n);
  9289. if (current_config->has_drrs)
  9290. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9291. } else
  9292. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9293. PIPE_CONF_CHECK_X(output_types);
  9294. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9295. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9296. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9297. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9298. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9299. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9300. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9301. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9302. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9303. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9304. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9305. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9306. PIPE_CONF_CHECK_I(pixel_multiplier);
  9307. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9308. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9309. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9310. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9311. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9312. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9313. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9314. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9315. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9316. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9317. DRM_MODE_FLAG_INTERLACE);
  9318. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9319. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9320. DRM_MODE_FLAG_PHSYNC);
  9321. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9322. DRM_MODE_FLAG_NHSYNC);
  9323. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9324. DRM_MODE_FLAG_PVSYNC);
  9325. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9326. DRM_MODE_FLAG_NVSYNC);
  9327. }
  9328. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9329. /* pfit ratios are autocomputed by the hw on gen4+ */
  9330. if (INTEL_GEN(dev_priv) < 4)
  9331. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9332. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9333. if (!adjust) {
  9334. PIPE_CONF_CHECK_I(pipe_src_w);
  9335. PIPE_CONF_CHECK_I(pipe_src_h);
  9336. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9337. if (current_config->pch_pfit.enabled) {
  9338. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9339. PIPE_CONF_CHECK_X(pch_pfit.size);
  9340. }
  9341. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9342. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9343. }
  9344. PIPE_CONF_CHECK_BOOL(double_wide);
  9345. PIPE_CONF_CHECK_P(shared_dpll);
  9346. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9347. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9348. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9349. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9350. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9351. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9352. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9353. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9354. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9355. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9356. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9357. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9358. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9359. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9360. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9361. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9362. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9363. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9364. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9365. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9366. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9367. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9368. PIPE_CONF_CHECK_X(dsi_pll.div);
  9369. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9370. PIPE_CONF_CHECK_I(pipe_bpp);
  9371. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9372. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9373. PIPE_CONF_CHECK_I(min_voltage_level);
  9374. #undef PIPE_CONF_CHECK_X
  9375. #undef PIPE_CONF_CHECK_I
  9376. #undef PIPE_CONF_CHECK_BOOL
  9377. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9378. #undef PIPE_CONF_CHECK_P
  9379. #undef PIPE_CONF_CHECK_FLAGS
  9380. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9381. #undef PIPE_CONF_QUIRK
  9382. return ret;
  9383. }
  9384. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9385. const struct intel_crtc_state *pipe_config)
  9386. {
  9387. if (pipe_config->has_pch_encoder) {
  9388. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9389. &pipe_config->fdi_m_n);
  9390. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9391. /*
  9392. * FDI already provided one idea for the dotclock.
  9393. * Yell if the encoder disagrees.
  9394. */
  9395. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9396. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9397. fdi_dotclock, dotclock);
  9398. }
  9399. }
  9400. static void verify_wm_state(struct drm_crtc *crtc,
  9401. struct drm_crtc_state *new_state)
  9402. {
  9403. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9404. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9405. struct skl_pipe_wm hw_wm, *sw_wm;
  9406. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9407. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9409. const enum pipe pipe = intel_crtc->pipe;
  9410. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9411. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9412. return;
  9413. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9414. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9415. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9416. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9417. /* planes */
  9418. for_each_universal_plane(dev_priv, pipe, plane) {
  9419. hw_plane_wm = &hw_wm.planes[plane];
  9420. sw_plane_wm = &sw_wm->planes[plane];
  9421. /* Watermarks */
  9422. for (level = 0; level <= max_level; level++) {
  9423. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9424. &sw_plane_wm->wm[level]))
  9425. continue;
  9426. DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9427. pipe_name(pipe), plane + 1, level,
  9428. sw_plane_wm->wm[level].plane_en,
  9429. sw_plane_wm->wm[level].plane_res_b,
  9430. sw_plane_wm->wm[level].plane_res_l,
  9431. hw_plane_wm->wm[level].plane_en,
  9432. hw_plane_wm->wm[level].plane_res_b,
  9433. hw_plane_wm->wm[level].plane_res_l);
  9434. }
  9435. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9436. &sw_plane_wm->trans_wm)) {
  9437. DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9438. pipe_name(pipe), plane + 1,
  9439. sw_plane_wm->trans_wm.plane_en,
  9440. sw_plane_wm->trans_wm.plane_res_b,
  9441. sw_plane_wm->trans_wm.plane_res_l,
  9442. hw_plane_wm->trans_wm.plane_en,
  9443. hw_plane_wm->trans_wm.plane_res_b,
  9444. hw_plane_wm->trans_wm.plane_res_l);
  9445. }
  9446. /* DDB */
  9447. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9448. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9449. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9450. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9451. pipe_name(pipe), plane + 1,
  9452. sw_ddb_entry->start, sw_ddb_entry->end,
  9453. hw_ddb_entry->start, hw_ddb_entry->end);
  9454. }
  9455. }
  9456. /*
  9457. * cursor
  9458. * If the cursor plane isn't active, we may not have updated it's ddb
  9459. * allocation. In that case since the ddb allocation will be updated
  9460. * once the plane becomes visible, we can skip this check
  9461. */
  9462. if (1) {
  9463. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9464. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9465. /* Watermarks */
  9466. for (level = 0; level <= max_level; level++) {
  9467. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9468. &sw_plane_wm->wm[level]))
  9469. continue;
  9470. DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9471. pipe_name(pipe), level,
  9472. sw_plane_wm->wm[level].plane_en,
  9473. sw_plane_wm->wm[level].plane_res_b,
  9474. sw_plane_wm->wm[level].plane_res_l,
  9475. hw_plane_wm->wm[level].plane_en,
  9476. hw_plane_wm->wm[level].plane_res_b,
  9477. hw_plane_wm->wm[level].plane_res_l);
  9478. }
  9479. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9480. &sw_plane_wm->trans_wm)) {
  9481. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9482. pipe_name(pipe),
  9483. sw_plane_wm->trans_wm.plane_en,
  9484. sw_plane_wm->trans_wm.plane_res_b,
  9485. sw_plane_wm->trans_wm.plane_res_l,
  9486. hw_plane_wm->trans_wm.plane_en,
  9487. hw_plane_wm->trans_wm.plane_res_b,
  9488. hw_plane_wm->trans_wm.plane_res_l);
  9489. }
  9490. /* DDB */
  9491. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9492. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9493. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9494. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9495. pipe_name(pipe),
  9496. sw_ddb_entry->start, sw_ddb_entry->end,
  9497. hw_ddb_entry->start, hw_ddb_entry->end);
  9498. }
  9499. }
  9500. }
  9501. static void
  9502. verify_connector_state(struct drm_device *dev,
  9503. struct drm_atomic_state *state,
  9504. struct drm_crtc *crtc)
  9505. {
  9506. struct drm_connector *connector;
  9507. struct drm_connector_state *new_conn_state;
  9508. int i;
  9509. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9510. struct drm_encoder *encoder = connector->encoder;
  9511. struct drm_crtc_state *crtc_state = NULL;
  9512. if (new_conn_state->crtc != crtc)
  9513. continue;
  9514. if (crtc)
  9515. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9516. intel_connector_verify_state(crtc_state, new_conn_state);
  9517. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9518. "connector's atomic encoder doesn't match legacy encoder\n");
  9519. }
  9520. }
  9521. static void
  9522. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9523. {
  9524. struct intel_encoder *encoder;
  9525. struct drm_connector *connector;
  9526. struct drm_connector_state *old_conn_state, *new_conn_state;
  9527. int i;
  9528. for_each_intel_encoder(dev, encoder) {
  9529. bool enabled = false, found = false;
  9530. enum pipe pipe;
  9531. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9532. encoder->base.base.id,
  9533. encoder->base.name);
  9534. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9535. new_conn_state, i) {
  9536. if (old_conn_state->best_encoder == &encoder->base)
  9537. found = true;
  9538. if (new_conn_state->best_encoder != &encoder->base)
  9539. continue;
  9540. found = enabled = true;
  9541. I915_STATE_WARN(new_conn_state->crtc !=
  9542. encoder->base.crtc,
  9543. "connector's crtc doesn't match encoder crtc\n");
  9544. }
  9545. if (!found)
  9546. continue;
  9547. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9548. "encoder's enabled state mismatch "
  9549. "(expected %i, found %i)\n",
  9550. !!encoder->base.crtc, enabled);
  9551. if (!encoder->base.crtc) {
  9552. bool active;
  9553. active = encoder->get_hw_state(encoder, &pipe);
  9554. I915_STATE_WARN(active,
  9555. "encoder detached but still enabled on pipe %c.\n",
  9556. pipe_name(pipe));
  9557. }
  9558. }
  9559. }
  9560. static void
  9561. verify_crtc_state(struct drm_crtc *crtc,
  9562. struct drm_crtc_state *old_crtc_state,
  9563. struct drm_crtc_state *new_crtc_state)
  9564. {
  9565. struct drm_device *dev = crtc->dev;
  9566. struct drm_i915_private *dev_priv = to_i915(dev);
  9567. struct intel_encoder *encoder;
  9568. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9569. struct intel_crtc_state *pipe_config, *sw_config;
  9570. struct drm_atomic_state *old_state;
  9571. bool active;
  9572. old_state = old_crtc_state->state;
  9573. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9574. pipe_config = to_intel_crtc_state(old_crtc_state);
  9575. memset(pipe_config, 0, sizeof(*pipe_config));
  9576. pipe_config->base.crtc = crtc;
  9577. pipe_config->base.state = old_state;
  9578. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9579. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9580. /* we keep both pipes enabled on 830 */
  9581. if (IS_I830(dev_priv))
  9582. active = new_crtc_state->active;
  9583. I915_STATE_WARN(new_crtc_state->active != active,
  9584. "crtc active state doesn't match with hw state "
  9585. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9586. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9587. "transitional active state does not match atomic hw state "
  9588. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9589. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9590. enum pipe pipe;
  9591. active = encoder->get_hw_state(encoder, &pipe);
  9592. I915_STATE_WARN(active != new_crtc_state->active,
  9593. "[ENCODER:%i] active %i with crtc active %i\n",
  9594. encoder->base.base.id, active, new_crtc_state->active);
  9595. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9596. "Encoder connected to wrong pipe %c\n",
  9597. pipe_name(pipe));
  9598. if (active)
  9599. encoder->get_config(encoder, pipe_config);
  9600. }
  9601. intel_crtc_compute_pixel_rate(pipe_config);
  9602. if (!new_crtc_state->active)
  9603. return;
  9604. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9605. sw_config = to_intel_crtc_state(new_crtc_state);
  9606. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9607. pipe_config, false)) {
  9608. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9609. intel_dump_pipe_config(intel_crtc, pipe_config,
  9610. "[hw state]");
  9611. intel_dump_pipe_config(intel_crtc, sw_config,
  9612. "[sw state]");
  9613. }
  9614. }
  9615. static void
  9616. intel_verify_planes(struct intel_atomic_state *state)
  9617. {
  9618. struct intel_plane *plane;
  9619. const struct intel_plane_state *plane_state;
  9620. int i;
  9621. for_each_new_intel_plane_in_state(state, plane,
  9622. plane_state, i)
  9623. assert_plane(plane, plane_state->base.visible);
  9624. }
  9625. static void
  9626. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9627. struct intel_shared_dpll *pll,
  9628. struct drm_crtc *crtc,
  9629. struct drm_crtc_state *new_state)
  9630. {
  9631. struct intel_dpll_hw_state dpll_hw_state;
  9632. unsigned crtc_mask;
  9633. bool active;
  9634. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9635. DRM_DEBUG_KMS("%s\n", pll->name);
  9636. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9637. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9638. I915_STATE_WARN(!pll->on && pll->active_mask,
  9639. "pll in active use but not on in sw tracking\n");
  9640. I915_STATE_WARN(pll->on && !pll->active_mask,
  9641. "pll is on but not used by any active crtc\n");
  9642. I915_STATE_WARN(pll->on != active,
  9643. "pll on state mismatch (expected %i, found %i)\n",
  9644. pll->on, active);
  9645. }
  9646. if (!crtc) {
  9647. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9648. "more active pll users than references: %x vs %x\n",
  9649. pll->active_mask, pll->state.crtc_mask);
  9650. return;
  9651. }
  9652. crtc_mask = 1 << drm_crtc_index(crtc);
  9653. if (new_state->active)
  9654. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9655. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9656. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9657. else
  9658. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9659. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9660. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9661. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9662. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9663. crtc_mask, pll->state.crtc_mask);
  9664. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9665. &dpll_hw_state,
  9666. sizeof(dpll_hw_state)),
  9667. "pll hw state mismatch\n");
  9668. }
  9669. static void
  9670. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9671. struct drm_crtc_state *old_crtc_state,
  9672. struct drm_crtc_state *new_crtc_state)
  9673. {
  9674. struct drm_i915_private *dev_priv = to_i915(dev);
  9675. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9676. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9677. if (new_state->shared_dpll)
  9678. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9679. if (old_state->shared_dpll &&
  9680. old_state->shared_dpll != new_state->shared_dpll) {
  9681. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9682. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9683. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9684. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9685. pipe_name(drm_crtc_index(crtc)));
  9686. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9687. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9688. pipe_name(drm_crtc_index(crtc)));
  9689. }
  9690. }
  9691. static void
  9692. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9693. struct drm_atomic_state *state,
  9694. struct drm_crtc_state *old_state,
  9695. struct drm_crtc_state *new_state)
  9696. {
  9697. if (!needs_modeset(new_state) &&
  9698. !to_intel_crtc_state(new_state)->update_pipe)
  9699. return;
  9700. verify_wm_state(crtc, new_state);
  9701. verify_connector_state(crtc->dev, state, crtc);
  9702. verify_crtc_state(crtc, old_state, new_state);
  9703. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9704. }
  9705. static void
  9706. verify_disabled_dpll_state(struct drm_device *dev)
  9707. {
  9708. struct drm_i915_private *dev_priv = to_i915(dev);
  9709. int i;
  9710. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9711. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9712. }
  9713. static void
  9714. intel_modeset_verify_disabled(struct drm_device *dev,
  9715. struct drm_atomic_state *state)
  9716. {
  9717. verify_encoder_state(dev, state);
  9718. verify_connector_state(dev, state, NULL);
  9719. verify_disabled_dpll_state(dev);
  9720. }
  9721. static void update_scanline_offset(struct intel_crtc *crtc)
  9722. {
  9723. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9724. /*
  9725. * The scanline counter increments at the leading edge of hsync.
  9726. *
  9727. * On most platforms it starts counting from vtotal-1 on the
  9728. * first active line. That means the scanline counter value is
  9729. * always one less than what we would expect. Ie. just after
  9730. * start of vblank, which also occurs at start of hsync (on the
  9731. * last active line), the scanline counter will read vblank_start-1.
  9732. *
  9733. * On gen2 the scanline counter starts counting from 1 instead
  9734. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9735. * to keep the value positive), instead of adding one.
  9736. *
  9737. * On HSW+ the behaviour of the scanline counter depends on the output
  9738. * type. For DP ports it behaves like most other platforms, but on HDMI
  9739. * there's an extra 1 line difference. So we need to add two instead of
  9740. * one to the value.
  9741. *
  9742. * On VLV/CHV DSI the scanline counter would appear to increment
  9743. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9744. * that means we can't tell whether we're in vblank or not while
  9745. * we're on that particular line. We must still set scanline_offset
  9746. * to 1 so that the vblank timestamps come out correct when we query
  9747. * the scanline counter from within the vblank interrupt handler.
  9748. * However if queried just before the start of vblank we'll get an
  9749. * answer that's slightly in the future.
  9750. */
  9751. if (IS_GEN2(dev_priv)) {
  9752. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9753. int vtotal;
  9754. vtotal = adjusted_mode->crtc_vtotal;
  9755. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9756. vtotal /= 2;
  9757. crtc->scanline_offset = vtotal - 1;
  9758. } else if (HAS_DDI(dev_priv) &&
  9759. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9760. crtc->scanline_offset = 2;
  9761. } else
  9762. crtc->scanline_offset = 1;
  9763. }
  9764. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9765. {
  9766. struct drm_device *dev = state->dev;
  9767. struct drm_i915_private *dev_priv = to_i915(dev);
  9768. struct drm_crtc *crtc;
  9769. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9770. int i;
  9771. if (!dev_priv->display.crtc_compute_clock)
  9772. return;
  9773. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9775. struct intel_shared_dpll *old_dpll =
  9776. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9777. if (!needs_modeset(new_crtc_state))
  9778. continue;
  9779. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9780. if (!old_dpll)
  9781. continue;
  9782. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9783. }
  9784. }
  9785. /*
  9786. * This implements the workaround described in the "notes" section of the mode
  9787. * set sequence documentation. When going from no pipes or single pipe to
  9788. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9789. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9790. */
  9791. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9792. {
  9793. struct drm_crtc_state *crtc_state;
  9794. struct intel_crtc *intel_crtc;
  9795. struct drm_crtc *crtc;
  9796. struct intel_crtc_state *first_crtc_state = NULL;
  9797. struct intel_crtc_state *other_crtc_state = NULL;
  9798. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9799. int i;
  9800. /* look at all crtc's that are going to be enabled in during modeset */
  9801. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9802. intel_crtc = to_intel_crtc(crtc);
  9803. if (!crtc_state->active || !needs_modeset(crtc_state))
  9804. continue;
  9805. if (first_crtc_state) {
  9806. other_crtc_state = to_intel_crtc_state(crtc_state);
  9807. break;
  9808. } else {
  9809. first_crtc_state = to_intel_crtc_state(crtc_state);
  9810. first_pipe = intel_crtc->pipe;
  9811. }
  9812. }
  9813. /* No workaround needed? */
  9814. if (!first_crtc_state)
  9815. return 0;
  9816. /* w/a possibly needed, check how many crtc's are already enabled. */
  9817. for_each_intel_crtc(state->dev, intel_crtc) {
  9818. struct intel_crtc_state *pipe_config;
  9819. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9820. if (IS_ERR(pipe_config))
  9821. return PTR_ERR(pipe_config);
  9822. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9823. if (!pipe_config->base.active ||
  9824. needs_modeset(&pipe_config->base))
  9825. continue;
  9826. /* 2 or more enabled crtcs means no need for w/a */
  9827. if (enabled_pipe != INVALID_PIPE)
  9828. return 0;
  9829. enabled_pipe = intel_crtc->pipe;
  9830. }
  9831. if (enabled_pipe != INVALID_PIPE)
  9832. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9833. else if (other_crtc_state)
  9834. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9835. return 0;
  9836. }
  9837. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9838. {
  9839. struct drm_crtc *crtc;
  9840. /* Add all pipes to the state */
  9841. for_each_crtc(state->dev, crtc) {
  9842. struct drm_crtc_state *crtc_state;
  9843. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9844. if (IS_ERR(crtc_state))
  9845. return PTR_ERR(crtc_state);
  9846. }
  9847. return 0;
  9848. }
  9849. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9850. {
  9851. struct drm_crtc *crtc;
  9852. /*
  9853. * Add all pipes to the state, and force
  9854. * a modeset on all the active ones.
  9855. */
  9856. for_each_crtc(state->dev, crtc) {
  9857. struct drm_crtc_state *crtc_state;
  9858. int ret;
  9859. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9860. if (IS_ERR(crtc_state))
  9861. return PTR_ERR(crtc_state);
  9862. if (!crtc_state->active || needs_modeset(crtc_state))
  9863. continue;
  9864. crtc_state->mode_changed = true;
  9865. ret = drm_atomic_add_affected_connectors(state, crtc);
  9866. if (ret)
  9867. return ret;
  9868. ret = drm_atomic_add_affected_planes(state, crtc);
  9869. if (ret)
  9870. return ret;
  9871. }
  9872. return 0;
  9873. }
  9874. static int intel_modeset_checks(struct drm_atomic_state *state)
  9875. {
  9876. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9877. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9878. struct drm_crtc *crtc;
  9879. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9880. int ret = 0, i;
  9881. if (!check_digital_port_conflicts(state)) {
  9882. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9883. return -EINVAL;
  9884. }
  9885. intel_state->modeset = true;
  9886. intel_state->active_crtcs = dev_priv->active_crtcs;
  9887. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9888. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  9889. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9890. if (new_crtc_state->active)
  9891. intel_state->active_crtcs |= 1 << i;
  9892. else
  9893. intel_state->active_crtcs &= ~(1 << i);
  9894. if (old_crtc_state->active != new_crtc_state->active)
  9895. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  9896. }
  9897. /*
  9898. * See if the config requires any additional preparation, e.g.
  9899. * to adjust global state with pipes off. We need to do this
  9900. * here so we can get the modeset_pipe updated config for the new
  9901. * mode set on this crtc. For other crtcs we need to use the
  9902. * adjusted_mode bits in the crtc directly.
  9903. */
  9904. if (dev_priv->display.modeset_calc_cdclk) {
  9905. ret = dev_priv->display.modeset_calc_cdclk(state);
  9906. if (ret < 0)
  9907. return ret;
  9908. /*
  9909. * Writes to dev_priv->cdclk.logical must protected by
  9910. * holding all the crtc locks, even if we don't end up
  9911. * touching the hardware
  9912. */
  9913. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  9914. &intel_state->cdclk.logical)) {
  9915. ret = intel_lock_all_pipes(state);
  9916. if (ret < 0)
  9917. return ret;
  9918. }
  9919. /* All pipes must be switched off while we change the cdclk. */
  9920. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  9921. &intel_state->cdclk.actual)) {
  9922. ret = intel_modeset_all_pipes(state);
  9923. if (ret < 0)
  9924. return ret;
  9925. }
  9926. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  9927. intel_state->cdclk.logical.cdclk,
  9928. intel_state->cdclk.actual.cdclk);
  9929. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  9930. intel_state->cdclk.logical.voltage_level,
  9931. intel_state->cdclk.actual.voltage_level);
  9932. } else {
  9933. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  9934. }
  9935. intel_modeset_clear_plls(state);
  9936. if (IS_HASWELL(dev_priv))
  9937. return haswell_mode_set_planes_workaround(state);
  9938. return 0;
  9939. }
  9940. /*
  9941. * Handle calculation of various watermark data at the end of the atomic check
  9942. * phase. The code here should be run after the per-crtc and per-plane 'check'
  9943. * handlers to ensure that all derived state has been updated.
  9944. */
  9945. static int calc_watermark_data(struct drm_atomic_state *state)
  9946. {
  9947. struct drm_device *dev = state->dev;
  9948. struct drm_i915_private *dev_priv = to_i915(dev);
  9949. /* Is there platform-specific watermark information to calculate? */
  9950. if (dev_priv->display.compute_global_watermarks)
  9951. return dev_priv->display.compute_global_watermarks(state);
  9952. return 0;
  9953. }
  9954. /**
  9955. * intel_atomic_check - validate state object
  9956. * @dev: drm device
  9957. * @state: state to validate
  9958. */
  9959. static int intel_atomic_check(struct drm_device *dev,
  9960. struct drm_atomic_state *state)
  9961. {
  9962. struct drm_i915_private *dev_priv = to_i915(dev);
  9963. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9964. struct drm_crtc *crtc;
  9965. struct drm_crtc_state *old_crtc_state, *crtc_state;
  9966. int ret, i;
  9967. bool any_ms = false;
  9968. ret = drm_atomic_helper_check_modeset(dev, state);
  9969. if (ret)
  9970. return ret;
  9971. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  9972. struct intel_crtc_state *pipe_config =
  9973. to_intel_crtc_state(crtc_state);
  9974. /* Catch I915_MODE_FLAG_INHERITED */
  9975. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  9976. crtc_state->mode_changed = true;
  9977. if (!needs_modeset(crtc_state))
  9978. continue;
  9979. if (!crtc_state->enable) {
  9980. any_ms = true;
  9981. continue;
  9982. }
  9983. /* FIXME: For only active_changed we shouldn't need to do any
  9984. * state recomputation at all. */
  9985. ret = drm_atomic_add_affected_connectors(state, crtc);
  9986. if (ret)
  9987. return ret;
  9988. ret = intel_modeset_pipe_config(crtc, pipe_config);
  9989. if (ret) {
  9990. intel_dump_pipe_config(to_intel_crtc(crtc),
  9991. pipe_config, "[failed]");
  9992. return ret;
  9993. }
  9994. if (i915_modparams.fastboot &&
  9995. intel_pipe_config_compare(dev_priv,
  9996. to_intel_crtc_state(old_crtc_state),
  9997. pipe_config, true)) {
  9998. crtc_state->mode_changed = false;
  9999. pipe_config->update_pipe = true;
  10000. }
  10001. if (needs_modeset(crtc_state))
  10002. any_ms = true;
  10003. ret = drm_atomic_add_affected_planes(state, crtc);
  10004. if (ret)
  10005. return ret;
  10006. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10007. needs_modeset(crtc_state) ?
  10008. "[modeset]" : "[fastset]");
  10009. }
  10010. if (any_ms) {
  10011. ret = intel_modeset_checks(state);
  10012. if (ret)
  10013. return ret;
  10014. } else {
  10015. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10016. }
  10017. ret = drm_atomic_helper_check_planes(dev, state);
  10018. if (ret)
  10019. return ret;
  10020. intel_fbc_choose_crtc(dev_priv, intel_state);
  10021. return calc_watermark_data(state);
  10022. }
  10023. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10024. struct drm_atomic_state *state)
  10025. {
  10026. return drm_atomic_helper_prepare_planes(dev, state);
  10027. }
  10028. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10029. {
  10030. struct drm_device *dev = crtc->base.dev;
  10031. if (!dev->max_vblank_count)
  10032. return drm_crtc_accurate_vblank_count(&crtc->base);
  10033. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10034. }
  10035. static void intel_update_crtc(struct drm_crtc *crtc,
  10036. struct drm_atomic_state *state,
  10037. struct drm_crtc_state *old_crtc_state,
  10038. struct drm_crtc_state *new_crtc_state)
  10039. {
  10040. struct drm_device *dev = crtc->dev;
  10041. struct drm_i915_private *dev_priv = to_i915(dev);
  10042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10043. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10044. bool modeset = needs_modeset(new_crtc_state);
  10045. if (modeset) {
  10046. update_scanline_offset(intel_crtc);
  10047. dev_priv->display.crtc_enable(pipe_config, state);
  10048. } else {
  10049. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10050. pipe_config);
  10051. }
  10052. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10053. intel_fbc_enable(
  10054. intel_crtc, pipe_config,
  10055. to_intel_plane_state(crtc->primary->state));
  10056. }
  10057. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10058. }
  10059. static void intel_update_crtcs(struct drm_atomic_state *state)
  10060. {
  10061. struct drm_crtc *crtc;
  10062. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10063. int i;
  10064. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10065. if (!new_crtc_state->active)
  10066. continue;
  10067. intel_update_crtc(crtc, state, old_crtc_state,
  10068. new_crtc_state);
  10069. }
  10070. }
  10071. static void skl_update_crtcs(struct drm_atomic_state *state)
  10072. {
  10073. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10074. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10075. struct drm_crtc *crtc;
  10076. struct intel_crtc *intel_crtc;
  10077. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10078. struct intel_crtc_state *cstate;
  10079. unsigned int updated = 0;
  10080. bool progress;
  10081. enum pipe pipe;
  10082. int i;
  10083. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10084. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10085. /* ignore allocations for crtc's that have been turned off. */
  10086. if (new_crtc_state->active)
  10087. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10088. /*
  10089. * Whenever the number of active pipes changes, we need to make sure we
  10090. * update the pipes in the right order so that their ddb allocations
  10091. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10092. * cause pipe underruns and other bad stuff.
  10093. */
  10094. do {
  10095. progress = false;
  10096. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10097. bool vbl_wait = false;
  10098. unsigned int cmask = drm_crtc_mask(crtc);
  10099. intel_crtc = to_intel_crtc(crtc);
  10100. cstate = to_intel_crtc_state(new_crtc_state);
  10101. pipe = intel_crtc->pipe;
  10102. if (updated & cmask || !cstate->base.active)
  10103. continue;
  10104. if (skl_ddb_allocation_overlaps(dev_priv,
  10105. entries,
  10106. &cstate->wm.skl.ddb,
  10107. i))
  10108. continue;
  10109. updated |= cmask;
  10110. entries[i] = &cstate->wm.skl.ddb;
  10111. /*
  10112. * If this is an already active pipe, it's DDB changed,
  10113. * and this isn't the last pipe that needs updating
  10114. * then we need to wait for a vblank to pass for the
  10115. * new ddb allocation to take effect.
  10116. */
  10117. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10118. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10119. !new_crtc_state->active_changed &&
  10120. intel_state->wm_results.dirty_pipes != updated)
  10121. vbl_wait = true;
  10122. intel_update_crtc(crtc, state, old_crtc_state,
  10123. new_crtc_state);
  10124. if (vbl_wait)
  10125. intel_wait_for_vblank(dev_priv, pipe);
  10126. progress = true;
  10127. }
  10128. } while (progress);
  10129. }
  10130. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10131. {
  10132. struct intel_atomic_state *state, *next;
  10133. struct llist_node *freed;
  10134. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10135. llist_for_each_entry_safe(state, next, freed, freed)
  10136. drm_atomic_state_put(&state->base);
  10137. }
  10138. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10139. {
  10140. struct drm_i915_private *dev_priv =
  10141. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10142. intel_atomic_helper_free_state(dev_priv);
  10143. }
  10144. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10145. {
  10146. struct wait_queue_entry wait_fence, wait_reset;
  10147. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10148. init_wait_entry(&wait_fence, 0);
  10149. init_wait_entry(&wait_reset, 0);
  10150. for (;;) {
  10151. prepare_to_wait(&intel_state->commit_ready.wait,
  10152. &wait_fence, TASK_UNINTERRUPTIBLE);
  10153. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10154. &wait_reset, TASK_UNINTERRUPTIBLE);
  10155. if (i915_sw_fence_done(&intel_state->commit_ready)
  10156. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10157. break;
  10158. schedule();
  10159. }
  10160. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10161. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10162. }
  10163. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10164. {
  10165. struct drm_device *dev = state->dev;
  10166. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10167. struct drm_i915_private *dev_priv = to_i915(dev);
  10168. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10169. struct drm_crtc *crtc;
  10170. struct intel_crtc_state *intel_cstate;
  10171. u64 put_domains[I915_MAX_PIPES] = {};
  10172. int i;
  10173. intel_atomic_commit_fence_wait(intel_state);
  10174. drm_atomic_helper_wait_for_dependencies(state);
  10175. if (intel_state->modeset)
  10176. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10177. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10179. if (needs_modeset(new_crtc_state) ||
  10180. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10181. put_domains[to_intel_crtc(crtc)->pipe] =
  10182. modeset_get_crtc_power_domains(crtc,
  10183. to_intel_crtc_state(new_crtc_state));
  10184. }
  10185. if (!needs_modeset(new_crtc_state))
  10186. continue;
  10187. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10188. to_intel_crtc_state(new_crtc_state));
  10189. if (old_crtc_state->active) {
  10190. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10191. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10192. intel_crtc->active = false;
  10193. intel_fbc_disable(intel_crtc);
  10194. intel_disable_shared_dpll(intel_crtc);
  10195. /*
  10196. * Underruns don't always raise
  10197. * interrupts, so check manually.
  10198. */
  10199. intel_check_cpu_fifo_underruns(dev_priv);
  10200. intel_check_pch_fifo_underruns(dev_priv);
  10201. if (!new_crtc_state->active) {
  10202. /*
  10203. * Make sure we don't call initial_watermarks
  10204. * for ILK-style watermark updates.
  10205. *
  10206. * No clue what this is supposed to achieve.
  10207. */
  10208. if (INTEL_GEN(dev_priv) >= 9)
  10209. dev_priv->display.initial_watermarks(intel_state,
  10210. to_intel_crtc_state(new_crtc_state));
  10211. }
  10212. }
  10213. }
  10214. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10215. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10216. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10217. if (intel_state->modeset) {
  10218. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10219. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10220. /*
  10221. * SKL workaround: bspec recommends we disable the SAGV when we
  10222. * have more then one pipe enabled
  10223. */
  10224. if (!intel_can_enable_sagv(state))
  10225. intel_disable_sagv(dev_priv);
  10226. intel_modeset_verify_disabled(dev, state);
  10227. }
  10228. /* Complete the events for pipes that have now been disabled */
  10229. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10230. bool modeset = needs_modeset(new_crtc_state);
  10231. /* Complete events for now disable pipes here. */
  10232. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10233. spin_lock_irq(&dev->event_lock);
  10234. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10235. spin_unlock_irq(&dev->event_lock);
  10236. new_crtc_state->event = NULL;
  10237. }
  10238. }
  10239. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10240. dev_priv->display.update_crtcs(state);
  10241. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10242. * already, but still need the state for the delayed optimization. To
  10243. * fix this:
  10244. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10245. * - schedule that vblank worker _before_ calling hw_done
  10246. * - at the start of commit_tail, cancel it _synchrously
  10247. * - switch over to the vblank wait helper in the core after that since
  10248. * we don't need out special handling any more.
  10249. */
  10250. drm_atomic_helper_wait_for_flip_done(dev, state);
  10251. /*
  10252. * Now that the vblank has passed, we can go ahead and program the
  10253. * optimal watermarks on platforms that need two-step watermark
  10254. * programming.
  10255. *
  10256. * TODO: Move this (and other cleanup) to an async worker eventually.
  10257. */
  10258. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10259. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10260. if (dev_priv->display.optimize_watermarks)
  10261. dev_priv->display.optimize_watermarks(intel_state,
  10262. intel_cstate);
  10263. }
  10264. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10265. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10266. if (put_domains[i])
  10267. modeset_put_power_domains(dev_priv, put_domains[i]);
  10268. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10269. }
  10270. if (intel_state->modeset)
  10271. intel_verify_planes(intel_state);
  10272. if (intel_state->modeset && intel_can_enable_sagv(state))
  10273. intel_enable_sagv(dev_priv);
  10274. drm_atomic_helper_commit_hw_done(state);
  10275. if (intel_state->modeset) {
  10276. /* As one of the primary mmio accessors, KMS has a high
  10277. * likelihood of triggering bugs in unclaimed access. After we
  10278. * finish modesetting, see if an error has been flagged, and if
  10279. * so enable debugging for the next modeset - and hope we catch
  10280. * the culprit.
  10281. */
  10282. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10283. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10284. }
  10285. drm_atomic_helper_cleanup_planes(dev, state);
  10286. drm_atomic_helper_commit_cleanup_done(state);
  10287. drm_atomic_state_put(state);
  10288. intel_atomic_helper_free_state(dev_priv);
  10289. }
  10290. static void intel_atomic_commit_work(struct work_struct *work)
  10291. {
  10292. struct drm_atomic_state *state =
  10293. container_of(work, struct drm_atomic_state, commit_work);
  10294. intel_atomic_commit_tail(state);
  10295. }
  10296. static int __i915_sw_fence_call
  10297. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10298. enum i915_sw_fence_notify notify)
  10299. {
  10300. struct intel_atomic_state *state =
  10301. container_of(fence, struct intel_atomic_state, commit_ready);
  10302. switch (notify) {
  10303. case FENCE_COMPLETE:
  10304. /* we do blocking waits in the worker, nothing to do here */
  10305. break;
  10306. case FENCE_FREE:
  10307. {
  10308. struct intel_atomic_helper *helper =
  10309. &to_i915(state->base.dev)->atomic_helper;
  10310. if (llist_add(&state->freed, &helper->free_list))
  10311. schedule_work(&helper->free_work);
  10312. break;
  10313. }
  10314. }
  10315. return NOTIFY_DONE;
  10316. }
  10317. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10318. {
  10319. struct drm_plane_state *old_plane_state, *new_plane_state;
  10320. struct drm_plane *plane;
  10321. int i;
  10322. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10323. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10324. intel_fb_obj(new_plane_state->fb),
  10325. to_intel_plane(plane)->frontbuffer_bit);
  10326. }
  10327. /**
  10328. * intel_atomic_commit - commit validated state object
  10329. * @dev: DRM device
  10330. * @state: the top-level driver state object
  10331. * @nonblock: nonblocking commit
  10332. *
  10333. * This function commits a top-level state object that has been validated
  10334. * with drm_atomic_helper_check().
  10335. *
  10336. * RETURNS
  10337. * Zero for success or -errno.
  10338. */
  10339. static int intel_atomic_commit(struct drm_device *dev,
  10340. struct drm_atomic_state *state,
  10341. bool nonblock)
  10342. {
  10343. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10344. struct drm_i915_private *dev_priv = to_i915(dev);
  10345. int ret = 0;
  10346. drm_atomic_state_get(state);
  10347. i915_sw_fence_init(&intel_state->commit_ready,
  10348. intel_atomic_commit_ready);
  10349. /*
  10350. * The intel_legacy_cursor_update() fast path takes care
  10351. * of avoiding the vblank waits for simple cursor
  10352. * movement and flips. For cursor on/off and size changes,
  10353. * we want to perform the vblank waits so that watermark
  10354. * updates happen during the correct frames. Gen9+ have
  10355. * double buffered watermarks and so shouldn't need this.
  10356. *
  10357. * Unset state->legacy_cursor_update before the call to
  10358. * drm_atomic_helper_setup_commit() because otherwise
  10359. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10360. * we get FIFO underruns because we didn't wait
  10361. * for vblank.
  10362. *
  10363. * FIXME doing watermarks and fb cleanup from a vblank worker
  10364. * (assuming we had any) would solve these problems.
  10365. */
  10366. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10367. struct intel_crtc_state *new_crtc_state;
  10368. struct intel_crtc *crtc;
  10369. int i;
  10370. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10371. if (new_crtc_state->wm.need_postvbl_update ||
  10372. new_crtc_state->update_wm_post)
  10373. state->legacy_cursor_update = false;
  10374. }
  10375. ret = intel_atomic_prepare_commit(dev, state);
  10376. if (ret) {
  10377. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10378. i915_sw_fence_commit(&intel_state->commit_ready);
  10379. return ret;
  10380. }
  10381. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10382. if (!ret)
  10383. ret = drm_atomic_helper_swap_state(state, true);
  10384. if (ret) {
  10385. i915_sw_fence_commit(&intel_state->commit_ready);
  10386. drm_atomic_helper_cleanup_planes(dev, state);
  10387. return ret;
  10388. }
  10389. dev_priv->wm.distrust_bios_wm = false;
  10390. intel_shared_dpll_swap_state(state);
  10391. intel_atomic_track_fbs(state);
  10392. if (intel_state->modeset) {
  10393. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10394. sizeof(intel_state->min_cdclk));
  10395. memcpy(dev_priv->min_voltage_level,
  10396. intel_state->min_voltage_level,
  10397. sizeof(intel_state->min_voltage_level));
  10398. dev_priv->active_crtcs = intel_state->active_crtcs;
  10399. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10400. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10401. }
  10402. drm_atomic_state_get(state);
  10403. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10404. i915_sw_fence_commit(&intel_state->commit_ready);
  10405. if (nonblock && intel_state->modeset) {
  10406. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10407. } else if (nonblock) {
  10408. queue_work(system_unbound_wq, &state->commit_work);
  10409. } else {
  10410. if (intel_state->modeset)
  10411. flush_workqueue(dev_priv->modeset_wq);
  10412. intel_atomic_commit_tail(state);
  10413. }
  10414. return 0;
  10415. }
  10416. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10417. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10418. .set_config = drm_atomic_helper_set_config,
  10419. .destroy = intel_crtc_destroy,
  10420. .page_flip = drm_atomic_helper_page_flip,
  10421. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10422. .atomic_destroy_state = intel_crtc_destroy_state,
  10423. .set_crc_source = intel_crtc_set_crc_source,
  10424. };
  10425. struct wait_rps_boost {
  10426. struct wait_queue_entry wait;
  10427. struct drm_crtc *crtc;
  10428. struct drm_i915_gem_request *request;
  10429. };
  10430. static int do_rps_boost(struct wait_queue_entry *_wait,
  10431. unsigned mode, int sync, void *key)
  10432. {
  10433. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10434. struct drm_i915_gem_request *rq = wait->request;
  10435. gen6_rps_boost(rq, NULL);
  10436. i915_gem_request_put(rq);
  10437. drm_crtc_vblank_put(wait->crtc);
  10438. list_del(&wait->wait.entry);
  10439. kfree(wait);
  10440. return 1;
  10441. }
  10442. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10443. struct dma_fence *fence)
  10444. {
  10445. struct wait_rps_boost *wait;
  10446. if (!dma_fence_is_i915(fence))
  10447. return;
  10448. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10449. return;
  10450. if (drm_crtc_vblank_get(crtc))
  10451. return;
  10452. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10453. if (!wait) {
  10454. drm_crtc_vblank_put(crtc);
  10455. return;
  10456. }
  10457. wait->request = to_request(dma_fence_get(fence));
  10458. wait->crtc = crtc;
  10459. wait->wait.func = do_rps_boost;
  10460. wait->wait.flags = 0;
  10461. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10462. }
  10463. /**
  10464. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10465. * @plane: drm plane to prepare for
  10466. * @fb: framebuffer to prepare for presentation
  10467. *
  10468. * Prepares a framebuffer for usage on a display plane. Generally this
  10469. * involves pinning the underlying object and updating the frontbuffer tracking
  10470. * bits. Some older platforms need special physical address handling for
  10471. * cursor planes.
  10472. *
  10473. * Must be called with struct_mutex held.
  10474. *
  10475. * Returns 0 on success, negative error code on failure.
  10476. */
  10477. int
  10478. intel_prepare_plane_fb(struct drm_plane *plane,
  10479. struct drm_plane_state *new_state)
  10480. {
  10481. struct intel_atomic_state *intel_state =
  10482. to_intel_atomic_state(new_state->state);
  10483. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10484. struct drm_framebuffer *fb = new_state->fb;
  10485. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10486. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10487. int ret;
  10488. if (old_obj) {
  10489. struct drm_crtc_state *crtc_state =
  10490. drm_atomic_get_existing_crtc_state(new_state->state,
  10491. plane->state->crtc);
  10492. /* Big Hammer, we also need to ensure that any pending
  10493. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10494. * current scanout is retired before unpinning the old
  10495. * framebuffer. Note that we rely on userspace rendering
  10496. * into the buffer attached to the pipe they are waiting
  10497. * on. If not, userspace generates a GPU hang with IPEHR
  10498. * point to the MI_WAIT_FOR_EVENT.
  10499. *
  10500. * This should only fail upon a hung GPU, in which case we
  10501. * can safely continue.
  10502. */
  10503. if (needs_modeset(crtc_state)) {
  10504. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10505. old_obj->resv, NULL,
  10506. false, 0,
  10507. GFP_KERNEL);
  10508. if (ret < 0)
  10509. return ret;
  10510. }
  10511. }
  10512. if (new_state->fence) { /* explicit fencing */
  10513. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10514. new_state->fence,
  10515. I915_FENCE_TIMEOUT,
  10516. GFP_KERNEL);
  10517. if (ret < 0)
  10518. return ret;
  10519. }
  10520. if (!obj)
  10521. return 0;
  10522. ret = i915_gem_object_pin_pages(obj);
  10523. if (ret)
  10524. return ret;
  10525. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10526. if (ret) {
  10527. i915_gem_object_unpin_pages(obj);
  10528. return ret;
  10529. }
  10530. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10531. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10532. const int align = intel_cursor_alignment(dev_priv);
  10533. ret = i915_gem_object_attach_phys(obj, align);
  10534. } else {
  10535. struct i915_vma *vma;
  10536. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10537. if (!IS_ERR(vma))
  10538. to_intel_plane_state(new_state)->vma = vma;
  10539. else
  10540. ret = PTR_ERR(vma);
  10541. }
  10542. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10543. mutex_unlock(&dev_priv->drm.struct_mutex);
  10544. i915_gem_object_unpin_pages(obj);
  10545. if (ret)
  10546. return ret;
  10547. if (!new_state->fence) { /* implicit fencing */
  10548. struct dma_fence *fence;
  10549. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10550. obj->resv, NULL,
  10551. false, I915_FENCE_TIMEOUT,
  10552. GFP_KERNEL);
  10553. if (ret < 0)
  10554. return ret;
  10555. fence = reservation_object_get_excl_rcu(obj->resv);
  10556. if (fence) {
  10557. add_rps_boost_after_vblank(new_state->crtc, fence);
  10558. dma_fence_put(fence);
  10559. }
  10560. } else {
  10561. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10562. }
  10563. return 0;
  10564. }
  10565. /**
  10566. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10567. * @plane: drm plane to clean up for
  10568. * @fb: old framebuffer that was on plane
  10569. *
  10570. * Cleans up a framebuffer that has just been removed from a plane.
  10571. *
  10572. * Must be called with struct_mutex held.
  10573. */
  10574. void
  10575. intel_cleanup_plane_fb(struct drm_plane *plane,
  10576. struct drm_plane_state *old_state)
  10577. {
  10578. struct i915_vma *vma;
  10579. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10580. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10581. if (vma) {
  10582. mutex_lock(&plane->dev->struct_mutex);
  10583. intel_unpin_fb_vma(vma);
  10584. mutex_unlock(&plane->dev->struct_mutex);
  10585. }
  10586. }
  10587. int
  10588. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10589. {
  10590. struct drm_i915_private *dev_priv;
  10591. int max_scale;
  10592. int crtc_clock, max_dotclk;
  10593. if (!intel_crtc || !crtc_state->base.enable)
  10594. return DRM_PLANE_HELPER_NO_SCALING;
  10595. dev_priv = to_i915(intel_crtc->base.dev);
  10596. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10597. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10598. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10599. max_dotclk *= 2;
  10600. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10601. return DRM_PLANE_HELPER_NO_SCALING;
  10602. /*
  10603. * skl max scale is lower of:
  10604. * close to 3 but not 3, -1 is for that purpose
  10605. * or
  10606. * cdclk/crtc_clock
  10607. */
  10608. max_scale = min((1 << 16) * 3 - 1,
  10609. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10610. return max_scale;
  10611. }
  10612. static int
  10613. intel_check_primary_plane(struct intel_plane *plane,
  10614. struct intel_crtc_state *crtc_state,
  10615. struct intel_plane_state *state)
  10616. {
  10617. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10618. struct drm_crtc *crtc = state->base.crtc;
  10619. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10620. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10621. bool can_position = false;
  10622. int ret;
  10623. if (INTEL_GEN(dev_priv) >= 9) {
  10624. /* use scaler when colorkey is not required */
  10625. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10626. min_scale = 1;
  10627. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10628. }
  10629. can_position = true;
  10630. }
  10631. ret = drm_atomic_helper_check_plane_state(&state->base,
  10632. &crtc_state->base,
  10633. &state->clip,
  10634. min_scale, max_scale,
  10635. can_position, true);
  10636. if (ret)
  10637. return ret;
  10638. if (!state->base.fb)
  10639. return 0;
  10640. if (INTEL_GEN(dev_priv) >= 9) {
  10641. ret = skl_check_plane_surface(state);
  10642. if (ret)
  10643. return ret;
  10644. state->ctl = skl_plane_ctl(crtc_state, state);
  10645. } else {
  10646. ret = i9xx_check_plane_surface(state);
  10647. if (ret)
  10648. return ret;
  10649. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10650. }
  10651. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10652. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10653. return 0;
  10654. }
  10655. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10656. struct drm_crtc_state *old_crtc_state)
  10657. {
  10658. struct drm_device *dev = crtc->dev;
  10659. struct drm_i915_private *dev_priv = to_i915(dev);
  10660. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10661. struct intel_crtc_state *old_intel_cstate =
  10662. to_intel_crtc_state(old_crtc_state);
  10663. struct intel_atomic_state *old_intel_state =
  10664. to_intel_atomic_state(old_crtc_state->state);
  10665. struct intel_crtc_state *intel_cstate =
  10666. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10667. bool modeset = needs_modeset(&intel_cstate->base);
  10668. if (!modeset &&
  10669. (intel_cstate->base.color_mgmt_changed ||
  10670. intel_cstate->update_pipe)) {
  10671. intel_color_set_csc(&intel_cstate->base);
  10672. intel_color_load_luts(&intel_cstate->base);
  10673. }
  10674. /* Perform vblank evasion around commit operation */
  10675. intel_pipe_update_start(intel_cstate);
  10676. if (modeset)
  10677. goto out;
  10678. if (intel_cstate->update_pipe)
  10679. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  10680. else if (INTEL_GEN(dev_priv) >= 9)
  10681. skl_detach_scalers(intel_crtc);
  10682. out:
  10683. if (dev_priv->display.atomic_update_watermarks)
  10684. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10685. intel_cstate);
  10686. }
  10687. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10688. struct drm_crtc_state *old_crtc_state)
  10689. {
  10690. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10692. struct intel_atomic_state *old_intel_state =
  10693. to_intel_atomic_state(old_crtc_state->state);
  10694. struct intel_crtc_state *new_crtc_state =
  10695. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  10696. intel_pipe_update_end(new_crtc_state);
  10697. if (new_crtc_state->update_pipe &&
  10698. !needs_modeset(&new_crtc_state->base) &&
  10699. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED) {
  10700. if (!IS_GEN2(dev_priv))
  10701. intel_set_cpu_fifo_underrun_reporting(dev_priv, intel_crtc->pipe, true);
  10702. if (new_crtc_state->has_pch_encoder) {
  10703. enum pipe pch_transcoder =
  10704. intel_crtc_pch_transcoder(intel_crtc);
  10705. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  10706. }
  10707. }
  10708. }
  10709. /**
  10710. * intel_plane_destroy - destroy a plane
  10711. * @plane: plane to destroy
  10712. *
  10713. * Common destruction function for all types of planes (primary, cursor,
  10714. * sprite).
  10715. */
  10716. void intel_plane_destroy(struct drm_plane *plane)
  10717. {
  10718. drm_plane_cleanup(plane);
  10719. kfree(to_intel_plane(plane));
  10720. }
  10721. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10722. {
  10723. switch (format) {
  10724. case DRM_FORMAT_C8:
  10725. case DRM_FORMAT_RGB565:
  10726. case DRM_FORMAT_XRGB1555:
  10727. case DRM_FORMAT_XRGB8888:
  10728. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10729. modifier == I915_FORMAT_MOD_X_TILED;
  10730. default:
  10731. return false;
  10732. }
  10733. }
  10734. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10735. {
  10736. switch (format) {
  10737. case DRM_FORMAT_C8:
  10738. case DRM_FORMAT_RGB565:
  10739. case DRM_FORMAT_XRGB8888:
  10740. case DRM_FORMAT_XBGR8888:
  10741. case DRM_FORMAT_XRGB2101010:
  10742. case DRM_FORMAT_XBGR2101010:
  10743. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10744. modifier == I915_FORMAT_MOD_X_TILED;
  10745. default:
  10746. return false;
  10747. }
  10748. }
  10749. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10750. {
  10751. switch (format) {
  10752. case DRM_FORMAT_XRGB8888:
  10753. case DRM_FORMAT_XBGR8888:
  10754. case DRM_FORMAT_ARGB8888:
  10755. case DRM_FORMAT_ABGR8888:
  10756. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10757. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10758. return true;
  10759. /* fall through */
  10760. case DRM_FORMAT_RGB565:
  10761. case DRM_FORMAT_XRGB2101010:
  10762. case DRM_FORMAT_XBGR2101010:
  10763. case DRM_FORMAT_YUYV:
  10764. case DRM_FORMAT_YVYU:
  10765. case DRM_FORMAT_UYVY:
  10766. case DRM_FORMAT_VYUY:
  10767. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10768. return true;
  10769. /* fall through */
  10770. case DRM_FORMAT_C8:
  10771. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10772. modifier == I915_FORMAT_MOD_X_TILED ||
  10773. modifier == I915_FORMAT_MOD_Y_TILED)
  10774. return true;
  10775. /* fall through */
  10776. default:
  10777. return false;
  10778. }
  10779. }
  10780. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10781. uint32_t format,
  10782. uint64_t modifier)
  10783. {
  10784. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10785. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10786. return false;
  10787. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10788. modifier != DRM_FORMAT_MOD_LINEAR)
  10789. return false;
  10790. if (INTEL_GEN(dev_priv) >= 9)
  10791. return skl_mod_supported(format, modifier);
  10792. else if (INTEL_GEN(dev_priv) >= 4)
  10793. return i965_mod_supported(format, modifier);
  10794. else
  10795. return i8xx_mod_supported(format, modifier);
  10796. unreachable();
  10797. }
  10798. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10799. uint32_t format,
  10800. uint64_t modifier)
  10801. {
  10802. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10803. return false;
  10804. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10805. }
  10806. static struct drm_plane_funcs intel_plane_funcs = {
  10807. .update_plane = drm_atomic_helper_update_plane,
  10808. .disable_plane = drm_atomic_helper_disable_plane,
  10809. .destroy = intel_plane_destroy,
  10810. .atomic_get_property = intel_plane_atomic_get_property,
  10811. .atomic_set_property = intel_plane_atomic_set_property,
  10812. .atomic_duplicate_state = intel_plane_duplicate_state,
  10813. .atomic_destroy_state = intel_plane_destroy_state,
  10814. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10815. };
  10816. static int
  10817. intel_legacy_cursor_update(struct drm_plane *plane,
  10818. struct drm_crtc *crtc,
  10819. struct drm_framebuffer *fb,
  10820. int crtc_x, int crtc_y,
  10821. unsigned int crtc_w, unsigned int crtc_h,
  10822. uint32_t src_x, uint32_t src_y,
  10823. uint32_t src_w, uint32_t src_h,
  10824. struct drm_modeset_acquire_ctx *ctx)
  10825. {
  10826. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10827. int ret;
  10828. struct drm_plane_state *old_plane_state, *new_plane_state;
  10829. struct intel_plane *intel_plane = to_intel_plane(plane);
  10830. struct drm_framebuffer *old_fb;
  10831. struct drm_crtc_state *crtc_state = crtc->state;
  10832. struct i915_vma *old_vma, *vma;
  10833. /*
  10834. * When crtc is inactive or there is a modeset pending,
  10835. * wait for it to complete in the slowpath
  10836. */
  10837. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10838. to_intel_crtc_state(crtc_state)->update_pipe)
  10839. goto slow;
  10840. old_plane_state = plane->state;
  10841. /*
  10842. * Don't do an async update if there is an outstanding commit modifying
  10843. * the plane. This prevents our async update's changes from getting
  10844. * overridden by a previous synchronous update's state.
  10845. */
  10846. if (old_plane_state->commit &&
  10847. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  10848. goto slow;
  10849. /*
  10850. * If any parameters change that may affect watermarks,
  10851. * take the slowpath. Only changing fb or position should be
  10852. * in the fastpath.
  10853. */
  10854. if (old_plane_state->crtc != crtc ||
  10855. old_plane_state->src_w != src_w ||
  10856. old_plane_state->src_h != src_h ||
  10857. old_plane_state->crtc_w != crtc_w ||
  10858. old_plane_state->crtc_h != crtc_h ||
  10859. !old_plane_state->fb != !fb)
  10860. goto slow;
  10861. new_plane_state = intel_plane_duplicate_state(plane);
  10862. if (!new_plane_state)
  10863. return -ENOMEM;
  10864. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10865. new_plane_state->src_x = src_x;
  10866. new_plane_state->src_y = src_y;
  10867. new_plane_state->src_w = src_w;
  10868. new_plane_state->src_h = src_h;
  10869. new_plane_state->crtc_x = crtc_x;
  10870. new_plane_state->crtc_y = crtc_y;
  10871. new_plane_state->crtc_w = crtc_w;
  10872. new_plane_state->crtc_h = crtc_h;
  10873. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10874. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  10875. to_intel_plane_state(plane->state),
  10876. to_intel_plane_state(new_plane_state));
  10877. if (ret)
  10878. goto out_free;
  10879. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10880. if (ret)
  10881. goto out_free;
  10882. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10883. int align = intel_cursor_alignment(dev_priv);
  10884. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10885. if (ret) {
  10886. DRM_DEBUG_KMS("failed to attach phys object\n");
  10887. goto out_unlock;
  10888. }
  10889. } else {
  10890. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10891. if (IS_ERR(vma)) {
  10892. DRM_DEBUG_KMS("failed to pin object\n");
  10893. ret = PTR_ERR(vma);
  10894. goto out_unlock;
  10895. }
  10896. to_intel_plane_state(new_plane_state)->vma = vma;
  10897. }
  10898. old_fb = old_plane_state->fb;
  10899. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10900. intel_plane->frontbuffer_bit);
  10901. /* Swap plane state */
  10902. plane->state = new_plane_state;
  10903. if (plane->state->visible) {
  10904. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10905. intel_plane->update_plane(intel_plane,
  10906. to_intel_crtc_state(crtc->state),
  10907. to_intel_plane_state(plane->state));
  10908. } else {
  10909. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10910. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10911. }
  10912. old_vma = fetch_and_zero(&to_intel_plane_state(old_plane_state)->vma);
  10913. if (old_vma)
  10914. intel_unpin_fb_vma(old_vma);
  10915. out_unlock:
  10916. mutex_unlock(&dev_priv->drm.struct_mutex);
  10917. out_free:
  10918. if (ret)
  10919. intel_plane_destroy_state(plane, new_plane_state);
  10920. else
  10921. intel_plane_destroy_state(plane, old_plane_state);
  10922. return ret;
  10923. slow:
  10924. return drm_atomic_helper_update_plane(plane, crtc, fb,
  10925. crtc_x, crtc_y, crtc_w, crtc_h,
  10926. src_x, src_y, src_w, src_h, ctx);
  10927. }
  10928. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10929. .update_plane = intel_legacy_cursor_update,
  10930. .disable_plane = drm_atomic_helper_disable_plane,
  10931. .destroy = intel_plane_destroy,
  10932. .atomic_get_property = intel_plane_atomic_get_property,
  10933. .atomic_set_property = intel_plane_atomic_set_property,
  10934. .atomic_duplicate_state = intel_plane_duplicate_state,
  10935. .atomic_destroy_state = intel_plane_destroy_state,
  10936. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  10937. };
  10938. static struct intel_plane *
  10939. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  10940. {
  10941. struct intel_plane *primary = NULL;
  10942. struct intel_plane_state *state = NULL;
  10943. const uint32_t *intel_primary_formats;
  10944. unsigned int supported_rotations;
  10945. unsigned int num_formats;
  10946. const uint64_t *modifiers;
  10947. int ret;
  10948. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10949. if (!primary) {
  10950. ret = -ENOMEM;
  10951. goto fail;
  10952. }
  10953. state = intel_create_plane_state(&primary->base);
  10954. if (!state) {
  10955. ret = -ENOMEM;
  10956. goto fail;
  10957. }
  10958. primary->base.state = &state->base;
  10959. primary->can_scale = false;
  10960. primary->max_downscale = 1;
  10961. if (INTEL_GEN(dev_priv) >= 9) {
  10962. primary->can_scale = true;
  10963. state->scaler_id = -1;
  10964. }
  10965. primary->pipe = pipe;
  10966. /*
  10967. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  10968. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  10969. */
  10970. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  10971. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  10972. else
  10973. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  10974. primary->id = PLANE_PRIMARY;
  10975. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10976. primary->check_plane = intel_check_primary_plane;
  10977. if (INTEL_GEN(dev_priv) >= 10) {
  10978. intel_primary_formats = skl_primary_formats;
  10979. num_formats = ARRAY_SIZE(skl_primary_formats);
  10980. modifiers = skl_format_modifiers_ccs;
  10981. primary->update_plane = skl_update_plane;
  10982. primary->disable_plane = skl_disable_plane;
  10983. primary->get_hw_state = skl_plane_get_hw_state;
  10984. } else if (INTEL_GEN(dev_priv) >= 9) {
  10985. intel_primary_formats = skl_primary_formats;
  10986. num_formats = ARRAY_SIZE(skl_primary_formats);
  10987. if (pipe < PIPE_C)
  10988. modifiers = skl_format_modifiers_ccs;
  10989. else
  10990. modifiers = skl_format_modifiers_noccs;
  10991. primary->update_plane = skl_update_plane;
  10992. primary->disable_plane = skl_disable_plane;
  10993. primary->get_hw_state = skl_plane_get_hw_state;
  10994. } else if (INTEL_GEN(dev_priv) >= 4) {
  10995. intel_primary_formats = i965_primary_formats;
  10996. num_formats = ARRAY_SIZE(i965_primary_formats);
  10997. modifiers = i9xx_format_modifiers;
  10998. primary->update_plane = i9xx_update_plane;
  10999. primary->disable_plane = i9xx_disable_plane;
  11000. primary->get_hw_state = i9xx_plane_get_hw_state;
  11001. } else {
  11002. intel_primary_formats = i8xx_primary_formats;
  11003. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11004. modifiers = i9xx_format_modifiers;
  11005. primary->update_plane = i9xx_update_plane;
  11006. primary->disable_plane = i9xx_disable_plane;
  11007. primary->get_hw_state = i9xx_plane_get_hw_state;
  11008. }
  11009. if (INTEL_GEN(dev_priv) >= 9)
  11010. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11011. 0, &intel_plane_funcs,
  11012. intel_primary_formats, num_formats,
  11013. modifiers,
  11014. DRM_PLANE_TYPE_PRIMARY,
  11015. "plane 1%c", pipe_name(pipe));
  11016. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11017. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11018. 0, &intel_plane_funcs,
  11019. intel_primary_formats, num_formats,
  11020. modifiers,
  11021. DRM_PLANE_TYPE_PRIMARY,
  11022. "primary %c", pipe_name(pipe));
  11023. else
  11024. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11025. 0, &intel_plane_funcs,
  11026. intel_primary_formats, num_formats,
  11027. modifiers,
  11028. DRM_PLANE_TYPE_PRIMARY,
  11029. "plane %c",
  11030. plane_name(primary->i9xx_plane));
  11031. if (ret)
  11032. goto fail;
  11033. if (INTEL_GEN(dev_priv) >= 10) {
  11034. supported_rotations =
  11035. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11036. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11037. DRM_MODE_REFLECT_X;
  11038. } else if (INTEL_GEN(dev_priv) >= 9) {
  11039. supported_rotations =
  11040. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11041. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11042. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11043. supported_rotations =
  11044. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11045. DRM_MODE_REFLECT_X;
  11046. } else if (INTEL_GEN(dev_priv) >= 4) {
  11047. supported_rotations =
  11048. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11049. } else {
  11050. supported_rotations = DRM_MODE_ROTATE_0;
  11051. }
  11052. if (INTEL_GEN(dev_priv) >= 4)
  11053. drm_plane_create_rotation_property(&primary->base,
  11054. DRM_MODE_ROTATE_0,
  11055. supported_rotations);
  11056. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11057. return primary;
  11058. fail:
  11059. kfree(state);
  11060. kfree(primary);
  11061. return ERR_PTR(ret);
  11062. }
  11063. static struct intel_plane *
  11064. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11065. enum pipe pipe)
  11066. {
  11067. struct intel_plane *cursor = NULL;
  11068. struct intel_plane_state *state = NULL;
  11069. int ret;
  11070. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11071. if (!cursor) {
  11072. ret = -ENOMEM;
  11073. goto fail;
  11074. }
  11075. state = intel_create_plane_state(&cursor->base);
  11076. if (!state) {
  11077. ret = -ENOMEM;
  11078. goto fail;
  11079. }
  11080. cursor->base.state = &state->base;
  11081. cursor->can_scale = false;
  11082. cursor->max_downscale = 1;
  11083. cursor->pipe = pipe;
  11084. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11085. cursor->id = PLANE_CURSOR;
  11086. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11087. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11088. cursor->update_plane = i845_update_cursor;
  11089. cursor->disable_plane = i845_disable_cursor;
  11090. cursor->get_hw_state = i845_cursor_get_hw_state;
  11091. cursor->check_plane = i845_check_cursor;
  11092. } else {
  11093. cursor->update_plane = i9xx_update_cursor;
  11094. cursor->disable_plane = i9xx_disable_cursor;
  11095. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11096. cursor->check_plane = i9xx_check_cursor;
  11097. }
  11098. cursor->cursor.base = ~0;
  11099. cursor->cursor.cntl = ~0;
  11100. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11101. cursor->cursor.size = ~0;
  11102. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11103. 0, &intel_cursor_plane_funcs,
  11104. intel_cursor_formats,
  11105. ARRAY_SIZE(intel_cursor_formats),
  11106. cursor_format_modifiers,
  11107. DRM_PLANE_TYPE_CURSOR,
  11108. "cursor %c", pipe_name(pipe));
  11109. if (ret)
  11110. goto fail;
  11111. if (INTEL_GEN(dev_priv) >= 4)
  11112. drm_plane_create_rotation_property(&cursor->base,
  11113. DRM_MODE_ROTATE_0,
  11114. DRM_MODE_ROTATE_0 |
  11115. DRM_MODE_ROTATE_180);
  11116. if (INTEL_GEN(dev_priv) >= 9)
  11117. state->scaler_id = -1;
  11118. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11119. return cursor;
  11120. fail:
  11121. kfree(state);
  11122. kfree(cursor);
  11123. return ERR_PTR(ret);
  11124. }
  11125. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11126. struct intel_crtc_state *crtc_state)
  11127. {
  11128. struct intel_crtc_scaler_state *scaler_state =
  11129. &crtc_state->scaler_state;
  11130. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11131. int i;
  11132. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11133. if (!crtc->num_scalers)
  11134. return;
  11135. for (i = 0; i < crtc->num_scalers; i++) {
  11136. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11137. scaler->in_use = 0;
  11138. scaler->mode = PS_SCALER_MODE_DYN;
  11139. }
  11140. scaler_state->scaler_id = -1;
  11141. }
  11142. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11143. {
  11144. struct intel_crtc *intel_crtc;
  11145. struct intel_crtc_state *crtc_state = NULL;
  11146. struct intel_plane *primary = NULL;
  11147. struct intel_plane *cursor = NULL;
  11148. int sprite, ret;
  11149. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11150. if (!intel_crtc)
  11151. return -ENOMEM;
  11152. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11153. if (!crtc_state) {
  11154. ret = -ENOMEM;
  11155. goto fail;
  11156. }
  11157. intel_crtc->config = crtc_state;
  11158. intel_crtc->base.state = &crtc_state->base;
  11159. crtc_state->base.crtc = &intel_crtc->base;
  11160. primary = intel_primary_plane_create(dev_priv, pipe);
  11161. if (IS_ERR(primary)) {
  11162. ret = PTR_ERR(primary);
  11163. goto fail;
  11164. }
  11165. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11166. for_each_sprite(dev_priv, pipe, sprite) {
  11167. struct intel_plane *plane;
  11168. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11169. if (IS_ERR(plane)) {
  11170. ret = PTR_ERR(plane);
  11171. goto fail;
  11172. }
  11173. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11174. }
  11175. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11176. if (IS_ERR(cursor)) {
  11177. ret = PTR_ERR(cursor);
  11178. goto fail;
  11179. }
  11180. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11181. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11182. &primary->base, &cursor->base,
  11183. &intel_crtc_funcs,
  11184. "pipe %c", pipe_name(pipe));
  11185. if (ret)
  11186. goto fail;
  11187. intel_crtc->pipe = pipe;
  11188. /* initialize shared scalers */
  11189. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11190. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11191. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] != NULL);
  11192. dev_priv->plane_to_crtc_mapping[primary->i9xx_plane] = intel_crtc;
  11193. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11194. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11195. intel_color_init(&intel_crtc->base);
  11196. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11197. return 0;
  11198. fail:
  11199. /*
  11200. * drm_mode_config_cleanup() will free up any
  11201. * crtcs/planes already initialized.
  11202. */
  11203. kfree(crtc_state);
  11204. kfree(intel_crtc);
  11205. return ret;
  11206. }
  11207. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11208. {
  11209. struct drm_device *dev = connector->base.dev;
  11210. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11211. if (!connector->base.state->crtc)
  11212. return INVALID_PIPE;
  11213. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11214. }
  11215. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11216. struct drm_file *file)
  11217. {
  11218. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11219. struct drm_crtc *drmmode_crtc;
  11220. struct intel_crtc *crtc;
  11221. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11222. if (!drmmode_crtc)
  11223. return -ENOENT;
  11224. crtc = to_intel_crtc(drmmode_crtc);
  11225. pipe_from_crtc_id->pipe = crtc->pipe;
  11226. return 0;
  11227. }
  11228. static int intel_encoder_clones(struct intel_encoder *encoder)
  11229. {
  11230. struct drm_device *dev = encoder->base.dev;
  11231. struct intel_encoder *source_encoder;
  11232. int index_mask = 0;
  11233. int entry = 0;
  11234. for_each_intel_encoder(dev, source_encoder) {
  11235. if (encoders_cloneable(encoder, source_encoder))
  11236. index_mask |= (1 << entry);
  11237. entry++;
  11238. }
  11239. return index_mask;
  11240. }
  11241. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11242. {
  11243. if (!IS_MOBILE(dev_priv))
  11244. return false;
  11245. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11246. return false;
  11247. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11248. return false;
  11249. return true;
  11250. }
  11251. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11252. {
  11253. if (INTEL_GEN(dev_priv) >= 9)
  11254. return false;
  11255. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11256. return false;
  11257. if (IS_CHERRYVIEW(dev_priv))
  11258. return false;
  11259. if (HAS_PCH_LPT_H(dev_priv) &&
  11260. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11261. return false;
  11262. /* DDI E can't be used if DDI A requires 4 lanes */
  11263. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11264. return false;
  11265. if (!dev_priv->vbt.int_crt_support)
  11266. return false;
  11267. return true;
  11268. }
  11269. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11270. {
  11271. int pps_num;
  11272. int pps_idx;
  11273. if (HAS_DDI(dev_priv))
  11274. return;
  11275. /*
  11276. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11277. * everywhere where registers can be write protected.
  11278. */
  11279. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11280. pps_num = 2;
  11281. else
  11282. pps_num = 1;
  11283. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11284. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11285. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11286. I915_WRITE(PP_CONTROL(pps_idx), val);
  11287. }
  11288. }
  11289. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11290. {
  11291. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11292. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11293. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11294. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11295. else
  11296. dev_priv->pps_mmio_base = PPS_BASE;
  11297. intel_pps_unlock_regs_wa(dev_priv);
  11298. }
  11299. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11300. {
  11301. struct intel_encoder *encoder;
  11302. bool dpd_is_edp = false;
  11303. intel_pps_init(dev_priv);
  11304. /*
  11305. * intel_edp_init_connector() depends on this completing first, to
  11306. * prevent the registeration of both eDP and LVDS and the incorrect
  11307. * sharing of the PPS.
  11308. */
  11309. intel_lvds_init(dev_priv);
  11310. if (intel_crt_present(dev_priv))
  11311. intel_crt_init(dev_priv);
  11312. if (IS_GEN9_LP(dev_priv)) {
  11313. /*
  11314. * FIXME: Broxton doesn't support port detection via the
  11315. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11316. * detect the ports.
  11317. */
  11318. intel_ddi_init(dev_priv, PORT_A);
  11319. intel_ddi_init(dev_priv, PORT_B);
  11320. intel_ddi_init(dev_priv, PORT_C);
  11321. intel_dsi_init(dev_priv);
  11322. } else if (HAS_DDI(dev_priv)) {
  11323. int found;
  11324. /*
  11325. * Haswell uses DDI functions to detect digital outputs.
  11326. * On SKL pre-D0 the strap isn't connected, so we assume
  11327. * it's there.
  11328. */
  11329. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11330. /* WaIgnoreDDIAStrap: skl */
  11331. if (found || IS_GEN9_BC(dev_priv))
  11332. intel_ddi_init(dev_priv, PORT_A);
  11333. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11334. * register */
  11335. found = I915_READ(SFUSE_STRAP);
  11336. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11337. intel_ddi_init(dev_priv, PORT_B);
  11338. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11339. intel_ddi_init(dev_priv, PORT_C);
  11340. if (found & SFUSE_STRAP_DDID_DETECTED)
  11341. intel_ddi_init(dev_priv, PORT_D);
  11342. /*
  11343. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11344. */
  11345. if (IS_GEN9_BC(dev_priv) &&
  11346. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11347. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11348. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11349. intel_ddi_init(dev_priv, PORT_E);
  11350. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11351. int found;
  11352. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11353. if (has_edp_a(dev_priv))
  11354. intel_dp_init(dev_priv, DP_A, PORT_A);
  11355. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11356. /* PCH SDVOB multiplex with HDMIB */
  11357. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11358. if (!found)
  11359. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11360. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11361. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11362. }
  11363. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11364. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11365. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11366. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11367. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11368. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11369. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11370. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11371. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11372. bool has_edp, has_port;
  11373. /*
  11374. * The DP_DETECTED bit is the latched state of the DDC
  11375. * SDA pin at boot. However since eDP doesn't require DDC
  11376. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11377. * eDP ports may have been muxed to an alternate function.
  11378. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11379. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11380. * detect eDP ports.
  11381. *
  11382. * Sadly the straps seem to be missing sometimes even for HDMI
  11383. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11384. * and VBT for the presence of the port. Additionally we can't
  11385. * trust the port type the VBT declares as we've seen at least
  11386. * HDMI ports that the VBT claim are DP or eDP.
  11387. */
  11388. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11389. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11390. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11391. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11392. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11393. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11394. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11395. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11396. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11397. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11398. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11399. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11400. if (IS_CHERRYVIEW(dev_priv)) {
  11401. /*
  11402. * eDP not supported on port D,
  11403. * so no need to worry about it
  11404. */
  11405. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11406. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11407. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11408. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11409. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11410. }
  11411. intel_dsi_init(dev_priv);
  11412. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11413. bool found = false;
  11414. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11415. DRM_DEBUG_KMS("probing SDVOB\n");
  11416. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11417. if (!found && IS_G4X(dev_priv)) {
  11418. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11419. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11420. }
  11421. if (!found && IS_G4X(dev_priv))
  11422. intel_dp_init(dev_priv, DP_B, PORT_B);
  11423. }
  11424. /* Before G4X SDVOC doesn't have its own detect register */
  11425. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11426. DRM_DEBUG_KMS("probing SDVOC\n");
  11427. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11428. }
  11429. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11430. if (IS_G4X(dev_priv)) {
  11431. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11432. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11433. }
  11434. if (IS_G4X(dev_priv))
  11435. intel_dp_init(dev_priv, DP_C, PORT_C);
  11436. }
  11437. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11438. intel_dp_init(dev_priv, DP_D, PORT_D);
  11439. } else if (IS_GEN2(dev_priv))
  11440. intel_dvo_init(dev_priv);
  11441. if (SUPPORTS_TV(dev_priv))
  11442. intel_tv_init(dev_priv);
  11443. intel_psr_init(dev_priv);
  11444. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11445. encoder->base.possible_crtcs = encoder->crtc_mask;
  11446. encoder->base.possible_clones =
  11447. intel_encoder_clones(encoder);
  11448. }
  11449. intel_init_pch_refclk(dev_priv);
  11450. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11451. }
  11452. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11453. {
  11454. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11455. drm_framebuffer_cleanup(fb);
  11456. i915_gem_object_lock(intel_fb->obj);
  11457. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11458. i915_gem_object_unlock(intel_fb->obj);
  11459. i915_gem_object_put(intel_fb->obj);
  11460. kfree(intel_fb);
  11461. }
  11462. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11463. struct drm_file *file,
  11464. unsigned int *handle)
  11465. {
  11466. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11467. struct drm_i915_gem_object *obj = intel_fb->obj;
  11468. if (obj->userptr.mm) {
  11469. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11470. return -EINVAL;
  11471. }
  11472. return drm_gem_handle_create(file, &obj->base, handle);
  11473. }
  11474. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11475. struct drm_file *file,
  11476. unsigned flags, unsigned color,
  11477. struct drm_clip_rect *clips,
  11478. unsigned num_clips)
  11479. {
  11480. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11481. i915_gem_object_flush_if_display(obj);
  11482. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11483. return 0;
  11484. }
  11485. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11486. .destroy = intel_user_framebuffer_destroy,
  11487. .create_handle = intel_user_framebuffer_create_handle,
  11488. .dirty = intel_user_framebuffer_dirty,
  11489. };
  11490. static
  11491. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11492. uint64_t fb_modifier, uint32_t pixel_format)
  11493. {
  11494. u32 gen = INTEL_GEN(dev_priv);
  11495. if (gen >= 9) {
  11496. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11497. /* "The stride in bytes must not exceed the of the size of 8K
  11498. * pixels and 32K bytes."
  11499. */
  11500. return min(8192 * cpp, 32768);
  11501. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11502. return 32*1024;
  11503. } else if (gen >= 4) {
  11504. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11505. return 16*1024;
  11506. else
  11507. return 32*1024;
  11508. } else if (gen >= 3) {
  11509. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11510. return 8*1024;
  11511. else
  11512. return 16*1024;
  11513. } else {
  11514. /* XXX DSPC is limited to 4k tiled */
  11515. return 8*1024;
  11516. }
  11517. }
  11518. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11519. struct drm_i915_gem_object *obj,
  11520. struct drm_mode_fb_cmd2 *mode_cmd)
  11521. {
  11522. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11523. struct drm_framebuffer *fb = &intel_fb->base;
  11524. struct drm_format_name_buf format_name;
  11525. u32 pitch_limit;
  11526. unsigned int tiling, stride;
  11527. int ret = -EINVAL;
  11528. int i;
  11529. i915_gem_object_lock(obj);
  11530. obj->framebuffer_references++;
  11531. tiling = i915_gem_object_get_tiling(obj);
  11532. stride = i915_gem_object_get_stride(obj);
  11533. i915_gem_object_unlock(obj);
  11534. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11535. /*
  11536. * If there's a fence, enforce that
  11537. * the fb modifier and tiling mode match.
  11538. */
  11539. if (tiling != I915_TILING_NONE &&
  11540. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11541. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11542. goto err;
  11543. }
  11544. } else {
  11545. if (tiling == I915_TILING_X) {
  11546. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11547. } else if (tiling == I915_TILING_Y) {
  11548. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11549. goto err;
  11550. }
  11551. }
  11552. /* Passed in modifier sanity checking. */
  11553. switch (mode_cmd->modifier[0]) {
  11554. case I915_FORMAT_MOD_Y_TILED_CCS:
  11555. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11556. switch (mode_cmd->pixel_format) {
  11557. case DRM_FORMAT_XBGR8888:
  11558. case DRM_FORMAT_ABGR8888:
  11559. case DRM_FORMAT_XRGB8888:
  11560. case DRM_FORMAT_ARGB8888:
  11561. break;
  11562. default:
  11563. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11564. goto err;
  11565. }
  11566. /* fall through */
  11567. case I915_FORMAT_MOD_Y_TILED:
  11568. case I915_FORMAT_MOD_Yf_TILED:
  11569. if (INTEL_GEN(dev_priv) < 9) {
  11570. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11571. mode_cmd->modifier[0]);
  11572. goto err;
  11573. }
  11574. case DRM_FORMAT_MOD_LINEAR:
  11575. case I915_FORMAT_MOD_X_TILED:
  11576. break;
  11577. default:
  11578. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11579. mode_cmd->modifier[0]);
  11580. goto err;
  11581. }
  11582. /*
  11583. * gen2/3 display engine uses the fence if present,
  11584. * so the tiling mode must match the fb modifier exactly.
  11585. */
  11586. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11587. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11588. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11589. goto err;
  11590. }
  11591. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11592. mode_cmd->pixel_format);
  11593. if (mode_cmd->pitches[0] > pitch_limit) {
  11594. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11595. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11596. "tiled" : "linear",
  11597. mode_cmd->pitches[0], pitch_limit);
  11598. goto err;
  11599. }
  11600. /*
  11601. * If there's a fence, enforce that
  11602. * the fb pitch and fence stride match.
  11603. */
  11604. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11605. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11606. mode_cmd->pitches[0], stride);
  11607. goto err;
  11608. }
  11609. /* Reject formats not supported by any plane early. */
  11610. switch (mode_cmd->pixel_format) {
  11611. case DRM_FORMAT_C8:
  11612. case DRM_FORMAT_RGB565:
  11613. case DRM_FORMAT_XRGB8888:
  11614. case DRM_FORMAT_ARGB8888:
  11615. break;
  11616. case DRM_FORMAT_XRGB1555:
  11617. if (INTEL_GEN(dev_priv) > 3) {
  11618. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11619. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11620. goto err;
  11621. }
  11622. break;
  11623. case DRM_FORMAT_ABGR8888:
  11624. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11625. INTEL_GEN(dev_priv) < 9) {
  11626. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11627. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11628. goto err;
  11629. }
  11630. break;
  11631. case DRM_FORMAT_XBGR8888:
  11632. case DRM_FORMAT_XRGB2101010:
  11633. case DRM_FORMAT_XBGR2101010:
  11634. if (INTEL_GEN(dev_priv) < 4) {
  11635. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11636. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11637. goto err;
  11638. }
  11639. break;
  11640. case DRM_FORMAT_ABGR2101010:
  11641. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11642. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11643. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11644. goto err;
  11645. }
  11646. break;
  11647. case DRM_FORMAT_YUYV:
  11648. case DRM_FORMAT_UYVY:
  11649. case DRM_FORMAT_YVYU:
  11650. case DRM_FORMAT_VYUY:
  11651. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11652. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11653. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11654. goto err;
  11655. }
  11656. break;
  11657. default:
  11658. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11659. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11660. goto err;
  11661. }
  11662. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11663. if (mode_cmd->offsets[0] != 0)
  11664. goto err;
  11665. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11666. for (i = 0; i < fb->format->num_planes; i++) {
  11667. u32 stride_alignment;
  11668. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11669. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11670. goto err;
  11671. }
  11672. stride_alignment = intel_fb_stride_alignment(fb, i);
  11673. /*
  11674. * Display WA #0531: skl,bxt,kbl,glk
  11675. *
  11676. * Render decompression and plane width > 3840
  11677. * combined with horizontal panning requires the
  11678. * plane stride to be a multiple of 4. We'll just
  11679. * require the entire fb to accommodate that to avoid
  11680. * potential runtime errors at plane configuration time.
  11681. */
  11682. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11683. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11684. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11685. stride_alignment *= 4;
  11686. if (fb->pitches[i] & (stride_alignment - 1)) {
  11687. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11688. i, fb->pitches[i], stride_alignment);
  11689. goto err;
  11690. }
  11691. }
  11692. intel_fb->obj = obj;
  11693. ret = intel_fill_fb_info(dev_priv, fb);
  11694. if (ret)
  11695. goto err;
  11696. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11697. if (ret) {
  11698. DRM_ERROR("framebuffer init failed %d\n", ret);
  11699. goto err;
  11700. }
  11701. return 0;
  11702. err:
  11703. i915_gem_object_lock(obj);
  11704. obj->framebuffer_references--;
  11705. i915_gem_object_unlock(obj);
  11706. return ret;
  11707. }
  11708. static struct drm_framebuffer *
  11709. intel_user_framebuffer_create(struct drm_device *dev,
  11710. struct drm_file *filp,
  11711. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11712. {
  11713. struct drm_framebuffer *fb;
  11714. struct drm_i915_gem_object *obj;
  11715. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11716. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11717. if (!obj)
  11718. return ERR_PTR(-ENOENT);
  11719. fb = intel_framebuffer_create(obj, &mode_cmd);
  11720. if (IS_ERR(fb))
  11721. i915_gem_object_put(obj);
  11722. return fb;
  11723. }
  11724. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11725. {
  11726. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11727. drm_atomic_state_default_release(state);
  11728. i915_sw_fence_fini(&intel_state->commit_ready);
  11729. kfree(state);
  11730. }
  11731. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11732. .fb_create = intel_user_framebuffer_create,
  11733. .get_format_info = intel_get_format_info,
  11734. .output_poll_changed = intel_fbdev_output_poll_changed,
  11735. .atomic_check = intel_atomic_check,
  11736. .atomic_commit = intel_atomic_commit,
  11737. .atomic_state_alloc = intel_atomic_state_alloc,
  11738. .atomic_state_clear = intel_atomic_state_clear,
  11739. .atomic_state_free = intel_atomic_state_free,
  11740. };
  11741. /**
  11742. * intel_init_display_hooks - initialize the display modesetting hooks
  11743. * @dev_priv: device private
  11744. */
  11745. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11746. {
  11747. intel_init_cdclk_hooks(dev_priv);
  11748. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11749. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11750. dev_priv->display.get_initial_plane_config =
  11751. skylake_get_initial_plane_config;
  11752. dev_priv->display.crtc_compute_clock =
  11753. haswell_crtc_compute_clock;
  11754. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11755. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11756. } else if (HAS_DDI(dev_priv)) {
  11757. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11758. dev_priv->display.get_initial_plane_config =
  11759. i9xx_get_initial_plane_config;
  11760. dev_priv->display.crtc_compute_clock =
  11761. haswell_crtc_compute_clock;
  11762. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11763. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11764. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11765. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11766. dev_priv->display.get_initial_plane_config =
  11767. i9xx_get_initial_plane_config;
  11768. dev_priv->display.crtc_compute_clock =
  11769. ironlake_crtc_compute_clock;
  11770. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11771. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11772. } else if (IS_CHERRYVIEW(dev_priv)) {
  11773. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11774. dev_priv->display.get_initial_plane_config =
  11775. i9xx_get_initial_plane_config;
  11776. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11777. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11778. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11779. } else if (IS_VALLEYVIEW(dev_priv)) {
  11780. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11781. dev_priv->display.get_initial_plane_config =
  11782. i9xx_get_initial_plane_config;
  11783. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11784. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11785. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11786. } else if (IS_G4X(dev_priv)) {
  11787. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11788. dev_priv->display.get_initial_plane_config =
  11789. i9xx_get_initial_plane_config;
  11790. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11791. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11792. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11793. } else if (IS_PINEVIEW(dev_priv)) {
  11794. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11795. dev_priv->display.get_initial_plane_config =
  11796. i9xx_get_initial_plane_config;
  11797. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11798. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11799. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11800. } else if (!IS_GEN2(dev_priv)) {
  11801. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11802. dev_priv->display.get_initial_plane_config =
  11803. i9xx_get_initial_plane_config;
  11804. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11805. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11806. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11807. } else {
  11808. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11809. dev_priv->display.get_initial_plane_config =
  11810. i9xx_get_initial_plane_config;
  11811. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11812. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11813. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11814. }
  11815. if (IS_GEN5(dev_priv)) {
  11816. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11817. } else if (IS_GEN6(dev_priv)) {
  11818. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11819. } else if (IS_IVYBRIDGE(dev_priv)) {
  11820. /* FIXME: detect B0+ stepping and use auto training */
  11821. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11822. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11823. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11824. }
  11825. if (INTEL_GEN(dev_priv) >= 9)
  11826. dev_priv->display.update_crtcs = skl_update_crtcs;
  11827. else
  11828. dev_priv->display.update_crtcs = intel_update_crtcs;
  11829. }
  11830. /*
  11831. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11832. */
  11833. static void quirk_ssc_force_disable(struct drm_device *dev)
  11834. {
  11835. struct drm_i915_private *dev_priv = to_i915(dev);
  11836. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11837. DRM_INFO("applying lvds SSC disable quirk\n");
  11838. }
  11839. /*
  11840. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11841. * brightness value
  11842. */
  11843. static void quirk_invert_brightness(struct drm_device *dev)
  11844. {
  11845. struct drm_i915_private *dev_priv = to_i915(dev);
  11846. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11847. DRM_INFO("applying inverted panel brightness quirk\n");
  11848. }
  11849. /* Some VBT's incorrectly indicate no backlight is present */
  11850. static void quirk_backlight_present(struct drm_device *dev)
  11851. {
  11852. struct drm_i915_private *dev_priv = to_i915(dev);
  11853. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11854. DRM_INFO("applying backlight present quirk\n");
  11855. }
  11856. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11857. * which is 300 ms greater than eDP spec T12 min.
  11858. */
  11859. static void quirk_increase_t12_delay(struct drm_device *dev)
  11860. {
  11861. struct drm_i915_private *dev_priv = to_i915(dev);
  11862. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11863. DRM_INFO("Applying T12 delay quirk\n");
  11864. }
  11865. struct intel_quirk {
  11866. int device;
  11867. int subsystem_vendor;
  11868. int subsystem_device;
  11869. void (*hook)(struct drm_device *dev);
  11870. };
  11871. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11872. struct intel_dmi_quirk {
  11873. void (*hook)(struct drm_device *dev);
  11874. const struct dmi_system_id (*dmi_id_list)[];
  11875. };
  11876. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11877. {
  11878. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11879. return 1;
  11880. }
  11881. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11882. {
  11883. .dmi_id_list = &(const struct dmi_system_id[]) {
  11884. {
  11885. .callback = intel_dmi_reverse_brightness,
  11886. .ident = "NCR Corporation",
  11887. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11888. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11889. },
  11890. },
  11891. { } /* terminating entry */
  11892. },
  11893. .hook = quirk_invert_brightness,
  11894. },
  11895. };
  11896. static struct intel_quirk intel_quirks[] = {
  11897. /* Lenovo U160 cannot use SSC on LVDS */
  11898. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11899. /* Sony Vaio Y cannot use SSC on LVDS */
  11900. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11901. /* Acer Aspire 5734Z must invert backlight brightness */
  11902. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11903. /* Acer/eMachines G725 */
  11904. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11905. /* Acer/eMachines e725 */
  11906. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11907. /* Acer/Packard Bell NCL20 */
  11908. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11909. /* Acer Aspire 4736Z */
  11910. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11911. /* Acer Aspire 5336 */
  11912. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11913. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11914. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11915. /* Acer C720 Chromebook (Core i3 4005U) */
  11916. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11917. /* Apple Macbook 2,1 (Core 2 T7400) */
  11918. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11919. /* Apple Macbook 4,1 */
  11920. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11921. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11922. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11923. /* HP Chromebook 14 (Celeron 2955U) */
  11924. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11925. /* Dell Chromebook 11 */
  11926. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11927. /* Dell Chromebook 11 (2015 version) */
  11928. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11929. /* Toshiba Satellite P50-C-18C */
  11930. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11931. };
  11932. static void intel_init_quirks(struct drm_device *dev)
  11933. {
  11934. struct pci_dev *d = dev->pdev;
  11935. int i;
  11936. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11937. struct intel_quirk *q = &intel_quirks[i];
  11938. if (d->device == q->device &&
  11939. (d->subsystem_vendor == q->subsystem_vendor ||
  11940. q->subsystem_vendor == PCI_ANY_ID) &&
  11941. (d->subsystem_device == q->subsystem_device ||
  11942. q->subsystem_device == PCI_ANY_ID))
  11943. q->hook(dev);
  11944. }
  11945. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11946. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11947. intel_dmi_quirks[i].hook(dev);
  11948. }
  11949. }
  11950. /* Disable the VGA plane that we never use */
  11951. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  11952. {
  11953. struct pci_dev *pdev = dev_priv->drm.pdev;
  11954. u8 sr1;
  11955. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  11956. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11957. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  11958. outb(SR01, VGA_SR_INDEX);
  11959. sr1 = inb(VGA_SR_DATA);
  11960. outb(sr1 | 1<<5, VGA_SR_DATA);
  11961. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  11962. udelay(300);
  11963. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11964. POSTING_READ(vga_reg);
  11965. }
  11966. void intel_modeset_init_hw(struct drm_device *dev)
  11967. {
  11968. struct drm_i915_private *dev_priv = to_i915(dev);
  11969. intel_update_cdclk(dev_priv);
  11970. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  11971. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  11972. }
  11973. /*
  11974. * Calculate what we think the watermarks should be for the state we've read
  11975. * out of the hardware and then immediately program those watermarks so that
  11976. * we ensure the hardware settings match our internal state.
  11977. *
  11978. * We can calculate what we think WM's should be by creating a duplicate of the
  11979. * current state (which was constructed during hardware readout) and running it
  11980. * through the atomic check code to calculate new watermark values in the
  11981. * state object.
  11982. */
  11983. static void sanitize_watermarks(struct drm_device *dev)
  11984. {
  11985. struct drm_i915_private *dev_priv = to_i915(dev);
  11986. struct drm_atomic_state *state;
  11987. struct intel_atomic_state *intel_state;
  11988. struct drm_crtc *crtc;
  11989. struct drm_crtc_state *cstate;
  11990. struct drm_modeset_acquire_ctx ctx;
  11991. int ret;
  11992. int i;
  11993. /* Only supported on platforms that use atomic watermark design */
  11994. if (!dev_priv->display.optimize_watermarks)
  11995. return;
  11996. /*
  11997. * We need to hold connection_mutex before calling duplicate_state so
  11998. * that the connector loop is protected.
  11999. */
  12000. drm_modeset_acquire_init(&ctx, 0);
  12001. retry:
  12002. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12003. if (ret == -EDEADLK) {
  12004. drm_modeset_backoff(&ctx);
  12005. goto retry;
  12006. } else if (WARN_ON(ret)) {
  12007. goto fail;
  12008. }
  12009. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12010. if (WARN_ON(IS_ERR(state)))
  12011. goto fail;
  12012. intel_state = to_intel_atomic_state(state);
  12013. /*
  12014. * Hardware readout is the only time we don't want to calculate
  12015. * intermediate watermarks (since we don't trust the current
  12016. * watermarks).
  12017. */
  12018. if (!HAS_GMCH_DISPLAY(dev_priv))
  12019. intel_state->skip_intermediate_wm = true;
  12020. ret = intel_atomic_check(dev, state);
  12021. if (ret) {
  12022. /*
  12023. * If we fail here, it means that the hardware appears to be
  12024. * programmed in a way that shouldn't be possible, given our
  12025. * understanding of watermark requirements. This might mean a
  12026. * mistake in the hardware readout code or a mistake in the
  12027. * watermark calculations for a given platform. Raise a WARN
  12028. * so that this is noticeable.
  12029. *
  12030. * If this actually happens, we'll have to just leave the
  12031. * BIOS-programmed watermarks untouched and hope for the best.
  12032. */
  12033. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12034. goto put_state;
  12035. }
  12036. /* Write calculated watermark values back */
  12037. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12038. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12039. cs->wm.need_postvbl_update = true;
  12040. dev_priv->display.optimize_watermarks(intel_state, cs);
  12041. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12042. }
  12043. put_state:
  12044. drm_atomic_state_put(state);
  12045. fail:
  12046. drm_modeset_drop_locks(&ctx);
  12047. drm_modeset_acquire_fini(&ctx);
  12048. }
  12049. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12050. {
  12051. if (IS_GEN5(dev_priv)) {
  12052. u32 fdi_pll_clk =
  12053. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12054. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12055. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12056. dev_priv->fdi_pll_freq = 270000;
  12057. } else {
  12058. return;
  12059. }
  12060. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12061. }
  12062. int intel_modeset_init(struct drm_device *dev)
  12063. {
  12064. struct drm_i915_private *dev_priv = to_i915(dev);
  12065. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12066. enum pipe pipe;
  12067. struct intel_crtc *crtc;
  12068. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12069. drm_mode_config_init(dev);
  12070. dev->mode_config.min_width = 0;
  12071. dev->mode_config.min_height = 0;
  12072. dev->mode_config.preferred_depth = 24;
  12073. dev->mode_config.prefer_shadow = 1;
  12074. dev->mode_config.allow_fb_modifiers = true;
  12075. dev->mode_config.funcs = &intel_mode_funcs;
  12076. init_llist_head(&dev_priv->atomic_helper.free_list);
  12077. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12078. intel_atomic_helper_free_state_worker);
  12079. intel_init_quirks(dev);
  12080. intel_init_pm(dev_priv);
  12081. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12082. return 0;
  12083. /*
  12084. * There may be no VBT; and if the BIOS enabled SSC we can
  12085. * just keep using it to avoid unnecessary flicker. Whereas if the
  12086. * BIOS isn't using it, don't assume it will work even if the VBT
  12087. * indicates as much.
  12088. */
  12089. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12090. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12091. DREF_SSC1_ENABLE);
  12092. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12093. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12094. bios_lvds_use_ssc ? "en" : "dis",
  12095. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12096. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12097. }
  12098. }
  12099. if (IS_GEN2(dev_priv)) {
  12100. dev->mode_config.max_width = 2048;
  12101. dev->mode_config.max_height = 2048;
  12102. } else if (IS_GEN3(dev_priv)) {
  12103. dev->mode_config.max_width = 4096;
  12104. dev->mode_config.max_height = 4096;
  12105. } else {
  12106. dev->mode_config.max_width = 8192;
  12107. dev->mode_config.max_height = 8192;
  12108. }
  12109. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12110. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12111. dev->mode_config.cursor_height = 1023;
  12112. } else if (IS_GEN2(dev_priv)) {
  12113. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12114. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12115. } else {
  12116. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12117. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12118. }
  12119. dev->mode_config.fb_base = ggtt->gmadr.start;
  12120. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12121. INTEL_INFO(dev_priv)->num_pipes,
  12122. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12123. for_each_pipe(dev_priv, pipe) {
  12124. int ret;
  12125. ret = intel_crtc_init(dev_priv, pipe);
  12126. if (ret) {
  12127. drm_mode_config_cleanup(dev);
  12128. return ret;
  12129. }
  12130. }
  12131. intel_shared_dpll_init(dev);
  12132. intel_update_fdi_pll_freq(dev_priv);
  12133. intel_update_czclk(dev_priv);
  12134. intel_modeset_init_hw(dev);
  12135. if (dev_priv->max_cdclk_freq == 0)
  12136. intel_update_max_cdclk(dev_priv);
  12137. /* Just disable it once at startup */
  12138. i915_disable_vga(dev_priv);
  12139. intel_setup_outputs(dev_priv);
  12140. drm_modeset_lock_all(dev);
  12141. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12142. drm_modeset_unlock_all(dev);
  12143. for_each_intel_crtc(dev, crtc) {
  12144. struct intel_initial_plane_config plane_config = {};
  12145. if (!crtc->active)
  12146. continue;
  12147. /*
  12148. * Note that reserving the BIOS fb up front prevents us
  12149. * from stuffing other stolen allocations like the ring
  12150. * on top. This prevents some ugliness at boot time, and
  12151. * can even allow for smooth boot transitions if the BIOS
  12152. * fb is large enough for the active pipe configuration.
  12153. */
  12154. dev_priv->display.get_initial_plane_config(crtc,
  12155. &plane_config);
  12156. /*
  12157. * If the fb is shared between multiple heads, we'll
  12158. * just get the first one.
  12159. */
  12160. intel_find_initial_plane_obj(crtc, &plane_config);
  12161. }
  12162. /*
  12163. * Make sure hardware watermarks really match the state we read out.
  12164. * Note that we need to do this after reconstructing the BIOS fb's
  12165. * since the watermark calculation done here will use pstate->fb.
  12166. */
  12167. if (!HAS_GMCH_DISPLAY(dev_priv))
  12168. sanitize_watermarks(dev);
  12169. return 0;
  12170. }
  12171. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12172. {
  12173. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12174. /* 640x480@60Hz, ~25175 kHz */
  12175. struct dpll clock = {
  12176. .m1 = 18,
  12177. .m2 = 7,
  12178. .p1 = 13,
  12179. .p2 = 4,
  12180. .n = 2,
  12181. };
  12182. u32 dpll, fp;
  12183. int i;
  12184. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12185. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12186. pipe_name(pipe), clock.vco, clock.dot);
  12187. fp = i9xx_dpll_compute_fp(&clock);
  12188. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12189. DPLL_VGA_MODE_DIS |
  12190. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12191. PLL_P2_DIVIDE_BY_4 |
  12192. PLL_REF_INPUT_DREFCLK |
  12193. DPLL_VCO_ENABLE;
  12194. I915_WRITE(FP0(pipe), fp);
  12195. I915_WRITE(FP1(pipe), fp);
  12196. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12197. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12198. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12199. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12200. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12201. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12202. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12203. /*
  12204. * Apparently we need to have VGA mode enabled prior to changing
  12205. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12206. * dividers, even though the register value does change.
  12207. */
  12208. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12209. I915_WRITE(DPLL(pipe), dpll);
  12210. /* Wait for the clocks to stabilize. */
  12211. POSTING_READ(DPLL(pipe));
  12212. udelay(150);
  12213. /* The pixel multiplier can only be updated once the
  12214. * DPLL is enabled and the clocks are stable.
  12215. *
  12216. * So write it again.
  12217. */
  12218. I915_WRITE(DPLL(pipe), dpll);
  12219. /* We do this three times for luck */
  12220. for (i = 0; i < 3 ; i++) {
  12221. I915_WRITE(DPLL(pipe), dpll);
  12222. POSTING_READ(DPLL(pipe));
  12223. udelay(150); /* wait for warmup */
  12224. }
  12225. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12226. POSTING_READ(PIPECONF(pipe));
  12227. intel_wait_for_pipe_scanline_moving(crtc);
  12228. }
  12229. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12230. {
  12231. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12232. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12233. pipe_name(pipe));
  12234. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12235. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12236. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12237. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & CURSOR_MODE);
  12238. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & CURSOR_MODE);
  12239. I915_WRITE(PIPECONF(pipe), 0);
  12240. POSTING_READ(PIPECONF(pipe));
  12241. intel_wait_for_pipe_scanline_stopped(crtc);
  12242. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12243. POSTING_READ(DPLL(pipe));
  12244. }
  12245. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12246. struct intel_plane *plane)
  12247. {
  12248. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12249. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  12250. u32 val = I915_READ(DSPCNTR(i9xx_plane));
  12251. return (val & DISPLAY_PLANE_ENABLE) == 0 ||
  12252. (val & DISPPLANE_SEL_PIPE_MASK) == DISPPLANE_SEL_PIPE(crtc->pipe);
  12253. }
  12254. static void
  12255. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12256. {
  12257. struct intel_crtc *crtc;
  12258. if (INTEL_GEN(dev_priv) >= 4)
  12259. return;
  12260. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12261. struct intel_plane *plane =
  12262. to_intel_plane(crtc->base.primary);
  12263. if (intel_plane_mapping_ok(crtc, plane))
  12264. continue;
  12265. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12266. plane->base.name);
  12267. intel_plane_disable_noatomic(crtc, plane);
  12268. }
  12269. }
  12270. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12271. {
  12272. struct drm_device *dev = crtc->base.dev;
  12273. struct intel_encoder *encoder;
  12274. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12275. return true;
  12276. return false;
  12277. }
  12278. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12279. {
  12280. struct drm_device *dev = encoder->base.dev;
  12281. struct intel_connector *connector;
  12282. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12283. return connector;
  12284. return NULL;
  12285. }
  12286. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12287. enum pipe pch_transcoder)
  12288. {
  12289. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12290. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12291. }
  12292. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12293. struct drm_modeset_acquire_ctx *ctx)
  12294. {
  12295. struct drm_device *dev = crtc->base.dev;
  12296. struct drm_i915_private *dev_priv = to_i915(dev);
  12297. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12298. /* Clear any frame start delays used for debugging left by the BIOS */
  12299. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12300. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12301. I915_WRITE(reg,
  12302. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12303. }
  12304. /* restore vblank interrupts to correct state */
  12305. drm_crtc_vblank_reset(&crtc->base);
  12306. if (crtc->active) {
  12307. struct intel_plane *plane;
  12308. drm_crtc_vblank_on(&crtc->base);
  12309. /* Disable everything but the primary plane */
  12310. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12311. const struct intel_plane_state *plane_state =
  12312. to_intel_plane_state(plane->base.state);
  12313. if (plane_state->base.visible &&
  12314. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12315. intel_plane_disable_noatomic(crtc, plane);
  12316. }
  12317. }
  12318. /* Adjust the state of the output pipe according to whether we
  12319. * have active connectors/encoders. */
  12320. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12321. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12322. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12323. /*
  12324. * We start out with underrun reporting disabled to avoid races.
  12325. * For correct bookkeeping mark this on active crtcs.
  12326. *
  12327. * Also on gmch platforms we dont have any hardware bits to
  12328. * disable the underrun reporting. Which means we need to start
  12329. * out with underrun reporting disabled also on inactive pipes,
  12330. * since otherwise we'll complain about the garbage we read when
  12331. * e.g. coming up after runtime pm.
  12332. *
  12333. * No protection against concurrent access is required - at
  12334. * worst a fifo underrun happens which also sets this to false.
  12335. */
  12336. crtc->cpu_fifo_underrun_disabled = true;
  12337. /*
  12338. * We track the PCH trancoder underrun reporting state
  12339. * within the crtc. With crtc for pipe A housing the underrun
  12340. * reporting state for PCH transcoder A, crtc for pipe B housing
  12341. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12342. * and marking underrun reporting as disabled for the non-existing
  12343. * PCH transcoders B and C would prevent enabling the south
  12344. * error interrupt (see cpt_can_enable_serr_int()).
  12345. */
  12346. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12347. crtc->pch_fifo_underrun_disabled = true;
  12348. }
  12349. }
  12350. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12351. {
  12352. struct intel_connector *connector;
  12353. /* We need to check both for a crtc link (meaning that the
  12354. * encoder is active and trying to read from a pipe) and the
  12355. * pipe itself being active. */
  12356. bool has_active_crtc = encoder->base.crtc &&
  12357. to_intel_crtc(encoder->base.crtc)->active;
  12358. connector = intel_encoder_find_connector(encoder);
  12359. if (connector && !has_active_crtc) {
  12360. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12361. encoder->base.base.id,
  12362. encoder->base.name);
  12363. /* Connector is active, but has no active pipe. This is
  12364. * fallout from our resume register restoring. Disable
  12365. * the encoder manually again. */
  12366. if (encoder->base.crtc) {
  12367. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12368. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12369. encoder->base.base.id,
  12370. encoder->base.name);
  12371. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12372. if (encoder->post_disable)
  12373. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12374. }
  12375. encoder->base.crtc = NULL;
  12376. /* Inconsistent output/port/pipe state happens presumably due to
  12377. * a bug in one of the get_hw_state functions. Or someplace else
  12378. * in our code, like the register restore mess on resume. Clamp
  12379. * things to off as a safer default. */
  12380. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12381. connector->base.encoder = NULL;
  12382. }
  12383. }
  12384. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12385. {
  12386. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12387. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12388. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12389. i915_disable_vga(dev_priv);
  12390. }
  12391. }
  12392. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12393. {
  12394. /* This function can be called both from intel_modeset_setup_hw_state or
  12395. * at a very early point in our resume sequence, where the power well
  12396. * structures are not yet restored. Since this function is at a very
  12397. * paranoid "someone might have enabled VGA while we were not looking"
  12398. * level, just check if the power well is enabled instead of trying to
  12399. * follow the "don't touch the power well if we don't need it" policy
  12400. * the rest of the driver uses. */
  12401. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12402. return;
  12403. i915_redisable_vga_power_on(dev_priv);
  12404. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12405. }
  12406. /* FIXME read out full plane state for all planes */
  12407. static void readout_plane_state(struct intel_crtc *crtc)
  12408. {
  12409. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12410. struct intel_crtc_state *crtc_state =
  12411. to_intel_crtc_state(crtc->base.state);
  12412. struct intel_plane *plane;
  12413. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12414. struct intel_plane_state *plane_state =
  12415. to_intel_plane_state(plane->base.state);
  12416. bool visible = plane->get_hw_state(plane);
  12417. intel_set_plane_visible(crtc_state, plane_state, visible);
  12418. }
  12419. }
  12420. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12421. {
  12422. struct drm_i915_private *dev_priv = to_i915(dev);
  12423. enum pipe pipe;
  12424. struct intel_crtc *crtc;
  12425. struct intel_encoder *encoder;
  12426. struct intel_connector *connector;
  12427. struct drm_connector_list_iter conn_iter;
  12428. int i;
  12429. dev_priv->active_crtcs = 0;
  12430. for_each_intel_crtc(dev, crtc) {
  12431. struct intel_crtc_state *crtc_state =
  12432. to_intel_crtc_state(crtc->base.state);
  12433. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12434. memset(crtc_state, 0, sizeof(*crtc_state));
  12435. crtc_state->base.crtc = &crtc->base;
  12436. crtc_state->base.active = crtc_state->base.enable =
  12437. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12438. crtc->base.enabled = crtc_state->base.enable;
  12439. crtc->active = crtc_state->base.active;
  12440. if (crtc_state->base.active)
  12441. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12442. readout_plane_state(crtc);
  12443. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12444. crtc->base.base.id, crtc->base.name,
  12445. enableddisabled(crtc_state->base.active));
  12446. }
  12447. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12448. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12449. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12450. &pll->state.hw_state);
  12451. pll->state.crtc_mask = 0;
  12452. for_each_intel_crtc(dev, crtc) {
  12453. struct intel_crtc_state *crtc_state =
  12454. to_intel_crtc_state(crtc->base.state);
  12455. if (crtc_state->base.active &&
  12456. crtc_state->shared_dpll == pll)
  12457. pll->state.crtc_mask |= 1 << crtc->pipe;
  12458. }
  12459. pll->active_mask = pll->state.crtc_mask;
  12460. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12461. pll->name, pll->state.crtc_mask, pll->on);
  12462. }
  12463. for_each_intel_encoder(dev, encoder) {
  12464. pipe = 0;
  12465. if (encoder->get_hw_state(encoder, &pipe)) {
  12466. struct intel_crtc_state *crtc_state;
  12467. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12468. crtc_state = to_intel_crtc_state(crtc->base.state);
  12469. encoder->base.crtc = &crtc->base;
  12470. encoder->get_config(encoder, crtc_state);
  12471. } else {
  12472. encoder->base.crtc = NULL;
  12473. }
  12474. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12475. encoder->base.base.id, encoder->base.name,
  12476. enableddisabled(encoder->base.crtc),
  12477. pipe_name(pipe));
  12478. }
  12479. drm_connector_list_iter_begin(dev, &conn_iter);
  12480. for_each_intel_connector_iter(connector, &conn_iter) {
  12481. if (connector->get_hw_state(connector)) {
  12482. connector->base.dpms = DRM_MODE_DPMS_ON;
  12483. encoder = connector->encoder;
  12484. connector->base.encoder = &encoder->base;
  12485. if (encoder->base.crtc &&
  12486. encoder->base.crtc->state->active) {
  12487. /*
  12488. * This has to be done during hardware readout
  12489. * because anything calling .crtc_disable may
  12490. * rely on the connector_mask being accurate.
  12491. */
  12492. encoder->base.crtc->state->connector_mask |=
  12493. 1 << drm_connector_index(&connector->base);
  12494. encoder->base.crtc->state->encoder_mask |=
  12495. 1 << drm_encoder_index(&encoder->base);
  12496. }
  12497. } else {
  12498. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12499. connector->base.encoder = NULL;
  12500. }
  12501. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12502. connector->base.base.id, connector->base.name,
  12503. enableddisabled(connector->base.encoder));
  12504. }
  12505. drm_connector_list_iter_end(&conn_iter);
  12506. for_each_intel_crtc(dev, crtc) {
  12507. struct intel_crtc_state *crtc_state =
  12508. to_intel_crtc_state(crtc->base.state);
  12509. int min_cdclk = 0;
  12510. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12511. if (crtc_state->base.active) {
  12512. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12513. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12514. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12515. /*
  12516. * The initial mode needs to be set in order to keep
  12517. * the atomic core happy. It wants a valid mode if the
  12518. * crtc's enabled, so we do the above call.
  12519. *
  12520. * But we don't set all the derived state fully, hence
  12521. * set a flag to indicate that a full recalculation is
  12522. * needed on the next commit.
  12523. */
  12524. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12525. intel_crtc_compute_pixel_rate(crtc_state);
  12526. if (dev_priv->display.modeset_calc_cdclk) {
  12527. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  12528. if (WARN_ON(min_cdclk < 0))
  12529. min_cdclk = 0;
  12530. }
  12531. drm_calc_timestamping_constants(&crtc->base,
  12532. &crtc_state->base.adjusted_mode);
  12533. update_scanline_offset(crtc);
  12534. }
  12535. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  12536. dev_priv->min_voltage_level[crtc->pipe] =
  12537. crtc_state->min_voltage_level;
  12538. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12539. }
  12540. }
  12541. static void
  12542. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12543. {
  12544. struct intel_encoder *encoder;
  12545. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12546. u64 get_domains;
  12547. enum intel_display_power_domain domain;
  12548. if (!encoder->get_power_domains)
  12549. continue;
  12550. get_domains = encoder->get_power_domains(encoder);
  12551. for_each_power_domain(domain, get_domains)
  12552. intel_display_power_get(dev_priv, domain);
  12553. }
  12554. }
  12555. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  12556. {
  12557. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  12558. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  12559. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  12560. DARBF_GATING_DIS);
  12561. if (IS_HASWELL(dev_priv)) {
  12562. /*
  12563. * WaRsPkgCStateDisplayPMReq:hsw
  12564. * System hang if this isn't done before disabling all planes!
  12565. */
  12566. I915_WRITE(CHICKEN_PAR1_1,
  12567. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  12568. }
  12569. }
  12570. /* Scan out the current hw modeset state,
  12571. * and sanitizes it to the current state
  12572. */
  12573. static void
  12574. intel_modeset_setup_hw_state(struct drm_device *dev,
  12575. struct drm_modeset_acquire_ctx *ctx)
  12576. {
  12577. struct drm_i915_private *dev_priv = to_i915(dev);
  12578. enum pipe pipe;
  12579. struct intel_crtc *crtc;
  12580. struct intel_encoder *encoder;
  12581. int i;
  12582. intel_early_display_was(dev_priv);
  12583. intel_modeset_readout_hw_state(dev);
  12584. /* HW state is read out, now we need to sanitize this mess. */
  12585. get_encoder_power_domains(dev_priv);
  12586. intel_sanitize_plane_mapping(dev_priv);
  12587. for_each_intel_encoder(dev, encoder) {
  12588. intel_sanitize_encoder(encoder);
  12589. }
  12590. for_each_pipe(dev_priv, pipe) {
  12591. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12592. intel_sanitize_crtc(crtc, ctx);
  12593. intel_dump_pipe_config(crtc, crtc->config,
  12594. "[setup_hw_state]");
  12595. }
  12596. intel_modeset_update_connector_atomic_state(dev);
  12597. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12598. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12599. if (!pll->on || pll->active_mask)
  12600. continue;
  12601. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12602. pll->funcs.disable(dev_priv, pll);
  12603. pll->on = false;
  12604. }
  12605. if (IS_G4X(dev_priv)) {
  12606. g4x_wm_get_hw_state(dev);
  12607. g4x_wm_sanitize(dev_priv);
  12608. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12609. vlv_wm_get_hw_state(dev);
  12610. vlv_wm_sanitize(dev_priv);
  12611. } else if (INTEL_GEN(dev_priv) >= 9) {
  12612. skl_wm_get_hw_state(dev);
  12613. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12614. ilk_wm_get_hw_state(dev);
  12615. }
  12616. for_each_intel_crtc(dev, crtc) {
  12617. u64 put_domains;
  12618. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12619. if (WARN_ON(put_domains))
  12620. modeset_put_power_domains(dev_priv, put_domains);
  12621. }
  12622. intel_display_set_init_power(dev_priv, false);
  12623. intel_power_domains_verify_state(dev_priv);
  12624. intel_fbc_init_pipe_state(dev_priv);
  12625. }
  12626. void intel_display_resume(struct drm_device *dev)
  12627. {
  12628. struct drm_i915_private *dev_priv = to_i915(dev);
  12629. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12630. struct drm_modeset_acquire_ctx ctx;
  12631. int ret;
  12632. dev_priv->modeset_restore_state = NULL;
  12633. if (state)
  12634. state->acquire_ctx = &ctx;
  12635. drm_modeset_acquire_init(&ctx, 0);
  12636. while (1) {
  12637. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12638. if (ret != -EDEADLK)
  12639. break;
  12640. drm_modeset_backoff(&ctx);
  12641. }
  12642. if (!ret)
  12643. ret = __intel_display_resume(dev, state, &ctx);
  12644. intel_enable_ipc(dev_priv);
  12645. drm_modeset_drop_locks(&ctx);
  12646. drm_modeset_acquire_fini(&ctx);
  12647. if (ret)
  12648. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12649. if (state)
  12650. drm_atomic_state_put(state);
  12651. }
  12652. int intel_connector_register(struct drm_connector *connector)
  12653. {
  12654. struct intel_connector *intel_connector = to_intel_connector(connector);
  12655. int ret;
  12656. ret = intel_backlight_device_register(intel_connector);
  12657. if (ret)
  12658. goto err;
  12659. return 0;
  12660. err:
  12661. return ret;
  12662. }
  12663. void intel_connector_unregister(struct drm_connector *connector)
  12664. {
  12665. struct intel_connector *intel_connector = to_intel_connector(connector);
  12666. intel_backlight_device_unregister(intel_connector);
  12667. intel_panel_destroy_backlight(connector);
  12668. }
  12669. static void intel_hpd_poll_fini(struct drm_device *dev)
  12670. {
  12671. struct intel_connector *connector;
  12672. struct drm_connector_list_iter conn_iter;
  12673. /* Kill all the work that may have been queued by hpd. */
  12674. drm_connector_list_iter_begin(dev, &conn_iter);
  12675. for_each_intel_connector_iter(connector, &conn_iter) {
  12676. if (connector->modeset_retry_work.func)
  12677. cancel_work_sync(&connector->modeset_retry_work);
  12678. }
  12679. drm_connector_list_iter_end(&conn_iter);
  12680. }
  12681. void intel_modeset_cleanup(struct drm_device *dev)
  12682. {
  12683. struct drm_i915_private *dev_priv = to_i915(dev);
  12684. flush_work(&dev_priv->atomic_helper.free_work);
  12685. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12686. intel_disable_gt_powersave(dev_priv);
  12687. /*
  12688. * Interrupts and polling as the first thing to avoid creating havoc.
  12689. * Too much stuff here (turning of connectors, ...) would
  12690. * experience fancy races otherwise.
  12691. */
  12692. intel_irq_uninstall(dev_priv);
  12693. /*
  12694. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12695. * poll handlers. Hence disable polling after hpd handling is shut down.
  12696. */
  12697. intel_hpd_poll_fini(dev);
  12698. /* poll work can call into fbdev, hence clean that up afterwards */
  12699. intel_fbdev_fini(dev_priv);
  12700. intel_unregister_dsm_handler();
  12701. intel_fbc_global_disable(dev_priv);
  12702. /* flush any delayed tasks or pending work */
  12703. flush_scheduled_work();
  12704. drm_mode_config_cleanup(dev);
  12705. intel_cleanup_overlay(dev_priv);
  12706. intel_cleanup_gt_powersave(dev_priv);
  12707. intel_teardown_gmbus(dev_priv);
  12708. destroy_workqueue(dev_priv->modeset_wq);
  12709. }
  12710. void intel_connector_attach_encoder(struct intel_connector *connector,
  12711. struct intel_encoder *encoder)
  12712. {
  12713. connector->encoder = encoder;
  12714. drm_mode_connector_attach_encoder(&connector->base,
  12715. &encoder->base);
  12716. }
  12717. /*
  12718. * set vga decode state - true == enable VGA decode
  12719. */
  12720. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12721. {
  12722. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12723. u16 gmch_ctrl;
  12724. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12725. DRM_ERROR("failed to read control word\n");
  12726. return -EIO;
  12727. }
  12728. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12729. return 0;
  12730. if (state)
  12731. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12732. else
  12733. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12734. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12735. DRM_ERROR("failed to write control word\n");
  12736. return -EIO;
  12737. }
  12738. return 0;
  12739. }
  12740. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12741. struct intel_display_error_state {
  12742. u32 power_well_driver;
  12743. int num_transcoders;
  12744. struct intel_cursor_error_state {
  12745. u32 control;
  12746. u32 position;
  12747. u32 base;
  12748. u32 size;
  12749. } cursor[I915_MAX_PIPES];
  12750. struct intel_pipe_error_state {
  12751. bool power_domain_on;
  12752. u32 source;
  12753. u32 stat;
  12754. } pipe[I915_MAX_PIPES];
  12755. struct intel_plane_error_state {
  12756. u32 control;
  12757. u32 stride;
  12758. u32 size;
  12759. u32 pos;
  12760. u32 addr;
  12761. u32 surface;
  12762. u32 tile_offset;
  12763. } plane[I915_MAX_PIPES];
  12764. struct intel_transcoder_error_state {
  12765. bool power_domain_on;
  12766. enum transcoder cpu_transcoder;
  12767. u32 conf;
  12768. u32 htotal;
  12769. u32 hblank;
  12770. u32 hsync;
  12771. u32 vtotal;
  12772. u32 vblank;
  12773. u32 vsync;
  12774. } transcoder[4];
  12775. };
  12776. struct intel_display_error_state *
  12777. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12778. {
  12779. struct intel_display_error_state *error;
  12780. int transcoders[] = {
  12781. TRANSCODER_A,
  12782. TRANSCODER_B,
  12783. TRANSCODER_C,
  12784. TRANSCODER_EDP,
  12785. };
  12786. int i;
  12787. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12788. return NULL;
  12789. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12790. if (error == NULL)
  12791. return NULL;
  12792. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12793. error->power_well_driver =
  12794. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  12795. for_each_pipe(dev_priv, i) {
  12796. error->pipe[i].power_domain_on =
  12797. __intel_display_power_is_enabled(dev_priv,
  12798. POWER_DOMAIN_PIPE(i));
  12799. if (!error->pipe[i].power_domain_on)
  12800. continue;
  12801. error->cursor[i].control = I915_READ(CURCNTR(i));
  12802. error->cursor[i].position = I915_READ(CURPOS(i));
  12803. error->cursor[i].base = I915_READ(CURBASE(i));
  12804. error->plane[i].control = I915_READ(DSPCNTR(i));
  12805. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12806. if (INTEL_GEN(dev_priv) <= 3) {
  12807. error->plane[i].size = I915_READ(DSPSIZE(i));
  12808. error->plane[i].pos = I915_READ(DSPPOS(i));
  12809. }
  12810. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12811. error->plane[i].addr = I915_READ(DSPADDR(i));
  12812. if (INTEL_GEN(dev_priv) >= 4) {
  12813. error->plane[i].surface = I915_READ(DSPSURF(i));
  12814. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12815. }
  12816. error->pipe[i].source = I915_READ(PIPESRC(i));
  12817. if (HAS_GMCH_DISPLAY(dev_priv))
  12818. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12819. }
  12820. /* Note: this does not include DSI transcoders. */
  12821. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12822. if (HAS_DDI(dev_priv))
  12823. error->num_transcoders++; /* Account for eDP. */
  12824. for (i = 0; i < error->num_transcoders; i++) {
  12825. enum transcoder cpu_transcoder = transcoders[i];
  12826. error->transcoder[i].power_domain_on =
  12827. __intel_display_power_is_enabled(dev_priv,
  12828. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12829. if (!error->transcoder[i].power_domain_on)
  12830. continue;
  12831. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12832. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12833. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12834. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12835. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12836. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12837. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12838. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12839. }
  12840. return error;
  12841. }
  12842. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12843. void
  12844. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12845. struct intel_display_error_state *error)
  12846. {
  12847. struct drm_i915_private *dev_priv = m->i915;
  12848. int i;
  12849. if (!error)
  12850. return;
  12851. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12852. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12853. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12854. error->power_well_driver);
  12855. for_each_pipe(dev_priv, i) {
  12856. err_printf(m, "Pipe [%d]:\n", i);
  12857. err_printf(m, " Power: %s\n",
  12858. onoff(error->pipe[i].power_domain_on));
  12859. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12860. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12861. err_printf(m, "Plane [%d]:\n", i);
  12862. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12863. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12864. if (INTEL_GEN(dev_priv) <= 3) {
  12865. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12866. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12867. }
  12868. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12869. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12870. if (INTEL_GEN(dev_priv) >= 4) {
  12871. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12872. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12873. }
  12874. err_printf(m, "Cursor [%d]:\n", i);
  12875. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12876. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12877. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12878. }
  12879. for (i = 0; i < error->num_transcoders; i++) {
  12880. err_printf(m, "CPU transcoder: %s\n",
  12881. transcoder_name(error->transcoder[i].cpu_transcoder));
  12882. err_printf(m, " Power: %s\n",
  12883. onoff(error->transcoder[i].power_domain_on));
  12884. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12885. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12886. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12887. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12888. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12889. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12890. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12891. }
  12892. }
  12893. #endif