intel_display.c 435 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430
  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 if (IS_GEN5(dev_priv))
  196. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  197. else
  198. return 270000;
  199. }
  200. static const struct intel_limit intel_limits_i8xx_dac = {
  201. .dot = { .min = 25000, .max = 350000 },
  202. .vco = { .min = 908000, .max = 1512000 },
  203. .n = { .min = 2, .max = 16 },
  204. .m = { .min = 96, .max = 140 },
  205. .m1 = { .min = 18, .max = 26 },
  206. .m2 = { .min = 6, .max = 16 },
  207. .p = { .min = 4, .max = 128 },
  208. .p1 = { .min = 2, .max = 33 },
  209. .p2 = { .dot_limit = 165000,
  210. .p2_slow = 4, .p2_fast = 2 },
  211. };
  212. static const struct intel_limit intel_limits_i8xx_dvo = {
  213. .dot = { .min = 25000, .max = 350000 },
  214. .vco = { .min = 908000, .max = 1512000 },
  215. .n = { .min = 2, .max = 16 },
  216. .m = { .min = 96, .max = 140 },
  217. .m1 = { .min = 18, .max = 26 },
  218. .m2 = { .min = 6, .max = 16 },
  219. .p = { .min = 4, .max = 128 },
  220. .p1 = { .min = 2, .max = 33 },
  221. .p2 = { .dot_limit = 165000,
  222. .p2_slow = 4, .p2_fast = 4 },
  223. };
  224. static const struct intel_limit intel_limits_i8xx_lvds = {
  225. .dot = { .min = 25000, .max = 350000 },
  226. .vco = { .min = 908000, .max = 1512000 },
  227. .n = { .min = 2, .max = 16 },
  228. .m = { .min = 96, .max = 140 },
  229. .m1 = { .min = 18, .max = 26 },
  230. .m2 = { .min = 6, .max = 16 },
  231. .p = { .min = 4, .max = 128 },
  232. .p1 = { .min = 1, .max = 6 },
  233. .p2 = { .dot_limit = 165000,
  234. .p2_slow = 14, .p2_fast = 7 },
  235. };
  236. static const struct intel_limit intel_limits_i9xx_sdvo = {
  237. .dot = { .min = 20000, .max = 400000 },
  238. .vco = { .min = 1400000, .max = 2800000 },
  239. .n = { .min = 1, .max = 6 },
  240. .m = { .min = 70, .max = 120 },
  241. .m1 = { .min = 8, .max = 18 },
  242. .m2 = { .min = 3, .max = 7 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 200000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const struct intel_limit intel_limits_i9xx_lvds = {
  249. .dot = { .min = 20000, .max = 400000 },
  250. .vco = { .min = 1400000, .max = 2800000 },
  251. .n = { .min = 1, .max = 6 },
  252. .m = { .min = 70, .max = 120 },
  253. .m1 = { .min = 8, .max = 18 },
  254. .m2 = { .min = 3, .max = 7 },
  255. .p = { .min = 7, .max = 98 },
  256. .p1 = { .min = 1, .max = 8 },
  257. .p2 = { .dot_limit = 112000,
  258. .p2_slow = 14, .p2_fast = 7 },
  259. };
  260. static const struct intel_limit intel_limits_g4x_sdvo = {
  261. .dot = { .min = 25000, .max = 270000 },
  262. .vco = { .min = 1750000, .max = 3500000},
  263. .n = { .min = 1, .max = 4 },
  264. .m = { .min = 104, .max = 138 },
  265. .m1 = { .min = 17, .max = 23 },
  266. .m2 = { .min = 5, .max = 11 },
  267. .p = { .min = 10, .max = 30 },
  268. .p1 = { .min = 1, .max = 3},
  269. .p2 = { .dot_limit = 270000,
  270. .p2_slow = 10,
  271. .p2_fast = 10
  272. },
  273. };
  274. static const struct intel_limit intel_limits_g4x_hdmi = {
  275. .dot = { .min = 22000, .max = 400000 },
  276. .vco = { .min = 1750000, .max = 3500000},
  277. .n = { .min = 1, .max = 4 },
  278. .m = { .min = 104, .max = 138 },
  279. .m1 = { .min = 16, .max = 23 },
  280. .m2 = { .min = 5, .max = 11 },
  281. .p = { .min = 5, .max = 80 },
  282. .p1 = { .min = 1, .max = 8},
  283. .p2 = { .dot_limit = 165000,
  284. .p2_slow = 10, .p2_fast = 5 },
  285. };
  286. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  287. .dot = { .min = 20000, .max = 115000 },
  288. .vco = { .min = 1750000, .max = 3500000 },
  289. .n = { .min = 1, .max = 3 },
  290. .m = { .min = 104, .max = 138 },
  291. .m1 = { .min = 17, .max = 23 },
  292. .m2 = { .min = 5, .max = 11 },
  293. .p = { .min = 28, .max = 112 },
  294. .p1 = { .min = 2, .max = 8 },
  295. .p2 = { .dot_limit = 0,
  296. .p2_slow = 14, .p2_fast = 14
  297. },
  298. };
  299. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  300. .dot = { .min = 80000, .max = 224000 },
  301. .vco = { .min = 1750000, .max = 3500000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 104, .max = 138 },
  304. .m1 = { .min = 17, .max = 23 },
  305. .m2 = { .min = 5, .max = 11 },
  306. .p = { .min = 14, .max = 42 },
  307. .p1 = { .min = 2, .max = 6 },
  308. .p2 = { .dot_limit = 0,
  309. .p2_slow = 7, .p2_fast = 7
  310. },
  311. };
  312. static const struct intel_limit intel_limits_pineview_sdvo = {
  313. .dot = { .min = 20000, .max = 400000},
  314. .vco = { .min = 1700000, .max = 3500000 },
  315. /* Pineview's Ncounter is a ring counter */
  316. .n = { .min = 3, .max = 6 },
  317. .m = { .min = 2, .max = 256 },
  318. /* Pineview only has one combined m divider, which we treat as m2. */
  319. .m1 = { .min = 0, .max = 0 },
  320. .m2 = { .min = 0, .max = 254 },
  321. .p = { .min = 5, .max = 80 },
  322. .p1 = { .min = 1, .max = 8 },
  323. .p2 = { .dot_limit = 200000,
  324. .p2_slow = 10, .p2_fast = 5 },
  325. };
  326. static const struct intel_limit intel_limits_pineview_lvds = {
  327. .dot = { .min = 20000, .max = 400000 },
  328. .vco = { .min = 1700000, .max = 3500000 },
  329. .n = { .min = 3, .max = 6 },
  330. .m = { .min = 2, .max = 256 },
  331. .m1 = { .min = 0, .max = 0 },
  332. .m2 = { .min = 0, .max = 254 },
  333. .p = { .min = 7, .max = 112 },
  334. .p1 = { .min = 1, .max = 8 },
  335. .p2 = { .dot_limit = 112000,
  336. .p2_slow = 14, .p2_fast = 14 },
  337. };
  338. /* Ironlake / Sandybridge
  339. *
  340. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  341. * the range value for them is (actual_value - 2).
  342. */
  343. static const struct intel_limit intel_limits_ironlake_dac = {
  344. .dot = { .min = 25000, .max = 350000 },
  345. .vco = { .min = 1760000, .max = 3510000 },
  346. .n = { .min = 1, .max = 5 },
  347. .m = { .min = 79, .max = 127 },
  348. .m1 = { .min = 12, .max = 22 },
  349. .m2 = { .min = 5, .max = 9 },
  350. .p = { .min = 5, .max = 80 },
  351. .p1 = { .min = 1, .max = 8 },
  352. .p2 = { .dot_limit = 225000,
  353. .p2_slow = 10, .p2_fast = 5 },
  354. };
  355. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  356. .dot = { .min = 25000, .max = 350000 },
  357. .vco = { .min = 1760000, .max = 3510000 },
  358. .n = { .min = 1, .max = 3 },
  359. .m = { .min = 79, .max = 118 },
  360. .m1 = { .min = 12, .max = 22 },
  361. .m2 = { .min = 5, .max = 9 },
  362. .p = { .min = 28, .max = 112 },
  363. .p1 = { .min = 2, .max = 8 },
  364. .p2 = { .dot_limit = 225000,
  365. .p2_slow = 14, .p2_fast = 14 },
  366. };
  367. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  368. .dot = { .min = 25000, .max = 350000 },
  369. .vco = { .min = 1760000, .max = 3510000 },
  370. .n = { .min = 1, .max = 3 },
  371. .m = { .min = 79, .max = 127 },
  372. .m1 = { .min = 12, .max = 22 },
  373. .m2 = { .min = 5, .max = 9 },
  374. .p = { .min = 14, .max = 56 },
  375. .p1 = { .min = 2, .max = 8 },
  376. .p2 = { .dot_limit = 225000,
  377. .p2_slow = 7, .p2_fast = 7 },
  378. };
  379. /* LVDS 100mhz refclk limits. */
  380. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  381. .dot = { .min = 25000, .max = 350000 },
  382. .vco = { .min = 1760000, .max = 3510000 },
  383. .n = { .min = 1, .max = 2 },
  384. .m = { .min = 79, .max = 126 },
  385. .m1 = { .min = 12, .max = 22 },
  386. .m2 = { .min = 5, .max = 9 },
  387. .p = { .min = 28, .max = 112 },
  388. .p1 = { .min = 2, .max = 8 },
  389. .p2 = { .dot_limit = 225000,
  390. .p2_slow = 14, .p2_fast = 14 },
  391. };
  392. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  393. .dot = { .min = 25000, .max = 350000 },
  394. .vco = { .min = 1760000, .max = 3510000 },
  395. .n = { .min = 1, .max = 3 },
  396. .m = { .min = 79, .max = 126 },
  397. .m1 = { .min = 12, .max = 22 },
  398. .m2 = { .min = 5, .max = 9 },
  399. .p = { .min = 14, .max = 42 },
  400. .p1 = { .min = 2, .max = 6 },
  401. .p2 = { .dot_limit = 225000,
  402. .p2_slow = 7, .p2_fast = 7 },
  403. };
  404. static const struct intel_limit intel_limits_vlv = {
  405. /*
  406. * These are the data rate limits (measured in fast clocks)
  407. * since those are the strictest limits we have. The fast
  408. * clock and actual rate limits are more relaxed, so checking
  409. * them would make no difference.
  410. */
  411. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  412. .vco = { .min = 4000000, .max = 6000000 },
  413. .n = { .min = 1, .max = 7 },
  414. .m1 = { .min = 2, .max = 3 },
  415. .m2 = { .min = 11, .max = 156 },
  416. .p1 = { .min = 2, .max = 3 },
  417. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  418. };
  419. static const struct intel_limit intel_limits_chv = {
  420. /*
  421. * These are the data rate limits (measured in fast clocks)
  422. * since those are the strictest limits we have. The fast
  423. * clock and actual rate limits are more relaxed, so checking
  424. * them would make no difference.
  425. */
  426. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  427. .vco = { .min = 4800000, .max = 6480000 },
  428. .n = { .min = 1, .max = 1 },
  429. .m1 = { .min = 2, .max = 2 },
  430. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  431. .p1 = { .min = 2, .max = 4 },
  432. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  433. };
  434. static const struct intel_limit intel_limits_bxt = {
  435. /* FIXME: find real dot limits */
  436. .dot = { .min = 0, .max = INT_MAX },
  437. .vco = { .min = 4800000, .max = 6700000 },
  438. .n = { .min = 1, .max = 1 },
  439. .m1 = { .min = 2, .max = 2 },
  440. /* FIXME: find real m2 limits */
  441. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  442. .p1 = { .min = 2, .max = 4 },
  443. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  444. };
  445. static bool
  446. needs_modeset(struct drm_crtc_state *state)
  447. {
  448. return drm_atomic_crtc_needs_modeset(state);
  449. }
  450. /*
  451. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  452. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  453. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  454. * The helpers' return value is the rate of the clock that is fed to the
  455. * display engine's pipe which can be the above fast dot clock rate or a
  456. * divided-down version of it.
  457. */
  458. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  459. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  460. {
  461. clock->m = clock->m2 + 2;
  462. clock->p = clock->p1 * clock->p2;
  463. if (WARN_ON(clock->n == 0 || clock->p == 0))
  464. return 0;
  465. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  466. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  467. return clock->dot;
  468. }
  469. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  470. {
  471. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  472. }
  473. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  474. {
  475. clock->m = i9xx_dpll_compute_m(clock);
  476. clock->p = clock->p1 * clock->p2;
  477. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  478. return 0;
  479. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  480. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  481. return clock->dot;
  482. }
  483. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  484. {
  485. clock->m = clock->m1 * clock->m2;
  486. clock->p = clock->p1 * clock->p2;
  487. if (WARN_ON(clock->n == 0 || clock->p == 0))
  488. return 0;
  489. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  490. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  491. return clock->dot / 5;
  492. }
  493. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  494. {
  495. clock->m = clock->m1 * clock->m2;
  496. clock->p = clock->p1 * clock->p2;
  497. if (WARN_ON(clock->n == 0 || clock->p == 0))
  498. return 0;
  499. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  500. clock->n << 22);
  501. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  502. return clock->dot / 5;
  503. }
  504. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  505. /**
  506. * Returns whether the given set of divisors are valid for a given refclk with
  507. * the given connectors.
  508. */
  509. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  510. const struct intel_limit *limit,
  511. const struct dpll *clock)
  512. {
  513. if (clock->n < limit->n.min || limit->n.max < clock->n)
  514. INTELPllInvalid("n out of range\n");
  515. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  516. INTELPllInvalid("p1 out of range\n");
  517. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  518. INTELPllInvalid("m2 out of range\n");
  519. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  520. INTELPllInvalid("m1 out of range\n");
  521. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  522. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  523. if (clock->m1 <= clock->m2)
  524. INTELPllInvalid("m1 <= m2\n");
  525. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  526. !IS_GEN9_LP(dev_priv)) {
  527. if (clock->p < limit->p.min || limit->p.max < clock->p)
  528. INTELPllInvalid("p out of range\n");
  529. if (clock->m < limit->m.min || limit->m.max < clock->m)
  530. INTELPllInvalid("m out of range\n");
  531. }
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static int
  542. i9xx_select_p2_div(const struct intel_limit *limit,
  543. const struct intel_crtc_state *crtc_state,
  544. int target)
  545. {
  546. struct drm_device *dev = crtc_state->base.crtc->dev;
  547. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  548. /*
  549. * For LVDS just rely on its current settings for dual-channel.
  550. * We haven't figured out how to reliably set up different
  551. * single/dual channel state, if we even can.
  552. */
  553. if (intel_is_dual_link_lvds(dev))
  554. return limit->p2.p2_fast;
  555. else
  556. return limit->p2.p2_slow;
  557. } else {
  558. if (target < limit->p2.dot_limit)
  559. return limit->p2.p2_slow;
  560. else
  561. return limit->p2.p2_fast;
  562. }
  563. }
  564. /*
  565. * Returns a set of divisors for the desired target clock with the given
  566. * refclk, or FALSE. The returned values represent the clock equation:
  567. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  568. *
  569. * Target and reference clocks are specified in kHz.
  570. *
  571. * If match_clock is provided, then best_clock P divider must match the P
  572. * divider from @match_clock used for LVDS downclocking.
  573. */
  574. static bool
  575. i9xx_find_best_dpll(const struct intel_limit *limit,
  576. struct intel_crtc_state *crtc_state,
  577. int target, int refclk, struct dpll *match_clock,
  578. struct dpll *best_clock)
  579. {
  580. struct drm_device *dev = crtc_state->base.crtc->dev;
  581. struct dpll clock;
  582. int err = target;
  583. memset(best_clock, 0, sizeof(*best_clock));
  584. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  585. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  586. clock.m1++) {
  587. for (clock.m2 = limit->m2.min;
  588. clock.m2 <= limit->m2.max; clock.m2++) {
  589. if (clock.m2 >= clock.m1)
  590. break;
  591. for (clock.n = limit->n.min;
  592. clock.n <= limit->n.max; clock.n++) {
  593. for (clock.p1 = limit->p1.min;
  594. clock.p1 <= limit->p1.max; clock.p1++) {
  595. int this_err;
  596. i9xx_calc_dpll_params(refclk, &clock);
  597. if (!intel_PLL_is_valid(to_i915(dev),
  598. limit,
  599. &clock))
  600. continue;
  601. if (match_clock &&
  602. clock.p != match_clock->p)
  603. continue;
  604. this_err = abs(clock.dot - target);
  605. if (this_err < err) {
  606. *best_clock = clock;
  607. err = this_err;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. return (err != target);
  614. }
  615. /*
  616. * Returns a set of divisors for the desired target clock with the given
  617. * refclk, or FALSE. The returned values represent the clock equation:
  618. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  619. *
  620. * Target and reference clocks are specified in kHz.
  621. *
  622. * If match_clock is provided, then best_clock P divider must match the P
  623. * divider from @match_clock used for LVDS downclocking.
  624. */
  625. static bool
  626. pnv_find_best_dpll(const struct intel_limit *limit,
  627. struct intel_crtc_state *crtc_state,
  628. int target, int refclk, struct dpll *match_clock,
  629. struct dpll *best_clock)
  630. {
  631. struct drm_device *dev = crtc_state->base.crtc->dev;
  632. struct dpll clock;
  633. int err = target;
  634. memset(best_clock, 0, sizeof(*best_clock));
  635. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  636. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  637. clock.m1++) {
  638. for (clock.m2 = limit->m2.min;
  639. clock.m2 <= limit->m2.max; clock.m2++) {
  640. for (clock.n = limit->n.min;
  641. clock.n <= limit->n.max; clock.n++) {
  642. for (clock.p1 = limit->p1.min;
  643. clock.p1 <= limit->p1.max; clock.p1++) {
  644. int this_err;
  645. pnv_calc_dpll_params(refclk, &clock);
  646. if (!intel_PLL_is_valid(to_i915(dev),
  647. limit,
  648. &clock))
  649. continue;
  650. if (match_clock &&
  651. clock.p != match_clock->p)
  652. continue;
  653. this_err = abs(clock.dot - target);
  654. if (this_err < err) {
  655. *best_clock = clock;
  656. err = this_err;
  657. }
  658. }
  659. }
  660. }
  661. }
  662. return (err != target);
  663. }
  664. /*
  665. * Returns a set of divisors for the desired target clock with the given
  666. * refclk, or FALSE. The returned values represent the clock equation:
  667. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  668. *
  669. * Target and reference clocks are specified in kHz.
  670. *
  671. * If match_clock is provided, then best_clock P divider must match the P
  672. * divider from @match_clock used for LVDS downclocking.
  673. */
  674. static bool
  675. g4x_find_best_dpll(const struct intel_limit *limit,
  676. struct intel_crtc_state *crtc_state,
  677. int target, int refclk, struct dpll *match_clock,
  678. struct dpll *best_clock)
  679. {
  680. struct drm_device *dev = crtc_state->base.crtc->dev;
  681. struct dpll clock;
  682. int max_n;
  683. bool found = false;
  684. /* approximately equals target * 0.00585 */
  685. int err_most = (target >> 8) + (target >> 9);
  686. memset(best_clock, 0, sizeof(*best_clock));
  687. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  688. max_n = limit->n.max;
  689. /* based on hardware requirement, prefer smaller n to precision */
  690. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  691. /* based on hardware requirement, prefere larger m1,m2 */
  692. for (clock.m1 = limit->m1.max;
  693. clock.m1 >= limit->m1.min; clock.m1--) {
  694. for (clock.m2 = limit->m2.max;
  695. clock.m2 >= limit->m2.min; clock.m2--) {
  696. for (clock.p1 = limit->p1.max;
  697. clock.p1 >= limit->p1.min; clock.p1--) {
  698. int this_err;
  699. i9xx_calc_dpll_params(refclk, &clock);
  700. if (!intel_PLL_is_valid(to_i915(dev),
  701. limit,
  702. &clock))
  703. continue;
  704. this_err = abs(clock.dot - target);
  705. if (this_err < err_most) {
  706. *best_clock = clock;
  707. err_most = this_err;
  708. max_n = clock.n;
  709. found = true;
  710. }
  711. }
  712. }
  713. }
  714. }
  715. return found;
  716. }
  717. /*
  718. * Check if the calculated PLL configuration is more optimal compared to the
  719. * best configuration and error found so far. Return the calculated error.
  720. */
  721. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  722. const struct dpll *calculated_clock,
  723. const struct dpll *best_clock,
  724. unsigned int best_error_ppm,
  725. unsigned int *error_ppm)
  726. {
  727. /*
  728. * For CHV ignore the error and consider only the P value.
  729. * Prefer a bigger P value based on HW requirements.
  730. */
  731. if (IS_CHERRYVIEW(to_i915(dev))) {
  732. *error_ppm = 0;
  733. return calculated_clock->p > best_clock->p;
  734. }
  735. if (WARN_ON_ONCE(!target_freq))
  736. return false;
  737. *error_ppm = div_u64(1000000ULL *
  738. abs(target_freq - calculated_clock->dot),
  739. target_freq);
  740. /*
  741. * Prefer a better P value over a better (smaller) error if the error
  742. * is small. Ensure this preference for future configurations too by
  743. * setting the error to 0.
  744. */
  745. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  746. *error_ppm = 0;
  747. return true;
  748. }
  749. return *error_ppm + 10 < best_error_ppm;
  750. }
  751. /*
  752. * Returns a set of divisors for the desired target clock with the given
  753. * refclk, or FALSE. The returned values represent the clock equation:
  754. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  755. */
  756. static bool
  757. vlv_find_best_dpll(const struct intel_limit *limit,
  758. struct intel_crtc_state *crtc_state,
  759. int target, int refclk, struct dpll *match_clock,
  760. struct dpll *best_clock)
  761. {
  762. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  763. struct drm_device *dev = crtc->base.dev;
  764. struct dpll clock;
  765. unsigned int bestppm = 1000000;
  766. /* min update 19.2 MHz */
  767. int max_n = min(limit->n.max, refclk / 19200);
  768. bool found = false;
  769. target *= 5; /* fast clock */
  770. memset(best_clock, 0, sizeof(*best_clock));
  771. /* based on hardware requirement, prefer smaller n to precision */
  772. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  773. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  774. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  775. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  776. clock.p = clock.p1 * clock.p2;
  777. /* based on hardware requirement, prefer bigger m1,m2 values */
  778. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  779. unsigned int ppm;
  780. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  781. refclk * clock.m1);
  782. vlv_calc_dpll_params(refclk, &clock);
  783. if (!intel_PLL_is_valid(to_i915(dev),
  784. limit,
  785. &clock))
  786. continue;
  787. if (!vlv_PLL_is_optimal(dev, target,
  788. &clock,
  789. best_clock,
  790. bestppm, &ppm))
  791. continue;
  792. *best_clock = clock;
  793. bestppm = ppm;
  794. found = true;
  795. }
  796. }
  797. }
  798. }
  799. return found;
  800. }
  801. /*
  802. * Returns a set of divisors for the desired target clock with the given
  803. * refclk, or FALSE. The returned values represent the clock equation:
  804. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  805. */
  806. static bool
  807. chv_find_best_dpll(const struct intel_limit *limit,
  808. struct intel_crtc_state *crtc_state,
  809. int target, int refclk, struct dpll *match_clock,
  810. struct dpll *best_clock)
  811. {
  812. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  813. struct drm_device *dev = crtc->base.dev;
  814. unsigned int best_error_ppm;
  815. struct dpll clock;
  816. uint64_t m2;
  817. int found = false;
  818. memset(best_clock, 0, sizeof(*best_clock));
  819. best_error_ppm = 1000000;
  820. /*
  821. * Based on hardware doc, the n always set to 1, and m1 always
  822. * set to 2. If requires to support 200Mhz refclk, we need to
  823. * revisit this because n may not 1 anymore.
  824. */
  825. clock.n = 1, clock.m1 = 2;
  826. target *= 5; /* fast clock */
  827. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  828. for (clock.p2 = limit->p2.p2_fast;
  829. clock.p2 >= limit->p2.p2_slow;
  830. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  831. unsigned int error_ppm;
  832. clock.p = clock.p1 * clock.p2;
  833. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  834. clock.n) << 22, refclk * clock.m1);
  835. if (m2 > INT_MAX/clock.m1)
  836. continue;
  837. clock.m2 = m2;
  838. chv_calc_dpll_params(refclk, &clock);
  839. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  840. continue;
  841. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  842. best_error_ppm, &error_ppm))
  843. continue;
  844. *best_clock = clock;
  845. best_error_ppm = error_ppm;
  846. found = true;
  847. }
  848. }
  849. return found;
  850. }
  851. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  852. struct dpll *best_clock)
  853. {
  854. int refclk = 100000;
  855. const struct intel_limit *limit = &intel_limits_bxt;
  856. return chv_find_best_dpll(limit, crtc_state,
  857. target_clock, refclk, NULL, best_clock);
  858. }
  859. bool intel_crtc_active(struct intel_crtc *crtc)
  860. {
  861. /* Be paranoid as we can arrive here with only partial
  862. * state retrieved from the hardware during setup.
  863. *
  864. * We can ditch the adjusted_mode.crtc_clock check as soon
  865. * as Haswell has gained clock readout/fastboot support.
  866. *
  867. * We can ditch the crtc->primary->fb check as soon as we can
  868. * properly reconstruct framebuffers.
  869. *
  870. * FIXME: The intel_crtc->active here should be switched to
  871. * crtc->state->active once we have proper CRTC states wired up
  872. * for atomic.
  873. */
  874. return crtc->active && crtc->base.primary->state->fb &&
  875. crtc->config->base.adjusted_mode.crtc_clock;
  876. }
  877. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  878. enum pipe pipe)
  879. {
  880. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  881. return crtc->config->cpu_transcoder;
  882. }
  883. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  884. {
  885. i915_reg_t reg = PIPEDSL(pipe);
  886. u32 line1, line2;
  887. u32 line_mask;
  888. if (IS_GEN2(dev_priv))
  889. line_mask = DSL_LINEMASK_GEN2;
  890. else
  891. line_mask = DSL_LINEMASK_GEN3;
  892. line1 = I915_READ(reg) & line_mask;
  893. msleep(5);
  894. line2 = I915_READ(reg) & line_mask;
  895. return line1 == line2;
  896. }
  897. /*
  898. * intel_wait_for_pipe_off - wait for pipe to turn off
  899. * @crtc: crtc whose pipe to wait for
  900. *
  901. * After disabling a pipe, we can't wait for vblank in the usual way,
  902. * spinning on the vblank interrupt status bit, since we won't actually
  903. * see an interrupt when the pipe is disabled.
  904. *
  905. * On Gen4 and above:
  906. * wait for the pipe register state bit to turn off
  907. *
  908. * Otherwise:
  909. * wait for the display line value to settle (it usually
  910. * ends up stopping at the start of the next frame).
  911. *
  912. */
  913. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  914. {
  915. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  916. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  917. enum pipe pipe = crtc->pipe;
  918. if (INTEL_GEN(dev_priv) >= 4) {
  919. i915_reg_t reg = PIPECONF(cpu_transcoder);
  920. /* Wait for the Pipe State to go off */
  921. if (intel_wait_for_register(dev_priv,
  922. reg, I965_PIPECONF_ACTIVE, 0,
  923. 100))
  924. WARN(1, "pipe_off wait timed out\n");
  925. } else {
  926. /* Wait for the display line to settle */
  927. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  928. WARN(1, "pipe_off wait timed out\n");
  929. }
  930. }
  931. /* Only for pre-ILK configs */
  932. void assert_pll(struct drm_i915_private *dev_priv,
  933. enum pipe pipe, bool state)
  934. {
  935. u32 val;
  936. bool cur_state;
  937. val = I915_READ(DPLL(pipe));
  938. cur_state = !!(val & DPLL_VCO_ENABLE);
  939. I915_STATE_WARN(cur_state != state,
  940. "PLL state assertion failure (expected %s, current %s)\n",
  941. onoff(state), onoff(cur_state));
  942. }
  943. /* XXX: the dsi pll is shared between MIPI DSI ports */
  944. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  945. {
  946. u32 val;
  947. bool cur_state;
  948. mutex_lock(&dev_priv->sb_lock);
  949. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  950. mutex_unlock(&dev_priv->sb_lock);
  951. cur_state = val & DSI_PLL_VCO_EN;
  952. I915_STATE_WARN(cur_state != state,
  953. "DSI PLL state assertion failure (expected %s, current %s)\n",
  954. onoff(state), onoff(cur_state));
  955. }
  956. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  957. enum pipe pipe, bool state)
  958. {
  959. bool cur_state;
  960. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  961. pipe);
  962. if (HAS_DDI(dev_priv)) {
  963. /* DDI does not have a specific FDI_TX register */
  964. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  965. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  966. } else {
  967. u32 val = I915_READ(FDI_TX_CTL(pipe));
  968. cur_state = !!(val & FDI_TX_ENABLE);
  969. }
  970. I915_STATE_WARN(cur_state != state,
  971. "FDI TX state assertion failure (expected %s, current %s)\n",
  972. onoff(state), onoff(cur_state));
  973. }
  974. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  975. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  976. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  977. enum pipe pipe, bool state)
  978. {
  979. u32 val;
  980. bool cur_state;
  981. val = I915_READ(FDI_RX_CTL(pipe));
  982. cur_state = !!(val & FDI_RX_ENABLE);
  983. I915_STATE_WARN(cur_state != state,
  984. "FDI RX state assertion failure (expected %s, current %s)\n",
  985. onoff(state), onoff(cur_state));
  986. }
  987. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  988. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  989. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (IS_GEN5(dev_priv))
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv))
  998. return;
  999. val = I915_READ(FDI_TX_CTL(pipe));
  1000. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1001. }
  1002. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe, bool state)
  1004. {
  1005. u32 val;
  1006. bool cur_state;
  1007. val = I915_READ(FDI_RX_CTL(pipe));
  1008. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1009. I915_STATE_WARN(cur_state != state,
  1010. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1011. onoff(state), onoff(cur_state));
  1012. }
  1013. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1014. {
  1015. i915_reg_t pp_reg;
  1016. u32 val;
  1017. enum pipe panel_pipe = PIPE_A;
  1018. bool locked = true;
  1019. if (WARN_ON(HAS_DDI(dev_priv)))
  1020. return;
  1021. if (HAS_PCH_SPLIT(dev_priv)) {
  1022. u32 port_sel;
  1023. pp_reg = PP_CONTROL(0);
  1024. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1025. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1026. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1027. panel_pipe = PIPE_B;
  1028. /* XXX: else fix for eDP */
  1029. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1030. /* presumably write lock depends on pipe, not port select */
  1031. pp_reg = PP_CONTROL(pipe);
  1032. panel_pipe = pipe;
  1033. } else {
  1034. pp_reg = PP_CONTROL(0);
  1035. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1036. panel_pipe = PIPE_B;
  1037. }
  1038. val = I915_READ(pp_reg);
  1039. if (!(val & PANEL_POWER_ON) ||
  1040. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1041. locked = false;
  1042. I915_STATE_WARN(panel_pipe == pipe && locked,
  1043. "panel assertion failure, pipe %c regs locked\n",
  1044. pipe_name(pipe));
  1045. }
  1046. static void assert_cursor(struct drm_i915_private *dev_priv,
  1047. enum pipe pipe, bool state)
  1048. {
  1049. bool cur_state;
  1050. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1051. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1052. else
  1053. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1054. I915_STATE_WARN(cur_state != state,
  1055. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1056. pipe_name(pipe), onoff(state), onoff(cur_state));
  1057. }
  1058. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1059. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1060. void assert_pipe(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe, bool state)
  1062. {
  1063. bool cur_state;
  1064. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1065. pipe);
  1066. enum intel_display_power_domain power_domain;
  1067. /* we keep both pipes enabled on 830 */
  1068. if (IS_I830(dev_priv))
  1069. state = true;
  1070. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1071. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1072. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1073. cur_state = !!(val & PIPECONF_ENABLE);
  1074. intel_display_power_put(dev_priv, power_domain);
  1075. } else {
  1076. cur_state = false;
  1077. }
  1078. I915_STATE_WARN(cur_state != state,
  1079. "pipe %c assertion failure (expected %s, current %s)\n",
  1080. pipe_name(pipe), onoff(state), onoff(cur_state));
  1081. }
  1082. static void assert_plane(struct drm_i915_private *dev_priv,
  1083. enum plane plane, bool state)
  1084. {
  1085. u32 val;
  1086. bool cur_state;
  1087. val = I915_READ(DSPCNTR(plane));
  1088. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1089. I915_STATE_WARN(cur_state != state,
  1090. "plane %c assertion failure (expected %s, current %s)\n",
  1091. plane_name(plane), onoff(state), onoff(cur_state));
  1092. }
  1093. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1094. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1095. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1096. enum pipe pipe)
  1097. {
  1098. int i;
  1099. /* Primary planes are fixed to pipes on gen4+ */
  1100. if (INTEL_GEN(dev_priv) >= 4) {
  1101. u32 val = I915_READ(DSPCNTR(pipe));
  1102. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1103. "plane %c assertion failure, should be disabled but not\n",
  1104. plane_name(pipe));
  1105. return;
  1106. }
  1107. /* Need to check both planes against the pipe */
  1108. for_each_pipe(dev_priv, i) {
  1109. u32 val = I915_READ(DSPCNTR(i));
  1110. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1111. DISPPLANE_SEL_PIPE_SHIFT;
  1112. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1113. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1114. plane_name(i), pipe_name(pipe));
  1115. }
  1116. }
  1117. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe)
  1119. {
  1120. int sprite;
  1121. if (INTEL_GEN(dev_priv) >= 9) {
  1122. for_each_sprite(dev_priv, pipe, sprite) {
  1123. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1124. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1125. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1126. sprite, pipe_name(pipe));
  1127. }
  1128. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1129. for_each_sprite(dev_priv, pipe, sprite) {
  1130. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1131. I915_STATE_WARN(val & SP_ENABLE,
  1132. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1133. sprite_name(pipe, sprite), pipe_name(pipe));
  1134. }
  1135. } else if (INTEL_GEN(dev_priv) >= 7) {
  1136. u32 val = I915_READ(SPRCTL(pipe));
  1137. I915_STATE_WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  1141. u32 val = I915_READ(DVSCNTR(pipe));
  1142. I915_STATE_WARN(val & DVS_ENABLE,
  1143. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1144. plane_name(pipe), pipe_name(pipe));
  1145. }
  1146. }
  1147. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1148. {
  1149. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1150. drm_crtc_vblank_put(crtc);
  1151. }
  1152. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. u32 val;
  1156. bool enabled;
  1157. val = I915_READ(PCH_TRANSCONF(pipe));
  1158. enabled = !!(val & TRANS_ENABLE);
  1159. I915_STATE_WARN(enabled,
  1160. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1161. pipe_name(pipe));
  1162. }
  1163. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1164. enum pipe pipe, u32 port_sel, u32 val)
  1165. {
  1166. if ((val & DP_PORT_EN) == 0)
  1167. return false;
  1168. if (HAS_PCH_CPT(dev_priv)) {
  1169. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1170. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1171. return false;
  1172. } else if (IS_CHERRYVIEW(dev_priv)) {
  1173. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1174. return false;
  1175. } else {
  1176. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1177. return false;
  1178. }
  1179. return true;
  1180. }
  1181. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1182. enum pipe pipe, u32 val)
  1183. {
  1184. if ((val & SDVO_ENABLE) == 0)
  1185. return false;
  1186. if (HAS_PCH_CPT(dev_priv)) {
  1187. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1188. return false;
  1189. } else if (IS_CHERRYVIEW(dev_priv)) {
  1190. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1191. return false;
  1192. } else {
  1193. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1194. return false;
  1195. }
  1196. return true;
  1197. }
  1198. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe, u32 val)
  1200. {
  1201. if ((val & LVDS_PORT_EN) == 0)
  1202. return false;
  1203. if (HAS_PCH_CPT(dev_priv)) {
  1204. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1205. return false;
  1206. } else {
  1207. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1208. return false;
  1209. }
  1210. return true;
  1211. }
  1212. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1213. enum pipe pipe, u32 val)
  1214. {
  1215. if ((val & ADPA_DAC_ENABLE) == 0)
  1216. return false;
  1217. if (HAS_PCH_CPT(dev_priv)) {
  1218. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1219. return false;
  1220. } else {
  1221. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1222. return false;
  1223. }
  1224. return true;
  1225. }
  1226. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1227. enum pipe pipe, i915_reg_t reg,
  1228. u32 port_sel)
  1229. {
  1230. u32 val = I915_READ(reg);
  1231. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1232. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1233. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1234. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1235. && (val & DP_PIPEB_SELECT),
  1236. "IBX PCH dp port still using transcoder B\n");
  1237. }
  1238. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1239. enum pipe pipe, i915_reg_t reg)
  1240. {
  1241. u32 val = I915_READ(reg);
  1242. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1243. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1244. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1245. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1246. && (val & SDVO_PIPE_B_SELECT),
  1247. "IBX PCH hdmi port still using transcoder B\n");
  1248. }
  1249. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe)
  1251. {
  1252. u32 val;
  1253. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1254. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1255. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1256. val = I915_READ(PCH_ADPA);
  1257. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1258. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1259. pipe_name(pipe));
  1260. val = I915_READ(PCH_LVDS);
  1261. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1262. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1263. pipe_name(pipe));
  1264. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1265. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1266. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1267. }
  1268. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1269. const struct intel_crtc_state *pipe_config)
  1270. {
  1271. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1272. enum pipe pipe = crtc->pipe;
  1273. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1274. POSTING_READ(DPLL(pipe));
  1275. udelay(150);
  1276. if (intel_wait_for_register(dev_priv,
  1277. DPLL(pipe),
  1278. DPLL_LOCK_VLV,
  1279. DPLL_LOCK_VLV,
  1280. 1))
  1281. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1282. }
  1283. static void vlv_enable_pll(struct intel_crtc *crtc,
  1284. const struct intel_crtc_state *pipe_config)
  1285. {
  1286. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1287. enum pipe pipe = crtc->pipe;
  1288. assert_pipe_disabled(dev_priv, pipe);
  1289. /* PLL is protected by panel, make sure we can write it */
  1290. assert_panel_unlocked(dev_priv, pipe);
  1291. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1292. _vlv_enable_pll(crtc, pipe_config);
  1293. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1294. POSTING_READ(DPLL_MD(pipe));
  1295. }
  1296. static void _chv_enable_pll(struct intel_crtc *crtc,
  1297. const struct intel_crtc_state *pipe_config)
  1298. {
  1299. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1300. enum pipe pipe = crtc->pipe;
  1301. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1302. u32 tmp;
  1303. mutex_lock(&dev_priv->sb_lock);
  1304. /* Enable back the 10bit clock to display controller */
  1305. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1306. tmp |= DPIO_DCLKP_EN;
  1307. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1308. mutex_unlock(&dev_priv->sb_lock);
  1309. /*
  1310. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1311. */
  1312. udelay(1);
  1313. /* Enable PLL */
  1314. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1315. /* Check PLL is locked */
  1316. if (intel_wait_for_register(dev_priv,
  1317. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1318. 1))
  1319. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1320. }
  1321. static void chv_enable_pll(struct intel_crtc *crtc,
  1322. const struct intel_crtc_state *pipe_config)
  1323. {
  1324. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1325. enum pipe pipe = crtc->pipe;
  1326. assert_pipe_disabled(dev_priv, pipe);
  1327. /* PLL is protected by panel, make sure we can write it */
  1328. assert_panel_unlocked(dev_priv, pipe);
  1329. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1330. _chv_enable_pll(crtc, pipe_config);
  1331. if (pipe != PIPE_A) {
  1332. /*
  1333. * WaPixelRepeatModeFixForC0:chv
  1334. *
  1335. * DPLLCMD is AWOL. Use chicken bits to propagate
  1336. * the value from DPLLBMD to either pipe B or C.
  1337. */
  1338. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1339. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1340. I915_WRITE(CBR4_VLV, 0);
  1341. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1342. /*
  1343. * DPLLB VGA mode also seems to cause problems.
  1344. * We should always have it disabled.
  1345. */
  1346. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1347. } else {
  1348. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1349. POSTING_READ(DPLL_MD(pipe));
  1350. }
  1351. }
  1352. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1353. {
  1354. struct intel_crtc *crtc;
  1355. int count = 0;
  1356. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1357. count += crtc->base.state->active &&
  1358. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1359. }
  1360. return count;
  1361. }
  1362. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1363. {
  1364. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1365. i915_reg_t reg = DPLL(crtc->pipe);
  1366. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1367. int i;
  1368. assert_pipe_disabled(dev_priv, crtc->pipe);
  1369. /* PLL is protected by panel, make sure we can write it */
  1370. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1371. assert_panel_unlocked(dev_priv, crtc->pipe);
  1372. /* Enable DVO 2x clock on both PLLs if necessary */
  1373. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1374. /*
  1375. * It appears to be important that we don't enable this
  1376. * for the current pipe before otherwise configuring the
  1377. * PLL. No idea how this should be handled if multiple
  1378. * DVO outputs are enabled simultaneosly.
  1379. */
  1380. dpll |= DPLL_DVO_2X_MODE;
  1381. I915_WRITE(DPLL(!crtc->pipe),
  1382. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1383. }
  1384. /*
  1385. * Apparently we need to have VGA mode enabled prior to changing
  1386. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1387. * dividers, even though the register value does change.
  1388. */
  1389. I915_WRITE(reg, 0);
  1390. I915_WRITE(reg, dpll);
  1391. /* Wait for the clocks to stabilize. */
  1392. POSTING_READ(reg);
  1393. udelay(150);
  1394. if (INTEL_GEN(dev_priv) >= 4) {
  1395. I915_WRITE(DPLL_MD(crtc->pipe),
  1396. crtc->config->dpll_hw_state.dpll_md);
  1397. } else {
  1398. /* The pixel multiplier can only be updated once the
  1399. * DPLL is enabled and the clocks are stable.
  1400. *
  1401. * So write it again.
  1402. */
  1403. I915_WRITE(reg, dpll);
  1404. }
  1405. /* We do this three times for luck */
  1406. for (i = 0; i < 3; i++) {
  1407. I915_WRITE(reg, dpll);
  1408. POSTING_READ(reg);
  1409. udelay(150); /* wait for warmup */
  1410. }
  1411. }
  1412. /**
  1413. * i9xx_disable_pll - disable a PLL
  1414. * @dev_priv: i915 private structure
  1415. * @pipe: pipe PLL to disable
  1416. *
  1417. * Disable the PLL for @pipe, making sure the pipe is off first.
  1418. *
  1419. * Note! This is for pre-ILK only.
  1420. */
  1421. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1422. {
  1423. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1424. enum pipe pipe = crtc->pipe;
  1425. /* Disable DVO 2x clock on both PLLs if necessary */
  1426. if (IS_I830(dev_priv) &&
  1427. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1428. !intel_num_dvo_pipes(dev_priv)) {
  1429. I915_WRITE(DPLL(PIPE_B),
  1430. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1431. I915_WRITE(DPLL(PIPE_A),
  1432. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1433. }
  1434. /* Don't disable pipe or pipe PLLs if needed */
  1435. if (IS_I830(dev_priv))
  1436. return;
  1437. /* Make sure the pipe isn't still relying on us */
  1438. assert_pipe_disabled(dev_priv, pipe);
  1439. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1440. POSTING_READ(DPLL(pipe));
  1441. }
  1442. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1443. {
  1444. u32 val;
  1445. /* Make sure the pipe isn't still relying on us */
  1446. assert_pipe_disabled(dev_priv, pipe);
  1447. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1448. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1449. if (pipe != PIPE_A)
  1450. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1451. I915_WRITE(DPLL(pipe), val);
  1452. POSTING_READ(DPLL(pipe));
  1453. }
  1454. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1455. {
  1456. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1457. u32 val;
  1458. /* Make sure the pipe isn't still relying on us */
  1459. assert_pipe_disabled(dev_priv, pipe);
  1460. val = DPLL_SSC_REF_CLK_CHV |
  1461. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1462. if (pipe != PIPE_A)
  1463. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1464. I915_WRITE(DPLL(pipe), val);
  1465. POSTING_READ(DPLL(pipe));
  1466. mutex_lock(&dev_priv->sb_lock);
  1467. /* Disable 10bit clock to display controller */
  1468. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1469. val &= ~DPIO_DCLKP_EN;
  1470. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1471. mutex_unlock(&dev_priv->sb_lock);
  1472. }
  1473. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1474. struct intel_digital_port *dport,
  1475. unsigned int expected_mask)
  1476. {
  1477. u32 port_mask;
  1478. i915_reg_t dpll_reg;
  1479. switch (dport->port) {
  1480. case PORT_B:
  1481. port_mask = DPLL_PORTB_READY_MASK;
  1482. dpll_reg = DPLL(0);
  1483. break;
  1484. case PORT_C:
  1485. port_mask = DPLL_PORTC_READY_MASK;
  1486. dpll_reg = DPLL(0);
  1487. expected_mask <<= 4;
  1488. break;
  1489. case PORT_D:
  1490. port_mask = DPLL_PORTD_READY_MASK;
  1491. dpll_reg = DPIO_PHY_STATUS;
  1492. break;
  1493. default:
  1494. BUG();
  1495. }
  1496. if (intel_wait_for_register(dev_priv,
  1497. dpll_reg, port_mask, expected_mask,
  1498. 1000))
  1499. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1500. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1501. }
  1502. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1503. enum pipe pipe)
  1504. {
  1505. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1506. pipe);
  1507. i915_reg_t reg;
  1508. uint32_t val, pipeconf_val;
  1509. /* Make sure PCH DPLL is enabled */
  1510. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1511. /* FDI must be feeding us bits for PCH ports */
  1512. assert_fdi_tx_enabled(dev_priv, pipe);
  1513. assert_fdi_rx_enabled(dev_priv, pipe);
  1514. if (HAS_PCH_CPT(dev_priv)) {
  1515. /* Workaround: Set the timing override bit before enabling the
  1516. * pch transcoder. */
  1517. reg = TRANS_CHICKEN2(pipe);
  1518. val = I915_READ(reg);
  1519. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1520. I915_WRITE(reg, val);
  1521. }
  1522. reg = PCH_TRANSCONF(pipe);
  1523. val = I915_READ(reg);
  1524. pipeconf_val = I915_READ(PIPECONF(pipe));
  1525. if (HAS_PCH_IBX(dev_priv)) {
  1526. /*
  1527. * Make the BPC in transcoder be consistent with
  1528. * that in pipeconf reg. For HDMI we must use 8bpc
  1529. * here for both 8bpc and 12bpc.
  1530. */
  1531. val &= ~PIPECONF_BPC_MASK;
  1532. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1533. val |= PIPECONF_8BPC;
  1534. else
  1535. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1536. }
  1537. val &= ~TRANS_INTERLACE_MASK;
  1538. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1539. if (HAS_PCH_IBX(dev_priv) &&
  1540. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1541. val |= TRANS_LEGACY_INTERLACED_ILK;
  1542. else
  1543. val |= TRANS_INTERLACED;
  1544. else
  1545. val |= TRANS_PROGRESSIVE;
  1546. I915_WRITE(reg, val | TRANS_ENABLE);
  1547. if (intel_wait_for_register(dev_priv,
  1548. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1549. 100))
  1550. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1551. }
  1552. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1553. enum transcoder cpu_transcoder)
  1554. {
  1555. u32 val, pipeconf_val;
  1556. /* FDI must be feeding us bits for PCH ports */
  1557. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1558. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1559. /* Workaround: set timing override bit. */
  1560. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1561. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1562. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1563. val = TRANS_ENABLE;
  1564. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1565. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1566. PIPECONF_INTERLACED_ILK)
  1567. val |= TRANS_INTERLACED;
  1568. else
  1569. val |= TRANS_PROGRESSIVE;
  1570. I915_WRITE(LPT_TRANSCONF, val);
  1571. if (intel_wait_for_register(dev_priv,
  1572. LPT_TRANSCONF,
  1573. TRANS_STATE_ENABLE,
  1574. TRANS_STATE_ENABLE,
  1575. 100))
  1576. DRM_ERROR("Failed to enable PCH transcoder\n");
  1577. }
  1578. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1579. enum pipe pipe)
  1580. {
  1581. i915_reg_t reg;
  1582. uint32_t val;
  1583. /* FDI relies on the transcoder */
  1584. assert_fdi_tx_disabled(dev_priv, pipe);
  1585. assert_fdi_rx_disabled(dev_priv, pipe);
  1586. /* Ports must be off as well */
  1587. assert_pch_ports_disabled(dev_priv, pipe);
  1588. reg = PCH_TRANSCONF(pipe);
  1589. val = I915_READ(reg);
  1590. val &= ~TRANS_ENABLE;
  1591. I915_WRITE(reg, val);
  1592. /* wait for PCH transcoder off, transcoder state */
  1593. if (intel_wait_for_register(dev_priv,
  1594. reg, TRANS_STATE_ENABLE, 0,
  1595. 50))
  1596. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1597. if (HAS_PCH_CPT(dev_priv)) {
  1598. /* Workaround: Clear the timing override chicken bit again. */
  1599. reg = TRANS_CHICKEN2(pipe);
  1600. val = I915_READ(reg);
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(reg, val);
  1603. }
  1604. }
  1605. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1606. {
  1607. u32 val;
  1608. val = I915_READ(LPT_TRANSCONF);
  1609. val &= ~TRANS_ENABLE;
  1610. I915_WRITE(LPT_TRANSCONF, val);
  1611. /* wait for PCH transcoder off, transcoder state */
  1612. if (intel_wait_for_register(dev_priv,
  1613. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1614. 50))
  1615. DRM_ERROR("Failed to disable PCH transcoder\n");
  1616. /* Workaround: clear timing override bit. */
  1617. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1618. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1619. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1620. }
  1621. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1622. {
  1623. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1624. WARN_ON(!crtc->config->has_pch_encoder);
  1625. if (HAS_PCH_LPT(dev_priv))
  1626. return PIPE_A;
  1627. else
  1628. return crtc->pipe;
  1629. }
  1630. /**
  1631. * intel_enable_pipe - enable a pipe, asserting requirements
  1632. * @crtc: crtc responsible for the pipe
  1633. *
  1634. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1635. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1636. */
  1637. static void intel_enable_pipe(struct intel_crtc *crtc)
  1638. {
  1639. struct drm_device *dev = crtc->base.dev;
  1640. struct drm_i915_private *dev_priv = to_i915(dev);
  1641. enum pipe pipe = crtc->pipe;
  1642. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1643. i915_reg_t reg;
  1644. u32 val;
  1645. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1646. assert_planes_disabled(dev_priv, pipe);
  1647. assert_cursor_disabled(dev_priv, pipe);
  1648. assert_sprites_disabled(dev_priv, pipe);
  1649. /*
  1650. * A pipe without a PLL won't actually be able to drive bits from
  1651. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1652. * need the check.
  1653. */
  1654. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1655. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1656. assert_dsi_pll_enabled(dev_priv);
  1657. else
  1658. assert_pll_enabled(dev_priv, pipe);
  1659. } else {
  1660. if (crtc->config->has_pch_encoder) {
  1661. /* if driving the PCH, we need FDI enabled */
  1662. assert_fdi_rx_pll_enabled(dev_priv,
  1663. intel_crtc_pch_transcoder(crtc));
  1664. assert_fdi_tx_pll_enabled(dev_priv,
  1665. (enum pipe) cpu_transcoder);
  1666. }
  1667. /* FIXME: assert CPU port conditions for SNB+ */
  1668. }
  1669. reg = PIPECONF(cpu_transcoder);
  1670. val = I915_READ(reg);
  1671. if (val & PIPECONF_ENABLE) {
  1672. /* we keep both pipes enabled on 830 */
  1673. WARN_ON(!IS_I830(dev_priv));
  1674. return;
  1675. }
  1676. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1677. POSTING_READ(reg);
  1678. /*
  1679. * Until the pipe starts DSL will read as 0, which would cause
  1680. * an apparent vblank timestamp jump, which messes up also the
  1681. * frame count when it's derived from the timestamps. So let's
  1682. * wait for the pipe to start properly before we call
  1683. * drm_crtc_vblank_on()
  1684. */
  1685. if (dev->max_vblank_count == 0 &&
  1686. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1687. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1688. }
  1689. /**
  1690. * intel_disable_pipe - disable a pipe, asserting requirements
  1691. * @crtc: crtc whose pipes is to be disabled
  1692. *
  1693. * Disable the pipe of @crtc, making sure that various hardware
  1694. * specific requirements are met, if applicable, e.g. plane
  1695. * disabled, panel fitter off, etc.
  1696. *
  1697. * Will wait until the pipe has shut down before returning.
  1698. */
  1699. static void intel_disable_pipe(struct intel_crtc *crtc)
  1700. {
  1701. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1702. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1703. enum pipe pipe = crtc->pipe;
  1704. i915_reg_t reg;
  1705. u32 val;
  1706. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1707. /*
  1708. * Make sure planes won't keep trying to pump pixels to us,
  1709. * or we might hang the display.
  1710. */
  1711. assert_planes_disabled(dev_priv, pipe);
  1712. assert_cursor_disabled(dev_priv, pipe);
  1713. assert_sprites_disabled(dev_priv, pipe);
  1714. reg = PIPECONF(cpu_transcoder);
  1715. val = I915_READ(reg);
  1716. if ((val & PIPECONF_ENABLE) == 0)
  1717. return;
  1718. /*
  1719. * Double wide has implications for planes
  1720. * so best keep it disabled when not needed.
  1721. */
  1722. if (crtc->config->double_wide)
  1723. val &= ~PIPECONF_DOUBLE_WIDE;
  1724. /* Don't disable pipe or pipe PLLs if needed */
  1725. if (!IS_I830(dev_priv))
  1726. val &= ~PIPECONF_ENABLE;
  1727. I915_WRITE(reg, val);
  1728. if ((val & PIPECONF_ENABLE) == 0)
  1729. intel_wait_for_pipe_off(crtc);
  1730. }
  1731. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1732. {
  1733. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1734. }
  1735. static unsigned int
  1736. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1739. unsigned int cpp = fb->format->cpp[plane];
  1740. switch (fb->modifier) {
  1741. case DRM_FORMAT_MOD_LINEAR:
  1742. return cpp;
  1743. case I915_FORMAT_MOD_X_TILED:
  1744. if (IS_GEN2(dev_priv))
  1745. return 128;
  1746. else
  1747. return 512;
  1748. case I915_FORMAT_MOD_Y_TILED_CCS:
  1749. if (plane == 1)
  1750. return 128;
  1751. /* fall through */
  1752. case I915_FORMAT_MOD_Y_TILED:
  1753. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1754. return 128;
  1755. else
  1756. return 512;
  1757. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1758. if (plane == 1)
  1759. return 128;
  1760. /* fall through */
  1761. case I915_FORMAT_MOD_Yf_TILED:
  1762. switch (cpp) {
  1763. case 1:
  1764. return 64;
  1765. case 2:
  1766. case 4:
  1767. return 128;
  1768. case 8:
  1769. case 16:
  1770. return 256;
  1771. default:
  1772. MISSING_CASE(cpp);
  1773. return cpp;
  1774. }
  1775. break;
  1776. default:
  1777. MISSING_CASE(fb->modifier);
  1778. return cpp;
  1779. }
  1780. }
  1781. static unsigned int
  1782. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1783. {
  1784. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1785. return 1;
  1786. else
  1787. return intel_tile_size(to_i915(fb->dev)) /
  1788. intel_tile_width_bytes(fb, plane);
  1789. }
  1790. /* Return the tile dimensions in pixel units */
  1791. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1792. unsigned int *tile_width,
  1793. unsigned int *tile_height)
  1794. {
  1795. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1796. unsigned int cpp = fb->format->cpp[plane];
  1797. *tile_width = tile_width_bytes / cpp;
  1798. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1799. }
  1800. unsigned int
  1801. intel_fb_align_height(const struct drm_framebuffer *fb,
  1802. int plane, unsigned int height)
  1803. {
  1804. unsigned int tile_height = intel_tile_height(fb, plane);
  1805. return ALIGN(height, tile_height);
  1806. }
  1807. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1808. {
  1809. unsigned int size = 0;
  1810. int i;
  1811. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1812. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1813. return size;
  1814. }
  1815. static void
  1816. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1817. const struct drm_framebuffer *fb,
  1818. unsigned int rotation)
  1819. {
  1820. view->type = I915_GGTT_VIEW_NORMAL;
  1821. if (drm_rotation_90_or_270(rotation)) {
  1822. view->type = I915_GGTT_VIEW_ROTATED;
  1823. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1824. }
  1825. }
  1826. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1827. {
  1828. if (IS_I830(dev_priv))
  1829. return 16 * 1024;
  1830. else if (IS_I85X(dev_priv))
  1831. return 256;
  1832. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1833. return 32;
  1834. else
  1835. return 4 * 1024;
  1836. }
  1837. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1838. {
  1839. if (INTEL_INFO(dev_priv)->gen >= 9)
  1840. return 256 * 1024;
  1841. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1842. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1843. return 128 * 1024;
  1844. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1845. return 4 * 1024;
  1846. else
  1847. return 0;
  1848. }
  1849. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1850. int plane)
  1851. {
  1852. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1853. /* AUX_DIST needs only 4K alignment */
  1854. if (plane == 1)
  1855. return 4096;
  1856. switch (fb->modifier) {
  1857. case DRM_FORMAT_MOD_LINEAR:
  1858. return intel_linear_alignment(dev_priv);
  1859. case I915_FORMAT_MOD_X_TILED:
  1860. if (INTEL_GEN(dev_priv) >= 9)
  1861. return 256 * 1024;
  1862. return 0;
  1863. case I915_FORMAT_MOD_Y_TILED_CCS:
  1864. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1865. case I915_FORMAT_MOD_Y_TILED:
  1866. case I915_FORMAT_MOD_Yf_TILED:
  1867. return 1 * 1024 * 1024;
  1868. default:
  1869. MISSING_CASE(fb->modifier);
  1870. return 0;
  1871. }
  1872. }
  1873. struct i915_vma *
  1874. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1875. {
  1876. struct drm_device *dev = fb->dev;
  1877. struct drm_i915_private *dev_priv = to_i915(dev);
  1878. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1879. struct i915_ggtt_view view;
  1880. struct i915_vma *vma;
  1881. u32 alignment;
  1882. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1883. alignment = intel_surf_alignment(fb, 0);
  1884. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1885. /* Note that the w/a also requires 64 PTE of padding following the
  1886. * bo. We currently fill all unused PTE with the shadow page and so
  1887. * we should always have valid PTE following the scanout preventing
  1888. * the VT-d warning.
  1889. */
  1890. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1891. alignment = 256 * 1024;
  1892. /*
  1893. * Global gtt pte registers are special registers which actually forward
  1894. * writes to a chunk of system memory. Which means that there is no risk
  1895. * that the register values disappear as soon as we call
  1896. * intel_runtime_pm_put(), so it is correct to wrap only the
  1897. * pin/unpin/fence and not more.
  1898. */
  1899. intel_runtime_pm_get(dev_priv);
  1900. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1901. if (IS_ERR(vma))
  1902. goto err;
  1903. if (i915_vma_is_map_and_fenceable(vma)) {
  1904. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1905. * fence, whereas 965+ only requires a fence if using
  1906. * framebuffer compression. For simplicity, we always, when
  1907. * possible, install a fence as the cost is not that onerous.
  1908. *
  1909. * If we fail to fence the tiled scanout, then either the
  1910. * modeset will reject the change (which is highly unlikely as
  1911. * the affected systems, all but one, do not have unmappable
  1912. * space) or we will not be able to enable full powersaving
  1913. * techniques (also likely not to apply due to various limits
  1914. * FBC and the like impose on the size of the buffer, which
  1915. * presumably we violated anyway with this unmappable buffer).
  1916. * Anyway, it is presumably better to stumble onwards with
  1917. * something and try to run the system in a "less than optimal"
  1918. * mode that matches the user configuration.
  1919. */
  1920. if (i915_vma_get_fence(vma) == 0)
  1921. i915_vma_pin_fence(vma);
  1922. }
  1923. i915_vma_get(vma);
  1924. err:
  1925. intel_runtime_pm_put(dev_priv);
  1926. return vma;
  1927. }
  1928. void intel_unpin_fb_vma(struct i915_vma *vma)
  1929. {
  1930. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1931. i915_vma_unpin_fence(vma);
  1932. i915_gem_object_unpin_from_display_plane(vma);
  1933. i915_vma_put(vma);
  1934. }
  1935. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1936. unsigned int rotation)
  1937. {
  1938. if (drm_rotation_90_or_270(rotation))
  1939. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1940. else
  1941. return fb->pitches[plane];
  1942. }
  1943. /*
  1944. * Convert the x/y offsets into a linear offset.
  1945. * Only valid with 0/180 degree rotation, which is fine since linear
  1946. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1947. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1948. */
  1949. u32 intel_fb_xy_to_linear(int x, int y,
  1950. const struct intel_plane_state *state,
  1951. int plane)
  1952. {
  1953. const struct drm_framebuffer *fb = state->base.fb;
  1954. unsigned int cpp = fb->format->cpp[plane];
  1955. unsigned int pitch = fb->pitches[plane];
  1956. return y * pitch + x * cpp;
  1957. }
  1958. /*
  1959. * Add the x/y offsets derived from fb->offsets[] to the user
  1960. * specified plane src x/y offsets. The resulting x/y offsets
  1961. * specify the start of scanout from the beginning of the gtt mapping.
  1962. */
  1963. void intel_add_fb_offsets(int *x, int *y,
  1964. const struct intel_plane_state *state,
  1965. int plane)
  1966. {
  1967. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1968. unsigned int rotation = state->base.rotation;
  1969. if (drm_rotation_90_or_270(rotation)) {
  1970. *x += intel_fb->rotated[plane].x;
  1971. *y += intel_fb->rotated[plane].y;
  1972. } else {
  1973. *x += intel_fb->normal[plane].x;
  1974. *y += intel_fb->normal[plane].y;
  1975. }
  1976. }
  1977. /*
  1978. * Input tile dimensions and pitch must already be
  1979. * rotated to match x and y, and in pixel units.
  1980. */
  1981. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1982. unsigned int tile_width,
  1983. unsigned int tile_height,
  1984. unsigned int tile_size,
  1985. unsigned int pitch_tiles,
  1986. u32 old_offset,
  1987. u32 new_offset)
  1988. {
  1989. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1990. unsigned int tiles;
  1991. WARN_ON(old_offset & (tile_size - 1));
  1992. WARN_ON(new_offset & (tile_size - 1));
  1993. WARN_ON(new_offset > old_offset);
  1994. tiles = (old_offset - new_offset) / tile_size;
  1995. *y += tiles / pitch_tiles * tile_height;
  1996. *x += tiles % pitch_tiles * tile_width;
  1997. /* minimize x in case it got needlessly big */
  1998. *y += *x / pitch_pixels * tile_height;
  1999. *x %= pitch_pixels;
  2000. return new_offset;
  2001. }
  2002. /*
  2003. * Adjust the tile offset by moving the difference into
  2004. * the x/y offsets.
  2005. */
  2006. static u32 intel_adjust_tile_offset(int *x, int *y,
  2007. const struct intel_plane_state *state, int plane,
  2008. u32 old_offset, u32 new_offset)
  2009. {
  2010. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2011. const struct drm_framebuffer *fb = state->base.fb;
  2012. unsigned int cpp = fb->format->cpp[plane];
  2013. unsigned int rotation = state->base.rotation;
  2014. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2015. WARN_ON(new_offset > old_offset);
  2016. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2017. unsigned int tile_size, tile_width, tile_height;
  2018. unsigned int pitch_tiles;
  2019. tile_size = intel_tile_size(dev_priv);
  2020. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2021. if (drm_rotation_90_or_270(rotation)) {
  2022. pitch_tiles = pitch / tile_height;
  2023. swap(tile_width, tile_height);
  2024. } else {
  2025. pitch_tiles = pitch / (tile_width * cpp);
  2026. }
  2027. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2028. tile_size, pitch_tiles,
  2029. old_offset, new_offset);
  2030. } else {
  2031. old_offset += *y * pitch + *x * cpp;
  2032. *y = (old_offset - new_offset) / pitch;
  2033. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2034. }
  2035. return new_offset;
  2036. }
  2037. /*
  2038. * Computes the linear offset to the base tile and adjusts
  2039. * x, y. bytes per pixel is assumed to be a power-of-two.
  2040. *
  2041. * In the 90/270 rotated case, x and y are assumed
  2042. * to be already rotated to match the rotated GTT view, and
  2043. * pitch is the tile_height aligned framebuffer height.
  2044. *
  2045. * This function is used when computing the derived information
  2046. * under intel_framebuffer, so using any of that information
  2047. * here is not allowed. Anything under drm_framebuffer can be
  2048. * used. This is why the user has to pass in the pitch since it
  2049. * is specified in the rotated orientation.
  2050. */
  2051. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2052. int *x, int *y,
  2053. const struct drm_framebuffer *fb, int plane,
  2054. unsigned int pitch,
  2055. unsigned int rotation,
  2056. u32 alignment)
  2057. {
  2058. uint64_t fb_modifier = fb->modifier;
  2059. unsigned int cpp = fb->format->cpp[plane];
  2060. u32 offset, offset_aligned;
  2061. if (alignment)
  2062. alignment--;
  2063. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2064. unsigned int tile_size, tile_width, tile_height;
  2065. unsigned int tile_rows, tiles, pitch_tiles;
  2066. tile_size = intel_tile_size(dev_priv);
  2067. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2068. if (drm_rotation_90_or_270(rotation)) {
  2069. pitch_tiles = pitch / tile_height;
  2070. swap(tile_width, tile_height);
  2071. } else {
  2072. pitch_tiles = pitch / (tile_width * cpp);
  2073. }
  2074. tile_rows = *y / tile_height;
  2075. *y %= tile_height;
  2076. tiles = *x / tile_width;
  2077. *x %= tile_width;
  2078. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2079. offset_aligned = offset & ~alignment;
  2080. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2081. tile_size, pitch_tiles,
  2082. offset, offset_aligned);
  2083. } else {
  2084. offset = *y * pitch + *x * cpp;
  2085. offset_aligned = offset & ~alignment;
  2086. *y = (offset & alignment) / pitch;
  2087. *x = ((offset & alignment) - *y * pitch) / cpp;
  2088. }
  2089. return offset_aligned;
  2090. }
  2091. u32 intel_compute_tile_offset(int *x, int *y,
  2092. const struct intel_plane_state *state,
  2093. int plane)
  2094. {
  2095. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2096. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2097. const struct drm_framebuffer *fb = state->base.fb;
  2098. unsigned int rotation = state->base.rotation;
  2099. int pitch = intel_fb_pitch(fb, plane, rotation);
  2100. u32 alignment;
  2101. if (intel_plane->id == PLANE_CURSOR)
  2102. alignment = intel_cursor_alignment(dev_priv);
  2103. else
  2104. alignment = intel_surf_alignment(fb, plane);
  2105. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2106. rotation, alignment);
  2107. }
  2108. /* Convert the fb->offset[] linear offset into x/y offsets */
  2109. static void intel_fb_offset_to_xy(int *x, int *y,
  2110. const struct drm_framebuffer *fb, int plane)
  2111. {
  2112. unsigned int cpp = fb->format->cpp[plane];
  2113. unsigned int pitch = fb->pitches[plane];
  2114. u32 linear_offset = fb->offsets[plane];
  2115. *y = linear_offset / pitch;
  2116. *x = linear_offset % pitch / cpp;
  2117. }
  2118. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2119. {
  2120. switch (fb_modifier) {
  2121. case I915_FORMAT_MOD_X_TILED:
  2122. return I915_TILING_X;
  2123. case I915_FORMAT_MOD_Y_TILED:
  2124. case I915_FORMAT_MOD_Y_TILED_CCS:
  2125. return I915_TILING_Y;
  2126. default:
  2127. return I915_TILING_NONE;
  2128. }
  2129. }
  2130. static const struct drm_format_info ccs_formats[] = {
  2131. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2132. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2133. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2134. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2135. };
  2136. static const struct drm_format_info *
  2137. lookup_format_info(const struct drm_format_info formats[],
  2138. int num_formats, u32 format)
  2139. {
  2140. int i;
  2141. for (i = 0; i < num_formats; i++) {
  2142. if (formats[i].format == format)
  2143. return &formats[i];
  2144. }
  2145. return NULL;
  2146. }
  2147. static const struct drm_format_info *
  2148. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2149. {
  2150. switch (cmd->modifier[0]) {
  2151. case I915_FORMAT_MOD_Y_TILED_CCS:
  2152. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2153. return lookup_format_info(ccs_formats,
  2154. ARRAY_SIZE(ccs_formats),
  2155. cmd->pixel_format);
  2156. default:
  2157. return NULL;
  2158. }
  2159. }
  2160. static int
  2161. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2162. struct drm_framebuffer *fb)
  2163. {
  2164. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2165. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2166. u32 gtt_offset_rotated = 0;
  2167. unsigned int max_size = 0;
  2168. int i, num_planes = fb->format->num_planes;
  2169. unsigned int tile_size = intel_tile_size(dev_priv);
  2170. for (i = 0; i < num_planes; i++) {
  2171. unsigned int width, height;
  2172. unsigned int cpp, size;
  2173. u32 offset;
  2174. int x, y;
  2175. cpp = fb->format->cpp[i];
  2176. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2177. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2178. intel_fb_offset_to_xy(&x, &y, fb, i);
  2179. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2180. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2181. int hsub = fb->format->hsub;
  2182. int vsub = fb->format->vsub;
  2183. int tile_width, tile_height;
  2184. int main_x, main_y;
  2185. int ccs_x, ccs_y;
  2186. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2187. ccs_x = (x * hsub) % (tile_width * hsub);
  2188. ccs_y = (y * vsub) % (tile_height * vsub);
  2189. main_x = intel_fb->normal[0].x % (tile_width * hsub);
  2190. main_y = intel_fb->normal[0].y % (tile_height * vsub);
  2191. /*
  2192. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2193. * x/y offsets must match between CCS and the main surface.
  2194. */
  2195. if (main_x != ccs_x || main_y != ccs_y) {
  2196. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2197. main_x, main_y,
  2198. ccs_x, ccs_y,
  2199. intel_fb->normal[0].x,
  2200. intel_fb->normal[0].y,
  2201. x, y);
  2202. return -EINVAL;
  2203. }
  2204. }
  2205. /*
  2206. * The fence (if used) is aligned to the start of the object
  2207. * so having the framebuffer wrap around across the edge of the
  2208. * fenced region doesn't really work. We have no API to configure
  2209. * the fence start offset within the object (nor could we probably
  2210. * on gen2/3). So it's just easier if we just require that the
  2211. * fb layout agrees with the fence layout. We already check that the
  2212. * fb stride matches the fence stride elsewhere.
  2213. */
  2214. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2215. (x + width) * cpp > fb->pitches[i]) {
  2216. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2217. i, fb->offsets[i]);
  2218. return -EINVAL;
  2219. }
  2220. /*
  2221. * First pixel of the framebuffer from
  2222. * the start of the normal gtt mapping.
  2223. */
  2224. intel_fb->normal[i].x = x;
  2225. intel_fb->normal[i].y = y;
  2226. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2227. fb, i, fb->pitches[i],
  2228. DRM_MODE_ROTATE_0, tile_size);
  2229. offset /= tile_size;
  2230. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2231. unsigned int tile_width, tile_height;
  2232. unsigned int pitch_tiles;
  2233. struct drm_rect r;
  2234. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2235. rot_info->plane[i].offset = offset;
  2236. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2237. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2238. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2239. intel_fb->rotated[i].pitch =
  2240. rot_info->plane[i].height * tile_height;
  2241. /* how many tiles does this plane need */
  2242. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2243. /*
  2244. * If the plane isn't horizontally tile aligned,
  2245. * we need one more tile.
  2246. */
  2247. if (x != 0)
  2248. size++;
  2249. /* rotate the x/y offsets to match the GTT view */
  2250. r.x1 = x;
  2251. r.y1 = y;
  2252. r.x2 = x + width;
  2253. r.y2 = y + height;
  2254. drm_rect_rotate(&r,
  2255. rot_info->plane[i].width * tile_width,
  2256. rot_info->plane[i].height * tile_height,
  2257. DRM_MODE_ROTATE_270);
  2258. x = r.x1;
  2259. y = r.y1;
  2260. /* rotate the tile dimensions to match the GTT view */
  2261. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2262. swap(tile_width, tile_height);
  2263. /*
  2264. * We only keep the x/y offsets, so push all of the
  2265. * gtt offset into the x/y offsets.
  2266. */
  2267. _intel_adjust_tile_offset(&x, &y,
  2268. tile_width, tile_height,
  2269. tile_size, pitch_tiles,
  2270. gtt_offset_rotated * tile_size, 0);
  2271. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2272. /*
  2273. * First pixel of the framebuffer from
  2274. * the start of the rotated gtt mapping.
  2275. */
  2276. intel_fb->rotated[i].x = x;
  2277. intel_fb->rotated[i].y = y;
  2278. } else {
  2279. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2280. x * cpp, tile_size);
  2281. }
  2282. /* how many tiles in total needed in the bo */
  2283. max_size = max(max_size, offset + size);
  2284. }
  2285. if (max_size * tile_size > intel_fb->obj->base.size) {
  2286. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2287. max_size * tile_size, intel_fb->obj->base.size);
  2288. return -EINVAL;
  2289. }
  2290. return 0;
  2291. }
  2292. static int i9xx_format_to_fourcc(int format)
  2293. {
  2294. switch (format) {
  2295. case DISPPLANE_8BPP:
  2296. return DRM_FORMAT_C8;
  2297. case DISPPLANE_BGRX555:
  2298. return DRM_FORMAT_XRGB1555;
  2299. case DISPPLANE_BGRX565:
  2300. return DRM_FORMAT_RGB565;
  2301. default:
  2302. case DISPPLANE_BGRX888:
  2303. return DRM_FORMAT_XRGB8888;
  2304. case DISPPLANE_RGBX888:
  2305. return DRM_FORMAT_XBGR8888;
  2306. case DISPPLANE_BGRX101010:
  2307. return DRM_FORMAT_XRGB2101010;
  2308. case DISPPLANE_RGBX101010:
  2309. return DRM_FORMAT_XBGR2101010;
  2310. }
  2311. }
  2312. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2313. {
  2314. switch (format) {
  2315. case PLANE_CTL_FORMAT_RGB_565:
  2316. return DRM_FORMAT_RGB565;
  2317. default:
  2318. case PLANE_CTL_FORMAT_XRGB_8888:
  2319. if (rgb_order) {
  2320. if (alpha)
  2321. return DRM_FORMAT_ABGR8888;
  2322. else
  2323. return DRM_FORMAT_XBGR8888;
  2324. } else {
  2325. if (alpha)
  2326. return DRM_FORMAT_ARGB8888;
  2327. else
  2328. return DRM_FORMAT_XRGB8888;
  2329. }
  2330. case PLANE_CTL_FORMAT_XRGB_2101010:
  2331. if (rgb_order)
  2332. return DRM_FORMAT_XBGR2101010;
  2333. else
  2334. return DRM_FORMAT_XRGB2101010;
  2335. }
  2336. }
  2337. static bool
  2338. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2339. struct intel_initial_plane_config *plane_config)
  2340. {
  2341. struct drm_device *dev = crtc->base.dev;
  2342. struct drm_i915_private *dev_priv = to_i915(dev);
  2343. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2344. struct drm_i915_gem_object *obj = NULL;
  2345. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2346. struct drm_framebuffer *fb = &plane_config->fb->base;
  2347. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2348. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2349. PAGE_SIZE);
  2350. size_aligned -= base_aligned;
  2351. if (plane_config->size == 0)
  2352. return false;
  2353. /* If the FB is too big, just don't use it since fbdev is not very
  2354. * important and we should probably use that space with FBC or other
  2355. * features. */
  2356. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2357. return false;
  2358. mutex_lock(&dev->struct_mutex);
  2359. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2360. base_aligned,
  2361. base_aligned,
  2362. size_aligned);
  2363. mutex_unlock(&dev->struct_mutex);
  2364. if (!obj)
  2365. return false;
  2366. if (plane_config->tiling == I915_TILING_X)
  2367. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2368. mode_cmd.pixel_format = fb->format->format;
  2369. mode_cmd.width = fb->width;
  2370. mode_cmd.height = fb->height;
  2371. mode_cmd.pitches[0] = fb->pitches[0];
  2372. mode_cmd.modifier[0] = fb->modifier;
  2373. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2374. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2375. DRM_DEBUG_KMS("intel fb init failed\n");
  2376. goto out_unref_obj;
  2377. }
  2378. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2379. return true;
  2380. out_unref_obj:
  2381. i915_gem_object_put(obj);
  2382. return false;
  2383. }
  2384. static void
  2385. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2386. struct intel_plane_state *plane_state,
  2387. bool visible)
  2388. {
  2389. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2390. plane_state->base.visible = visible;
  2391. /* FIXME pre-g4x don't work like this */
  2392. if (visible) {
  2393. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2394. crtc_state->active_planes |= BIT(plane->id);
  2395. } else {
  2396. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2397. crtc_state->active_planes &= ~BIT(plane->id);
  2398. }
  2399. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2400. crtc_state->base.crtc->name,
  2401. crtc_state->active_planes);
  2402. }
  2403. static void
  2404. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2405. struct intel_initial_plane_config *plane_config)
  2406. {
  2407. struct drm_device *dev = intel_crtc->base.dev;
  2408. struct drm_i915_private *dev_priv = to_i915(dev);
  2409. struct drm_crtc *c;
  2410. struct drm_i915_gem_object *obj;
  2411. struct drm_plane *primary = intel_crtc->base.primary;
  2412. struct drm_plane_state *plane_state = primary->state;
  2413. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2414. struct intel_plane *intel_plane = to_intel_plane(primary);
  2415. struct intel_plane_state *intel_state =
  2416. to_intel_plane_state(plane_state);
  2417. struct drm_framebuffer *fb;
  2418. if (!plane_config->fb)
  2419. return;
  2420. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2421. fb = &plane_config->fb->base;
  2422. goto valid_fb;
  2423. }
  2424. kfree(plane_config->fb);
  2425. /*
  2426. * Failed to alloc the obj, check to see if we should share
  2427. * an fb with another CRTC instead
  2428. */
  2429. for_each_crtc(dev, c) {
  2430. struct intel_plane_state *state;
  2431. if (c == &intel_crtc->base)
  2432. continue;
  2433. if (!to_intel_crtc(c)->active)
  2434. continue;
  2435. state = to_intel_plane_state(c->primary->state);
  2436. if (!state->vma)
  2437. continue;
  2438. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2439. fb = c->primary->fb;
  2440. drm_framebuffer_reference(fb);
  2441. goto valid_fb;
  2442. }
  2443. }
  2444. /*
  2445. * We've failed to reconstruct the BIOS FB. Current display state
  2446. * indicates that the primary plane is visible, but has a NULL FB,
  2447. * which will lead to problems later if we don't fix it up. The
  2448. * simplest solution is to just disable the primary plane now and
  2449. * pretend the BIOS never had it enabled.
  2450. */
  2451. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2452. to_intel_plane_state(plane_state),
  2453. false);
  2454. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2455. trace_intel_disable_plane(primary, intel_crtc);
  2456. intel_plane->disable_plane(intel_plane, intel_crtc);
  2457. return;
  2458. valid_fb:
  2459. mutex_lock(&dev->struct_mutex);
  2460. intel_state->vma =
  2461. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2462. mutex_unlock(&dev->struct_mutex);
  2463. if (IS_ERR(intel_state->vma)) {
  2464. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2465. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2466. intel_state->vma = NULL;
  2467. drm_framebuffer_unreference(fb);
  2468. return;
  2469. }
  2470. plane_state->src_x = 0;
  2471. plane_state->src_y = 0;
  2472. plane_state->src_w = fb->width << 16;
  2473. plane_state->src_h = fb->height << 16;
  2474. plane_state->crtc_x = 0;
  2475. plane_state->crtc_y = 0;
  2476. plane_state->crtc_w = fb->width;
  2477. plane_state->crtc_h = fb->height;
  2478. intel_state->base.src = drm_plane_state_src(plane_state);
  2479. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2480. obj = intel_fb_obj(fb);
  2481. if (i915_gem_object_is_tiled(obj))
  2482. dev_priv->preserve_bios_swizzle = true;
  2483. drm_framebuffer_reference(fb);
  2484. primary->fb = primary->state->fb = fb;
  2485. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2486. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2487. to_intel_plane_state(plane_state),
  2488. true);
  2489. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2490. &obj->frontbuffer_bits);
  2491. }
  2492. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2493. unsigned int rotation)
  2494. {
  2495. int cpp = fb->format->cpp[plane];
  2496. switch (fb->modifier) {
  2497. case DRM_FORMAT_MOD_LINEAR:
  2498. case I915_FORMAT_MOD_X_TILED:
  2499. switch (cpp) {
  2500. case 8:
  2501. return 4096;
  2502. case 4:
  2503. case 2:
  2504. case 1:
  2505. return 8192;
  2506. default:
  2507. MISSING_CASE(cpp);
  2508. break;
  2509. }
  2510. break;
  2511. case I915_FORMAT_MOD_Y_TILED_CCS:
  2512. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2513. /* FIXME AUX plane? */
  2514. case I915_FORMAT_MOD_Y_TILED:
  2515. case I915_FORMAT_MOD_Yf_TILED:
  2516. switch (cpp) {
  2517. case 8:
  2518. return 2048;
  2519. case 4:
  2520. return 4096;
  2521. case 2:
  2522. case 1:
  2523. return 8192;
  2524. default:
  2525. MISSING_CASE(cpp);
  2526. break;
  2527. }
  2528. break;
  2529. default:
  2530. MISSING_CASE(fb->modifier);
  2531. }
  2532. return 2048;
  2533. }
  2534. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2535. int main_x, int main_y, u32 main_offset)
  2536. {
  2537. const struct drm_framebuffer *fb = plane_state->base.fb;
  2538. int hsub = fb->format->hsub;
  2539. int vsub = fb->format->vsub;
  2540. int aux_x = plane_state->aux.x;
  2541. int aux_y = plane_state->aux.y;
  2542. u32 aux_offset = plane_state->aux.offset;
  2543. u32 alignment = intel_surf_alignment(fb, 1);
  2544. while (aux_offset >= main_offset && aux_y <= main_y) {
  2545. int x, y;
  2546. if (aux_x == main_x && aux_y == main_y)
  2547. break;
  2548. if (aux_offset == 0)
  2549. break;
  2550. x = aux_x / hsub;
  2551. y = aux_y / vsub;
  2552. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2553. aux_offset, aux_offset - alignment);
  2554. aux_x = x * hsub + aux_x % hsub;
  2555. aux_y = y * vsub + aux_y % vsub;
  2556. }
  2557. if (aux_x != main_x || aux_y != main_y)
  2558. return false;
  2559. plane_state->aux.offset = aux_offset;
  2560. plane_state->aux.x = aux_x;
  2561. plane_state->aux.y = aux_y;
  2562. return true;
  2563. }
  2564. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2565. {
  2566. const struct drm_framebuffer *fb = plane_state->base.fb;
  2567. unsigned int rotation = plane_state->base.rotation;
  2568. int x = plane_state->base.src.x1 >> 16;
  2569. int y = plane_state->base.src.y1 >> 16;
  2570. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2571. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2572. int max_width = skl_max_plane_width(fb, 0, rotation);
  2573. int max_height = 4096;
  2574. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2575. if (w > max_width || h > max_height) {
  2576. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2577. w, h, max_width, max_height);
  2578. return -EINVAL;
  2579. }
  2580. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2581. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2582. alignment = intel_surf_alignment(fb, 0);
  2583. /*
  2584. * AUX surface offset is specified as the distance from the
  2585. * main surface offset, and it must be non-negative. Make
  2586. * sure that is what we will get.
  2587. */
  2588. if (offset > aux_offset)
  2589. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2590. offset, aux_offset & ~(alignment - 1));
  2591. /*
  2592. * When using an X-tiled surface, the plane blows up
  2593. * if the x offset + width exceed the stride.
  2594. *
  2595. * TODO: linear and Y-tiled seem fine, Yf untested,
  2596. */
  2597. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2598. int cpp = fb->format->cpp[0];
  2599. while ((x + w) * cpp > fb->pitches[0]) {
  2600. if (offset == 0) {
  2601. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2602. return -EINVAL;
  2603. }
  2604. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2605. offset, offset - alignment);
  2606. }
  2607. }
  2608. /*
  2609. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2610. * they match with the main surface x/y offsets.
  2611. */
  2612. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2613. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2614. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2615. if (offset == 0)
  2616. break;
  2617. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2618. offset, offset - alignment);
  2619. }
  2620. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2621. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2622. return -EINVAL;
  2623. }
  2624. }
  2625. plane_state->main.offset = offset;
  2626. plane_state->main.x = x;
  2627. plane_state->main.y = y;
  2628. return 0;
  2629. }
  2630. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2631. {
  2632. const struct drm_framebuffer *fb = plane_state->base.fb;
  2633. unsigned int rotation = plane_state->base.rotation;
  2634. int max_width = skl_max_plane_width(fb, 1, rotation);
  2635. int max_height = 4096;
  2636. int x = plane_state->base.src.x1 >> 17;
  2637. int y = plane_state->base.src.y1 >> 17;
  2638. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2639. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2640. u32 offset;
  2641. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2642. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2643. /* FIXME not quite sure how/if these apply to the chroma plane */
  2644. if (w > max_width || h > max_height) {
  2645. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2646. w, h, max_width, max_height);
  2647. return -EINVAL;
  2648. }
  2649. plane_state->aux.offset = offset;
  2650. plane_state->aux.x = x;
  2651. plane_state->aux.y = y;
  2652. return 0;
  2653. }
  2654. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2655. {
  2656. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2657. struct intel_crtc *crtc = to_intel_crtc(plane_state->base.crtc);
  2658. const struct drm_framebuffer *fb = plane_state->base.fb;
  2659. int src_x = plane_state->base.src.x1 >> 16;
  2660. int src_y = plane_state->base.src.y1 >> 16;
  2661. int hsub = fb->format->hsub;
  2662. int vsub = fb->format->vsub;
  2663. int x = src_x / hsub;
  2664. int y = src_y / vsub;
  2665. u32 offset;
  2666. switch (plane->id) {
  2667. case PLANE_PRIMARY:
  2668. case PLANE_SPRITE0:
  2669. break;
  2670. default:
  2671. DRM_DEBUG_KMS("RC support only on plane 1 and 2\n");
  2672. return -EINVAL;
  2673. }
  2674. if (crtc->pipe == PIPE_C) {
  2675. DRM_DEBUG_KMS("No RC support on pipe C\n");
  2676. return -EINVAL;
  2677. }
  2678. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2679. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2680. plane_state->base.rotation);
  2681. return -EINVAL;
  2682. }
  2683. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2684. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2685. plane_state->aux.offset = offset;
  2686. plane_state->aux.x = x * hsub + src_x % hsub;
  2687. plane_state->aux.y = y * vsub + src_y % vsub;
  2688. return 0;
  2689. }
  2690. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2691. {
  2692. const struct drm_framebuffer *fb = plane_state->base.fb;
  2693. unsigned int rotation = plane_state->base.rotation;
  2694. int ret;
  2695. if (!plane_state->base.visible)
  2696. return 0;
  2697. /* Rotate src coordinates to match rotated GTT view */
  2698. if (drm_rotation_90_or_270(rotation))
  2699. drm_rect_rotate(&plane_state->base.src,
  2700. fb->width << 16, fb->height << 16,
  2701. DRM_MODE_ROTATE_270);
  2702. /*
  2703. * Handle the AUX surface first since
  2704. * the main surface setup depends on it.
  2705. */
  2706. if (fb->format->format == DRM_FORMAT_NV12) {
  2707. ret = skl_check_nv12_aux_surface(plane_state);
  2708. if (ret)
  2709. return ret;
  2710. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2711. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2712. ret = skl_check_ccs_aux_surface(plane_state);
  2713. if (ret)
  2714. return ret;
  2715. } else {
  2716. plane_state->aux.offset = ~0xfff;
  2717. plane_state->aux.x = 0;
  2718. plane_state->aux.y = 0;
  2719. }
  2720. ret = skl_check_main_surface(plane_state);
  2721. if (ret)
  2722. return ret;
  2723. return 0;
  2724. }
  2725. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2726. const struct intel_plane_state *plane_state)
  2727. {
  2728. struct drm_i915_private *dev_priv =
  2729. to_i915(plane_state->base.plane->dev);
  2730. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2731. const struct drm_framebuffer *fb = plane_state->base.fb;
  2732. unsigned int rotation = plane_state->base.rotation;
  2733. u32 dspcntr;
  2734. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2735. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2736. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2737. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2738. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2739. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2740. if (INTEL_GEN(dev_priv) < 4)
  2741. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2742. switch (fb->format->format) {
  2743. case DRM_FORMAT_C8:
  2744. dspcntr |= DISPPLANE_8BPP;
  2745. break;
  2746. case DRM_FORMAT_XRGB1555:
  2747. dspcntr |= DISPPLANE_BGRX555;
  2748. break;
  2749. case DRM_FORMAT_RGB565:
  2750. dspcntr |= DISPPLANE_BGRX565;
  2751. break;
  2752. case DRM_FORMAT_XRGB8888:
  2753. dspcntr |= DISPPLANE_BGRX888;
  2754. break;
  2755. case DRM_FORMAT_XBGR8888:
  2756. dspcntr |= DISPPLANE_RGBX888;
  2757. break;
  2758. case DRM_FORMAT_XRGB2101010:
  2759. dspcntr |= DISPPLANE_BGRX101010;
  2760. break;
  2761. case DRM_FORMAT_XBGR2101010:
  2762. dspcntr |= DISPPLANE_RGBX101010;
  2763. break;
  2764. default:
  2765. MISSING_CASE(fb->format->format);
  2766. return 0;
  2767. }
  2768. if (INTEL_GEN(dev_priv) >= 4 &&
  2769. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2770. dspcntr |= DISPPLANE_TILED;
  2771. if (rotation & DRM_MODE_ROTATE_180)
  2772. dspcntr |= DISPPLANE_ROTATE_180;
  2773. if (rotation & DRM_MODE_REFLECT_X)
  2774. dspcntr |= DISPPLANE_MIRROR;
  2775. return dspcntr;
  2776. }
  2777. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2778. {
  2779. struct drm_i915_private *dev_priv =
  2780. to_i915(plane_state->base.plane->dev);
  2781. int src_x = plane_state->base.src.x1 >> 16;
  2782. int src_y = plane_state->base.src.y1 >> 16;
  2783. u32 offset;
  2784. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2785. if (INTEL_GEN(dev_priv) >= 4)
  2786. offset = intel_compute_tile_offset(&src_x, &src_y,
  2787. plane_state, 0);
  2788. else
  2789. offset = 0;
  2790. /* HSW/BDW do this automagically in hardware */
  2791. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2792. unsigned int rotation = plane_state->base.rotation;
  2793. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2794. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2795. if (rotation & DRM_MODE_ROTATE_180) {
  2796. src_x += src_w - 1;
  2797. src_y += src_h - 1;
  2798. } else if (rotation & DRM_MODE_REFLECT_X) {
  2799. src_x += src_w - 1;
  2800. }
  2801. }
  2802. plane_state->main.offset = offset;
  2803. plane_state->main.x = src_x;
  2804. plane_state->main.y = src_y;
  2805. return 0;
  2806. }
  2807. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2808. const struct intel_crtc_state *crtc_state,
  2809. const struct intel_plane_state *plane_state)
  2810. {
  2811. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2812. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2813. const struct drm_framebuffer *fb = plane_state->base.fb;
  2814. enum plane plane = primary->plane;
  2815. u32 linear_offset;
  2816. u32 dspcntr = plane_state->ctl;
  2817. i915_reg_t reg = DSPCNTR(plane);
  2818. int x = plane_state->main.x;
  2819. int y = plane_state->main.y;
  2820. unsigned long irqflags;
  2821. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2822. if (INTEL_GEN(dev_priv) >= 4)
  2823. crtc->dspaddr_offset = plane_state->main.offset;
  2824. else
  2825. crtc->dspaddr_offset = linear_offset;
  2826. crtc->adjusted_x = x;
  2827. crtc->adjusted_y = y;
  2828. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2829. if (INTEL_GEN(dev_priv) < 4) {
  2830. /* pipesrc and dspsize control the size that is scaled from,
  2831. * which should always be the user's requested size.
  2832. */
  2833. I915_WRITE_FW(DSPSIZE(plane),
  2834. ((crtc_state->pipe_src_h - 1) << 16) |
  2835. (crtc_state->pipe_src_w - 1));
  2836. I915_WRITE_FW(DSPPOS(plane), 0);
  2837. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2838. I915_WRITE_FW(PRIMSIZE(plane),
  2839. ((crtc_state->pipe_src_h - 1) << 16) |
  2840. (crtc_state->pipe_src_w - 1));
  2841. I915_WRITE_FW(PRIMPOS(plane), 0);
  2842. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2843. }
  2844. I915_WRITE_FW(reg, dspcntr);
  2845. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2846. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2847. I915_WRITE_FW(DSPSURF(plane),
  2848. intel_plane_ggtt_offset(plane_state) +
  2849. crtc->dspaddr_offset);
  2850. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2851. } else if (INTEL_GEN(dev_priv) >= 4) {
  2852. I915_WRITE_FW(DSPSURF(plane),
  2853. intel_plane_ggtt_offset(plane_state) +
  2854. crtc->dspaddr_offset);
  2855. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2856. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2857. } else {
  2858. I915_WRITE_FW(DSPADDR(plane),
  2859. intel_plane_ggtt_offset(plane_state) +
  2860. crtc->dspaddr_offset);
  2861. }
  2862. POSTING_READ_FW(reg);
  2863. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2864. }
  2865. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2866. struct intel_crtc *crtc)
  2867. {
  2868. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2869. enum plane plane = primary->plane;
  2870. unsigned long irqflags;
  2871. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2872. I915_WRITE_FW(DSPCNTR(plane), 0);
  2873. if (INTEL_INFO(dev_priv)->gen >= 4)
  2874. I915_WRITE_FW(DSPSURF(plane), 0);
  2875. else
  2876. I915_WRITE_FW(DSPADDR(plane), 0);
  2877. POSTING_READ_FW(DSPCNTR(plane));
  2878. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2879. }
  2880. static u32
  2881. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2882. {
  2883. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2884. return 64;
  2885. else
  2886. return intel_tile_width_bytes(fb, plane);
  2887. }
  2888. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2889. {
  2890. struct drm_device *dev = intel_crtc->base.dev;
  2891. struct drm_i915_private *dev_priv = to_i915(dev);
  2892. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2893. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2894. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2895. }
  2896. /*
  2897. * This function detaches (aka. unbinds) unused scalers in hardware
  2898. */
  2899. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2900. {
  2901. struct intel_crtc_scaler_state *scaler_state;
  2902. int i;
  2903. scaler_state = &intel_crtc->config->scaler_state;
  2904. /* loop through and disable scalers that aren't in use */
  2905. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2906. if (!scaler_state->scalers[i].in_use)
  2907. skl_detach_scaler(intel_crtc, i);
  2908. }
  2909. }
  2910. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2911. unsigned int rotation)
  2912. {
  2913. u32 stride;
  2914. if (plane >= fb->format->num_planes)
  2915. return 0;
  2916. stride = intel_fb_pitch(fb, plane, rotation);
  2917. /*
  2918. * The stride is either expressed as a multiple of 64 bytes chunks for
  2919. * linear buffers or in number of tiles for tiled buffers.
  2920. */
  2921. if (drm_rotation_90_or_270(rotation))
  2922. stride /= intel_tile_height(fb, plane);
  2923. else
  2924. stride /= intel_fb_stride_alignment(fb, plane);
  2925. return stride;
  2926. }
  2927. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2928. {
  2929. switch (pixel_format) {
  2930. case DRM_FORMAT_C8:
  2931. return PLANE_CTL_FORMAT_INDEXED;
  2932. case DRM_FORMAT_RGB565:
  2933. return PLANE_CTL_FORMAT_RGB_565;
  2934. case DRM_FORMAT_XBGR8888:
  2935. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2936. case DRM_FORMAT_XRGB8888:
  2937. return PLANE_CTL_FORMAT_XRGB_8888;
  2938. /*
  2939. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2940. * to be already pre-multiplied. We need to add a knob (or a different
  2941. * DRM_FORMAT) for user-space to configure that.
  2942. */
  2943. case DRM_FORMAT_ABGR8888:
  2944. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2945. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2946. case DRM_FORMAT_ARGB8888:
  2947. return PLANE_CTL_FORMAT_XRGB_8888 |
  2948. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2949. case DRM_FORMAT_XRGB2101010:
  2950. return PLANE_CTL_FORMAT_XRGB_2101010;
  2951. case DRM_FORMAT_XBGR2101010:
  2952. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2953. case DRM_FORMAT_YUYV:
  2954. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2955. case DRM_FORMAT_YVYU:
  2956. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2957. case DRM_FORMAT_UYVY:
  2958. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2959. case DRM_FORMAT_VYUY:
  2960. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2961. default:
  2962. MISSING_CASE(pixel_format);
  2963. }
  2964. return 0;
  2965. }
  2966. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2967. {
  2968. switch (fb_modifier) {
  2969. case DRM_FORMAT_MOD_LINEAR:
  2970. break;
  2971. case I915_FORMAT_MOD_X_TILED:
  2972. return PLANE_CTL_TILED_X;
  2973. case I915_FORMAT_MOD_Y_TILED:
  2974. return PLANE_CTL_TILED_Y;
  2975. case I915_FORMAT_MOD_Y_TILED_CCS:
  2976. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  2977. case I915_FORMAT_MOD_Yf_TILED:
  2978. return PLANE_CTL_TILED_YF;
  2979. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2980. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  2981. default:
  2982. MISSING_CASE(fb_modifier);
  2983. }
  2984. return 0;
  2985. }
  2986. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  2987. {
  2988. switch (rotation) {
  2989. case DRM_MODE_ROTATE_0:
  2990. break;
  2991. /*
  2992. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2993. * while i915 HW rotation is clockwise, thats why this swapping.
  2994. */
  2995. case DRM_MODE_ROTATE_90:
  2996. return PLANE_CTL_ROTATE_270;
  2997. case DRM_MODE_ROTATE_180:
  2998. return PLANE_CTL_ROTATE_180;
  2999. case DRM_MODE_ROTATE_270:
  3000. return PLANE_CTL_ROTATE_90;
  3001. default:
  3002. MISSING_CASE(rotation);
  3003. }
  3004. return 0;
  3005. }
  3006. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3007. const struct intel_plane_state *plane_state)
  3008. {
  3009. struct drm_i915_private *dev_priv =
  3010. to_i915(plane_state->base.plane->dev);
  3011. const struct drm_framebuffer *fb = plane_state->base.fb;
  3012. unsigned int rotation = plane_state->base.rotation;
  3013. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3014. u32 plane_ctl;
  3015. plane_ctl = PLANE_CTL_ENABLE;
  3016. if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
  3017. plane_ctl |=
  3018. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3019. PLANE_CTL_PIPE_CSC_ENABLE |
  3020. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3021. }
  3022. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3023. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3024. plane_ctl |= skl_plane_ctl_rotation(rotation);
  3025. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3026. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3027. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3028. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3029. return plane_ctl;
  3030. }
  3031. static void skylake_update_primary_plane(struct intel_plane *plane,
  3032. const struct intel_crtc_state *crtc_state,
  3033. const struct intel_plane_state *plane_state)
  3034. {
  3035. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  3036. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3037. const struct drm_framebuffer *fb = plane_state->base.fb;
  3038. enum plane_id plane_id = plane->id;
  3039. enum pipe pipe = plane->pipe;
  3040. u32 plane_ctl = plane_state->ctl;
  3041. unsigned int rotation = plane_state->base.rotation;
  3042. u32 stride = skl_plane_stride(fb, 0, rotation);
  3043. u32 aux_stride = skl_plane_stride(fb, 1, rotation);
  3044. u32 surf_addr = plane_state->main.offset;
  3045. int scaler_id = plane_state->scaler_id;
  3046. int src_x = plane_state->main.x;
  3047. int src_y = plane_state->main.y;
  3048. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  3049. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  3050. int dst_x = plane_state->base.dst.x1;
  3051. int dst_y = plane_state->base.dst.y1;
  3052. int dst_w = drm_rect_width(&plane_state->base.dst);
  3053. int dst_h = drm_rect_height(&plane_state->base.dst);
  3054. unsigned long irqflags;
  3055. /* Sizes are 0 based */
  3056. src_w--;
  3057. src_h--;
  3058. dst_w--;
  3059. dst_h--;
  3060. crtc->dspaddr_offset = surf_addr;
  3061. crtc->adjusted_x = src_x;
  3062. crtc->adjusted_y = src_y;
  3063. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3064. if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
  3065. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  3066. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  3067. PLANE_COLOR_PIPE_CSC_ENABLE |
  3068. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  3069. }
  3070. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  3071. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  3072. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  3073. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  3074. I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
  3075. (plane_state->aux.offset - surf_addr) | aux_stride);
  3076. I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
  3077. (plane_state->aux.y << 16) | plane_state->aux.x);
  3078. if (scaler_id >= 0) {
  3079. uint32_t ps_ctrl = 0;
  3080. WARN_ON(!dst_w || !dst_h);
  3081. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  3082. crtc_state->scaler_state.scalers[scaler_id].mode;
  3083. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  3084. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  3085. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  3086. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  3087. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  3088. } else {
  3089. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  3090. }
  3091. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  3092. intel_plane_ggtt_offset(plane_state) + surf_addr);
  3093. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  3094. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3095. }
  3096. static void skylake_disable_primary_plane(struct intel_plane *primary,
  3097. struct intel_crtc *crtc)
  3098. {
  3099. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  3100. enum plane_id plane_id = primary->id;
  3101. enum pipe pipe = primary->pipe;
  3102. unsigned long irqflags;
  3103. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3104. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  3105. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  3106. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  3107. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3108. }
  3109. static int
  3110. __intel_display_resume(struct drm_device *dev,
  3111. struct drm_atomic_state *state,
  3112. struct drm_modeset_acquire_ctx *ctx)
  3113. {
  3114. struct drm_crtc_state *crtc_state;
  3115. struct drm_crtc *crtc;
  3116. int i, ret;
  3117. intel_modeset_setup_hw_state(dev, ctx);
  3118. i915_redisable_vga(to_i915(dev));
  3119. if (!state)
  3120. return 0;
  3121. /*
  3122. * We've duplicated the state, pointers to the old state are invalid.
  3123. *
  3124. * Don't attempt to use the old state until we commit the duplicated state.
  3125. */
  3126. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3127. /*
  3128. * Force recalculation even if we restore
  3129. * current state. With fast modeset this may not result
  3130. * in a modeset when the state is compatible.
  3131. */
  3132. crtc_state->mode_changed = true;
  3133. }
  3134. /* ignore any reset values/BIOS leftovers in the WM registers */
  3135. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3136. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3137. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3138. WARN_ON(ret == -EDEADLK);
  3139. return ret;
  3140. }
  3141. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3142. {
  3143. return intel_has_gpu_reset(dev_priv) &&
  3144. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3145. }
  3146. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3147. {
  3148. struct drm_device *dev = &dev_priv->drm;
  3149. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3150. struct drm_atomic_state *state;
  3151. int ret;
  3152. /* reset doesn't touch the display */
  3153. if (!i915.force_reset_modeset_test &&
  3154. !gpu_reset_clobbers_display(dev_priv))
  3155. return;
  3156. /*
  3157. * Need mode_config.mutex so that we don't
  3158. * trample ongoing ->detect() and whatnot.
  3159. */
  3160. mutex_lock(&dev->mode_config.mutex);
  3161. drm_modeset_acquire_init(ctx, 0);
  3162. while (1) {
  3163. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3164. if (ret != -EDEADLK)
  3165. break;
  3166. drm_modeset_backoff(ctx);
  3167. }
  3168. /*
  3169. * Disabling the crtcs gracefully seems nicer. Also the
  3170. * g33 docs say we should at least disable all the planes.
  3171. */
  3172. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3173. if (IS_ERR(state)) {
  3174. ret = PTR_ERR(state);
  3175. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3176. return;
  3177. }
  3178. ret = drm_atomic_helper_disable_all(dev, ctx);
  3179. if (ret) {
  3180. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3181. drm_atomic_state_put(state);
  3182. return;
  3183. }
  3184. dev_priv->modeset_restore_state = state;
  3185. state->acquire_ctx = ctx;
  3186. }
  3187. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3188. {
  3189. struct drm_device *dev = &dev_priv->drm;
  3190. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3191. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3192. int ret;
  3193. /* reset doesn't touch the display */
  3194. if (!i915.force_reset_modeset_test &&
  3195. !gpu_reset_clobbers_display(dev_priv))
  3196. return;
  3197. if (!state)
  3198. goto unlock;
  3199. dev_priv->modeset_restore_state = NULL;
  3200. /* reset doesn't touch the display */
  3201. if (!gpu_reset_clobbers_display(dev_priv)) {
  3202. /* for testing only restore the display */
  3203. ret = __intel_display_resume(dev, state, ctx);
  3204. if (ret)
  3205. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3206. } else {
  3207. /*
  3208. * The display has been reset as well,
  3209. * so need a full re-initialization.
  3210. */
  3211. intel_runtime_pm_disable_interrupts(dev_priv);
  3212. intel_runtime_pm_enable_interrupts(dev_priv);
  3213. intel_pps_unlock_regs_wa(dev_priv);
  3214. intel_modeset_init_hw(dev);
  3215. spin_lock_irq(&dev_priv->irq_lock);
  3216. if (dev_priv->display.hpd_irq_setup)
  3217. dev_priv->display.hpd_irq_setup(dev_priv);
  3218. spin_unlock_irq(&dev_priv->irq_lock);
  3219. ret = __intel_display_resume(dev, state, ctx);
  3220. if (ret)
  3221. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3222. intel_hpd_init(dev_priv);
  3223. }
  3224. drm_atomic_state_put(state);
  3225. unlock:
  3226. drm_modeset_drop_locks(ctx);
  3227. drm_modeset_acquire_fini(ctx);
  3228. mutex_unlock(&dev->mode_config.mutex);
  3229. }
  3230. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3231. struct intel_crtc_state *old_crtc_state)
  3232. {
  3233. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3234. struct intel_crtc_state *pipe_config =
  3235. to_intel_crtc_state(crtc->base.state);
  3236. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3237. crtc->base.mode = crtc->base.state->mode;
  3238. /*
  3239. * Update pipe size and adjust fitter if needed: the reason for this is
  3240. * that in compute_mode_changes we check the native mode (not the pfit
  3241. * mode) to see if we can flip rather than do a full mode set. In the
  3242. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3243. * pfit state, we'll end up with a big fb scanned out into the wrong
  3244. * sized surface.
  3245. */
  3246. I915_WRITE(PIPESRC(crtc->pipe),
  3247. ((pipe_config->pipe_src_w - 1) << 16) |
  3248. (pipe_config->pipe_src_h - 1));
  3249. /* on skylake this is done by detaching scalers */
  3250. if (INTEL_GEN(dev_priv) >= 9) {
  3251. skl_detach_scalers(crtc);
  3252. if (pipe_config->pch_pfit.enabled)
  3253. skylake_pfit_enable(crtc);
  3254. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3255. if (pipe_config->pch_pfit.enabled)
  3256. ironlake_pfit_enable(crtc);
  3257. else if (old_crtc_state->pch_pfit.enabled)
  3258. ironlake_pfit_disable(crtc, true);
  3259. }
  3260. }
  3261. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3262. {
  3263. struct drm_device *dev = crtc->base.dev;
  3264. struct drm_i915_private *dev_priv = to_i915(dev);
  3265. int pipe = crtc->pipe;
  3266. i915_reg_t reg;
  3267. u32 temp;
  3268. /* enable normal train */
  3269. reg = FDI_TX_CTL(pipe);
  3270. temp = I915_READ(reg);
  3271. if (IS_IVYBRIDGE(dev_priv)) {
  3272. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3273. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3274. } else {
  3275. temp &= ~FDI_LINK_TRAIN_NONE;
  3276. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3277. }
  3278. I915_WRITE(reg, temp);
  3279. reg = FDI_RX_CTL(pipe);
  3280. temp = I915_READ(reg);
  3281. if (HAS_PCH_CPT(dev_priv)) {
  3282. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3283. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3284. } else {
  3285. temp &= ~FDI_LINK_TRAIN_NONE;
  3286. temp |= FDI_LINK_TRAIN_NONE;
  3287. }
  3288. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3289. /* wait one idle pattern time */
  3290. POSTING_READ(reg);
  3291. udelay(1000);
  3292. /* IVB wants error correction enabled */
  3293. if (IS_IVYBRIDGE(dev_priv))
  3294. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3295. FDI_FE_ERRC_ENABLE);
  3296. }
  3297. /* The FDI link training functions for ILK/Ibexpeak. */
  3298. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3299. const struct intel_crtc_state *crtc_state)
  3300. {
  3301. struct drm_device *dev = crtc->base.dev;
  3302. struct drm_i915_private *dev_priv = to_i915(dev);
  3303. int pipe = crtc->pipe;
  3304. i915_reg_t reg;
  3305. u32 temp, tries;
  3306. /* FDI needs bits from pipe first */
  3307. assert_pipe_enabled(dev_priv, pipe);
  3308. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3309. for train result */
  3310. reg = FDI_RX_IMR(pipe);
  3311. temp = I915_READ(reg);
  3312. temp &= ~FDI_RX_SYMBOL_LOCK;
  3313. temp &= ~FDI_RX_BIT_LOCK;
  3314. I915_WRITE(reg, temp);
  3315. I915_READ(reg);
  3316. udelay(150);
  3317. /* enable CPU FDI TX and PCH FDI RX */
  3318. reg = FDI_TX_CTL(pipe);
  3319. temp = I915_READ(reg);
  3320. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3321. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3322. temp &= ~FDI_LINK_TRAIN_NONE;
  3323. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3324. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3325. reg = FDI_RX_CTL(pipe);
  3326. temp = I915_READ(reg);
  3327. temp &= ~FDI_LINK_TRAIN_NONE;
  3328. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3329. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3330. POSTING_READ(reg);
  3331. udelay(150);
  3332. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3333. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3334. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3335. FDI_RX_PHASE_SYNC_POINTER_EN);
  3336. reg = FDI_RX_IIR(pipe);
  3337. for (tries = 0; tries < 5; tries++) {
  3338. temp = I915_READ(reg);
  3339. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3340. if ((temp & FDI_RX_BIT_LOCK)) {
  3341. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3342. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3343. break;
  3344. }
  3345. }
  3346. if (tries == 5)
  3347. DRM_ERROR("FDI train 1 fail!\n");
  3348. /* Train 2 */
  3349. reg = FDI_TX_CTL(pipe);
  3350. temp = I915_READ(reg);
  3351. temp &= ~FDI_LINK_TRAIN_NONE;
  3352. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3353. I915_WRITE(reg, temp);
  3354. reg = FDI_RX_CTL(pipe);
  3355. temp = I915_READ(reg);
  3356. temp &= ~FDI_LINK_TRAIN_NONE;
  3357. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3358. I915_WRITE(reg, temp);
  3359. POSTING_READ(reg);
  3360. udelay(150);
  3361. reg = FDI_RX_IIR(pipe);
  3362. for (tries = 0; tries < 5; tries++) {
  3363. temp = I915_READ(reg);
  3364. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3365. if (temp & FDI_RX_SYMBOL_LOCK) {
  3366. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3367. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3368. break;
  3369. }
  3370. }
  3371. if (tries == 5)
  3372. DRM_ERROR("FDI train 2 fail!\n");
  3373. DRM_DEBUG_KMS("FDI train done\n");
  3374. }
  3375. static const int snb_b_fdi_train_param[] = {
  3376. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3377. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3378. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3379. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3380. };
  3381. /* The FDI link training functions for SNB/Cougarpoint. */
  3382. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3383. const struct intel_crtc_state *crtc_state)
  3384. {
  3385. struct drm_device *dev = crtc->base.dev;
  3386. struct drm_i915_private *dev_priv = to_i915(dev);
  3387. int pipe = crtc->pipe;
  3388. i915_reg_t reg;
  3389. u32 temp, i, retry;
  3390. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3391. for train result */
  3392. reg = FDI_RX_IMR(pipe);
  3393. temp = I915_READ(reg);
  3394. temp &= ~FDI_RX_SYMBOL_LOCK;
  3395. temp &= ~FDI_RX_BIT_LOCK;
  3396. I915_WRITE(reg, temp);
  3397. POSTING_READ(reg);
  3398. udelay(150);
  3399. /* enable CPU FDI TX and PCH FDI RX */
  3400. reg = FDI_TX_CTL(pipe);
  3401. temp = I915_READ(reg);
  3402. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3403. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3404. temp &= ~FDI_LINK_TRAIN_NONE;
  3405. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3406. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3407. /* SNB-B */
  3408. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3409. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3410. I915_WRITE(FDI_RX_MISC(pipe),
  3411. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3412. reg = FDI_RX_CTL(pipe);
  3413. temp = I915_READ(reg);
  3414. if (HAS_PCH_CPT(dev_priv)) {
  3415. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3416. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3417. } else {
  3418. temp &= ~FDI_LINK_TRAIN_NONE;
  3419. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3420. }
  3421. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3422. POSTING_READ(reg);
  3423. udelay(150);
  3424. for (i = 0; i < 4; i++) {
  3425. reg = FDI_TX_CTL(pipe);
  3426. temp = I915_READ(reg);
  3427. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3428. temp |= snb_b_fdi_train_param[i];
  3429. I915_WRITE(reg, temp);
  3430. POSTING_READ(reg);
  3431. udelay(500);
  3432. for (retry = 0; retry < 5; retry++) {
  3433. reg = FDI_RX_IIR(pipe);
  3434. temp = I915_READ(reg);
  3435. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3436. if (temp & FDI_RX_BIT_LOCK) {
  3437. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3438. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3439. break;
  3440. }
  3441. udelay(50);
  3442. }
  3443. if (retry < 5)
  3444. break;
  3445. }
  3446. if (i == 4)
  3447. DRM_ERROR("FDI train 1 fail!\n");
  3448. /* Train 2 */
  3449. reg = FDI_TX_CTL(pipe);
  3450. temp = I915_READ(reg);
  3451. temp &= ~FDI_LINK_TRAIN_NONE;
  3452. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3453. if (IS_GEN6(dev_priv)) {
  3454. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3455. /* SNB-B */
  3456. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3457. }
  3458. I915_WRITE(reg, temp);
  3459. reg = FDI_RX_CTL(pipe);
  3460. temp = I915_READ(reg);
  3461. if (HAS_PCH_CPT(dev_priv)) {
  3462. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3463. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3464. } else {
  3465. temp &= ~FDI_LINK_TRAIN_NONE;
  3466. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3467. }
  3468. I915_WRITE(reg, temp);
  3469. POSTING_READ(reg);
  3470. udelay(150);
  3471. for (i = 0; i < 4; i++) {
  3472. reg = FDI_TX_CTL(pipe);
  3473. temp = I915_READ(reg);
  3474. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3475. temp |= snb_b_fdi_train_param[i];
  3476. I915_WRITE(reg, temp);
  3477. POSTING_READ(reg);
  3478. udelay(500);
  3479. for (retry = 0; retry < 5; retry++) {
  3480. reg = FDI_RX_IIR(pipe);
  3481. temp = I915_READ(reg);
  3482. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3483. if (temp & FDI_RX_SYMBOL_LOCK) {
  3484. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3485. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3486. break;
  3487. }
  3488. udelay(50);
  3489. }
  3490. if (retry < 5)
  3491. break;
  3492. }
  3493. if (i == 4)
  3494. DRM_ERROR("FDI train 2 fail!\n");
  3495. DRM_DEBUG_KMS("FDI train done.\n");
  3496. }
  3497. /* Manual link training for Ivy Bridge A0 parts */
  3498. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3499. const struct intel_crtc_state *crtc_state)
  3500. {
  3501. struct drm_device *dev = crtc->base.dev;
  3502. struct drm_i915_private *dev_priv = to_i915(dev);
  3503. int pipe = crtc->pipe;
  3504. i915_reg_t reg;
  3505. u32 temp, i, j;
  3506. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3507. for train result */
  3508. reg = FDI_RX_IMR(pipe);
  3509. temp = I915_READ(reg);
  3510. temp &= ~FDI_RX_SYMBOL_LOCK;
  3511. temp &= ~FDI_RX_BIT_LOCK;
  3512. I915_WRITE(reg, temp);
  3513. POSTING_READ(reg);
  3514. udelay(150);
  3515. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3516. I915_READ(FDI_RX_IIR(pipe)));
  3517. /* Try each vswing and preemphasis setting twice before moving on */
  3518. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3519. /* disable first in case we need to retry */
  3520. reg = FDI_TX_CTL(pipe);
  3521. temp = I915_READ(reg);
  3522. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3523. temp &= ~FDI_TX_ENABLE;
  3524. I915_WRITE(reg, temp);
  3525. reg = FDI_RX_CTL(pipe);
  3526. temp = I915_READ(reg);
  3527. temp &= ~FDI_LINK_TRAIN_AUTO;
  3528. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3529. temp &= ~FDI_RX_ENABLE;
  3530. I915_WRITE(reg, temp);
  3531. /* enable CPU FDI TX and PCH FDI RX */
  3532. reg = FDI_TX_CTL(pipe);
  3533. temp = I915_READ(reg);
  3534. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3535. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3536. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3537. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3538. temp |= snb_b_fdi_train_param[j/2];
  3539. temp |= FDI_COMPOSITE_SYNC;
  3540. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3541. I915_WRITE(FDI_RX_MISC(pipe),
  3542. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3543. reg = FDI_RX_CTL(pipe);
  3544. temp = I915_READ(reg);
  3545. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3546. temp |= FDI_COMPOSITE_SYNC;
  3547. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3548. POSTING_READ(reg);
  3549. udelay(1); /* should be 0.5us */
  3550. for (i = 0; i < 4; i++) {
  3551. reg = FDI_RX_IIR(pipe);
  3552. temp = I915_READ(reg);
  3553. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3554. if (temp & FDI_RX_BIT_LOCK ||
  3555. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3556. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3557. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3558. i);
  3559. break;
  3560. }
  3561. udelay(1); /* should be 0.5us */
  3562. }
  3563. if (i == 4) {
  3564. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3565. continue;
  3566. }
  3567. /* Train 2 */
  3568. reg = FDI_TX_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3571. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3572. I915_WRITE(reg, temp);
  3573. reg = FDI_RX_CTL(pipe);
  3574. temp = I915_READ(reg);
  3575. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3576. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3577. I915_WRITE(reg, temp);
  3578. POSTING_READ(reg);
  3579. udelay(2); /* should be 1.5us */
  3580. for (i = 0; i < 4; i++) {
  3581. reg = FDI_RX_IIR(pipe);
  3582. temp = I915_READ(reg);
  3583. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3584. if (temp & FDI_RX_SYMBOL_LOCK ||
  3585. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3586. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3587. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3588. i);
  3589. goto train_done;
  3590. }
  3591. udelay(2); /* should be 1.5us */
  3592. }
  3593. if (i == 4)
  3594. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3595. }
  3596. train_done:
  3597. DRM_DEBUG_KMS("FDI train done.\n");
  3598. }
  3599. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3600. {
  3601. struct drm_device *dev = intel_crtc->base.dev;
  3602. struct drm_i915_private *dev_priv = to_i915(dev);
  3603. int pipe = intel_crtc->pipe;
  3604. i915_reg_t reg;
  3605. u32 temp;
  3606. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3607. reg = FDI_RX_CTL(pipe);
  3608. temp = I915_READ(reg);
  3609. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3610. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3611. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3612. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3613. POSTING_READ(reg);
  3614. udelay(200);
  3615. /* Switch from Rawclk to PCDclk */
  3616. temp = I915_READ(reg);
  3617. I915_WRITE(reg, temp | FDI_PCDCLK);
  3618. POSTING_READ(reg);
  3619. udelay(200);
  3620. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3621. reg = FDI_TX_CTL(pipe);
  3622. temp = I915_READ(reg);
  3623. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3624. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3625. POSTING_READ(reg);
  3626. udelay(100);
  3627. }
  3628. }
  3629. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3630. {
  3631. struct drm_device *dev = intel_crtc->base.dev;
  3632. struct drm_i915_private *dev_priv = to_i915(dev);
  3633. int pipe = intel_crtc->pipe;
  3634. i915_reg_t reg;
  3635. u32 temp;
  3636. /* Switch from PCDclk to Rawclk */
  3637. reg = FDI_RX_CTL(pipe);
  3638. temp = I915_READ(reg);
  3639. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3640. /* Disable CPU FDI TX PLL */
  3641. reg = FDI_TX_CTL(pipe);
  3642. temp = I915_READ(reg);
  3643. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3644. POSTING_READ(reg);
  3645. udelay(100);
  3646. reg = FDI_RX_CTL(pipe);
  3647. temp = I915_READ(reg);
  3648. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3649. /* Wait for the clocks to turn off. */
  3650. POSTING_READ(reg);
  3651. udelay(100);
  3652. }
  3653. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3654. {
  3655. struct drm_device *dev = crtc->dev;
  3656. struct drm_i915_private *dev_priv = to_i915(dev);
  3657. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3658. int pipe = intel_crtc->pipe;
  3659. i915_reg_t reg;
  3660. u32 temp;
  3661. /* disable CPU FDI tx and PCH FDI rx */
  3662. reg = FDI_TX_CTL(pipe);
  3663. temp = I915_READ(reg);
  3664. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3665. POSTING_READ(reg);
  3666. reg = FDI_RX_CTL(pipe);
  3667. temp = I915_READ(reg);
  3668. temp &= ~(0x7 << 16);
  3669. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3670. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3671. POSTING_READ(reg);
  3672. udelay(100);
  3673. /* Ironlake workaround, disable clock pointer after downing FDI */
  3674. if (HAS_PCH_IBX(dev_priv))
  3675. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3676. /* still set train pattern 1 */
  3677. reg = FDI_TX_CTL(pipe);
  3678. temp = I915_READ(reg);
  3679. temp &= ~FDI_LINK_TRAIN_NONE;
  3680. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3681. I915_WRITE(reg, temp);
  3682. reg = FDI_RX_CTL(pipe);
  3683. temp = I915_READ(reg);
  3684. if (HAS_PCH_CPT(dev_priv)) {
  3685. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3686. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3687. } else {
  3688. temp &= ~FDI_LINK_TRAIN_NONE;
  3689. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3690. }
  3691. /* BPC in FDI rx is consistent with that in PIPECONF */
  3692. temp &= ~(0x07 << 16);
  3693. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3694. I915_WRITE(reg, temp);
  3695. POSTING_READ(reg);
  3696. udelay(100);
  3697. }
  3698. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3699. {
  3700. struct drm_crtc *crtc;
  3701. bool cleanup_done;
  3702. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3703. struct drm_crtc_commit *commit;
  3704. spin_lock(&crtc->commit_lock);
  3705. commit = list_first_entry_or_null(&crtc->commit_list,
  3706. struct drm_crtc_commit, commit_entry);
  3707. cleanup_done = commit ?
  3708. try_wait_for_completion(&commit->cleanup_done) : true;
  3709. spin_unlock(&crtc->commit_lock);
  3710. if (cleanup_done)
  3711. continue;
  3712. drm_crtc_wait_one_vblank(crtc);
  3713. return true;
  3714. }
  3715. return false;
  3716. }
  3717. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3718. {
  3719. u32 temp;
  3720. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3721. mutex_lock(&dev_priv->sb_lock);
  3722. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3723. temp |= SBI_SSCCTL_DISABLE;
  3724. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3725. mutex_unlock(&dev_priv->sb_lock);
  3726. }
  3727. /* Program iCLKIP clock to the desired frequency */
  3728. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3729. {
  3730. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3731. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3732. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3733. u32 temp;
  3734. lpt_disable_iclkip(dev_priv);
  3735. /* The iCLK virtual clock root frequency is in MHz,
  3736. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3737. * divisors, it is necessary to divide one by another, so we
  3738. * convert the virtual clock precision to KHz here for higher
  3739. * precision.
  3740. */
  3741. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3742. u32 iclk_virtual_root_freq = 172800 * 1000;
  3743. u32 iclk_pi_range = 64;
  3744. u32 desired_divisor;
  3745. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3746. clock << auxdiv);
  3747. divsel = (desired_divisor / iclk_pi_range) - 2;
  3748. phaseinc = desired_divisor % iclk_pi_range;
  3749. /*
  3750. * Near 20MHz is a corner case which is
  3751. * out of range for the 7-bit divisor
  3752. */
  3753. if (divsel <= 0x7f)
  3754. break;
  3755. }
  3756. /* This should not happen with any sane values */
  3757. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3758. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3759. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3760. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3761. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3762. clock,
  3763. auxdiv,
  3764. divsel,
  3765. phasedir,
  3766. phaseinc);
  3767. mutex_lock(&dev_priv->sb_lock);
  3768. /* Program SSCDIVINTPHASE6 */
  3769. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3770. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3771. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3772. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3773. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3774. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3775. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3776. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3777. /* Program SSCAUXDIV */
  3778. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3779. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3780. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3781. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3782. /* Enable modulator and associated divider */
  3783. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3784. temp &= ~SBI_SSCCTL_DISABLE;
  3785. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3786. mutex_unlock(&dev_priv->sb_lock);
  3787. /* Wait for initialization time */
  3788. udelay(24);
  3789. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3790. }
  3791. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3792. {
  3793. u32 divsel, phaseinc, auxdiv;
  3794. u32 iclk_virtual_root_freq = 172800 * 1000;
  3795. u32 iclk_pi_range = 64;
  3796. u32 desired_divisor;
  3797. u32 temp;
  3798. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3799. return 0;
  3800. mutex_lock(&dev_priv->sb_lock);
  3801. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3802. if (temp & SBI_SSCCTL_DISABLE) {
  3803. mutex_unlock(&dev_priv->sb_lock);
  3804. return 0;
  3805. }
  3806. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3807. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3808. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3809. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3810. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3811. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3812. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3813. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3814. mutex_unlock(&dev_priv->sb_lock);
  3815. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3816. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3817. desired_divisor << auxdiv);
  3818. }
  3819. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3820. enum pipe pch_transcoder)
  3821. {
  3822. struct drm_device *dev = crtc->base.dev;
  3823. struct drm_i915_private *dev_priv = to_i915(dev);
  3824. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3825. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3826. I915_READ(HTOTAL(cpu_transcoder)));
  3827. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3828. I915_READ(HBLANK(cpu_transcoder)));
  3829. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3830. I915_READ(HSYNC(cpu_transcoder)));
  3831. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3832. I915_READ(VTOTAL(cpu_transcoder)));
  3833. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3834. I915_READ(VBLANK(cpu_transcoder)));
  3835. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3836. I915_READ(VSYNC(cpu_transcoder)));
  3837. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3838. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3839. }
  3840. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3841. {
  3842. struct drm_i915_private *dev_priv = to_i915(dev);
  3843. uint32_t temp;
  3844. temp = I915_READ(SOUTH_CHICKEN1);
  3845. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3846. return;
  3847. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3848. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3849. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3850. if (enable)
  3851. temp |= FDI_BC_BIFURCATION_SELECT;
  3852. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3853. I915_WRITE(SOUTH_CHICKEN1, temp);
  3854. POSTING_READ(SOUTH_CHICKEN1);
  3855. }
  3856. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3857. {
  3858. struct drm_device *dev = intel_crtc->base.dev;
  3859. switch (intel_crtc->pipe) {
  3860. case PIPE_A:
  3861. break;
  3862. case PIPE_B:
  3863. if (intel_crtc->config->fdi_lanes > 2)
  3864. cpt_set_fdi_bc_bifurcation(dev, false);
  3865. else
  3866. cpt_set_fdi_bc_bifurcation(dev, true);
  3867. break;
  3868. case PIPE_C:
  3869. cpt_set_fdi_bc_bifurcation(dev, true);
  3870. break;
  3871. default:
  3872. BUG();
  3873. }
  3874. }
  3875. /* Return which DP Port should be selected for Transcoder DP control */
  3876. static enum port
  3877. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3878. {
  3879. struct drm_device *dev = crtc->base.dev;
  3880. struct intel_encoder *encoder;
  3881. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3882. if (encoder->type == INTEL_OUTPUT_DP ||
  3883. encoder->type == INTEL_OUTPUT_EDP)
  3884. return enc_to_dig_port(&encoder->base)->port;
  3885. }
  3886. return -1;
  3887. }
  3888. /*
  3889. * Enable PCH resources required for PCH ports:
  3890. * - PCH PLLs
  3891. * - FDI training & RX/TX
  3892. * - update transcoder timings
  3893. * - DP transcoding bits
  3894. * - transcoder
  3895. */
  3896. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3897. {
  3898. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3899. struct drm_device *dev = crtc->base.dev;
  3900. struct drm_i915_private *dev_priv = to_i915(dev);
  3901. int pipe = crtc->pipe;
  3902. u32 temp;
  3903. assert_pch_transcoder_disabled(dev_priv, pipe);
  3904. if (IS_IVYBRIDGE(dev_priv))
  3905. ivybridge_update_fdi_bc_bifurcation(crtc);
  3906. /* Write the TU size bits before fdi link training, so that error
  3907. * detection works. */
  3908. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3909. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3910. /* For PCH output, training FDI link */
  3911. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3912. /* We need to program the right clock selection before writing the pixel
  3913. * mutliplier into the DPLL. */
  3914. if (HAS_PCH_CPT(dev_priv)) {
  3915. u32 sel;
  3916. temp = I915_READ(PCH_DPLL_SEL);
  3917. temp |= TRANS_DPLL_ENABLE(pipe);
  3918. sel = TRANS_DPLLB_SEL(pipe);
  3919. if (crtc_state->shared_dpll ==
  3920. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3921. temp |= sel;
  3922. else
  3923. temp &= ~sel;
  3924. I915_WRITE(PCH_DPLL_SEL, temp);
  3925. }
  3926. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3927. * transcoder, and we actually should do this to not upset any PCH
  3928. * transcoder that already use the clock when we share it.
  3929. *
  3930. * Note that enable_shared_dpll tries to do the right thing, but
  3931. * get_shared_dpll unconditionally resets the pll - we need that to have
  3932. * the right LVDS enable sequence. */
  3933. intel_enable_shared_dpll(crtc);
  3934. /* set transcoder timing, panel must allow it */
  3935. assert_panel_unlocked(dev_priv, pipe);
  3936. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3937. intel_fdi_normal_train(crtc);
  3938. /* For PCH DP, enable TRANS_DP_CTL */
  3939. if (HAS_PCH_CPT(dev_priv) &&
  3940. intel_crtc_has_dp_encoder(crtc_state)) {
  3941. const struct drm_display_mode *adjusted_mode =
  3942. &crtc_state->base.adjusted_mode;
  3943. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3944. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3945. temp = I915_READ(reg);
  3946. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3947. TRANS_DP_SYNC_MASK |
  3948. TRANS_DP_BPC_MASK);
  3949. temp |= TRANS_DP_OUTPUT_ENABLE;
  3950. temp |= bpc << 9; /* same format but at 11:9 */
  3951. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3952. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3953. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3954. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3955. switch (intel_trans_dp_port_sel(crtc)) {
  3956. case PORT_B:
  3957. temp |= TRANS_DP_PORT_SEL_B;
  3958. break;
  3959. case PORT_C:
  3960. temp |= TRANS_DP_PORT_SEL_C;
  3961. break;
  3962. case PORT_D:
  3963. temp |= TRANS_DP_PORT_SEL_D;
  3964. break;
  3965. default:
  3966. BUG();
  3967. }
  3968. I915_WRITE(reg, temp);
  3969. }
  3970. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3971. }
  3972. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3973. {
  3974. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3975. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3976. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3977. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3978. lpt_program_iclkip(crtc);
  3979. /* Set transcoder timing. */
  3980. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3981. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3982. }
  3983. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3984. {
  3985. struct drm_i915_private *dev_priv = to_i915(dev);
  3986. i915_reg_t dslreg = PIPEDSL(pipe);
  3987. u32 temp;
  3988. temp = I915_READ(dslreg);
  3989. udelay(500);
  3990. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3991. if (wait_for(I915_READ(dslreg) != temp, 5))
  3992. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3993. }
  3994. }
  3995. static int
  3996. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3997. unsigned int scaler_user, int *scaler_id,
  3998. int src_w, int src_h, int dst_w, int dst_h)
  3999. {
  4000. struct intel_crtc_scaler_state *scaler_state =
  4001. &crtc_state->scaler_state;
  4002. struct intel_crtc *intel_crtc =
  4003. to_intel_crtc(crtc_state->base.crtc);
  4004. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4005. const struct drm_display_mode *adjusted_mode =
  4006. &crtc_state->base.adjusted_mode;
  4007. int need_scaling;
  4008. /*
  4009. * Src coordinates are already rotated by 270 degrees for
  4010. * the 90/270 degree plane rotation cases (to match the
  4011. * GTT mapping), hence no need to account for rotation here.
  4012. */
  4013. need_scaling = src_w != dst_w || src_h != dst_h;
  4014. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4015. need_scaling = true;
  4016. /*
  4017. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4018. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4019. * Once NV12 is enabled, handle it here while allocating scaler
  4020. * for NV12.
  4021. */
  4022. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4023. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4024. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4025. return -EINVAL;
  4026. }
  4027. /*
  4028. * if plane is being disabled or scaler is no more required or force detach
  4029. * - free scaler binded to this plane/crtc
  4030. * - in order to do this, update crtc->scaler_usage
  4031. *
  4032. * Here scaler state in crtc_state is set free so that
  4033. * scaler can be assigned to other user. Actual register
  4034. * update to free the scaler is done in plane/panel-fit programming.
  4035. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4036. */
  4037. if (force_detach || !need_scaling) {
  4038. if (*scaler_id >= 0) {
  4039. scaler_state->scaler_users &= ~(1 << scaler_user);
  4040. scaler_state->scalers[*scaler_id].in_use = 0;
  4041. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4042. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4043. intel_crtc->pipe, scaler_user, *scaler_id,
  4044. scaler_state->scaler_users);
  4045. *scaler_id = -1;
  4046. }
  4047. return 0;
  4048. }
  4049. /* range checks */
  4050. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4051. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4052. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4053. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4054. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4055. "size is out of scaler range\n",
  4056. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4057. return -EINVAL;
  4058. }
  4059. /* mark this plane as a scaler user in crtc_state */
  4060. scaler_state->scaler_users |= (1 << scaler_user);
  4061. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4062. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4063. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4064. scaler_state->scaler_users);
  4065. return 0;
  4066. }
  4067. /**
  4068. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4069. *
  4070. * @state: crtc's scaler state
  4071. *
  4072. * Return
  4073. * 0 - scaler_usage updated successfully
  4074. * error - requested scaling cannot be supported or other error condition
  4075. */
  4076. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4077. {
  4078. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4079. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4080. &state->scaler_state.scaler_id,
  4081. state->pipe_src_w, state->pipe_src_h,
  4082. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4083. }
  4084. /**
  4085. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4086. *
  4087. * @state: crtc's scaler state
  4088. * @plane_state: atomic plane state to update
  4089. *
  4090. * Return
  4091. * 0 - scaler_usage updated successfully
  4092. * error - requested scaling cannot be supported or other error condition
  4093. */
  4094. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4095. struct intel_plane_state *plane_state)
  4096. {
  4097. struct intel_plane *intel_plane =
  4098. to_intel_plane(plane_state->base.plane);
  4099. struct drm_framebuffer *fb = plane_state->base.fb;
  4100. int ret;
  4101. bool force_detach = !fb || !plane_state->base.visible;
  4102. ret = skl_update_scaler(crtc_state, force_detach,
  4103. drm_plane_index(&intel_plane->base),
  4104. &plane_state->scaler_id,
  4105. drm_rect_width(&plane_state->base.src) >> 16,
  4106. drm_rect_height(&plane_state->base.src) >> 16,
  4107. drm_rect_width(&plane_state->base.dst),
  4108. drm_rect_height(&plane_state->base.dst));
  4109. if (ret || plane_state->scaler_id < 0)
  4110. return ret;
  4111. /* check colorkey */
  4112. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4113. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4114. intel_plane->base.base.id,
  4115. intel_plane->base.name);
  4116. return -EINVAL;
  4117. }
  4118. /* Check src format */
  4119. switch (fb->format->format) {
  4120. case DRM_FORMAT_RGB565:
  4121. case DRM_FORMAT_XBGR8888:
  4122. case DRM_FORMAT_XRGB8888:
  4123. case DRM_FORMAT_ABGR8888:
  4124. case DRM_FORMAT_ARGB8888:
  4125. case DRM_FORMAT_XRGB2101010:
  4126. case DRM_FORMAT_XBGR2101010:
  4127. case DRM_FORMAT_YUYV:
  4128. case DRM_FORMAT_YVYU:
  4129. case DRM_FORMAT_UYVY:
  4130. case DRM_FORMAT_VYUY:
  4131. break;
  4132. default:
  4133. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4134. intel_plane->base.base.id, intel_plane->base.name,
  4135. fb->base.id, fb->format->format);
  4136. return -EINVAL;
  4137. }
  4138. return 0;
  4139. }
  4140. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4141. {
  4142. int i;
  4143. for (i = 0; i < crtc->num_scalers; i++)
  4144. skl_detach_scaler(crtc, i);
  4145. }
  4146. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4147. {
  4148. struct drm_device *dev = crtc->base.dev;
  4149. struct drm_i915_private *dev_priv = to_i915(dev);
  4150. int pipe = crtc->pipe;
  4151. struct intel_crtc_scaler_state *scaler_state =
  4152. &crtc->config->scaler_state;
  4153. if (crtc->config->pch_pfit.enabled) {
  4154. int id;
  4155. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4156. return;
  4157. id = scaler_state->scaler_id;
  4158. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4159. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4160. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4161. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4162. }
  4163. }
  4164. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4165. {
  4166. struct drm_device *dev = crtc->base.dev;
  4167. struct drm_i915_private *dev_priv = to_i915(dev);
  4168. int pipe = crtc->pipe;
  4169. if (crtc->config->pch_pfit.enabled) {
  4170. /* Force use of hard-coded filter coefficients
  4171. * as some pre-programmed values are broken,
  4172. * e.g. x201.
  4173. */
  4174. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4175. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4176. PF_PIPE_SEL_IVB(pipe));
  4177. else
  4178. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4179. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4180. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4181. }
  4182. }
  4183. void hsw_enable_ips(struct intel_crtc *crtc)
  4184. {
  4185. struct drm_device *dev = crtc->base.dev;
  4186. struct drm_i915_private *dev_priv = to_i915(dev);
  4187. if (!crtc->config->ips_enabled)
  4188. return;
  4189. /*
  4190. * We can only enable IPS after we enable a plane and wait for a vblank
  4191. * This function is called from post_plane_update, which is run after
  4192. * a vblank wait.
  4193. */
  4194. assert_plane_enabled(dev_priv, crtc->plane);
  4195. if (IS_BROADWELL(dev_priv)) {
  4196. mutex_lock(&dev_priv->rps.hw_lock);
  4197. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4198. mutex_unlock(&dev_priv->rps.hw_lock);
  4199. /* Quoting Art Runyan: "its not safe to expect any particular
  4200. * value in IPS_CTL bit 31 after enabling IPS through the
  4201. * mailbox." Moreover, the mailbox may return a bogus state,
  4202. * so we need to just enable it and continue on.
  4203. */
  4204. } else {
  4205. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4206. /* The bit only becomes 1 in the next vblank, so this wait here
  4207. * is essentially intel_wait_for_vblank. If we don't have this
  4208. * and don't wait for vblanks until the end of crtc_enable, then
  4209. * the HW state readout code will complain that the expected
  4210. * IPS_CTL value is not the one we read. */
  4211. if (intel_wait_for_register(dev_priv,
  4212. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4213. 50))
  4214. DRM_ERROR("Timed out waiting for IPS enable\n");
  4215. }
  4216. }
  4217. void hsw_disable_ips(struct intel_crtc *crtc)
  4218. {
  4219. struct drm_device *dev = crtc->base.dev;
  4220. struct drm_i915_private *dev_priv = to_i915(dev);
  4221. if (!crtc->config->ips_enabled)
  4222. return;
  4223. assert_plane_enabled(dev_priv, crtc->plane);
  4224. if (IS_BROADWELL(dev_priv)) {
  4225. mutex_lock(&dev_priv->rps.hw_lock);
  4226. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4227. mutex_unlock(&dev_priv->rps.hw_lock);
  4228. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4229. if (intel_wait_for_register(dev_priv,
  4230. IPS_CTL, IPS_ENABLE, 0,
  4231. 42))
  4232. DRM_ERROR("Timed out waiting for IPS disable\n");
  4233. } else {
  4234. I915_WRITE(IPS_CTL, 0);
  4235. POSTING_READ(IPS_CTL);
  4236. }
  4237. /* We need to wait for a vblank before we can disable the plane. */
  4238. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4239. }
  4240. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4241. {
  4242. if (intel_crtc->overlay) {
  4243. struct drm_device *dev = intel_crtc->base.dev;
  4244. mutex_lock(&dev->struct_mutex);
  4245. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4246. mutex_unlock(&dev->struct_mutex);
  4247. }
  4248. /* Let userspace switch the overlay on again. In most cases userspace
  4249. * has to recompute where to put it anyway.
  4250. */
  4251. }
  4252. /**
  4253. * intel_post_enable_primary - Perform operations after enabling primary plane
  4254. * @crtc: the CRTC whose primary plane was just enabled
  4255. *
  4256. * Performs potentially sleeping operations that must be done after the primary
  4257. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4258. * called due to an explicit primary plane update, or due to an implicit
  4259. * re-enable that is caused when a sprite plane is updated to no longer
  4260. * completely hide the primary plane.
  4261. */
  4262. static void
  4263. intel_post_enable_primary(struct drm_crtc *crtc)
  4264. {
  4265. struct drm_device *dev = crtc->dev;
  4266. struct drm_i915_private *dev_priv = to_i915(dev);
  4267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4268. int pipe = intel_crtc->pipe;
  4269. /*
  4270. * FIXME IPS should be fine as long as one plane is
  4271. * enabled, but in practice it seems to have problems
  4272. * when going from primary only to sprite only and vice
  4273. * versa.
  4274. */
  4275. hsw_enable_ips(intel_crtc);
  4276. /*
  4277. * Gen2 reports pipe underruns whenever all planes are disabled.
  4278. * So don't enable underrun reporting before at least some planes
  4279. * are enabled.
  4280. * FIXME: Need to fix the logic to work when we turn off all planes
  4281. * but leave the pipe running.
  4282. */
  4283. if (IS_GEN2(dev_priv))
  4284. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4285. /* Underruns don't always raise interrupts, so check manually. */
  4286. intel_check_cpu_fifo_underruns(dev_priv);
  4287. intel_check_pch_fifo_underruns(dev_priv);
  4288. }
  4289. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4290. static void
  4291. intel_pre_disable_primary(struct drm_crtc *crtc)
  4292. {
  4293. struct drm_device *dev = crtc->dev;
  4294. struct drm_i915_private *dev_priv = to_i915(dev);
  4295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4296. int pipe = intel_crtc->pipe;
  4297. /*
  4298. * Gen2 reports pipe underruns whenever all planes are disabled.
  4299. * So diasble underrun reporting before all the planes get disabled.
  4300. * FIXME: Need to fix the logic to work when we turn off all planes
  4301. * but leave the pipe running.
  4302. */
  4303. if (IS_GEN2(dev_priv))
  4304. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4305. /*
  4306. * FIXME IPS should be fine as long as one plane is
  4307. * enabled, but in practice it seems to have problems
  4308. * when going from primary only to sprite only and vice
  4309. * versa.
  4310. */
  4311. hsw_disable_ips(intel_crtc);
  4312. }
  4313. /* FIXME get rid of this and use pre_plane_update */
  4314. static void
  4315. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4316. {
  4317. struct drm_device *dev = crtc->dev;
  4318. struct drm_i915_private *dev_priv = to_i915(dev);
  4319. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4320. int pipe = intel_crtc->pipe;
  4321. intel_pre_disable_primary(crtc);
  4322. /*
  4323. * Vblank time updates from the shadow to live plane control register
  4324. * are blocked if the memory self-refresh mode is active at that
  4325. * moment. So to make sure the plane gets truly disabled, disable
  4326. * first the self-refresh mode. The self-refresh enable bit in turn
  4327. * will be checked/applied by the HW only at the next frame start
  4328. * event which is after the vblank start event, so we need to have a
  4329. * wait-for-vblank between disabling the plane and the pipe.
  4330. */
  4331. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4332. intel_set_memory_cxsr(dev_priv, false))
  4333. intel_wait_for_vblank(dev_priv, pipe);
  4334. }
  4335. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4336. {
  4337. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4338. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4339. struct intel_crtc_state *pipe_config =
  4340. to_intel_crtc_state(crtc->base.state);
  4341. struct drm_plane *primary = crtc->base.primary;
  4342. struct drm_plane_state *old_pri_state =
  4343. drm_atomic_get_existing_plane_state(old_state, primary);
  4344. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4345. if (pipe_config->update_wm_post && pipe_config->base.active)
  4346. intel_update_watermarks(crtc);
  4347. if (old_pri_state) {
  4348. struct intel_plane_state *primary_state =
  4349. to_intel_plane_state(primary->state);
  4350. struct intel_plane_state *old_primary_state =
  4351. to_intel_plane_state(old_pri_state);
  4352. intel_fbc_post_update(crtc);
  4353. if (primary_state->base.visible &&
  4354. (needs_modeset(&pipe_config->base) ||
  4355. !old_primary_state->base.visible))
  4356. intel_post_enable_primary(&crtc->base);
  4357. }
  4358. }
  4359. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4360. struct intel_crtc_state *pipe_config)
  4361. {
  4362. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4363. struct drm_device *dev = crtc->base.dev;
  4364. struct drm_i915_private *dev_priv = to_i915(dev);
  4365. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4366. struct drm_plane *primary = crtc->base.primary;
  4367. struct drm_plane_state *old_pri_state =
  4368. drm_atomic_get_existing_plane_state(old_state, primary);
  4369. bool modeset = needs_modeset(&pipe_config->base);
  4370. struct intel_atomic_state *old_intel_state =
  4371. to_intel_atomic_state(old_state);
  4372. if (old_pri_state) {
  4373. struct intel_plane_state *primary_state =
  4374. to_intel_plane_state(primary->state);
  4375. struct intel_plane_state *old_primary_state =
  4376. to_intel_plane_state(old_pri_state);
  4377. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4378. if (old_primary_state->base.visible &&
  4379. (modeset || !primary_state->base.visible))
  4380. intel_pre_disable_primary(&crtc->base);
  4381. }
  4382. /*
  4383. * Vblank time updates from the shadow to live plane control register
  4384. * are blocked if the memory self-refresh mode is active at that
  4385. * moment. So to make sure the plane gets truly disabled, disable
  4386. * first the self-refresh mode. The self-refresh enable bit in turn
  4387. * will be checked/applied by the HW only at the next frame start
  4388. * event which is after the vblank start event, so we need to have a
  4389. * wait-for-vblank between disabling the plane and the pipe.
  4390. */
  4391. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4392. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4393. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4394. /*
  4395. * IVB workaround: must disable low power watermarks for at least
  4396. * one frame before enabling scaling. LP watermarks can be re-enabled
  4397. * when scaling is disabled.
  4398. *
  4399. * WaCxSRDisabledForSpriteScaling:ivb
  4400. */
  4401. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4402. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4403. /*
  4404. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4405. * watermark programming here.
  4406. */
  4407. if (needs_modeset(&pipe_config->base))
  4408. return;
  4409. /*
  4410. * For platforms that support atomic watermarks, program the
  4411. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4412. * will be the intermediate values that are safe for both pre- and
  4413. * post- vblank; when vblank happens, the 'active' values will be set
  4414. * to the final 'target' values and we'll do this again to get the
  4415. * optimal watermarks. For gen9+ platforms, the values we program here
  4416. * will be the final target values which will get automatically latched
  4417. * at vblank time; no further programming will be necessary.
  4418. *
  4419. * If a platform hasn't been transitioned to atomic watermarks yet,
  4420. * we'll continue to update watermarks the old way, if flags tell
  4421. * us to.
  4422. */
  4423. if (dev_priv->display.initial_watermarks != NULL)
  4424. dev_priv->display.initial_watermarks(old_intel_state,
  4425. pipe_config);
  4426. else if (pipe_config->update_wm_pre)
  4427. intel_update_watermarks(crtc);
  4428. }
  4429. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4430. {
  4431. struct drm_device *dev = crtc->dev;
  4432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4433. struct drm_plane *p;
  4434. int pipe = intel_crtc->pipe;
  4435. intel_crtc_dpms_overlay_disable(intel_crtc);
  4436. drm_for_each_plane_mask(p, dev, plane_mask)
  4437. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4438. /*
  4439. * FIXME: Once we grow proper nuclear flip support out of this we need
  4440. * to compute the mask of flip planes precisely. For the time being
  4441. * consider this a flip to a NULL plane.
  4442. */
  4443. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4444. }
  4445. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4446. struct intel_crtc_state *crtc_state,
  4447. struct drm_atomic_state *old_state)
  4448. {
  4449. struct drm_connector_state *conn_state;
  4450. struct drm_connector *conn;
  4451. int i;
  4452. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4453. struct intel_encoder *encoder =
  4454. to_intel_encoder(conn_state->best_encoder);
  4455. if (conn_state->crtc != crtc)
  4456. continue;
  4457. if (encoder->pre_pll_enable)
  4458. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4459. }
  4460. }
  4461. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4462. struct intel_crtc_state *crtc_state,
  4463. struct drm_atomic_state *old_state)
  4464. {
  4465. struct drm_connector_state *conn_state;
  4466. struct drm_connector *conn;
  4467. int i;
  4468. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4469. struct intel_encoder *encoder =
  4470. to_intel_encoder(conn_state->best_encoder);
  4471. if (conn_state->crtc != crtc)
  4472. continue;
  4473. if (encoder->pre_enable)
  4474. encoder->pre_enable(encoder, crtc_state, conn_state);
  4475. }
  4476. }
  4477. static void intel_encoders_enable(struct drm_crtc *crtc,
  4478. struct intel_crtc_state *crtc_state,
  4479. struct drm_atomic_state *old_state)
  4480. {
  4481. struct drm_connector_state *conn_state;
  4482. struct drm_connector *conn;
  4483. int i;
  4484. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4485. struct intel_encoder *encoder =
  4486. to_intel_encoder(conn_state->best_encoder);
  4487. if (conn_state->crtc != crtc)
  4488. continue;
  4489. encoder->enable(encoder, crtc_state, conn_state);
  4490. intel_opregion_notify_encoder(encoder, true);
  4491. }
  4492. }
  4493. static void intel_encoders_disable(struct drm_crtc *crtc,
  4494. struct intel_crtc_state *old_crtc_state,
  4495. struct drm_atomic_state *old_state)
  4496. {
  4497. struct drm_connector_state *old_conn_state;
  4498. struct drm_connector *conn;
  4499. int i;
  4500. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4501. struct intel_encoder *encoder =
  4502. to_intel_encoder(old_conn_state->best_encoder);
  4503. if (old_conn_state->crtc != crtc)
  4504. continue;
  4505. intel_opregion_notify_encoder(encoder, false);
  4506. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4507. }
  4508. }
  4509. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4510. struct intel_crtc_state *old_crtc_state,
  4511. struct drm_atomic_state *old_state)
  4512. {
  4513. struct drm_connector_state *old_conn_state;
  4514. struct drm_connector *conn;
  4515. int i;
  4516. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4517. struct intel_encoder *encoder =
  4518. to_intel_encoder(old_conn_state->best_encoder);
  4519. if (old_conn_state->crtc != crtc)
  4520. continue;
  4521. if (encoder->post_disable)
  4522. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4523. }
  4524. }
  4525. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4526. struct intel_crtc_state *old_crtc_state,
  4527. struct drm_atomic_state *old_state)
  4528. {
  4529. struct drm_connector_state *old_conn_state;
  4530. struct drm_connector *conn;
  4531. int i;
  4532. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4533. struct intel_encoder *encoder =
  4534. to_intel_encoder(old_conn_state->best_encoder);
  4535. if (old_conn_state->crtc != crtc)
  4536. continue;
  4537. if (encoder->post_pll_disable)
  4538. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4539. }
  4540. }
  4541. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4542. struct drm_atomic_state *old_state)
  4543. {
  4544. struct drm_crtc *crtc = pipe_config->base.crtc;
  4545. struct drm_device *dev = crtc->dev;
  4546. struct drm_i915_private *dev_priv = to_i915(dev);
  4547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4548. int pipe = intel_crtc->pipe;
  4549. struct intel_atomic_state *old_intel_state =
  4550. to_intel_atomic_state(old_state);
  4551. if (WARN_ON(intel_crtc->active))
  4552. return;
  4553. /*
  4554. * Sometimes spurious CPU pipe underruns happen during FDI
  4555. * training, at least with VGA+HDMI cloning. Suppress them.
  4556. *
  4557. * On ILK we get an occasional spurious CPU pipe underruns
  4558. * between eDP port A enable and vdd enable. Also PCH port
  4559. * enable seems to result in the occasional CPU pipe underrun.
  4560. *
  4561. * Spurious PCH underruns also occur during PCH enabling.
  4562. */
  4563. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4564. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4565. if (intel_crtc->config->has_pch_encoder)
  4566. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4567. if (intel_crtc->config->has_pch_encoder)
  4568. intel_prepare_shared_dpll(intel_crtc);
  4569. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4570. intel_dp_set_m_n(intel_crtc, M1_N1);
  4571. intel_set_pipe_timings(intel_crtc);
  4572. intel_set_pipe_src_size(intel_crtc);
  4573. if (intel_crtc->config->has_pch_encoder) {
  4574. intel_cpu_transcoder_set_m_n(intel_crtc,
  4575. &intel_crtc->config->fdi_m_n, NULL);
  4576. }
  4577. ironlake_set_pipeconf(crtc);
  4578. intel_crtc->active = true;
  4579. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4580. if (intel_crtc->config->has_pch_encoder) {
  4581. /* Note: FDI PLL enabling _must_ be done before we enable the
  4582. * cpu pipes, hence this is separate from all the other fdi/pch
  4583. * enabling. */
  4584. ironlake_fdi_pll_enable(intel_crtc);
  4585. } else {
  4586. assert_fdi_tx_disabled(dev_priv, pipe);
  4587. assert_fdi_rx_disabled(dev_priv, pipe);
  4588. }
  4589. ironlake_pfit_enable(intel_crtc);
  4590. /*
  4591. * On ILK+ LUT must be loaded before the pipe is running but with
  4592. * clocks enabled
  4593. */
  4594. intel_color_load_luts(&pipe_config->base);
  4595. if (dev_priv->display.initial_watermarks != NULL)
  4596. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4597. intel_enable_pipe(intel_crtc);
  4598. if (intel_crtc->config->has_pch_encoder)
  4599. ironlake_pch_enable(pipe_config);
  4600. assert_vblank_disabled(crtc);
  4601. drm_crtc_vblank_on(crtc);
  4602. intel_encoders_enable(crtc, pipe_config, old_state);
  4603. if (HAS_PCH_CPT(dev_priv))
  4604. cpt_verify_modeset(dev, intel_crtc->pipe);
  4605. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4606. if (intel_crtc->config->has_pch_encoder)
  4607. intel_wait_for_vblank(dev_priv, pipe);
  4608. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4609. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4610. }
  4611. /* IPS only exists on ULT machines and is tied to pipe A. */
  4612. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4613. {
  4614. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4615. }
  4616. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4617. struct drm_atomic_state *old_state)
  4618. {
  4619. struct drm_crtc *crtc = pipe_config->base.crtc;
  4620. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4621. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4622. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4623. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4624. struct intel_atomic_state *old_intel_state =
  4625. to_intel_atomic_state(old_state);
  4626. if (WARN_ON(intel_crtc->active))
  4627. return;
  4628. if (intel_crtc->config->has_pch_encoder)
  4629. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4630. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4631. if (intel_crtc->config->shared_dpll)
  4632. intel_enable_shared_dpll(intel_crtc);
  4633. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4634. intel_dp_set_m_n(intel_crtc, M1_N1);
  4635. if (!transcoder_is_dsi(cpu_transcoder))
  4636. intel_set_pipe_timings(intel_crtc);
  4637. intel_set_pipe_src_size(intel_crtc);
  4638. if (cpu_transcoder != TRANSCODER_EDP &&
  4639. !transcoder_is_dsi(cpu_transcoder)) {
  4640. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4641. intel_crtc->config->pixel_multiplier - 1);
  4642. }
  4643. if (intel_crtc->config->has_pch_encoder) {
  4644. intel_cpu_transcoder_set_m_n(intel_crtc,
  4645. &intel_crtc->config->fdi_m_n, NULL);
  4646. }
  4647. if (!transcoder_is_dsi(cpu_transcoder))
  4648. haswell_set_pipeconf(crtc);
  4649. haswell_set_pipemisc(crtc);
  4650. intel_color_set_csc(&pipe_config->base);
  4651. intel_crtc->active = true;
  4652. if (intel_crtc->config->has_pch_encoder)
  4653. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4654. else
  4655. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4656. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4657. if (intel_crtc->config->has_pch_encoder)
  4658. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4659. if (!transcoder_is_dsi(cpu_transcoder))
  4660. intel_ddi_enable_pipe_clock(pipe_config);
  4661. if (INTEL_GEN(dev_priv) >= 9)
  4662. skylake_pfit_enable(intel_crtc);
  4663. else
  4664. ironlake_pfit_enable(intel_crtc);
  4665. /*
  4666. * On ILK+ LUT must be loaded before the pipe is running but with
  4667. * clocks enabled
  4668. */
  4669. intel_color_load_luts(&pipe_config->base);
  4670. intel_ddi_set_pipe_settings(pipe_config);
  4671. if (!transcoder_is_dsi(cpu_transcoder))
  4672. intel_ddi_enable_transcoder_func(pipe_config);
  4673. if (dev_priv->display.initial_watermarks != NULL)
  4674. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4675. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4676. if (!transcoder_is_dsi(cpu_transcoder))
  4677. intel_enable_pipe(intel_crtc);
  4678. if (intel_crtc->config->has_pch_encoder)
  4679. lpt_pch_enable(pipe_config);
  4680. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4681. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4682. assert_vblank_disabled(crtc);
  4683. drm_crtc_vblank_on(crtc);
  4684. intel_encoders_enable(crtc, pipe_config, old_state);
  4685. if (intel_crtc->config->has_pch_encoder) {
  4686. intel_wait_for_vblank(dev_priv, pipe);
  4687. intel_wait_for_vblank(dev_priv, pipe);
  4688. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4689. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4690. }
  4691. /* If we change the relative order between pipe/planes enabling, we need
  4692. * to change the workaround. */
  4693. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4694. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4695. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4696. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4697. }
  4698. }
  4699. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4700. {
  4701. struct drm_device *dev = crtc->base.dev;
  4702. struct drm_i915_private *dev_priv = to_i915(dev);
  4703. int pipe = crtc->pipe;
  4704. /* To avoid upsetting the power well on haswell only disable the pfit if
  4705. * it's in use. The hw state code will make sure we get this right. */
  4706. if (force || crtc->config->pch_pfit.enabled) {
  4707. I915_WRITE(PF_CTL(pipe), 0);
  4708. I915_WRITE(PF_WIN_POS(pipe), 0);
  4709. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4710. }
  4711. }
  4712. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4713. struct drm_atomic_state *old_state)
  4714. {
  4715. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4716. struct drm_device *dev = crtc->dev;
  4717. struct drm_i915_private *dev_priv = to_i915(dev);
  4718. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4719. int pipe = intel_crtc->pipe;
  4720. /*
  4721. * Sometimes spurious CPU pipe underruns happen when the
  4722. * pipe is already disabled, but FDI RX/TX is still enabled.
  4723. * Happens at least with VGA+HDMI cloning. Suppress them.
  4724. */
  4725. if (intel_crtc->config->has_pch_encoder) {
  4726. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4727. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4728. }
  4729. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4730. drm_crtc_vblank_off(crtc);
  4731. assert_vblank_disabled(crtc);
  4732. intel_disable_pipe(intel_crtc);
  4733. ironlake_pfit_disable(intel_crtc, false);
  4734. if (intel_crtc->config->has_pch_encoder)
  4735. ironlake_fdi_disable(crtc);
  4736. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4737. if (intel_crtc->config->has_pch_encoder) {
  4738. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4739. if (HAS_PCH_CPT(dev_priv)) {
  4740. i915_reg_t reg;
  4741. u32 temp;
  4742. /* disable TRANS_DP_CTL */
  4743. reg = TRANS_DP_CTL(pipe);
  4744. temp = I915_READ(reg);
  4745. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4746. TRANS_DP_PORT_SEL_MASK);
  4747. temp |= TRANS_DP_PORT_SEL_NONE;
  4748. I915_WRITE(reg, temp);
  4749. /* disable DPLL_SEL */
  4750. temp = I915_READ(PCH_DPLL_SEL);
  4751. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4752. I915_WRITE(PCH_DPLL_SEL, temp);
  4753. }
  4754. ironlake_fdi_pll_disable(intel_crtc);
  4755. }
  4756. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4757. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4758. }
  4759. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4760. struct drm_atomic_state *old_state)
  4761. {
  4762. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4763. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4764. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4765. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4766. if (intel_crtc->config->has_pch_encoder)
  4767. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4768. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4769. drm_crtc_vblank_off(crtc);
  4770. assert_vblank_disabled(crtc);
  4771. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4772. if (!transcoder_is_dsi(cpu_transcoder))
  4773. intel_disable_pipe(intel_crtc);
  4774. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4775. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4776. if (!transcoder_is_dsi(cpu_transcoder))
  4777. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4778. if (INTEL_GEN(dev_priv) >= 9)
  4779. skylake_scaler_disable(intel_crtc);
  4780. else
  4781. ironlake_pfit_disable(intel_crtc, false);
  4782. if (!transcoder_is_dsi(cpu_transcoder))
  4783. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4784. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4785. if (old_crtc_state->has_pch_encoder)
  4786. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4787. }
  4788. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4789. {
  4790. struct drm_device *dev = crtc->base.dev;
  4791. struct drm_i915_private *dev_priv = to_i915(dev);
  4792. struct intel_crtc_state *pipe_config = crtc->config;
  4793. if (!pipe_config->gmch_pfit.control)
  4794. return;
  4795. /*
  4796. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4797. * according to register description and PRM.
  4798. */
  4799. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4800. assert_pipe_disabled(dev_priv, crtc->pipe);
  4801. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4802. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4803. /* Border color in case we don't scale up to the full screen. Black by
  4804. * default, change to something else for debugging. */
  4805. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4806. }
  4807. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4808. {
  4809. switch (port) {
  4810. case PORT_A:
  4811. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4812. case PORT_B:
  4813. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4814. case PORT_C:
  4815. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4816. case PORT_D:
  4817. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4818. case PORT_E:
  4819. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4820. default:
  4821. MISSING_CASE(port);
  4822. return POWER_DOMAIN_PORT_OTHER;
  4823. }
  4824. }
  4825. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4826. struct intel_crtc_state *crtc_state)
  4827. {
  4828. struct drm_device *dev = crtc->dev;
  4829. struct drm_i915_private *dev_priv = to_i915(dev);
  4830. struct drm_encoder *encoder;
  4831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4832. enum pipe pipe = intel_crtc->pipe;
  4833. u64 mask;
  4834. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4835. if (!crtc_state->base.active)
  4836. return 0;
  4837. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4838. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4839. if (crtc_state->pch_pfit.enabled ||
  4840. crtc_state->pch_pfit.force_thru)
  4841. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4842. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4843. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4844. mask |= BIT_ULL(intel_encoder->power_domain);
  4845. }
  4846. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4847. mask |= BIT(POWER_DOMAIN_AUDIO);
  4848. if (crtc_state->shared_dpll)
  4849. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4850. return mask;
  4851. }
  4852. static u64
  4853. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4854. struct intel_crtc_state *crtc_state)
  4855. {
  4856. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4858. enum intel_display_power_domain domain;
  4859. u64 domains, new_domains, old_domains;
  4860. old_domains = intel_crtc->enabled_power_domains;
  4861. intel_crtc->enabled_power_domains = new_domains =
  4862. get_crtc_power_domains(crtc, crtc_state);
  4863. domains = new_domains & ~old_domains;
  4864. for_each_power_domain(domain, domains)
  4865. intel_display_power_get(dev_priv, domain);
  4866. return old_domains & ~new_domains;
  4867. }
  4868. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4869. u64 domains)
  4870. {
  4871. enum intel_display_power_domain domain;
  4872. for_each_power_domain(domain, domains)
  4873. intel_display_power_put(dev_priv, domain);
  4874. }
  4875. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4876. struct drm_atomic_state *old_state)
  4877. {
  4878. struct intel_atomic_state *old_intel_state =
  4879. to_intel_atomic_state(old_state);
  4880. struct drm_crtc *crtc = pipe_config->base.crtc;
  4881. struct drm_device *dev = crtc->dev;
  4882. struct drm_i915_private *dev_priv = to_i915(dev);
  4883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4884. int pipe = intel_crtc->pipe;
  4885. if (WARN_ON(intel_crtc->active))
  4886. return;
  4887. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4888. intel_dp_set_m_n(intel_crtc, M1_N1);
  4889. intel_set_pipe_timings(intel_crtc);
  4890. intel_set_pipe_src_size(intel_crtc);
  4891. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4892. struct drm_i915_private *dev_priv = to_i915(dev);
  4893. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4894. I915_WRITE(CHV_CANVAS(pipe), 0);
  4895. }
  4896. i9xx_set_pipeconf(intel_crtc);
  4897. intel_crtc->active = true;
  4898. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4899. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4900. if (IS_CHERRYVIEW(dev_priv)) {
  4901. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4902. chv_enable_pll(intel_crtc, intel_crtc->config);
  4903. } else {
  4904. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4905. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4906. }
  4907. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4908. i9xx_pfit_enable(intel_crtc);
  4909. intel_color_load_luts(&pipe_config->base);
  4910. dev_priv->display.initial_watermarks(old_intel_state,
  4911. pipe_config);
  4912. intel_enable_pipe(intel_crtc);
  4913. assert_vblank_disabled(crtc);
  4914. drm_crtc_vblank_on(crtc);
  4915. intel_encoders_enable(crtc, pipe_config, old_state);
  4916. }
  4917. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4918. {
  4919. struct drm_device *dev = crtc->base.dev;
  4920. struct drm_i915_private *dev_priv = to_i915(dev);
  4921. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4922. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4923. }
  4924. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4925. struct drm_atomic_state *old_state)
  4926. {
  4927. struct intel_atomic_state *old_intel_state =
  4928. to_intel_atomic_state(old_state);
  4929. struct drm_crtc *crtc = pipe_config->base.crtc;
  4930. struct drm_device *dev = crtc->dev;
  4931. struct drm_i915_private *dev_priv = to_i915(dev);
  4932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4933. enum pipe pipe = intel_crtc->pipe;
  4934. if (WARN_ON(intel_crtc->active))
  4935. return;
  4936. i9xx_set_pll_dividers(intel_crtc);
  4937. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4938. intel_dp_set_m_n(intel_crtc, M1_N1);
  4939. intel_set_pipe_timings(intel_crtc);
  4940. intel_set_pipe_src_size(intel_crtc);
  4941. i9xx_set_pipeconf(intel_crtc);
  4942. intel_crtc->active = true;
  4943. if (!IS_GEN2(dev_priv))
  4944. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4945. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4946. i9xx_enable_pll(intel_crtc);
  4947. i9xx_pfit_enable(intel_crtc);
  4948. intel_color_load_luts(&pipe_config->base);
  4949. if (dev_priv->display.initial_watermarks != NULL)
  4950. dev_priv->display.initial_watermarks(old_intel_state,
  4951. intel_crtc->config);
  4952. else
  4953. intel_update_watermarks(intel_crtc);
  4954. intel_enable_pipe(intel_crtc);
  4955. assert_vblank_disabled(crtc);
  4956. drm_crtc_vblank_on(crtc);
  4957. intel_encoders_enable(crtc, pipe_config, old_state);
  4958. }
  4959. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4960. {
  4961. struct drm_device *dev = crtc->base.dev;
  4962. struct drm_i915_private *dev_priv = to_i915(dev);
  4963. if (!crtc->config->gmch_pfit.control)
  4964. return;
  4965. assert_pipe_disabled(dev_priv, crtc->pipe);
  4966. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4967. I915_READ(PFIT_CONTROL));
  4968. I915_WRITE(PFIT_CONTROL, 0);
  4969. }
  4970. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4971. struct drm_atomic_state *old_state)
  4972. {
  4973. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4974. struct drm_device *dev = crtc->dev;
  4975. struct drm_i915_private *dev_priv = to_i915(dev);
  4976. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4977. int pipe = intel_crtc->pipe;
  4978. /*
  4979. * On gen2 planes are double buffered but the pipe isn't, so we must
  4980. * wait for planes to fully turn off before disabling the pipe.
  4981. */
  4982. if (IS_GEN2(dev_priv))
  4983. intel_wait_for_vblank(dev_priv, pipe);
  4984. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4985. drm_crtc_vblank_off(crtc);
  4986. assert_vblank_disabled(crtc);
  4987. intel_disable_pipe(intel_crtc);
  4988. i9xx_pfit_disable(intel_crtc);
  4989. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4990. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4991. if (IS_CHERRYVIEW(dev_priv))
  4992. chv_disable_pll(dev_priv, pipe);
  4993. else if (IS_VALLEYVIEW(dev_priv))
  4994. vlv_disable_pll(dev_priv, pipe);
  4995. else
  4996. i9xx_disable_pll(intel_crtc);
  4997. }
  4998. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4999. if (!IS_GEN2(dev_priv))
  5000. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5001. if (!dev_priv->display.initial_watermarks)
  5002. intel_update_watermarks(intel_crtc);
  5003. /* clock the pipe down to 640x480@60 to potentially save power */
  5004. if (IS_I830(dev_priv))
  5005. i830_enable_pipe(dev_priv, pipe);
  5006. }
  5007. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5008. struct drm_modeset_acquire_ctx *ctx)
  5009. {
  5010. struct intel_encoder *encoder;
  5011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5012. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5013. enum intel_display_power_domain domain;
  5014. u64 domains;
  5015. struct drm_atomic_state *state;
  5016. struct intel_crtc_state *crtc_state;
  5017. int ret;
  5018. if (!intel_crtc->active)
  5019. return;
  5020. if (crtc->primary->state->visible) {
  5021. intel_pre_disable_primary_noatomic(crtc);
  5022. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5023. crtc->primary->state->visible = false;
  5024. }
  5025. state = drm_atomic_state_alloc(crtc->dev);
  5026. if (!state) {
  5027. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5028. crtc->base.id, crtc->name);
  5029. return;
  5030. }
  5031. state->acquire_ctx = ctx;
  5032. /* Everything's already locked, -EDEADLK can't happen. */
  5033. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5034. ret = drm_atomic_add_affected_connectors(state, crtc);
  5035. WARN_ON(IS_ERR(crtc_state) || ret);
  5036. dev_priv->display.crtc_disable(crtc_state, state);
  5037. drm_atomic_state_put(state);
  5038. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5039. crtc->base.id, crtc->name);
  5040. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5041. crtc->state->active = false;
  5042. intel_crtc->active = false;
  5043. crtc->enabled = false;
  5044. crtc->state->connector_mask = 0;
  5045. crtc->state->encoder_mask = 0;
  5046. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5047. encoder->base.crtc = NULL;
  5048. intel_fbc_disable(intel_crtc);
  5049. intel_update_watermarks(intel_crtc);
  5050. intel_disable_shared_dpll(intel_crtc);
  5051. domains = intel_crtc->enabled_power_domains;
  5052. for_each_power_domain(domain, domains)
  5053. intel_display_power_put(dev_priv, domain);
  5054. intel_crtc->enabled_power_domains = 0;
  5055. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5056. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5057. }
  5058. /*
  5059. * turn all crtc's off, but do not adjust state
  5060. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5061. */
  5062. int intel_display_suspend(struct drm_device *dev)
  5063. {
  5064. struct drm_i915_private *dev_priv = to_i915(dev);
  5065. struct drm_atomic_state *state;
  5066. int ret;
  5067. state = drm_atomic_helper_suspend(dev);
  5068. ret = PTR_ERR_OR_ZERO(state);
  5069. if (ret)
  5070. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5071. else
  5072. dev_priv->modeset_restore_state = state;
  5073. return ret;
  5074. }
  5075. void intel_encoder_destroy(struct drm_encoder *encoder)
  5076. {
  5077. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5078. drm_encoder_cleanup(encoder);
  5079. kfree(intel_encoder);
  5080. }
  5081. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5082. * internal consistency). */
  5083. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5084. struct drm_connector_state *conn_state)
  5085. {
  5086. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5087. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5088. connector->base.base.id,
  5089. connector->base.name);
  5090. if (connector->get_hw_state(connector)) {
  5091. struct intel_encoder *encoder = connector->encoder;
  5092. I915_STATE_WARN(!crtc_state,
  5093. "connector enabled without attached crtc\n");
  5094. if (!crtc_state)
  5095. return;
  5096. I915_STATE_WARN(!crtc_state->active,
  5097. "connector is active, but attached crtc isn't\n");
  5098. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5099. return;
  5100. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5101. "atomic encoder doesn't match attached encoder\n");
  5102. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5103. "attached encoder crtc differs from connector crtc\n");
  5104. } else {
  5105. I915_STATE_WARN(crtc_state && crtc_state->active,
  5106. "attached crtc is active, but connector isn't\n");
  5107. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5108. "best encoder set without crtc!\n");
  5109. }
  5110. }
  5111. int intel_connector_init(struct intel_connector *connector)
  5112. {
  5113. struct intel_digital_connector_state *conn_state;
  5114. /*
  5115. * Allocate enough memory to hold intel_digital_connector_state,
  5116. * This might be a few bytes too many, but for connectors that don't
  5117. * need it we'll free the state and allocate a smaller one on the first
  5118. * succesful commit anyway.
  5119. */
  5120. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5121. if (!conn_state)
  5122. return -ENOMEM;
  5123. __drm_atomic_helper_connector_reset(&connector->base,
  5124. &conn_state->base);
  5125. return 0;
  5126. }
  5127. struct intel_connector *intel_connector_alloc(void)
  5128. {
  5129. struct intel_connector *connector;
  5130. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5131. if (!connector)
  5132. return NULL;
  5133. if (intel_connector_init(connector) < 0) {
  5134. kfree(connector);
  5135. return NULL;
  5136. }
  5137. return connector;
  5138. }
  5139. /* Simple connector->get_hw_state implementation for encoders that support only
  5140. * one connector and no cloning and hence the encoder state determines the state
  5141. * of the connector. */
  5142. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5143. {
  5144. enum pipe pipe = 0;
  5145. struct intel_encoder *encoder = connector->encoder;
  5146. return encoder->get_hw_state(encoder, &pipe);
  5147. }
  5148. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5149. {
  5150. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5151. return crtc_state->fdi_lanes;
  5152. return 0;
  5153. }
  5154. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5155. struct intel_crtc_state *pipe_config)
  5156. {
  5157. struct drm_i915_private *dev_priv = to_i915(dev);
  5158. struct drm_atomic_state *state = pipe_config->base.state;
  5159. struct intel_crtc *other_crtc;
  5160. struct intel_crtc_state *other_crtc_state;
  5161. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5162. pipe_name(pipe), pipe_config->fdi_lanes);
  5163. if (pipe_config->fdi_lanes > 4) {
  5164. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5165. pipe_name(pipe), pipe_config->fdi_lanes);
  5166. return -EINVAL;
  5167. }
  5168. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5169. if (pipe_config->fdi_lanes > 2) {
  5170. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5171. pipe_config->fdi_lanes);
  5172. return -EINVAL;
  5173. } else {
  5174. return 0;
  5175. }
  5176. }
  5177. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5178. return 0;
  5179. /* Ivybridge 3 pipe is really complicated */
  5180. switch (pipe) {
  5181. case PIPE_A:
  5182. return 0;
  5183. case PIPE_B:
  5184. if (pipe_config->fdi_lanes <= 2)
  5185. return 0;
  5186. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5187. other_crtc_state =
  5188. intel_atomic_get_crtc_state(state, other_crtc);
  5189. if (IS_ERR(other_crtc_state))
  5190. return PTR_ERR(other_crtc_state);
  5191. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5192. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5193. pipe_name(pipe), pipe_config->fdi_lanes);
  5194. return -EINVAL;
  5195. }
  5196. return 0;
  5197. case PIPE_C:
  5198. if (pipe_config->fdi_lanes > 2) {
  5199. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5200. pipe_name(pipe), pipe_config->fdi_lanes);
  5201. return -EINVAL;
  5202. }
  5203. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5204. other_crtc_state =
  5205. intel_atomic_get_crtc_state(state, other_crtc);
  5206. if (IS_ERR(other_crtc_state))
  5207. return PTR_ERR(other_crtc_state);
  5208. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5209. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5210. return -EINVAL;
  5211. }
  5212. return 0;
  5213. default:
  5214. BUG();
  5215. }
  5216. }
  5217. #define RETRY 1
  5218. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5219. struct intel_crtc_state *pipe_config)
  5220. {
  5221. struct drm_device *dev = intel_crtc->base.dev;
  5222. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5223. int lane, link_bw, fdi_dotclock, ret;
  5224. bool needs_recompute = false;
  5225. retry:
  5226. /* FDI is a binary signal running at ~2.7GHz, encoding
  5227. * each output octet as 10 bits. The actual frequency
  5228. * is stored as a divider into a 100MHz clock, and the
  5229. * mode pixel clock is stored in units of 1KHz.
  5230. * Hence the bw of each lane in terms of the mode signal
  5231. * is:
  5232. */
  5233. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5234. fdi_dotclock = adjusted_mode->crtc_clock;
  5235. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5236. pipe_config->pipe_bpp);
  5237. pipe_config->fdi_lanes = lane;
  5238. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5239. link_bw, &pipe_config->fdi_m_n, false);
  5240. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5241. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5242. pipe_config->pipe_bpp -= 2*3;
  5243. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5244. pipe_config->pipe_bpp);
  5245. needs_recompute = true;
  5246. pipe_config->bw_constrained = true;
  5247. goto retry;
  5248. }
  5249. if (needs_recompute)
  5250. return RETRY;
  5251. return ret;
  5252. }
  5253. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5254. struct intel_crtc_state *pipe_config)
  5255. {
  5256. if (pipe_config->pipe_bpp > 24)
  5257. return false;
  5258. /* HSW can handle pixel rate up to cdclk? */
  5259. if (IS_HASWELL(dev_priv))
  5260. return true;
  5261. /*
  5262. * We compare against max which means we must take
  5263. * the increased cdclk requirement into account when
  5264. * calculating the new cdclk.
  5265. *
  5266. * Should measure whether using a lower cdclk w/o IPS
  5267. */
  5268. return pipe_config->pixel_rate <=
  5269. dev_priv->max_cdclk_freq * 95 / 100;
  5270. }
  5271. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5272. struct intel_crtc_state *pipe_config)
  5273. {
  5274. struct drm_device *dev = crtc->base.dev;
  5275. struct drm_i915_private *dev_priv = to_i915(dev);
  5276. pipe_config->ips_enabled = i915.enable_ips &&
  5277. hsw_crtc_supports_ips(crtc) &&
  5278. pipe_config_supports_ips(dev_priv, pipe_config);
  5279. }
  5280. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5281. {
  5282. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5283. /* GDG double wide on either pipe, otherwise pipe A only */
  5284. return INTEL_INFO(dev_priv)->gen < 4 &&
  5285. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5286. }
  5287. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5288. {
  5289. uint32_t pixel_rate;
  5290. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5291. /*
  5292. * We only use IF-ID interlacing. If we ever use
  5293. * PF-ID we'll need to adjust the pixel_rate here.
  5294. */
  5295. if (pipe_config->pch_pfit.enabled) {
  5296. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5297. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5298. pipe_w = pipe_config->pipe_src_w;
  5299. pipe_h = pipe_config->pipe_src_h;
  5300. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5301. pfit_h = pfit_size & 0xFFFF;
  5302. if (pipe_w < pfit_w)
  5303. pipe_w = pfit_w;
  5304. if (pipe_h < pfit_h)
  5305. pipe_h = pfit_h;
  5306. if (WARN_ON(!pfit_w || !pfit_h))
  5307. return pixel_rate;
  5308. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5309. pfit_w * pfit_h);
  5310. }
  5311. return pixel_rate;
  5312. }
  5313. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5314. {
  5315. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5316. if (HAS_GMCH_DISPLAY(dev_priv))
  5317. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5318. crtc_state->pixel_rate =
  5319. crtc_state->base.adjusted_mode.crtc_clock;
  5320. else
  5321. crtc_state->pixel_rate =
  5322. ilk_pipe_pixel_rate(crtc_state);
  5323. }
  5324. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5325. struct intel_crtc_state *pipe_config)
  5326. {
  5327. struct drm_device *dev = crtc->base.dev;
  5328. struct drm_i915_private *dev_priv = to_i915(dev);
  5329. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5330. int clock_limit = dev_priv->max_dotclk_freq;
  5331. if (INTEL_GEN(dev_priv) < 4) {
  5332. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5333. /*
  5334. * Enable double wide mode when the dot clock
  5335. * is > 90% of the (display) core speed.
  5336. */
  5337. if (intel_crtc_supports_double_wide(crtc) &&
  5338. adjusted_mode->crtc_clock > clock_limit) {
  5339. clock_limit = dev_priv->max_dotclk_freq;
  5340. pipe_config->double_wide = true;
  5341. }
  5342. }
  5343. if (adjusted_mode->crtc_clock > clock_limit) {
  5344. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5345. adjusted_mode->crtc_clock, clock_limit,
  5346. yesno(pipe_config->double_wide));
  5347. return -EINVAL;
  5348. }
  5349. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5350. /*
  5351. * There is only one pipe CSC unit per pipe, and we need that
  5352. * for output conversion from RGB->YCBCR. So if CTM is already
  5353. * applied we can't support YCBCR420 output.
  5354. */
  5355. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5356. return -EINVAL;
  5357. }
  5358. /*
  5359. * Pipe horizontal size must be even in:
  5360. * - DVO ganged mode
  5361. * - LVDS dual channel mode
  5362. * - Double wide pipe
  5363. */
  5364. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5365. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5366. pipe_config->pipe_src_w &= ~1;
  5367. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5368. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5369. */
  5370. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5371. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5372. return -EINVAL;
  5373. intel_crtc_compute_pixel_rate(pipe_config);
  5374. if (HAS_IPS(dev_priv))
  5375. hsw_compute_ips_config(crtc, pipe_config);
  5376. if (pipe_config->has_pch_encoder)
  5377. return ironlake_fdi_compute_config(crtc, pipe_config);
  5378. return 0;
  5379. }
  5380. static void
  5381. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5382. {
  5383. while (*num > DATA_LINK_M_N_MASK ||
  5384. *den > DATA_LINK_M_N_MASK) {
  5385. *num >>= 1;
  5386. *den >>= 1;
  5387. }
  5388. }
  5389. static void compute_m_n(unsigned int m, unsigned int n,
  5390. uint32_t *ret_m, uint32_t *ret_n,
  5391. bool reduce_m_n)
  5392. {
  5393. /*
  5394. * Reduce M/N as much as possible without loss in precision. Several DP
  5395. * dongles in particular seem to be fussy about too large *link* M/N
  5396. * values. The passed in values are more likely to have the least
  5397. * significant bits zero than M after rounding below, so do this first.
  5398. */
  5399. if (reduce_m_n) {
  5400. while ((m & 1) == 0 && (n & 1) == 0) {
  5401. m >>= 1;
  5402. n >>= 1;
  5403. }
  5404. }
  5405. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5406. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5407. intel_reduce_m_n_ratio(ret_m, ret_n);
  5408. }
  5409. void
  5410. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5411. int pixel_clock, int link_clock,
  5412. struct intel_link_m_n *m_n,
  5413. bool reduce_m_n)
  5414. {
  5415. m_n->tu = 64;
  5416. compute_m_n(bits_per_pixel * pixel_clock,
  5417. link_clock * nlanes * 8,
  5418. &m_n->gmch_m, &m_n->gmch_n,
  5419. reduce_m_n);
  5420. compute_m_n(pixel_clock, link_clock,
  5421. &m_n->link_m, &m_n->link_n,
  5422. reduce_m_n);
  5423. }
  5424. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5425. {
  5426. if (i915.panel_use_ssc >= 0)
  5427. return i915.panel_use_ssc != 0;
  5428. return dev_priv->vbt.lvds_use_ssc
  5429. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5430. }
  5431. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5432. {
  5433. return (1 << dpll->n) << 16 | dpll->m2;
  5434. }
  5435. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5436. {
  5437. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5438. }
  5439. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5440. struct intel_crtc_state *crtc_state,
  5441. struct dpll *reduced_clock)
  5442. {
  5443. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5444. u32 fp, fp2 = 0;
  5445. if (IS_PINEVIEW(dev_priv)) {
  5446. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5447. if (reduced_clock)
  5448. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5449. } else {
  5450. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5451. if (reduced_clock)
  5452. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5453. }
  5454. crtc_state->dpll_hw_state.fp0 = fp;
  5455. crtc->lowfreq_avail = false;
  5456. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5457. reduced_clock) {
  5458. crtc_state->dpll_hw_state.fp1 = fp2;
  5459. crtc->lowfreq_avail = true;
  5460. } else {
  5461. crtc_state->dpll_hw_state.fp1 = fp;
  5462. }
  5463. }
  5464. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5465. pipe)
  5466. {
  5467. u32 reg_val;
  5468. /*
  5469. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5470. * and set it to a reasonable value instead.
  5471. */
  5472. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5473. reg_val &= 0xffffff00;
  5474. reg_val |= 0x00000030;
  5475. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5476. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5477. reg_val &= 0x00ffffff;
  5478. reg_val |= 0x8c000000;
  5479. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5480. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5481. reg_val &= 0xffffff00;
  5482. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5483. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5484. reg_val &= 0x00ffffff;
  5485. reg_val |= 0xb0000000;
  5486. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5487. }
  5488. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5489. struct intel_link_m_n *m_n)
  5490. {
  5491. struct drm_device *dev = crtc->base.dev;
  5492. struct drm_i915_private *dev_priv = to_i915(dev);
  5493. int pipe = crtc->pipe;
  5494. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5495. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5496. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5497. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5498. }
  5499. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5500. struct intel_link_m_n *m_n,
  5501. struct intel_link_m_n *m2_n2)
  5502. {
  5503. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5504. int pipe = crtc->pipe;
  5505. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5506. if (INTEL_GEN(dev_priv) >= 5) {
  5507. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5508. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5509. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5510. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5511. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5512. * for gen < 8) and if DRRS is supported (to make sure the
  5513. * registers are not unnecessarily accessed).
  5514. */
  5515. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5516. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5517. I915_WRITE(PIPE_DATA_M2(transcoder),
  5518. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5519. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5520. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5521. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5522. }
  5523. } else {
  5524. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5525. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5526. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5527. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5528. }
  5529. }
  5530. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5531. {
  5532. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5533. if (m_n == M1_N1) {
  5534. dp_m_n = &crtc->config->dp_m_n;
  5535. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5536. } else if (m_n == M2_N2) {
  5537. /*
  5538. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5539. * needs to be programmed into M1_N1.
  5540. */
  5541. dp_m_n = &crtc->config->dp_m2_n2;
  5542. } else {
  5543. DRM_ERROR("Unsupported divider value\n");
  5544. return;
  5545. }
  5546. if (crtc->config->has_pch_encoder)
  5547. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5548. else
  5549. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5550. }
  5551. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5552. struct intel_crtc_state *pipe_config)
  5553. {
  5554. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5555. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5556. if (crtc->pipe != PIPE_A)
  5557. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5558. /* DPLL not used with DSI, but still need the rest set up */
  5559. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5560. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5561. DPLL_EXT_BUFFER_ENABLE_VLV;
  5562. pipe_config->dpll_hw_state.dpll_md =
  5563. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5564. }
  5565. static void chv_compute_dpll(struct intel_crtc *crtc,
  5566. struct intel_crtc_state *pipe_config)
  5567. {
  5568. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5569. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5570. if (crtc->pipe != PIPE_A)
  5571. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5572. /* DPLL not used with DSI, but still need the rest set up */
  5573. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5574. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5575. pipe_config->dpll_hw_state.dpll_md =
  5576. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5577. }
  5578. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5579. const struct intel_crtc_state *pipe_config)
  5580. {
  5581. struct drm_device *dev = crtc->base.dev;
  5582. struct drm_i915_private *dev_priv = to_i915(dev);
  5583. enum pipe pipe = crtc->pipe;
  5584. u32 mdiv;
  5585. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5586. u32 coreclk, reg_val;
  5587. /* Enable Refclk */
  5588. I915_WRITE(DPLL(pipe),
  5589. pipe_config->dpll_hw_state.dpll &
  5590. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5591. /* No need to actually set up the DPLL with DSI */
  5592. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5593. return;
  5594. mutex_lock(&dev_priv->sb_lock);
  5595. bestn = pipe_config->dpll.n;
  5596. bestm1 = pipe_config->dpll.m1;
  5597. bestm2 = pipe_config->dpll.m2;
  5598. bestp1 = pipe_config->dpll.p1;
  5599. bestp2 = pipe_config->dpll.p2;
  5600. /* See eDP HDMI DPIO driver vbios notes doc */
  5601. /* PLL B needs special handling */
  5602. if (pipe == PIPE_B)
  5603. vlv_pllb_recal_opamp(dev_priv, pipe);
  5604. /* Set up Tx target for periodic Rcomp update */
  5605. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5606. /* Disable target IRef on PLL */
  5607. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5608. reg_val &= 0x00ffffff;
  5609. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5610. /* Disable fast lock */
  5611. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5612. /* Set idtafcrecal before PLL is enabled */
  5613. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5614. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5615. mdiv |= ((bestn << DPIO_N_SHIFT));
  5616. mdiv |= (1 << DPIO_K_SHIFT);
  5617. /*
  5618. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5619. * but we don't support that).
  5620. * Note: don't use the DAC post divider as it seems unstable.
  5621. */
  5622. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5623. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5624. mdiv |= DPIO_ENABLE_CALIBRATION;
  5625. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5626. /* Set HBR and RBR LPF coefficients */
  5627. if (pipe_config->port_clock == 162000 ||
  5628. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5629. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5630. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5631. 0x009f0003);
  5632. else
  5633. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5634. 0x00d0000f);
  5635. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5636. /* Use SSC source */
  5637. if (pipe == PIPE_A)
  5638. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5639. 0x0df40000);
  5640. else
  5641. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5642. 0x0df70000);
  5643. } else { /* HDMI or VGA */
  5644. /* Use bend source */
  5645. if (pipe == PIPE_A)
  5646. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5647. 0x0df70000);
  5648. else
  5649. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5650. 0x0df40000);
  5651. }
  5652. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5653. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5654. if (intel_crtc_has_dp_encoder(crtc->config))
  5655. coreclk |= 0x01000000;
  5656. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5657. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5658. mutex_unlock(&dev_priv->sb_lock);
  5659. }
  5660. static void chv_prepare_pll(struct intel_crtc *crtc,
  5661. const struct intel_crtc_state *pipe_config)
  5662. {
  5663. struct drm_device *dev = crtc->base.dev;
  5664. struct drm_i915_private *dev_priv = to_i915(dev);
  5665. enum pipe pipe = crtc->pipe;
  5666. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5667. u32 loopfilter, tribuf_calcntr;
  5668. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5669. u32 dpio_val;
  5670. int vco;
  5671. /* Enable Refclk and SSC */
  5672. I915_WRITE(DPLL(pipe),
  5673. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5674. /* No need to actually set up the DPLL with DSI */
  5675. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5676. return;
  5677. bestn = pipe_config->dpll.n;
  5678. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5679. bestm1 = pipe_config->dpll.m1;
  5680. bestm2 = pipe_config->dpll.m2 >> 22;
  5681. bestp1 = pipe_config->dpll.p1;
  5682. bestp2 = pipe_config->dpll.p2;
  5683. vco = pipe_config->dpll.vco;
  5684. dpio_val = 0;
  5685. loopfilter = 0;
  5686. mutex_lock(&dev_priv->sb_lock);
  5687. /* p1 and p2 divider */
  5688. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5689. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5690. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5691. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5692. 1 << DPIO_CHV_K_DIV_SHIFT);
  5693. /* Feedback post-divider - m2 */
  5694. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5695. /* Feedback refclk divider - n and m1 */
  5696. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5697. DPIO_CHV_M1_DIV_BY_2 |
  5698. 1 << DPIO_CHV_N_DIV_SHIFT);
  5699. /* M2 fraction division */
  5700. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5701. /* M2 fraction division enable */
  5702. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5703. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5704. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5705. if (bestm2_frac)
  5706. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5707. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5708. /* Program digital lock detect threshold */
  5709. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5710. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5711. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5712. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5713. if (!bestm2_frac)
  5714. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5715. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5716. /* Loop filter */
  5717. if (vco == 5400000) {
  5718. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5719. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5720. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5721. tribuf_calcntr = 0x9;
  5722. } else if (vco <= 6200000) {
  5723. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5724. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5725. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5726. tribuf_calcntr = 0x9;
  5727. } else if (vco <= 6480000) {
  5728. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5729. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5730. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5731. tribuf_calcntr = 0x8;
  5732. } else {
  5733. /* Not supported. Apply the same limits as in the max case */
  5734. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5735. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5736. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5737. tribuf_calcntr = 0;
  5738. }
  5739. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5740. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5741. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5742. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5743. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5744. /* AFC Recal */
  5745. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5746. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5747. DPIO_AFC_RECAL);
  5748. mutex_unlock(&dev_priv->sb_lock);
  5749. }
  5750. /**
  5751. * vlv_force_pll_on - forcibly enable just the PLL
  5752. * @dev_priv: i915 private structure
  5753. * @pipe: pipe PLL to enable
  5754. * @dpll: PLL configuration
  5755. *
  5756. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5757. * in cases where we need the PLL enabled even when @pipe is not going to
  5758. * be enabled.
  5759. */
  5760. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5761. const struct dpll *dpll)
  5762. {
  5763. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5764. struct intel_crtc_state *pipe_config;
  5765. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5766. if (!pipe_config)
  5767. return -ENOMEM;
  5768. pipe_config->base.crtc = &crtc->base;
  5769. pipe_config->pixel_multiplier = 1;
  5770. pipe_config->dpll = *dpll;
  5771. if (IS_CHERRYVIEW(dev_priv)) {
  5772. chv_compute_dpll(crtc, pipe_config);
  5773. chv_prepare_pll(crtc, pipe_config);
  5774. chv_enable_pll(crtc, pipe_config);
  5775. } else {
  5776. vlv_compute_dpll(crtc, pipe_config);
  5777. vlv_prepare_pll(crtc, pipe_config);
  5778. vlv_enable_pll(crtc, pipe_config);
  5779. }
  5780. kfree(pipe_config);
  5781. return 0;
  5782. }
  5783. /**
  5784. * vlv_force_pll_off - forcibly disable just the PLL
  5785. * @dev_priv: i915 private structure
  5786. * @pipe: pipe PLL to disable
  5787. *
  5788. * Disable the PLL for @pipe. To be used in cases where we need
  5789. * the PLL enabled even when @pipe is not going to be enabled.
  5790. */
  5791. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5792. {
  5793. if (IS_CHERRYVIEW(dev_priv))
  5794. chv_disable_pll(dev_priv, pipe);
  5795. else
  5796. vlv_disable_pll(dev_priv, pipe);
  5797. }
  5798. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5799. struct intel_crtc_state *crtc_state,
  5800. struct dpll *reduced_clock)
  5801. {
  5802. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5803. u32 dpll;
  5804. struct dpll *clock = &crtc_state->dpll;
  5805. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5806. dpll = DPLL_VGA_MODE_DIS;
  5807. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5808. dpll |= DPLLB_MODE_LVDS;
  5809. else
  5810. dpll |= DPLLB_MODE_DAC_SERIAL;
  5811. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5812. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5813. dpll |= (crtc_state->pixel_multiplier - 1)
  5814. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5815. }
  5816. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5817. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5818. dpll |= DPLL_SDVO_HIGH_SPEED;
  5819. if (intel_crtc_has_dp_encoder(crtc_state))
  5820. dpll |= DPLL_SDVO_HIGH_SPEED;
  5821. /* compute bitmask from p1 value */
  5822. if (IS_PINEVIEW(dev_priv))
  5823. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5824. else {
  5825. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5826. if (IS_G4X(dev_priv) && reduced_clock)
  5827. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5828. }
  5829. switch (clock->p2) {
  5830. case 5:
  5831. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5832. break;
  5833. case 7:
  5834. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5835. break;
  5836. case 10:
  5837. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5838. break;
  5839. case 14:
  5840. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5841. break;
  5842. }
  5843. if (INTEL_GEN(dev_priv) >= 4)
  5844. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5845. if (crtc_state->sdvo_tv_clock)
  5846. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5847. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5848. intel_panel_use_ssc(dev_priv))
  5849. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5850. else
  5851. dpll |= PLL_REF_INPUT_DREFCLK;
  5852. dpll |= DPLL_VCO_ENABLE;
  5853. crtc_state->dpll_hw_state.dpll = dpll;
  5854. if (INTEL_GEN(dev_priv) >= 4) {
  5855. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5856. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5857. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5858. }
  5859. }
  5860. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5861. struct intel_crtc_state *crtc_state,
  5862. struct dpll *reduced_clock)
  5863. {
  5864. struct drm_device *dev = crtc->base.dev;
  5865. struct drm_i915_private *dev_priv = to_i915(dev);
  5866. u32 dpll;
  5867. struct dpll *clock = &crtc_state->dpll;
  5868. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5869. dpll = DPLL_VGA_MODE_DIS;
  5870. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5871. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5872. } else {
  5873. if (clock->p1 == 2)
  5874. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5875. else
  5876. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5877. if (clock->p2 == 4)
  5878. dpll |= PLL_P2_DIVIDE_BY_4;
  5879. }
  5880. if (!IS_I830(dev_priv) &&
  5881. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5882. dpll |= DPLL_DVO_2X_MODE;
  5883. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5884. intel_panel_use_ssc(dev_priv))
  5885. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5886. else
  5887. dpll |= PLL_REF_INPUT_DREFCLK;
  5888. dpll |= DPLL_VCO_ENABLE;
  5889. crtc_state->dpll_hw_state.dpll = dpll;
  5890. }
  5891. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5892. {
  5893. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5894. enum pipe pipe = intel_crtc->pipe;
  5895. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5896. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5897. uint32_t crtc_vtotal, crtc_vblank_end;
  5898. int vsyncshift = 0;
  5899. /* We need to be careful not to changed the adjusted mode, for otherwise
  5900. * the hw state checker will get angry at the mismatch. */
  5901. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5902. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5903. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5904. /* the chip adds 2 halflines automatically */
  5905. crtc_vtotal -= 1;
  5906. crtc_vblank_end -= 1;
  5907. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5908. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5909. else
  5910. vsyncshift = adjusted_mode->crtc_hsync_start -
  5911. adjusted_mode->crtc_htotal / 2;
  5912. if (vsyncshift < 0)
  5913. vsyncshift += adjusted_mode->crtc_htotal;
  5914. }
  5915. if (INTEL_GEN(dev_priv) > 3)
  5916. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5917. I915_WRITE(HTOTAL(cpu_transcoder),
  5918. (adjusted_mode->crtc_hdisplay - 1) |
  5919. ((adjusted_mode->crtc_htotal - 1) << 16));
  5920. I915_WRITE(HBLANK(cpu_transcoder),
  5921. (adjusted_mode->crtc_hblank_start - 1) |
  5922. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5923. I915_WRITE(HSYNC(cpu_transcoder),
  5924. (adjusted_mode->crtc_hsync_start - 1) |
  5925. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5926. I915_WRITE(VTOTAL(cpu_transcoder),
  5927. (adjusted_mode->crtc_vdisplay - 1) |
  5928. ((crtc_vtotal - 1) << 16));
  5929. I915_WRITE(VBLANK(cpu_transcoder),
  5930. (adjusted_mode->crtc_vblank_start - 1) |
  5931. ((crtc_vblank_end - 1) << 16));
  5932. I915_WRITE(VSYNC(cpu_transcoder),
  5933. (adjusted_mode->crtc_vsync_start - 1) |
  5934. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5935. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5936. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5937. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5938. * bits. */
  5939. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5940. (pipe == PIPE_B || pipe == PIPE_C))
  5941. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5942. }
  5943. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5944. {
  5945. struct drm_device *dev = intel_crtc->base.dev;
  5946. struct drm_i915_private *dev_priv = to_i915(dev);
  5947. enum pipe pipe = intel_crtc->pipe;
  5948. /* pipesrc controls the size that is scaled from, which should
  5949. * always be the user's requested size.
  5950. */
  5951. I915_WRITE(PIPESRC(pipe),
  5952. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5953. (intel_crtc->config->pipe_src_h - 1));
  5954. }
  5955. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5956. struct intel_crtc_state *pipe_config)
  5957. {
  5958. struct drm_device *dev = crtc->base.dev;
  5959. struct drm_i915_private *dev_priv = to_i915(dev);
  5960. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5961. uint32_t tmp;
  5962. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5963. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5964. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5965. tmp = I915_READ(HBLANK(cpu_transcoder));
  5966. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5967. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5968. tmp = I915_READ(HSYNC(cpu_transcoder));
  5969. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5970. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5971. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5972. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5973. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5974. tmp = I915_READ(VBLANK(cpu_transcoder));
  5975. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5976. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5977. tmp = I915_READ(VSYNC(cpu_transcoder));
  5978. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5979. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5980. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5981. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5982. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5983. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5984. }
  5985. }
  5986. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5987. struct intel_crtc_state *pipe_config)
  5988. {
  5989. struct drm_device *dev = crtc->base.dev;
  5990. struct drm_i915_private *dev_priv = to_i915(dev);
  5991. u32 tmp;
  5992. tmp = I915_READ(PIPESRC(crtc->pipe));
  5993. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5994. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5995. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5996. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5997. }
  5998. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5999. struct intel_crtc_state *pipe_config)
  6000. {
  6001. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6002. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6003. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6004. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6005. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6006. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6007. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6008. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6009. mode->flags = pipe_config->base.adjusted_mode.flags;
  6010. mode->type = DRM_MODE_TYPE_DRIVER;
  6011. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6012. mode->hsync = drm_mode_hsync(mode);
  6013. mode->vrefresh = drm_mode_vrefresh(mode);
  6014. drm_mode_set_name(mode);
  6015. }
  6016. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6017. {
  6018. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6019. uint32_t pipeconf;
  6020. pipeconf = 0;
  6021. /* we keep both pipes enabled on 830 */
  6022. if (IS_I830(dev_priv))
  6023. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6024. if (intel_crtc->config->double_wide)
  6025. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6026. /* only g4x and later have fancy bpc/dither controls */
  6027. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6028. IS_CHERRYVIEW(dev_priv)) {
  6029. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6030. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6031. pipeconf |= PIPECONF_DITHER_EN |
  6032. PIPECONF_DITHER_TYPE_SP;
  6033. switch (intel_crtc->config->pipe_bpp) {
  6034. case 18:
  6035. pipeconf |= PIPECONF_6BPC;
  6036. break;
  6037. case 24:
  6038. pipeconf |= PIPECONF_8BPC;
  6039. break;
  6040. case 30:
  6041. pipeconf |= PIPECONF_10BPC;
  6042. break;
  6043. default:
  6044. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6045. BUG();
  6046. }
  6047. }
  6048. if (HAS_PIPE_CXSR(dev_priv)) {
  6049. if (intel_crtc->lowfreq_avail) {
  6050. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6051. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6052. } else {
  6053. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6054. }
  6055. }
  6056. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6057. if (INTEL_GEN(dev_priv) < 4 ||
  6058. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6059. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6060. else
  6061. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6062. } else
  6063. pipeconf |= PIPECONF_PROGRESSIVE;
  6064. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6065. intel_crtc->config->limited_color_range)
  6066. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6067. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6068. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6069. }
  6070. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6071. struct intel_crtc_state *crtc_state)
  6072. {
  6073. struct drm_device *dev = crtc->base.dev;
  6074. struct drm_i915_private *dev_priv = to_i915(dev);
  6075. const struct intel_limit *limit;
  6076. int refclk = 48000;
  6077. memset(&crtc_state->dpll_hw_state, 0,
  6078. sizeof(crtc_state->dpll_hw_state));
  6079. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6080. if (intel_panel_use_ssc(dev_priv)) {
  6081. refclk = dev_priv->vbt.lvds_ssc_freq;
  6082. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6083. }
  6084. limit = &intel_limits_i8xx_lvds;
  6085. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6086. limit = &intel_limits_i8xx_dvo;
  6087. } else {
  6088. limit = &intel_limits_i8xx_dac;
  6089. }
  6090. if (!crtc_state->clock_set &&
  6091. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6092. refclk, NULL, &crtc_state->dpll)) {
  6093. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6094. return -EINVAL;
  6095. }
  6096. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6097. return 0;
  6098. }
  6099. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6100. struct intel_crtc_state *crtc_state)
  6101. {
  6102. struct drm_device *dev = crtc->base.dev;
  6103. struct drm_i915_private *dev_priv = to_i915(dev);
  6104. const struct intel_limit *limit;
  6105. int refclk = 96000;
  6106. memset(&crtc_state->dpll_hw_state, 0,
  6107. sizeof(crtc_state->dpll_hw_state));
  6108. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6109. if (intel_panel_use_ssc(dev_priv)) {
  6110. refclk = dev_priv->vbt.lvds_ssc_freq;
  6111. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6112. }
  6113. if (intel_is_dual_link_lvds(dev))
  6114. limit = &intel_limits_g4x_dual_channel_lvds;
  6115. else
  6116. limit = &intel_limits_g4x_single_channel_lvds;
  6117. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6118. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6119. limit = &intel_limits_g4x_hdmi;
  6120. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6121. limit = &intel_limits_g4x_sdvo;
  6122. } else {
  6123. /* The option is for other outputs */
  6124. limit = &intel_limits_i9xx_sdvo;
  6125. }
  6126. if (!crtc_state->clock_set &&
  6127. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6128. refclk, NULL, &crtc_state->dpll)) {
  6129. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6130. return -EINVAL;
  6131. }
  6132. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6133. return 0;
  6134. }
  6135. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6136. struct intel_crtc_state *crtc_state)
  6137. {
  6138. struct drm_device *dev = crtc->base.dev;
  6139. struct drm_i915_private *dev_priv = to_i915(dev);
  6140. const struct intel_limit *limit;
  6141. int refclk = 96000;
  6142. memset(&crtc_state->dpll_hw_state, 0,
  6143. sizeof(crtc_state->dpll_hw_state));
  6144. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6145. if (intel_panel_use_ssc(dev_priv)) {
  6146. refclk = dev_priv->vbt.lvds_ssc_freq;
  6147. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6148. }
  6149. limit = &intel_limits_pineview_lvds;
  6150. } else {
  6151. limit = &intel_limits_pineview_sdvo;
  6152. }
  6153. if (!crtc_state->clock_set &&
  6154. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6155. refclk, NULL, &crtc_state->dpll)) {
  6156. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6157. return -EINVAL;
  6158. }
  6159. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6160. return 0;
  6161. }
  6162. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6163. struct intel_crtc_state *crtc_state)
  6164. {
  6165. struct drm_device *dev = crtc->base.dev;
  6166. struct drm_i915_private *dev_priv = to_i915(dev);
  6167. const struct intel_limit *limit;
  6168. int refclk = 96000;
  6169. memset(&crtc_state->dpll_hw_state, 0,
  6170. sizeof(crtc_state->dpll_hw_state));
  6171. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6172. if (intel_panel_use_ssc(dev_priv)) {
  6173. refclk = dev_priv->vbt.lvds_ssc_freq;
  6174. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6175. }
  6176. limit = &intel_limits_i9xx_lvds;
  6177. } else {
  6178. limit = &intel_limits_i9xx_sdvo;
  6179. }
  6180. if (!crtc_state->clock_set &&
  6181. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6182. refclk, NULL, &crtc_state->dpll)) {
  6183. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6184. return -EINVAL;
  6185. }
  6186. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6187. return 0;
  6188. }
  6189. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6190. struct intel_crtc_state *crtc_state)
  6191. {
  6192. int refclk = 100000;
  6193. const struct intel_limit *limit = &intel_limits_chv;
  6194. memset(&crtc_state->dpll_hw_state, 0,
  6195. sizeof(crtc_state->dpll_hw_state));
  6196. if (!crtc_state->clock_set &&
  6197. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6198. refclk, NULL, &crtc_state->dpll)) {
  6199. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6200. return -EINVAL;
  6201. }
  6202. chv_compute_dpll(crtc, crtc_state);
  6203. return 0;
  6204. }
  6205. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6206. struct intel_crtc_state *crtc_state)
  6207. {
  6208. int refclk = 100000;
  6209. const struct intel_limit *limit = &intel_limits_vlv;
  6210. memset(&crtc_state->dpll_hw_state, 0,
  6211. sizeof(crtc_state->dpll_hw_state));
  6212. if (!crtc_state->clock_set &&
  6213. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6214. refclk, NULL, &crtc_state->dpll)) {
  6215. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6216. return -EINVAL;
  6217. }
  6218. vlv_compute_dpll(crtc, crtc_state);
  6219. return 0;
  6220. }
  6221. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6222. struct intel_crtc_state *pipe_config)
  6223. {
  6224. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6225. uint32_t tmp;
  6226. if (INTEL_GEN(dev_priv) <= 3 &&
  6227. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6228. return;
  6229. tmp = I915_READ(PFIT_CONTROL);
  6230. if (!(tmp & PFIT_ENABLE))
  6231. return;
  6232. /* Check whether the pfit is attached to our pipe. */
  6233. if (INTEL_GEN(dev_priv) < 4) {
  6234. if (crtc->pipe != PIPE_B)
  6235. return;
  6236. } else {
  6237. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6238. return;
  6239. }
  6240. pipe_config->gmch_pfit.control = tmp;
  6241. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6242. }
  6243. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6244. struct intel_crtc_state *pipe_config)
  6245. {
  6246. struct drm_device *dev = crtc->base.dev;
  6247. struct drm_i915_private *dev_priv = to_i915(dev);
  6248. int pipe = pipe_config->cpu_transcoder;
  6249. struct dpll clock;
  6250. u32 mdiv;
  6251. int refclk = 100000;
  6252. /* In case of DSI, DPLL will not be used */
  6253. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6254. return;
  6255. mutex_lock(&dev_priv->sb_lock);
  6256. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6257. mutex_unlock(&dev_priv->sb_lock);
  6258. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6259. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6260. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6261. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6262. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6263. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6264. }
  6265. static void
  6266. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6267. struct intel_initial_plane_config *plane_config)
  6268. {
  6269. struct drm_device *dev = crtc->base.dev;
  6270. struct drm_i915_private *dev_priv = to_i915(dev);
  6271. u32 val, base, offset;
  6272. int pipe = crtc->pipe, plane = crtc->plane;
  6273. int fourcc, pixel_format;
  6274. unsigned int aligned_height;
  6275. struct drm_framebuffer *fb;
  6276. struct intel_framebuffer *intel_fb;
  6277. val = I915_READ(DSPCNTR(plane));
  6278. if (!(val & DISPLAY_PLANE_ENABLE))
  6279. return;
  6280. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6281. if (!intel_fb) {
  6282. DRM_DEBUG_KMS("failed to alloc fb\n");
  6283. return;
  6284. }
  6285. fb = &intel_fb->base;
  6286. fb->dev = dev;
  6287. if (INTEL_GEN(dev_priv) >= 4) {
  6288. if (val & DISPPLANE_TILED) {
  6289. plane_config->tiling = I915_TILING_X;
  6290. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6291. }
  6292. }
  6293. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6294. fourcc = i9xx_format_to_fourcc(pixel_format);
  6295. fb->format = drm_format_info(fourcc);
  6296. if (INTEL_GEN(dev_priv) >= 4) {
  6297. if (plane_config->tiling)
  6298. offset = I915_READ(DSPTILEOFF(plane));
  6299. else
  6300. offset = I915_READ(DSPLINOFF(plane));
  6301. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6302. } else {
  6303. base = I915_READ(DSPADDR(plane));
  6304. }
  6305. plane_config->base = base;
  6306. val = I915_READ(PIPESRC(pipe));
  6307. fb->width = ((val >> 16) & 0xfff) + 1;
  6308. fb->height = ((val >> 0) & 0xfff) + 1;
  6309. val = I915_READ(DSPSTRIDE(pipe));
  6310. fb->pitches[0] = val & 0xffffffc0;
  6311. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6312. plane_config->size = fb->pitches[0] * aligned_height;
  6313. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6314. pipe_name(pipe), plane, fb->width, fb->height,
  6315. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6316. plane_config->size);
  6317. plane_config->fb = intel_fb;
  6318. }
  6319. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6320. struct intel_crtc_state *pipe_config)
  6321. {
  6322. struct drm_device *dev = crtc->base.dev;
  6323. struct drm_i915_private *dev_priv = to_i915(dev);
  6324. int pipe = pipe_config->cpu_transcoder;
  6325. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6326. struct dpll clock;
  6327. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6328. int refclk = 100000;
  6329. /* In case of DSI, DPLL will not be used */
  6330. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6331. return;
  6332. mutex_lock(&dev_priv->sb_lock);
  6333. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6334. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6335. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6336. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6337. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6338. mutex_unlock(&dev_priv->sb_lock);
  6339. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6340. clock.m2 = (pll_dw0 & 0xff) << 22;
  6341. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6342. clock.m2 |= pll_dw2 & 0x3fffff;
  6343. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6344. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6345. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6346. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6347. }
  6348. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6349. struct intel_crtc_state *pipe_config)
  6350. {
  6351. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6352. enum intel_display_power_domain power_domain;
  6353. uint32_t tmp;
  6354. bool ret;
  6355. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6356. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6357. return false;
  6358. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6359. pipe_config->shared_dpll = NULL;
  6360. ret = false;
  6361. tmp = I915_READ(PIPECONF(crtc->pipe));
  6362. if (!(tmp & PIPECONF_ENABLE))
  6363. goto out;
  6364. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6365. IS_CHERRYVIEW(dev_priv)) {
  6366. switch (tmp & PIPECONF_BPC_MASK) {
  6367. case PIPECONF_6BPC:
  6368. pipe_config->pipe_bpp = 18;
  6369. break;
  6370. case PIPECONF_8BPC:
  6371. pipe_config->pipe_bpp = 24;
  6372. break;
  6373. case PIPECONF_10BPC:
  6374. pipe_config->pipe_bpp = 30;
  6375. break;
  6376. default:
  6377. break;
  6378. }
  6379. }
  6380. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6381. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6382. pipe_config->limited_color_range = true;
  6383. if (INTEL_GEN(dev_priv) < 4)
  6384. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6385. intel_get_pipe_timings(crtc, pipe_config);
  6386. intel_get_pipe_src_size(crtc, pipe_config);
  6387. i9xx_get_pfit_config(crtc, pipe_config);
  6388. if (INTEL_GEN(dev_priv) >= 4) {
  6389. /* No way to read it out on pipes B and C */
  6390. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6391. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6392. else
  6393. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6394. pipe_config->pixel_multiplier =
  6395. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6396. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6397. pipe_config->dpll_hw_state.dpll_md = tmp;
  6398. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6399. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6400. tmp = I915_READ(DPLL(crtc->pipe));
  6401. pipe_config->pixel_multiplier =
  6402. ((tmp & SDVO_MULTIPLIER_MASK)
  6403. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6404. } else {
  6405. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6406. * port and will be fixed up in the encoder->get_config
  6407. * function. */
  6408. pipe_config->pixel_multiplier = 1;
  6409. }
  6410. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6411. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6412. /*
  6413. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6414. * on 830. Filter it out here so that we don't
  6415. * report errors due to that.
  6416. */
  6417. if (IS_I830(dev_priv))
  6418. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6419. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6420. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6421. } else {
  6422. /* Mask out read-only status bits. */
  6423. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6424. DPLL_PORTC_READY_MASK |
  6425. DPLL_PORTB_READY_MASK);
  6426. }
  6427. if (IS_CHERRYVIEW(dev_priv))
  6428. chv_crtc_clock_get(crtc, pipe_config);
  6429. else if (IS_VALLEYVIEW(dev_priv))
  6430. vlv_crtc_clock_get(crtc, pipe_config);
  6431. else
  6432. i9xx_crtc_clock_get(crtc, pipe_config);
  6433. /*
  6434. * Normally the dotclock is filled in by the encoder .get_config()
  6435. * but in case the pipe is enabled w/o any ports we need a sane
  6436. * default.
  6437. */
  6438. pipe_config->base.adjusted_mode.crtc_clock =
  6439. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6440. ret = true;
  6441. out:
  6442. intel_display_power_put(dev_priv, power_domain);
  6443. return ret;
  6444. }
  6445. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6446. {
  6447. struct intel_encoder *encoder;
  6448. int i;
  6449. u32 val, final;
  6450. bool has_lvds = false;
  6451. bool has_cpu_edp = false;
  6452. bool has_panel = false;
  6453. bool has_ck505 = false;
  6454. bool can_ssc = false;
  6455. bool using_ssc_source = false;
  6456. /* We need to take the global config into account */
  6457. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6458. switch (encoder->type) {
  6459. case INTEL_OUTPUT_LVDS:
  6460. has_panel = true;
  6461. has_lvds = true;
  6462. break;
  6463. case INTEL_OUTPUT_EDP:
  6464. has_panel = true;
  6465. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6466. has_cpu_edp = true;
  6467. break;
  6468. default:
  6469. break;
  6470. }
  6471. }
  6472. if (HAS_PCH_IBX(dev_priv)) {
  6473. has_ck505 = dev_priv->vbt.display_clock_mode;
  6474. can_ssc = has_ck505;
  6475. } else {
  6476. has_ck505 = false;
  6477. can_ssc = true;
  6478. }
  6479. /* Check if any DPLLs are using the SSC source */
  6480. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6481. u32 temp = I915_READ(PCH_DPLL(i));
  6482. if (!(temp & DPLL_VCO_ENABLE))
  6483. continue;
  6484. if ((temp & PLL_REF_INPUT_MASK) ==
  6485. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6486. using_ssc_source = true;
  6487. break;
  6488. }
  6489. }
  6490. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6491. has_panel, has_lvds, has_ck505, using_ssc_source);
  6492. /* Ironlake: try to setup display ref clock before DPLL
  6493. * enabling. This is only under driver's control after
  6494. * PCH B stepping, previous chipset stepping should be
  6495. * ignoring this setting.
  6496. */
  6497. val = I915_READ(PCH_DREF_CONTROL);
  6498. /* As we must carefully and slowly disable/enable each source in turn,
  6499. * compute the final state we want first and check if we need to
  6500. * make any changes at all.
  6501. */
  6502. final = val;
  6503. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6504. if (has_ck505)
  6505. final |= DREF_NONSPREAD_CK505_ENABLE;
  6506. else
  6507. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6508. final &= ~DREF_SSC_SOURCE_MASK;
  6509. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6510. final &= ~DREF_SSC1_ENABLE;
  6511. if (has_panel) {
  6512. final |= DREF_SSC_SOURCE_ENABLE;
  6513. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6514. final |= DREF_SSC1_ENABLE;
  6515. if (has_cpu_edp) {
  6516. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6517. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6518. else
  6519. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6520. } else
  6521. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6522. } else if (using_ssc_source) {
  6523. final |= DREF_SSC_SOURCE_ENABLE;
  6524. final |= DREF_SSC1_ENABLE;
  6525. }
  6526. if (final == val)
  6527. return;
  6528. /* Always enable nonspread source */
  6529. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6530. if (has_ck505)
  6531. val |= DREF_NONSPREAD_CK505_ENABLE;
  6532. else
  6533. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6534. if (has_panel) {
  6535. val &= ~DREF_SSC_SOURCE_MASK;
  6536. val |= DREF_SSC_SOURCE_ENABLE;
  6537. /* SSC must be turned on before enabling the CPU output */
  6538. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6539. DRM_DEBUG_KMS("Using SSC on panel\n");
  6540. val |= DREF_SSC1_ENABLE;
  6541. } else
  6542. val &= ~DREF_SSC1_ENABLE;
  6543. /* Get SSC going before enabling the outputs */
  6544. I915_WRITE(PCH_DREF_CONTROL, val);
  6545. POSTING_READ(PCH_DREF_CONTROL);
  6546. udelay(200);
  6547. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6548. /* Enable CPU source on CPU attached eDP */
  6549. if (has_cpu_edp) {
  6550. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6551. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6552. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6553. } else
  6554. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6555. } else
  6556. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6557. I915_WRITE(PCH_DREF_CONTROL, val);
  6558. POSTING_READ(PCH_DREF_CONTROL);
  6559. udelay(200);
  6560. } else {
  6561. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6562. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6563. /* Turn off CPU output */
  6564. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6565. I915_WRITE(PCH_DREF_CONTROL, val);
  6566. POSTING_READ(PCH_DREF_CONTROL);
  6567. udelay(200);
  6568. if (!using_ssc_source) {
  6569. DRM_DEBUG_KMS("Disabling SSC source\n");
  6570. /* Turn off the SSC source */
  6571. val &= ~DREF_SSC_SOURCE_MASK;
  6572. val |= DREF_SSC_SOURCE_DISABLE;
  6573. /* Turn off SSC1 */
  6574. val &= ~DREF_SSC1_ENABLE;
  6575. I915_WRITE(PCH_DREF_CONTROL, val);
  6576. POSTING_READ(PCH_DREF_CONTROL);
  6577. udelay(200);
  6578. }
  6579. }
  6580. BUG_ON(val != final);
  6581. }
  6582. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6583. {
  6584. uint32_t tmp;
  6585. tmp = I915_READ(SOUTH_CHICKEN2);
  6586. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6587. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6588. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6589. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6590. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6591. tmp = I915_READ(SOUTH_CHICKEN2);
  6592. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6593. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6594. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6595. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6596. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6597. }
  6598. /* WaMPhyProgramming:hsw */
  6599. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6600. {
  6601. uint32_t tmp;
  6602. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6603. tmp &= ~(0xFF << 24);
  6604. tmp |= (0x12 << 24);
  6605. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6606. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6607. tmp |= (1 << 11);
  6608. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6609. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6610. tmp |= (1 << 11);
  6611. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6612. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6613. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6614. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6615. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6616. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6617. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6618. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6619. tmp &= ~(7 << 13);
  6620. tmp |= (5 << 13);
  6621. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6622. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6623. tmp &= ~(7 << 13);
  6624. tmp |= (5 << 13);
  6625. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6626. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6627. tmp &= ~0xFF;
  6628. tmp |= 0x1C;
  6629. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6630. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6631. tmp &= ~0xFF;
  6632. tmp |= 0x1C;
  6633. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6634. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6635. tmp &= ~(0xFF << 16);
  6636. tmp |= (0x1C << 16);
  6637. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6638. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6639. tmp &= ~(0xFF << 16);
  6640. tmp |= (0x1C << 16);
  6641. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6642. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6643. tmp |= (1 << 27);
  6644. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6645. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6646. tmp |= (1 << 27);
  6647. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6648. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6649. tmp &= ~(0xF << 28);
  6650. tmp |= (4 << 28);
  6651. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6652. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6653. tmp &= ~(0xF << 28);
  6654. tmp |= (4 << 28);
  6655. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6656. }
  6657. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6658. * Programming" based on the parameters passed:
  6659. * - Sequence to enable CLKOUT_DP
  6660. * - Sequence to enable CLKOUT_DP without spread
  6661. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6662. */
  6663. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6664. bool with_spread, bool with_fdi)
  6665. {
  6666. uint32_t reg, tmp;
  6667. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6668. with_spread = true;
  6669. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6670. with_fdi, "LP PCH doesn't have FDI\n"))
  6671. with_fdi = false;
  6672. mutex_lock(&dev_priv->sb_lock);
  6673. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6674. tmp &= ~SBI_SSCCTL_DISABLE;
  6675. tmp |= SBI_SSCCTL_PATHALT;
  6676. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6677. udelay(24);
  6678. if (with_spread) {
  6679. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6680. tmp &= ~SBI_SSCCTL_PATHALT;
  6681. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6682. if (with_fdi) {
  6683. lpt_reset_fdi_mphy(dev_priv);
  6684. lpt_program_fdi_mphy(dev_priv);
  6685. }
  6686. }
  6687. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6688. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6689. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6690. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6691. mutex_unlock(&dev_priv->sb_lock);
  6692. }
  6693. /* Sequence to disable CLKOUT_DP */
  6694. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6695. {
  6696. uint32_t reg, tmp;
  6697. mutex_lock(&dev_priv->sb_lock);
  6698. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6699. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6700. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6701. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6702. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6703. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6704. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6705. tmp |= SBI_SSCCTL_PATHALT;
  6706. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6707. udelay(32);
  6708. }
  6709. tmp |= SBI_SSCCTL_DISABLE;
  6710. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6711. }
  6712. mutex_unlock(&dev_priv->sb_lock);
  6713. }
  6714. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6715. static const uint16_t sscdivintphase[] = {
  6716. [BEND_IDX( 50)] = 0x3B23,
  6717. [BEND_IDX( 45)] = 0x3B23,
  6718. [BEND_IDX( 40)] = 0x3C23,
  6719. [BEND_IDX( 35)] = 0x3C23,
  6720. [BEND_IDX( 30)] = 0x3D23,
  6721. [BEND_IDX( 25)] = 0x3D23,
  6722. [BEND_IDX( 20)] = 0x3E23,
  6723. [BEND_IDX( 15)] = 0x3E23,
  6724. [BEND_IDX( 10)] = 0x3F23,
  6725. [BEND_IDX( 5)] = 0x3F23,
  6726. [BEND_IDX( 0)] = 0x0025,
  6727. [BEND_IDX( -5)] = 0x0025,
  6728. [BEND_IDX(-10)] = 0x0125,
  6729. [BEND_IDX(-15)] = 0x0125,
  6730. [BEND_IDX(-20)] = 0x0225,
  6731. [BEND_IDX(-25)] = 0x0225,
  6732. [BEND_IDX(-30)] = 0x0325,
  6733. [BEND_IDX(-35)] = 0x0325,
  6734. [BEND_IDX(-40)] = 0x0425,
  6735. [BEND_IDX(-45)] = 0x0425,
  6736. [BEND_IDX(-50)] = 0x0525,
  6737. };
  6738. /*
  6739. * Bend CLKOUT_DP
  6740. * steps -50 to 50 inclusive, in steps of 5
  6741. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6742. * change in clock period = -(steps / 10) * 5.787 ps
  6743. */
  6744. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6745. {
  6746. uint32_t tmp;
  6747. int idx = BEND_IDX(steps);
  6748. if (WARN_ON(steps % 5 != 0))
  6749. return;
  6750. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6751. return;
  6752. mutex_lock(&dev_priv->sb_lock);
  6753. if (steps % 10 != 0)
  6754. tmp = 0xAAAAAAAB;
  6755. else
  6756. tmp = 0x00000000;
  6757. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6758. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6759. tmp &= 0xffff0000;
  6760. tmp |= sscdivintphase[idx];
  6761. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6762. mutex_unlock(&dev_priv->sb_lock);
  6763. }
  6764. #undef BEND_IDX
  6765. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6766. {
  6767. struct intel_encoder *encoder;
  6768. bool has_vga = false;
  6769. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6770. switch (encoder->type) {
  6771. case INTEL_OUTPUT_ANALOG:
  6772. has_vga = true;
  6773. break;
  6774. default:
  6775. break;
  6776. }
  6777. }
  6778. if (has_vga) {
  6779. lpt_bend_clkout_dp(dev_priv, 0);
  6780. lpt_enable_clkout_dp(dev_priv, true, true);
  6781. } else {
  6782. lpt_disable_clkout_dp(dev_priv);
  6783. }
  6784. }
  6785. /*
  6786. * Initialize reference clocks when the driver loads
  6787. */
  6788. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6789. {
  6790. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6791. ironlake_init_pch_refclk(dev_priv);
  6792. else if (HAS_PCH_LPT(dev_priv))
  6793. lpt_init_pch_refclk(dev_priv);
  6794. }
  6795. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6796. {
  6797. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6798. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6799. int pipe = intel_crtc->pipe;
  6800. uint32_t val;
  6801. val = 0;
  6802. switch (intel_crtc->config->pipe_bpp) {
  6803. case 18:
  6804. val |= PIPECONF_6BPC;
  6805. break;
  6806. case 24:
  6807. val |= PIPECONF_8BPC;
  6808. break;
  6809. case 30:
  6810. val |= PIPECONF_10BPC;
  6811. break;
  6812. case 36:
  6813. val |= PIPECONF_12BPC;
  6814. break;
  6815. default:
  6816. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6817. BUG();
  6818. }
  6819. if (intel_crtc->config->dither)
  6820. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6821. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6822. val |= PIPECONF_INTERLACED_ILK;
  6823. else
  6824. val |= PIPECONF_PROGRESSIVE;
  6825. if (intel_crtc->config->limited_color_range)
  6826. val |= PIPECONF_COLOR_RANGE_SELECT;
  6827. I915_WRITE(PIPECONF(pipe), val);
  6828. POSTING_READ(PIPECONF(pipe));
  6829. }
  6830. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6831. {
  6832. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6833. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6834. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6835. u32 val = 0;
  6836. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6837. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6838. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6839. val |= PIPECONF_INTERLACED_ILK;
  6840. else
  6841. val |= PIPECONF_PROGRESSIVE;
  6842. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6843. POSTING_READ(PIPECONF(cpu_transcoder));
  6844. }
  6845. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6846. {
  6847. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6849. struct intel_crtc_state *config = intel_crtc->config;
  6850. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6851. u32 val = 0;
  6852. switch (intel_crtc->config->pipe_bpp) {
  6853. case 18:
  6854. val |= PIPEMISC_DITHER_6_BPC;
  6855. break;
  6856. case 24:
  6857. val |= PIPEMISC_DITHER_8_BPC;
  6858. break;
  6859. case 30:
  6860. val |= PIPEMISC_DITHER_10_BPC;
  6861. break;
  6862. case 36:
  6863. val |= PIPEMISC_DITHER_12_BPC;
  6864. break;
  6865. default:
  6866. /* Case prevented by pipe_config_set_bpp. */
  6867. BUG();
  6868. }
  6869. if (intel_crtc->config->dither)
  6870. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6871. if (config->ycbcr420) {
  6872. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  6873. PIPEMISC_YUV420_ENABLE |
  6874. PIPEMISC_YUV420_MODE_FULL_BLEND;
  6875. }
  6876. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6877. }
  6878. }
  6879. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6880. {
  6881. /*
  6882. * Account for spread spectrum to avoid
  6883. * oversubscribing the link. Max center spread
  6884. * is 2.5%; use 5% for safety's sake.
  6885. */
  6886. u32 bps = target_clock * bpp * 21 / 20;
  6887. return DIV_ROUND_UP(bps, link_bw * 8);
  6888. }
  6889. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6890. {
  6891. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6892. }
  6893. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6894. struct intel_crtc_state *crtc_state,
  6895. struct dpll *reduced_clock)
  6896. {
  6897. struct drm_crtc *crtc = &intel_crtc->base;
  6898. struct drm_device *dev = crtc->dev;
  6899. struct drm_i915_private *dev_priv = to_i915(dev);
  6900. u32 dpll, fp, fp2;
  6901. int factor;
  6902. /* Enable autotuning of the PLL clock (if permissible) */
  6903. factor = 21;
  6904. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6905. if ((intel_panel_use_ssc(dev_priv) &&
  6906. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6907. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6908. factor = 25;
  6909. } else if (crtc_state->sdvo_tv_clock)
  6910. factor = 20;
  6911. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6912. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6913. fp |= FP_CB_TUNE;
  6914. if (reduced_clock) {
  6915. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6916. if (reduced_clock->m < factor * reduced_clock->n)
  6917. fp2 |= FP_CB_TUNE;
  6918. } else {
  6919. fp2 = fp;
  6920. }
  6921. dpll = 0;
  6922. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6923. dpll |= DPLLB_MODE_LVDS;
  6924. else
  6925. dpll |= DPLLB_MODE_DAC_SERIAL;
  6926. dpll |= (crtc_state->pixel_multiplier - 1)
  6927. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6928. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6929. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6930. dpll |= DPLL_SDVO_HIGH_SPEED;
  6931. if (intel_crtc_has_dp_encoder(crtc_state))
  6932. dpll |= DPLL_SDVO_HIGH_SPEED;
  6933. /*
  6934. * The high speed IO clock is only really required for
  6935. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6936. * possible to share the DPLL between CRT and HDMI. Enabling
  6937. * the clock needlessly does no real harm, except use up a
  6938. * bit of power potentially.
  6939. *
  6940. * We'll limit this to IVB with 3 pipes, since it has only two
  6941. * DPLLs and so DPLL sharing is the only way to get three pipes
  6942. * driving PCH ports at the same time. On SNB we could do this,
  6943. * and potentially avoid enabling the second DPLL, but it's not
  6944. * clear if it''s a win or loss power wise. No point in doing
  6945. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6946. */
  6947. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6948. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6949. dpll |= DPLL_SDVO_HIGH_SPEED;
  6950. /* compute bitmask from p1 value */
  6951. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6952. /* also FPA1 */
  6953. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6954. switch (crtc_state->dpll.p2) {
  6955. case 5:
  6956. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6957. break;
  6958. case 7:
  6959. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6960. break;
  6961. case 10:
  6962. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6963. break;
  6964. case 14:
  6965. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6966. break;
  6967. }
  6968. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6969. intel_panel_use_ssc(dev_priv))
  6970. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6971. else
  6972. dpll |= PLL_REF_INPUT_DREFCLK;
  6973. dpll |= DPLL_VCO_ENABLE;
  6974. crtc_state->dpll_hw_state.dpll = dpll;
  6975. crtc_state->dpll_hw_state.fp0 = fp;
  6976. crtc_state->dpll_hw_state.fp1 = fp2;
  6977. }
  6978. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6979. struct intel_crtc_state *crtc_state)
  6980. {
  6981. struct drm_device *dev = crtc->base.dev;
  6982. struct drm_i915_private *dev_priv = to_i915(dev);
  6983. const struct intel_limit *limit;
  6984. int refclk = 120000;
  6985. memset(&crtc_state->dpll_hw_state, 0,
  6986. sizeof(crtc_state->dpll_hw_state));
  6987. crtc->lowfreq_avail = false;
  6988. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6989. if (!crtc_state->has_pch_encoder)
  6990. return 0;
  6991. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6992. if (intel_panel_use_ssc(dev_priv)) {
  6993. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6994. dev_priv->vbt.lvds_ssc_freq);
  6995. refclk = dev_priv->vbt.lvds_ssc_freq;
  6996. }
  6997. if (intel_is_dual_link_lvds(dev)) {
  6998. if (refclk == 100000)
  6999. limit = &intel_limits_ironlake_dual_lvds_100m;
  7000. else
  7001. limit = &intel_limits_ironlake_dual_lvds;
  7002. } else {
  7003. if (refclk == 100000)
  7004. limit = &intel_limits_ironlake_single_lvds_100m;
  7005. else
  7006. limit = &intel_limits_ironlake_single_lvds;
  7007. }
  7008. } else {
  7009. limit = &intel_limits_ironlake_dac;
  7010. }
  7011. if (!crtc_state->clock_set &&
  7012. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7013. refclk, NULL, &crtc_state->dpll)) {
  7014. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7015. return -EINVAL;
  7016. }
  7017. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7018. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7019. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7020. pipe_name(crtc->pipe));
  7021. return -EINVAL;
  7022. }
  7023. return 0;
  7024. }
  7025. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7026. struct intel_link_m_n *m_n)
  7027. {
  7028. struct drm_device *dev = crtc->base.dev;
  7029. struct drm_i915_private *dev_priv = to_i915(dev);
  7030. enum pipe pipe = crtc->pipe;
  7031. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7032. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7033. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7034. & ~TU_SIZE_MASK;
  7035. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7036. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7037. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7038. }
  7039. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7040. enum transcoder transcoder,
  7041. struct intel_link_m_n *m_n,
  7042. struct intel_link_m_n *m2_n2)
  7043. {
  7044. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7045. enum pipe pipe = crtc->pipe;
  7046. if (INTEL_GEN(dev_priv) >= 5) {
  7047. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7048. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7049. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7050. & ~TU_SIZE_MASK;
  7051. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7052. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7053. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7054. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7055. * gen < 8) and if DRRS is supported (to make sure the
  7056. * registers are not unnecessarily read).
  7057. */
  7058. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7059. crtc->config->has_drrs) {
  7060. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7061. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7062. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7063. & ~TU_SIZE_MASK;
  7064. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7065. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7066. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7067. }
  7068. } else {
  7069. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7070. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7071. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7072. & ~TU_SIZE_MASK;
  7073. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7074. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7075. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7076. }
  7077. }
  7078. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7079. struct intel_crtc_state *pipe_config)
  7080. {
  7081. if (pipe_config->has_pch_encoder)
  7082. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7083. else
  7084. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7085. &pipe_config->dp_m_n,
  7086. &pipe_config->dp_m2_n2);
  7087. }
  7088. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7089. struct intel_crtc_state *pipe_config)
  7090. {
  7091. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7092. &pipe_config->fdi_m_n, NULL);
  7093. }
  7094. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7095. struct intel_crtc_state *pipe_config)
  7096. {
  7097. struct drm_device *dev = crtc->base.dev;
  7098. struct drm_i915_private *dev_priv = to_i915(dev);
  7099. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7100. uint32_t ps_ctrl = 0;
  7101. int id = -1;
  7102. int i;
  7103. /* find scaler attached to this pipe */
  7104. for (i = 0; i < crtc->num_scalers; i++) {
  7105. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7106. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7107. id = i;
  7108. pipe_config->pch_pfit.enabled = true;
  7109. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7110. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7111. break;
  7112. }
  7113. }
  7114. scaler_state->scaler_id = id;
  7115. if (id >= 0) {
  7116. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7117. } else {
  7118. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7119. }
  7120. }
  7121. static void
  7122. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7123. struct intel_initial_plane_config *plane_config)
  7124. {
  7125. struct drm_device *dev = crtc->base.dev;
  7126. struct drm_i915_private *dev_priv = to_i915(dev);
  7127. u32 val, base, offset, stride_mult, tiling;
  7128. int pipe = crtc->pipe;
  7129. int fourcc, pixel_format;
  7130. unsigned int aligned_height;
  7131. struct drm_framebuffer *fb;
  7132. struct intel_framebuffer *intel_fb;
  7133. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7134. if (!intel_fb) {
  7135. DRM_DEBUG_KMS("failed to alloc fb\n");
  7136. return;
  7137. }
  7138. fb = &intel_fb->base;
  7139. fb->dev = dev;
  7140. val = I915_READ(PLANE_CTL(pipe, 0));
  7141. if (!(val & PLANE_CTL_ENABLE))
  7142. goto error;
  7143. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7144. fourcc = skl_format_to_fourcc(pixel_format,
  7145. val & PLANE_CTL_ORDER_RGBX,
  7146. val & PLANE_CTL_ALPHA_MASK);
  7147. fb->format = drm_format_info(fourcc);
  7148. tiling = val & PLANE_CTL_TILED_MASK;
  7149. switch (tiling) {
  7150. case PLANE_CTL_TILED_LINEAR:
  7151. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7152. break;
  7153. case PLANE_CTL_TILED_X:
  7154. plane_config->tiling = I915_TILING_X;
  7155. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7156. break;
  7157. case PLANE_CTL_TILED_Y:
  7158. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7159. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7160. else
  7161. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7162. break;
  7163. case PLANE_CTL_TILED_YF:
  7164. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7165. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7166. else
  7167. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7168. break;
  7169. default:
  7170. MISSING_CASE(tiling);
  7171. goto error;
  7172. }
  7173. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7174. plane_config->base = base;
  7175. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7176. val = I915_READ(PLANE_SIZE(pipe, 0));
  7177. fb->height = ((val >> 16) & 0xfff) + 1;
  7178. fb->width = ((val >> 0) & 0x1fff) + 1;
  7179. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7180. stride_mult = intel_fb_stride_alignment(fb, 0);
  7181. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7182. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7183. plane_config->size = fb->pitches[0] * aligned_height;
  7184. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7185. pipe_name(pipe), fb->width, fb->height,
  7186. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7187. plane_config->size);
  7188. plane_config->fb = intel_fb;
  7189. return;
  7190. error:
  7191. kfree(intel_fb);
  7192. }
  7193. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7194. struct intel_crtc_state *pipe_config)
  7195. {
  7196. struct drm_device *dev = crtc->base.dev;
  7197. struct drm_i915_private *dev_priv = to_i915(dev);
  7198. uint32_t tmp;
  7199. tmp = I915_READ(PF_CTL(crtc->pipe));
  7200. if (tmp & PF_ENABLE) {
  7201. pipe_config->pch_pfit.enabled = true;
  7202. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7203. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7204. /* We currently do not free assignements of panel fitters on
  7205. * ivb/hsw (since we don't use the higher upscaling modes which
  7206. * differentiates them) so just WARN about this case for now. */
  7207. if (IS_GEN7(dev_priv)) {
  7208. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7209. PF_PIPE_SEL_IVB(crtc->pipe));
  7210. }
  7211. }
  7212. }
  7213. static void
  7214. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7215. struct intel_initial_plane_config *plane_config)
  7216. {
  7217. struct drm_device *dev = crtc->base.dev;
  7218. struct drm_i915_private *dev_priv = to_i915(dev);
  7219. u32 val, base, offset;
  7220. int pipe = crtc->pipe;
  7221. int fourcc, pixel_format;
  7222. unsigned int aligned_height;
  7223. struct drm_framebuffer *fb;
  7224. struct intel_framebuffer *intel_fb;
  7225. val = I915_READ(DSPCNTR(pipe));
  7226. if (!(val & DISPLAY_PLANE_ENABLE))
  7227. return;
  7228. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7229. if (!intel_fb) {
  7230. DRM_DEBUG_KMS("failed to alloc fb\n");
  7231. return;
  7232. }
  7233. fb = &intel_fb->base;
  7234. fb->dev = dev;
  7235. if (INTEL_GEN(dev_priv) >= 4) {
  7236. if (val & DISPPLANE_TILED) {
  7237. plane_config->tiling = I915_TILING_X;
  7238. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7239. }
  7240. }
  7241. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7242. fourcc = i9xx_format_to_fourcc(pixel_format);
  7243. fb->format = drm_format_info(fourcc);
  7244. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7245. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7246. offset = I915_READ(DSPOFFSET(pipe));
  7247. } else {
  7248. if (plane_config->tiling)
  7249. offset = I915_READ(DSPTILEOFF(pipe));
  7250. else
  7251. offset = I915_READ(DSPLINOFF(pipe));
  7252. }
  7253. plane_config->base = base;
  7254. val = I915_READ(PIPESRC(pipe));
  7255. fb->width = ((val >> 16) & 0xfff) + 1;
  7256. fb->height = ((val >> 0) & 0xfff) + 1;
  7257. val = I915_READ(DSPSTRIDE(pipe));
  7258. fb->pitches[0] = val & 0xffffffc0;
  7259. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7260. plane_config->size = fb->pitches[0] * aligned_height;
  7261. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7262. pipe_name(pipe), fb->width, fb->height,
  7263. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7264. plane_config->size);
  7265. plane_config->fb = intel_fb;
  7266. }
  7267. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7268. struct intel_crtc_state *pipe_config)
  7269. {
  7270. struct drm_device *dev = crtc->base.dev;
  7271. struct drm_i915_private *dev_priv = to_i915(dev);
  7272. enum intel_display_power_domain power_domain;
  7273. uint32_t tmp;
  7274. bool ret;
  7275. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7276. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7277. return false;
  7278. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7279. pipe_config->shared_dpll = NULL;
  7280. ret = false;
  7281. tmp = I915_READ(PIPECONF(crtc->pipe));
  7282. if (!(tmp & PIPECONF_ENABLE))
  7283. goto out;
  7284. switch (tmp & PIPECONF_BPC_MASK) {
  7285. case PIPECONF_6BPC:
  7286. pipe_config->pipe_bpp = 18;
  7287. break;
  7288. case PIPECONF_8BPC:
  7289. pipe_config->pipe_bpp = 24;
  7290. break;
  7291. case PIPECONF_10BPC:
  7292. pipe_config->pipe_bpp = 30;
  7293. break;
  7294. case PIPECONF_12BPC:
  7295. pipe_config->pipe_bpp = 36;
  7296. break;
  7297. default:
  7298. break;
  7299. }
  7300. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7301. pipe_config->limited_color_range = true;
  7302. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7303. struct intel_shared_dpll *pll;
  7304. enum intel_dpll_id pll_id;
  7305. pipe_config->has_pch_encoder = true;
  7306. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7307. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7308. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7309. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7310. if (HAS_PCH_IBX(dev_priv)) {
  7311. /*
  7312. * The pipe->pch transcoder and pch transcoder->pll
  7313. * mapping is fixed.
  7314. */
  7315. pll_id = (enum intel_dpll_id) crtc->pipe;
  7316. } else {
  7317. tmp = I915_READ(PCH_DPLL_SEL);
  7318. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7319. pll_id = DPLL_ID_PCH_PLL_B;
  7320. else
  7321. pll_id= DPLL_ID_PCH_PLL_A;
  7322. }
  7323. pipe_config->shared_dpll =
  7324. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7325. pll = pipe_config->shared_dpll;
  7326. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7327. &pipe_config->dpll_hw_state));
  7328. tmp = pipe_config->dpll_hw_state.dpll;
  7329. pipe_config->pixel_multiplier =
  7330. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7331. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7332. ironlake_pch_clock_get(crtc, pipe_config);
  7333. } else {
  7334. pipe_config->pixel_multiplier = 1;
  7335. }
  7336. intel_get_pipe_timings(crtc, pipe_config);
  7337. intel_get_pipe_src_size(crtc, pipe_config);
  7338. ironlake_get_pfit_config(crtc, pipe_config);
  7339. ret = true;
  7340. out:
  7341. intel_display_power_put(dev_priv, power_domain);
  7342. return ret;
  7343. }
  7344. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7345. {
  7346. struct drm_device *dev = &dev_priv->drm;
  7347. struct intel_crtc *crtc;
  7348. for_each_intel_crtc(dev, crtc)
  7349. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7350. pipe_name(crtc->pipe));
  7351. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7352. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7353. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7354. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7355. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7356. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7357. "CPU PWM1 enabled\n");
  7358. if (IS_HASWELL(dev_priv))
  7359. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7360. "CPU PWM2 enabled\n");
  7361. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7362. "PCH PWM1 enabled\n");
  7363. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7364. "Utility pin enabled\n");
  7365. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7366. /*
  7367. * In theory we can still leave IRQs enabled, as long as only the HPD
  7368. * interrupts remain enabled. We used to check for that, but since it's
  7369. * gen-specific and since we only disable LCPLL after we fully disable
  7370. * the interrupts, the check below should be enough.
  7371. */
  7372. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7373. }
  7374. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7375. {
  7376. if (IS_HASWELL(dev_priv))
  7377. return I915_READ(D_COMP_HSW);
  7378. else
  7379. return I915_READ(D_COMP_BDW);
  7380. }
  7381. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7382. {
  7383. if (IS_HASWELL(dev_priv)) {
  7384. mutex_lock(&dev_priv->rps.hw_lock);
  7385. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7386. val))
  7387. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7388. mutex_unlock(&dev_priv->rps.hw_lock);
  7389. } else {
  7390. I915_WRITE(D_COMP_BDW, val);
  7391. POSTING_READ(D_COMP_BDW);
  7392. }
  7393. }
  7394. /*
  7395. * This function implements pieces of two sequences from BSpec:
  7396. * - Sequence for display software to disable LCPLL
  7397. * - Sequence for display software to allow package C8+
  7398. * The steps implemented here are just the steps that actually touch the LCPLL
  7399. * register. Callers should take care of disabling all the display engine
  7400. * functions, doing the mode unset, fixing interrupts, etc.
  7401. */
  7402. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7403. bool switch_to_fclk, bool allow_power_down)
  7404. {
  7405. uint32_t val;
  7406. assert_can_disable_lcpll(dev_priv);
  7407. val = I915_READ(LCPLL_CTL);
  7408. if (switch_to_fclk) {
  7409. val |= LCPLL_CD_SOURCE_FCLK;
  7410. I915_WRITE(LCPLL_CTL, val);
  7411. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7412. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7413. DRM_ERROR("Switching to FCLK failed\n");
  7414. val = I915_READ(LCPLL_CTL);
  7415. }
  7416. val |= LCPLL_PLL_DISABLE;
  7417. I915_WRITE(LCPLL_CTL, val);
  7418. POSTING_READ(LCPLL_CTL);
  7419. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7420. DRM_ERROR("LCPLL still locked\n");
  7421. val = hsw_read_dcomp(dev_priv);
  7422. val |= D_COMP_COMP_DISABLE;
  7423. hsw_write_dcomp(dev_priv, val);
  7424. ndelay(100);
  7425. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7426. 1))
  7427. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7428. if (allow_power_down) {
  7429. val = I915_READ(LCPLL_CTL);
  7430. val |= LCPLL_POWER_DOWN_ALLOW;
  7431. I915_WRITE(LCPLL_CTL, val);
  7432. POSTING_READ(LCPLL_CTL);
  7433. }
  7434. }
  7435. /*
  7436. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7437. * source.
  7438. */
  7439. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7440. {
  7441. uint32_t val;
  7442. val = I915_READ(LCPLL_CTL);
  7443. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7444. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7445. return;
  7446. /*
  7447. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7448. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7449. */
  7450. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7451. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7452. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7453. I915_WRITE(LCPLL_CTL, val);
  7454. POSTING_READ(LCPLL_CTL);
  7455. }
  7456. val = hsw_read_dcomp(dev_priv);
  7457. val |= D_COMP_COMP_FORCE;
  7458. val &= ~D_COMP_COMP_DISABLE;
  7459. hsw_write_dcomp(dev_priv, val);
  7460. val = I915_READ(LCPLL_CTL);
  7461. val &= ~LCPLL_PLL_DISABLE;
  7462. I915_WRITE(LCPLL_CTL, val);
  7463. if (intel_wait_for_register(dev_priv,
  7464. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7465. 5))
  7466. DRM_ERROR("LCPLL not locked yet\n");
  7467. if (val & LCPLL_CD_SOURCE_FCLK) {
  7468. val = I915_READ(LCPLL_CTL);
  7469. val &= ~LCPLL_CD_SOURCE_FCLK;
  7470. I915_WRITE(LCPLL_CTL, val);
  7471. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7472. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7473. DRM_ERROR("Switching back to LCPLL failed\n");
  7474. }
  7475. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7476. intel_update_cdclk(dev_priv);
  7477. }
  7478. /*
  7479. * Package states C8 and deeper are really deep PC states that can only be
  7480. * reached when all the devices on the system allow it, so even if the graphics
  7481. * device allows PC8+, it doesn't mean the system will actually get to these
  7482. * states. Our driver only allows PC8+ when going into runtime PM.
  7483. *
  7484. * The requirements for PC8+ are that all the outputs are disabled, the power
  7485. * well is disabled and most interrupts are disabled, and these are also
  7486. * requirements for runtime PM. When these conditions are met, we manually do
  7487. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7488. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7489. * hang the machine.
  7490. *
  7491. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7492. * the state of some registers, so when we come back from PC8+ we need to
  7493. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7494. * need to take care of the registers kept by RC6. Notice that this happens even
  7495. * if we don't put the device in PCI D3 state (which is what currently happens
  7496. * because of the runtime PM support).
  7497. *
  7498. * For more, read "Display Sequences for Package C8" on the hardware
  7499. * documentation.
  7500. */
  7501. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7502. {
  7503. uint32_t val;
  7504. DRM_DEBUG_KMS("Enabling package C8+\n");
  7505. if (HAS_PCH_LPT_LP(dev_priv)) {
  7506. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7507. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7508. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7509. }
  7510. lpt_disable_clkout_dp(dev_priv);
  7511. hsw_disable_lcpll(dev_priv, true, true);
  7512. }
  7513. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7514. {
  7515. uint32_t val;
  7516. DRM_DEBUG_KMS("Disabling package C8+\n");
  7517. hsw_restore_lcpll(dev_priv);
  7518. lpt_init_pch_refclk(dev_priv);
  7519. if (HAS_PCH_LPT_LP(dev_priv)) {
  7520. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7521. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7522. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7523. }
  7524. }
  7525. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7526. struct intel_crtc_state *crtc_state)
  7527. {
  7528. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7529. struct intel_encoder *encoder =
  7530. intel_ddi_get_crtc_new_encoder(crtc_state);
  7531. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7532. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7533. pipe_name(crtc->pipe));
  7534. return -EINVAL;
  7535. }
  7536. }
  7537. crtc->lowfreq_avail = false;
  7538. return 0;
  7539. }
  7540. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7541. enum port port,
  7542. struct intel_crtc_state *pipe_config)
  7543. {
  7544. enum intel_dpll_id id;
  7545. u32 temp;
  7546. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7547. id = temp >> (port * 2);
  7548. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7549. return;
  7550. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7551. }
  7552. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7553. enum port port,
  7554. struct intel_crtc_state *pipe_config)
  7555. {
  7556. enum intel_dpll_id id;
  7557. switch (port) {
  7558. case PORT_A:
  7559. id = DPLL_ID_SKL_DPLL0;
  7560. break;
  7561. case PORT_B:
  7562. id = DPLL_ID_SKL_DPLL1;
  7563. break;
  7564. case PORT_C:
  7565. id = DPLL_ID_SKL_DPLL2;
  7566. break;
  7567. default:
  7568. DRM_ERROR("Incorrect port type\n");
  7569. return;
  7570. }
  7571. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7572. }
  7573. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7574. enum port port,
  7575. struct intel_crtc_state *pipe_config)
  7576. {
  7577. enum intel_dpll_id id;
  7578. u32 temp;
  7579. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7580. id = temp >> (port * 3 + 1);
  7581. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7582. return;
  7583. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7584. }
  7585. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7586. enum port port,
  7587. struct intel_crtc_state *pipe_config)
  7588. {
  7589. enum intel_dpll_id id;
  7590. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7591. switch (ddi_pll_sel) {
  7592. case PORT_CLK_SEL_WRPLL1:
  7593. id = DPLL_ID_WRPLL1;
  7594. break;
  7595. case PORT_CLK_SEL_WRPLL2:
  7596. id = DPLL_ID_WRPLL2;
  7597. break;
  7598. case PORT_CLK_SEL_SPLL:
  7599. id = DPLL_ID_SPLL;
  7600. break;
  7601. case PORT_CLK_SEL_LCPLL_810:
  7602. id = DPLL_ID_LCPLL_810;
  7603. break;
  7604. case PORT_CLK_SEL_LCPLL_1350:
  7605. id = DPLL_ID_LCPLL_1350;
  7606. break;
  7607. case PORT_CLK_SEL_LCPLL_2700:
  7608. id = DPLL_ID_LCPLL_2700;
  7609. break;
  7610. default:
  7611. MISSING_CASE(ddi_pll_sel);
  7612. /* fall through */
  7613. case PORT_CLK_SEL_NONE:
  7614. return;
  7615. }
  7616. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7617. }
  7618. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7619. struct intel_crtc_state *pipe_config,
  7620. u64 *power_domain_mask)
  7621. {
  7622. struct drm_device *dev = crtc->base.dev;
  7623. struct drm_i915_private *dev_priv = to_i915(dev);
  7624. enum intel_display_power_domain power_domain;
  7625. u32 tmp;
  7626. /*
  7627. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7628. * transcoder handled below.
  7629. */
  7630. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7631. /*
  7632. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7633. * consistency and less surprising code; it's in always on power).
  7634. */
  7635. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7636. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7637. enum pipe trans_edp_pipe;
  7638. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7639. default:
  7640. WARN(1, "unknown pipe linked to edp transcoder\n");
  7641. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7642. case TRANS_DDI_EDP_INPUT_A_ON:
  7643. trans_edp_pipe = PIPE_A;
  7644. break;
  7645. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7646. trans_edp_pipe = PIPE_B;
  7647. break;
  7648. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7649. trans_edp_pipe = PIPE_C;
  7650. break;
  7651. }
  7652. if (trans_edp_pipe == crtc->pipe)
  7653. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7654. }
  7655. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7656. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7657. return false;
  7658. *power_domain_mask |= BIT_ULL(power_domain);
  7659. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7660. return tmp & PIPECONF_ENABLE;
  7661. }
  7662. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7663. struct intel_crtc_state *pipe_config,
  7664. u64 *power_domain_mask)
  7665. {
  7666. struct drm_device *dev = crtc->base.dev;
  7667. struct drm_i915_private *dev_priv = to_i915(dev);
  7668. enum intel_display_power_domain power_domain;
  7669. enum port port;
  7670. enum transcoder cpu_transcoder;
  7671. u32 tmp;
  7672. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7673. if (port == PORT_A)
  7674. cpu_transcoder = TRANSCODER_DSI_A;
  7675. else
  7676. cpu_transcoder = TRANSCODER_DSI_C;
  7677. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7678. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7679. continue;
  7680. *power_domain_mask |= BIT_ULL(power_domain);
  7681. /*
  7682. * The PLL needs to be enabled with a valid divider
  7683. * configuration, otherwise accessing DSI registers will hang
  7684. * the machine. See BSpec North Display Engine
  7685. * registers/MIPI[BXT]. We can break out here early, since we
  7686. * need the same DSI PLL to be enabled for both DSI ports.
  7687. */
  7688. if (!intel_dsi_pll_is_enabled(dev_priv))
  7689. break;
  7690. /* XXX: this works for video mode only */
  7691. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7692. if (!(tmp & DPI_ENABLE))
  7693. continue;
  7694. tmp = I915_READ(MIPI_CTRL(port));
  7695. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7696. continue;
  7697. pipe_config->cpu_transcoder = cpu_transcoder;
  7698. break;
  7699. }
  7700. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7701. }
  7702. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7703. struct intel_crtc_state *pipe_config)
  7704. {
  7705. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7706. struct intel_shared_dpll *pll;
  7707. enum port port;
  7708. uint32_t tmp;
  7709. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7710. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7711. if (IS_CANNONLAKE(dev_priv))
  7712. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7713. else if (IS_GEN9_BC(dev_priv))
  7714. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7715. else if (IS_GEN9_LP(dev_priv))
  7716. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7717. else
  7718. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7719. pll = pipe_config->shared_dpll;
  7720. if (pll) {
  7721. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7722. &pipe_config->dpll_hw_state));
  7723. }
  7724. /*
  7725. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7726. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7727. * the PCH transcoder is on.
  7728. */
  7729. if (INTEL_GEN(dev_priv) < 9 &&
  7730. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7731. pipe_config->has_pch_encoder = true;
  7732. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7733. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7734. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7735. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7736. }
  7737. }
  7738. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7739. struct intel_crtc_state *pipe_config)
  7740. {
  7741. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7742. enum intel_display_power_domain power_domain;
  7743. u64 power_domain_mask;
  7744. bool active;
  7745. intel_crtc_init_scalers(crtc, pipe_config);
  7746. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7747. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7748. return false;
  7749. power_domain_mask = BIT_ULL(power_domain);
  7750. pipe_config->shared_dpll = NULL;
  7751. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7752. if (IS_GEN9_LP(dev_priv) &&
  7753. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7754. WARN_ON(active);
  7755. active = true;
  7756. }
  7757. if (!active)
  7758. goto out;
  7759. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7760. haswell_get_ddi_port_state(crtc, pipe_config);
  7761. intel_get_pipe_timings(crtc, pipe_config);
  7762. }
  7763. intel_get_pipe_src_size(crtc, pipe_config);
  7764. pipe_config->gamma_mode =
  7765. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7766. if (IS_BROADWELL(dev_priv) || dev_priv->info.gen >= 9) {
  7767. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7768. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7769. if (IS_GEMINILAKE(dev_priv) || dev_priv->info.gen >= 10) {
  7770. bool blend_mode_420 = tmp &
  7771. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7772. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7773. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7774. pipe_config->ycbcr420 != blend_mode_420)
  7775. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7776. } else if (clrspace_yuv) {
  7777. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7778. }
  7779. }
  7780. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7781. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7782. power_domain_mask |= BIT_ULL(power_domain);
  7783. if (INTEL_GEN(dev_priv) >= 9)
  7784. skylake_get_pfit_config(crtc, pipe_config);
  7785. else
  7786. ironlake_get_pfit_config(crtc, pipe_config);
  7787. }
  7788. if (IS_HASWELL(dev_priv))
  7789. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7790. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7791. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7792. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7793. pipe_config->pixel_multiplier =
  7794. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7795. } else {
  7796. pipe_config->pixel_multiplier = 1;
  7797. }
  7798. out:
  7799. for_each_power_domain(power_domain, power_domain_mask)
  7800. intel_display_power_put(dev_priv, power_domain);
  7801. return active;
  7802. }
  7803. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7804. {
  7805. struct drm_i915_private *dev_priv =
  7806. to_i915(plane_state->base.plane->dev);
  7807. const struct drm_framebuffer *fb = plane_state->base.fb;
  7808. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7809. u32 base;
  7810. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7811. base = obj->phys_handle->busaddr;
  7812. else
  7813. base = intel_plane_ggtt_offset(plane_state);
  7814. base += plane_state->main.offset;
  7815. /* ILK+ do this automagically */
  7816. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7817. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7818. base += (plane_state->base.crtc_h *
  7819. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7820. return base;
  7821. }
  7822. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7823. {
  7824. int x = plane_state->base.crtc_x;
  7825. int y = plane_state->base.crtc_y;
  7826. u32 pos = 0;
  7827. if (x < 0) {
  7828. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7829. x = -x;
  7830. }
  7831. pos |= x << CURSOR_X_SHIFT;
  7832. if (y < 0) {
  7833. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7834. y = -y;
  7835. }
  7836. pos |= y << CURSOR_Y_SHIFT;
  7837. return pos;
  7838. }
  7839. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7840. {
  7841. const struct drm_mode_config *config =
  7842. &plane_state->base.plane->dev->mode_config;
  7843. int width = plane_state->base.crtc_w;
  7844. int height = plane_state->base.crtc_h;
  7845. return width > 0 && width <= config->cursor_width &&
  7846. height > 0 && height <= config->cursor_height;
  7847. }
  7848. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7849. struct intel_plane_state *plane_state)
  7850. {
  7851. const struct drm_framebuffer *fb = plane_state->base.fb;
  7852. int src_x, src_y;
  7853. u32 offset;
  7854. int ret;
  7855. ret = drm_plane_helper_check_state(&plane_state->base,
  7856. &plane_state->clip,
  7857. DRM_PLANE_HELPER_NO_SCALING,
  7858. DRM_PLANE_HELPER_NO_SCALING,
  7859. true, true);
  7860. if (ret)
  7861. return ret;
  7862. if (!fb)
  7863. return 0;
  7864. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7865. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7866. return -EINVAL;
  7867. }
  7868. src_x = plane_state->base.src_x >> 16;
  7869. src_y = plane_state->base.src_y >> 16;
  7870. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7871. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7872. if (src_x != 0 || src_y != 0) {
  7873. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7874. return -EINVAL;
  7875. }
  7876. plane_state->main.offset = offset;
  7877. return 0;
  7878. }
  7879. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7880. const struct intel_plane_state *plane_state)
  7881. {
  7882. const struct drm_framebuffer *fb = plane_state->base.fb;
  7883. return CURSOR_ENABLE |
  7884. CURSOR_GAMMA_ENABLE |
  7885. CURSOR_FORMAT_ARGB |
  7886. CURSOR_STRIDE(fb->pitches[0]);
  7887. }
  7888. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7889. {
  7890. int width = plane_state->base.crtc_w;
  7891. /*
  7892. * 845g/865g are only limited by the width of their cursors,
  7893. * the height is arbitrary up to the precision of the register.
  7894. */
  7895. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7896. }
  7897. static int i845_check_cursor(struct intel_plane *plane,
  7898. struct intel_crtc_state *crtc_state,
  7899. struct intel_plane_state *plane_state)
  7900. {
  7901. const struct drm_framebuffer *fb = plane_state->base.fb;
  7902. int ret;
  7903. ret = intel_check_cursor(crtc_state, plane_state);
  7904. if (ret)
  7905. return ret;
  7906. /* if we want to turn off the cursor ignore width and height */
  7907. if (!fb)
  7908. return 0;
  7909. /* Check for which cursor types we support */
  7910. if (!i845_cursor_size_ok(plane_state)) {
  7911. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7912. plane_state->base.crtc_w,
  7913. plane_state->base.crtc_h);
  7914. return -EINVAL;
  7915. }
  7916. switch (fb->pitches[0]) {
  7917. case 256:
  7918. case 512:
  7919. case 1024:
  7920. case 2048:
  7921. break;
  7922. default:
  7923. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7924. fb->pitches[0]);
  7925. return -EINVAL;
  7926. }
  7927. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7928. return 0;
  7929. }
  7930. static void i845_update_cursor(struct intel_plane *plane,
  7931. const struct intel_crtc_state *crtc_state,
  7932. const struct intel_plane_state *plane_state)
  7933. {
  7934. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7935. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7936. unsigned long irqflags;
  7937. if (plane_state && plane_state->base.visible) {
  7938. unsigned int width = plane_state->base.crtc_w;
  7939. unsigned int height = plane_state->base.crtc_h;
  7940. cntl = plane_state->ctl;
  7941. size = (height << 12) | width;
  7942. base = intel_cursor_base(plane_state);
  7943. pos = intel_cursor_position(plane_state);
  7944. }
  7945. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7946. /* On these chipsets we can only modify the base/size/stride
  7947. * whilst the cursor is disabled.
  7948. */
  7949. if (plane->cursor.base != base ||
  7950. plane->cursor.size != size ||
  7951. plane->cursor.cntl != cntl) {
  7952. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7953. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7954. I915_WRITE_FW(CURSIZE, size);
  7955. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7956. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7957. plane->cursor.base = base;
  7958. plane->cursor.size = size;
  7959. plane->cursor.cntl = cntl;
  7960. } else {
  7961. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7962. }
  7963. POSTING_READ_FW(CURCNTR(PIPE_A));
  7964. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7965. }
  7966. static void i845_disable_cursor(struct intel_plane *plane,
  7967. struct intel_crtc *crtc)
  7968. {
  7969. i845_update_cursor(plane, NULL, NULL);
  7970. }
  7971. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7972. const struct intel_plane_state *plane_state)
  7973. {
  7974. struct drm_i915_private *dev_priv =
  7975. to_i915(plane_state->base.plane->dev);
  7976. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7977. u32 cntl;
  7978. cntl = MCURSOR_GAMMA_ENABLE;
  7979. if (HAS_DDI(dev_priv))
  7980. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7981. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7982. switch (plane_state->base.crtc_w) {
  7983. case 64:
  7984. cntl |= CURSOR_MODE_64_ARGB_AX;
  7985. break;
  7986. case 128:
  7987. cntl |= CURSOR_MODE_128_ARGB_AX;
  7988. break;
  7989. case 256:
  7990. cntl |= CURSOR_MODE_256_ARGB_AX;
  7991. break;
  7992. default:
  7993. MISSING_CASE(plane_state->base.crtc_w);
  7994. return 0;
  7995. }
  7996. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7997. cntl |= CURSOR_ROTATE_180;
  7998. return cntl;
  7999. }
  8000. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8001. {
  8002. struct drm_i915_private *dev_priv =
  8003. to_i915(plane_state->base.plane->dev);
  8004. int width = plane_state->base.crtc_w;
  8005. int height = plane_state->base.crtc_h;
  8006. if (!intel_cursor_size_ok(plane_state))
  8007. return false;
  8008. /* Cursor width is limited to a few power-of-two sizes */
  8009. switch (width) {
  8010. case 256:
  8011. case 128:
  8012. case 64:
  8013. break;
  8014. default:
  8015. return false;
  8016. }
  8017. /*
  8018. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8019. * height from 8 lines up to the cursor width, when the
  8020. * cursor is not rotated. Everything else requires square
  8021. * cursors.
  8022. */
  8023. if (HAS_CUR_FBC(dev_priv) &&
  8024. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8025. if (height < 8 || height > width)
  8026. return false;
  8027. } else {
  8028. if (height != width)
  8029. return false;
  8030. }
  8031. return true;
  8032. }
  8033. static int i9xx_check_cursor(struct intel_plane *plane,
  8034. struct intel_crtc_state *crtc_state,
  8035. struct intel_plane_state *plane_state)
  8036. {
  8037. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8038. const struct drm_framebuffer *fb = plane_state->base.fb;
  8039. enum pipe pipe = plane->pipe;
  8040. int ret;
  8041. ret = intel_check_cursor(crtc_state, plane_state);
  8042. if (ret)
  8043. return ret;
  8044. /* if we want to turn off the cursor ignore width and height */
  8045. if (!fb)
  8046. return 0;
  8047. /* Check for which cursor types we support */
  8048. if (!i9xx_cursor_size_ok(plane_state)) {
  8049. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8050. plane_state->base.crtc_w,
  8051. plane_state->base.crtc_h);
  8052. return -EINVAL;
  8053. }
  8054. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8055. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8056. fb->pitches[0], plane_state->base.crtc_w);
  8057. return -EINVAL;
  8058. }
  8059. /*
  8060. * There's something wrong with the cursor on CHV pipe C.
  8061. * If it straddles the left edge of the screen then
  8062. * moving it away from the edge or disabling it often
  8063. * results in a pipe underrun, and often that can lead to
  8064. * dead pipe (constant underrun reported, and it scans
  8065. * out just a solid color). To recover from that, the
  8066. * display power well must be turned off and on again.
  8067. * Refuse the put the cursor into that compromised position.
  8068. */
  8069. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8070. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8071. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8072. return -EINVAL;
  8073. }
  8074. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8075. return 0;
  8076. }
  8077. static void i9xx_update_cursor(struct intel_plane *plane,
  8078. const struct intel_crtc_state *crtc_state,
  8079. const struct intel_plane_state *plane_state)
  8080. {
  8081. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8082. enum pipe pipe = plane->pipe;
  8083. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8084. unsigned long irqflags;
  8085. if (plane_state && plane_state->base.visible) {
  8086. cntl = plane_state->ctl;
  8087. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8088. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8089. base = intel_cursor_base(plane_state);
  8090. pos = intel_cursor_position(plane_state);
  8091. }
  8092. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8093. /*
  8094. * On some platforms writing CURCNTR first will also
  8095. * cause CURPOS to be armed by the CURBASE write.
  8096. * Without the CURCNTR write the CURPOS write would
  8097. * arm itself. Thus we always start the full update
  8098. * with a CURCNTR write.
  8099. *
  8100. * On other platforms CURPOS always requires the
  8101. * CURBASE write to arm the update. Additonally
  8102. * a write to any of the cursor register will cancel
  8103. * an already armed cursor update. Thus leaving out
  8104. * the CURBASE write after CURPOS could lead to a
  8105. * cursor that doesn't appear to move, or even change
  8106. * shape. Thus we always write CURBASE.
  8107. *
  8108. * CURCNTR and CUR_FBC_CTL are always
  8109. * armed by the CURBASE write only.
  8110. */
  8111. if (plane->cursor.base != base ||
  8112. plane->cursor.size != fbc_ctl ||
  8113. plane->cursor.cntl != cntl) {
  8114. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8115. if (HAS_CUR_FBC(dev_priv))
  8116. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8117. I915_WRITE_FW(CURPOS(pipe), pos);
  8118. I915_WRITE_FW(CURBASE(pipe), base);
  8119. plane->cursor.base = base;
  8120. plane->cursor.size = fbc_ctl;
  8121. plane->cursor.cntl = cntl;
  8122. } else {
  8123. I915_WRITE_FW(CURPOS(pipe), pos);
  8124. I915_WRITE_FW(CURBASE(pipe), base);
  8125. }
  8126. POSTING_READ_FW(CURBASE(pipe));
  8127. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8128. }
  8129. static void i9xx_disable_cursor(struct intel_plane *plane,
  8130. struct intel_crtc *crtc)
  8131. {
  8132. i9xx_update_cursor(plane, NULL, NULL);
  8133. }
  8134. /* VESA 640x480x72Hz mode to set on the pipe */
  8135. static struct drm_display_mode load_detect_mode = {
  8136. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8137. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8138. };
  8139. struct drm_framebuffer *
  8140. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8141. struct drm_mode_fb_cmd2 *mode_cmd)
  8142. {
  8143. struct intel_framebuffer *intel_fb;
  8144. int ret;
  8145. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8146. if (!intel_fb)
  8147. return ERR_PTR(-ENOMEM);
  8148. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8149. if (ret)
  8150. goto err;
  8151. return &intel_fb->base;
  8152. err:
  8153. kfree(intel_fb);
  8154. return ERR_PTR(ret);
  8155. }
  8156. static u32
  8157. intel_framebuffer_pitch_for_width(int width, int bpp)
  8158. {
  8159. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8160. return ALIGN(pitch, 64);
  8161. }
  8162. static u32
  8163. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8164. {
  8165. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8166. return PAGE_ALIGN(pitch * mode->vdisplay);
  8167. }
  8168. static struct drm_framebuffer *
  8169. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8170. struct drm_display_mode *mode,
  8171. int depth, int bpp)
  8172. {
  8173. struct drm_framebuffer *fb;
  8174. struct drm_i915_gem_object *obj;
  8175. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8176. obj = i915_gem_object_create(to_i915(dev),
  8177. intel_framebuffer_size_for_mode(mode, bpp));
  8178. if (IS_ERR(obj))
  8179. return ERR_CAST(obj);
  8180. mode_cmd.width = mode->hdisplay;
  8181. mode_cmd.height = mode->vdisplay;
  8182. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8183. bpp);
  8184. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8185. fb = intel_framebuffer_create(obj, &mode_cmd);
  8186. if (IS_ERR(fb))
  8187. i915_gem_object_put(obj);
  8188. return fb;
  8189. }
  8190. static struct drm_framebuffer *
  8191. mode_fits_in_fbdev(struct drm_device *dev,
  8192. struct drm_display_mode *mode)
  8193. {
  8194. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8195. struct drm_i915_private *dev_priv = to_i915(dev);
  8196. struct drm_i915_gem_object *obj;
  8197. struct drm_framebuffer *fb;
  8198. if (!dev_priv->fbdev)
  8199. return NULL;
  8200. if (!dev_priv->fbdev->fb)
  8201. return NULL;
  8202. obj = dev_priv->fbdev->fb->obj;
  8203. BUG_ON(!obj);
  8204. fb = &dev_priv->fbdev->fb->base;
  8205. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8206. fb->format->cpp[0] * 8))
  8207. return NULL;
  8208. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8209. return NULL;
  8210. drm_framebuffer_reference(fb);
  8211. return fb;
  8212. #else
  8213. return NULL;
  8214. #endif
  8215. }
  8216. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8217. struct drm_crtc *crtc,
  8218. struct drm_display_mode *mode,
  8219. struct drm_framebuffer *fb,
  8220. int x, int y)
  8221. {
  8222. struct drm_plane_state *plane_state;
  8223. int hdisplay, vdisplay;
  8224. int ret;
  8225. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8226. if (IS_ERR(plane_state))
  8227. return PTR_ERR(plane_state);
  8228. if (mode)
  8229. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  8230. else
  8231. hdisplay = vdisplay = 0;
  8232. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8233. if (ret)
  8234. return ret;
  8235. drm_atomic_set_fb_for_plane(plane_state, fb);
  8236. plane_state->crtc_x = 0;
  8237. plane_state->crtc_y = 0;
  8238. plane_state->crtc_w = hdisplay;
  8239. plane_state->crtc_h = vdisplay;
  8240. plane_state->src_x = x << 16;
  8241. plane_state->src_y = y << 16;
  8242. plane_state->src_w = hdisplay << 16;
  8243. plane_state->src_h = vdisplay << 16;
  8244. return 0;
  8245. }
  8246. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8247. struct drm_display_mode *mode,
  8248. struct intel_load_detect_pipe *old,
  8249. struct drm_modeset_acquire_ctx *ctx)
  8250. {
  8251. struct intel_crtc *intel_crtc;
  8252. struct intel_encoder *intel_encoder =
  8253. intel_attached_encoder(connector);
  8254. struct drm_crtc *possible_crtc;
  8255. struct drm_encoder *encoder = &intel_encoder->base;
  8256. struct drm_crtc *crtc = NULL;
  8257. struct drm_device *dev = encoder->dev;
  8258. struct drm_i915_private *dev_priv = to_i915(dev);
  8259. struct drm_framebuffer *fb;
  8260. struct drm_mode_config *config = &dev->mode_config;
  8261. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8262. struct drm_connector_state *connector_state;
  8263. struct intel_crtc_state *crtc_state;
  8264. int ret, i = -1;
  8265. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8266. connector->base.id, connector->name,
  8267. encoder->base.id, encoder->name);
  8268. old->restore_state = NULL;
  8269. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8270. /*
  8271. * Algorithm gets a little messy:
  8272. *
  8273. * - if the connector already has an assigned crtc, use it (but make
  8274. * sure it's on first)
  8275. *
  8276. * - try to find the first unused crtc that can drive this connector,
  8277. * and use that if we find one
  8278. */
  8279. /* See if we already have a CRTC for this connector */
  8280. if (connector->state->crtc) {
  8281. crtc = connector->state->crtc;
  8282. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8283. if (ret)
  8284. goto fail;
  8285. /* Make sure the crtc and connector are running */
  8286. goto found;
  8287. }
  8288. /* Find an unused one (if possible) */
  8289. for_each_crtc(dev, possible_crtc) {
  8290. i++;
  8291. if (!(encoder->possible_crtcs & (1 << i)))
  8292. continue;
  8293. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8294. if (ret)
  8295. goto fail;
  8296. if (possible_crtc->state->enable) {
  8297. drm_modeset_unlock(&possible_crtc->mutex);
  8298. continue;
  8299. }
  8300. crtc = possible_crtc;
  8301. break;
  8302. }
  8303. /*
  8304. * If we didn't find an unused CRTC, don't use any.
  8305. */
  8306. if (!crtc) {
  8307. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8308. ret = -ENODEV;
  8309. goto fail;
  8310. }
  8311. found:
  8312. intel_crtc = to_intel_crtc(crtc);
  8313. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8314. if (ret)
  8315. goto fail;
  8316. state = drm_atomic_state_alloc(dev);
  8317. restore_state = drm_atomic_state_alloc(dev);
  8318. if (!state || !restore_state) {
  8319. ret = -ENOMEM;
  8320. goto fail;
  8321. }
  8322. state->acquire_ctx = ctx;
  8323. restore_state->acquire_ctx = ctx;
  8324. connector_state = drm_atomic_get_connector_state(state, connector);
  8325. if (IS_ERR(connector_state)) {
  8326. ret = PTR_ERR(connector_state);
  8327. goto fail;
  8328. }
  8329. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8330. if (ret)
  8331. goto fail;
  8332. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8333. if (IS_ERR(crtc_state)) {
  8334. ret = PTR_ERR(crtc_state);
  8335. goto fail;
  8336. }
  8337. crtc_state->base.active = crtc_state->base.enable = true;
  8338. if (!mode)
  8339. mode = &load_detect_mode;
  8340. /* We need a framebuffer large enough to accommodate all accesses
  8341. * that the plane may generate whilst we perform load detection.
  8342. * We can not rely on the fbcon either being present (we get called
  8343. * during its initialisation to detect all boot displays, or it may
  8344. * not even exist) or that it is large enough to satisfy the
  8345. * requested mode.
  8346. */
  8347. fb = mode_fits_in_fbdev(dev, mode);
  8348. if (fb == NULL) {
  8349. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8350. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8351. } else
  8352. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8353. if (IS_ERR(fb)) {
  8354. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8355. ret = PTR_ERR(fb);
  8356. goto fail;
  8357. }
  8358. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8359. if (ret)
  8360. goto fail;
  8361. drm_framebuffer_unreference(fb);
  8362. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8363. if (ret)
  8364. goto fail;
  8365. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8366. if (!ret)
  8367. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8368. if (!ret)
  8369. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8370. if (ret) {
  8371. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8372. goto fail;
  8373. }
  8374. ret = drm_atomic_commit(state);
  8375. if (ret) {
  8376. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8377. goto fail;
  8378. }
  8379. old->restore_state = restore_state;
  8380. drm_atomic_state_put(state);
  8381. /* let the connector get through one full cycle before testing */
  8382. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8383. return true;
  8384. fail:
  8385. if (state) {
  8386. drm_atomic_state_put(state);
  8387. state = NULL;
  8388. }
  8389. if (restore_state) {
  8390. drm_atomic_state_put(restore_state);
  8391. restore_state = NULL;
  8392. }
  8393. if (ret == -EDEADLK)
  8394. return ret;
  8395. return false;
  8396. }
  8397. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8398. struct intel_load_detect_pipe *old,
  8399. struct drm_modeset_acquire_ctx *ctx)
  8400. {
  8401. struct intel_encoder *intel_encoder =
  8402. intel_attached_encoder(connector);
  8403. struct drm_encoder *encoder = &intel_encoder->base;
  8404. struct drm_atomic_state *state = old->restore_state;
  8405. int ret;
  8406. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8407. connector->base.id, connector->name,
  8408. encoder->base.id, encoder->name);
  8409. if (!state)
  8410. return;
  8411. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8412. if (ret)
  8413. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8414. drm_atomic_state_put(state);
  8415. }
  8416. static int i9xx_pll_refclk(struct drm_device *dev,
  8417. const struct intel_crtc_state *pipe_config)
  8418. {
  8419. struct drm_i915_private *dev_priv = to_i915(dev);
  8420. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8421. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8422. return dev_priv->vbt.lvds_ssc_freq;
  8423. else if (HAS_PCH_SPLIT(dev_priv))
  8424. return 120000;
  8425. else if (!IS_GEN2(dev_priv))
  8426. return 96000;
  8427. else
  8428. return 48000;
  8429. }
  8430. /* Returns the clock of the currently programmed mode of the given pipe. */
  8431. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8432. struct intel_crtc_state *pipe_config)
  8433. {
  8434. struct drm_device *dev = crtc->base.dev;
  8435. struct drm_i915_private *dev_priv = to_i915(dev);
  8436. int pipe = pipe_config->cpu_transcoder;
  8437. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8438. u32 fp;
  8439. struct dpll clock;
  8440. int port_clock;
  8441. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8442. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8443. fp = pipe_config->dpll_hw_state.fp0;
  8444. else
  8445. fp = pipe_config->dpll_hw_state.fp1;
  8446. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8447. if (IS_PINEVIEW(dev_priv)) {
  8448. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8449. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8450. } else {
  8451. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8452. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8453. }
  8454. if (!IS_GEN2(dev_priv)) {
  8455. if (IS_PINEVIEW(dev_priv))
  8456. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8457. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8458. else
  8459. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8460. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8461. switch (dpll & DPLL_MODE_MASK) {
  8462. case DPLLB_MODE_DAC_SERIAL:
  8463. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8464. 5 : 10;
  8465. break;
  8466. case DPLLB_MODE_LVDS:
  8467. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8468. 7 : 14;
  8469. break;
  8470. default:
  8471. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8472. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8473. return;
  8474. }
  8475. if (IS_PINEVIEW(dev_priv))
  8476. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8477. else
  8478. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8479. } else {
  8480. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8481. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8482. if (is_lvds) {
  8483. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8484. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8485. if (lvds & LVDS_CLKB_POWER_UP)
  8486. clock.p2 = 7;
  8487. else
  8488. clock.p2 = 14;
  8489. } else {
  8490. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8491. clock.p1 = 2;
  8492. else {
  8493. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8494. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8495. }
  8496. if (dpll & PLL_P2_DIVIDE_BY_4)
  8497. clock.p2 = 4;
  8498. else
  8499. clock.p2 = 2;
  8500. }
  8501. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8502. }
  8503. /*
  8504. * This value includes pixel_multiplier. We will use
  8505. * port_clock to compute adjusted_mode.crtc_clock in the
  8506. * encoder's get_config() function.
  8507. */
  8508. pipe_config->port_clock = port_clock;
  8509. }
  8510. int intel_dotclock_calculate(int link_freq,
  8511. const struct intel_link_m_n *m_n)
  8512. {
  8513. /*
  8514. * The calculation for the data clock is:
  8515. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8516. * But we want to avoid losing precison if possible, so:
  8517. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8518. *
  8519. * and the link clock is simpler:
  8520. * link_clock = (m * link_clock) / n
  8521. */
  8522. if (!m_n->link_n)
  8523. return 0;
  8524. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8525. }
  8526. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8527. struct intel_crtc_state *pipe_config)
  8528. {
  8529. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8530. /* read out port_clock from the DPLL */
  8531. i9xx_crtc_clock_get(crtc, pipe_config);
  8532. /*
  8533. * In case there is an active pipe without active ports,
  8534. * we may need some idea for the dotclock anyway.
  8535. * Calculate one based on the FDI configuration.
  8536. */
  8537. pipe_config->base.adjusted_mode.crtc_clock =
  8538. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8539. &pipe_config->fdi_m_n);
  8540. }
  8541. /** Returns the currently programmed mode of the given pipe. */
  8542. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8543. struct drm_crtc *crtc)
  8544. {
  8545. struct drm_i915_private *dev_priv = to_i915(dev);
  8546. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8547. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8548. struct drm_display_mode *mode;
  8549. struct intel_crtc_state *pipe_config;
  8550. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8551. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8552. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8553. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8554. enum pipe pipe = intel_crtc->pipe;
  8555. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8556. if (!mode)
  8557. return NULL;
  8558. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8559. if (!pipe_config) {
  8560. kfree(mode);
  8561. return NULL;
  8562. }
  8563. /*
  8564. * Construct a pipe_config sufficient for getting the clock info
  8565. * back out of crtc_clock_get.
  8566. *
  8567. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8568. * to use a real value here instead.
  8569. */
  8570. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8571. pipe_config->pixel_multiplier = 1;
  8572. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8573. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8574. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8575. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8576. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8577. mode->hdisplay = (htot & 0xffff) + 1;
  8578. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8579. mode->hsync_start = (hsync & 0xffff) + 1;
  8580. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8581. mode->vdisplay = (vtot & 0xffff) + 1;
  8582. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8583. mode->vsync_start = (vsync & 0xffff) + 1;
  8584. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8585. drm_mode_set_name(mode);
  8586. kfree(pipe_config);
  8587. return mode;
  8588. }
  8589. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8590. {
  8591. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8592. drm_crtc_cleanup(crtc);
  8593. kfree(intel_crtc);
  8594. }
  8595. /**
  8596. * intel_wm_need_update - Check whether watermarks need updating
  8597. * @plane: drm plane
  8598. * @state: new plane state
  8599. *
  8600. * Check current plane state versus the new one to determine whether
  8601. * watermarks need to be recalculated.
  8602. *
  8603. * Returns true or false.
  8604. */
  8605. static bool intel_wm_need_update(struct drm_plane *plane,
  8606. struct drm_plane_state *state)
  8607. {
  8608. struct intel_plane_state *new = to_intel_plane_state(state);
  8609. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8610. /* Update watermarks on tiling or size changes. */
  8611. if (new->base.visible != cur->base.visible)
  8612. return true;
  8613. if (!cur->base.fb || !new->base.fb)
  8614. return false;
  8615. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8616. cur->base.rotation != new->base.rotation ||
  8617. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8618. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8619. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8620. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8621. return true;
  8622. return false;
  8623. }
  8624. static bool needs_scaling(struct intel_plane_state *state)
  8625. {
  8626. int src_w = drm_rect_width(&state->base.src) >> 16;
  8627. int src_h = drm_rect_height(&state->base.src) >> 16;
  8628. int dst_w = drm_rect_width(&state->base.dst);
  8629. int dst_h = drm_rect_height(&state->base.dst);
  8630. return (src_w != dst_w || src_h != dst_h);
  8631. }
  8632. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  8633. struct drm_plane_state *plane_state)
  8634. {
  8635. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8636. struct drm_crtc *crtc = crtc_state->crtc;
  8637. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8638. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8639. struct drm_device *dev = crtc->dev;
  8640. struct drm_i915_private *dev_priv = to_i915(dev);
  8641. struct intel_plane_state *old_plane_state =
  8642. to_intel_plane_state(plane->base.state);
  8643. bool mode_changed = needs_modeset(crtc_state);
  8644. bool was_crtc_enabled = crtc->state->active;
  8645. bool is_crtc_enabled = crtc_state->active;
  8646. bool turn_off, turn_on, visible, was_visible;
  8647. struct drm_framebuffer *fb = plane_state->fb;
  8648. int ret;
  8649. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8650. ret = skl_update_scaler_plane(
  8651. to_intel_crtc_state(crtc_state),
  8652. to_intel_plane_state(plane_state));
  8653. if (ret)
  8654. return ret;
  8655. }
  8656. was_visible = old_plane_state->base.visible;
  8657. visible = plane_state->visible;
  8658. if (!was_crtc_enabled && WARN_ON(was_visible))
  8659. was_visible = false;
  8660. /*
  8661. * Visibility is calculated as if the crtc was on, but
  8662. * after scaler setup everything depends on it being off
  8663. * when the crtc isn't active.
  8664. *
  8665. * FIXME this is wrong for watermarks. Watermarks should also
  8666. * be computed as if the pipe would be active. Perhaps move
  8667. * per-plane wm computation to the .check_plane() hook, and
  8668. * only combine the results from all planes in the current place?
  8669. */
  8670. if (!is_crtc_enabled) {
  8671. plane_state->visible = visible = false;
  8672. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8673. }
  8674. if (!was_visible && !visible)
  8675. return 0;
  8676. if (fb != old_plane_state->base.fb)
  8677. pipe_config->fb_changed = true;
  8678. turn_off = was_visible && (!visible || mode_changed);
  8679. turn_on = visible && (!was_visible || mode_changed);
  8680. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8681. intel_crtc->base.base.id, intel_crtc->base.name,
  8682. plane->base.base.id, plane->base.name,
  8683. fb ? fb->base.id : -1);
  8684. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8685. plane->base.base.id, plane->base.name,
  8686. was_visible, visible,
  8687. turn_off, turn_on, mode_changed);
  8688. if (turn_on) {
  8689. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8690. pipe_config->update_wm_pre = true;
  8691. /* must disable cxsr around plane enable/disable */
  8692. if (plane->id != PLANE_CURSOR)
  8693. pipe_config->disable_cxsr = true;
  8694. } else if (turn_off) {
  8695. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8696. pipe_config->update_wm_post = true;
  8697. /* must disable cxsr around plane enable/disable */
  8698. if (plane->id != PLANE_CURSOR)
  8699. pipe_config->disable_cxsr = true;
  8700. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8701. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8702. /* FIXME bollocks */
  8703. pipe_config->update_wm_pre = true;
  8704. pipe_config->update_wm_post = true;
  8705. }
  8706. }
  8707. if (visible || was_visible)
  8708. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8709. /*
  8710. * WaCxSRDisabledForSpriteScaling:ivb
  8711. *
  8712. * cstate->update_wm was already set above, so this flag will
  8713. * take effect when we commit and program watermarks.
  8714. */
  8715. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8716. needs_scaling(to_intel_plane_state(plane_state)) &&
  8717. !needs_scaling(old_plane_state))
  8718. pipe_config->disable_lp_wm = true;
  8719. return 0;
  8720. }
  8721. static bool encoders_cloneable(const struct intel_encoder *a,
  8722. const struct intel_encoder *b)
  8723. {
  8724. /* masks could be asymmetric, so check both ways */
  8725. return a == b || (a->cloneable & (1 << b->type) &&
  8726. b->cloneable & (1 << a->type));
  8727. }
  8728. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8729. struct intel_crtc *crtc,
  8730. struct intel_encoder *encoder)
  8731. {
  8732. struct intel_encoder *source_encoder;
  8733. struct drm_connector *connector;
  8734. struct drm_connector_state *connector_state;
  8735. int i;
  8736. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8737. if (connector_state->crtc != &crtc->base)
  8738. continue;
  8739. source_encoder =
  8740. to_intel_encoder(connector_state->best_encoder);
  8741. if (!encoders_cloneable(encoder, source_encoder))
  8742. return false;
  8743. }
  8744. return true;
  8745. }
  8746. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8747. struct drm_crtc_state *crtc_state)
  8748. {
  8749. struct drm_device *dev = crtc->dev;
  8750. struct drm_i915_private *dev_priv = to_i915(dev);
  8751. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8752. struct intel_crtc_state *pipe_config =
  8753. to_intel_crtc_state(crtc_state);
  8754. struct drm_atomic_state *state = crtc_state->state;
  8755. int ret;
  8756. bool mode_changed = needs_modeset(crtc_state);
  8757. if (mode_changed && !crtc_state->active)
  8758. pipe_config->update_wm_post = true;
  8759. if (mode_changed && crtc_state->enable &&
  8760. dev_priv->display.crtc_compute_clock &&
  8761. !WARN_ON(pipe_config->shared_dpll)) {
  8762. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8763. pipe_config);
  8764. if (ret)
  8765. return ret;
  8766. }
  8767. if (crtc_state->color_mgmt_changed) {
  8768. ret = intel_color_check(crtc, crtc_state);
  8769. if (ret)
  8770. return ret;
  8771. /*
  8772. * Changing color management on Intel hardware is
  8773. * handled as part of planes update.
  8774. */
  8775. crtc_state->planes_changed = true;
  8776. }
  8777. ret = 0;
  8778. if (dev_priv->display.compute_pipe_wm) {
  8779. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8780. if (ret) {
  8781. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8782. return ret;
  8783. }
  8784. }
  8785. if (dev_priv->display.compute_intermediate_wm &&
  8786. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8787. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8788. return 0;
  8789. /*
  8790. * Calculate 'intermediate' watermarks that satisfy both the
  8791. * old state and the new state. We can program these
  8792. * immediately.
  8793. */
  8794. ret = dev_priv->display.compute_intermediate_wm(dev,
  8795. intel_crtc,
  8796. pipe_config);
  8797. if (ret) {
  8798. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8799. return ret;
  8800. }
  8801. } else if (dev_priv->display.compute_intermediate_wm) {
  8802. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8803. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8804. }
  8805. if (INTEL_GEN(dev_priv) >= 9) {
  8806. if (mode_changed)
  8807. ret = skl_update_scaler_crtc(pipe_config);
  8808. if (!ret)
  8809. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8810. pipe_config);
  8811. if (!ret)
  8812. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8813. pipe_config);
  8814. }
  8815. return ret;
  8816. }
  8817. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8818. .atomic_begin = intel_begin_crtc_commit,
  8819. .atomic_flush = intel_finish_crtc_commit,
  8820. .atomic_check = intel_crtc_atomic_check,
  8821. };
  8822. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8823. {
  8824. struct intel_connector *connector;
  8825. struct drm_connector_list_iter conn_iter;
  8826. drm_connector_list_iter_begin(dev, &conn_iter);
  8827. for_each_intel_connector_iter(connector, &conn_iter) {
  8828. if (connector->base.state->crtc)
  8829. drm_connector_unreference(&connector->base);
  8830. if (connector->base.encoder) {
  8831. connector->base.state->best_encoder =
  8832. connector->base.encoder;
  8833. connector->base.state->crtc =
  8834. connector->base.encoder->crtc;
  8835. drm_connector_reference(&connector->base);
  8836. } else {
  8837. connector->base.state->best_encoder = NULL;
  8838. connector->base.state->crtc = NULL;
  8839. }
  8840. }
  8841. drm_connector_list_iter_end(&conn_iter);
  8842. }
  8843. static void
  8844. connected_sink_compute_bpp(struct intel_connector *connector,
  8845. struct intel_crtc_state *pipe_config)
  8846. {
  8847. const struct drm_display_info *info = &connector->base.display_info;
  8848. int bpp = pipe_config->pipe_bpp;
  8849. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8850. connector->base.base.id,
  8851. connector->base.name);
  8852. /* Don't use an invalid EDID bpc value */
  8853. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8854. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8855. bpp, info->bpc * 3);
  8856. pipe_config->pipe_bpp = info->bpc * 3;
  8857. }
  8858. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8859. if (info->bpc == 0 && bpp > 24) {
  8860. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8861. bpp);
  8862. pipe_config->pipe_bpp = 24;
  8863. }
  8864. }
  8865. static int
  8866. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8867. struct intel_crtc_state *pipe_config)
  8868. {
  8869. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8870. struct drm_atomic_state *state;
  8871. struct drm_connector *connector;
  8872. struct drm_connector_state *connector_state;
  8873. int bpp, i;
  8874. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8875. IS_CHERRYVIEW(dev_priv)))
  8876. bpp = 10*3;
  8877. else if (INTEL_GEN(dev_priv) >= 5)
  8878. bpp = 12*3;
  8879. else
  8880. bpp = 8*3;
  8881. pipe_config->pipe_bpp = bpp;
  8882. state = pipe_config->base.state;
  8883. /* Clamp display bpp to EDID value */
  8884. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8885. if (connector_state->crtc != &crtc->base)
  8886. continue;
  8887. connected_sink_compute_bpp(to_intel_connector(connector),
  8888. pipe_config);
  8889. }
  8890. return bpp;
  8891. }
  8892. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8893. {
  8894. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8895. "type: 0x%x flags: 0x%x\n",
  8896. mode->crtc_clock,
  8897. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8898. mode->crtc_hsync_end, mode->crtc_htotal,
  8899. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8900. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8901. }
  8902. static inline void
  8903. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8904. unsigned int lane_count, struct intel_link_m_n *m_n)
  8905. {
  8906. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8907. id, lane_count,
  8908. m_n->gmch_m, m_n->gmch_n,
  8909. m_n->link_m, m_n->link_n, m_n->tu);
  8910. }
  8911. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8912. struct intel_crtc_state *pipe_config,
  8913. const char *context)
  8914. {
  8915. struct drm_device *dev = crtc->base.dev;
  8916. struct drm_i915_private *dev_priv = to_i915(dev);
  8917. struct drm_plane *plane;
  8918. struct intel_plane *intel_plane;
  8919. struct intel_plane_state *state;
  8920. struct drm_framebuffer *fb;
  8921. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8922. crtc->base.base.id, crtc->base.name, context);
  8923. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8924. transcoder_name(pipe_config->cpu_transcoder),
  8925. pipe_config->pipe_bpp, pipe_config->dither);
  8926. if (pipe_config->has_pch_encoder)
  8927. intel_dump_m_n_config(pipe_config, "fdi",
  8928. pipe_config->fdi_lanes,
  8929. &pipe_config->fdi_m_n);
  8930. if (pipe_config->ycbcr420)
  8931. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  8932. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8933. intel_dump_m_n_config(pipe_config, "dp m_n",
  8934. pipe_config->lane_count, &pipe_config->dp_m_n);
  8935. if (pipe_config->has_drrs)
  8936. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8937. pipe_config->lane_count,
  8938. &pipe_config->dp_m2_n2);
  8939. }
  8940. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8941. pipe_config->has_audio, pipe_config->has_infoframe);
  8942. DRM_DEBUG_KMS("requested mode:\n");
  8943. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8944. DRM_DEBUG_KMS("adjusted mode:\n");
  8945. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8946. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8947. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8948. pipe_config->port_clock,
  8949. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8950. pipe_config->pixel_rate);
  8951. if (INTEL_GEN(dev_priv) >= 9)
  8952. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8953. crtc->num_scalers,
  8954. pipe_config->scaler_state.scaler_users,
  8955. pipe_config->scaler_state.scaler_id);
  8956. if (HAS_GMCH_DISPLAY(dev_priv))
  8957. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8958. pipe_config->gmch_pfit.control,
  8959. pipe_config->gmch_pfit.pgm_ratios,
  8960. pipe_config->gmch_pfit.lvds_border_bits);
  8961. else
  8962. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8963. pipe_config->pch_pfit.pos,
  8964. pipe_config->pch_pfit.size,
  8965. enableddisabled(pipe_config->pch_pfit.enabled));
  8966. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  8967. pipe_config->ips_enabled, pipe_config->double_wide);
  8968. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  8969. DRM_DEBUG_KMS("planes on this crtc\n");
  8970. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  8971. struct drm_format_name_buf format_name;
  8972. intel_plane = to_intel_plane(plane);
  8973. if (intel_plane->pipe != crtc->pipe)
  8974. continue;
  8975. state = to_intel_plane_state(plane->state);
  8976. fb = state->base.fb;
  8977. if (!fb) {
  8978. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  8979. plane->base.id, plane->name, state->scaler_id);
  8980. continue;
  8981. }
  8982. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  8983. plane->base.id, plane->name,
  8984. fb->base.id, fb->width, fb->height,
  8985. drm_get_format_name(fb->format->format, &format_name));
  8986. if (INTEL_GEN(dev_priv) >= 9)
  8987. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  8988. state->scaler_id,
  8989. state->base.src.x1 >> 16,
  8990. state->base.src.y1 >> 16,
  8991. drm_rect_width(&state->base.src) >> 16,
  8992. drm_rect_height(&state->base.src) >> 16,
  8993. state->base.dst.x1, state->base.dst.y1,
  8994. drm_rect_width(&state->base.dst),
  8995. drm_rect_height(&state->base.dst));
  8996. }
  8997. }
  8998. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  8999. {
  9000. struct drm_device *dev = state->dev;
  9001. struct drm_connector *connector;
  9002. struct drm_connector_list_iter conn_iter;
  9003. unsigned int used_ports = 0;
  9004. unsigned int used_mst_ports = 0;
  9005. /*
  9006. * Walk the connector list instead of the encoder
  9007. * list to detect the problem on ddi platforms
  9008. * where there's just one encoder per digital port.
  9009. */
  9010. drm_connector_list_iter_begin(dev, &conn_iter);
  9011. drm_for_each_connector_iter(connector, &conn_iter) {
  9012. struct drm_connector_state *connector_state;
  9013. struct intel_encoder *encoder;
  9014. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9015. if (!connector_state)
  9016. connector_state = connector->state;
  9017. if (!connector_state->best_encoder)
  9018. continue;
  9019. encoder = to_intel_encoder(connector_state->best_encoder);
  9020. WARN_ON(!connector_state->crtc);
  9021. switch (encoder->type) {
  9022. unsigned int port_mask;
  9023. case INTEL_OUTPUT_UNKNOWN:
  9024. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9025. break;
  9026. case INTEL_OUTPUT_DP:
  9027. case INTEL_OUTPUT_HDMI:
  9028. case INTEL_OUTPUT_EDP:
  9029. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9030. /* the same port mustn't appear more than once */
  9031. if (used_ports & port_mask)
  9032. return false;
  9033. used_ports |= port_mask;
  9034. break;
  9035. case INTEL_OUTPUT_DP_MST:
  9036. used_mst_ports |=
  9037. 1 << enc_to_mst(&encoder->base)->primary->port;
  9038. break;
  9039. default:
  9040. break;
  9041. }
  9042. }
  9043. drm_connector_list_iter_end(&conn_iter);
  9044. /* can't mix MST and SST/HDMI on the same port */
  9045. if (used_ports & used_mst_ports)
  9046. return false;
  9047. return true;
  9048. }
  9049. static void
  9050. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9051. {
  9052. struct drm_i915_private *dev_priv =
  9053. to_i915(crtc_state->base.crtc->dev);
  9054. struct intel_crtc_scaler_state scaler_state;
  9055. struct intel_dpll_hw_state dpll_hw_state;
  9056. struct intel_shared_dpll *shared_dpll;
  9057. struct intel_crtc_wm_state wm_state;
  9058. bool force_thru;
  9059. /* FIXME: before the switch to atomic started, a new pipe_config was
  9060. * kzalloc'd. Code that depends on any field being zero should be
  9061. * fixed, so that the crtc_state can be safely duplicated. For now,
  9062. * only fields that are know to not cause problems are preserved. */
  9063. scaler_state = crtc_state->scaler_state;
  9064. shared_dpll = crtc_state->shared_dpll;
  9065. dpll_hw_state = crtc_state->dpll_hw_state;
  9066. force_thru = crtc_state->pch_pfit.force_thru;
  9067. if (IS_G4X(dev_priv) ||
  9068. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9069. wm_state = crtc_state->wm;
  9070. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9071. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9072. memset(&crtc_state->base + 1, 0,
  9073. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9074. crtc_state->scaler_state = scaler_state;
  9075. crtc_state->shared_dpll = shared_dpll;
  9076. crtc_state->dpll_hw_state = dpll_hw_state;
  9077. crtc_state->pch_pfit.force_thru = force_thru;
  9078. if (IS_G4X(dev_priv) ||
  9079. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9080. crtc_state->wm = wm_state;
  9081. }
  9082. static int
  9083. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9084. struct intel_crtc_state *pipe_config)
  9085. {
  9086. struct drm_atomic_state *state = pipe_config->base.state;
  9087. struct intel_encoder *encoder;
  9088. struct drm_connector *connector;
  9089. struct drm_connector_state *connector_state;
  9090. int base_bpp, ret = -EINVAL;
  9091. int i;
  9092. bool retry = true;
  9093. clear_intel_crtc_state(pipe_config);
  9094. pipe_config->cpu_transcoder =
  9095. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9096. /*
  9097. * Sanitize sync polarity flags based on requested ones. If neither
  9098. * positive or negative polarity is requested, treat this as meaning
  9099. * negative polarity.
  9100. */
  9101. if (!(pipe_config->base.adjusted_mode.flags &
  9102. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9103. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9104. if (!(pipe_config->base.adjusted_mode.flags &
  9105. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9106. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9107. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9108. pipe_config);
  9109. if (base_bpp < 0)
  9110. goto fail;
  9111. /*
  9112. * Determine the real pipe dimensions. Note that stereo modes can
  9113. * increase the actual pipe size due to the frame doubling and
  9114. * insertion of additional space for blanks between the frame. This
  9115. * is stored in the crtc timings. We use the requested mode to do this
  9116. * computation to clearly distinguish it from the adjusted mode, which
  9117. * can be changed by the connectors in the below retry loop.
  9118. */
  9119. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9120. &pipe_config->pipe_src_w,
  9121. &pipe_config->pipe_src_h);
  9122. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9123. if (connector_state->crtc != crtc)
  9124. continue;
  9125. encoder = to_intel_encoder(connector_state->best_encoder);
  9126. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9127. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9128. goto fail;
  9129. }
  9130. /*
  9131. * Determine output_types before calling the .compute_config()
  9132. * hooks so that the hooks can use this information safely.
  9133. */
  9134. pipe_config->output_types |= 1 << encoder->type;
  9135. }
  9136. encoder_retry:
  9137. /* Ensure the port clock defaults are reset when retrying. */
  9138. pipe_config->port_clock = 0;
  9139. pipe_config->pixel_multiplier = 1;
  9140. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9141. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9142. CRTC_STEREO_DOUBLE);
  9143. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9144. * adjust it according to limitations or connector properties, and also
  9145. * a chance to reject the mode entirely.
  9146. */
  9147. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9148. if (connector_state->crtc != crtc)
  9149. continue;
  9150. encoder = to_intel_encoder(connector_state->best_encoder);
  9151. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9152. DRM_DEBUG_KMS("Encoder config failure\n");
  9153. goto fail;
  9154. }
  9155. }
  9156. /* Set default port clock if not overwritten by the encoder. Needs to be
  9157. * done afterwards in case the encoder adjusts the mode. */
  9158. if (!pipe_config->port_clock)
  9159. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9160. * pipe_config->pixel_multiplier;
  9161. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9162. if (ret < 0) {
  9163. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9164. goto fail;
  9165. }
  9166. if (ret == RETRY) {
  9167. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9168. ret = -EINVAL;
  9169. goto fail;
  9170. }
  9171. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9172. retry = false;
  9173. goto encoder_retry;
  9174. }
  9175. /* Dithering seems to not pass-through bits correctly when it should, so
  9176. * only enable it on 6bpc panels and when its not a compliance
  9177. * test requesting 6bpc video pattern.
  9178. */
  9179. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9180. !pipe_config->dither_force_disable;
  9181. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9182. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9183. fail:
  9184. return ret;
  9185. }
  9186. static void
  9187. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9188. {
  9189. struct drm_crtc *crtc;
  9190. struct drm_crtc_state *new_crtc_state;
  9191. int i;
  9192. /* Double check state. */
  9193. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9194. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9195. /*
  9196. * Update legacy state to satisfy fbc code. This can
  9197. * be removed when fbc uses the atomic state.
  9198. */
  9199. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9200. struct drm_plane_state *plane_state = crtc->primary->state;
  9201. crtc->primary->fb = plane_state->fb;
  9202. crtc->x = plane_state->src_x >> 16;
  9203. crtc->y = plane_state->src_y >> 16;
  9204. }
  9205. }
  9206. }
  9207. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9208. {
  9209. int diff;
  9210. if (clock1 == clock2)
  9211. return true;
  9212. if (!clock1 || !clock2)
  9213. return false;
  9214. diff = abs(clock1 - clock2);
  9215. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9216. return true;
  9217. return false;
  9218. }
  9219. static bool
  9220. intel_compare_m_n(unsigned int m, unsigned int n,
  9221. unsigned int m2, unsigned int n2,
  9222. bool exact)
  9223. {
  9224. if (m == m2 && n == n2)
  9225. return true;
  9226. if (exact || !m || !n || !m2 || !n2)
  9227. return false;
  9228. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9229. if (n > n2) {
  9230. while (n > n2) {
  9231. m2 <<= 1;
  9232. n2 <<= 1;
  9233. }
  9234. } else if (n < n2) {
  9235. while (n < n2) {
  9236. m <<= 1;
  9237. n <<= 1;
  9238. }
  9239. }
  9240. if (n != n2)
  9241. return false;
  9242. return intel_fuzzy_clock_check(m, m2);
  9243. }
  9244. static bool
  9245. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9246. struct intel_link_m_n *m2_n2,
  9247. bool adjust)
  9248. {
  9249. if (m_n->tu == m2_n2->tu &&
  9250. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9251. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9252. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9253. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9254. if (adjust)
  9255. *m2_n2 = *m_n;
  9256. return true;
  9257. }
  9258. return false;
  9259. }
  9260. static void __printf(3, 4)
  9261. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9262. {
  9263. char *level;
  9264. unsigned int category;
  9265. struct va_format vaf;
  9266. va_list args;
  9267. if (adjust) {
  9268. level = KERN_DEBUG;
  9269. category = DRM_UT_KMS;
  9270. } else {
  9271. level = KERN_ERR;
  9272. category = DRM_UT_NONE;
  9273. }
  9274. va_start(args, format);
  9275. vaf.fmt = format;
  9276. vaf.va = &args;
  9277. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9278. va_end(args);
  9279. }
  9280. static bool
  9281. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9282. struct intel_crtc_state *current_config,
  9283. struct intel_crtc_state *pipe_config,
  9284. bool adjust)
  9285. {
  9286. bool ret = true;
  9287. #define PIPE_CONF_CHECK_X(name) \
  9288. if (current_config->name != pipe_config->name) { \
  9289. pipe_config_err(adjust, __stringify(name), \
  9290. "(expected 0x%08x, found 0x%08x)\n", \
  9291. current_config->name, \
  9292. pipe_config->name); \
  9293. ret = false; \
  9294. }
  9295. #define PIPE_CONF_CHECK_I(name) \
  9296. if (current_config->name != pipe_config->name) { \
  9297. pipe_config_err(adjust, __stringify(name), \
  9298. "(expected %i, found %i)\n", \
  9299. current_config->name, \
  9300. pipe_config->name); \
  9301. ret = false; \
  9302. }
  9303. #define PIPE_CONF_CHECK_P(name) \
  9304. if (current_config->name != pipe_config->name) { \
  9305. pipe_config_err(adjust, __stringify(name), \
  9306. "(expected %p, found %p)\n", \
  9307. current_config->name, \
  9308. pipe_config->name); \
  9309. ret = false; \
  9310. }
  9311. #define PIPE_CONF_CHECK_M_N(name) \
  9312. if (!intel_compare_link_m_n(&current_config->name, \
  9313. &pipe_config->name,\
  9314. adjust)) { \
  9315. pipe_config_err(adjust, __stringify(name), \
  9316. "(expected tu %i gmch %i/%i link %i/%i, " \
  9317. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9318. current_config->name.tu, \
  9319. current_config->name.gmch_m, \
  9320. current_config->name.gmch_n, \
  9321. current_config->name.link_m, \
  9322. current_config->name.link_n, \
  9323. pipe_config->name.tu, \
  9324. pipe_config->name.gmch_m, \
  9325. pipe_config->name.gmch_n, \
  9326. pipe_config->name.link_m, \
  9327. pipe_config->name.link_n); \
  9328. ret = false; \
  9329. }
  9330. /* This is required for BDW+ where there is only one set of registers for
  9331. * switching between high and low RR.
  9332. * This macro can be used whenever a comparison has to be made between one
  9333. * hw state and multiple sw state variables.
  9334. */
  9335. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9336. if (!intel_compare_link_m_n(&current_config->name, \
  9337. &pipe_config->name, adjust) && \
  9338. !intel_compare_link_m_n(&current_config->alt_name, \
  9339. &pipe_config->name, adjust)) { \
  9340. pipe_config_err(adjust, __stringify(name), \
  9341. "(expected tu %i gmch %i/%i link %i/%i, " \
  9342. "or tu %i gmch %i/%i link %i/%i, " \
  9343. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9344. current_config->name.tu, \
  9345. current_config->name.gmch_m, \
  9346. current_config->name.gmch_n, \
  9347. current_config->name.link_m, \
  9348. current_config->name.link_n, \
  9349. current_config->alt_name.tu, \
  9350. current_config->alt_name.gmch_m, \
  9351. current_config->alt_name.gmch_n, \
  9352. current_config->alt_name.link_m, \
  9353. current_config->alt_name.link_n, \
  9354. pipe_config->name.tu, \
  9355. pipe_config->name.gmch_m, \
  9356. pipe_config->name.gmch_n, \
  9357. pipe_config->name.link_m, \
  9358. pipe_config->name.link_n); \
  9359. ret = false; \
  9360. }
  9361. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9362. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9363. pipe_config_err(adjust, __stringify(name), \
  9364. "(%x) (expected %i, found %i)\n", \
  9365. (mask), \
  9366. current_config->name & (mask), \
  9367. pipe_config->name & (mask)); \
  9368. ret = false; \
  9369. }
  9370. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9371. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9372. pipe_config_err(adjust, __stringify(name), \
  9373. "(expected %i, found %i)\n", \
  9374. current_config->name, \
  9375. pipe_config->name); \
  9376. ret = false; \
  9377. }
  9378. #define PIPE_CONF_QUIRK(quirk) \
  9379. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9380. PIPE_CONF_CHECK_I(cpu_transcoder);
  9381. PIPE_CONF_CHECK_I(has_pch_encoder);
  9382. PIPE_CONF_CHECK_I(fdi_lanes);
  9383. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9384. PIPE_CONF_CHECK_I(lane_count);
  9385. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9386. if (INTEL_GEN(dev_priv) < 8) {
  9387. PIPE_CONF_CHECK_M_N(dp_m_n);
  9388. if (current_config->has_drrs)
  9389. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9390. } else
  9391. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9392. PIPE_CONF_CHECK_X(output_types);
  9393. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9394. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9395. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9396. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9397. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9398. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9399. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9400. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9401. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9402. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9403. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9404. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9405. PIPE_CONF_CHECK_I(pixel_multiplier);
  9406. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9407. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9408. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9409. PIPE_CONF_CHECK_I(limited_color_range);
  9410. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9411. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9412. PIPE_CONF_CHECK_I(has_infoframe);
  9413. PIPE_CONF_CHECK_I(ycbcr420);
  9414. PIPE_CONF_CHECK_I(has_audio);
  9415. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9416. DRM_MODE_FLAG_INTERLACE);
  9417. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9418. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9419. DRM_MODE_FLAG_PHSYNC);
  9420. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9421. DRM_MODE_FLAG_NHSYNC);
  9422. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9423. DRM_MODE_FLAG_PVSYNC);
  9424. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9425. DRM_MODE_FLAG_NVSYNC);
  9426. }
  9427. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9428. /* pfit ratios are autocomputed by the hw on gen4+ */
  9429. if (INTEL_GEN(dev_priv) < 4)
  9430. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9431. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9432. if (!adjust) {
  9433. PIPE_CONF_CHECK_I(pipe_src_w);
  9434. PIPE_CONF_CHECK_I(pipe_src_h);
  9435. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9436. if (current_config->pch_pfit.enabled) {
  9437. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9438. PIPE_CONF_CHECK_X(pch_pfit.size);
  9439. }
  9440. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9441. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9442. }
  9443. /* BDW+ don't expose a synchronous way to read the state */
  9444. if (IS_HASWELL(dev_priv))
  9445. PIPE_CONF_CHECK_I(ips_enabled);
  9446. PIPE_CONF_CHECK_I(double_wide);
  9447. PIPE_CONF_CHECK_P(shared_dpll);
  9448. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9449. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9450. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9451. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9452. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9453. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9454. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9455. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9456. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9457. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9458. PIPE_CONF_CHECK_X(dsi_pll.div);
  9459. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9460. PIPE_CONF_CHECK_I(pipe_bpp);
  9461. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9462. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9463. #undef PIPE_CONF_CHECK_X
  9464. #undef PIPE_CONF_CHECK_I
  9465. #undef PIPE_CONF_CHECK_P
  9466. #undef PIPE_CONF_CHECK_FLAGS
  9467. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9468. #undef PIPE_CONF_QUIRK
  9469. return ret;
  9470. }
  9471. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9472. const struct intel_crtc_state *pipe_config)
  9473. {
  9474. if (pipe_config->has_pch_encoder) {
  9475. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9476. &pipe_config->fdi_m_n);
  9477. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9478. /*
  9479. * FDI already provided one idea for the dotclock.
  9480. * Yell if the encoder disagrees.
  9481. */
  9482. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9483. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9484. fdi_dotclock, dotclock);
  9485. }
  9486. }
  9487. static void verify_wm_state(struct drm_crtc *crtc,
  9488. struct drm_crtc_state *new_state)
  9489. {
  9490. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9491. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9492. struct skl_pipe_wm hw_wm, *sw_wm;
  9493. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9494. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9496. const enum pipe pipe = intel_crtc->pipe;
  9497. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9498. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9499. return;
  9500. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9501. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9502. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9503. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9504. /* planes */
  9505. for_each_universal_plane(dev_priv, pipe, plane) {
  9506. hw_plane_wm = &hw_wm.planes[plane];
  9507. sw_plane_wm = &sw_wm->planes[plane];
  9508. /* Watermarks */
  9509. for (level = 0; level <= max_level; level++) {
  9510. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9511. &sw_plane_wm->wm[level]))
  9512. continue;
  9513. 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",
  9514. pipe_name(pipe), plane + 1, level,
  9515. sw_plane_wm->wm[level].plane_en,
  9516. sw_plane_wm->wm[level].plane_res_b,
  9517. sw_plane_wm->wm[level].plane_res_l,
  9518. hw_plane_wm->wm[level].plane_en,
  9519. hw_plane_wm->wm[level].plane_res_b,
  9520. hw_plane_wm->wm[level].plane_res_l);
  9521. }
  9522. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9523. &sw_plane_wm->trans_wm)) {
  9524. 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",
  9525. pipe_name(pipe), plane + 1,
  9526. sw_plane_wm->trans_wm.plane_en,
  9527. sw_plane_wm->trans_wm.plane_res_b,
  9528. sw_plane_wm->trans_wm.plane_res_l,
  9529. hw_plane_wm->trans_wm.plane_en,
  9530. hw_plane_wm->trans_wm.plane_res_b,
  9531. hw_plane_wm->trans_wm.plane_res_l);
  9532. }
  9533. /* DDB */
  9534. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9535. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9536. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9537. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9538. pipe_name(pipe), plane + 1,
  9539. sw_ddb_entry->start, sw_ddb_entry->end,
  9540. hw_ddb_entry->start, hw_ddb_entry->end);
  9541. }
  9542. }
  9543. /*
  9544. * cursor
  9545. * If the cursor plane isn't active, we may not have updated it's ddb
  9546. * allocation. In that case since the ddb allocation will be updated
  9547. * once the plane becomes visible, we can skip this check
  9548. */
  9549. if (1) {
  9550. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9551. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9552. /* Watermarks */
  9553. for (level = 0; level <= max_level; level++) {
  9554. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9555. &sw_plane_wm->wm[level]))
  9556. continue;
  9557. 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",
  9558. pipe_name(pipe), level,
  9559. sw_plane_wm->wm[level].plane_en,
  9560. sw_plane_wm->wm[level].plane_res_b,
  9561. sw_plane_wm->wm[level].plane_res_l,
  9562. hw_plane_wm->wm[level].plane_en,
  9563. hw_plane_wm->wm[level].plane_res_b,
  9564. hw_plane_wm->wm[level].plane_res_l);
  9565. }
  9566. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9567. &sw_plane_wm->trans_wm)) {
  9568. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9569. pipe_name(pipe),
  9570. sw_plane_wm->trans_wm.plane_en,
  9571. sw_plane_wm->trans_wm.plane_res_b,
  9572. sw_plane_wm->trans_wm.plane_res_l,
  9573. hw_plane_wm->trans_wm.plane_en,
  9574. hw_plane_wm->trans_wm.plane_res_b,
  9575. hw_plane_wm->trans_wm.plane_res_l);
  9576. }
  9577. /* DDB */
  9578. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9579. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9580. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9581. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9582. pipe_name(pipe),
  9583. sw_ddb_entry->start, sw_ddb_entry->end,
  9584. hw_ddb_entry->start, hw_ddb_entry->end);
  9585. }
  9586. }
  9587. }
  9588. static void
  9589. verify_connector_state(struct drm_device *dev,
  9590. struct drm_atomic_state *state,
  9591. struct drm_crtc *crtc)
  9592. {
  9593. struct drm_connector *connector;
  9594. struct drm_connector_state *new_conn_state;
  9595. int i;
  9596. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9597. struct drm_encoder *encoder = connector->encoder;
  9598. struct drm_crtc_state *crtc_state = NULL;
  9599. if (new_conn_state->crtc != crtc)
  9600. continue;
  9601. if (crtc)
  9602. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9603. intel_connector_verify_state(crtc_state, new_conn_state);
  9604. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9605. "connector's atomic encoder doesn't match legacy encoder\n");
  9606. }
  9607. }
  9608. static void
  9609. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9610. {
  9611. struct intel_encoder *encoder;
  9612. struct drm_connector *connector;
  9613. struct drm_connector_state *old_conn_state, *new_conn_state;
  9614. int i;
  9615. for_each_intel_encoder(dev, encoder) {
  9616. bool enabled = false, found = false;
  9617. enum pipe pipe;
  9618. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9619. encoder->base.base.id,
  9620. encoder->base.name);
  9621. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9622. new_conn_state, i) {
  9623. if (old_conn_state->best_encoder == &encoder->base)
  9624. found = true;
  9625. if (new_conn_state->best_encoder != &encoder->base)
  9626. continue;
  9627. found = enabled = true;
  9628. I915_STATE_WARN(new_conn_state->crtc !=
  9629. encoder->base.crtc,
  9630. "connector's crtc doesn't match encoder crtc\n");
  9631. }
  9632. if (!found)
  9633. continue;
  9634. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9635. "encoder's enabled state mismatch "
  9636. "(expected %i, found %i)\n",
  9637. !!encoder->base.crtc, enabled);
  9638. if (!encoder->base.crtc) {
  9639. bool active;
  9640. active = encoder->get_hw_state(encoder, &pipe);
  9641. I915_STATE_WARN(active,
  9642. "encoder detached but still enabled on pipe %c.\n",
  9643. pipe_name(pipe));
  9644. }
  9645. }
  9646. }
  9647. static void
  9648. verify_crtc_state(struct drm_crtc *crtc,
  9649. struct drm_crtc_state *old_crtc_state,
  9650. struct drm_crtc_state *new_crtc_state)
  9651. {
  9652. struct drm_device *dev = crtc->dev;
  9653. struct drm_i915_private *dev_priv = to_i915(dev);
  9654. struct intel_encoder *encoder;
  9655. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9656. struct intel_crtc_state *pipe_config, *sw_config;
  9657. struct drm_atomic_state *old_state;
  9658. bool active;
  9659. old_state = old_crtc_state->state;
  9660. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9661. pipe_config = to_intel_crtc_state(old_crtc_state);
  9662. memset(pipe_config, 0, sizeof(*pipe_config));
  9663. pipe_config->base.crtc = crtc;
  9664. pipe_config->base.state = old_state;
  9665. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9666. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9667. /* we keep both pipes enabled on 830 */
  9668. if (IS_I830(dev_priv))
  9669. active = new_crtc_state->active;
  9670. I915_STATE_WARN(new_crtc_state->active != active,
  9671. "crtc active state doesn't match with hw state "
  9672. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9673. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9674. "transitional active state does not match atomic hw state "
  9675. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9676. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9677. enum pipe pipe;
  9678. active = encoder->get_hw_state(encoder, &pipe);
  9679. I915_STATE_WARN(active != new_crtc_state->active,
  9680. "[ENCODER:%i] active %i with crtc active %i\n",
  9681. encoder->base.base.id, active, new_crtc_state->active);
  9682. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9683. "Encoder connected to wrong pipe %c\n",
  9684. pipe_name(pipe));
  9685. if (active) {
  9686. pipe_config->output_types |= 1 << encoder->type;
  9687. encoder->get_config(encoder, pipe_config);
  9688. }
  9689. }
  9690. intel_crtc_compute_pixel_rate(pipe_config);
  9691. if (!new_crtc_state->active)
  9692. return;
  9693. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9694. sw_config = to_intel_crtc_state(new_crtc_state);
  9695. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9696. pipe_config, false)) {
  9697. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9698. intel_dump_pipe_config(intel_crtc, pipe_config,
  9699. "[hw state]");
  9700. intel_dump_pipe_config(intel_crtc, sw_config,
  9701. "[sw state]");
  9702. }
  9703. }
  9704. static void
  9705. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9706. struct intel_shared_dpll *pll,
  9707. struct drm_crtc *crtc,
  9708. struct drm_crtc_state *new_state)
  9709. {
  9710. struct intel_dpll_hw_state dpll_hw_state;
  9711. unsigned crtc_mask;
  9712. bool active;
  9713. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9714. DRM_DEBUG_KMS("%s\n", pll->name);
  9715. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9716. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9717. I915_STATE_WARN(!pll->on && pll->active_mask,
  9718. "pll in active use but not on in sw tracking\n");
  9719. I915_STATE_WARN(pll->on && !pll->active_mask,
  9720. "pll is on but not used by any active crtc\n");
  9721. I915_STATE_WARN(pll->on != active,
  9722. "pll on state mismatch (expected %i, found %i)\n",
  9723. pll->on, active);
  9724. }
  9725. if (!crtc) {
  9726. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9727. "more active pll users than references: %x vs %x\n",
  9728. pll->active_mask, pll->state.crtc_mask);
  9729. return;
  9730. }
  9731. crtc_mask = 1 << drm_crtc_index(crtc);
  9732. if (new_state->active)
  9733. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9734. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9735. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9736. else
  9737. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9738. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9739. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9740. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9741. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9742. crtc_mask, pll->state.crtc_mask);
  9743. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9744. &dpll_hw_state,
  9745. sizeof(dpll_hw_state)),
  9746. "pll hw state mismatch\n");
  9747. }
  9748. static void
  9749. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9750. struct drm_crtc_state *old_crtc_state,
  9751. struct drm_crtc_state *new_crtc_state)
  9752. {
  9753. struct drm_i915_private *dev_priv = to_i915(dev);
  9754. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9755. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9756. if (new_state->shared_dpll)
  9757. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9758. if (old_state->shared_dpll &&
  9759. old_state->shared_dpll != new_state->shared_dpll) {
  9760. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9761. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9762. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9763. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9764. pipe_name(drm_crtc_index(crtc)));
  9765. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9766. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9767. pipe_name(drm_crtc_index(crtc)));
  9768. }
  9769. }
  9770. static void
  9771. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9772. struct drm_atomic_state *state,
  9773. struct drm_crtc_state *old_state,
  9774. struct drm_crtc_state *new_state)
  9775. {
  9776. if (!needs_modeset(new_state) &&
  9777. !to_intel_crtc_state(new_state)->update_pipe)
  9778. return;
  9779. verify_wm_state(crtc, new_state);
  9780. verify_connector_state(crtc->dev, state, crtc);
  9781. verify_crtc_state(crtc, old_state, new_state);
  9782. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9783. }
  9784. static void
  9785. verify_disabled_dpll_state(struct drm_device *dev)
  9786. {
  9787. struct drm_i915_private *dev_priv = to_i915(dev);
  9788. int i;
  9789. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9790. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9791. }
  9792. static void
  9793. intel_modeset_verify_disabled(struct drm_device *dev,
  9794. struct drm_atomic_state *state)
  9795. {
  9796. verify_encoder_state(dev, state);
  9797. verify_connector_state(dev, state, NULL);
  9798. verify_disabled_dpll_state(dev);
  9799. }
  9800. static void update_scanline_offset(struct intel_crtc *crtc)
  9801. {
  9802. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9803. /*
  9804. * The scanline counter increments at the leading edge of hsync.
  9805. *
  9806. * On most platforms it starts counting from vtotal-1 on the
  9807. * first active line. That means the scanline counter value is
  9808. * always one less than what we would expect. Ie. just after
  9809. * start of vblank, which also occurs at start of hsync (on the
  9810. * last active line), the scanline counter will read vblank_start-1.
  9811. *
  9812. * On gen2 the scanline counter starts counting from 1 instead
  9813. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9814. * to keep the value positive), instead of adding one.
  9815. *
  9816. * On HSW+ the behaviour of the scanline counter depends on the output
  9817. * type. For DP ports it behaves like most other platforms, but on HDMI
  9818. * there's an extra 1 line difference. So we need to add two instead of
  9819. * one to the value.
  9820. *
  9821. * On VLV/CHV DSI the scanline counter would appear to increment
  9822. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9823. * that means we can't tell whether we're in vblank or not while
  9824. * we're on that particular line. We must still set scanline_offset
  9825. * to 1 so that the vblank timestamps come out correct when we query
  9826. * the scanline counter from within the vblank interrupt handler.
  9827. * However if queried just before the start of vblank we'll get an
  9828. * answer that's slightly in the future.
  9829. */
  9830. if (IS_GEN2(dev_priv)) {
  9831. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9832. int vtotal;
  9833. vtotal = adjusted_mode->crtc_vtotal;
  9834. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9835. vtotal /= 2;
  9836. crtc->scanline_offset = vtotal - 1;
  9837. } else if (HAS_DDI(dev_priv) &&
  9838. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9839. crtc->scanline_offset = 2;
  9840. } else
  9841. crtc->scanline_offset = 1;
  9842. }
  9843. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9844. {
  9845. struct drm_device *dev = state->dev;
  9846. struct drm_i915_private *dev_priv = to_i915(dev);
  9847. struct drm_crtc *crtc;
  9848. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9849. int i;
  9850. if (!dev_priv->display.crtc_compute_clock)
  9851. return;
  9852. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9853. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9854. struct intel_shared_dpll *old_dpll =
  9855. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9856. if (!needs_modeset(new_crtc_state))
  9857. continue;
  9858. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9859. if (!old_dpll)
  9860. continue;
  9861. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9862. }
  9863. }
  9864. /*
  9865. * This implements the workaround described in the "notes" section of the mode
  9866. * set sequence documentation. When going from no pipes or single pipe to
  9867. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9868. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9869. */
  9870. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9871. {
  9872. struct drm_crtc_state *crtc_state;
  9873. struct intel_crtc *intel_crtc;
  9874. struct drm_crtc *crtc;
  9875. struct intel_crtc_state *first_crtc_state = NULL;
  9876. struct intel_crtc_state *other_crtc_state = NULL;
  9877. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9878. int i;
  9879. /* look at all crtc's that are going to be enabled in during modeset */
  9880. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9881. intel_crtc = to_intel_crtc(crtc);
  9882. if (!crtc_state->active || !needs_modeset(crtc_state))
  9883. continue;
  9884. if (first_crtc_state) {
  9885. other_crtc_state = to_intel_crtc_state(crtc_state);
  9886. break;
  9887. } else {
  9888. first_crtc_state = to_intel_crtc_state(crtc_state);
  9889. first_pipe = intel_crtc->pipe;
  9890. }
  9891. }
  9892. /* No workaround needed? */
  9893. if (!first_crtc_state)
  9894. return 0;
  9895. /* w/a possibly needed, check how many crtc's are already enabled. */
  9896. for_each_intel_crtc(state->dev, intel_crtc) {
  9897. struct intel_crtc_state *pipe_config;
  9898. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9899. if (IS_ERR(pipe_config))
  9900. return PTR_ERR(pipe_config);
  9901. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9902. if (!pipe_config->base.active ||
  9903. needs_modeset(&pipe_config->base))
  9904. continue;
  9905. /* 2 or more enabled crtcs means no need for w/a */
  9906. if (enabled_pipe != INVALID_PIPE)
  9907. return 0;
  9908. enabled_pipe = intel_crtc->pipe;
  9909. }
  9910. if (enabled_pipe != INVALID_PIPE)
  9911. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9912. else if (other_crtc_state)
  9913. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9914. return 0;
  9915. }
  9916. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9917. {
  9918. struct drm_crtc *crtc;
  9919. /* Add all pipes to the state */
  9920. for_each_crtc(state->dev, crtc) {
  9921. struct drm_crtc_state *crtc_state;
  9922. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9923. if (IS_ERR(crtc_state))
  9924. return PTR_ERR(crtc_state);
  9925. }
  9926. return 0;
  9927. }
  9928. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9929. {
  9930. struct drm_crtc *crtc;
  9931. /*
  9932. * Add all pipes to the state, and force
  9933. * a modeset on all the active ones.
  9934. */
  9935. for_each_crtc(state->dev, crtc) {
  9936. struct drm_crtc_state *crtc_state;
  9937. int ret;
  9938. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9939. if (IS_ERR(crtc_state))
  9940. return PTR_ERR(crtc_state);
  9941. if (!crtc_state->active || needs_modeset(crtc_state))
  9942. continue;
  9943. crtc_state->mode_changed = true;
  9944. ret = drm_atomic_add_affected_connectors(state, crtc);
  9945. if (ret)
  9946. return ret;
  9947. ret = drm_atomic_add_affected_planes(state, crtc);
  9948. if (ret)
  9949. return ret;
  9950. }
  9951. return 0;
  9952. }
  9953. static int intel_modeset_checks(struct drm_atomic_state *state)
  9954. {
  9955. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9956. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9957. struct drm_crtc *crtc;
  9958. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9959. int ret = 0, i;
  9960. if (!check_digital_port_conflicts(state)) {
  9961. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9962. return -EINVAL;
  9963. }
  9964. intel_state->modeset = true;
  9965. intel_state->active_crtcs = dev_priv->active_crtcs;
  9966. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9967. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  9968. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9969. if (new_crtc_state->active)
  9970. intel_state->active_crtcs |= 1 << i;
  9971. else
  9972. intel_state->active_crtcs &= ~(1 << i);
  9973. if (old_crtc_state->active != new_crtc_state->active)
  9974. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  9975. }
  9976. /*
  9977. * See if the config requires any additional preparation, e.g.
  9978. * to adjust global state with pipes off. We need to do this
  9979. * here so we can get the modeset_pipe updated config for the new
  9980. * mode set on this crtc. For other crtcs we need to use the
  9981. * adjusted_mode bits in the crtc directly.
  9982. */
  9983. if (dev_priv->display.modeset_calc_cdclk) {
  9984. ret = dev_priv->display.modeset_calc_cdclk(state);
  9985. if (ret < 0)
  9986. return ret;
  9987. /*
  9988. * Writes to dev_priv->cdclk.logical must protected by
  9989. * holding all the crtc locks, even if we don't end up
  9990. * touching the hardware
  9991. */
  9992. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  9993. &intel_state->cdclk.logical)) {
  9994. ret = intel_lock_all_pipes(state);
  9995. if (ret < 0)
  9996. return ret;
  9997. }
  9998. /* All pipes must be switched off while we change the cdclk. */
  9999. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10000. &intel_state->cdclk.actual)) {
  10001. ret = intel_modeset_all_pipes(state);
  10002. if (ret < 0)
  10003. return ret;
  10004. }
  10005. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10006. intel_state->cdclk.logical.cdclk,
  10007. intel_state->cdclk.actual.cdclk);
  10008. } else {
  10009. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10010. }
  10011. intel_modeset_clear_plls(state);
  10012. if (IS_HASWELL(dev_priv))
  10013. return haswell_mode_set_planes_workaround(state);
  10014. return 0;
  10015. }
  10016. /*
  10017. * Handle calculation of various watermark data at the end of the atomic check
  10018. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10019. * handlers to ensure that all derived state has been updated.
  10020. */
  10021. static int calc_watermark_data(struct drm_atomic_state *state)
  10022. {
  10023. struct drm_device *dev = state->dev;
  10024. struct drm_i915_private *dev_priv = to_i915(dev);
  10025. /* Is there platform-specific watermark information to calculate? */
  10026. if (dev_priv->display.compute_global_watermarks)
  10027. return dev_priv->display.compute_global_watermarks(state);
  10028. return 0;
  10029. }
  10030. /**
  10031. * intel_atomic_check - validate state object
  10032. * @dev: drm device
  10033. * @state: state to validate
  10034. */
  10035. static int intel_atomic_check(struct drm_device *dev,
  10036. struct drm_atomic_state *state)
  10037. {
  10038. struct drm_i915_private *dev_priv = to_i915(dev);
  10039. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10040. struct drm_crtc *crtc;
  10041. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10042. int ret, i;
  10043. bool any_ms = false;
  10044. ret = drm_atomic_helper_check_modeset(dev, state);
  10045. if (ret)
  10046. return ret;
  10047. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10048. struct intel_crtc_state *pipe_config =
  10049. to_intel_crtc_state(crtc_state);
  10050. /* Catch I915_MODE_FLAG_INHERITED */
  10051. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10052. crtc_state->mode_changed = true;
  10053. if (!needs_modeset(crtc_state))
  10054. continue;
  10055. if (!crtc_state->enable) {
  10056. any_ms = true;
  10057. continue;
  10058. }
  10059. /* FIXME: For only active_changed we shouldn't need to do any
  10060. * state recomputation at all. */
  10061. ret = drm_atomic_add_affected_connectors(state, crtc);
  10062. if (ret)
  10063. return ret;
  10064. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10065. if (ret) {
  10066. intel_dump_pipe_config(to_intel_crtc(crtc),
  10067. pipe_config, "[failed]");
  10068. return ret;
  10069. }
  10070. if (i915.fastboot &&
  10071. intel_pipe_config_compare(dev_priv,
  10072. to_intel_crtc_state(old_crtc_state),
  10073. pipe_config, true)) {
  10074. crtc_state->mode_changed = false;
  10075. pipe_config->update_pipe = true;
  10076. }
  10077. if (needs_modeset(crtc_state))
  10078. any_ms = true;
  10079. ret = drm_atomic_add_affected_planes(state, crtc);
  10080. if (ret)
  10081. return ret;
  10082. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10083. needs_modeset(crtc_state) ?
  10084. "[modeset]" : "[fastset]");
  10085. }
  10086. if (any_ms) {
  10087. ret = intel_modeset_checks(state);
  10088. if (ret)
  10089. return ret;
  10090. } else {
  10091. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10092. }
  10093. ret = drm_atomic_helper_check_planes(dev, state);
  10094. if (ret)
  10095. return ret;
  10096. intel_fbc_choose_crtc(dev_priv, state);
  10097. return calc_watermark_data(state);
  10098. }
  10099. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10100. struct drm_atomic_state *state)
  10101. {
  10102. return drm_atomic_helper_prepare_planes(dev, state);
  10103. }
  10104. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10105. {
  10106. struct drm_device *dev = crtc->base.dev;
  10107. if (!dev->max_vblank_count)
  10108. return drm_crtc_accurate_vblank_count(&crtc->base);
  10109. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10110. }
  10111. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10112. struct drm_i915_private *dev_priv,
  10113. unsigned crtc_mask)
  10114. {
  10115. unsigned last_vblank_count[I915_MAX_PIPES];
  10116. enum pipe pipe;
  10117. int ret;
  10118. if (!crtc_mask)
  10119. return;
  10120. for_each_pipe(dev_priv, pipe) {
  10121. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10122. pipe);
  10123. if (!((1 << pipe) & crtc_mask))
  10124. continue;
  10125. ret = drm_crtc_vblank_get(&crtc->base);
  10126. if (WARN_ON(ret != 0)) {
  10127. crtc_mask &= ~(1 << pipe);
  10128. continue;
  10129. }
  10130. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10131. }
  10132. for_each_pipe(dev_priv, pipe) {
  10133. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10134. pipe);
  10135. long lret;
  10136. if (!((1 << pipe) & crtc_mask))
  10137. continue;
  10138. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10139. last_vblank_count[pipe] !=
  10140. drm_crtc_vblank_count(&crtc->base),
  10141. msecs_to_jiffies(50));
  10142. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10143. drm_crtc_vblank_put(&crtc->base);
  10144. }
  10145. }
  10146. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10147. {
  10148. /* fb updated, need to unpin old fb */
  10149. if (crtc_state->fb_changed)
  10150. return true;
  10151. /* wm changes, need vblank before final wm's */
  10152. if (crtc_state->update_wm_post)
  10153. return true;
  10154. if (crtc_state->wm.need_postvbl_update)
  10155. return true;
  10156. return false;
  10157. }
  10158. static void intel_update_crtc(struct drm_crtc *crtc,
  10159. struct drm_atomic_state *state,
  10160. struct drm_crtc_state *old_crtc_state,
  10161. struct drm_crtc_state *new_crtc_state,
  10162. unsigned int *crtc_vblank_mask)
  10163. {
  10164. struct drm_device *dev = crtc->dev;
  10165. struct drm_i915_private *dev_priv = to_i915(dev);
  10166. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10167. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10168. bool modeset = needs_modeset(new_crtc_state);
  10169. if (modeset) {
  10170. update_scanline_offset(intel_crtc);
  10171. dev_priv->display.crtc_enable(pipe_config, state);
  10172. } else {
  10173. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10174. pipe_config);
  10175. }
  10176. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10177. intel_fbc_enable(
  10178. intel_crtc, pipe_config,
  10179. to_intel_plane_state(crtc->primary->state));
  10180. }
  10181. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10182. if (needs_vblank_wait(pipe_config))
  10183. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10184. }
  10185. static void intel_update_crtcs(struct drm_atomic_state *state,
  10186. unsigned int *crtc_vblank_mask)
  10187. {
  10188. struct drm_crtc *crtc;
  10189. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10190. int i;
  10191. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10192. if (!new_crtc_state->active)
  10193. continue;
  10194. intel_update_crtc(crtc, state, old_crtc_state,
  10195. new_crtc_state, crtc_vblank_mask);
  10196. }
  10197. }
  10198. static void skl_update_crtcs(struct drm_atomic_state *state,
  10199. unsigned int *crtc_vblank_mask)
  10200. {
  10201. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10202. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10203. struct drm_crtc *crtc;
  10204. struct intel_crtc *intel_crtc;
  10205. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10206. struct intel_crtc_state *cstate;
  10207. unsigned int updated = 0;
  10208. bool progress;
  10209. enum pipe pipe;
  10210. int i;
  10211. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10212. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10213. /* ignore allocations for crtc's that have been turned off. */
  10214. if (new_crtc_state->active)
  10215. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10216. /*
  10217. * Whenever the number of active pipes changes, we need to make sure we
  10218. * update the pipes in the right order so that their ddb allocations
  10219. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10220. * cause pipe underruns and other bad stuff.
  10221. */
  10222. do {
  10223. progress = false;
  10224. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10225. bool vbl_wait = false;
  10226. unsigned int cmask = drm_crtc_mask(crtc);
  10227. intel_crtc = to_intel_crtc(crtc);
  10228. cstate = to_intel_crtc_state(crtc->state);
  10229. pipe = intel_crtc->pipe;
  10230. if (updated & cmask || !cstate->base.active)
  10231. continue;
  10232. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10233. continue;
  10234. updated |= cmask;
  10235. entries[i] = &cstate->wm.skl.ddb;
  10236. /*
  10237. * If this is an already active pipe, it's DDB changed,
  10238. * and this isn't the last pipe that needs updating
  10239. * then we need to wait for a vblank to pass for the
  10240. * new ddb allocation to take effect.
  10241. */
  10242. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10243. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10244. !new_crtc_state->active_changed &&
  10245. intel_state->wm_results.dirty_pipes != updated)
  10246. vbl_wait = true;
  10247. intel_update_crtc(crtc, state, old_crtc_state,
  10248. new_crtc_state, crtc_vblank_mask);
  10249. if (vbl_wait)
  10250. intel_wait_for_vblank(dev_priv, pipe);
  10251. progress = true;
  10252. }
  10253. } while (progress);
  10254. }
  10255. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10256. {
  10257. struct intel_atomic_state *state, *next;
  10258. struct llist_node *freed;
  10259. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10260. llist_for_each_entry_safe(state, next, freed, freed)
  10261. drm_atomic_state_put(&state->base);
  10262. }
  10263. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10264. {
  10265. struct drm_i915_private *dev_priv =
  10266. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10267. intel_atomic_helper_free_state(dev_priv);
  10268. }
  10269. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10270. {
  10271. struct drm_device *dev = state->dev;
  10272. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10273. struct drm_i915_private *dev_priv = to_i915(dev);
  10274. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10275. struct drm_crtc *crtc;
  10276. struct intel_crtc_state *intel_cstate;
  10277. bool hw_check = intel_state->modeset;
  10278. u64 put_domains[I915_MAX_PIPES] = {};
  10279. unsigned crtc_vblank_mask = 0;
  10280. int i;
  10281. drm_atomic_helper_wait_for_dependencies(state);
  10282. if (intel_state->modeset)
  10283. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10284. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10286. if (needs_modeset(new_crtc_state) ||
  10287. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10288. hw_check = true;
  10289. put_domains[to_intel_crtc(crtc)->pipe] =
  10290. modeset_get_crtc_power_domains(crtc,
  10291. to_intel_crtc_state(new_crtc_state));
  10292. }
  10293. if (!needs_modeset(new_crtc_state))
  10294. continue;
  10295. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10296. to_intel_crtc_state(new_crtc_state));
  10297. if (old_crtc_state->active) {
  10298. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10299. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10300. intel_crtc->active = false;
  10301. intel_fbc_disable(intel_crtc);
  10302. intel_disable_shared_dpll(intel_crtc);
  10303. /*
  10304. * Underruns don't always raise
  10305. * interrupts, so check manually.
  10306. */
  10307. intel_check_cpu_fifo_underruns(dev_priv);
  10308. intel_check_pch_fifo_underruns(dev_priv);
  10309. if (!crtc->state->active) {
  10310. /*
  10311. * Make sure we don't call initial_watermarks
  10312. * for ILK-style watermark updates.
  10313. *
  10314. * No clue what this is supposed to achieve.
  10315. */
  10316. if (INTEL_GEN(dev_priv) >= 9)
  10317. dev_priv->display.initial_watermarks(intel_state,
  10318. to_intel_crtc_state(crtc->state));
  10319. }
  10320. }
  10321. }
  10322. /* Only after disabling all output pipelines that will be changed can we
  10323. * update the the output configuration. */
  10324. intel_modeset_update_crtc_state(state);
  10325. if (intel_state->modeset) {
  10326. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10327. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10328. /*
  10329. * SKL workaround: bspec recommends we disable the SAGV when we
  10330. * have more then one pipe enabled
  10331. */
  10332. if (!intel_can_enable_sagv(state))
  10333. intel_disable_sagv(dev_priv);
  10334. intel_modeset_verify_disabled(dev, state);
  10335. }
  10336. /* Complete the events for pipes that have now been disabled */
  10337. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10338. bool modeset = needs_modeset(new_crtc_state);
  10339. /* Complete events for now disable pipes here. */
  10340. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10341. spin_lock_irq(&dev->event_lock);
  10342. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10343. spin_unlock_irq(&dev->event_lock);
  10344. new_crtc_state->event = NULL;
  10345. }
  10346. }
  10347. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10348. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10349. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10350. * already, but still need the state for the delayed optimization. To
  10351. * fix this:
  10352. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10353. * - schedule that vblank worker _before_ calling hw_done
  10354. * - at the start of commit_tail, cancel it _synchrously
  10355. * - switch over to the vblank wait helper in the core after that since
  10356. * we don't need out special handling any more.
  10357. */
  10358. if (!state->legacy_cursor_update)
  10359. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10360. /*
  10361. * Now that the vblank has passed, we can go ahead and program the
  10362. * optimal watermarks on platforms that need two-step watermark
  10363. * programming.
  10364. *
  10365. * TODO: Move this (and other cleanup) to an async worker eventually.
  10366. */
  10367. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10368. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10369. if (dev_priv->display.optimize_watermarks)
  10370. dev_priv->display.optimize_watermarks(intel_state,
  10371. intel_cstate);
  10372. }
  10373. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10374. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10375. if (put_domains[i])
  10376. modeset_put_power_domains(dev_priv, put_domains[i]);
  10377. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10378. }
  10379. if (intel_state->modeset && intel_can_enable_sagv(state))
  10380. intel_enable_sagv(dev_priv);
  10381. drm_atomic_helper_commit_hw_done(state);
  10382. if (intel_state->modeset) {
  10383. /* As one of the primary mmio accessors, KMS has a high
  10384. * likelihood of triggering bugs in unclaimed access. After we
  10385. * finish modesetting, see if an error has been flagged, and if
  10386. * so enable debugging for the next modeset - and hope we catch
  10387. * the culprit.
  10388. */
  10389. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10390. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10391. }
  10392. drm_atomic_helper_cleanup_planes(dev, state);
  10393. drm_atomic_helper_commit_cleanup_done(state);
  10394. drm_atomic_state_put(state);
  10395. intel_atomic_helper_free_state(dev_priv);
  10396. }
  10397. static void intel_atomic_commit_work(struct work_struct *work)
  10398. {
  10399. struct drm_atomic_state *state =
  10400. container_of(work, struct drm_atomic_state, commit_work);
  10401. intel_atomic_commit_tail(state);
  10402. }
  10403. static int __i915_sw_fence_call
  10404. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10405. enum i915_sw_fence_notify notify)
  10406. {
  10407. struct intel_atomic_state *state =
  10408. container_of(fence, struct intel_atomic_state, commit_ready);
  10409. switch (notify) {
  10410. case FENCE_COMPLETE:
  10411. if (state->base.commit_work.func)
  10412. queue_work(system_unbound_wq, &state->base.commit_work);
  10413. break;
  10414. case FENCE_FREE:
  10415. {
  10416. struct intel_atomic_helper *helper =
  10417. &to_i915(state->base.dev)->atomic_helper;
  10418. if (llist_add(&state->freed, &helper->free_list))
  10419. schedule_work(&helper->free_work);
  10420. break;
  10421. }
  10422. }
  10423. return NOTIFY_DONE;
  10424. }
  10425. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10426. {
  10427. struct drm_plane_state *old_plane_state, *new_plane_state;
  10428. struct drm_plane *plane;
  10429. int i;
  10430. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10431. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10432. intel_fb_obj(new_plane_state->fb),
  10433. to_intel_plane(plane)->frontbuffer_bit);
  10434. }
  10435. /**
  10436. * intel_atomic_commit - commit validated state object
  10437. * @dev: DRM device
  10438. * @state: the top-level driver state object
  10439. * @nonblock: nonblocking commit
  10440. *
  10441. * This function commits a top-level state object that has been validated
  10442. * with drm_atomic_helper_check().
  10443. *
  10444. * RETURNS
  10445. * Zero for success or -errno.
  10446. */
  10447. static int intel_atomic_commit(struct drm_device *dev,
  10448. struct drm_atomic_state *state,
  10449. bool nonblock)
  10450. {
  10451. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10452. struct drm_i915_private *dev_priv = to_i915(dev);
  10453. int ret = 0;
  10454. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10455. if (ret)
  10456. return ret;
  10457. drm_atomic_state_get(state);
  10458. i915_sw_fence_init(&intel_state->commit_ready,
  10459. intel_atomic_commit_ready);
  10460. ret = intel_atomic_prepare_commit(dev, state);
  10461. if (ret) {
  10462. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10463. i915_sw_fence_commit(&intel_state->commit_ready);
  10464. return ret;
  10465. }
  10466. /*
  10467. * The intel_legacy_cursor_update() fast path takes care
  10468. * of avoiding the vblank waits for simple cursor
  10469. * movement and flips. For cursor on/off and size changes,
  10470. * we want to perform the vblank waits so that watermark
  10471. * updates happen during the correct frames. Gen9+ have
  10472. * double buffered watermarks and so shouldn't need this.
  10473. *
  10474. * Do this after drm_atomic_helper_setup_commit() and
  10475. * intel_atomic_prepare_commit() because we still want
  10476. * to skip the flip and fb cleanup waits. Although that
  10477. * does risk yanking the mapping from under the display
  10478. * engine.
  10479. *
  10480. * FIXME doing watermarks and fb cleanup from a vblank worker
  10481. * (assuming we had any) would solve these problems.
  10482. */
  10483. if (INTEL_GEN(dev_priv) < 9)
  10484. state->legacy_cursor_update = false;
  10485. ret = drm_atomic_helper_swap_state(state, true);
  10486. if (ret) {
  10487. i915_sw_fence_commit(&intel_state->commit_ready);
  10488. drm_atomic_helper_cleanup_planes(dev, state);
  10489. return ret;
  10490. }
  10491. dev_priv->wm.distrust_bios_wm = false;
  10492. intel_shared_dpll_swap_state(state);
  10493. intel_atomic_track_fbs(state);
  10494. if (intel_state->modeset) {
  10495. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10496. sizeof(intel_state->min_pixclk));
  10497. dev_priv->active_crtcs = intel_state->active_crtcs;
  10498. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10499. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10500. }
  10501. drm_atomic_state_get(state);
  10502. INIT_WORK(&state->commit_work,
  10503. nonblock ? intel_atomic_commit_work : NULL);
  10504. i915_sw_fence_commit(&intel_state->commit_ready);
  10505. if (!nonblock) {
  10506. i915_sw_fence_wait(&intel_state->commit_ready);
  10507. intel_atomic_commit_tail(state);
  10508. }
  10509. return 0;
  10510. }
  10511. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10512. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10513. .set_config = drm_atomic_helper_set_config,
  10514. .destroy = intel_crtc_destroy,
  10515. .page_flip = drm_atomic_helper_page_flip,
  10516. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10517. .atomic_destroy_state = intel_crtc_destroy_state,
  10518. .set_crc_source = intel_crtc_set_crc_source,
  10519. };
  10520. /**
  10521. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10522. * @plane: drm plane to prepare for
  10523. * @fb: framebuffer to prepare for presentation
  10524. *
  10525. * Prepares a framebuffer for usage on a display plane. Generally this
  10526. * involves pinning the underlying object and updating the frontbuffer tracking
  10527. * bits. Some older platforms need special physical address handling for
  10528. * cursor planes.
  10529. *
  10530. * Must be called with struct_mutex held.
  10531. *
  10532. * Returns 0 on success, negative error code on failure.
  10533. */
  10534. int
  10535. intel_prepare_plane_fb(struct drm_plane *plane,
  10536. struct drm_plane_state *new_state)
  10537. {
  10538. struct intel_atomic_state *intel_state =
  10539. to_intel_atomic_state(new_state->state);
  10540. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10541. struct drm_framebuffer *fb = new_state->fb;
  10542. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10543. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10544. int ret;
  10545. if (old_obj) {
  10546. struct drm_crtc_state *crtc_state =
  10547. drm_atomic_get_existing_crtc_state(new_state->state,
  10548. plane->state->crtc);
  10549. /* Big Hammer, we also need to ensure that any pending
  10550. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10551. * current scanout is retired before unpinning the old
  10552. * framebuffer. Note that we rely on userspace rendering
  10553. * into the buffer attached to the pipe they are waiting
  10554. * on. If not, userspace generates a GPU hang with IPEHR
  10555. * point to the MI_WAIT_FOR_EVENT.
  10556. *
  10557. * This should only fail upon a hung GPU, in which case we
  10558. * can safely continue.
  10559. */
  10560. if (needs_modeset(crtc_state)) {
  10561. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10562. old_obj->resv, NULL,
  10563. false, 0,
  10564. GFP_KERNEL);
  10565. if (ret < 0)
  10566. return ret;
  10567. }
  10568. }
  10569. if (new_state->fence) { /* explicit fencing */
  10570. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10571. new_state->fence,
  10572. I915_FENCE_TIMEOUT,
  10573. GFP_KERNEL);
  10574. if (ret < 0)
  10575. return ret;
  10576. }
  10577. if (!obj)
  10578. return 0;
  10579. ret = i915_gem_object_pin_pages(obj);
  10580. if (ret)
  10581. return ret;
  10582. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10583. if (ret) {
  10584. i915_gem_object_unpin_pages(obj);
  10585. return ret;
  10586. }
  10587. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10588. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10589. const int align = intel_cursor_alignment(dev_priv);
  10590. ret = i915_gem_object_attach_phys(obj, align);
  10591. } else {
  10592. struct i915_vma *vma;
  10593. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10594. if (!IS_ERR(vma))
  10595. to_intel_plane_state(new_state)->vma = vma;
  10596. else
  10597. ret = PTR_ERR(vma);
  10598. }
  10599. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10600. mutex_unlock(&dev_priv->drm.struct_mutex);
  10601. i915_gem_object_unpin_pages(obj);
  10602. if (ret)
  10603. return ret;
  10604. if (!new_state->fence) { /* implicit fencing */
  10605. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10606. obj->resv, NULL,
  10607. false, I915_FENCE_TIMEOUT,
  10608. GFP_KERNEL);
  10609. if (ret < 0)
  10610. return ret;
  10611. }
  10612. return 0;
  10613. }
  10614. /**
  10615. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10616. * @plane: drm plane to clean up for
  10617. * @fb: old framebuffer that was on plane
  10618. *
  10619. * Cleans up a framebuffer that has just been removed from a plane.
  10620. *
  10621. * Must be called with struct_mutex held.
  10622. */
  10623. void
  10624. intel_cleanup_plane_fb(struct drm_plane *plane,
  10625. struct drm_plane_state *old_state)
  10626. {
  10627. struct i915_vma *vma;
  10628. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10629. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10630. if (vma) {
  10631. mutex_lock(&plane->dev->struct_mutex);
  10632. intel_unpin_fb_vma(vma);
  10633. mutex_unlock(&plane->dev->struct_mutex);
  10634. }
  10635. }
  10636. int
  10637. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10638. {
  10639. struct drm_i915_private *dev_priv;
  10640. int max_scale;
  10641. int crtc_clock, max_dotclk;
  10642. if (!intel_crtc || !crtc_state->base.enable)
  10643. return DRM_PLANE_HELPER_NO_SCALING;
  10644. dev_priv = to_i915(intel_crtc->base.dev);
  10645. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10646. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10647. if (IS_GEMINILAKE(dev_priv))
  10648. max_dotclk *= 2;
  10649. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10650. return DRM_PLANE_HELPER_NO_SCALING;
  10651. /*
  10652. * skl max scale is lower of:
  10653. * close to 3 but not 3, -1 is for that purpose
  10654. * or
  10655. * cdclk/crtc_clock
  10656. */
  10657. max_scale = min((1 << 16) * 3 - 1,
  10658. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10659. return max_scale;
  10660. }
  10661. static int
  10662. intel_check_primary_plane(struct intel_plane *plane,
  10663. struct intel_crtc_state *crtc_state,
  10664. struct intel_plane_state *state)
  10665. {
  10666. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10667. struct drm_crtc *crtc = state->base.crtc;
  10668. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10669. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10670. bool can_position = false;
  10671. int ret;
  10672. if (INTEL_GEN(dev_priv) >= 9) {
  10673. /* use scaler when colorkey is not required */
  10674. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10675. min_scale = 1;
  10676. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10677. }
  10678. can_position = true;
  10679. }
  10680. ret = drm_plane_helper_check_state(&state->base,
  10681. &state->clip,
  10682. min_scale, max_scale,
  10683. can_position, true);
  10684. if (ret)
  10685. return ret;
  10686. if (!state->base.fb)
  10687. return 0;
  10688. if (INTEL_GEN(dev_priv) >= 9) {
  10689. ret = skl_check_plane_surface(state);
  10690. if (ret)
  10691. return ret;
  10692. state->ctl = skl_plane_ctl(crtc_state, state);
  10693. } else {
  10694. ret = i9xx_check_plane_surface(state);
  10695. if (ret)
  10696. return ret;
  10697. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10698. }
  10699. return 0;
  10700. }
  10701. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10702. struct drm_crtc_state *old_crtc_state)
  10703. {
  10704. struct drm_device *dev = crtc->dev;
  10705. struct drm_i915_private *dev_priv = to_i915(dev);
  10706. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10707. struct intel_crtc_state *intel_cstate =
  10708. to_intel_crtc_state(crtc->state);
  10709. struct intel_crtc_state *old_intel_cstate =
  10710. to_intel_crtc_state(old_crtc_state);
  10711. struct intel_atomic_state *old_intel_state =
  10712. to_intel_atomic_state(old_crtc_state->state);
  10713. bool modeset = needs_modeset(crtc->state);
  10714. if (!modeset &&
  10715. (intel_cstate->base.color_mgmt_changed ||
  10716. intel_cstate->update_pipe)) {
  10717. intel_color_set_csc(crtc->state);
  10718. intel_color_load_luts(crtc->state);
  10719. }
  10720. /* Perform vblank evasion around commit operation */
  10721. intel_pipe_update_start(intel_crtc);
  10722. if (modeset)
  10723. goto out;
  10724. if (intel_cstate->update_pipe)
  10725. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  10726. else if (INTEL_GEN(dev_priv) >= 9)
  10727. skl_detach_scalers(intel_crtc);
  10728. out:
  10729. if (dev_priv->display.atomic_update_watermarks)
  10730. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10731. intel_cstate);
  10732. }
  10733. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10734. struct drm_crtc_state *old_crtc_state)
  10735. {
  10736. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10737. intel_pipe_update_end(intel_crtc);
  10738. }
  10739. /**
  10740. * intel_plane_destroy - destroy a plane
  10741. * @plane: plane to destroy
  10742. *
  10743. * Common destruction function for all types of planes (primary, cursor,
  10744. * sprite).
  10745. */
  10746. void intel_plane_destroy(struct drm_plane *plane)
  10747. {
  10748. drm_plane_cleanup(plane);
  10749. kfree(to_intel_plane(plane));
  10750. }
  10751. static bool i8xx_mod_supported(uint32_t format, uint64_t modifier)
  10752. {
  10753. switch (format) {
  10754. case DRM_FORMAT_C8:
  10755. case DRM_FORMAT_RGB565:
  10756. case DRM_FORMAT_XRGB1555:
  10757. case DRM_FORMAT_XRGB8888:
  10758. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10759. modifier == I915_FORMAT_MOD_X_TILED;
  10760. default:
  10761. return false;
  10762. }
  10763. }
  10764. static bool i965_mod_supported(uint32_t format, uint64_t modifier)
  10765. {
  10766. switch (format) {
  10767. case DRM_FORMAT_C8:
  10768. case DRM_FORMAT_RGB565:
  10769. case DRM_FORMAT_XRGB8888:
  10770. case DRM_FORMAT_XBGR8888:
  10771. case DRM_FORMAT_XRGB2101010:
  10772. case DRM_FORMAT_XBGR2101010:
  10773. return modifier == DRM_FORMAT_MOD_LINEAR ||
  10774. modifier == I915_FORMAT_MOD_X_TILED;
  10775. default:
  10776. return false;
  10777. }
  10778. }
  10779. static bool skl_mod_supported(uint32_t format, uint64_t modifier)
  10780. {
  10781. switch (format) {
  10782. case DRM_FORMAT_XRGB8888:
  10783. case DRM_FORMAT_XBGR8888:
  10784. case DRM_FORMAT_ARGB8888:
  10785. case DRM_FORMAT_ABGR8888:
  10786. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  10787. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  10788. return true;
  10789. /* fall through */
  10790. case DRM_FORMAT_RGB565:
  10791. case DRM_FORMAT_XRGB2101010:
  10792. case DRM_FORMAT_XBGR2101010:
  10793. case DRM_FORMAT_YUYV:
  10794. case DRM_FORMAT_YVYU:
  10795. case DRM_FORMAT_UYVY:
  10796. case DRM_FORMAT_VYUY:
  10797. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  10798. return true;
  10799. /* fall through */
  10800. case DRM_FORMAT_C8:
  10801. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  10802. modifier == I915_FORMAT_MOD_X_TILED ||
  10803. modifier == I915_FORMAT_MOD_Y_TILED)
  10804. return true;
  10805. /* fall through */
  10806. default:
  10807. return false;
  10808. }
  10809. }
  10810. static bool intel_primary_plane_format_mod_supported(struct drm_plane *plane,
  10811. uint32_t format,
  10812. uint64_t modifier)
  10813. {
  10814. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10815. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10816. return false;
  10817. if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
  10818. modifier != DRM_FORMAT_MOD_LINEAR)
  10819. return false;
  10820. if (INTEL_GEN(dev_priv) >= 9)
  10821. return skl_mod_supported(format, modifier);
  10822. else if (INTEL_GEN(dev_priv) >= 4)
  10823. return i965_mod_supported(format, modifier);
  10824. else
  10825. return i8xx_mod_supported(format, modifier);
  10826. unreachable();
  10827. }
  10828. static bool intel_cursor_plane_format_mod_supported(struct drm_plane *plane,
  10829. uint32_t format,
  10830. uint64_t modifier)
  10831. {
  10832. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  10833. return false;
  10834. return modifier == DRM_FORMAT_MOD_LINEAR && format == DRM_FORMAT_ARGB8888;
  10835. }
  10836. static struct drm_plane_funcs intel_plane_funcs = {
  10837. .update_plane = drm_atomic_helper_update_plane,
  10838. .disable_plane = drm_atomic_helper_disable_plane,
  10839. .destroy = intel_plane_destroy,
  10840. .atomic_get_property = intel_plane_atomic_get_property,
  10841. .atomic_set_property = intel_plane_atomic_set_property,
  10842. .atomic_duplicate_state = intel_plane_duplicate_state,
  10843. .atomic_destroy_state = intel_plane_destroy_state,
  10844. .format_mod_supported = intel_primary_plane_format_mod_supported,
  10845. };
  10846. static int
  10847. intel_legacy_cursor_update(struct drm_plane *plane,
  10848. struct drm_crtc *crtc,
  10849. struct drm_framebuffer *fb,
  10850. int crtc_x, int crtc_y,
  10851. unsigned int crtc_w, unsigned int crtc_h,
  10852. uint32_t src_x, uint32_t src_y,
  10853. uint32_t src_w, uint32_t src_h,
  10854. struct drm_modeset_acquire_ctx *ctx)
  10855. {
  10856. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10857. int ret;
  10858. struct drm_plane_state *old_plane_state, *new_plane_state;
  10859. struct intel_plane *intel_plane = to_intel_plane(plane);
  10860. struct drm_framebuffer *old_fb;
  10861. struct drm_crtc_state *crtc_state = crtc->state;
  10862. struct i915_vma *old_vma, *vma;
  10863. /*
  10864. * When crtc is inactive or there is a modeset pending,
  10865. * wait for it to complete in the slowpath
  10866. */
  10867. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10868. to_intel_crtc_state(crtc_state)->update_pipe)
  10869. goto slow;
  10870. old_plane_state = plane->state;
  10871. /*
  10872. * If any parameters change that may affect watermarks,
  10873. * take the slowpath. Only changing fb or position should be
  10874. * in the fastpath.
  10875. */
  10876. if (old_plane_state->crtc != crtc ||
  10877. old_plane_state->src_w != src_w ||
  10878. old_plane_state->src_h != src_h ||
  10879. old_plane_state->crtc_w != crtc_w ||
  10880. old_plane_state->crtc_h != crtc_h ||
  10881. !old_plane_state->fb != !fb)
  10882. goto slow;
  10883. new_plane_state = intel_plane_duplicate_state(plane);
  10884. if (!new_plane_state)
  10885. return -ENOMEM;
  10886. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10887. new_plane_state->src_x = src_x;
  10888. new_plane_state->src_y = src_y;
  10889. new_plane_state->src_w = src_w;
  10890. new_plane_state->src_h = src_h;
  10891. new_plane_state->crtc_x = crtc_x;
  10892. new_plane_state->crtc_y = crtc_y;
  10893. new_plane_state->crtc_w = crtc_w;
  10894. new_plane_state->crtc_h = crtc_h;
  10895. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10896. to_intel_plane_state(new_plane_state));
  10897. if (ret)
  10898. goto out_free;
  10899. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10900. if (ret)
  10901. goto out_free;
  10902. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10903. int align = intel_cursor_alignment(dev_priv);
  10904. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10905. if (ret) {
  10906. DRM_DEBUG_KMS("failed to attach phys object\n");
  10907. goto out_unlock;
  10908. }
  10909. } else {
  10910. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10911. if (IS_ERR(vma)) {
  10912. DRM_DEBUG_KMS("failed to pin object\n");
  10913. ret = PTR_ERR(vma);
  10914. goto out_unlock;
  10915. }
  10916. to_intel_plane_state(new_plane_state)->vma = vma;
  10917. }
  10918. old_fb = old_plane_state->fb;
  10919. old_vma = to_intel_plane_state(old_plane_state)->vma;
  10920. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10921. intel_plane->frontbuffer_bit);
  10922. /* Swap plane state */
  10923. new_plane_state->fence = old_plane_state->fence;
  10924. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  10925. new_plane_state->fence = NULL;
  10926. new_plane_state->fb = old_fb;
  10927. to_intel_plane_state(new_plane_state)->vma = NULL;
  10928. if (plane->state->visible) {
  10929. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10930. intel_plane->update_plane(intel_plane,
  10931. to_intel_crtc_state(crtc->state),
  10932. to_intel_plane_state(plane->state));
  10933. } else {
  10934. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10935. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10936. }
  10937. if (old_vma)
  10938. intel_unpin_fb_vma(old_vma);
  10939. out_unlock:
  10940. mutex_unlock(&dev_priv->drm.struct_mutex);
  10941. out_free:
  10942. intel_plane_destroy_state(plane, new_plane_state);
  10943. return ret;
  10944. slow:
  10945. return drm_atomic_helper_update_plane(plane, crtc, fb,
  10946. crtc_x, crtc_y, crtc_w, crtc_h,
  10947. src_x, src_y, src_w, src_h, ctx);
  10948. }
  10949. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10950. .update_plane = intel_legacy_cursor_update,
  10951. .disable_plane = drm_atomic_helper_disable_plane,
  10952. .destroy = intel_plane_destroy,
  10953. .atomic_get_property = intel_plane_atomic_get_property,
  10954. .atomic_set_property = intel_plane_atomic_set_property,
  10955. .atomic_duplicate_state = intel_plane_duplicate_state,
  10956. .atomic_destroy_state = intel_plane_destroy_state,
  10957. .format_mod_supported = intel_cursor_plane_format_mod_supported,
  10958. };
  10959. static struct intel_plane *
  10960. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  10961. {
  10962. struct intel_plane *primary = NULL;
  10963. struct intel_plane_state *state = NULL;
  10964. const uint32_t *intel_primary_formats;
  10965. unsigned int supported_rotations;
  10966. unsigned int num_formats;
  10967. const uint64_t *modifiers;
  10968. int ret;
  10969. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10970. if (!primary) {
  10971. ret = -ENOMEM;
  10972. goto fail;
  10973. }
  10974. state = intel_create_plane_state(&primary->base);
  10975. if (!state) {
  10976. ret = -ENOMEM;
  10977. goto fail;
  10978. }
  10979. primary->base.state = &state->base;
  10980. primary->can_scale = false;
  10981. primary->max_downscale = 1;
  10982. if (INTEL_GEN(dev_priv) >= 9) {
  10983. primary->can_scale = true;
  10984. state->scaler_id = -1;
  10985. }
  10986. primary->pipe = pipe;
  10987. /*
  10988. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  10989. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  10990. */
  10991. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  10992. primary->plane = (enum plane) !pipe;
  10993. else
  10994. primary->plane = (enum plane) pipe;
  10995. primary->id = PLANE_PRIMARY;
  10996. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10997. primary->check_plane = intel_check_primary_plane;
  10998. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  10999. intel_primary_formats = skl_primary_formats;
  11000. num_formats = ARRAY_SIZE(skl_primary_formats);
  11001. modifiers = skl_format_modifiers_ccs;
  11002. primary->update_plane = skylake_update_primary_plane;
  11003. primary->disable_plane = skylake_disable_primary_plane;
  11004. } else if (INTEL_GEN(dev_priv) >= 9) {
  11005. intel_primary_formats = skl_primary_formats;
  11006. num_formats = ARRAY_SIZE(skl_primary_formats);
  11007. if (pipe < PIPE_C)
  11008. modifiers = skl_format_modifiers_ccs;
  11009. else
  11010. modifiers = skl_format_modifiers_noccs;
  11011. primary->update_plane = skylake_update_primary_plane;
  11012. primary->disable_plane = skylake_disable_primary_plane;
  11013. } else if (INTEL_GEN(dev_priv) >= 4) {
  11014. intel_primary_formats = i965_primary_formats;
  11015. num_formats = ARRAY_SIZE(i965_primary_formats);
  11016. modifiers = i9xx_format_modifiers;
  11017. primary->update_plane = i9xx_update_primary_plane;
  11018. primary->disable_plane = i9xx_disable_primary_plane;
  11019. } else {
  11020. intel_primary_formats = i8xx_primary_formats;
  11021. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11022. modifiers = i9xx_format_modifiers;
  11023. primary->update_plane = i9xx_update_primary_plane;
  11024. primary->disable_plane = i9xx_disable_primary_plane;
  11025. }
  11026. if (INTEL_GEN(dev_priv) >= 9)
  11027. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11028. 0, &intel_plane_funcs,
  11029. intel_primary_formats, num_formats,
  11030. modifiers,
  11031. DRM_PLANE_TYPE_PRIMARY,
  11032. "plane 1%c", pipe_name(pipe));
  11033. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11034. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11035. 0, &intel_plane_funcs,
  11036. intel_primary_formats, num_formats,
  11037. modifiers,
  11038. DRM_PLANE_TYPE_PRIMARY,
  11039. "primary %c", pipe_name(pipe));
  11040. else
  11041. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11042. 0, &intel_plane_funcs,
  11043. intel_primary_formats, num_formats,
  11044. modifiers,
  11045. DRM_PLANE_TYPE_PRIMARY,
  11046. "plane %c", plane_name(primary->plane));
  11047. if (ret)
  11048. goto fail;
  11049. if (INTEL_GEN(dev_priv) >= 9) {
  11050. supported_rotations =
  11051. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11052. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11053. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11054. supported_rotations =
  11055. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11056. DRM_MODE_REFLECT_X;
  11057. } else if (INTEL_GEN(dev_priv) >= 4) {
  11058. supported_rotations =
  11059. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11060. } else {
  11061. supported_rotations = DRM_MODE_ROTATE_0;
  11062. }
  11063. if (INTEL_GEN(dev_priv) >= 4)
  11064. drm_plane_create_rotation_property(&primary->base,
  11065. DRM_MODE_ROTATE_0,
  11066. supported_rotations);
  11067. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11068. return primary;
  11069. fail:
  11070. kfree(state);
  11071. kfree(primary);
  11072. return ERR_PTR(ret);
  11073. }
  11074. static struct intel_plane *
  11075. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11076. enum pipe pipe)
  11077. {
  11078. struct intel_plane *cursor = NULL;
  11079. struct intel_plane_state *state = NULL;
  11080. int ret;
  11081. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11082. if (!cursor) {
  11083. ret = -ENOMEM;
  11084. goto fail;
  11085. }
  11086. state = intel_create_plane_state(&cursor->base);
  11087. if (!state) {
  11088. ret = -ENOMEM;
  11089. goto fail;
  11090. }
  11091. cursor->base.state = &state->base;
  11092. cursor->can_scale = false;
  11093. cursor->max_downscale = 1;
  11094. cursor->pipe = pipe;
  11095. cursor->plane = pipe;
  11096. cursor->id = PLANE_CURSOR;
  11097. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11098. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11099. cursor->update_plane = i845_update_cursor;
  11100. cursor->disable_plane = i845_disable_cursor;
  11101. cursor->check_plane = i845_check_cursor;
  11102. } else {
  11103. cursor->update_plane = i9xx_update_cursor;
  11104. cursor->disable_plane = i9xx_disable_cursor;
  11105. cursor->check_plane = i9xx_check_cursor;
  11106. }
  11107. cursor->cursor.base = ~0;
  11108. cursor->cursor.cntl = ~0;
  11109. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11110. cursor->cursor.size = ~0;
  11111. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11112. 0, &intel_cursor_plane_funcs,
  11113. intel_cursor_formats,
  11114. ARRAY_SIZE(intel_cursor_formats),
  11115. cursor_format_modifiers,
  11116. DRM_PLANE_TYPE_CURSOR,
  11117. "cursor %c", pipe_name(pipe));
  11118. if (ret)
  11119. goto fail;
  11120. if (INTEL_GEN(dev_priv) >= 4)
  11121. drm_plane_create_rotation_property(&cursor->base,
  11122. DRM_MODE_ROTATE_0,
  11123. DRM_MODE_ROTATE_0 |
  11124. DRM_MODE_ROTATE_180);
  11125. if (INTEL_GEN(dev_priv) >= 9)
  11126. state->scaler_id = -1;
  11127. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11128. return cursor;
  11129. fail:
  11130. kfree(state);
  11131. kfree(cursor);
  11132. return ERR_PTR(ret);
  11133. }
  11134. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11135. struct intel_crtc_state *crtc_state)
  11136. {
  11137. struct intel_crtc_scaler_state *scaler_state =
  11138. &crtc_state->scaler_state;
  11139. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11140. int i;
  11141. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11142. if (!crtc->num_scalers)
  11143. return;
  11144. for (i = 0; i < crtc->num_scalers; i++) {
  11145. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11146. scaler->in_use = 0;
  11147. scaler->mode = PS_SCALER_MODE_DYN;
  11148. }
  11149. scaler_state->scaler_id = -1;
  11150. }
  11151. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11152. {
  11153. struct intel_crtc *intel_crtc;
  11154. struct intel_crtc_state *crtc_state = NULL;
  11155. struct intel_plane *primary = NULL;
  11156. struct intel_plane *cursor = NULL;
  11157. int sprite, ret;
  11158. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11159. if (!intel_crtc)
  11160. return -ENOMEM;
  11161. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11162. if (!crtc_state) {
  11163. ret = -ENOMEM;
  11164. goto fail;
  11165. }
  11166. intel_crtc->config = crtc_state;
  11167. intel_crtc->base.state = &crtc_state->base;
  11168. crtc_state->base.crtc = &intel_crtc->base;
  11169. primary = intel_primary_plane_create(dev_priv, pipe);
  11170. if (IS_ERR(primary)) {
  11171. ret = PTR_ERR(primary);
  11172. goto fail;
  11173. }
  11174. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11175. for_each_sprite(dev_priv, pipe, sprite) {
  11176. struct intel_plane *plane;
  11177. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11178. if (IS_ERR(plane)) {
  11179. ret = PTR_ERR(plane);
  11180. goto fail;
  11181. }
  11182. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11183. }
  11184. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11185. if (IS_ERR(cursor)) {
  11186. ret = PTR_ERR(cursor);
  11187. goto fail;
  11188. }
  11189. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11190. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11191. &primary->base, &cursor->base,
  11192. &intel_crtc_funcs,
  11193. "pipe %c", pipe_name(pipe));
  11194. if (ret)
  11195. goto fail;
  11196. intel_crtc->pipe = pipe;
  11197. intel_crtc->plane = primary->plane;
  11198. /* initialize shared scalers */
  11199. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11200. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11201. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11202. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11203. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11204. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11205. intel_color_init(&intel_crtc->base);
  11206. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11207. return 0;
  11208. fail:
  11209. /*
  11210. * drm_mode_config_cleanup() will free up any
  11211. * crtcs/planes already initialized.
  11212. */
  11213. kfree(crtc_state);
  11214. kfree(intel_crtc);
  11215. return ret;
  11216. }
  11217. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11218. {
  11219. struct drm_device *dev = connector->base.dev;
  11220. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11221. if (!connector->base.state->crtc)
  11222. return INVALID_PIPE;
  11223. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11224. }
  11225. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11226. struct drm_file *file)
  11227. {
  11228. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11229. struct drm_crtc *drmmode_crtc;
  11230. struct intel_crtc *crtc;
  11231. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11232. if (!drmmode_crtc)
  11233. return -ENOENT;
  11234. crtc = to_intel_crtc(drmmode_crtc);
  11235. pipe_from_crtc_id->pipe = crtc->pipe;
  11236. return 0;
  11237. }
  11238. static int intel_encoder_clones(struct intel_encoder *encoder)
  11239. {
  11240. struct drm_device *dev = encoder->base.dev;
  11241. struct intel_encoder *source_encoder;
  11242. int index_mask = 0;
  11243. int entry = 0;
  11244. for_each_intel_encoder(dev, source_encoder) {
  11245. if (encoders_cloneable(encoder, source_encoder))
  11246. index_mask |= (1 << entry);
  11247. entry++;
  11248. }
  11249. return index_mask;
  11250. }
  11251. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11252. {
  11253. if (!IS_MOBILE(dev_priv))
  11254. return false;
  11255. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11256. return false;
  11257. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11258. return false;
  11259. return true;
  11260. }
  11261. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11262. {
  11263. if (INTEL_GEN(dev_priv) >= 9)
  11264. return false;
  11265. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11266. return false;
  11267. if (IS_CHERRYVIEW(dev_priv))
  11268. return false;
  11269. if (HAS_PCH_LPT_H(dev_priv) &&
  11270. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11271. return false;
  11272. /* DDI E can't be used if DDI A requires 4 lanes */
  11273. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11274. return false;
  11275. if (!dev_priv->vbt.int_crt_support)
  11276. return false;
  11277. return true;
  11278. }
  11279. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11280. {
  11281. int pps_num;
  11282. int pps_idx;
  11283. if (HAS_DDI(dev_priv))
  11284. return;
  11285. /*
  11286. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11287. * everywhere where registers can be write protected.
  11288. */
  11289. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11290. pps_num = 2;
  11291. else
  11292. pps_num = 1;
  11293. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11294. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11295. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11296. I915_WRITE(PP_CONTROL(pps_idx), val);
  11297. }
  11298. }
  11299. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11300. {
  11301. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11302. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11303. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11304. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11305. else
  11306. dev_priv->pps_mmio_base = PPS_BASE;
  11307. intel_pps_unlock_regs_wa(dev_priv);
  11308. }
  11309. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11310. {
  11311. struct intel_encoder *encoder;
  11312. bool dpd_is_edp = false;
  11313. intel_pps_init(dev_priv);
  11314. /*
  11315. * intel_edp_init_connector() depends on this completing first, to
  11316. * prevent the registeration of both eDP and LVDS and the incorrect
  11317. * sharing of the PPS.
  11318. */
  11319. intel_lvds_init(dev_priv);
  11320. if (intel_crt_present(dev_priv))
  11321. intel_crt_init(dev_priv);
  11322. if (IS_GEN9_LP(dev_priv)) {
  11323. /*
  11324. * FIXME: Broxton doesn't support port detection via the
  11325. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11326. * detect the ports.
  11327. */
  11328. intel_ddi_init(dev_priv, PORT_A);
  11329. intel_ddi_init(dev_priv, PORT_B);
  11330. intel_ddi_init(dev_priv, PORT_C);
  11331. intel_dsi_init(dev_priv);
  11332. } else if (HAS_DDI(dev_priv)) {
  11333. int found;
  11334. /*
  11335. * Haswell uses DDI functions to detect digital outputs.
  11336. * On SKL pre-D0 the strap isn't connected, so we assume
  11337. * it's there.
  11338. */
  11339. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11340. /* WaIgnoreDDIAStrap: skl */
  11341. if (found || IS_GEN9_BC(dev_priv))
  11342. intel_ddi_init(dev_priv, PORT_A);
  11343. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11344. * register */
  11345. found = I915_READ(SFUSE_STRAP);
  11346. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11347. intel_ddi_init(dev_priv, PORT_B);
  11348. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11349. intel_ddi_init(dev_priv, PORT_C);
  11350. if (found & SFUSE_STRAP_DDID_DETECTED)
  11351. intel_ddi_init(dev_priv, PORT_D);
  11352. /*
  11353. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11354. */
  11355. if (IS_GEN9_BC(dev_priv) &&
  11356. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11357. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11358. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11359. intel_ddi_init(dev_priv, PORT_E);
  11360. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11361. int found;
  11362. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11363. if (has_edp_a(dev_priv))
  11364. intel_dp_init(dev_priv, DP_A, PORT_A);
  11365. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11366. /* PCH SDVOB multiplex with HDMIB */
  11367. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11368. if (!found)
  11369. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11370. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11371. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11372. }
  11373. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11374. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11375. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11376. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11377. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11378. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11379. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11380. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11381. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11382. bool has_edp, has_port;
  11383. /*
  11384. * The DP_DETECTED bit is the latched state of the DDC
  11385. * SDA pin at boot. However since eDP doesn't require DDC
  11386. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11387. * eDP ports may have been muxed to an alternate function.
  11388. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11389. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11390. * detect eDP ports.
  11391. *
  11392. * Sadly the straps seem to be missing sometimes even for HDMI
  11393. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11394. * and VBT for the presence of the port. Additionally we can't
  11395. * trust the port type the VBT declares as we've seen at least
  11396. * HDMI ports that the VBT claim are DP or eDP.
  11397. */
  11398. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11399. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11400. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11401. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11402. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11403. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11404. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11405. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11406. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11407. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11408. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11409. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11410. if (IS_CHERRYVIEW(dev_priv)) {
  11411. /*
  11412. * eDP not supported on port D,
  11413. * so no need to worry about it
  11414. */
  11415. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11416. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11417. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11418. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11419. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11420. }
  11421. intel_dsi_init(dev_priv);
  11422. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11423. bool found = false;
  11424. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11425. DRM_DEBUG_KMS("probing SDVOB\n");
  11426. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11427. if (!found && IS_G4X(dev_priv)) {
  11428. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11429. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11430. }
  11431. if (!found && IS_G4X(dev_priv))
  11432. intel_dp_init(dev_priv, DP_B, PORT_B);
  11433. }
  11434. /* Before G4X SDVOC doesn't have its own detect register */
  11435. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11436. DRM_DEBUG_KMS("probing SDVOC\n");
  11437. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11438. }
  11439. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11440. if (IS_G4X(dev_priv)) {
  11441. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11442. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11443. }
  11444. if (IS_G4X(dev_priv))
  11445. intel_dp_init(dev_priv, DP_C, PORT_C);
  11446. }
  11447. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11448. intel_dp_init(dev_priv, DP_D, PORT_D);
  11449. } else if (IS_GEN2(dev_priv))
  11450. intel_dvo_init(dev_priv);
  11451. if (SUPPORTS_TV(dev_priv))
  11452. intel_tv_init(dev_priv);
  11453. intel_psr_init(dev_priv);
  11454. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11455. encoder->base.possible_crtcs = encoder->crtc_mask;
  11456. encoder->base.possible_clones =
  11457. intel_encoder_clones(encoder);
  11458. }
  11459. intel_init_pch_refclk(dev_priv);
  11460. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11461. }
  11462. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11463. {
  11464. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11465. drm_framebuffer_cleanup(fb);
  11466. i915_gem_object_lock(intel_fb->obj);
  11467. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11468. i915_gem_object_unlock(intel_fb->obj);
  11469. i915_gem_object_put(intel_fb->obj);
  11470. kfree(intel_fb);
  11471. }
  11472. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11473. struct drm_file *file,
  11474. unsigned int *handle)
  11475. {
  11476. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11477. struct drm_i915_gem_object *obj = intel_fb->obj;
  11478. if (obj->userptr.mm) {
  11479. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11480. return -EINVAL;
  11481. }
  11482. return drm_gem_handle_create(file, &obj->base, handle);
  11483. }
  11484. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11485. struct drm_file *file,
  11486. unsigned flags, unsigned color,
  11487. struct drm_clip_rect *clips,
  11488. unsigned num_clips)
  11489. {
  11490. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11491. i915_gem_object_flush_if_display(obj);
  11492. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11493. return 0;
  11494. }
  11495. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11496. .destroy = intel_user_framebuffer_destroy,
  11497. .create_handle = intel_user_framebuffer_create_handle,
  11498. .dirty = intel_user_framebuffer_dirty,
  11499. };
  11500. static
  11501. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11502. uint64_t fb_modifier, uint32_t pixel_format)
  11503. {
  11504. u32 gen = INTEL_GEN(dev_priv);
  11505. if (gen >= 9) {
  11506. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11507. /* "The stride in bytes must not exceed the of the size of 8K
  11508. * pixels and 32K bytes."
  11509. */
  11510. return min(8192 * cpp, 32768);
  11511. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11512. return 32*1024;
  11513. } else if (gen >= 4) {
  11514. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11515. return 16*1024;
  11516. else
  11517. return 32*1024;
  11518. } else if (gen >= 3) {
  11519. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11520. return 8*1024;
  11521. else
  11522. return 16*1024;
  11523. } else {
  11524. /* XXX DSPC is limited to 4k tiled */
  11525. return 8*1024;
  11526. }
  11527. }
  11528. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11529. struct drm_i915_gem_object *obj,
  11530. struct drm_mode_fb_cmd2 *mode_cmd)
  11531. {
  11532. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11533. struct drm_framebuffer *fb = &intel_fb->base;
  11534. struct drm_format_name_buf format_name;
  11535. u32 pitch_limit;
  11536. unsigned int tiling, stride;
  11537. int ret = -EINVAL;
  11538. int i;
  11539. i915_gem_object_lock(obj);
  11540. obj->framebuffer_references++;
  11541. tiling = i915_gem_object_get_tiling(obj);
  11542. stride = i915_gem_object_get_stride(obj);
  11543. i915_gem_object_unlock(obj);
  11544. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11545. /*
  11546. * If there's a fence, enforce that
  11547. * the fb modifier and tiling mode match.
  11548. */
  11549. if (tiling != I915_TILING_NONE &&
  11550. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11551. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11552. goto err;
  11553. }
  11554. } else {
  11555. if (tiling == I915_TILING_X) {
  11556. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11557. } else if (tiling == I915_TILING_Y) {
  11558. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11559. goto err;
  11560. }
  11561. }
  11562. /* Passed in modifier sanity checking. */
  11563. switch (mode_cmd->modifier[0]) {
  11564. case I915_FORMAT_MOD_Y_TILED_CCS:
  11565. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11566. switch (mode_cmd->pixel_format) {
  11567. case DRM_FORMAT_XBGR8888:
  11568. case DRM_FORMAT_ABGR8888:
  11569. case DRM_FORMAT_XRGB8888:
  11570. case DRM_FORMAT_ARGB8888:
  11571. break;
  11572. default:
  11573. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  11574. goto err;
  11575. }
  11576. /* fall through */
  11577. case I915_FORMAT_MOD_Y_TILED:
  11578. case I915_FORMAT_MOD_Yf_TILED:
  11579. if (INTEL_GEN(dev_priv) < 9) {
  11580. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11581. mode_cmd->modifier[0]);
  11582. goto err;
  11583. }
  11584. case DRM_FORMAT_MOD_LINEAR:
  11585. case I915_FORMAT_MOD_X_TILED:
  11586. break;
  11587. default:
  11588. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11589. mode_cmd->modifier[0]);
  11590. goto err;
  11591. }
  11592. /*
  11593. * gen2/3 display engine uses the fence if present,
  11594. * so the tiling mode must match the fb modifier exactly.
  11595. */
  11596. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11597. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11598. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11599. goto err;
  11600. }
  11601. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11602. mode_cmd->pixel_format);
  11603. if (mode_cmd->pitches[0] > pitch_limit) {
  11604. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11605. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11606. "tiled" : "linear",
  11607. mode_cmd->pitches[0], pitch_limit);
  11608. goto err;
  11609. }
  11610. /*
  11611. * If there's a fence, enforce that
  11612. * the fb pitch and fence stride match.
  11613. */
  11614. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11615. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11616. mode_cmd->pitches[0], stride);
  11617. goto err;
  11618. }
  11619. /* Reject formats not supported by any plane early. */
  11620. switch (mode_cmd->pixel_format) {
  11621. case DRM_FORMAT_C8:
  11622. case DRM_FORMAT_RGB565:
  11623. case DRM_FORMAT_XRGB8888:
  11624. case DRM_FORMAT_ARGB8888:
  11625. break;
  11626. case DRM_FORMAT_XRGB1555:
  11627. if (INTEL_GEN(dev_priv) > 3) {
  11628. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11629. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11630. goto err;
  11631. }
  11632. break;
  11633. case DRM_FORMAT_ABGR8888:
  11634. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11635. INTEL_GEN(dev_priv) < 9) {
  11636. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11637. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11638. goto err;
  11639. }
  11640. break;
  11641. case DRM_FORMAT_XBGR8888:
  11642. case DRM_FORMAT_XRGB2101010:
  11643. case DRM_FORMAT_XBGR2101010:
  11644. if (INTEL_GEN(dev_priv) < 4) {
  11645. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11646. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11647. goto err;
  11648. }
  11649. break;
  11650. case DRM_FORMAT_ABGR2101010:
  11651. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(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. case DRM_FORMAT_YUYV:
  11658. case DRM_FORMAT_UYVY:
  11659. case DRM_FORMAT_YVYU:
  11660. case DRM_FORMAT_VYUY:
  11661. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11662. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11663. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11664. goto err;
  11665. }
  11666. break;
  11667. default:
  11668. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11669. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11670. goto err;
  11671. }
  11672. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11673. if (mode_cmd->offsets[0] != 0)
  11674. goto err;
  11675. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  11676. for (i = 0; i < fb->format->num_planes; i++) {
  11677. u32 stride_alignment;
  11678. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  11679. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  11680. return -EINVAL;
  11681. }
  11682. stride_alignment = intel_fb_stride_alignment(fb, i);
  11683. /*
  11684. * Display WA #0531: skl,bxt,kbl,glk
  11685. *
  11686. * Render decompression and plane width > 3840
  11687. * combined with horizontal panning requires the
  11688. * plane stride to be a multiple of 4. We'll just
  11689. * require the entire fb to accommodate that to avoid
  11690. * potential runtime errors at plane configuration time.
  11691. */
  11692. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  11693. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  11694. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  11695. stride_alignment *= 4;
  11696. if (fb->pitches[i] & (stride_alignment - 1)) {
  11697. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  11698. i, fb->pitches[i], stride_alignment);
  11699. goto err;
  11700. }
  11701. }
  11702. intel_fb->obj = obj;
  11703. ret = intel_fill_fb_info(dev_priv, fb);
  11704. if (ret)
  11705. goto err;
  11706. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  11707. if (ret) {
  11708. DRM_ERROR("framebuffer init failed %d\n", ret);
  11709. goto err;
  11710. }
  11711. return 0;
  11712. err:
  11713. i915_gem_object_lock(obj);
  11714. obj->framebuffer_references--;
  11715. i915_gem_object_unlock(obj);
  11716. return ret;
  11717. }
  11718. static struct drm_framebuffer *
  11719. intel_user_framebuffer_create(struct drm_device *dev,
  11720. struct drm_file *filp,
  11721. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11722. {
  11723. struct drm_framebuffer *fb;
  11724. struct drm_i915_gem_object *obj;
  11725. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11726. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11727. if (!obj)
  11728. return ERR_PTR(-ENOENT);
  11729. fb = intel_framebuffer_create(obj, &mode_cmd);
  11730. if (IS_ERR(fb))
  11731. i915_gem_object_put(obj);
  11732. return fb;
  11733. }
  11734. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11735. {
  11736. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11737. drm_atomic_state_default_release(state);
  11738. i915_sw_fence_fini(&intel_state->commit_ready);
  11739. kfree(state);
  11740. }
  11741. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11742. .fb_create = intel_user_framebuffer_create,
  11743. .get_format_info = intel_get_format_info,
  11744. .output_poll_changed = intel_fbdev_output_poll_changed,
  11745. .atomic_check = intel_atomic_check,
  11746. .atomic_commit = intel_atomic_commit,
  11747. .atomic_state_alloc = intel_atomic_state_alloc,
  11748. .atomic_state_clear = intel_atomic_state_clear,
  11749. .atomic_state_free = intel_atomic_state_free,
  11750. };
  11751. /**
  11752. * intel_init_display_hooks - initialize the display modesetting hooks
  11753. * @dev_priv: device private
  11754. */
  11755. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11756. {
  11757. intel_init_cdclk_hooks(dev_priv);
  11758. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11759. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11760. dev_priv->display.get_initial_plane_config =
  11761. skylake_get_initial_plane_config;
  11762. dev_priv->display.crtc_compute_clock =
  11763. haswell_crtc_compute_clock;
  11764. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11765. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11766. } else if (HAS_DDI(dev_priv)) {
  11767. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11768. dev_priv->display.get_initial_plane_config =
  11769. ironlake_get_initial_plane_config;
  11770. dev_priv->display.crtc_compute_clock =
  11771. haswell_crtc_compute_clock;
  11772. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11773. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11774. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11775. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11776. dev_priv->display.get_initial_plane_config =
  11777. ironlake_get_initial_plane_config;
  11778. dev_priv->display.crtc_compute_clock =
  11779. ironlake_crtc_compute_clock;
  11780. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11781. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11782. } else if (IS_CHERRYVIEW(dev_priv)) {
  11783. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11784. dev_priv->display.get_initial_plane_config =
  11785. i9xx_get_initial_plane_config;
  11786. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11787. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11788. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11789. } else if (IS_VALLEYVIEW(dev_priv)) {
  11790. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11791. dev_priv->display.get_initial_plane_config =
  11792. i9xx_get_initial_plane_config;
  11793. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11794. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11795. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11796. } else if (IS_G4X(dev_priv)) {
  11797. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11798. dev_priv->display.get_initial_plane_config =
  11799. i9xx_get_initial_plane_config;
  11800. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11801. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11802. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11803. } else if (IS_PINEVIEW(dev_priv)) {
  11804. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11805. dev_priv->display.get_initial_plane_config =
  11806. i9xx_get_initial_plane_config;
  11807. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11808. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11809. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11810. } else if (!IS_GEN2(dev_priv)) {
  11811. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11812. dev_priv->display.get_initial_plane_config =
  11813. i9xx_get_initial_plane_config;
  11814. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11815. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11816. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11817. } else {
  11818. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11819. dev_priv->display.get_initial_plane_config =
  11820. i9xx_get_initial_plane_config;
  11821. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11822. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11823. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11824. }
  11825. if (IS_GEN5(dev_priv)) {
  11826. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11827. } else if (IS_GEN6(dev_priv)) {
  11828. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11829. } else if (IS_IVYBRIDGE(dev_priv)) {
  11830. /* FIXME: detect B0+ stepping and use auto training */
  11831. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11832. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11833. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11834. }
  11835. if (dev_priv->info.gen >= 9)
  11836. dev_priv->display.update_crtcs = skl_update_crtcs;
  11837. else
  11838. dev_priv->display.update_crtcs = intel_update_crtcs;
  11839. }
  11840. /*
  11841. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11842. */
  11843. static void quirk_ssc_force_disable(struct drm_device *dev)
  11844. {
  11845. struct drm_i915_private *dev_priv = to_i915(dev);
  11846. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11847. DRM_INFO("applying lvds SSC disable quirk\n");
  11848. }
  11849. /*
  11850. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11851. * brightness value
  11852. */
  11853. static void quirk_invert_brightness(struct drm_device *dev)
  11854. {
  11855. struct drm_i915_private *dev_priv = to_i915(dev);
  11856. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11857. DRM_INFO("applying inverted panel brightness quirk\n");
  11858. }
  11859. /* Some VBT's incorrectly indicate no backlight is present */
  11860. static void quirk_backlight_present(struct drm_device *dev)
  11861. {
  11862. struct drm_i915_private *dev_priv = to_i915(dev);
  11863. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11864. DRM_INFO("applying backlight present quirk\n");
  11865. }
  11866. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11867. * which is 300 ms greater than eDP spec T12 min.
  11868. */
  11869. static void quirk_increase_t12_delay(struct drm_device *dev)
  11870. {
  11871. struct drm_i915_private *dev_priv = to_i915(dev);
  11872. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11873. DRM_INFO("Applying T12 delay quirk\n");
  11874. }
  11875. struct intel_quirk {
  11876. int device;
  11877. int subsystem_vendor;
  11878. int subsystem_device;
  11879. void (*hook)(struct drm_device *dev);
  11880. };
  11881. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11882. struct intel_dmi_quirk {
  11883. void (*hook)(struct drm_device *dev);
  11884. const struct dmi_system_id (*dmi_id_list)[];
  11885. };
  11886. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11887. {
  11888. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11889. return 1;
  11890. }
  11891. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11892. {
  11893. .dmi_id_list = &(const struct dmi_system_id[]) {
  11894. {
  11895. .callback = intel_dmi_reverse_brightness,
  11896. .ident = "NCR Corporation",
  11897. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11898. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11899. },
  11900. },
  11901. { } /* terminating entry */
  11902. },
  11903. .hook = quirk_invert_brightness,
  11904. },
  11905. };
  11906. static struct intel_quirk intel_quirks[] = {
  11907. /* Lenovo U160 cannot use SSC on LVDS */
  11908. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11909. /* Sony Vaio Y cannot use SSC on LVDS */
  11910. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11911. /* Acer Aspire 5734Z must invert backlight brightness */
  11912. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11913. /* Acer/eMachines G725 */
  11914. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11915. /* Acer/eMachines e725 */
  11916. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11917. /* Acer/Packard Bell NCL20 */
  11918. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11919. /* Acer Aspire 4736Z */
  11920. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11921. /* Acer Aspire 5336 */
  11922. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11923. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11924. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11925. /* Acer C720 Chromebook (Core i3 4005U) */
  11926. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11927. /* Apple Macbook 2,1 (Core 2 T7400) */
  11928. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11929. /* Apple Macbook 4,1 */
  11930. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11931. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11932. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11933. /* HP Chromebook 14 (Celeron 2955U) */
  11934. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11935. /* Dell Chromebook 11 */
  11936. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11937. /* Dell Chromebook 11 (2015 version) */
  11938. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11939. /* Toshiba Satellite P50-C-18C */
  11940. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11941. };
  11942. static void intel_init_quirks(struct drm_device *dev)
  11943. {
  11944. struct pci_dev *d = dev->pdev;
  11945. int i;
  11946. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11947. struct intel_quirk *q = &intel_quirks[i];
  11948. if (d->device == q->device &&
  11949. (d->subsystem_vendor == q->subsystem_vendor ||
  11950. q->subsystem_vendor == PCI_ANY_ID) &&
  11951. (d->subsystem_device == q->subsystem_device ||
  11952. q->subsystem_device == PCI_ANY_ID))
  11953. q->hook(dev);
  11954. }
  11955. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11956. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11957. intel_dmi_quirks[i].hook(dev);
  11958. }
  11959. }
  11960. /* Disable the VGA plane that we never use */
  11961. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  11962. {
  11963. struct pci_dev *pdev = dev_priv->drm.pdev;
  11964. u8 sr1;
  11965. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  11966. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11967. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  11968. outb(SR01, VGA_SR_INDEX);
  11969. sr1 = inb(VGA_SR_DATA);
  11970. outb(sr1 | 1<<5, VGA_SR_DATA);
  11971. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  11972. udelay(300);
  11973. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11974. POSTING_READ(vga_reg);
  11975. }
  11976. void intel_modeset_init_hw(struct drm_device *dev)
  11977. {
  11978. struct drm_i915_private *dev_priv = to_i915(dev);
  11979. intel_update_cdclk(dev_priv);
  11980. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  11981. intel_init_clock_gating(dev_priv);
  11982. }
  11983. /*
  11984. * Calculate what we think the watermarks should be for the state we've read
  11985. * out of the hardware and then immediately program those watermarks so that
  11986. * we ensure the hardware settings match our internal state.
  11987. *
  11988. * We can calculate what we think WM's should be by creating a duplicate of the
  11989. * current state (which was constructed during hardware readout) and running it
  11990. * through the atomic check code to calculate new watermark values in the
  11991. * state object.
  11992. */
  11993. static void sanitize_watermarks(struct drm_device *dev)
  11994. {
  11995. struct drm_i915_private *dev_priv = to_i915(dev);
  11996. struct drm_atomic_state *state;
  11997. struct intel_atomic_state *intel_state;
  11998. struct drm_crtc *crtc;
  11999. struct drm_crtc_state *cstate;
  12000. struct drm_modeset_acquire_ctx ctx;
  12001. int ret;
  12002. int i;
  12003. /* Only supported on platforms that use atomic watermark design */
  12004. if (!dev_priv->display.optimize_watermarks)
  12005. return;
  12006. /*
  12007. * We need to hold connection_mutex before calling duplicate_state so
  12008. * that the connector loop is protected.
  12009. */
  12010. drm_modeset_acquire_init(&ctx, 0);
  12011. retry:
  12012. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12013. if (ret == -EDEADLK) {
  12014. drm_modeset_backoff(&ctx);
  12015. goto retry;
  12016. } else if (WARN_ON(ret)) {
  12017. goto fail;
  12018. }
  12019. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12020. if (WARN_ON(IS_ERR(state)))
  12021. goto fail;
  12022. intel_state = to_intel_atomic_state(state);
  12023. /*
  12024. * Hardware readout is the only time we don't want to calculate
  12025. * intermediate watermarks (since we don't trust the current
  12026. * watermarks).
  12027. */
  12028. if (!HAS_GMCH_DISPLAY(dev_priv))
  12029. intel_state->skip_intermediate_wm = true;
  12030. ret = intel_atomic_check(dev, state);
  12031. if (ret) {
  12032. /*
  12033. * If we fail here, it means that the hardware appears to be
  12034. * programmed in a way that shouldn't be possible, given our
  12035. * understanding of watermark requirements. This might mean a
  12036. * mistake in the hardware readout code or a mistake in the
  12037. * watermark calculations for a given platform. Raise a WARN
  12038. * so that this is noticeable.
  12039. *
  12040. * If this actually happens, we'll have to just leave the
  12041. * BIOS-programmed watermarks untouched and hope for the best.
  12042. */
  12043. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12044. goto put_state;
  12045. }
  12046. /* Write calculated watermark values back */
  12047. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12048. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12049. cs->wm.need_postvbl_update = true;
  12050. dev_priv->display.optimize_watermarks(intel_state, cs);
  12051. }
  12052. put_state:
  12053. drm_atomic_state_put(state);
  12054. fail:
  12055. drm_modeset_drop_locks(&ctx);
  12056. drm_modeset_acquire_fini(&ctx);
  12057. }
  12058. int intel_modeset_init(struct drm_device *dev)
  12059. {
  12060. struct drm_i915_private *dev_priv = to_i915(dev);
  12061. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12062. enum pipe pipe;
  12063. struct intel_crtc *crtc;
  12064. drm_mode_config_init(dev);
  12065. dev->mode_config.min_width = 0;
  12066. dev->mode_config.min_height = 0;
  12067. dev->mode_config.preferred_depth = 24;
  12068. dev->mode_config.prefer_shadow = 1;
  12069. dev->mode_config.allow_fb_modifiers = true;
  12070. dev->mode_config.funcs = &intel_mode_funcs;
  12071. init_llist_head(&dev_priv->atomic_helper.free_list);
  12072. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12073. intel_atomic_helper_free_state_worker);
  12074. intel_init_quirks(dev);
  12075. intel_init_pm(dev_priv);
  12076. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12077. return 0;
  12078. /*
  12079. * There may be no VBT; and if the BIOS enabled SSC we can
  12080. * just keep using it to avoid unnecessary flicker. Whereas if the
  12081. * BIOS isn't using it, don't assume it will work even if the VBT
  12082. * indicates as much.
  12083. */
  12084. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12085. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12086. DREF_SSC1_ENABLE);
  12087. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12088. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12089. bios_lvds_use_ssc ? "en" : "dis",
  12090. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12091. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12092. }
  12093. }
  12094. if (IS_GEN2(dev_priv)) {
  12095. dev->mode_config.max_width = 2048;
  12096. dev->mode_config.max_height = 2048;
  12097. } else if (IS_GEN3(dev_priv)) {
  12098. dev->mode_config.max_width = 4096;
  12099. dev->mode_config.max_height = 4096;
  12100. } else {
  12101. dev->mode_config.max_width = 8192;
  12102. dev->mode_config.max_height = 8192;
  12103. }
  12104. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12105. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12106. dev->mode_config.cursor_height = 1023;
  12107. } else if (IS_GEN2(dev_priv)) {
  12108. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12109. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12110. } else {
  12111. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12112. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12113. }
  12114. dev->mode_config.fb_base = ggtt->mappable_base;
  12115. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12116. INTEL_INFO(dev_priv)->num_pipes,
  12117. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12118. for_each_pipe(dev_priv, pipe) {
  12119. int ret;
  12120. ret = intel_crtc_init(dev_priv, pipe);
  12121. if (ret) {
  12122. drm_mode_config_cleanup(dev);
  12123. return ret;
  12124. }
  12125. }
  12126. intel_shared_dpll_init(dev);
  12127. intel_update_czclk(dev_priv);
  12128. intel_modeset_init_hw(dev);
  12129. if (dev_priv->max_cdclk_freq == 0)
  12130. intel_update_max_cdclk(dev_priv);
  12131. /* Just disable it once at startup */
  12132. i915_disable_vga(dev_priv);
  12133. intel_setup_outputs(dev_priv);
  12134. drm_modeset_lock_all(dev);
  12135. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12136. drm_modeset_unlock_all(dev);
  12137. for_each_intel_crtc(dev, crtc) {
  12138. struct intel_initial_plane_config plane_config = {};
  12139. if (!crtc->active)
  12140. continue;
  12141. /*
  12142. * Note that reserving the BIOS fb up front prevents us
  12143. * from stuffing other stolen allocations like the ring
  12144. * on top. This prevents some ugliness at boot time, and
  12145. * can even allow for smooth boot transitions if the BIOS
  12146. * fb is large enough for the active pipe configuration.
  12147. */
  12148. dev_priv->display.get_initial_plane_config(crtc,
  12149. &plane_config);
  12150. /*
  12151. * If the fb is shared between multiple heads, we'll
  12152. * just get the first one.
  12153. */
  12154. intel_find_initial_plane_obj(crtc, &plane_config);
  12155. }
  12156. /*
  12157. * Make sure hardware watermarks really match the state we read out.
  12158. * Note that we need to do this after reconstructing the BIOS fb's
  12159. * since the watermark calculation done here will use pstate->fb.
  12160. */
  12161. if (!HAS_GMCH_DISPLAY(dev_priv))
  12162. sanitize_watermarks(dev);
  12163. return 0;
  12164. }
  12165. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12166. {
  12167. /* 640x480@60Hz, ~25175 kHz */
  12168. struct dpll clock = {
  12169. .m1 = 18,
  12170. .m2 = 7,
  12171. .p1 = 13,
  12172. .p2 = 4,
  12173. .n = 2,
  12174. };
  12175. u32 dpll, fp;
  12176. int i;
  12177. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12178. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12179. pipe_name(pipe), clock.vco, clock.dot);
  12180. fp = i9xx_dpll_compute_fp(&clock);
  12181. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12182. DPLL_VGA_MODE_DIS |
  12183. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12184. PLL_P2_DIVIDE_BY_4 |
  12185. PLL_REF_INPUT_DREFCLK |
  12186. DPLL_VCO_ENABLE;
  12187. I915_WRITE(FP0(pipe), fp);
  12188. I915_WRITE(FP1(pipe), fp);
  12189. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12190. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12191. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12192. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12193. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12194. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12195. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12196. /*
  12197. * Apparently we need to have VGA mode enabled prior to changing
  12198. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12199. * dividers, even though the register value does change.
  12200. */
  12201. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12202. I915_WRITE(DPLL(pipe), dpll);
  12203. /* Wait for the clocks to stabilize. */
  12204. POSTING_READ(DPLL(pipe));
  12205. udelay(150);
  12206. /* The pixel multiplier can only be updated once the
  12207. * DPLL is enabled and the clocks are stable.
  12208. *
  12209. * So write it again.
  12210. */
  12211. I915_WRITE(DPLL(pipe), dpll);
  12212. /* We do this three times for luck */
  12213. for (i = 0; i < 3 ; i++) {
  12214. I915_WRITE(DPLL(pipe), dpll);
  12215. POSTING_READ(DPLL(pipe));
  12216. udelay(150); /* wait for warmup */
  12217. }
  12218. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12219. POSTING_READ(PIPECONF(pipe));
  12220. }
  12221. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12222. {
  12223. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12224. pipe_name(pipe));
  12225. assert_plane_disabled(dev_priv, PLANE_A);
  12226. assert_plane_disabled(dev_priv, PLANE_B);
  12227. I915_WRITE(PIPECONF(pipe), 0);
  12228. POSTING_READ(PIPECONF(pipe));
  12229. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  12230. DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
  12231. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12232. POSTING_READ(DPLL(pipe));
  12233. }
  12234. static bool
  12235. intel_check_plane_mapping(struct intel_crtc *crtc)
  12236. {
  12237. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12238. u32 val;
  12239. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12240. return true;
  12241. val = I915_READ(DSPCNTR(!crtc->plane));
  12242. if ((val & DISPLAY_PLANE_ENABLE) &&
  12243. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12244. return false;
  12245. return true;
  12246. }
  12247. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12248. {
  12249. struct drm_device *dev = crtc->base.dev;
  12250. struct intel_encoder *encoder;
  12251. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12252. return true;
  12253. return false;
  12254. }
  12255. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12256. {
  12257. struct drm_device *dev = encoder->base.dev;
  12258. struct intel_connector *connector;
  12259. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12260. return connector;
  12261. return NULL;
  12262. }
  12263. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12264. enum transcoder pch_transcoder)
  12265. {
  12266. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12267. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12268. }
  12269. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12270. struct drm_modeset_acquire_ctx *ctx)
  12271. {
  12272. struct drm_device *dev = crtc->base.dev;
  12273. struct drm_i915_private *dev_priv = to_i915(dev);
  12274. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12275. /* Clear any frame start delays used for debugging left by the BIOS */
  12276. if (!transcoder_is_dsi(cpu_transcoder)) {
  12277. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12278. I915_WRITE(reg,
  12279. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12280. }
  12281. /* restore vblank interrupts to correct state */
  12282. drm_crtc_vblank_reset(&crtc->base);
  12283. if (crtc->active) {
  12284. struct intel_plane *plane;
  12285. drm_crtc_vblank_on(&crtc->base);
  12286. /* Disable everything but the primary plane */
  12287. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12288. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12289. continue;
  12290. trace_intel_disable_plane(&plane->base, crtc);
  12291. plane->disable_plane(plane, crtc);
  12292. }
  12293. }
  12294. /* We need to sanitize the plane -> pipe mapping first because this will
  12295. * disable the crtc (and hence change the state) if it is wrong. Note
  12296. * that gen4+ has a fixed plane -> pipe mapping. */
  12297. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12298. bool plane;
  12299. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12300. crtc->base.base.id, crtc->base.name);
  12301. /* Pipe has the wrong plane attached and the plane is active.
  12302. * Temporarily change the plane mapping and disable everything
  12303. * ... */
  12304. plane = crtc->plane;
  12305. crtc->base.primary->state->visible = true;
  12306. crtc->plane = !plane;
  12307. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12308. crtc->plane = plane;
  12309. }
  12310. /* Adjust the state of the output pipe according to whether we
  12311. * have active connectors/encoders. */
  12312. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12313. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12314. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12315. /*
  12316. * We start out with underrun reporting disabled to avoid races.
  12317. * For correct bookkeeping mark this on active crtcs.
  12318. *
  12319. * Also on gmch platforms we dont have any hardware bits to
  12320. * disable the underrun reporting. Which means we need to start
  12321. * out with underrun reporting disabled also on inactive pipes,
  12322. * since otherwise we'll complain about the garbage we read when
  12323. * e.g. coming up after runtime pm.
  12324. *
  12325. * No protection against concurrent access is required - at
  12326. * worst a fifo underrun happens which also sets this to false.
  12327. */
  12328. crtc->cpu_fifo_underrun_disabled = true;
  12329. /*
  12330. * We track the PCH trancoder underrun reporting state
  12331. * within the crtc. With crtc for pipe A housing the underrun
  12332. * reporting state for PCH transcoder A, crtc for pipe B housing
  12333. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12334. * and marking underrun reporting as disabled for the non-existing
  12335. * PCH transcoders B and C would prevent enabling the south
  12336. * error interrupt (see cpt_can_enable_serr_int()).
  12337. */
  12338. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12339. crtc->pch_fifo_underrun_disabled = true;
  12340. }
  12341. }
  12342. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12343. {
  12344. struct intel_connector *connector;
  12345. /* We need to check both for a crtc link (meaning that the
  12346. * encoder is active and trying to read from a pipe) and the
  12347. * pipe itself being active. */
  12348. bool has_active_crtc = encoder->base.crtc &&
  12349. to_intel_crtc(encoder->base.crtc)->active;
  12350. connector = intel_encoder_find_connector(encoder);
  12351. if (connector && !has_active_crtc) {
  12352. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12353. encoder->base.base.id,
  12354. encoder->base.name);
  12355. /* Connector is active, but has no active pipe. This is
  12356. * fallout from our resume register restoring. Disable
  12357. * the encoder manually again. */
  12358. if (encoder->base.crtc) {
  12359. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12360. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12361. encoder->base.base.id,
  12362. encoder->base.name);
  12363. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12364. if (encoder->post_disable)
  12365. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12366. }
  12367. encoder->base.crtc = NULL;
  12368. /* Inconsistent output/port/pipe state happens presumably due to
  12369. * a bug in one of the get_hw_state functions. Or someplace else
  12370. * in our code, like the register restore mess on resume. Clamp
  12371. * things to off as a safer default. */
  12372. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12373. connector->base.encoder = NULL;
  12374. }
  12375. /* Enabled encoders without active connectors will be fixed in
  12376. * the crtc fixup. */
  12377. }
  12378. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12379. {
  12380. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12381. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12382. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12383. i915_disable_vga(dev_priv);
  12384. }
  12385. }
  12386. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12387. {
  12388. /* This function can be called both from intel_modeset_setup_hw_state or
  12389. * at a very early point in our resume sequence, where the power well
  12390. * structures are not yet restored. Since this function is at a very
  12391. * paranoid "someone might have enabled VGA while we were not looking"
  12392. * level, just check if the power well is enabled instead of trying to
  12393. * follow the "don't touch the power well if we don't need it" policy
  12394. * the rest of the driver uses. */
  12395. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12396. return;
  12397. i915_redisable_vga_power_on(dev_priv);
  12398. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12399. }
  12400. static bool primary_get_hw_state(struct intel_plane *plane)
  12401. {
  12402. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12403. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12404. }
  12405. /* FIXME read out full plane state for all planes */
  12406. static void readout_plane_state(struct intel_crtc *crtc)
  12407. {
  12408. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12409. bool visible;
  12410. visible = crtc->active && primary_get_hw_state(primary);
  12411. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12412. to_intel_plane_state(primary->base.state),
  12413. visible);
  12414. }
  12415. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12416. {
  12417. struct drm_i915_private *dev_priv = to_i915(dev);
  12418. enum pipe pipe;
  12419. struct intel_crtc *crtc;
  12420. struct intel_encoder *encoder;
  12421. struct intel_connector *connector;
  12422. struct drm_connector_list_iter conn_iter;
  12423. int i;
  12424. dev_priv->active_crtcs = 0;
  12425. for_each_intel_crtc(dev, crtc) {
  12426. struct intel_crtc_state *crtc_state =
  12427. to_intel_crtc_state(crtc->base.state);
  12428. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12429. memset(crtc_state, 0, sizeof(*crtc_state));
  12430. crtc_state->base.crtc = &crtc->base;
  12431. crtc_state->base.active = crtc_state->base.enable =
  12432. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12433. crtc->base.enabled = crtc_state->base.enable;
  12434. crtc->active = crtc_state->base.active;
  12435. if (crtc_state->base.active)
  12436. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12437. readout_plane_state(crtc);
  12438. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12439. crtc->base.base.id, crtc->base.name,
  12440. enableddisabled(crtc_state->base.active));
  12441. }
  12442. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12443. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12444. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12445. &pll->state.hw_state);
  12446. pll->state.crtc_mask = 0;
  12447. for_each_intel_crtc(dev, crtc) {
  12448. struct intel_crtc_state *crtc_state =
  12449. to_intel_crtc_state(crtc->base.state);
  12450. if (crtc_state->base.active &&
  12451. crtc_state->shared_dpll == pll)
  12452. pll->state.crtc_mask |= 1 << crtc->pipe;
  12453. }
  12454. pll->active_mask = pll->state.crtc_mask;
  12455. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12456. pll->name, pll->state.crtc_mask, pll->on);
  12457. }
  12458. for_each_intel_encoder(dev, encoder) {
  12459. pipe = 0;
  12460. if (encoder->get_hw_state(encoder, &pipe)) {
  12461. struct intel_crtc_state *crtc_state;
  12462. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12463. crtc_state = to_intel_crtc_state(crtc->base.state);
  12464. encoder->base.crtc = &crtc->base;
  12465. crtc_state->output_types |= 1 << encoder->type;
  12466. encoder->get_config(encoder, crtc_state);
  12467. } else {
  12468. encoder->base.crtc = NULL;
  12469. }
  12470. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12471. encoder->base.base.id, encoder->base.name,
  12472. enableddisabled(encoder->base.crtc),
  12473. pipe_name(pipe));
  12474. }
  12475. drm_connector_list_iter_begin(dev, &conn_iter);
  12476. for_each_intel_connector_iter(connector, &conn_iter) {
  12477. if (connector->get_hw_state(connector)) {
  12478. connector->base.dpms = DRM_MODE_DPMS_ON;
  12479. encoder = connector->encoder;
  12480. connector->base.encoder = &encoder->base;
  12481. if (encoder->base.crtc &&
  12482. encoder->base.crtc->state->active) {
  12483. /*
  12484. * This has to be done during hardware readout
  12485. * because anything calling .crtc_disable may
  12486. * rely on the connector_mask being accurate.
  12487. */
  12488. encoder->base.crtc->state->connector_mask |=
  12489. 1 << drm_connector_index(&connector->base);
  12490. encoder->base.crtc->state->encoder_mask |=
  12491. 1 << drm_encoder_index(&encoder->base);
  12492. }
  12493. } else {
  12494. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12495. connector->base.encoder = NULL;
  12496. }
  12497. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12498. connector->base.base.id, connector->base.name,
  12499. enableddisabled(connector->base.encoder));
  12500. }
  12501. drm_connector_list_iter_end(&conn_iter);
  12502. for_each_intel_crtc(dev, crtc) {
  12503. struct intel_crtc_state *crtc_state =
  12504. to_intel_crtc_state(crtc->base.state);
  12505. int pixclk = 0;
  12506. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12507. if (crtc_state->base.active) {
  12508. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12509. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12510. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12511. /*
  12512. * The initial mode needs to be set in order to keep
  12513. * the atomic core happy. It wants a valid mode if the
  12514. * crtc's enabled, so we do the above call.
  12515. *
  12516. * But we don't set all the derived state fully, hence
  12517. * set a flag to indicate that a full recalculation is
  12518. * needed on the next commit.
  12519. */
  12520. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12521. intel_crtc_compute_pixel_rate(crtc_state);
  12522. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12523. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12524. pixclk = crtc_state->pixel_rate;
  12525. else
  12526. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12527. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12528. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12529. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12530. drm_calc_timestamping_constants(&crtc->base,
  12531. &crtc_state->base.adjusted_mode);
  12532. update_scanline_offset(crtc);
  12533. }
  12534. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12535. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12536. }
  12537. }
  12538. static void
  12539. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12540. {
  12541. struct intel_encoder *encoder;
  12542. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12543. u64 get_domains;
  12544. enum intel_display_power_domain domain;
  12545. if (!encoder->get_power_domains)
  12546. continue;
  12547. get_domains = encoder->get_power_domains(encoder);
  12548. for_each_power_domain(domain, get_domains)
  12549. intel_display_power_get(dev_priv, domain);
  12550. }
  12551. }
  12552. /* Scan out the current hw modeset state,
  12553. * and sanitizes it to the current state
  12554. */
  12555. static void
  12556. intel_modeset_setup_hw_state(struct drm_device *dev,
  12557. struct drm_modeset_acquire_ctx *ctx)
  12558. {
  12559. struct drm_i915_private *dev_priv = to_i915(dev);
  12560. enum pipe pipe;
  12561. struct intel_crtc *crtc;
  12562. struct intel_encoder *encoder;
  12563. int i;
  12564. intel_modeset_readout_hw_state(dev);
  12565. /* HW state is read out, now we need to sanitize this mess. */
  12566. get_encoder_power_domains(dev_priv);
  12567. for_each_intel_encoder(dev, encoder) {
  12568. intel_sanitize_encoder(encoder);
  12569. }
  12570. for_each_pipe(dev_priv, pipe) {
  12571. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12572. intel_sanitize_crtc(crtc, ctx);
  12573. intel_dump_pipe_config(crtc, crtc->config,
  12574. "[setup_hw_state]");
  12575. }
  12576. intel_modeset_update_connector_atomic_state(dev);
  12577. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12578. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12579. if (!pll->on || pll->active_mask)
  12580. continue;
  12581. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12582. pll->funcs.disable(dev_priv, pll);
  12583. pll->on = false;
  12584. }
  12585. if (IS_G4X(dev_priv)) {
  12586. g4x_wm_get_hw_state(dev);
  12587. g4x_wm_sanitize(dev_priv);
  12588. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12589. vlv_wm_get_hw_state(dev);
  12590. vlv_wm_sanitize(dev_priv);
  12591. } else if (IS_GEN9(dev_priv)) {
  12592. skl_wm_get_hw_state(dev);
  12593. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12594. ilk_wm_get_hw_state(dev);
  12595. }
  12596. for_each_intel_crtc(dev, crtc) {
  12597. u64 put_domains;
  12598. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12599. if (WARN_ON(put_domains))
  12600. modeset_put_power_domains(dev_priv, put_domains);
  12601. }
  12602. intel_display_set_init_power(dev_priv, false);
  12603. intel_power_domains_verify_state(dev_priv);
  12604. intel_fbc_init_pipe_state(dev_priv);
  12605. }
  12606. void intel_display_resume(struct drm_device *dev)
  12607. {
  12608. struct drm_i915_private *dev_priv = to_i915(dev);
  12609. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12610. struct drm_modeset_acquire_ctx ctx;
  12611. int ret;
  12612. dev_priv->modeset_restore_state = NULL;
  12613. if (state)
  12614. state->acquire_ctx = &ctx;
  12615. drm_modeset_acquire_init(&ctx, 0);
  12616. while (1) {
  12617. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12618. if (ret != -EDEADLK)
  12619. break;
  12620. drm_modeset_backoff(&ctx);
  12621. }
  12622. if (!ret)
  12623. ret = __intel_display_resume(dev, state, &ctx);
  12624. drm_modeset_drop_locks(&ctx);
  12625. drm_modeset_acquire_fini(&ctx);
  12626. if (ret)
  12627. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12628. if (state)
  12629. drm_atomic_state_put(state);
  12630. }
  12631. void intel_modeset_gem_init(struct drm_device *dev)
  12632. {
  12633. struct drm_i915_private *dev_priv = to_i915(dev);
  12634. intel_init_gt_powersave(dev_priv);
  12635. intel_setup_overlay(dev_priv);
  12636. }
  12637. int intel_connector_register(struct drm_connector *connector)
  12638. {
  12639. struct intel_connector *intel_connector = to_intel_connector(connector);
  12640. int ret;
  12641. ret = intel_backlight_device_register(intel_connector);
  12642. if (ret)
  12643. goto err;
  12644. return 0;
  12645. err:
  12646. return ret;
  12647. }
  12648. void intel_connector_unregister(struct drm_connector *connector)
  12649. {
  12650. struct intel_connector *intel_connector = to_intel_connector(connector);
  12651. intel_backlight_device_unregister(intel_connector);
  12652. intel_panel_destroy_backlight(connector);
  12653. }
  12654. void intel_modeset_cleanup(struct drm_device *dev)
  12655. {
  12656. struct drm_i915_private *dev_priv = to_i915(dev);
  12657. flush_work(&dev_priv->atomic_helper.free_work);
  12658. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12659. intel_disable_gt_powersave(dev_priv);
  12660. /*
  12661. * Interrupts and polling as the first thing to avoid creating havoc.
  12662. * Too much stuff here (turning of connectors, ...) would
  12663. * experience fancy races otherwise.
  12664. */
  12665. intel_irq_uninstall(dev_priv);
  12666. /*
  12667. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12668. * poll handlers. Hence disable polling after hpd handling is shut down.
  12669. */
  12670. drm_kms_helper_poll_fini(dev);
  12671. /* poll work can call into fbdev, hence clean that up afterwards */
  12672. intel_fbdev_fini(dev_priv);
  12673. intel_unregister_dsm_handler();
  12674. intel_fbc_global_disable(dev_priv);
  12675. /* flush any delayed tasks or pending work */
  12676. flush_scheduled_work();
  12677. drm_mode_config_cleanup(dev);
  12678. intel_cleanup_overlay(dev_priv);
  12679. intel_cleanup_gt_powersave(dev_priv);
  12680. intel_teardown_gmbus(dev_priv);
  12681. }
  12682. void intel_connector_attach_encoder(struct intel_connector *connector,
  12683. struct intel_encoder *encoder)
  12684. {
  12685. connector->encoder = encoder;
  12686. drm_mode_connector_attach_encoder(&connector->base,
  12687. &encoder->base);
  12688. }
  12689. /*
  12690. * set vga decode state - true == enable VGA decode
  12691. */
  12692. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12693. {
  12694. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12695. u16 gmch_ctrl;
  12696. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12697. DRM_ERROR("failed to read control word\n");
  12698. return -EIO;
  12699. }
  12700. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12701. return 0;
  12702. if (state)
  12703. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12704. else
  12705. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12706. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12707. DRM_ERROR("failed to write control word\n");
  12708. return -EIO;
  12709. }
  12710. return 0;
  12711. }
  12712. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12713. struct intel_display_error_state {
  12714. u32 power_well_driver;
  12715. int num_transcoders;
  12716. struct intel_cursor_error_state {
  12717. u32 control;
  12718. u32 position;
  12719. u32 base;
  12720. u32 size;
  12721. } cursor[I915_MAX_PIPES];
  12722. struct intel_pipe_error_state {
  12723. bool power_domain_on;
  12724. u32 source;
  12725. u32 stat;
  12726. } pipe[I915_MAX_PIPES];
  12727. struct intel_plane_error_state {
  12728. u32 control;
  12729. u32 stride;
  12730. u32 size;
  12731. u32 pos;
  12732. u32 addr;
  12733. u32 surface;
  12734. u32 tile_offset;
  12735. } plane[I915_MAX_PIPES];
  12736. struct intel_transcoder_error_state {
  12737. bool power_domain_on;
  12738. enum transcoder cpu_transcoder;
  12739. u32 conf;
  12740. u32 htotal;
  12741. u32 hblank;
  12742. u32 hsync;
  12743. u32 vtotal;
  12744. u32 vblank;
  12745. u32 vsync;
  12746. } transcoder[4];
  12747. };
  12748. struct intel_display_error_state *
  12749. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12750. {
  12751. struct intel_display_error_state *error;
  12752. int transcoders[] = {
  12753. TRANSCODER_A,
  12754. TRANSCODER_B,
  12755. TRANSCODER_C,
  12756. TRANSCODER_EDP,
  12757. };
  12758. int i;
  12759. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12760. return NULL;
  12761. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12762. if (error == NULL)
  12763. return NULL;
  12764. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12765. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12766. for_each_pipe(dev_priv, i) {
  12767. error->pipe[i].power_domain_on =
  12768. __intel_display_power_is_enabled(dev_priv,
  12769. POWER_DOMAIN_PIPE(i));
  12770. if (!error->pipe[i].power_domain_on)
  12771. continue;
  12772. error->cursor[i].control = I915_READ(CURCNTR(i));
  12773. error->cursor[i].position = I915_READ(CURPOS(i));
  12774. error->cursor[i].base = I915_READ(CURBASE(i));
  12775. error->plane[i].control = I915_READ(DSPCNTR(i));
  12776. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12777. if (INTEL_GEN(dev_priv) <= 3) {
  12778. error->plane[i].size = I915_READ(DSPSIZE(i));
  12779. error->plane[i].pos = I915_READ(DSPPOS(i));
  12780. }
  12781. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12782. error->plane[i].addr = I915_READ(DSPADDR(i));
  12783. if (INTEL_GEN(dev_priv) >= 4) {
  12784. error->plane[i].surface = I915_READ(DSPSURF(i));
  12785. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12786. }
  12787. error->pipe[i].source = I915_READ(PIPESRC(i));
  12788. if (HAS_GMCH_DISPLAY(dev_priv))
  12789. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12790. }
  12791. /* Note: this does not include DSI transcoders. */
  12792. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12793. if (HAS_DDI(dev_priv))
  12794. error->num_transcoders++; /* Account for eDP. */
  12795. for (i = 0; i < error->num_transcoders; i++) {
  12796. enum transcoder cpu_transcoder = transcoders[i];
  12797. error->transcoder[i].power_domain_on =
  12798. __intel_display_power_is_enabled(dev_priv,
  12799. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12800. if (!error->transcoder[i].power_domain_on)
  12801. continue;
  12802. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12803. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12804. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12805. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12806. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12807. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12808. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12809. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12810. }
  12811. return error;
  12812. }
  12813. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12814. void
  12815. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12816. struct intel_display_error_state *error)
  12817. {
  12818. struct drm_i915_private *dev_priv = m->i915;
  12819. int i;
  12820. if (!error)
  12821. return;
  12822. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12823. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12824. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12825. error->power_well_driver);
  12826. for_each_pipe(dev_priv, i) {
  12827. err_printf(m, "Pipe [%d]:\n", i);
  12828. err_printf(m, " Power: %s\n",
  12829. onoff(error->pipe[i].power_domain_on));
  12830. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12831. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12832. err_printf(m, "Plane [%d]:\n", i);
  12833. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12834. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12835. if (INTEL_GEN(dev_priv) <= 3) {
  12836. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12837. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12838. }
  12839. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12840. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12841. if (INTEL_GEN(dev_priv) >= 4) {
  12842. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12843. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12844. }
  12845. err_printf(m, "Cursor [%d]:\n", i);
  12846. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12847. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12848. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12849. }
  12850. for (i = 0; i < error->num_transcoders; i++) {
  12851. err_printf(m, "CPU transcoder: %s\n",
  12852. transcoder_name(error->transcoder[i].cpu_transcoder));
  12853. err_printf(m, " Power: %s\n",
  12854. onoff(error->transcoder[i].power_domain_on));
  12855. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12856. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12857. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12858. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12859. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12860. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12861. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12862. }
  12863. }
  12864. #endif